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