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