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