001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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;
038
039public class CreateUserVisitCampaignCommand
040        extends BaseSimpleCommand<CreateUserVisitCampaignForm> {
041    
042    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
043    
044    static {
045        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
046                new FieldDefinition("CampaignValue", FieldType.STRING, false, null, null),
047                new FieldDefinition("CampaignSourceValue", FieldType.STRING, false, null, null),
048                new FieldDefinition("CampaignMediumValue", FieldType.STRING, false, null, null),
049                new FieldDefinition("CampaignTermValue", FieldType.STRING, false, null, null),
050                new FieldDefinition("CampaignContentValue", FieldType.STRING, false, null, null)
051                ));
052    }
053    
054    /** Creates a new instance of CreateCampaignCommand */
055    public CreateUserVisitCampaignCommand(UserVisitPK userVisitPK, CreateUserVisitCampaignForm form) {
056        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, false);
057    }
058    
059    @Override
060    protected BaseResult execute() {
061        String campaignValue = form.getCampaignValue();
062        String campaignSourceValue = form.getCampaignSourceValue();
063        String campaignMediumValue = form.getCampaignMediumValue();
064        String campaignTermValue = form.getCampaignTermValue();
065        String campaignContentValue = form.getCampaignContentValue();
066        var parameterCount = (campaignValue == null ? 0 : 1) + (campaignSourceValue == null ? 0 : 1) + (campaignMediumValue == null ? 0 : 1)
067                + (campaignTermValue == null ? 0 : 1) + (campaignContentValue == null ? 0 : 1);
068
069        if(parameterCount > 0) {
070            var campaignControl = Session.getModelController(CampaignControl.class);
071            Campaign campaign = null;
072            CampaignSource campaignSource = null;
073            CampaignMedium campaignMedium = null;
074            CampaignTerm campaignTerm = null;
075            CampaignContent campaignContent = null;
076            
077            if(campaignValue != null) {
078                campaign = campaignControl.getCampaignByValue(campaignValue);
079                
080                if(campaign == null) {
081                    try {
082                        campaign = campaignControl.createCampaign(campaignValue, Boolean.FALSE, 0, getPartyPK());
083                    } catch(PersistenceDatabaseException pde) {
084                        if(PersistenceUtils.getInstance().isIntegrityConstraintViolation(pde)) {
085                            campaign = campaignControl.getCampaignByValue(campaignValue);
086                            pde = null;
087                        }
088
089                        if(pde != null) {
090                            throw pde;
091                        }
092                    }
093                }
094            }
095            
096            if(campaignSourceValue != null) {
097                campaignSource = campaignControl.getCampaignSourceByValue(campaignSourceValue);
098                
099                if(campaignSource == null) {
100                    try {
101                        campaignSource = campaignControl.createCampaignSource(campaignSourceValue, Boolean.FALSE, 0, getPartyPK());
102                    } catch(PersistenceDatabaseException pde) {
103                        if(PersistenceUtils.getInstance().isIntegrityConstraintViolation(pde)) {
104                            campaignSource = campaignControl.getCampaignSourceByValue(campaignSourceValue);
105                            pde = null;
106                        }
107
108                        if(pde != null) {
109                            throw pde;
110                        }
111                    }
112                }
113            }
114            
115            if(campaignMediumValue != null) {
116                campaignMedium = campaignControl.getCampaignMediumByValue(campaignMediumValue);
117                
118                if(campaignMedium == null) {
119                    try {
120                        campaignMedium = campaignControl.createCampaignMedium(campaignMediumValue, Boolean.FALSE, 0, getPartyPK());
121                    } catch(PersistenceDatabaseException pde) {
122                        if(PersistenceUtils.getInstance().isIntegrityConstraintViolation(pde)) {
123                            campaignMedium = campaignControl.getCampaignMediumByValue(campaignMediumValue);
124                            pde = null;
125                        }
126
127                        if(pde != null) {
128                            throw pde;
129                        }
130                    }
131                }
132            }
133            
134            if(campaignTermValue != null) {
135                campaignTerm = campaignControl.getCampaignTermByValue(campaignTermValue);
136                
137                if(campaignTerm == null) {
138                    try {
139                        campaignTerm = campaignControl.createCampaignTerm(campaignTermValue, Boolean.FALSE, 0, getPartyPK());
140                    } catch(PersistenceDatabaseException pde) {
141                        if(PersistenceUtils.getInstance().isIntegrityConstraintViolation(pde)) {
142                            campaignTerm = campaignControl.getCampaignTermByValue(campaignTermValue);
143                            pde = null;
144                        }
145
146                        if(pde != null) {
147                            throw pde;
148                        }
149                    }
150                }
151            }
152            
153            if(campaignContentValue != null) {
154                campaignContent = campaignControl.getCampaignContentByValue(campaignContentValue);
155                
156                if(campaignContent == null) {
157                    try {
158                        campaignContent = campaignControl.createCampaignContent(campaignContentValue, Boolean.FALSE, 0, getPartyPK());
159                    } catch(PersistenceDatabaseException pde) {
160                        if(PersistenceUtils.getInstance().isIntegrityConstraintViolation(pde)) {
161                            campaignContent = campaignControl.getCampaignContentByValue(campaignContentValue);
162                            pde = null;
163                        }
164
165                        if(pde != null) {
166                            throw pde;
167                        }
168                    }
169                }
170            }
171            
172            campaignControl.createUserVisitCampaign(getUserVisit(), session.START_TIME_LONG, campaign, campaignSource, campaignMedium, campaignTerm,
173                    campaignContent);
174        } else {
175            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
176        }
177        
178        return null;
179    }
180    
181}