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.result.EditLetterContactMechanismPurposeResult; 022import com.echothree.control.user.letter.common.result.LetterResultFactory; 023import com.echothree.control.user.letter.common.spec.LetterContactMechanismPurposeSpec; 024import com.echothree.model.control.contact.common.ContactMechanismTypes; 025import com.echothree.model.control.contact.server.logic.ContactMechanismPurposeLogic; 026import com.echothree.model.control.letter.server.control.LetterControl; 027import com.echothree.model.control.letter.server.logic.LetterContactMechanismPurposeLogic; 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.contact.server.entity.ContactMechanismPurpose; 032import com.echothree.model.data.letter.server.entity.Letter; 033import com.echothree.model.data.letter.server.entity.LetterContactMechanismPurpose; 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 EditLetterContactMechanismPurposeCommand 047 extends BaseEditCommand<LetterContactMechanismPurposeSpec, LetterContactMechanismPurposeEdit, EditLetterContactMechanismPurposeResult, LetterContactMechanismPurpose, 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.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 private ContactMechanismPurpose contactMechanismPurpose; 074 075 @Inject 076 LetterControl letterControl; 077 078 @Inject 079 ContactMechanismPurposeLogic contactMechanismPurposeLogic; 080 081 @Inject 082 LetterContactMechanismPurposeLogic letterContactMechanismPurposeLogic; 083 084 /** Creates a new instance of EditLetterContactMechanismPurposeCommand */ 085 public EditLetterContactMechanismPurposeCommand() { 086 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 087 } 088 089 @Override 090 public EditLetterContactMechanismPurposeResult getResult() { 091 return LetterResultFactory.getEditLetterContactMechanismPurposeResult(); 092 } 093 094 @Override 095 public LetterContactMechanismPurposeEdit getEdit() { 096 return LetterEditFactory.getLetterContactMechanismPurposeEdit(); 097 } 098 099 @Override 100 public LetterContactMechanismPurpose getEntity(EditLetterContactMechanismPurposeResult result) { 101 return letterContactMechanismPurposeLogic.getLetterContactMechanismPurpose(this, spec.getChainKindName(), spec.getChainTypeName(), 102 spec.getLetterName(), Integer.valueOf(spec.getPriority()), editModeToEntityPermission(editMode)); 103 } 104 105 @Override 106 public Letter getLockEntity(LetterContactMechanismPurpose letterContactMechanismPurpose) { 107 return letterContactMechanismPurpose.getLastDetail().getLetter(); 108 } 109 110 @Override 111 public void fillInResult(EditLetterContactMechanismPurposeResult result, LetterContactMechanismPurpose letterContactMechanismPurpose) { 112 result.setLetterContactMechanismPurpose(letterControl.getLetterContactMechanismPurposeTransfer(getUserVisit(), letterContactMechanismPurpose)); 113 } 114 115 @Override 116 public void doLock(LetterContactMechanismPurposeEdit edit, LetterContactMechanismPurpose letterContactMechanismPurpose) { 117 edit.setContactMechanismPurposeName(letterContactMechanismPurpose.getLastDetail().getContactMechanismPurpose().getContactMechanismPurposeName()); 118 } 119 120 @Override 121 public void canUpdate(LetterContactMechanismPurpose letterContactMechanismPurpose) { 122 contactMechanismPurpose = contactMechanismPurposeLogic.getContactMechanismPurposeByName(this, edit.getContactMechanismPurposeName()); 123 124 if(!hasExecutionErrors()) { 125 var contactMechanismTypeName = contactMechanismPurpose.getContactMechanismType().getContactMechanismTypeName(); 126 127 if(!ContactMechanismTypes.EMAIL_ADDRESS.name().equals(contactMechanismTypeName) 128 && !ContactMechanismTypes.POSTAL_ADDRESS.name().equals(contactMechanismTypeName)) { 129 addExecutionError(ExecutionErrors.InvalidContactMechanismType.name(), contactMechanismTypeName); 130 } 131 } 132 } 133 134 @Override 135 public void doUpdate(LetterContactMechanismPurpose letterContactMechanismPurpose) { 136 var letterContactMechanismPurposeDetailValue = letterControl.getLetterContactMechanismPurposeDetailValueForUpdate(letterContactMechanismPurpose); 137 138 letterContactMechanismPurposeDetailValue.setContactMechanismPurposePK(contactMechanismPurpose.getPrimaryKey()); 139 140 letterControl.updateLetterContactMechanismPurposeFromValue(letterContactMechanismPurposeDetailValue, getPartyPK()); 141 } 142 143}