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.ChainActionDescriptionEdit;
020import com.echothree.control.user.chain.common.edit.ChainEditFactory;
021import com.echothree.control.user.chain.common.form.EditChainActionDescriptionForm;
022import com.echothree.control.user.chain.common.result.ChainResultFactory;
023import com.echothree.control.user.chain.common.result.EditChainActionDescriptionResult;
024import com.echothree.control.user.chain.common.spec.ChainActionDescriptionSpec;
025import com.echothree.model.control.chain.server.control.ChainControl;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.control.party.server.control.PartyControl;
028import com.echothree.model.control.security.common.SecurityRoleGroups;
029import com.echothree.model.control.security.common.SecurityRoles;
030import com.echothree.model.data.chain.server.entity.ChainAction;
031import com.echothree.model.data.chain.server.entity.ChainActionDescription;
032import com.echothree.model.data.user.common.pk.UserVisitPK;
033import com.echothree.util.common.message.ExecutionErrors;
034import com.echothree.util.common.validation.FieldDefinition;
035import com.echothree.util.common.validation.FieldType;
036import com.echothree.util.common.command.EditMode;
037import com.echothree.util.server.control.BaseAbstractEditCommand;
038import com.echothree.util.server.control.CommandSecurityDefinition;
039import com.echothree.util.server.control.PartyTypeDefinition;
040import com.echothree.util.server.control.SecurityRoleDefinition;
041import com.echothree.util.server.persistence.Session;
042import java.util.List;
043import javax.enterprise.context.Dependent;
044
045@Dependent
046public class EditChainActionDescriptionCommand
047        extends BaseAbstractEditCommand<ChainActionDescriptionSpec, ChainActionDescriptionEdit, EditChainActionDescriptionResult, ChainActionDescription, ChainAction> {
048
049    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
050    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
051    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
052
053    static {
054        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
055                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
056                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
057                        new SecurityRoleDefinition(SecurityRoleGroups.ChainAction.name(), SecurityRoles.Description.name())
058                        ))
059                ));
060
061        SPEC_FIELD_DEFINITIONS = List.of(
062                new FieldDefinition("ChainKindName", FieldType.ENTITY_NAME, true, null, null),
063                new FieldDefinition("ChainTypeName", FieldType.ENTITY_NAME, true, null, null),
064                new FieldDefinition("ChainName", FieldType.ENTITY_NAME, true, null, null),
065                new FieldDefinition("ChainActionSetName", FieldType.ENTITY_NAME, true, null, null),
066                new FieldDefinition("ChainActionName", FieldType.ENTITY_NAME, true, null, null),
067                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
068                );
069
070        EDIT_FIELD_DEFINITIONS = List.of(
071                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
072                );
073    }
074
075    /** Creates a new instance of EditChainActionDescriptionCommand */
076    public EditChainActionDescriptionCommand() {
077        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
078    }
079
080    @Override
081    public EditChainActionDescriptionResult getResult() {
082        return ChainResultFactory.getEditChainActionDescriptionResult();
083    }
084
085    @Override
086    public ChainActionDescriptionEdit getEdit() {
087        return ChainEditFactory.getChainActionDescriptionEdit();
088    }
089
090    @Override
091    public ChainActionDescription getEntity(EditChainActionDescriptionResult result) {
092        var chainControl = Session.getModelController(ChainControl.class);
093        ChainActionDescription chainActionDescription = null;
094        var chainKindName = spec.getChainKindName();
095        var chainKind = chainControl.getChainKindByName(chainKindName);
096
097        if(chainKind != null) {
098            var chainTypeName = spec.getChainTypeName();
099            var chainType = chainControl.getChainTypeByName(chainKind, chainTypeName);
100
101            if(chainType != null) {
102                var chainName = spec.getChainName();
103                var chain = chainControl.getChainByName(chainType, chainName);
104
105                if(chain != null) {
106                    var chainActionSetName = spec.getChainActionSetName();
107                    var chainActionSet = chainControl.getChainActionSetByName(chain, chainActionSetName);
108
109                    if(chainActionSet != null) {
110                        var chainActionName = spec.getChainActionName();
111                        var chainAction = chainControl.getChainActionByName(chainActionSet, chainActionName);
112
113                        if(chainAction != null) {
114                            var partyControl = Session.getModelController(PartyControl.class);
115                            var languageIsoName = spec.getLanguageIsoName();
116                            var language = partyControl.getLanguageByIsoName(languageIsoName);
117
118                            if(language != null) {
119                                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
120                                    chainActionDescription = chainControl.getChainActionDescription(chainAction, language);
121                                } else { // EditMode.UPDATE
122                                    chainActionDescription = chainControl.getChainActionDescriptionForUpdate(chainAction, language);
123                                }
124
125                                if(chainActionDescription == null) {
126                                    addExecutionError(ExecutionErrors.UnknownChainActionDescription.name(), chainKindName, chainTypeName, chainName,
127                                            chainActionSetName, chainActionName, languageIsoName);
128                                }
129                            } else {
130                                addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
131                            }
132                        } else {
133                            addExecutionError(ExecutionErrors.UnknownChainActionName.name(), chainKindName, chainTypeName, chainName, chainActionSetName,
134                                    chainActionName);
135                        }
136                    } else {
137                        addExecutionError(ExecutionErrors.UnknownChainActionSetName.name(), chainKindName, chainTypeName, chainName, chainActionSetName);
138                    }
139                } else {
140                    addExecutionError(ExecutionErrors.UnknownChainName.name(), chainKindName, chainTypeName, chainName);
141                }
142            } else {
143                addExecutionError(ExecutionErrors.UnknownChainTypeName.name(), chainKindName, chainTypeName);
144            }
145        } else {
146            addExecutionError(ExecutionErrors.UnknownChainKindName.name(), chainKindName);
147        }
148
149        return chainActionDescription;
150    }
151
152    @Override
153    public ChainAction getLockEntity(ChainActionDescription chainActionDescription) {
154        return chainActionDescription.getChainAction();
155    }
156
157    @Override
158    public void fillInResult(EditChainActionDescriptionResult result, ChainActionDescription chainActionDescription) {
159        var chainControl = Session.getModelController(ChainControl.class);
160
161        result.setChainActionDescription(chainControl.getChainActionDescriptionTransfer(getUserVisit(), chainActionDescription));
162    }
163
164    @Override
165    public void doLock(ChainActionDescriptionEdit edit, ChainActionDescription chainActionDescription) {
166        edit.setDescription(chainActionDescription.getDescription());
167    }
168
169    @Override
170    public void doUpdate(ChainActionDescription chainActionDescription) {
171        var chainControl = Session.getModelController(ChainControl.class);
172        var chainActionDescriptionValue = chainControl.getChainActionDescriptionValue(chainActionDescription);
173
174        chainActionDescriptionValue.setDescription(edit.getDescription());
175
176        chainControl.updateChainActionDescriptionFromValue(chainActionDescriptionValue, getPartyPK());
177    }
178
179}