LogoLogo
timvero.comMaven Repository
  • timveroOS SDK guide
  • timveroOS how-to
  • timveroOS admin-side setup
  • Campaigns in timveroOS
    • 7.11
      • Business Context
      • Introduction
      • Core Concept
        • Entity Diagram
        • Lifecycle Diagram
        • Expression
      • Design and Development of the Campaign Model
        • Campaign model
        • Campaign repository
        • Campaign forms
        • Campaign сontroller
        • UI elements
        • Campaign actions
      • Design and Development of the CampaignExecution Model
        • CampaignExecution model
        • CampaignExecution repository
        • CampaignExecution controller
        • UI elements
      • Expression
      • Services
        • CampaignService
        • CampaignExecutionService
      • Campaign Processing
        • Producer
        • Consumer
      • Post-processing
        • Checker
Powered by GitBook
On this page

Was this helpful?

  1. Campaigns in timveroOS
  2. 7.11
  3. Services

CampaignService

CampaignService

The CampaignService is responsible for managing campaigns — including manual launches and status transitions. It acts as a facade for initiating campaign execution, including the creation of CampaignExecution.


🔧 Method: startCampaign(Long campaignId)

@Service
public class CampaignService {

    @Autowired
    private CampaignRepository campaignRepository;
    @Autowired
    private TransactionTemplateBuilder ttb;
    @Autowired
    private CampaignExecutionService campaignExecutionService;

    @Transactional(rollbackFor = Throwable.class)
    public void startCampaign(Long campaignId) {
        Campaign campaign = campaignRepository.getSync(campaignId);
        Assert.isTrue(campaign.isActive(), "Campaign is not active");
    
        if (campaign.getExecutionType() == CampaignExecutionType.AUTOMATIC) {
            if (campaign.getAutomaticCampaignStatus() != AutomaticCampaignStatus.SCHEDULED) {
                campaign.setAutomaticCampaignStatus(AutomaticCampaignStatus.SCHEDULED);
            }
        } else if (campaign.isNotInExecution()) {
            CampaignExecution execution = campaignExecutionService.createExecution(campaign);
            TransactionUtils.afterTransaction(() -> {
                ttb.requiresNew().executeWithoutResult(status -> 
                    campaignExecutionService.runExecution(execution.getId())
                );
            });
        }
    }
}

📌 Behavior

  • Retrieves the Campaign using getSync(...) — ensures the entity is up-to-date and synchronized

  • Verifies that the campaign is active

  • If the campaign is automatic, it sets the status to SCHEDULED (if not already)

  • If it's a manual campaign and no execution is currently IN_PROGRESS, it:

    • Creates a new CampaignExecution

    • After the current transaction commits (TransactionUtils.afterTransaction), it launches the execution in a new transaction


🔒 Transactional Handling

The entire method is wrapped with @Transactional(rollbackFor = Throwable.class) and uses TransactionTemplateBuilder to ensure safe execution in an isolated transaction.

PreviousServicesNextCampaignExecutionService

Last updated 1 month ago

Was this helpful?