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.result.EditPeriodKindResult;
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.period.server.logic.PeriodKindLogic;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.control.workflow.server.logic.WorkflowEntranceLogic;
030import com.echothree.model.data.period.server.entity.PeriodKind;
031import com.echothree.model.data.workflow.server.entity.WorkflowEntrance;
032import com.echothree.util.common.message.ExecutionErrors;
033import com.echothree.util.common.validation.FieldDefinition;
034import com.echothree.util.common.validation.FieldType;
035import com.echothree.util.server.control.BaseAbstractEditCommand;
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 BaseAbstractEditCommand<PeriodKindSpec, PeriodKindEdit, EditPeriodKindResult, PeriodKind, PeriodKind> {
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    PeriodKindLogic periodKindLogic;
078
079    @Inject
080    WorkflowEntranceLogic workflowEntranceLogic;
081
082    /** Creates a new instance of EditPeriodKindCommand */
083    public EditPeriodKindCommand() {
084        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
085    }
086
087    @Override
088    public EditPeriodKindResult getResult() {
089        return PeriodResultFactory.getEditPeriodKindResult();
090    }
091
092    @Override
093    public PeriodKindEdit getEdit() {
094        return PeriodEditFactory.getPeriodKindEdit();
095    }
096
097    @Override
098    public PeriodKind getEntity(EditPeriodKindResult result) {
099        return periodKindLogic.getPeriodKindByName(this, spec.getPeriodKindName(), editModeToEntityPermission(editMode));
100    }
101
102    @Override
103    public PeriodKind getLockEntity(PeriodKind periodKind) {
104        return periodKind;
105    }
106
107    @Override
108    public void fillInResult(EditPeriodKindResult result, PeriodKind periodKind) {
109        result.setPeriodKind(periodControl.getPeriodKindTransfer(getUserVisit(), periodKind));
110    }
111
112    private WorkflowEntrance workflowEntrance;
113
114    @Override
115    public void doLock(PeriodKindEdit edit, PeriodKind periodKind) {
116        var periodKindDescription = periodControl.getPeriodKindDescription(periodKind, getPreferredLanguage());
117        var periodKindDetail = periodKind.getLastDetail();
118        var workflowEntrance = periodKindDetail.getWorkflowEntrance();
119        var workflow = workflowEntrance == null? null: workflowEntrance.getLastDetail().getWorkflow();
120
121        edit.setPeriodKindName(periodKindDetail.getPeriodKindName());
122        edit.setWorkflowName(workflow == null? null: workflow.getLastDetail().getWorkflowName());
123        edit.setWorkflowEntranceName(workflowEntrance == null? null: workflowEntrance.getLastDetail().getWorkflowEntranceName());
124        edit.setIsDefault(periodKindDetail.getIsDefault().toString());
125        edit.setSortOrder(periodKindDetail.getSortOrder().toString());
126
127        if(periodKindDescription != null) {
128            edit.setDescription(periodKindDescription.getDescription());
129        }
130    }
131
132    @Override
133    public void canUpdate(PeriodKind periodKind) {
134        var periodKindName = edit.getPeriodKindName();
135        var duplicatePeriodKind = periodControl.getPeriodKindByName(periodKindName);
136
137        if(duplicatePeriodKind == null || periodKind.equals(duplicatePeriodKind)) {
138            var workflowName = edit.getWorkflowName();
139            var workflowEntranceName = edit.getWorkflowEntranceName();
140            var parameterCount = (workflowName == null ? 0 : 1) + (workflowEntranceName == null ? 0 : 1);
141
142            if(parameterCount == 0 || parameterCount == 2) {
143                workflowEntrance = parameterCount == 0? null: workflowEntranceLogic.getWorkflowEntranceByName(this, workflowName, workflowEntranceName);
144            } else {
145                addExecutionError(ExecutionErrors.InvalidParameterCount.name());
146            }
147        } else {
148            addExecutionError(ExecutionErrors.DuplicatePeriodKindName.name(), periodKindName);
149        }
150    }
151
152    @Override
153    public void doUpdate(PeriodKind periodKind) {
154        var partyPK = getPartyPK();
155        var periodKindDetailValue = periodControl.getPeriodKindDetailValueForUpdate(periodKind);
156        var periodKindDescription = periodControl.getPeriodKindDescriptionForUpdate(periodKind, getPreferredLanguage());
157        var description = edit.getDescription();
158
159        periodKindDetailValue.setPeriodKindName(edit.getPeriodKindName());
160        periodKindDetailValue.setWorkflowEntrancePK(workflowEntrance == null? null: workflowEntrance.getPrimaryKey());
161        periodKindDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
162        periodKindDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
163
164        periodControl.updatePeriodKindFromValue(periodKindDetailValue, partyPK);
165
166        if(periodKindDescription == null && description != null) {
167            periodControl.createPeriodKindDescription(periodKind, getPreferredLanguage(), description, partyPK);
168        } else if(periodKindDescription != null && description == null) {
169            periodControl.deletePeriodKindDescription(periodKindDescription, partyPK);
170        } else if(periodKindDescription != null && description != null) {
171            var periodKindDescriptionValue = periodControl.getPeriodKindDescriptionValue(periodKindDescription);
172
173            periodKindDescriptionValue.setDescription(description);
174            periodControl.updatePeriodKindDescriptionFromValue(periodKindDescriptionValue, partyPK);
175        }
176    }
177
178}