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.period.server.command;
018
019import com.echothree.control.user.period.common.edit.PeriodEditFactory;
020import com.echothree.control.user.period.common.edit.PeriodKindEdit;
021import com.echothree.control.user.period.common.form.EditPeriodKindForm;
022import com.echothree.control.user.period.common.result.PeriodResultFactory;
023import com.echothree.control.user.period.common.spec.PeriodKindSpec;
024import com.echothree.model.control.party.common.PartyTypes;
025import com.echothree.model.control.period.server.control.PeriodControl;
026import com.echothree.model.control.security.common.SecurityRoleGroups;
027import com.echothree.model.control.security.common.SecurityRoles;
028import com.echothree.model.control.workflow.server.control.WorkflowControl;
029import com.echothree.model.data.user.common.pk.UserVisitPK;
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.common.command.BaseResult;
034import com.echothree.util.common.command.EditMode;
035import com.echothree.util.server.control.BaseEditCommand;
036import com.echothree.util.server.control.CommandSecurityDefinition;
037import com.echothree.util.server.control.PartyTypeDefinition;
038import com.echothree.util.server.control.SecurityRoleDefinition;
039import com.echothree.util.server.persistence.Session;
040import java.util.Arrays;
041import java.util.Collections;
042import java.util.List;
043import javax.enterprise.context.RequestScoped;
044
045@RequestScoped
046public class EditPeriodKindCommand
047        extends BaseEditCommand<PeriodKindSpec, PeriodKindEdit> {
048    
049    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
050    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
051    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
052    
053    static {
054        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
055                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
056                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
057                    new SecurityRoleDefinition(SecurityRoleGroups.PeriodKind.name(), SecurityRoles.Edit.name())
058                    )))
059                )));
060        
061        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
062                new FieldDefinition("PeriodKindName", FieldType.ENTITY_NAME, true, null, null)
063                ));
064        
065        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
066                new FieldDefinition("PeriodKindName", FieldType.ENTITY_NAME, true, null, null),
067                new FieldDefinition("WorkflowName", FieldType.ENTITY_NAME, false, null, null),
068                new FieldDefinition("WorkflowEntranceName", 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 EditPeriodKindCommand */
076    public EditPeriodKindCommand() {
077        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
078    }
079    
080    @Override
081    protected BaseResult execute() {
082        var periodControl = Session.getModelController(PeriodControl.class);
083        var result = PeriodResultFactory.getEditPeriodKindResult();
084        
085        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
086            var periodKindName = spec.getPeriodKindName();
087            var periodKind = periodControl.getPeriodKindByName(periodKindName);
088            
089            if(periodKind != null) {
090                if(editMode.equals(EditMode.LOCK)) {
091                    if(lockEntity(periodKind)) {
092                        var periodKindDescription = periodControl.getPeriodKindDescription(periodKind, getPreferredLanguage());
093                        var edit = PeriodEditFactory.getPeriodKindEdit();
094                        var periodKindDetail = periodKind.getLastDetail();
095                        var workflowEntrance = periodKindDetail.getWorkflowEntrance();
096                        var workflow = workflowEntrance == null? null: workflowEntrance.getLastDetail().getWorkflow();
097
098                        result.setPeriodKind(periodControl.getPeriodKindTransfer(getUserVisit(), periodKind));
099
100                        result.setEdit(edit);
101                        edit.setPeriodKindName(periodKindDetail.getPeriodKindName());
102                        edit.setWorkflowName(workflow == null? null: workflow.getLastDetail().getWorkflowName());
103                        edit.setWorkflowEntranceName(workflowEntrance == null? null: workflowEntrance.getLastDetail().getWorkflowEntranceName());
104                        edit.setIsDefault(periodKindDetail.getIsDefault().toString());
105                        edit.setSortOrder(periodKindDetail.getSortOrder().toString());
106
107                        if(periodKindDescription != null) {
108                            edit.setDescription(periodKindDescription.getDescription());
109                        }
110                    } else {
111                        addExecutionError(ExecutionErrors.EntityLockFailed.name());
112                    }
113
114                    result.setEntityLock(getEntityLockTransfer(periodKind));
115                } else { // EditMode.ABANDON
116                    unlockEntity(periodKind);
117                }
118            } else {
119                addExecutionError(ExecutionErrors.UnknownPeriodKindName.name(), periodKindName);
120            }
121        } else if(editMode.equals(EditMode.UPDATE)) {
122            var periodKindName = spec.getPeriodKindName();
123            var periodKind = periodControl.getPeriodKindByNameForUpdate(periodKindName);
124
125            if(periodKind != null) {
126                periodKindName = edit.getPeriodKindName();
127                var duplicatePeriodKind = periodControl.getPeriodKindByName(periodKindName);
128
129                if(duplicatePeriodKind == null || periodKind.equals(duplicatePeriodKind)) {
130                    var workflowName = edit.getWorkflowName();
131                    var workflowEntranceName = edit.getWorkflowEntranceName();
132                    var parameterCount = (workflowName == null ? 0 : 1) + (workflowEntranceName == null ? 0 : 1);
133
134                    if(parameterCount == 0 || parameterCount == 2) {
135                        var workflowControl = Session.getModelController(WorkflowControl.class);
136                        var workflow = workflowName == null ? null : workflowControl.getWorkflowByName(workflowName);
137
138                        if(workflowName == null || workflow != null) {
139                            var workflowEntrance = workflowEntranceName == null? null: workflowControl.getWorkflowEntranceByName(workflow, workflowEntranceName);
140
141                            if(workflowEntranceName == null || workflowEntrance != null) {
142                                if(lockEntityForUpdate(periodKind)) {
143                                    try {
144                                        var partyPK = getPartyPK();
145                                        var periodKindDetailValue = periodControl.getPeriodKindDetailValueForUpdate(periodKind);
146                                        var periodKindDescription = periodControl.getPeriodKindDescriptionForUpdate(periodKind, getPreferredLanguage());
147                                        var description = edit.getDescription();
148
149                                        periodKindDetailValue.setPeriodKindName(edit.getPeriodKindName());
150                                        periodKindDetailValue.setWorkflowEntrancePK(workflowEntrance == null? null: workflowEntrance.getPrimaryKey());
151                                        periodKindDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
152                                        periodKindDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
153
154                                        periodControl.updatePeriodKindFromValue(periodKindDetailValue, partyPK);
155
156                                        if(periodKindDescription == null && description != null) {
157                                            periodControl.createPeriodKindDescription(periodKind, getPreferredLanguage(), description, partyPK);
158                                        } else if(periodKindDescription != null && description == null) {
159                                            periodControl.deletePeriodKindDescription(periodKindDescription, partyPK);
160                                        } else if(periodKindDescription != null && description != null) {
161                                            var periodKindDescriptionValue = periodControl.getPeriodKindDescriptionValue(periodKindDescription);
162
163                                            periodKindDescriptionValue.setDescription(description);
164                                            periodControl.updatePeriodKindDescriptionFromValue(periodKindDescriptionValue, partyPK);
165                                        }
166                                    } finally {
167                                        unlockEntity(periodKind);
168                                    }
169                                } else {
170                                    addExecutionError(ExecutionErrors.EntityLockStale.name());
171                                }
172                            } else {
173                                addExecutionError(ExecutionErrors.UnknownWorkflowEntranceName.name(), workflowName, workflowEntranceName);
174                            }
175                        } else {
176                            addExecutionError(ExecutionErrors.UnknownWorkflowName.name(), workflowName);
177                        }
178                    } else {
179                        addExecutionError(ExecutionErrors.InvalidParameterCount.name());
180                    }
181                } else {
182                    addExecutionError(ExecutionErrors.DuplicatePeriodKindName.name(), periodKindName);
183                }
184            } else {
185                addExecutionError(ExecutionErrors.UnknownPeriodKindName.name(), periodKindName);
186            }
187        }
188        
189        return result;
190    }
191    
192}