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.cancellationpolicy.server.command;
018
019import com.echothree.control.user.cancellationpolicy.common.edit.CancellationPolicyEditFactory;
020import com.echothree.control.user.cancellationpolicy.common.edit.CancellationReasonDescriptionEdit;
021import com.echothree.control.user.cancellationpolicy.common.form.EditCancellationReasonDescriptionForm;
022import com.echothree.control.user.cancellationpolicy.common.result.CancellationPolicyResultFactory;
023import com.echothree.control.user.cancellationpolicy.common.result.EditCancellationReasonDescriptionResult;
024import com.echothree.control.user.cancellationpolicy.common.spec.CancellationReasonDescriptionSpec;
025import com.echothree.model.control.cancellationpolicy.server.control.CancellationPolicyControl;
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.cancellationpolicy.server.entity.CancellationKind;
031import com.echothree.model.data.cancellationpolicy.server.entity.CancellationReason;
032import com.echothree.model.data.cancellationpolicy.server.entity.CancellationReasonDescription;
033import com.echothree.model.data.cancellationpolicy.server.value.CancellationReasonDescriptionValue;
034import com.echothree.model.data.party.server.entity.Language;
035import com.echothree.model.data.user.common.pk.UserVisitPK;
036import com.echothree.util.common.message.ExecutionErrors;
037import com.echothree.util.common.validation.FieldDefinition;
038import com.echothree.util.common.validation.FieldType;
039import com.echothree.util.common.command.BaseResult;
040import com.echothree.util.common.command.EditMode;
041import com.echothree.util.server.control.BaseEditCommand;
042import com.echothree.util.server.control.CommandSecurityDefinition;
043import com.echothree.util.server.control.PartyTypeDefinition;
044import com.echothree.util.server.control.SecurityRoleDefinition;
045import com.echothree.util.server.persistence.Session;
046import java.util.Arrays;
047import java.util.Collections;
048import java.util.List;
049
050public class EditCancellationReasonDescriptionCommand
051        extends BaseEditCommand<CancellationReasonDescriptionSpec, CancellationReasonDescriptionEdit> {
052    
053    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
054    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
055    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
056    
057    static {
058        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
059                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
060                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
061                        new SecurityRoleDefinition(SecurityRoleGroups.CancellationReason.name(), SecurityRoles.Description.name())
062                        )))
063                )));
064        
065        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
066                new FieldDefinition("CancellationKindName", FieldType.ENTITY_NAME, true, null, null),
067                new FieldDefinition("CancellationReasonName", FieldType.ENTITY_NAME, true, null, null),
068                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
069                ));
070        
071        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
072                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
073                ));
074    }
075    
076    /** Creates a new instance of EditCancellationReasonDescriptionCommand */
077    public EditCancellationReasonDescriptionCommand(UserVisitPK userVisitPK, EditCancellationReasonDescriptionForm form) {
078        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
079    }
080    
081    @Override
082    protected BaseResult execute() {
083        var cancellationPolicyControl = Session.getModelController(CancellationPolicyControl.class);
084        EditCancellationReasonDescriptionResult result = CancellationPolicyResultFactory.getEditCancellationReasonDescriptionResult();
085        String cancellationKindName = spec.getCancellationKindName();
086        CancellationKind cancellationKind = cancellationPolicyControl.getCancellationKindByName(cancellationKindName);
087        
088        if(cancellationKind != null) {
089            String cancellationReasonName = spec.getCancellationReasonName();
090            CancellationReason cancellationReason = cancellationPolicyControl.getCancellationReasonByName(cancellationKind, cancellationReasonName);
091            
092            if(cancellationReason != null) {
093                var partyControl = Session.getModelController(PartyControl.class);
094                String languageIsoName = spec.getLanguageIsoName();
095                Language language = partyControl.getLanguageByIsoName(languageIsoName);
096                
097                if(language != null) {
098                    if(editMode.equals(EditMode.LOCK)) {
099                        CancellationReasonDescription cancellationReasonDescription = cancellationPolicyControl.getCancellationReasonDescription(cancellationReason, language);
100                        
101                        if(cancellationReasonDescription != null) {
102                            result.setCancellationReasonDescription(cancellationPolicyControl.getCancellationReasonDescriptionTransfer(getUserVisit(), cancellationReasonDescription));
103                            
104                            if(lockEntity(cancellationReason)) {
105                                CancellationReasonDescriptionEdit edit = CancellationPolicyEditFactory.getCancellationReasonDescriptionEdit();
106                                
107                                result.setEdit(edit);
108                                edit.setDescription(cancellationReasonDescription.getDescription());
109                            } else {
110                                addExecutionError(ExecutionErrors.EntityLockFailed.name());
111                            }
112                            
113                            result.setEntityLock(getEntityLockTransfer(cancellationReason));
114                        } else {
115                            addExecutionError(ExecutionErrors.UnknownCancellationReasonDescription.name());
116                        }
117                    } else if(editMode.equals(EditMode.UPDATE)) {
118                        CancellationReasonDescriptionValue cancellationReasonDescriptionValue = cancellationPolicyControl.getCancellationReasonDescriptionValueForUpdate(cancellationReason, language);
119                        
120                        if(cancellationReasonDescriptionValue != null) {
121                            if(lockEntityForUpdate(cancellationReason)) {
122                                try {
123                                    String description = edit.getDescription();
124                                    
125                                    cancellationReasonDescriptionValue.setDescription(description);
126                                    
127                                    cancellationPolicyControl.updateCancellationReasonDescriptionFromValue(cancellationReasonDescriptionValue, getPartyPK());
128                                } finally {
129                                    unlockEntity(cancellationReason);
130                                }
131                            } else {
132                                addExecutionError(ExecutionErrors.EntityLockStale.name());
133                            }
134                        } else {
135                            addExecutionError(ExecutionErrors.UnknownCancellationReasonDescription.name());
136                        }
137                    }
138                } else {
139                    addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
140                }
141            } else {
142                addExecutionError(ExecutionErrors.UnknownCancellationReasonName.name(), cancellationReasonName);
143            }
144        } else {
145            addExecutionError(ExecutionErrors.UnknownCancellationKindName.name(), cancellationKindName);
146        }
147        
148        return result;
149    }
150    
151}