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