001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.EditLetterContactMechanismPurposeResult;
023import com.echothree.control.user.letter.common.result.LetterResultFactory;
024import com.echothree.control.user.letter.common.spec.LetterContactMechanismPurposeSpec;
025import com.echothree.model.control.chain.server.control.ChainControl;
026import com.echothree.model.control.contact.common.ContactMechanismTypes;
027import com.echothree.model.control.contact.server.control.ContactControl;
028import com.echothree.model.control.letter.server.control.LetterControl;
029import com.echothree.model.control.party.common.PartyTypes;
030import com.echothree.model.control.security.common.SecurityRoleGroups;
031import com.echothree.model.control.security.common.SecurityRoles;
032import com.echothree.model.data.chain.server.entity.ChainKind;
033import com.echothree.model.data.chain.server.entity.ChainType;
034import com.echothree.model.data.contact.server.entity.ContactMechanismPurpose;
035import com.echothree.model.data.letter.server.entity.Letter;
036import com.echothree.model.data.letter.server.entity.LetterContactMechanismPurpose;
037import com.echothree.model.data.letter.server.entity.LetterContactMechanismPurposeDetail;
038import com.echothree.model.data.letter.server.value.LetterContactMechanismPurposeDetailValue;
039import com.echothree.model.data.user.common.pk.UserVisitPK;
040import com.echothree.util.common.message.ExecutionErrors;
041import com.echothree.util.common.validation.FieldDefinition;
042import com.echothree.util.common.validation.FieldType;
043import com.echothree.util.common.command.BaseResult;
044import com.echothree.util.common.command.EditMode;
045import com.echothree.util.server.control.BaseEditCommand;
046import com.echothree.util.server.control.CommandSecurityDefinition;
047import com.echothree.util.server.control.PartyTypeDefinition;
048import com.echothree.util.server.control.SecurityRoleDefinition;
049import com.echothree.util.server.persistence.Session;
050import java.util.Arrays;
051import java.util.Collections;
052import java.util.List;
053
054public class EditLetterContactMechanismPurposeCommand
055        extends BaseEditCommand<LetterContactMechanismPurposeSpec, LetterContactMechanismPurposeEdit> {
056    
057    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
058    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
059    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
060    
061    static {
062        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
063                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
064                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
065                new SecurityRoleDefinition(SecurityRoleGroups.LetterContactMechanismPurpose.name(), SecurityRoles.Edit.name())
066                )))
067                )));
068        
069        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
070                new FieldDefinition("ChainKindName", FieldType.ENTITY_NAME, true, null, null),
071                new FieldDefinition("ChainTypeName", FieldType.ENTITY_NAME, true, null, null),
072                new FieldDefinition("LetterName", FieldType.ENTITY_NAME, true, null, null),
073                new FieldDefinition("Priority", FieldType.SIGNED_INTEGER, true, null, null)
074                ));
075        
076        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
077                new FieldDefinition("ContactMechanismPurposeName", FieldType.ENTITY_NAME, true, null, null)
078                ));
079    }
080    
081    /** Creates a new instance of EditLetterContactMechanismPurposeCommand */
082    public EditLetterContactMechanismPurposeCommand(UserVisitPK userVisitPK, EditLetterContactMechanismPurposeForm form) {
083        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
084    }
085    
086    @Override
087    protected BaseResult execute() {
088        var chainControl = Session.getModelController(ChainControl.class);
089        EditLetterContactMechanismPurposeResult result = LetterResultFactory.getEditLetterContactMechanismPurposeResult();
090        String chainKindName = spec.getChainKindName();
091        ChainKind chainKind = chainControl.getChainKindByName(chainKindName);
092        
093        if(chainKind != null) {
094            String chainTypeName = spec.getChainTypeName();
095            ChainType chainType = chainControl.getChainTypeByName(chainKind, chainTypeName);
096            
097            if(chainType != null) {
098                var letterControl = Session.getModelController(LetterControl.class);
099                String letterName = spec.getLetterName();
100                Letter letter = letterControl.getLetterByName(chainType, letterName);
101                
102                if(letter != null) {
103                    Integer priority = Integer.valueOf(spec.getPriority());
104                    
105                    if(editMode.equals(EditMode.LOCK)) {
106                        LetterContactMechanismPurpose 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                                LetterContactMechanismPurposeDetail letterContactMechanismPurposeDetail = letterContactMechanismPurpose.getLastDetail();
115                                LetterContactMechanismPurposeEdit 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                        LetterContactMechanismPurpose letterContactMechanismPurpose = letterControl.getLetterContactMechanismPurposeForUpdate(letter,
129                                priority);
130                        LetterContactMechanismPurposeDetailValue letterContactMechanismPurposeDetailValue = letterControl.getLetterContactMechanismPurposeDetailValueForUpdate(letterContactMechanismPurpose);
131                        
132                        if(letterContactMechanismPurposeDetailValue != null) {
133                            var contactControl = Session.getModelController(ContactControl.class);
134                            String contactMechanismPurposeName = edit.getContactMechanismPurposeName();
135                            ContactMechanismPurpose contactMechanismPurpose = contactControl.getContactMechanismPurposeByName(contactMechanismPurposeName);
136                            
137                            if(contactMechanismPurpose != null) {
138                                String contactMechanismTypeName = contactMechanismPurpose.getContactMechanismType().getContactMechanismTypeName();
139                                
140                                if(contactMechanismTypeName.equals(ContactMechanismTypes.EMAIL_ADDRESS.name())
141                                        || contactMechanismTypeName.equals(ContactMechanismTypes.POSTAL_ADDRESS.name())) {
142                                    if(lockEntityForUpdate(letter)) {
143                                        try {
144                                            letterContactMechanismPurposeDetailValue.setContactMechanismPurposePK(contactMechanismPurpose.getPrimaryKey());
145                                            
146                                            letterControl.updateLetterContactMechanismPurposeFromValue(letterContactMechanismPurposeDetailValue, getPartyPK());
147                                        } finally {
148                                            unlockEntity(letter);
149                                        }
150                                    } else {
151                                        addExecutionError(ExecutionErrors.EntityLockStale.name());
152                                    }
153                                } else {
154                                    addExecutionError(ExecutionErrors.InvalidContactMechanismType.name(), contactMechanismTypeName);
155                                }
156                            } else {
157                                addExecutionError(ExecutionErrors.UnknownContactMechanismPurposeName.name(), contactMechanismPurposeName);
158                            }
159                        } else {
160                            addExecutionError(ExecutionErrors.UnknownLetterContactMechanismPurpose.name());
161                        }
162                        
163                        if(hasExecutionErrors()) {
164                            result.setLetterContactMechanismPurpose(letterControl.getLetterContactMechanismPurposeTransfer(getUserVisit(),
165                                    letterContactMechanismPurpose));
166                            result.setEntityLock(getEntityLockTransfer(letter));
167                        }
168                    }
169                } else {
170                    addExecutionError(ExecutionErrors.UnknownLetterName.name(), letterName);
171                }
172            } else {
173                addExecutionError(ExecutionErrors.UnknownChainTypeName.name(), chainTypeName);
174            }
175        } else {
176            addExecutionError(ExecutionErrors.UnknownChainKindName.name(), chainKindName);
177        }
178        
179        return result;
180    }
181    
182}