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