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.form.EditLetterForm; 022import com.echothree.control.user.letter.common.result.LetterResultFactory; 023import com.echothree.control.user.letter.common.spec.LetterSpec; 024import com.echothree.model.control.chain.server.control.ChainControl; 025import com.echothree.model.control.contactlist.server.control.ContactListControl; 026import com.echothree.model.control.letter.server.control.LetterControl; 027import com.echothree.model.control.party.common.PartyTypes; 028import com.echothree.model.control.security.common.SecurityRoleGroups; 029import com.echothree.model.control.security.common.SecurityRoles; 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.BaseResult; 035import com.echothree.util.common.command.EditMode; 036import com.echothree.util.server.control.BaseEditCommand; 037import com.echothree.util.server.control.CommandSecurityDefinition; 038import com.echothree.util.server.control.PartyTypeDefinition; 039import com.echothree.util.server.control.SecurityRoleDefinition; 040import java.util.List; 041import javax.enterprise.context.Dependent; 042import javax.inject.Inject; 043 044@Dependent 045public class EditLetterCommand 046 extends BaseEditCommand<LetterSpec, LetterEdit> { 047 048 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 049 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 050 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 051 052 static { 053 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 054 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 055 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 056 new SecurityRoleDefinition(SecurityRoleGroups.Letter.name(), SecurityRoles.Edit.name()) 057 )) 058 )); 059 060 SPEC_FIELD_DEFINITIONS = List.of( 061 new FieldDefinition("ChainKindName", FieldType.ENTITY_NAME, true, null, null), 062 new FieldDefinition("ChainTypeName", FieldType.ENTITY_NAME, true, null, null), 063 new FieldDefinition("LetterName", FieldType.ENTITY_NAME, true, null, null) 064 ); 065 066 EDIT_FIELD_DEFINITIONS = List.of( 067 new FieldDefinition("LetterName", FieldType.ENTITY_NAME, true, null, null), 068 new FieldDefinition("LetterSourceName", FieldType.ENTITY_NAME, true, null, null), 069 new FieldDefinition("ContactListName", FieldType.ENTITY_NAME, false, null, null), 070 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 071 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 072 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 073 ); 074 } 075 076 @Inject 077 ChainControl chainControl; 078 079 @Inject 080 ContactListControl contactListControl; 081 082 @Inject 083 LetterControl letterControl; 084 085 086 /** Creates a new instance of EditLetterCommand */ 087 public EditLetterCommand() { 088 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 089 } 090 091 @Override 092 protected BaseResult execute() { 093 var result = LetterResultFactory.getEditLetterResult(); 094 var chainKindName = spec.getChainKindName(); 095 var chainKind = chainControl.getChainKindByName(chainKindName); 096 097 if(chainKind != null) { 098 var chainTypeName = spec.getChainTypeName(); 099 var chainType = chainControl.getChainTypeByName(chainKind, chainTypeName); 100 101 if(chainType != null) { 102 if(editMode.equals(EditMode.LOCK)) { 103 var letterName = spec.getLetterName(); 104 var letter = letterControl.getLetterByName(chainType, letterName); 105 106 if(letter != null) { 107 result.setLetter(letterControl.getLetterTransfer(getUserVisit(), letter)); 108 109 if(lockEntity(letter)) { 110 var letterDescription = letterControl.getLetterDescription(letter, getPreferredLanguage()); 111 var edit = LetterEditFactory.getLetterEdit(); 112 var letterDetail = letter.getLastDetail(); 113 var contactList = letterDetail.getContactList(); 114 115 result.setEdit(edit); 116 edit.setLetterName(letterDetail.getLetterName()); 117 edit.setLetterSourceName(letterDetail.getLetterSource().getLastDetail().getLetterSourceName()); 118 edit.setContactListName(contactList == null? null: contactList.getLastDetail().getContactListName()); 119 edit.setIsDefault(letterDetail.getIsDefault().toString()); 120 edit.setSortOrder(letterDetail.getSortOrder().toString()); 121 122 if(letterDescription != null) 123 edit.setDescription(letterDescription.getDescription()); 124 } else { 125 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 126 } 127 128 result.setEntityLock(getEntityLockTransfer(letter)); 129 } else { 130 addExecutionError(ExecutionErrors.UnknownLetterName.name(), letterName); 131 } 132 } else if(editMode.equals(EditMode.UPDATE)) { 133 var letterName = spec.getLetterName(); 134 var letter = letterControl.getLetterByNameForUpdate(chainType, letterName); 135 136 if(letter != null) { 137 letterName = edit.getLetterName(); 138 var duplicateLetter = letterControl.getLetterByName(chainType, letterName); 139 140 if(duplicateLetter == null || letter.equals(duplicateLetter)) { 141 var letterSourceName = edit.getLetterSourceName(); 142 var letterSource = letterControl.getLetterSourceByName(letterSourceName); 143 144 if(letterSource != null) { 145 var contactListName = edit.getContactListName(); 146 var contactList = contactListName == null? null: contactListControl.getContactListByName(contactListName); 147 148 if(contactListName == null || contactList != null) { 149 if(lockEntityForUpdate(letter)) { 150 try { 151 var partyPK = getPartyPK(); 152 var letterDetailValue = letterControl.getLetterDetailValueForUpdate(letter); 153 var letterDescription = letterControl.getLetterDescriptionForUpdate(letter, getPreferredLanguage()); 154 var description = edit.getDescription(); 155 156 letterDetailValue.setLetterName(edit.getLetterName()); 157 letterDetailValue.setLetterSourcePK(letterSource.getPrimaryKey()); 158 letterDetailValue.setContactListPK(contactList == null? null: contactList.getPrimaryKey()); 159 letterDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 160 letterDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 161 162 letterControl.updateLetterFromValue(letterDetailValue, partyPK); 163 164 if(letterDescription == null && description != null) { 165 letterControl.createLetterDescription(letter, getPreferredLanguage(), description, partyPK); 166 } else if(letterDescription != null && description == null) { 167 letterControl.deleteLetterDescription(letterDescription, partyPK); 168 } else if(letterDescription != null && description != null) { 169 var letterDescriptionValue = letterControl.getLetterDescriptionValue(letterDescription); 170 171 letterDescriptionValue.setDescription(description); 172 letterControl.updateLetterDescriptionFromValue(letterDescriptionValue, partyPK); 173 } 174 } finally { 175 unlockEntity(letter); 176 } 177 } else { 178 addExecutionError(ExecutionErrors.EntityLockStale.name()); 179 } 180 } else { 181 addExecutionError(ExecutionErrors.UnknownContactListName.name(), contactListName); 182 } 183 } else { 184 addExecutionError(ExecutionErrors.UnknownLetterSourceName.name(), letterSourceName); 185 } 186 } else { 187 addExecutionError(ExecutionErrors.DuplicateLetterName.name(), letterName); 188 } 189 } else { 190 addExecutionError(ExecutionErrors.UnknownLetterName.name(), letterName); 191 } 192 193 if(hasExecutionErrors()) { 194 result.setLetter(letterControl.getLetterTransfer(getUserVisit(), letter)); 195 result.setEntityLock(getEntityLockTransfer(letter)); 196 } 197 } 198 } else { 199 addExecutionError(ExecutionErrors.UnknownChainTypeName.name(), chainTypeName); 200 } 201 } else { 202 addExecutionError(ExecutionErrors.UnknownChainKindName.name(), chainKindName); 203 } 204 205 return result; 206 } 207 208}