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 java.util.List;
042import javax.enterprise.context.Dependent;
043import javax.inject.Inject;
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    @Inject
074    ChainControl chainControl;
075
076    @Inject
077    ContactControl contactControl;
078
079    @Inject
080    LetterControl letterControl;
081
082    
083    /** Creates a new instance of EditLetterContactMechanismPurposeCommand */
084    public EditLetterContactMechanismPurposeCommand() {
085        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
086    }
087    
088    @Override
089    protected BaseResult execute() {
090        var result = LetterResultFactory.getEditLetterContactMechanismPurposeResult();
091        var chainKindName = spec.getChainKindName();
092        var chainKind = chainControl.getChainKindByName(chainKindName);
093        
094        if(chainKind != null) {
095            var chainTypeName = spec.getChainTypeName();
096            var chainType = chainControl.getChainTypeByName(chainKind, chainTypeName);
097            
098            if(chainType != null) {
099                var letterName = spec.getLetterName();
100                var letter = letterControl.getLetterByName(chainType, letterName);
101                
102                if(letter != null) {
103                    var priority = Integer.valueOf(spec.getPriority());
104                    
105                    if(editMode.equals(EditMode.LOCK)) {
106                        var letterContactMechanismPurpose = letterControl.getLetterContactMechanismPurpose(letter,
107                                priority);
108                        
109                        if(letterContactMechanismPurpose != null) {
110                            result.setLetterContactMechanismPurpose(letterControl.getLetterContactMechanismPurposeTransfer(getUserVisit(),
111                                    letterContactMechanismPurpose));
112                            
113                            if(lockEntity(letter)) {
114                                var letterContactMechanismPurposeDetail = letterContactMechanismPurpose.getLastDetail();
115                                var edit = LetterEditFactory.getLetterContactMechanismPurposeEdit();
116                                
117                                result.setEdit(edit);
118                                edit.setContactMechanismPurposeName(letterContactMechanismPurposeDetail.getContactMechanismPurpose().getContactMechanismPurposeName());
119                            } else {
120                                addExecutionError(ExecutionErrors.EntityLockFailed.name());
121                            }
122                            
123                            result.setEntityLock(getEntityLockTransfer(letter));
124                        } else {
125                            addExecutionError(ExecutionErrors.UnknownLetterContactMechanismPurpose.name());
126                        }
127                    } else if(editMode.equals(EditMode.UPDATE)) {
128                        var letterContactMechanismPurpose = letterControl.getLetterContactMechanismPurposeForUpdate(letter,
129                                priority);
130                        var letterContactMechanismPurposeDetailValue = letterControl.getLetterContactMechanismPurposeDetailValueForUpdate(letterContactMechanismPurpose);
131                        
132                        if(letterContactMechanismPurposeDetailValue != null) {
133                            var contactMechanismPurposeName = edit.getContactMechanismPurposeName();
134                            var contactMechanismPurpose = contactControl.getContactMechanismPurposeByName(contactMechanismPurposeName);
135                            
136                            if(contactMechanismPurpose != null) {
137                                var contactMechanismTypeName = contactMechanismPurpose.getContactMechanismType().getContactMechanismTypeName();
138                                
139                                if(contactMechanismTypeName.equals(ContactMechanismTypes.EMAIL_ADDRESS.name())
140                                        || contactMechanismTypeName.equals(ContactMechanismTypes.POSTAL_ADDRESS.name())) {
141                                    if(lockEntityForUpdate(letter)) {
142                                        try {
143                                            letterContactMechanismPurposeDetailValue.setContactMechanismPurposePK(contactMechanismPurpose.getPrimaryKey());
144                                            
145                                            letterControl.updateLetterContactMechanismPurposeFromValue(letterContactMechanismPurposeDetailValue, getPartyPK());
146                                        } finally {
147                                            unlockEntity(letter);
148                                        }
149                                    } else {
150                                        addExecutionError(ExecutionErrors.EntityLockStale.name());
151                                    }
152                                } else {
153                                    addExecutionError(ExecutionErrors.InvalidContactMechanismType.name(), contactMechanismTypeName);
154                                }
155                            } else {
156                                addExecutionError(ExecutionErrors.UnknownContactMechanismPurposeName.name(), contactMechanismPurposeName);
157                            }
158                        } else {
159                            addExecutionError(ExecutionErrors.UnknownLetterContactMechanismPurpose.name());
160                        }
161                        
162                        if(hasExecutionErrors()) {
163                            result.setLetterContactMechanismPurpose(letterControl.getLetterContactMechanismPurposeTransfer(getUserVisit(),
164                                    letterContactMechanismPurpose));
165                            result.setEntityLock(getEntityLockTransfer(letter));
166                        }
167                    }
168                } else {
169                    addExecutionError(ExecutionErrors.UnknownLetterName.name(), letterName);
170                }
171            } else {
172                addExecutionError(ExecutionErrors.UnknownChainTypeName.name(), chainTypeName);
173            }
174        } else {
175            addExecutionError(ExecutionErrors.UnknownChainKindName.name(), chainKindName);
176        }
177        
178        return result;
179    }
180    
181}