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.ChainActionSetEdit;
020import com.echothree.control.user.chain.common.edit.ChainEditFactory;
021import com.echothree.control.user.chain.common.form.EditChainActionSetForm;
022import com.echothree.control.user.chain.common.result.ChainResultFactory;
023import com.echothree.control.user.chain.common.result.EditChainActionSetResult;
024import com.echothree.control.user.chain.common.spec.ChainActionSetSpec;
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.Chain;
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 com.echothree.util.server.persistence.Session;
041import java.util.List;
042import javax.enterprise.context.Dependent;
043
044@Dependent
045public class EditChainActionSetCommand
046        extends BaseAbstractEditCommand<ChainActionSetSpec, ChainActionSetEdit, EditChainActionSetResult, ChainActionSet, ChainActionSet> {
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.ChainActionSet.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                );
066
067        EDIT_FIELD_DEFINITIONS = List.of(
068                new FieldDefinition("ChainActionSetName", FieldType.ENTITY_NAME, true, null, null),
069                new FieldDefinition("IsDefault", FieldType.BOOLEAN, 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    /** Creates a new instance of EditChainActionSetCommand */
076    public EditChainActionSetCommand() {
077        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
078    }
079
080    @Override
081    public EditChainActionSetResult getResult() {
082        return ChainResultFactory.getEditChainActionSetResult();
083    }
084
085    @Override
086    public ChainActionSetEdit getEdit() {
087        return ChainEditFactory.getChainActionSetEdit();
088    }
089
090    Chain chain;
091
092    @Override
093    public ChainActionSet getEntity(EditChainActionSetResult result) {
094        var chainControl = Session.getModelController(ChainControl.class);
095        ChainActionSet chainActionSet = 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                
106                chain = chainControl.getChainByName(chainType, chainName);
107
108                if(chain != null) {
109                    var chainActionSetName = spec.getChainActionSetName();
110
111                    if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
112                        chainActionSet = chainControl.getChainActionSetByName(chain, chainActionSetName);
113                    } else { // EditMode.UPDATE
114                        chainActionSet = chainControl.getChainActionSetByNameForUpdate(chain, chainActionSetName);
115                    }
116
117                    if(chainActionSet == null) {
118                        addExecutionError(ExecutionErrors.UnknownChainActionSetName.name(), chainKindName, chainTypeName, chainName, chainActionSetName);
119                    }
120                } else {
121                    addExecutionError(ExecutionErrors.UnknownChainName.name(), chainKindName, chainTypeName, chainName);
122                }
123            } else {
124                addExecutionError(ExecutionErrors.UnknownChainTypeName.name(), chainKindName, chainTypeName);
125            }
126        } else {
127            addExecutionError(ExecutionErrors.UnknownChainKindName.name(), chainKindName);
128        }
129
130        return chainActionSet;
131    }
132
133    @Override
134    public ChainActionSet getLockEntity(ChainActionSet chainActionSet) {
135        return chainActionSet;
136    }
137
138    @Override
139    public void fillInResult(EditChainActionSetResult result, ChainActionSet chainActionSet) {
140        var chainControl = Session.getModelController(ChainControl.class);
141
142        result.setChainActionSet(chainControl.getChainActionSetTransfer(getUserVisit(), chainActionSet));
143    }
144
145    @Override
146    public void doLock(ChainActionSetEdit edit, ChainActionSet chainActionSet) {
147        var chainControl = Session.getModelController(ChainControl.class);
148        var chainActionSetDescription = chainControl.getChainActionSetDescription(chainActionSet, getPreferredLanguage());
149        var chainActionSetDetail = chainActionSet.getLastDetail();
150
151        edit.setChainActionSetName(chainActionSetDetail.getChainActionSetName());
152        edit.setIsDefault(chainActionSetDetail.getIsDefault().toString());
153        edit.setSortOrder(chainActionSetDetail.getSortOrder().toString());
154
155        if(chainActionSetDescription != null) {
156            edit.setDescription(chainActionSetDescription.getDescription());
157        }
158    }
159
160    @Override
161    public void canUpdate(ChainActionSet chainActionSet) {
162        var chainControl = Session.getModelController(ChainControl.class);
163        var chainActionSetName = edit.getChainActionSetName();
164        var duplicateChainActionSet = chainControl.getChainActionSetByName(chain, chainActionSetName);
165
166        if(duplicateChainActionSet != null && !chainActionSet.equals(duplicateChainActionSet)) {
167            var chainDetail = chain.getLastDetail();
168            var chainTypeDetail = chainDetail.getChainType().getLastDetail();
169            var chainKindDetail = chainTypeDetail.getChainKind().getLastDetail();
170            
171            addExecutionError(ExecutionErrors.DuplicateChainActionSetName.name(), chainKindDetail.getChainKindName(), chainTypeDetail.getChainTypeName(),
172                    chainDetail.getChainName(), chainActionSetName);
173        }
174    }
175
176    @Override
177    public void doUpdate(ChainActionSet chainActionSet) {
178        var chainControl = Session.getModelController(ChainControl.class);
179        var partyPK = getPartyPK();
180        var chainActionSetDetailValue = chainControl.getChainActionSetDetailValueForUpdate(chainActionSet);
181        var chainActionSetDescription = chainControl.getChainActionSetDescriptionForUpdate(chainActionSet, getPreferredLanguage());
182        var description = edit.getDescription();
183
184        chainActionSetDetailValue.setChainActionSetName(edit.getChainActionSetName());
185        chainActionSetDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
186        chainActionSetDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
187
188        chainControl.updateChainActionSetFromValue(chainActionSetDetailValue, partyPK);
189
190        if(chainActionSetDescription == null && description != null) {
191            chainControl.createChainActionSetDescription(chainActionSet, getPreferredLanguage(), description, partyPK);
192        } else if(chainActionSetDescription != null && description == null) {
193            chainControl.deleteChainActionSetDescription(chainActionSetDescription, partyPK);
194        } else if(chainActionSetDescription != null && description != null) {
195            var chainActionSetDescriptionValue = chainControl.getChainActionSetDescriptionValue(chainActionSetDescription);
196
197            chainActionSetDescriptionValue.setDescription(description);
198            chainControl.updateChainActionSetDescriptionFromValue(chainActionSetDescriptionValue, partyPK);
199        }
200    }
201
202}