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.result.CancellationPolicyResultFactory;
022import com.echothree.control.user.cancellationpolicy.common.result.EditCancellationReasonResult;
023import com.echothree.control.user.cancellationpolicy.common.spec.CancellationReasonSpec;
024import com.echothree.model.control.cancellationpolicy.server.control.CancellationPolicyControl;
025import com.echothree.model.control.cancellationpolicy.server.logic.CancellationKindLogic;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.data.cancellationpolicy.server.entity.CancellationReason;
030import com.echothree.util.common.message.ExecutionErrors;
031import com.echothree.util.common.validation.FieldDefinition;
032import com.echothree.util.common.validation.FieldType;
033import com.echothree.util.server.control.BaseAbstractEditCommand;
034import com.echothree.util.server.control.CommandSecurityDefinition;
035import com.echothree.util.server.control.PartyTypeDefinition;
036import com.echothree.util.server.control.SecurityRoleDefinition;
037import java.util.List;
038import javax.enterprise.context.Dependent;
039import javax.inject.Inject;
040
041@Dependent
042public class EditCancellationReasonCommand
043        extends BaseAbstractEditCommand<CancellationReasonSpec, CancellationReasonEdit, EditCancellationReasonResult,
044                CancellationReason, CancellationReason> {
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    @Inject
072    CancellationPolicyControl cancellationPolicyControl;
073
074    @Inject
075    CancellationKindLogic cancellationKindLogic;
076    
077    /** Creates a new instance of EditCancellationReasonCommand */
078    public EditCancellationReasonCommand() {
079        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
080    }
081    
082    @Override
083    public EditCancellationReasonResult getResult() {
084        return CancellationPolicyResultFactory.getEditCancellationReasonResult();
085    }
086
087    @Override
088    public CancellationReasonEdit getEdit() {
089        return CancellationPolicyEditFactory.getCancellationReasonEdit();
090    }
091
092    @Override
093    public CancellationReason getEntity(EditCancellationReasonResult result) {
094        CancellationReason cancellationReason = null;
095        var cancellationKindName = spec.getCancellationKindName();
096        var cancellationKind = cancellationKindLogic.getCancellationKindByName(this, cancellationKindName);
097
098        if(!hasExecutionErrors()) {
099            var cancellationReasonName = spec.getCancellationReasonName();
100
101            cancellationReason = cancellationPolicyControl.getCancellationReasonByName(cancellationKind, cancellationReasonName,
102                    editModeToEntityPermission(editMode));
103
104            if(cancellationReason == null) {
105                    addExecutionError(ExecutionErrors.UnknownCancellationReasonName.name(), cancellationKindName, cancellationReasonName);
106            }
107        }
108
109        return cancellationReason;
110    }
111
112    @Override
113    public CancellationReason getLockEntity(CancellationReason cancellationReason) {
114        return cancellationReason;
115    }
116
117    @Override
118    public void fillInResult(EditCancellationReasonResult result, CancellationReason cancellationReason) {
119        result.setCancellationReason(cancellationPolicyControl.getCancellationReasonTransfer(getUserVisit(), cancellationReason));
120    }
121
122    @Override
123    public void doLock(CancellationReasonEdit edit, CancellationReason cancellationReason) {
124        var cancellationReasonDescription = cancellationPolicyControl.getCancellationReasonDescription(cancellationReason, getPreferredLanguage());
125        var cancellationReasonDetail = cancellationReason.getLastDetail();
126
127        edit.setCancellationReasonName(cancellationReasonDetail.getCancellationReasonName());
128        edit.setIsDefault(cancellationReasonDetail.getIsDefault().toString());
129        edit.setSortOrder(cancellationReasonDetail.getSortOrder().toString());
130
131        if(cancellationReasonDescription != null) {
132            edit.setDescription(cancellationReasonDescription.getDescription());
133        }
134    }
135
136    @Override
137    public void canUpdate(CancellationReason cancellationReason) {
138        var cancellationKind = cancellationReason.getLastDetail().getCancellationKind();
139        var cancellationReasonName = edit.getCancellationReasonName();
140        var duplicateCancellationReason = cancellationPolicyControl.getCancellationReasonByName(cancellationKind, cancellationReasonName);
141
142        if(duplicateCancellationReason != null && !cancellationReason.equals(duplicateCancellationReason)) {
143            addExecutionError(ExecutionErrors.DuplicateCancellationReasonName.name(), cancellationReasonName);
144        }
145    }
146
147    @Override
148    public void doUpdate(CancellationReason cancellationReason) {
149        var partyPK = getPartyPK();
150        var cancellationReasonDetailValue = cancellationPolicyControl.getCancellationReasonDetailValueForUpdate(cancellationReason);
151        var cancellationReasonDescription = cancellationPolicyControl.getCancellationReasonDescriptionForUpdate(cancellationReason, getPreferredLanguage());
152        var description = edit.getDescription();
153
154        cancellationReasonDetailValue.setCancellationReasonName(edit.getCancellationReasonName());
155        cancellationReasonDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
156        cancellationReasonDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
157
158        cancellationPolicyControl.updateCancellationReasonFromValue(cancellationReasonDetailValue, partyPK);
159
160        if(cancellationReasonDescription == null && description != null) {
161            cancellationPolicyControl.createCancellationReasonDescription(cancellationReason, getPreferredLanguage(), description, partyPK);
162        } else if(cancellationReasonDescription != null && description == null) {
163            cancellationPolicyControl.deleteCancellationReasonDescription(cancellationReasonDescription, partyPK);
164        } else if(cancellationReasonDescription != null && description != null) {
165            var cancellationReasonDescriptionValue = cancellationPolicyControl.getCancellationReasonDescriptionValue(cancellationReasonDescription);
166
167            cancellationReasonDescriptionValue.setDescription(description);
168            cancellationPolicyControl.updateCancellationReasonDescriptionFromValue(cancellationReasonDescriptionValue, partyPK);
169        }
170    }
171    
172}