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