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.LetterDescriptionEdit;
020import com.echothree.control.user.letter.common.edit.LetterEditFactory;
021import com.echothree.control.user.letter.common.form.EditLetterDescriptionForm;
022import com.echothree.control.user.letter.common.result.LetterResultFactory;
023import com.echothree.control.user.letter.common.spec.LetterDescriptionSpec;
024import com.echothree.model.control.chain.server.control.ChainControl;
025import com.echothree.model.control.letter.server.control.LetterControl;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.control.party.server.control.PartyControl;
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.List;
042import javax.enterprise.context.Dependent;
043
044@Dependent
045public class EditLetterDescriptionCommand
046        extends BaseEditCommand<LetterDescriptionSpec, LetterDescriptionEdit> {
047    
048    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
049    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
050    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
051    
052    static {
053        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
054                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
055                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
056                        new SecurityRoleDefinition(SecurityRoleGroups.Letter.name(), SecurityRoles.Description.name())
057                        ))
058                ));
059        
060        SPEC_FIELD_DEFINITIONS = List.of(
061                new FieldDefinition("ChainKindName", FieldType.ENTITY_NAME, true, null, null),
062                new FieldDefinition("ChainTypeName", FieldType.ENTITY_NAME, true, null, null),
063                new FieldDefinition("LetterName", FieldType.ENTITY_NAME, true, null, null),
064                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
065                );
066        
067        EDIT_FIELD_DEFINITIONS = List.of(
068                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
069                );
070    }
071    
072    /** Creates a new instance of EditLetterDescriptionCommand */
073    public EditLetterDescriptionCommand() {
074        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
075    }
076    
077    @Override
078    protected BaseResult execute() {
079        var chainControl = Session.getModelController(ChainControl.class);
080        var result = LetterResultFactory.getEditLetterDescriptionResult();
081        var chainKindName = spec.getChainKindName();
082        var chainKind = chainControl.getChainKindByName(chainKindName);
083        
084        if(chainKind != null) {
085            var chainTypeName = spec.getChainTypeName();
086            var chainType = chainControl.getChainTypeByName(chainKind, chainTypeName);
087            
088            if(chainType != null) {
089                var letterControl = Session.getModelController(LetterControl.class);
090                var letterName = spec.getLetterName();
091                var letter = letterControl.getLetterByName(chainType, letterName);
092                
093                if(letter != null) {
094                    var partyControl = Session.getModelController(PartyControl.class);
095                    var languageIsoName = spec.getLanguageIsoName();
096                    var language = partyControl.getLanguageByIsoName(languageIsoName);
097                    
098                    if(language != null) {
099                        if(editMode.equals(EditMode.LOCK)) {
100                            var letterDescription = letterControl.getLetterDescription(letter, language);
101                            
102                            if(letterDescription != null) {
103                                result.setLetterDescription(letterControl.getLetterDescriptionTransfer(getUserVisit(), letterDescription));
104                                
105                                if(lockEntity(letter)) {
106                                    var edit = LetterEditFactory.getLetterDescriptionEdit();
107                                    
108                                    result.setEdit(edit);
109                                    edit.setDescription(letterDescription.getDescription());
110                                } else {
111                                    addExecutionError(ExecutionErrors.EntityLockFailed.name());
112                                }
113                                
114                                result.setEntityLock(getEntityLockTransfer(letter));
115                            } else {
116                                addExecutionError(ExecutionErrors.UnknownLetterDescription.name());
117                            }
118                        } else if(editMode.equals(EditMode.UPDATE)) {
119                            var letterDescriptionValue = letterControl.getLetterDescriptionValueForUpdate(letter, language);
120                            
121                            if(letterDescriptionValue != null) {
122                                if(lockEntityForUpdate(letter)) {
123                                    try {
124                                        var description = edit.getDescription();
125                                        
126                                        letterDescriptionValue.setDescription(description);
127                                        
128                                        letterControl.updateLetterDescriptionFromValue(letterDescriptionValue, getPartyPK());
129                                    } finally {
130                                        unlockEntity(letter);
131                                    }
132                                } else {
133                                    addExecutionError(ExecutionErrors.EntityLockStale.name());
134                                }
135                            } else {
136                                addExecutionError(ExecutionErrors.UnknownLetterDescription.name());
137                            }
138                        }
139                    } else {
140                        addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
141                    }
142                } else {
143                    addExecutionError(ExecutionErrors.UnknownLetterName.name(), letterName);
144                }
145            } else {
146                addExecutionError(ExecutionErrors.UnknownChainTypeName.name(), chainTypeName);
147            }
148        } else {
149            addExecutionError(ExecutionErrors.UnknownChainKindName.name(), chainKindName);
150        }
151        
152        return result;
153    }
154    
155}