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.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 java.util.List;
040import javax.enterprise.context.Dependent;
041import javax.inject.Inject;
042
043@Dependent
044public class EditPeriodKindCommand
045        extends BaseEditCommand<PeriodKindSpec, PeriodKindEdit> {
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.PeriodKind.name(), SecurityRoles.Edit.name())
056                ))
057        ));
058        
059        SPEC_FIELD_DEFINITIONS = List.of(
060                new FieldDefinition("PeriodKindName", FieldType.ENTITY_NAME, true, null, null)
061        );
062        
063        EDIT_FIELD_DEFINITIONS = List.of(
064                new FieldDefinition("PeriodKindName", FieldType.ENTITY_NAME, true, null, null),
065                new FieldDefinition("WorkflowName", FieldType.ENTITY_NAME, false, null, null),
066                new FieldDefinition("WorkflowEntranceName", 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    @Inject
074    PeriodControl periodControl;
075
076    @Inject
077    WorkflowControl workflowControl;
078
079    
080    /** Creates a new instance of EditPeriodKindCommand */
081    public EditPeriodKindCommand() {
082        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
083    }
084    
085    @Override
086    protected BaseResult execute() {
087        var result = PeriodResultFactory.getEditPeriodKindResult();
088        
089        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
090            var periodKindName = spec.getPeriodKindName();
091            var periodKind = periodControl.getPeriodKindByName(periodKindName);
092            
093            if(periodKind != null) {
094                if(editMode.equals(EditMode.LOCK)) {
095                    if(lockEntity(periodKind)) {
096                        var periodKindDescription = periodControl.getPeriodKindDescription(periodKind, getPreferredLanguage());
097                        var edit = PeriodEditFactory.getPeriodKindEdit();
098                        var periodKindDetail = periodKind.getLastDetail();
099                        var workflowEntrance = periodKindDetail.getWorkflowEntrance();
100                        var workflow = workflowEntrance == null? null: workflowEntrance.getLastDetail().getWorkflow();
101
102                        result.setPeriodKind(periodControl.getPeriodKindTransfer(getUserVisit(), periodKind));
103
104                        result.setEdit(edit);
105                        edit.setPeriodKindName(periodKindDetail.getPeriodKindName());
106                        edit.setWorkflowName(workflow == null? null: workflow.getLastDetail().getWorkflowName());
107                        edit.setWorkflowEntranceName(workflowEntrance == null? null: workflowEntrance.getLastDetail().getWorkflowEntranceName());
108                        edit.setIsDefault(periodKindDetail.getIsDefault().toString());
109                        edit.setSortOrder(periodKindDetail.getSortOrder().toString());
110
111                        if(periodKindDescription != null) {
112                            edit.setDescription(periodKindDescription.getDescription());
113                        }
114                    } else {
115                        addExecutionError(ExecutionErrors.EntityLockFailed.name());
116                    }
117
118                    result.setEntityLock(getEntityLockTransfer(periodKind));
119                } else { // EditMode.ABANDON
120                    unlockEntity(periodKind);
121                }
122            } else {
123                addExecutionError(ExecutionErrors.UnknownPeriodKindName.name(), periodKindName);
124            }
125        } else if(editMode.equals(EditMode.UPDATE)) {
126            var periodKindName = spec.getPeriodKindName();
127            var periodKind = periodControl.getPeriodKindByNameForUpdate(periodKindName);
128
129            if(periodKind != null) {
130                periodKindName = edit.getPeriodKindName();
131                var duplicatePeriodKind = periodControl.getPeriodKindByName(periodKindName);
132
133                if(duplicatePeriodKind == null || periodKind.equals(duplicatePeriodKind)) {
134                    var workflowName = edit.getWorkflowName();
135                    var workflowEntranceName = edit.getWorkflowEntranceName();
136                    var parameterCount = (workflowName == null ? 0 : 1) + (workflowEntranceName == null ? 0 : 1);
137
138                    if(parameterCount == 0 || parameterCount == 2) {
139                        var workflow = workflowName == null ? null : workflowControl.getWorkflowByName(workflowName);
140
141                        if(workflowName == null || workflow != null) {
142                            var workflowEntrance = workflowEntranceName == null? null: workflowControl.getWorkflowEntranceByName(workflow, workflowEntranceName);
143
144                            if(workflowEntranceName == null || workflowEntrance != null) {
145                                if(lockEntityForUpdate(periodKind)) {
146                                    try {
147                                        var partyPK = getPartyPK();
148                                        var periodKindDetailValue = periodControl.getPeriodKindDetailValueForUpdate(periodKind);
149                                        var periodKindDescription = periodControl.getPeriodKindDescriptionForUpdate(periodKind, getPreferredLanguage());
150                                        var description = edit.getDescription();
151
152                                        periodKindDetailValue.setPeriodKindName(edit.getPeriodKindName());
153                                        periodKindDetailValue.setWorkflowEntrancePK(workflowEntrance == null? null: workflowEntrance.getPrimaryKey());
154                                        periodKindDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
155                                        periodKindDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
156
157                                        periodControl.updatePeriodKindFromValue(periodKindDetailValue, partyPK);
158
159                                        if(periodKindDescription == null && description != null) {
160                                            periodControl.createPeriodKindDescription(periodKind, getPreferredLanguage(), description, partyPK);
161                                        } else if(periodKindDescription != null && description == null) {
162                                            periodControl.deletePeriodKindDescription(periodKindDescription, partyPK);
163                                        } else if(periodKindDescription != null && description != null) {
164                                            var periodKindDescriptionValue = periodControl.getPeriodKindDescriptionValue(periodKindDescription);
165
166                                            periodKindDescriptionValue.setDescription(description);
167                                            periodControl.updatePeriodKindDescriptionFromValue(periodKindDescriptionValue, partyPK);
168                                        }
169                                    } finally {
170                                        unlockEntity(periodKind);
171                                    }
172                                } else {
173                                    addExecutionError(ExecutionErrors.EntityLockStale.name());
174                                }
175                            } else {
176                                addExecutionError(ExecutionErrors.UnknownWorkflowEntranceName.name(), workflowName, workflowEntranceName);
177                            }
178                        } else {
179                            addExecutionError(ExecutionErrors.UnknownWorkflowName.name(), workflowName);
180                        }
181                    } else {
182                        addExecutionError(ExecutionErrors.InvalidParameterCount.name());
183                    }
184                } else {
185                    addExecutionError(ExecutionErrors.DuplicatePeriodKindName.name(), periodKindName);
186                }
187            } else {
188                addExecutionError(ExecutionErrors.UnknownPeriodKindName.name(), periodKindName);
189            }
190        }
191        
192        return result;
193    }
194    
195}