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.ChainActionSetDescriptionEdit;
020import com.echothree.control.user.chain.common.edit.ChainEditFactory;
021import com.echothree.control.user.chain.common.form.EditChainActionSetDescriptionForm;
022import com.echothree.control.user.chain.common.result.ChainResultFactory;
023import com.echothree.control.user.chain.common.result.EditChainActionSetDescriptionResult;
024import com.echothree.control.user.chain.common.spec.ChainActionSetDescriptionSpec;
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.ChainActionSet;
031import com.echothree.model.data.chain.server.entity.ChainActionSetDescription;
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 EditChainActionSetDescriptionCommand
047        extends BaseAbstractEditCommand<ChainActionSetDescriptionSpec, ChainActionSetDescriptionEdit, EditChainActionSetDescriptionResult, ChainActionSetDescription, ChainActionSet> {
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.ChainActionSet.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("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
067                );
068
069        EDIT_FIELD_DEFINITIONS = List.of(
070                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
071                );
072    }
073
074    /** Creates a new instance of EditChainActionSetDescriptionCommand */
075    public EditChainActionSetDescriptionCommand() {
076        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
077    }
078
079    @Override
080    public EditChainActionSetDescriptionResult getResult() {
081        return ChainResultFactory.getEditChainActionSetDescriptionResult();
082    }
083
084    @Override
085    public ChainActionSetDescriptionEdit getEdit() {
086        return ChainEditFactory.getChainActionSetDescriptionEdit();
087    }
088
089    @Override
090    public ChainActionSetDescription getEntity(EditChainActionSetDescriptionResult result) {
091        var chainControl = Session.getModelController(ChainControl.class);
092        ChainActionSetDescription chainActionSetDescription = null;
093        var chainKindName = spec.getChainKindName();
094        var chainKind = chainControl.getChainKindByName(chainKindName);
095
096        if(chainKind != null) {
097            var chainTypeName = spec.getChainTypeName();
098            var chainType = chainControl.getChainTypeByName(chainKind, chainTypeName);
099
100            if(chainType != null) {
101                var chainName = spec.getChainName();
102                var chain = chainControl.getChainByName(chainType, chainName);
103
104                if(chain != null) {
105                    var chainActionSetName = spec.getChainActionSetName();
106                    var chainActionSet = chainControl.getChainActionSetByName(chain, chainActionSetName);
107
108                    if(chainActionSet != null) {
109                        var partyControl = Session.getModelController(PartyControl.class);
110                        var languageIsoName = spec.getLanguageIsoName();
111                        var language = partyControl.getLanguageByIsoName(languageIsoName);
112
113                        if(language != null) {
114                            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
115                                chainActionSetDescription = chainControl.getChainActionSetDescription(chainActionSet, language);
116                            } else { // EditMode.UPDATE
117                                chainActionSetDescription = chainControl.getChainActionSetDescriptionForUpdate(chainActionSet, language);
118                            }
119
120                            if(chainActionSetDescription == null) {
121                                addExecutionError(ExecutionErrors.UnknownChainActionSetDescription.name(), chainKindName, chainTypeName, chainName, chainActionSetName, languageIsoName);
122                            }
123                        } else {
124                            addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
125                        }
126                    } else {
127                        addExecutionError(ExecutionErrors.UnknownChainActionSetName.name(), chainKindName, chainTypeName, chainName, chainActionSetName);
128                    }
129                } else {
130                    addExecutionError(ExecutionErrors.UnknownChainName.name(), chainKindName, chainTypeName, chainName);
131                }
132            } else {
133                addExecutionError(ExecutionErrors.UnknownChainTypeName.name(), chainKindName, chainTypeName);
134            }
135        } else {
136            addExecutionError(ExecutionErrors.UnknownChainKindName.name(), chainKindName);
137        }
138
139        return chainActionSetDescription;
140    }
141
142    @Override
143    public ChainActionSet getLockEntity(ChainActionSetDescription chainActionSetDescription) {
144        return chainActionSetDescription.getChainActionSet();
145    }
146
147    @Override
148    public void fillInResult(EditChainActionSetDescriptionResult result, ChainActionSetDescription chainActionSetDescription) {
149        var chainControl = Session.getModelController(ChainControl.class);
150
151        result.setChainActionSetDescription(chainControl.getChainActionSetDescriptionTransfer(getUserVisit(), chainActionSetDescription));
152    }
153
154    @Override
155    public void doLock(ChainActionSetDescriptionEdit edit, ChainActionSetDescription chainActionSetDescription) {
156        edit.setDescription(chainActionSetDescription.getDescription());
157    }
158
159    @Override
160    public void doUpdate(ChainActionSetDescription chainActionSetDescription) {
161        var chainControl = Session.getModelController(ChainControl.class);
162        var chainActionSetDescriptionValue = chainControl.getChainActionSetDescriptionValue(chainActionSetDescription);
163
164        chainActionSetDescriptionValue.setDescription(edit.getDescription());
165
166        chainControl.updateChainActionSetDescriptionFromValue(chainActionSetDescriptionValue, getPartyPK());
167    }
168
169}