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.chain.server.command;
018
019import com.echothree.control.user.chain.common.edit.ChainActionEdit;
020import com.echothree.control.user.chain.common.edit.ChainEditFactory;
021import com.echothree.control.user.chain.common.form.EditChainActionForm;
022import com.echothree.control.user.chain.common.result.ChainResultFactory;
023import com.echothree.control.user.chain.common.result.EditChainActionResult;
024import com.echothree.control.user.chain.common.spec.ChainActionSpec;
025import com.echothree.model.control.chain.server.control.ChainControl;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.data.chain.server.entity.ChainAction;
030import com.echothree.model.data.chain.server.entity.ChainActionSet;
031import com.echothree.model.data.user.common.pk.UserVisitPK;
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.common.command.EditMode;
036import com.echothree.util.server.control.BaseAbstractEditCommand;
037import com.echothree.util.server.control.CommandSecurityDefinition;
038import com.echothree.util.server.control.PartyTypeDefinition;
039import com.echothree.util.server.control.SecurityRoleDefinition;
040import java.util.List;
041import javax.enterprise.context.Dependent;
042import javax.inject.Inject;
043
044@Dependent
045public class EditChainActionCommand
046        extends BaseAbstractEditCommand<ChainActionSpec, ChainActionEdit, EditChainActionResult, ChainAction, ChainAction> {
047
048    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
049    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
050    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
051
052    static {
053        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
054                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
055                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
056                        new SecurityRoleDefinition(SecurityRoleGroups.ChainAction.name(), SecurityRoles.Edit.name())
057                ))
058        ));
059
060        SPEC_FIELD_DEFINITIONS = List.of(
061                new FieldDefinition("ChainKindName", FieldType.ENTITY_NAME, true, null, null),
062                new FieldDefinition("ChainTypeName", FieldType.ENTITY_NAME, true, null, null),
063                new FieldDefinition("ChainName", FieldType.ENTITY_NAME, true, null, null),
064                new FieldDefinition("ChainActionSetName", FieldType.ENTITY_NAME, true, null, null),
065                new FieldDefinition("ChainActionName", FieldType.ENTITY_NAME, true, null, null)
066        );
067
068        EDIT_FIELD_DEFINITIONS = List.of(
069                new FieldDefinition("ChainActionName", FieldType.ENTITY_NAME, 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    @Inject
076    ChainControl chainControl;
077
078    /** Creates a new instance of EditChainActionCommand */
079    public EditChainActionCommand() {
080        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
081    }
082
083    @Override
084    public EditChainActionResult getResult() {
085        return ChainResultFactory.getEditChainActionResult();
086    }
087
088    @Override
089    public ChainActionEdit getEdit() {
090        return ChainEditFactory.getChainActionEdit();
091    }
092
093    ChainActionSet chainActionSet;
094
095    @Override
096    public ChainAction getEntity(EditChainActionResult result) {
097        ChainAction chainAction = null;
098        var chainKindName = spec.getChainKindName();
099        var chainKind = chainControl.getChainKindByName(chainKindName);
100
101        if(chainKind != null) {
102            var chainTypeName = spec.getChainTypeName();
103            var chainType = chainControl.getChainTypeByName(chainKind, chainTypeName);
104
105            if(chainType != null) {
106                var chainName = spec.getChainName();
107                var chain = chainControl.getChainByName(chainType, chainName);
108
109                if(chain != null) {
110                    var chainActionSetName = spec.getChainActionSetName();
111                    
112                    chainActionSet = chainControl.getChainActionSetByName(chain, chainActionSetName);
113
114                    if(chainActionSet != null) {
115                        var chainActionName = spec.getChainActionName();
116
117                        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
118                            chainAction = chainControl.getChainActionByName(chainActionSet, chainActionName);
119                        } else { // EditMode.UPDATE
120                            chainAction = chainControl.getChainActionByNameForUpdate(chainActionSet, chainActionName);
121                        }
122
123                        if(chainAction == null) {
124                            addExecutionError(ExecutionErrors.UnknownChainActionName.name(), chainKindName, chainTypeName, chainName, chainActionSetName,
125                                    chainActionName);
126                        }
127                    } else {
128                        addExecutionError(ExecutionErrors.UnknownChainActionSetName.name(), chainKindName, chainTypeName, chainName, chainActionSetName);
129                    }
130                } else {
131                    addExecutionError(ExecutionErrors.UnknownChainName.name(), chainKindName, chainTypeName, chainName);
132                }
133            } else {
134                addExecutionError(ExecutionErrors.UnknownChainTypeName.name(), chainKindName, chainTypeName);
135            }
136        } else {
137            addExecutionError(ExecutionErrors.UnknownChainKindName.name(), chainKindName);
138        }
139
140        return chainAction;
141    }
142
143    @Override
144    public ChainAction getLockEntity(ChainAction chainAction) {
145        return chainAction;
146    }
147
148    @Override
149    public void fillInResult(EditChainActionResult result, ChainAction chainAction) {
150        result.setChainAction(chainControl.getChainActionTransfer(getUserVisit(), chainAction));
151    }
152
153    @Override
154    public void doLock(ChainActionEdit edit, ChainAction chainAction) {
155        var chainActionDescription = chainControl.getChainActionDescription(chainAction, getPreferredLanguage());
156        var chainActionDetail = chainAction.getLastDetail();
157
158        edit.setChainActionName(chainActionDetail.getChainActionName());
159        edit.setSortOrder(chainActionDetail.getSortOrder().toString());
160
161        if(chainActionDescription != null) {
162            edit.setDescription(chainActionDescription.getDescription());
163        }
164    }
165
166    @Override
167    public void canUpdate(ChainAction chainAction) {
168        var chainActionName = edit.getChainActionName();
169        var duplicateChainAction = chainControl.getChainActionByName(chainActionSet, chainActionName);
170
171        if(duplicateChainAction != null && !chainAction.equals(duplicateChainAction)) {
172            var chainActionSetDetail = chainActionSet.getLastDetail();
173            var chainDetail = chainActionSetDetail.getChain().getLastDetail();
174            var chainTypeDetail = chainDetail.getChainType().getLastDetail();
175            var chainKindDetail = chainTypeDetail.getChainKind().getLastDetail();
176            
177            addExecutionError(ExecutionErrors.DuplicateChainActionName.name(), chainKindDetail.getChainKindName(), chainTypeDetail.getChainTypeName(),
178                    chainDetail.getChainName(), chainActionSetDetail.getChainActionSetName(), chainActionName);
179        }
180    }
181
182    @Override
183    public void doUpdate(ChainAction chainAction) {
184        var partyPK = getPartyPK();
185        var chainActionDetailValue = chainControl.getChainActionDetailValueForUpdate(chainAction);
186        var chainActionDescription = chainControl.getChainActionDescriptionForUpdate(chainAction, getPreferredLanguage());
187        var description = edit.getDescription();
188
189        chainActionDetailValue.setChainActionName(edit.getChainActionName());
190        chainActionDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
191
192        chainControl.updateChainActionFromValue(chainActionDetailValue, partyPK);
193
194        if(chainActionDescription == null && description != null) {
195            chainControl.createChainActionDescription(chainAction, getPreferredLanguage(), description, partyPK);
196        } else if(chainActionDescription != null && description == null) {
197            chainControl.deleteChainActionDescription(chainActionDescription, partyPK);
198        } else if(chainActionDescription != null && description != null) {
199            var chainActionDescriptionValue = chainControl.getChainActionDescriptionValue(chainActionDescription);
200
201            chainActionDescriptionValue.setDescription(description);
202            chainControl.updateChainActionDescriptionFromValue(chainActionDescriptionValue, partyPK);
203        }
204    }
205
206}