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.cancellationpolicy.server.command;
018
019import com.echothree.control.user.cancellationpolicy.common.edit.CancellationPolicyEditFactory;
020import com.echothree.control.user.cancellationpolicy.common.edit.CancellationReasonEdit;
021import com.echothree.control.user.cancellationpolicy.common.form.EditCancellationReasonForm;
022import com.echothree.control.user.cancellationpolicy.common.result.CancellationPolicyResultFactory;
023import com.echothree.control.user.cancellationpolicy.common.spec.CancellationReasonSpec;
024import com.echothree.model.control.cancellationpolicy.server.control.CancellationPolicyControl;
025import com.echothree.model.control.party.common.PartyTypes;
026import com.echothree.model.control.security.common.SecurityRoleGroups;
027import com.echothree.model.control.security.common.SecurityRoles;
028import com.echothree.model.data.user.common.pk.UserVisitPK;
029import com.echothree.util.common.message.ExecutionErrors;
030import com.echothree.util.common.validation.FieldDefinition;
031import com.echothree.util.common.validation.FieldType;
032import com.echothree.util.common.command.BaseResult;
033import com.echothree.util.common.command.EditMode;
034import com.echothree.util.server.control.BaseEditCommand;
035import com.echothree.util.server.control.CommandSecurityDefinition;
036import com.echothree.util.server.control.PartyTypeDefinition;
037import com.echothree.util.server.control.SecurityRoleDefinition;
038import com.echothree.util.server.persistence.Session;
039import java.util.List;
040import javax.enterprise.context.Dependent;
041
042@Dependent
043public class EditCancellationReasonCommand
044        extends BaseEditCommand<CancellationReasonSpec, CancellationReasonEdit> {
045    
046    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
047    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
048    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
049    
050    static {
051        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
052                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
053                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
054                        new SecurityRoleDefinition(SecurityRoleGroups.CancellationReason.name(), SecurityRoles.Edit.name())
055                        ))
056                ));
057        
058        SPEC_FIELD_DEFINITIONS = List.of(
059                new FieldDefinition("CancellationKindName", FieldType.ENTITY_NAME, true, null, null),
060                new FieldDefinition("CancellationReasonName", FieldType.ENTITY_NAME, true, null, null)
061                );
062        
063        EDIT_FIELD_DEFINITIONS = List.of(
064                new FieldDefinition("CancellationReasonName", FieldType.ENTITY_NAME, true, null, null),
065                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
066                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
067                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
068                );
069    }
070    
071    /** Creates a new instance of EditCancellationReasonCommand */
072    public EditCancellationReasonCommand() {
073        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
074    }
075    
076    @Override
077    protected BaseResult execute() {
078        var cancellationPolicyControl = Session.getModelController(CancellationPolicyControl.class);
079        var result = CancellationPolicyResultFactory.getEditCancellationReasonResult();
080        var cancellationKindName = spec.getCancellationKindName();
081        var cancellationKind = cancellationPolicyControl.getCancellationKindByName(cancellationKindName);
082        
083        if(cancellationKind != null) {
084            if(editMode.equals(EditMode.LOCK)) {
085                var cancellationReasonName = spec.getCancellationReasonName();
086                var cancellationReason = cancellationPolicyControl.getCancellationReasonByName(cancellationKind, cancellationReasonName);
087                
088                if(cancellationReason != null) {
089                    result.setCancellationReason(cancellationPolicyControl.getCancellationReasonTransfer(getUserVisit(), cancellationReason));
090                    
091                    if(lockEntity(cancellationReason)) {
092                        var cancellationReasonDescription = cancellationPolicyControl.getCancellationReasonDescription(cancellationReason, getPreferredLanguage());
093                        var edit = CancellationPolicyEditFactory.getCancellationReasonEdit();
094                        var cancellationReasonDetail = cancellationReason.getLastDetail();
095                        
096                        result.setEdit(edit);
097                        edit.setCancellationReasonName(cancellationReasonDetail.getCancellationReasonName());
098                        edit.setIsDefault(cancellationReasonDetail.getIsDefault().toString());
099                        edit.setSortOrder(cancellationReasonDetail.getSortOrder().toString());
100                        
101                        if(cancellationReasonDescription != null)
102                            edit.setDescription(cancellationReasonDescription.getDescription());
103                    } else {
104                        addExecutionError(ExecutionErrors.EntityLockFailed.name());
105                    }
106                    
107                    result.setEntityLock(getEntityLockTransfer(cancellationReason));
108                } else {
109                    addExecutionError(ExecutionErrors.UnknownCancellationReasonName.name(), cancellationReasonName);
110                }
111            } else if(editMode.equals(EditMode.UPDATE)) {
112                var cancellationReasonName = spec.getCancellationReasonName();
113                var cancellationReason = cancellationPolicyControl.getCancellationReasonByNameForUpdate(cancellationKind, cancellationReasonName);
114                
115                if(cancellationReason != null) {
116                    cancellationReasonName = edit.getCancellationReasonName();
117                    var duplicateCancellationReason = cancellationPolicyControl.getCancellationReasonByName(cancellationKind, cancellationReasonName);
118                    
119                    if(duplicateCancellationReason == null || cancellationReason.equals(duplicateCancellationReason)) {
120                        if(lockEntityForUpdate(cancellationReason)) {
121                            try {
122                                var partyPK = getPartyPK();
123                                var cancellationReasonDetailValue = cancellationPolicyControl.getCancellationReasonDetailValueForUpdate(cancellationReason);
124                                var cancellationReasonDescription = cancellationPolicyControl.getCancellationReasonDescriptionForUpdate(cancellationReason, getPreferredLanguage());
125                                var description = edit.getDescription();
126                                
127                                cancellationReasonDetailValue.setCancellationReasonName(edit.getCancellationReasonName());
128                                cancellationReasonDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
129                                cancellationReasonDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
130                                
131                                cancellationPolicyControl.updateCancellationReasonFromValue(cancellationReasonDetailValue, partyPK);
132                                
133                                if(cancellationReasonDescription == null && description != null) {
134                                    cancellationPolicyControl.createCancellationReasonDescription(cancellationReason, getPreferredLanguage(), description, partyPK);
135                                } else if(cancellationReasonDescription != null && description == null) {
136                                    cancellationPolicyControl.deleteCancellationReasonDescription(cancellationReasonDescription, partyPK);
137                                } else if(cancellationReasonDescription != null && description != null) {
138                                    var cancellationReasonDescriptionValue = cancellationPolicyControl.getCancellationReasonDescriptionValue(cancellationReasonDescription);
139                                    
140                                    cancellationReasonDescriptionValue.setDescription(description);
141                                    cancellationPolicyControl.updateCancellationReasonDescriptionFromValue(cancellationReasonDescriptionValue, partyPK);
142                                }
143                            } finally {
144                                unlockEntity(cancellationReason);
145                            }
146                        } else {
147                            addExecutionError(ExecutionErrors.EntityLockStale.name());
148                        }
149                    } else {
150                        addExecutionError(ExecutionErrors.DuplicateCancellationReasonName.name(), cancellationReasonName);
151                    }
152                } else {
153                    addExecutionError(ExecutionErrors.UnknownCancellationReasonName.name(), cancellationReasonName);
154                }
155            }
156        } else {
157            addExecutionError(ExecutionErrors.UnknownCancellationKindName.name(), cancellationKindName);
158        }
159        
160        return result;
161    }
162    
163}