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.form.EditCampaignContentForm; 022import com.echothree.control.user.campaign.common.result.CampaignResultFactory; 023import com.echothree.control.user.campaign.common.result.EditCampaignContentResult; 024import com.echothree.control.user.campaign.common.spec.CampaignContentSpec; 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.CampaignContent; 030import com.echothree.model.data.user.common.pk.UserVisitPK; 031import com.echothree.util.common.message.ExecutionErrors; 032import com.echothree.util.common.validation.FieldDefinition; 033import com.echothree.util.common.validation.FieldType; 034import com.echothree.util.common.command.EditMode; 035import com.echothree.util.server.control.BaseAbstractEditCommand; 036import com.echothree.util.server.control.CommandSecurityDefinition; 037import com.echothree.util.server.control.PartyTypeDefinition; 038import com.echothree.util.server.control.SecurityRoleDefinition; 039import com.echothree.util.server.persistence.Session; 040import java.util.List; 041import javax.enterprise.context.Dependent; 042 043@Dependent 044public class EditCampaignContentCommand 045 extends BaseAbstractEditCommand<CampaignContentSpec, CampaignContentEdit, EditCampaignContentResult, CampaignContent, CampaignContent> { 046 047 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 048 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 049 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 050 051 static { 052 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 053 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 054 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 055 new SecurityRoleDefinition(SecurityRoleGroups.CampaignContent.name(), SecurityRoles.Edit.name()) 056 )) 057 )); 058 059 SPEC_FIELD_DEFINITIONS = List.of( 060 new FieldDefinition("CampaignContentName", FieldType.ENTITY_NAME, true, 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 /** Creates a new instance of EditCampaignContentCommand */ 072 public EditCampaignContentCommand() { 073 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 074 } 075 076 @Override 077 public EditCampaignContentResult getResult() { 078 return CampaignResultFactory.getEditCampaignContentResult(); 079 } 080 081 @Override 082 public CampaignContentEdit getEdit() { 083 return CampaignEditFactory.getCampaignContentEdit(); 084 } 085 086 @Override 087 public CampaignContent getEntity(EditCampaignContentResult result) { 088 var campaignControl = Session.getModelController(CampaignControl.class); 089 CampaignContent campaignContent; 090 var campaignContentName = spec.getCampaignContentName(); 091 092 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 093 campaignContent = campaignControl.getCampaignContentByName(campaignContentName); 094 } else { // EditMode.UPDATE 095 campaignContent = campaignControl.getCampaignContentByNameForUpdate(campaignContentName); 096 } 097 098 if(campaignContent == null) { 099 addExecutionError(ExecutionErrors.UnknownCampaignContentName.name(), campaignContentName); 100 } 101 102 return campaignContent; 103 } 104 105 @Override 106 public CampaignContent getLockEntity(CampaignContent campaignContent) { 107 return campaignContent; 108 } 109 110 @Override 111 public void fillInResult(EditCampaignContentResult result, CampaignContent campaignContent) { 112 var campaignControl = Session.getModelController(CampaignControl.class); 113 114 result.setCampaignContent(campaignControl.getCampaignContentTransfer(getUserVisit(), campaignContent)); 115 } 116 117 @Override 118 public void doLock(CampaignContentEdit edit, CampaignContent campaignContent) { 119 var campaignControl = Session.getModelController(CampaignControl.class); 120 var campaignContentDescription = campaignControl.getCampaignContentDescription(campaignContent, getPreferredLanguage()); 121 var campaignContentDetail = campaignContent.getLastDetail(); 122 123 edit.setValue(campaignContentDetail.getValue()); 124 edit.setIsDefault(campaignContentDetail.getIsDefault().toString()); 125 edit.setSortOrder(campaignContentDetail.getSortOrder().toString()); 126 127 if(campaignContentDescription != null) { 128 edit.setDescription(campaignContentDescription.getDescription()); 129 } 130 } 131 132 @Override 133 public void canUpdate(CampaignContent campaignContent) { 134 var campaignControl = Session.getModelController(CampaignControl.class); 135 var value = edit.getValue(); 136 var duplicateCampaignContent = campaignControl.getCampaignContentByValue(value); 137 138 if(duplicateCampaignContent != null && !campaignContent.equals(duplicateCampaignContent)) { 139 addExecutionError(ExecutionErrors.DuplicateCampaignContentValue.name(), value); 140 } 141 } 142 143 @Override 144 public void doUpdate(CampaignContent campaignContent) { 145 var campaignControl = Session.getModelController(CampaignControl.class); 146 var partyPK = getPartyPK(); 147 var campaignContentDetailValue = campaignControl.getCampaignContentDetailValueForUpdate(campaignContent); 148 var campaignContentDescription = campaignControl.getCampaignContentDescriptionForUpdate(campaignContent, getPreferredLanguage()); 149 var description = edit.getDescription(); 150 151 campaignContentDetailValue.setValue(edit.getValue()); 152 campaignContentDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 153 campaignContentDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 154 155 campaignControl.updateCampaignContentFromValue(campaignContentDetailValue, partyPK); 156 157 if(campaignContentDescription == null && description != null) { 158 campaignControl.createCampaignContentDescription(campaignContent, getPreferredLanguage(), description, partyPK); 159 } else { 160 if(campaignContentDescription != null && description == null) { 161 campaignControl.deleteCampaignContentDescription(campaignContentDescription, partyPK); 162 } else { 163 if(campaignContentDescription != null && description != null) { 164 var campaignContentDescriptionValue = campaignControl.getCampaignContentDescriptionValue(campaignContentDescription); 165 166 campaignContentDescriptionValue.setDescription(description); 167 campaignControl.updateCampaignContentDescriptionFromValue(campaignContentDescriptionValue, partyPK); 168 } 169 } 170 } 171 } 172 173}