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.LetterEdit;
020import com.echothree.control.user.letter.common.edit.LetterEditFactory;
021import com.echothree.control.user.letter.common.form.EditLetterForm;
022import com.echothree.control.user.letter.common.result.LetterResultFactory;
023import com.echothree.control.user.letter.common.spec.LetterSpec;
024import com.echothree.model.control.chain.server.control.ChainControl;
025import com.echothree.model.control.contactlist.server.control.ContactListControl;
026import com.echothree.model.control.letter.server.control.LetterControl;
027import com.echothree.model.control.party.common.PartyTypes;
028import com.echothree.model.control.security.common.SecurityRoleGroups;
029import com.echothree.model.control.security.common.SecurityRoles;
030import com.echothree.model.data.user.common.pk.UserVisitPK;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.common.command.BaseResult;
035import com.echothree.util.common.command.EditMode;
036import com.echothree.util.server.control.BaseEditCommand;
037import com.echothree.util.server.control.CommandSecurityDefinition;
038import com.echothree.util.server.control.PartyTypeDefinition;
039import com.echothree.util.server.control.SecurityRoleDefinition;
040import com.echothree.util.server.persistence.Session;
041import java.util.Arrays;
042import java.util.Collections;
043import java.util.List;
044import javax.enterprise.context.RequestScoped;
045
046@RequestScoped
047public class EditLetterCommand
048        extends BaseEditCommand<LetterSpec, LetterEdit> {
049    
050    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
051    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
052    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
053    
054    static {
055        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
056                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
057                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
058                    new SecurityRoleDefinition(SecurityRoleGroups.Letter.name(), SecurityRoles.Edit.name())
059                    )))
060                )));
061        
062        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
063                new FieldDefinition("ChainKindName", FieldType.ENTITY_NAME, true, null, null),
064                new FieldDefinition("ChainTypeName", FieldType.ENTITY_NAME, true, null, null),
065                new FieldDefinition("LetterName", FieldType.ENTITY_NAME, true, null, null)
066                ));
067        
068        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
069                new FieldDefinition("LetterName", FieldType.ENTITY_NAME, true, null, null),
070                new FieldDefinition("LetterSourceName", FieldType.ENTITY_NAME, true, null, null),
071                new FieldDefinition("ContactListName", FieldType.ENTITY_NAME, false, null, null),
072                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
073                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
074                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
075                ));
076    }
077    
078    /** Creates a new instance of EditLetterCommand */
079    public EditLetterCommand() {
080        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
081    }
082    
083    @Override
084    protected BaseResult execute() {
085        var chainControl = Session.getModelController(ChainControl.class);
086        var result = LetterResultFactory.getEditLetterResult();
087        var chainKindName = spec.getChainKindName();
088        var chainKind = chainControl.getChainKindByName(chainKindName);
089        
090        if(chainKind != null) {
091            var chainTypeName = spec.getChainTypeName();
092            var chainType = chainControl.getChainTypeByName(chainKind, chainTypeName);
093            
094            if(chainType != null) {
095                var letterControl = Session.getModelController(LetterControl.class);
096                
097                if(editMode.equals(EditMode.LOCK)) {
098                    var letterName = spec.getLetterName();
099                    var letter = letterControl.getLetterByName(chainType, letterName);
100                    
101                    if(letter != null) {
102                        result.setLetter(letterControl.getLetterTransfer(getUserVisit(), letter));
103                        
104                        if(lockEntity(letter)) {
105                            var letterDescription = letterControl.getLetterDescription(letter, getPreferredLanguage());
106                            var edit = LetterEditFactory.getLetterEdit();
107                            var letterDetail = letter.getLastDetail();
108                            var contactList = letterDetail.getContactList();
109                            
110                            result.setEdit(edit);
111                            edit.setLetterName(letterDetail.getLetterName());
112                            edit.setLetterSourceName(letterDetail.getLetterSource().getLastDetail().getLetterSourceName());
113                            edit.setContactListName(contactList == null? null: contactList.getLastDetail().getContactListName());
114                            edit.setIsDefault(letterDetail.getIsDefault().toString());
115                            edit.setSortOrder(letterDetail.getSortOrder().toString());
116                            
117                            if(letterDescription != null)
118                                edit.setDescription(letterDescription.getDescription());
119                        } else {
120                            addExecutionError(ExecutionErrors.EntityLockFailed.name());
121                        }
122                        
123                        result.setEntityLock(getEntityLockTransfer(letter));
124                    } else {
125                        addExecutionError(ExecutionErrors.UnknownLetterName.name(), letterName);
126                    }
127                } else if(editMode.equals(EditMode.UPDATE)) {
128                    var letterName = spec.getLetterName();
129                    var letter = letterControl.getLetterByNameForUpdate(chainType, letterName);
130                    
131                    if(letter != null) {
132                        letterName = edit.getLetterName();
133                        var duplicateLetter = letterControl.getLetterByName(chainType, letterName);
134                        
135                        if(duplicateLetter == null || letter.equals(duplicateLetter)) {
136                            var letterSourceName = edit.getLetterSourceName();
137                            var letterSource = letterControl.getLetterSourceByName(letterSourceName);
138                            
139                            if(letterSource != null) {
140                                var contactListControl = Session.getModelController(ContactListControl.class);
141                                var contactListName = edit.getContactListName();
142                                var contactList = contactListName == null? null: contactListControl.getContactListByName(contactListName);
143                                
144                                if(contactListName == null || contactList != null) {
145                                    if(lockEntityForUpdate(letter)) {
146                                        try {
147                                            var partyPK = getPartyPK();
148                                            var letterDetailValue = letterControl.getLetterDetailValueForUpdate(letter);
149                                            var letterDescription = letterControl.getLetterDescriptionForUpdate(letter, getPreferredLanguage());
150                                            var description = edit.getDescription();
151                                            
152                                            letterDetailValue.setLetterName(edit.getLetterName());
153                                            letterDetailValue.setLetterSourcePK(letterSource.getPrimaryKey());
154                                            letterDetailValue.setContactListPK(contactList == null? null: contactList.getPrimaryKey());
155                                            letterDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
156                                            letterDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
157                                            
158                                            letterControl.updateLetterFromValue(letterDetailValue, partyPK);
159                                            
160                                            if(letterDescription == null && description != null) {
161                                                letterControl.createLetterDescription(letter, getPreferredLanguage(), description, partyPK);
162                                            } else if(letterDescription != null && description == null) {
163                                                letterControl.deleteLetterDescription(letterDescription, partyPK);
164                                            } else if(letterDescription != null && description != null) {
165                                                var letterDescriptionValue = letterControl.getLetterDescriptionValue(letterDescription);
166                                                
167                                                letterDescriptionValue.setDescription(description);
168                                                letterControl.updateLetterDescriptionFromValue(letterDescriptionValue, partyPK);
169                                            }
170                                        } finally {
171                                            unlockEntity(letter);
172                                        }
173                                    } else {
174                                        addExecutionError(ExecutionErrors.EntityLockStale.name());
175                                    }
176                                } else {
177                                    addExecutionError(ExecutionErrors.UnknownContactListName.name(), contactListName);
178                                }
179                            } else {
180                                addExecutionError(ExecutionErrors.UnknownLetterSourceName.name(), letterSourceName);
181                            }
182                        } else {
183                            addExecutionError(ExecutionErrors.DuplicateLetterName.name(), letterName);
184                        }
185                    } else {
186                        addExecutionError(ExecutionErrors.UnknownLetterName.name(), letterName);
187                    }
188                    
189                    if(hasExecutionErrors()) {
190                        result.setLetter(letterControl.getLetterTransfer(getUserVisit(), letter));
191                        result.setEntityLock(getEntityLockTransfer(letter));
192                    }
193                }
194            } else {
195                addExecutionError(ExecutionErrors.UnknownChainTypeName.name(), chainTypeName);
196            }
197        } else {
198            addExecutionError(ExecutionErrors.UnknownChainKindName.name(), chainKindName);
199        }
200        
201        return result;
202    }
203    
204}