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.edit.CampaignSourceEdit; 020import com.echothree.control.user.campaign.common.edit.CampaignEditFactory; 021import com.echothree.control.user.campaign.common.result.CampaignResultFactory; 022import com.echothree.control.user.campaign.common.result.EditCampaignSourceResult; 023import com.echothree.control.user.campaign.common.spec.CampaignSourceUniversalSpec; 024import com.echothree.model.control.campaign.server.control.CampaignControl; 025import com.echothree.model.control.campaign.server.logic.CampaignSourceLogic; 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.util.common.message.ExecutionErrors; 031import com.echothree.util.common.validation.FieldDefinition; 032import com.echothree.util.common.validation.FieldType; 033import com.echothree.util.server.control.BaseAbstractEditCommand; 034import com.echothree.util.server.control.CommandSecurityDefinition; 035import com.echothree.util.server.control.PartyTypeDefinition; 036import com.echothree.util.server.control.SecurityRoleDefinition; 037import java.util.List; 038import javax.enterprise.context.Dependent; 039import javax.inject.Inject; 040 041@Dependent 042public class EditCampaignSourceCommand 043 extends BaseAbstractEditCommand<CampaignSourceUniversalSpec, CampaignSourceEdit, EditCampaignSourceResult, CampaignSource, CampaignSource> { 044 045 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 046 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 047 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 048 049 static { 050 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 051 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 052 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 053 new SecurityRoleDefinition(SecurityRoleGroups.CampaignSource.name(), SecurityRoles.Edit.name()) 054 )) 055 )); 056 057 SPEC_FIELD_DEFINITIONS = List.of( 058 new FieldDefinition("CampaignSourceName", FieldType.ENTITY_NAME, false, null, null), 059 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 060 new FieldDefinition("Uuid", FieldType.UUID, false, null, null) 061 ); 062 063 EDIT_FIELD_DEFINITIONS = List.of( 064 new FieldDefinition("Value", FieldType.STRING, true, null, null), 065 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 066 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 067 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 068 ); 069 } 070 @Inject 071 CampaignControl campaignControl; 072 073 @Inject 074 CampaignSourceLogic campaignSourceLogic; 075 076 /** Creates a new instance of EditCampaignSourceCommand */ 077 public EditCampaignSourceCommand() { 078 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 079 } 080 081 @Override 082 public EditCampaignSourceResult getResult() { 083 return CampaignResultFactory.getEditCampaignSourceResult(); 084 } 085 086 @Override 087 public CampaignSourceEdit getEdit() { 088 return CampaignEditFactory.getCampaignSourceEdit(); 089 } 090 091 @Override 092 public CampaignSource getEntity(EditCampaignSourceResult result) { 093 return campaignSourceLogic.getCampaignSourceByUniversalSpec(this, spec, editModeToEntityPermission(editMode)); 094 } 095 096 @Override 097 public CampaignSource getLockEntity(CampaignSource campaignSource) { 098 return campaignSource; 099 } 100 101 @Override 102 public void fillInResult(EditCampaignSourceResult result, CampaignSource campaignSource) { 103 result.setCampaignSource(campaignControl.getCampaignSourceTransfer(getUserVisit(), campaignSource)); 104 } 105 106 @Override 107 public void doLock(CampaignSourceEdit edit, CampaignSource campaignSource) { 108 var campaignSourceDescription = campaignControl.getCampaignSourceDescription(campaignSource, getPreferredLanguage()); 109 var campaignSourceDetail = campaignSource.getLastDetail(); 110 111 edit.setValue(campaignSourceDetail.getValue()); 112 edit.setIsDefault(campaignSourceDetail.getIsDefault().toString()); 113 edit.setSortOrder(campaignSourceDetail.getSortOrder().toString()); 114 115 if(campaignSourceDescription != null) { 116 edit.setDescription(campaignSourceDescription.getDescription()); 117 } 118 } 119 120 @Override 121 public void canUpdate(CampaignSource campaignSource) { 122 var value = edit.getValue(); 123 var duplicateCampaignSource = campaignControl.getCampaignSourceByValue(value); 124 125 if(duplicateCampaignSource != null && !campaignSource.equals(duplicateCampaignSource)) { 126 addExecutionError(ExecutionErrors.DuplicateCampaignSourceValue.name(), value); 127 } 128 } 129 130 @Override 131 public void doUpdate(CampaignSource campaignSource) { 132 var partyPK = getPartyPK(); 133 var campaignSourceDetailValue = campaignControl.getCampaignSourceDetailValueForUpdate(campaignSource); 134 var campaignSourceDescription = campaignControl.getCampaignSourceDescriptionForUpdate(campaignSource, getPreferredLanguage()); 135 var description = edit.getDescription(); 136 137 campaignSourceDetailValue.setValue(edit.getValue()); 138 campaignSourceDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 139 campaignSourceDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 140 141 campaignControl.updateCampaignSourceFromValue(campaignSourceDetailValue, partyPK); 142 143 if(campaignSourceDescription == null && description != null) { 144 campaignControl.createCampaignSourceDescription(campaignSource, getPreferredLanguage(), description, partyPK); 145 } else { 146 if(campaignSourceDescription != null && description == null) { 147 campaignControl.deleteCampaignSourceDescription(campaignSourceDescription, partyPK); 148 } else { 149 if(campaignSourceDescription != null && description != null) { 150 var campaignSourceDescriptionValue = campaignControl.getCampaignSourceDescriptionValue(campaignSourceDescription); 151 152 campaignSourceDescriptionValue.setDescription(description); 153 campaignControl.updateCampaignSourceDescriptionFromValue(campaignSourceDescriptionValue, partyPK); 154 } 155 } 156 } 157 } 158 159}