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