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.edit.CampaignEditFactory; 020import com.echothree.control.user.campaign.common.edit.CampaignSourceEdit; 021import com.echothree.control.user.campaign.common.form.EditCampaignSourceForm; 022import com.echothree.control.user.campaign.common.result.CampaignResultFactory; 023import com.echothree.control.user.campaign.common.result.EditCampaignSourceResult; 024import com.echothree.control.user.campaign.common.spec.CampaignSourceSpec; 025import com.echothree.model.control.campaign.server.control.CampaignControl; 026import com.echothree.model.control.party.common.PartyTypes; 027import com.echothree.model.control.security.common.SecurityRoleGroups; 028import com.echothree.model.control.security.common.SecurityRoles; 029import com.echothree.model.data.campaign.server.entity.CampaignSource; 030import com.echothree.model.data.campaign.server.entity.CampaignSourceDescription; 031import com.echothree.model.data.campaign.server.entity.CampaignSourceDetail; 032import com.echothree.model.data.campaign.server.value.CampaignSourceDescriptionValue; 033import com.echothree.model.data.campaign.server.value.CampaignSourceDetailValue; 034import com.echothree.model.data.user.common.pk.UserVisitPK; 035import com.echothree.util.common.message.ExecutionErrors; 036import com.echothree.util.common.validation.FieldDefinition; 037import com.echothree.util.common.validation.FieldType; 038import com.echothree.util.common.command.EditMode; 039import com.echothree.util.server.control.BaseAbstractEditCommand; 040import com.echothree.util.server.control.CommandSecurityDefinition; 041import com.echothree.util.server.control.PartyTypeDefinition; 042import com.echothree.util.server.control.SecurityRoleDefinition; 043import com.echothree.util.server.persistence.Session; 044import java.util.Arrays; 045import java.util.Collections; 046import java.util.List; 047 048public class EditCampaignSourceCommand 049 extends BaseAbstractEditCommand<CampaignSourceSpec, CampaignSourceEdit, EditCampaignSourceResult, CampaignSource, CampaignSource> { 050 051 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 052 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 053 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 054 055 static { 056 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 057 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 058 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 059 new SecurityRoleDefinition(SecurityRoleGroups.CampaignSource.name(), SecurityRoles.Edit.name()) 060 ))) 061 ))); 062 063 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 064 new FieldDefinition("CampaignSourceName", FieldType.ENTITY_NAME, true, null, null) 065 )); 066 067 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 068 new FieldDefinition("Value", FieldType.STRING, true, null, null), 069 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 070 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 071 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 072 )); 073 } 074 075 /** Creates a new instance of EditCampaignSourceCommand */ 076 public EditCampaignSourceCommand(UserVisitPK userVisitPK, EditCampaignSourceForm form) { 077 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 078 } 079 080 @Override 081 public EditCampaignSourceResult getResult() { 082 return CampaignResultFactory.getEditCampaignSourceResult(); 083 } 084 085 @Override 086 public CampaignSourceEdit getEdit() { 087 return CampaignEditFactory.getCampaignSourceEdit(); 088 } 089 090 @Override 091 public CampaignSource getEntity(EditCampaignSourceResult result) { 092 var campaignControl = Session.getModelController(CampaignControl.class); 093 CampaignSource campaignSource; 094 String campaignSourceName = spec.getCampaignSourceName(); 095 096 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 097 campaignSource = campaignControl.getCampaignSourceByName(campaignSourceName); 098 } else { // EditMode.UPDATE 099 campaignSource = campaignControl.getCampaignSourceByNameForUpdate(campaignSourceName); 100 } 101 102 if(campaignSource == null) { 103 addExecutionError(ExecutionErrors.UnknownCampaignSourceName.name(), campaignSourceName); 104 } 105 106 return campaignSource; 107 } 108 109 @Override 110 public CampaignSource getLockEntity(CampaignSource campaignSource) { 111 return campaignSource; 112 } 113 114 @Override 115 public void fillInResult(EditCampaignSourceResult result, CampaignSource campaignSource) { 116 var campaignControl = Session.getModelController(CampaignControl.class); 117 118 result.setCampaignSource(campaignControl.getCampaignSourceTransfer(getUserVisit(), campaignSource)); 119 } 120 121 @Override 122 public void doLock(CampaignSourceEdit edit, CampaignSource campaignSource) { 123 var campaignControl = Session.getModelController(CampaignControl.class); 124 CampaignSourceDescription campaignSourceDescription = campaignControl.getCampaignSourceDescription(campaignSource, getPreferredLanguage()); 125 CampaignSourceDetail campaignSourceDetail = campaignSource.getLastDetail(); 126 127 edit.setValue(campaignSourceDetail.getValue()); 128 edit.setIsDefault(campaignSourceDetail.getIsDefault().toString()); 129 edit.setSortOrder(campaignSourceDetail.getSortOrder().toString()); 130 131 if(campaignSourceDescription != null) { 132 edit.setDescription(campaignSourceDescription.getDescription()); 133 } 134 } 135 136 @Override 137 public void canUpdate(CampaignSource campaignSource) { 138 var campaignControl = Session.getModelController(CampaignControl.class); 139 String value = edit.getValue(); 140 CampaignSource duplicateCampaignSource = campaignControl.getCampaignSourceByValue(value); 141 142 if(duplicateCampaignSource != null && !campaignSource.equals(duplicateCampaignSource)) { 143 addExecutionError(ExecutionErrors.DuplicateCampaignSourceValue.name(), value); 144 } 145 } 146 147 @Override 148 public void doUpdate(CampaignSource campaignSource) { 149 var campaignControl = Session.getModelController(CampaignControl.class); 150 var partyPK = getPartyPK(); 151 CampaignSourceDetailValue campaignSourceDetailValue = campaignControl.getCampaignSourceDetailValueForUpdate(campaignSource); 152 CampaignSourceDescription campaignSourceDescription = campaignControl.getCampaignSourceDescriptionForUpdate(campaignSource, getPreferredLanguage()); 153 String description = edit.getDescription(); 154 155 campaignSourceDetailValue.setValue(edit.getValue()); 156 campaignSourceDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 157 campaignSourceDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 158 159 campaignControl.updateCampaignSourceFromValue(campaignSourceDetailValue, partyPK); 160 161 if(campaignSourceDescription == null && description != null) { 162 campaignControl.createCampaignSourceDescription(campaignSource, getPreferredLanguage(), description, partyPK); 163 } else { 164 if(campaignSourceDescription != null && description == null) { 165 campaignControl.deleteCampaignSourceDescription(campaignSourceDescription, partyPK); 166 } else { 167 if(campaignSourceDescription != null && description != null) { 168 CampaignSourceDescriptionValue campaignSourceDescriptionValue = campaignControl.getCampaignSourceDescriptionValue(campaignSourceDescription); 169 170 campaignSourceDescriptionValue.setDescription(description); 171 campaignControl.updateCampaignSourceDescriptionFromValue(campaignSourceDescriptionValue, partyPK); 172 } 173 } 174 } 175 } 176 177}