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.CancellationKindEdit;
020import com.echothree.control.user.cancellationpolicy.common.edit.CancellationPolicyEditFactory;
021import com.echothree.control.user.cancellationpolicy.common.result.CancellationPolicyResultFactory;
022import com.echothree.control.user.cancellationpolicy.common.result.EditCancellationKindResult;
023import com.echothree.control.user.cancellationpolicy.common.spec.CancellationKindSpec;
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.control.sequence.server.control.SequenceControl;
030import com.echothree.model.data.cancellationpolicy.server.entity.CancellationKind;
031import com.echothree.model.data.sequence.server.entity.SequenceType;
032import com.echothree.util.common.message.ExecutionErrors;
033import com.echothree.util.common.validation.FieldDefinition;
034import com.echothree.util.common.validation.FieldType;
035import com.echothree.util.server.control.BaseAbstractEditCommand;
036import com.echothree.util.server.control.CommandSecurityDefinition;
037import com.echothree.util.server.control.PartyTypeDefinition;
038import com.echothree.util.server.control.SecurityRoleDefinition;
039import java.util.List;
040import javax.enterprise.context.Dependent;
041import javax.inject.Inject;
042
043@Dependent
044public class EditCancellationKindCommand
045        extends BaseAbstractEditCommand<CancellationKindSpec, CancellationKindEdit, EditCancellationKindResult, CancellationKind, CancellationKind> {
046    
047    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
048    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
049    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
050    
051    static {
052        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
053                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
054                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
055                        new SecurityRoleDefinition(SecurityRoleGroups.CancellationKind.name(), SecurityRoles.Edit.name())
056                ))
057        ));
058        
059        SPEC_FIELD_DEFINITIONS = List.of(
060                new FieldDefinition("CancellationKindName", FieldType.ENTITY_NAME, true, null, null)
061        );
062        
063        EDIT_FIELD_DEFINITIONS = List.of(
064                new FieldDefinition("CancellationKindName", FieldType.ENTITY_NAME, true, null, null),
065                new FieldDefinition("CancellationSequenceTypeName", FieldType.ENTITY_NAME, false, null, null),
066                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
067                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
068                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
069        );
070    }
071
072    @Inject
073    CancellationPolicyControl cancellationPolicyControl;
074
075    @Inject
076    CancellationKindLogic cancellationKindLogic;
077
078    @Inject
079    SequenceControl sequenceControl;
080    
081    /** Creates a new instance of EditCancellationKindCommand */
082    public EditCancellationKindCommand() {
083        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
084    }
085    
086    @Override
087    public EditCancellationKindResult getResult() {
088        return CancellationPolicyResultFactory.getEditCancellationKindResult();
089    }
090
091    @Override
092    public CancellationKindEdit getEdit() {
093        return CancellationPolicyEditFactory.getCancellationKindEdit();
094    }
095
096    @Override
097    public CancellationKind getEntity(EditCancellationKindResult result) {
098        return cancellationKindLogic.getCancellationKindByName(this, spec.getCancellationKindName(), editModeToEntityPermission(editMode));
099    }
100
101    @Override
102    public CancellationKind getLockEntity(CancellationKind cancellationKind) {
103        return cancellationKind;
104    }
105
106    @Override
107    public void fillInResult(EditCancellationKindResult result, CancellationKind cancellationKind) {
108        result.setCancellationKind(cancellationPolicyControl.getCancellationKindTransfer(getUserVisit(), cancellationKind));
109    }
110
111    @Override
112    public void doLock(CancellationKindEdit edit, CancellationKind cancellationKind) {
113        var cancellationKindDescription = cancellationPolicyControl.getCancellationKindDescription(cancellationKind, getPreferredLanguage());
114        var cancellationKindDetail = cancellationKind.getLastDetail();
115        var cancellationSequenceType = cancellationKindDetail.getCancellationSequenceType();
116
117        edit.setCancellationKindName(cancellationKindDetail.getCancellationKindName());
118        edit.setCancellationSequenceTypeName(cancellationSequenceType == null? null: cancellationSequenceType.getLastDetail().getSequenceTypeName());
119        edit.setIsDefault(cancellationKindDetail.getIsDefault().toString());
120        edit.setSortOrder(cancellationKindDetail.getSortOrder().toString());
121
122        if(cancellationKindDescription != null) {
123            edit.setDescription(cancellationKindDescription.getDescription());
124        }
125    }
126
127    SequenceType cancellationSequenceType;
128
129    @Override
130    public void canUpdate(CancellationKind cancellationKind) {
131        var cancellationKindName = edit.getCancellationKindName();
132        var duplicateCancellationKind = cancellationPolicyControl.getCancellationKindByName(cancellationKindName);
133
134        if(duplicateCancellationKind == null || cancellationKind.equals(duplicateCancellationKind)) {
135            var cancellationSequenceTypeName = edit.getCancellationSequenceTypeName();
136
137            if(cancellationSequenceTypeName != null) {
138                cancellationSequenceType = sequenceControl.getSequenceTypeByName(cancellationSequenceTypeName);
139            }
140
141            if(cancellationSequenceTypeName != null && cancellationSequenceType == null) {
142                addExecutionError(ExecutionErrors.UnknownCancellationSequenceTypeName.name(), cancellationSequenceTypeName);
143            }
144        } else {
145            addExecutionError(ExecutionErrors.DuplicateCancellationKindName.name(), cancellationKindName);
146        }
147    }
148
149    @Override
150    public void doUpdate(CancellationKind cancellationKind) {
151        var partyPK = getPartyPK();
152        var cancellationKindDetailValue = cancellationPolicyControl.getCancellationKindDetailValueForUpdate(cancellationKind);
153        var cancellationKindDescription = cancellationPolicyControl.getCancellationKindDescriptionForUpdate(cancellationKind, getPreferredLanguage());
154        var description = edit.getDescription();
155
156        cancellationKindDetailValue.setCancellationKindName(edit.getCancellationKindName());
157        cancellationKindDetailValue.setCancellationSequenceTypePK(cancellationSequenceType == null? null: cancellationSequenceType.getPrimaryKey());
158        cancellationKindDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
159        cancellationKindDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
160
161        cancellationPolicyControl.updateCancellationKindFromValue(cancellationKindDetailValue, partyPK);
162
163        if(cancellationKindDescription == null && description != null) {
164            cancellationPolicyControl.createCancellationKindDescription(cancellationKind, getPreferredLanguage(), description, partyPK);
165        } else if(cancellationKindDescription != null && description == null) {
166            cancellationPolicyControl.deleteCancellationKindDescription(cancellationKindDescription, partyPK);
167        } else if(cancellationKindDescription != null && description != null) {
168            var cancellationKindDescriptionValue = cancellationPolicyControl.getCancellationKindDescriptionValue(cancellationKindDescription);
169
170            cancellationKindDescriptionValue.setDescription(description);
171            cancellationPolicyControl.updateCancellationKindDescriptionFromValue(cancellationKindDescriptionValue, partyPK);
172        }
173    }
174    
175}