001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.model.data.user.common.pk.UserVisitPK;
027import com.echothree.util.common.exception.PersistenceDatabaseException;
028import com.echothree.util.common.message.ExecutionErrors;
029import com.echothree.util.common.validation.FieldDefinition;
030import com.echothree.util.common.validation.FieldType;
031import com.echothree.util.common.command.BaseResult;
032import com.echothree.util.server.control.BaseSimpleCommand;
033import com.echothree.util.server.persistence.PersistenceUtils;
034import com.echothree.util.server.persistence.Session;
035import java.util.Arrays;
036import java.util.Collections;
037import java.util.List;
038import javax.enterprise.context.RequestScoped;
039
040@RequestScoped
041public class CreateUserVisitCampaignCommand
042        extends BaseSimpleCommand<CreateUserVisitCampaignForm> {
043    
044    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
045    
046    static {
047        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
048                new FieldDefinition("CampaignValue", FieldType.STRING, false, null, null),
049                new FieldDefinition("CampaignSourceValue", FieldType.STRING, false, null, null),
050                new FieldDefinition("CampaignMediumValue", FieldType.STRING, false, null, null),
051                new FieldDefinition("CampaignTermValue", FieldType.STRING, false, null, null),
052                new FieldDefinition("CampaignContentValue", FieldType.STRING, false, null, null)
053                ));
054    }
055    
056    /** Creates a new instance of CreateCampaignCommand */
057    public CreateUserVisitCampaignCommand() {
058        super(null, FORM_FIELD_DEFINITIONS, false);
059    }
060    
061    @Override
062    protected BaseResult execute() {
063        var campaignValue = form.getCampaignValue();
064        var campaignSourceValue = form.getCampaignSourceValue();
065        var campaignMediumValue = form.getCampaignMediumValue();
066        var campaignTermValue = form.getCampaignTermValue();
067        var campaignContentValue = form.getCampaignContentValue();
068        var parameterCount = (campaignValue == null ? 0 : 1) + (campaignSourceValue == null ? 0 : 1) + (campaignMediumValue == null ? 0 : 1)
069                + (campaignTermValue == null ? 0 : 1) + (campaignContentValue == null ? 0 : 1);
070
071        if(parameterCount > 0) {
072            var campaignControl = Session.getModelController(CampaignControl.class);
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.START_TIME_LONG, campaign, campaignSource, campaignMedium, campaignTerm,
175                    campaignContent);
176        } else {
177            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
178        }
179        
180        return null;
181    }
182    
183}