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.form.EditCancellationKindForm;
022import com.echothree.control.user.cancellationpolicy.common.result.CancellationPolicyResultFactory;
023import com.echothree.control.user.cancellationpolicy.common.spec.CancellationKindSpec;
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.control.sequence.server.control.SequenceControl;
029import com.echothree.model.data.sequence.server.entity.SequenceType;
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 EditCancellationKindCommand
046        extends BaseEditCommand<CancellationKindSpec, CancellationKindEdit> {
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.CancellationKind.name(), SecurityRoles.Edit.name())
057                        ))
058                ));
059        
060        SPEC_FIELD_DEFINITIONS = List.of(
061                new FieldDefinition("CancellationKindName", FieldType.ENTITY_NAME, true, null, null)
062                );
063        
064        EDIT_FIELD_DEFINITIONS = List.of(
065                new FieldDefinition("CancellationKindName", FieldType.ENTITY_NAME, true, null, null),
066                new FieldDefinition("CancellationSequenceTypeName", FieldType.ENTITY_NAME, false, null, null),
067                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
068                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
069                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
070                );
071    }
072    
073    /** Creates a new instance of EditCancellationKindCommand */
074    public EditCancellationKindCommand() {
075        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
076    }
077    
078    @Override
079    protected BaseResult execute() {
080        var cancellationPolicyControl = Session.getModelController(CancellationPolicyControl.class);
081        var result = CancellationPolicyResultFactory.getEditCancellationKindResult();
082        
083        if(editMode.equals(EditMode.LOCK)) {
084            var cancellationKindName = spec.getCancellationKindName();
085            var cancellationKind = cancellationPolicyControl.getCancellationKindByName(cancellationKindName);
086            
087            if(cancellationKind != null) {
088                result.setCancellationKind(cancellationPolicyControl.getCancellationKindTransfer(getUserVisit(), cancellationKind));
089                
090                if(lockEntity(cancellationKind)) {
091                    var cancellationKindDescription = cancellationPolicyControl.getCancellationKindDescription(cancellationKind, getPreferredLanguage());
092                    var edit = CancellationPolicyEditFactory.getCancellationKindEdit();
093                    var cancellationKindDetail = cancellationKind.getLastDetail();
094                    var cancellationSequenceType = cancellationKindDetail.getCancellationSequenceType();
095                    
096                    result.setEdit(edit);
097                    edit.setCancellationKindName(cancellationKindDetail.getCancellationKindName());
098                    edit.setCancellationSequenceTypeName(cancellationSequenceType == null? null: cancellationSequenceType.getLastDetail().getSequenceTypeName());
099                    edit.setIsDefault(cancellationKindDetail.getIsDefault().toString());
100                    edit.setSortOrder(cancellationKindDetail.getSortOrder().toString());
101                    
102                    if(cancellationKindDescription != null)
103                        edit.setDescription(cancellationKindDescription.getDescription());
104                } else {
105                    addExecutionError(ExecutionErrors.EntityLockFailed.name());
106                }
107                
108                result.setEntityLock(getEntityLockTransfer(cancellationKind));
109            } else {
110                addExecutionError(ExecutionErrors.UnknownCancellationKindName.name(), cancellationKindName);
111            }
112        } else if(editMode.equals(EditMode.UPDATE)) {
113            var cancellationKindName = spec.getCancellationKindName();
114            var cancellationKind = cancellationPolicyControl.getCancellationKindByNameForUpdate(cancellationKindName);
115            
116            if(cancellationKind != null) {
117                cancellationKindName = edit.getCancellationKindName();
118                var duplicateCancellationKind = cancellationPolicyControl.getCancellationKindByName(cancellationKindName);
119                
120                if(duplicateCancellationKind == null || cancellationKind.equals(duplicateCancellationKind)) {
121                    var sequenceControl = Session.getModelController(SequenceControl.class);
122                    var cancellationSequenceTypeName = edit.getCancellationSequenceTypeName();
123                    SequenceType cancellationSequenceType = null;
124                    
125                    if(cancellationSequenceTypeName != null) {
126                        cancellationSequenceType = sequenceControl.getSequenceTypeByName(cancellationSequenceTypeName);
127                    }
128                    
129                    if(cancellationSequenceTypeName == null || cancellationSequenceType != null) {
130                        if(lockEntityForUpdate(cancellationKind)) {
131                            try {
132                                var partyPK = getPartyPK();
133                                var cancellationKindDetailValue = cancellationPolicyControl.getCancellationKindDetailValueForUpdate(cancellationKind);
134                                var cancellationKindDescription = cancellationPolicyControl.getCancellationKindDescriptionForUpdate(cancellationKind, getPreferredLanguage());
135                                var description = edit.getDescription();
136                                
137                                cancellationKindDetailValue.setCancellationKindName(edit.getCancellationKindName());
138                                cancellationKindDetailValue.setCancellationSequenceTypePK(cancellationSequenceType == null? null: cancellationSequenceType.getPrimaryKey());
139                                cancellationKindDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
140                                cancellationKindDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
141                                
142                                cancellationPolicyControl.updateCancellationKindFromValue(cancellationKindDetailValue, partyPK);
143                                
144                                if(cancellationKindDescription == null && description != null) {
145                                    cancellationPolicyControl.createCancellationKindDescription(cancellationKind, getPreferredLanguage(), description, partyPK);
146                                } else if(cancellationKindDescription != null && description == null) {
147                                    cancellationPolicyControl.deleteCancellationKindDescription(cancellationKindDescription, partyPK);
148                                } else if(cancellationKindDescription != null && description != null) {
149                                    var cancellationKindDescriptionValue = cancellationPolicyControl.getCancellationKindDescriptionValue(cancellationKindDescription);
150                                    
151                                    cancellationKindDescriptionValue.setDescription(description);
152                                    cancellationPolicyControl.updateCancellationKindDescriptionFromValue(cancellationKindDescriptionValue, partyPK);
153                                }
154                            } finally {
155                                unlockEntity(cancellationKind);
156                            }
157                        } else {
158                            addExecutionError(ExecutionErrors.EntityLockStale.name());
159                        }
160                    } else {
161                        addExecutionError(ExecutionErrors.UnknownCancellationSequenceTypeName.name(), cancellationSequenceTypeName);
162                    }
163                } else {
164                    addExecutionError(ExecutionErrors.DuplicateCancellationKindName.name(), cancellationKindName);
165                }
166            } else {
167                addExecutionError(ExecutionErrors.UnknownCancellationKindName.name(), cancellationKindName);
168            }
169        }
170        
171        return result;
172    }
173    
174}