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 com.echothree.util.server.persistence.Session; 034import java.util.List; 035import javax.enterprise.context.Dependent; 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 /** Creates a new instance of CreateCampaignCommand */ 054 public CreateUserVisitCampaignCommand() { 055 super(null, FORM_FIELD_DEFINITIONS, false); 056 } 057 058 @Override 059 protected BaseResult execute() { 060 var campaignValue = form.getCampaignValue(); 061 var campaignSourceValue = form.getCampaignSourceValue(); 062 var campaignMediumValue = form.getCampaignMediumValue(); 063 var campaignTermValue = form.getCampaignTermValue(); 064 var campaignContentValue = form.getCampaignContentValue(); 065 var parameterCount = (campaignValue == null ? 0 : 1) + (campaignSourceValue == null ? 0 : 1) + (campaignMediumValue == null ? 0 : 1) 066 + (campaignTermValue == null ? 0 : 1) + (campaignContentValue == null ? 0 : 1); 067 068 if(parameterCount > 0) { 069 var campaignControl = Session.getModelController(CampaignControl.class); 070 Campaign campaign = null; 071 CampaignSource campaignSource = null; 072 CampaignMedium campaignMedium = null; 073 CampaignTerm campaignTerm = null; 074 CampaignContent campaignContent = null; 075 076 if(campaignValue != null) { 077 campaign = campaignControl.getCampaignByValue(campaignValue); 078 079 if(campaign == null) { 080 try { 081 campaign = campaignControl.createCampaign(campaignValue, false, 0, getPartyPK()); 082 } catch(PersistenceDatabaseException pde) { 083 if(PersistenceUtils.getInstance().isIntegrityConstraintViolation(pde)) { 084 campaign = campaignControl.getCampaignByValue(campaignValue); 085 pde = null; 086 } 087 088 if(pde != null) { 089 throw pde; 090 } 091 } 092 } 093 } 094 095 if(campaignSourceValue != null) { 096 campaignSource = campaignControl.getCampaignSourceByValue(campaignSourceValue); 097 098 if(campaignSource == null) { 099 try { 100 campaignSource = campaignControl.createCampaignSource(campaignSourceValue, false, 0, getPartyPK()); 101 } catch(PersistenceDatabaseException pde) { 102 if(PersistenceUtils.getInstance().isIntegrityConstraintViolation(pde)) { 103 campaignSource = campaignControl.getCampaignSourceByValue(campaignSourceValue); 104 pde = null; 105 } 106 107 if(pde != null) { 108 throw pde; 109 } 110 } 111 } 112 } 113 114 if(campaignMediumValue != null) { 115 campaignMedium = campaignControl.getCampaignMediumByValue(campaignMediumValue); 116 117 if(campaignMedium == null) { 118 try { 119 campaignMedium = campaignControl.createCampaignMedium(campaignMediumValue, false, 0, getPartyPK()); 120 } catch(PersistenceDatabaseException pde) { 121 if(PersistenceUtils.getInstance().isIntegrityConstraintViolation(pde)) { 122 campaignMedium = campaignControl.getCampaignMediumByValue(campaignMediumValue); 123 pde = null; 124 } 125 126 if(pde != null) { 127 throw pde; 128 } 129 } 130 } 131 } 132 133 if(campaignTermValue != null) { 134 campaignTerm = campaignControl.getCampaignTermByValue(campaignTermValue); 135 136 if(campaignTerm == null) { 137 try { 138 campaignTerm = campaignControl.createCampaignTerm(campaignTermValue, false, 0, getPartyPK()); 139 } catch(PersistenceDatabaseException pde) { 140 if(PersistenceUtils.getInstance().isIntegrityConstraintViolation(pde)) { 141 campaignTerm = campaignControl.getCampaignTermByValue(campaignTermValue); 142 pde = null; 143 } 144 145 if(pde != null) { 146 throw pde; 147 } 148 } 149 } 150 } 151 152 if(campaignContentValue != null) { 153 campaignContent = campaignControl.getCampaignContentByValue(campaignContentValue); 154 155 if(campaignContent == null) { 156 try { 157 campaignContent = campaignControl.createCampaignContent(campaignContentValue, false, 0, getPartyPK()); 158 } catch(PersistenceDatabaseException pde) { 159 if(PersistenceUtils.getInstance().isIntegrityConstraintViolation(pde)) { 160 campaignContent = campaignControl.getCampaignContentByValue(campaignContentValue); 161 pde = null; 162 } 163 164 if(pde != null) { 165 throw pde; 166 } 167 } 168 } 169 } 170 171 campaignControl.createUserVisitCampaign(getUserVisit(), session.getStartTime(), campaign, campaignSource, campaignMedium, campaignTerm, 172 campaignContent); 173 } else { 174 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 175 } 176 177 return null; 178 } 179 180}