001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016
017package com.echothree.control.user.campaign.server.command;
018
019import com.echothree.control.user.campaign.common.form.CreateUserVisitCampaignForm;
020import com.echothree.model.control.campaign.server.control.CampaignControl;
021import com.echothree.model.data.campaign.server.entity.Campaign;
022import com.echothree.model.data.campaign.server.entity.CampaignContent;
023import com.echothree.model.data.campaign.server.entity.CampaignMedium;
024import com.echothree.model.data.campaign.server.entity.CampaignSource;
025import com.echothree.model.data.campaign.server.entity.CampaignTerm;
026import com.echothree.util.common.exception.PersistenceDatabaseException;
027import com.echothree.util.common.message.ExecutionErrors;
028import com.echothree.util.common.validation.FieldDefinition;
029import com.echothree.util.common.validation.FieldType;
030import com.echothree.util.common.command.BaseResult;
031import com.echothree.util.server.control.BaseSimpleCommand;
032import com.echothree.util.server.persistence.PersistenceUtils;
033import java.util.List;
034import javax.enterprise.context.Dependent;
035import javax.inject.Inject;
036
037@Dependent
038public class CreateUserVisitCampaignCommand
039        extends BaseSimpleCommand<CreateUserVisitCampaignForm> {
040    
041    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
042    
043    static {
044        FORM_FIELD_DEFINITIONS = List.of(
045                new FieldDefinition("CampaignValue", FieldType.STRING, false, null, null),
046                new FieldDefinition("CampaignSourceValue", FieldType.STRING, false, null, null),
047                new FieldDefinition("CampaignMediumValue", FieldType.STRING, false, null, null),
048                new FieldDefinition("CampaignTermValue", FieldType.STRING, false, null, null),
049                new FieldDefinition("CampaignContentValue", FieldType.STRING, false, null, null)
050        );
051    }
052
053    @Inject
054    CampaignControl campaignControl;
055
056    
057    /** Creates a new instance of CreateCampaignCommand */
058    public CreateUserVisitCampaignCommand() {
059        super(null, FORM_FIELD_DEFINITIONS, false);
060    }
061    
062    @Override
063    protected BaseResult execute() {
064        var campaignValue = form.getCampaignValue();
065        var campaignSourceValue = form.getCampaignSourceValue();
066        var campaignMediumValue = form.getCampaignMediumValue();
067        var campaignTermValue = form.getCampaignTermValue();
068        var campaignContentValue = form.getCampaignContentValue();
069        var parameterCount = (campaignValue == null ? 0 : 1) + (campaignSourceValue == null ? 0 : 1) + (campaignMediumValue == null ? 0 : 1)
070                + (campaignTermValue == null ? 0 : 1) + (campaignContentValue == null ? 0 : 1);
071
072        if(parameterCount > 0) {
073            Campaign campaign = null;
074            CampaignSource campaignSource = null;
075            CampaignMedium campaignMedium = null;
076            CampaignTerm campaignTerm = null;
077            CampaignContent campaignContent = null;
078            
079            if(campaignValue != null) {
080                campaign = campaignControl.getCampaignByValue(campaignValue);
081                
082                if(campaign == null) {
083                    try {
084                        campaign = campaignControl.createCampaign(campaignValue, false, 0, getPartyPK());
085                    } catch(PersistenceDatabaseException pde) {
086                        if(PersistenceUtils.getInstance().isIntegrityConstraintViolation(pde)) {
087                            campaign = campaignControl.getCampaignByValue(campaignValue);
088                            pde = null;
089                        }
090
091                        if(pde != null) {
092                            throw pde;
093                        }
094                    }
095                }
096            }
097            
098            if(campaignSourceValue != null) {
099                campaignSource = campaignControl.getCampaignSourceByValue(campaignSourceValue);
100                
101                if(campaignSource == null) {
102                    try {
103                        campaignSource = campaignControl.createCampaignSource(campaignSourceValue, false, 0, getPartyPK());
104                    } catch(PersistenceDatabaseException pde) {
105                        if(PersistenceUtils.getInstance().isIntegrityConstraintViolation(pde)) {
106                            campaignSource = campaignControl.getCampaignSourceByValue(campaignSourceValue);
107                            pde = null;
108                        }
109
110                        if(pde != null) {
111                            throw pde;
112                        }
113                    }
114                }
115            }
116            
117            if(campaignMediumValue != null) {
118                campaignMedium = campaignControl.getCampaignMediumByValue(campaignMediumValue);
119                
120                if(campaignMedium == null) {
121                    try {
122                        campaignMedium = campaignControl.createCampaignMedium(campaignMediumValue, false, 0, getPartyPK());
123                    } catch(PersistenceDatabaseException pde) {
124                        if(PersistenceUtils.getInstance().isIntegrityConstraintViolation(pde)) {
125                            campaignMedium = campaignControl.getCampaignMediumByValue(campaignMediumValue);
126                            pde = null;
127                        }
128
129                        if(pde != null) {
130                            throw pde;
131                        }
132                    }
133                }
134            }
135            
136            if(campaignTermValue != null) {
137                campaignTerm = campaignControl.getCampaignTermByValue(campaignTermValue);
138                
139                if(campaignTerm == null) {
140                    try {
141                        campaignTerm = campaignControl.createCampaignTerm(campaignTermValue, false, 0, getPartyPK());
142                    } catch(PersistenceDatabaseException pde) {
143                        if(PersistenceUtils.getInstance().isIntegrityConstraintViolation(pde)) {
144                            campaignTerm = campaignControl.getCampaignTermByValue(campaignTermValue);
145                            pde = null;
146                        }
147
148                        if(pde != null) {
149                            throw pde;
150                        }
151                    }
152                }
153            }
154            
155            if(campaignContentValue != null) {
156                campaignContent = campaignControl.getCampaignContentByValue(campaignContentValue);
157                
158                if(campaignContent == null) {
159                    try {
160                        campaignContent = campaignControl.createCampaignContent(campaignContentValue, false, 0, getPartyPK());
161                    } catch(PersistenceDatabaseException pde) {
162                        if(PersistenceUtils.getInstance().isIntegrityConstraintViolation(pde)) {
163                            campaignContent = campaignControl.getCampaignContentByValue(campaignContentValue);
164                            pde = null;
165                        }
166
167                        if(pde != null) {
168                            throw pde;
169                        }
170                    }
171                }
172            }
173            
174            campaignControl.createUserVisitCampaign(getUserVisit(), session.getStartTime(), campaign, campaignSource, campaignMedium, campaignTerm,
175                    campaignContent);
176        } else {
177            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
178        }
179        
180        return null;
181    }
182    
183}