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.letter.server.command; 018 019import com.echothree.control.user.letter.common.edit.LetterEdit; 020import com.echothree.control.user.letter.common.edit.LetterEditFactory; 021import com.echothree.control.user.letter.common.result.EditLetterResult; 022import com.echothree.control.user.letter.common.result.LetterResultFactory; 023import com.echothree.control.user.letter.common.spec.LetterUniversalSpec; 024import com.echothree.model.control.contactlist.server.logic.ContactListLogic; 025import com.echothree.model.control.letter.server.control.LetterControl; 026import com.echothree.model.control.letter.server.logic.LetterLogic; 027import com.echothree.model.control.letter.server.logic.LetterSourceLogic; 028import com.echothree.model.control.party.common.PartyTypes; 029import com.echothree.model.control.security.common.SecurityRoleGroups; 030import com.echothree.model.control.security.common.SecurityRoles; 031import com.echothree.model.data.contactlist.server.entity.ContactList; 032import com.echothree.model.data.letter.server.entity.Letter; 033import com.echothree.model.data.letter.server.entity.LetterSource; 034import com.echothree.util.common.message.ExecutionErrors; 035import com.echothree.util.common.validation.FieldDefinition; 036import com.echothree.util.common.validation.FieldType; 037import com.echothree.util.server.control.BaseEditCommand; 038import com.echothree.util.server.control.CommandSecurityDefinition; 039import com.echothree.util.server.control.PartyTypeDefinition; 040import com.echothree.util.server.control.SecurityRoleDefinition; 041import java.util.List; 042import javax.enterprise.context.Dependent; 043import javax.inject.Inject; 044 045@Dependent 046public class EditLetterCommand 047 extends BaseEditCommand<LetterUniversalSpec, LetterEdit, EditLetterResult, Letter, Letter> { 048 049 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 050 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 051 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 052 053 static { 054 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 055 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 056 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 057 new SecurityRoleDefinition(SecurityRoleGroups.Letter.name(), SecurityRoles.Edit.name()) 058 )) 059 )); 060 061 SPEC_FIELD_DEFINITIONS = List.of( 062 new FieldDefinition("ChainKindName", FieldType.ENTITY_NAME, false, null, null), 063 new FieldDefinition("ChainTypeName", FieldType.ENTITY_NAME, false, null, null), 064 new FieldDefinition("LetterName", FieldType.ENTITY_NAME, false, null, null), 065 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 066 new FieldDefinition("Uuid", FieldType.UUID, false, null, null) 067 ); 068 069 EDIT_FIELD_DEFINITIONS = List.of( 070 new FieldDefinition("LetterName", FieldType.ENTITY_NAME, true, null, null), 071 new FieldDefinition("LetterSourceName", FieldType.ENTITY_NAME, true, null, null), 072 new FieldDefinition("ContactListName", FieldType.ENTITY_NAME, false, null, null), 073 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 074 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 075 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 076 ); 077 } 078 079 private ContactList contactList; 080 private LetterSource letterSource; 081 082 @Inject 083 LetterControl letterControl; 084 085 @Inject 086 ContactListLogic contactListLogic; 087 088 @Inject 089 LetterLogic letterLogic; 090 091 @Inject 092 LetterSourceLogic letterSourceLogic; 093 094 /** Creates a new instance of EditLetterCommand */ 095 public EditLetterCommand() { 096 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 097 } 098 099 @Override 100 public EditLetterResult getResult() { 101 return LetterResultFactory.getEditLetterResult(); 102 } 103 104 @Override 105 public LetterEdit getEdit() { 106 return LetterEditFactory.getLetterEdit(); 107 } 108 109 @Override 110 public Letter getEntity(EditLetterResult result) { 111 return letterLogic.getLetterByUniversalSpec(this, spec, editModeToEntityPermission(editMode)); 112 } 113 114 @Override 115 public Letter getLockEntity(Letter letter) { 116 return letter; 117 } 118 119 @Override 120 public void fillInResult(EditLetterResult result, Letter letter) { 121 result.setLetter(letterControl.getLetterTransfer(getUserVisit(), letter)); 122 } 123 124 @Override 125 public void doLock(LetterEdit edit, Letter letter) { 126 var letterDescription = letterControl.getLetterDescription(letter, getPreferredLanguage()); 127 var letterDetail = letter.getLastDetail(); 128 var contactList = letterDetail.getContactList(); 129 130 edit.setLetterName(letterDetail.getLetterName()); 131 edit.setLetterSourceName(letterDetail.getLetterSource().getLastDetail().getLetterSourceName()); 132 edit.setContactListName(contactList == null? null: contactList.getLastDetail().getContactListName()); 133 edit.setIsDefault(letterDetail.getIsDefault().toString()); 134 edit.setSortOrder(letterDetail.getSortOrder().toString()); 135 136 if(letterDescription != null) { 137 edit.setDescription(letterDescription.getDescription()); 138 } 139 } 140 141 @Override 142 public void canUpdate(Letter letter) { 143 var chainType = letter.getLastDetail().getChainType(); 144 var letterName = edit.getLetterName(); 145 var duplicateLetter = letterControl.getLetterByName(chainType, letterName); 146 var letterSourceName = edit.getLetterSourceName(); 147 var contactListName = edit.getContactListName(); 148 149 if(duplicateLetter != null && !letter.equals(duplicateLetter)) { 150 var chainTypeDetail = chainType.getLastDetail(); 151 152 addExecutionError(ExecutionErrors.DuplicateLetterName.name(), 153 chainTypeDetail.getChainKind().getLastDetail().getChainKindName(), chainTypeDetail.getChainTypeName(), letterName); 154 } 155 156 letterSource = letterSourceLogic.getLetterSourceByName(this, letterSourceName); 157 contactList = contactListName == null ? null : contactListLogic.getContactListByName(this, contactListName); 158 } 159 160 @Override 161 public void doUpdate(Letter letter) { 162 var partyPK = getPartyPK(); 163 var letterDetailValue = letterControl.getLetterDetailValueForUpdate(letter); 164 var letterDescription = letterControl.getLetterDescriptionForUpdate(letter, getPreferredLanguage()); 165 var description = edit.getDescription(); 166 167 letterDetailValue.setLetterName(edit.getLetterName()); 168 letterDetailValue.setLetterSourcePK(letterSource.getPrimaryKey()); 169 letterDetailValue.setContactListPK(contactList == null? null: contactList.getPrimaryKey()); 170 letterDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 171 letterDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 172 173 letterControl.updateLetterFromValue(letterDetailValue, partyPK); 174 175 if(letterDescription == null && description != null) { 176 letterControl.createLetterDescription(letter, getPreferredLanguage(), description, partyPK); 177 } else if(letterDescription != null && description == null) { 178 letterControl.deleteLetterDescription(letterDescription, partyPK); 179 } else if(letterDescription != null && description != null) { 180 var letterDescriptionValue = letterControl.getLetterDescriptionValue(letterDescription); 181 182 letterDescriptionValue.setDescription(description); 183 184 letterControl.updateLetterDescriptionFromValue(letterDescriptionValue, partyPK); 185 } 186 } 187 188}