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.CampaignContentEdit; 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.EditCampaignContentResult; 023import com.echothree.control.user.campaign.common.spec.CampaignContentUniversalSpec; 024import com.echothree.model.control.campaign.server.control.CampaignControl; 025import com.echothree.model.control.campaign.server.logic.CampaignContentLogic; 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.CampaignContent; 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 EditCampaignContentCommand 043 extends BaseAbstractEditCommand<CampaignContentUniversalSpec, CampaignContentEdit, EditCampaignContentResult, CampaignContent, CampaignContent> { 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.CampaignContent.name(), SecurityRoles.Edit.name()) 054 )) 055 )); 056 057 SPEC_FIELD_DEFINITIONS = List.of( 058 new FieldDefinition("CampaignContentName", 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 071 @Inject 072 CampaignControl campaignControl; 073 074 @Inject 075 CampaignContentLogic campaignContentLogic; 076 077 /** Creates a new instance of EditCampaignContentCommand */ 078 public EditCampaignContentCommand() { 079 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 080 } 081 082 @Override 083 public EditCampaignContentResult getResult() { 084 return CampaignResultFactory.getEditCampaignContentResult(); 085 } 086 087 @Override 088 public CampaignContentEdit getEdit() { 089 return CampaignEditFactory.getCampaignContentEdit(); 090 } 091 092 @Override 093 public CampaignContent getEntity(EditCampaignContentResult result) { 094 return campaignContentLogic.getCampaignContentByUniversalSpec(this, spec, editModeToEntityPermission(editMode)); 095 } 096 097 @Override 098 public CampaignContent getLockEntity(CampaignContent campaignContent) { 099 return campaignContent; 100 } 101 102 @Override 103 public void fillInResult(EditCampaignContentResult result, CampaignContent campaignContent) { 104 result.setCampaignContent(campaignControl.getCampaignContentTransfer(getUserVisit(), campaignContent)); 105 } 106 107 @Override 108 public void doLock(CampaignContentEdit edit, CampaignContent campaignContent) { 109 var campaignContentDescription = campaignControl.getCampaignContentDescription(campaignContent, getPreferredLanguage()); 110 var campaignContentDetail = campaignContent.getLastDetail(); 111 112 edit.setValue(campaignContentDetail.getValue()); 113 edit.setIsDefault(campaignContentDetail.getIsDefault().toString()); 114 edit.setSortOrder(campaignContentDetail.getSortOrder().toString()); 115 116 if(campaignContentDescription != null) { 117 edit.setDescription(campaignContentDescription.getDescription()); 118 } 119 } 120 121 @Override 122 public void canUpdate(CampaignContent campaignContent) { 123 var value = edit.getValue(); 124 var duplicateCampaignContent = campaignControl.getCampaignContentByValue(value); 125 126 if(duplicateCampaignContent != null && !campaignContent.equals(duplicateCampaignContent)) { 127 addExecutionError(ExecutionErrors.DuplicateCampaignContentValue.name(), value); 128 } 129 } 130 131 @Override 132 public void doUpdate(CampaignContent campaignContent) { 133 var partyPK = getPartyPK(); 134 var campaignContentDetailValue = campaignControl.getCampaignContentDetailValueForUpdate(campaignContent); 135 var campaignContentDescription = campaignControl.getCampaignContentDescriptionForUpdate(campaignContent, getPreferredLanguage()); 136 var description = edit.getDescription(); 137 138 campaignContentDetailValue.setValue(edit.getValue()); 139 campaignContentDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 140 campaignContentDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 141 142 campaignControl.updateCampaignContentFromValue(campaignContentDetailValue, partyPK); 143 144 if(campaignContentDescription == null && description != null) { 145 campaignControl.createCampaignContentDescription(campaignContent, getPreferredLanguage(), description, partyPK); 146 } else { 147 if(campaignContentDescription != null && description == null) { 148 campaignControl.deleteCampaignContentDescription(campaignContentDescription, partyPK); 149 } else { 150 if(campaignContentDescription != null && description != null) { 151 var campaignContentDescriptionValue = campaignControl.getCampaignContentDescriptionValue(campaignContentDescription); 152 153 campaignContentDescriptionValue.setDescription(description); 154 campaignControl.updateCampaignContentDescriptionFromValue(campaignContentDescriptionValue, partyPK); 155 } 156 } 157 } 158 } 159 160}