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.LetterContactMechanismPurposeEdit; 020import com.echothree.control.user.letter.common.edit.LetterEditFactory; 021import com.echothree.control.user.letter.common.form.EditLetterContactMechanismPurposeForm; 022import com.echothree.control.user.letter.common.result.LetterResultFactory; 023import com.echothree.control.user.letter.common.spec.LetterContactMechanismPurposeSpec; 024import com.echothree.model.control.chain.server.control.ChainControl; 025import com.echothree.model.control.contact.common.ContactMechanismTypes; 026import com.echothree.model.control.contact.server.control.ContactControl; 027import com.echothree.model.control.letter.server.control.LetterControl; 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.user.common.pk.UserVisitPK; 032import com.echothree.util.common.message.ExecutionErrors; 033import com.echothree.util.common.validation.FieldDefinition; 034import com.echothree.util.common.validation.FieldType; 035import com.echothree.util.common.command.BaseResult; 036import com.echothree.util.common.command.EditMode; 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 com.echothree.util.server.persistence.Session; 042import java.util.List; 043import javax.enterprise.context.Dependent; 044 045@Dependent 046public class EditLetterContactMechanismPurposeCommand 047 extends BaseEditCommand<LetterContactMechanismPurposeSpec, LetterContactMechanismPurposeEdit> { 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.LetterContactMechanismPurpose.name(), SecurityRoles.Edit.name()) 058 )) 059 )); 060 061 SPEC_FIELD_DEFINITIONS = List.of( 062 new FieldDefinition("ChainKindName", FieldType.ENTITY_NAME, true, null, null), 063 new FieldDefinition("ChainTypeName", FieldType.ENTITY_NAME, true, null, null), 064 new FieldDefinition("LetterName", FieldType.ENTITY_NAME, true, null, null), 065 new FieldDefinition("Priority", FieldType.SIGNED_INTEGER, true, null, null) 066 ); 067 068 EDIT_FIELD_DEFINITIONS = List.of( 069 new FieldDefinition("ContactMechanismPurposeName", FieldType.ENTITY_NAME, true, null, null) 070 ); 071 } 072 073 /** Creates a new instance of EditLetterContactMechanismPurposeCommand */ 074 public EditLetterContactMechanismPurposeCommand() { 075 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 076 } 077 078 @Override 079 protected BaseResult execute() { 080 var chainControl = Session.getModelController(ChainControl.class); 081 var result = LetterResultFactory.getEditLetterContactMechanismPurposeResult(); 082 var chainKindName = spec.getChainKindName(); 083 var chainKind = chainControl.getChainKindByName(chainKindName); 084 085 if(chainKind != null) { 086 var chainTypeName = spec.getChainTypeName(); 087 var chainType = chainControl.getChainTypeByName(chainKind, chainTypeName); 088 089 if(chainType != null) { 090 var letterControl = Session.getModelController(LetterControl.class); 091 var letterName = spec.getLetterName(); 092 var letter = letterControl.getLetterByName(chainType, letterName); 093 094 if(letter != null) { 095 var priority = Integer.valueOf(spec.getPriority()); 096 097 if(editMode.equals(EditMode.LOCK)) { 098 var letterContactMechanismPurpose = letterControl.getLetterContactMechanismPurpose(letter, 099 priority); 100 101 if(letterContactMechanismPurpose != null) { 102 result.setLetterContactMechanismPurpose(letterControl.getLetterContactMechanismPurposeTransfer(getUserVisit(), 103 letterContactMechanismPurpose)); 104 105 if(lockEntity(letter)) { 106 var letterContactMechanismPurposeDetail = letterContactMechanismPurpose.getLastDetail(); 107 var edit = LetterEditFactory.getLetterContactMechanismPurposeEdit(); 108 109 result.setEdit(edit); 110 edit.setContactMechanismPurposeName(letterContactMechanismPurposeDetail.getContactMechanismPurpose().getContactMechanismPurposeName()); 111 } else { 112 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 113 } 114 115 result.setEntityLock(getEntityLockTransfer(letter)); 116 } else { 117 addExecutionError(ExecutionErrors.UnknownLetterContactMechanismPurpose.name()); 118 } 119 } else if(editMode.equals(EditMode.UPDATE)) { 120 var letterContactMechanismPurpose = letterControl.getLetterContactMechanismPurposeForUpdate(letter, 121 priority); 122 var letterContactMechanismPurposeDetailValue = letterControl.getLetterContactMechanismPurposeDetailValueForUpdate(letterContactMechanismPurpose); 123 124 if(letterContactMechanismPurposeDetailValue != null) { 125 var contactControl = Session.getModelController(ContactControl.class); 126 var contactMechanismPurposeName = edit.getContactMechanismPurposeName(); 127 var contactMechanismPurpose = contactControl.getContactMechanismPurposeByName(contactMechanismPurposeName); 128 129 if(contactMechanismPurpose != null) { 130 var contactMechanismTypeName = contactMechanismPurpose.getContactMechanismType().getContactMechanismTypeName(); 131 132 if(contactMechanismTypeName.equals(ContactMechanismTypes.EMAIL_ADDRESS.name()) 133 || contactMechanismTypeName.equals(ContactMechanismTypes.POSTAL_ADDRESS.name())) { 134 if(lockEntityForUpdate(letter)) { 135 try { 136 letterContactMechanismPurposeDetailValue.setContactMechanismPurposePK(contactMechanismPurpose.getPrimaryKey()); 137 138 letterControl.updateLetterContactMechanismPurposeFromValue(letterContactMechanismPurposeDetailValue, getPartyPK()); 139 } finally { 140 unlockEntity(letter); 141 } 142 } else { 143 addExecutionError(ExecutionErrors.EntityLockStale.name()); 144 } 145 } else { 146 addExecutionError(ExecutionErrors.InvalidContactMechanismType.name(), contactMechanismTypeName); 147 } 148 } else { 149 addExecutionError(ExecutionErrors.UnknownContactMechanismPurposeName.name(), contactMechanismPurposeName); 150 } 151 } else { 152 addExecutionError(ExecutionErrors.UnknownLetterContactMechanismPurpose.name()); 153 } 154 155 if(hasExecutionErrors()) { 156 result.setLetterContactMechanismPurpose(letterControl.getLetterContactMechanismPurposeTransfer(getUserVisit(), 157 letterContactMechanismPurpose)); 158 result.setEntityLock(getEntityLockTransfer(letter)); 159 } 160 } 161 } else { 162 addExecutionError(ExecutionErrors.UnknownLetterName.name(), letterName); 163 } 164 } else { 165 addExecutionError(ExecutionErrors.UnknownChainTypeName.name(), chainTypeName); 166 } 167 } else { 168 addExecutionError(ExecutionErrors.UnknownChainKindName.name(), chainKindName); 169 } 170 171 return result; 172 } 173 174}