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.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.Arrays;
042import java.util.Collections;
043import java.util.List;
044import javax.enterprise.context.RequestScoped;
045
046@RequestScoped
047public class EditChainActionSetCommand
048        extends BaseAbstractEditCommand<ChainActionSetSpec, ChainActionSetEdit, EditChainActionSetResult, ChainActionSet, ChainActionSet> {
049
050    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
051    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
052    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
053
054    static {
055        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
056                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
057                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
058                        new SecurityRoleDefinition(SecurityRoleGroups.ChainActionSet.name(), SecurityRoles.Edit.name())
059                        )))
060                )));
061
062        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
063                new FieldDefinition("ChainKindName", FieldType.ENTITY_NAME, true, null, null),
064                new FieldDefinition("ChainTypeName", FieldType.ENTITY_NAME, true, null, null),
065                new FieldDefinition("ChainName", FieldType.ENTITY_NAME, true, null, null),
066                new FieldDefinition("ChainActionSetName", FieldType.ENTITY_NAME, true, null, null)
067                ));
068
069        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
070                new FieldDefinition("ChainActionSetName", FieldType.ENTITY_NAME, true, null, null),
071                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
072                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
073                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
074                ));
075    }
076
077    /** Creates a new instance of EditChainActionSetCommand */
078    public EditChainActionSetCommand() {
079        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
080    }
081
082    @Override
083    public EditChainActionSetResult getResult() {
084        return ChainResultFactory.getEditChainActionSetResult();
085    }
086
087    @Override
088    public ChainActionSetEdit getEdit() {
089        return ChainEditFactory.getChainActionSetEdit();
090    }
091
092    Chain chain;
093
094    @Override
095    public ChainActionSet getEntity(EditChainActionSetResult result) {
096        var chainControl = Session.getModelController(ChainControl.class);
097        ChainActionSet chainActionSet = null;
098        var chainKindName = spec.getChainKindName();
099        var chainKind = chainControl.getChainKindByName(chainKindName);
100
101        if(chainKind != null) {
102            var chainTypeName = spec.getChainTypeName();
103            var chainType = chainControl.getChainTypeByName(chainKind, chainTypeName);
104
105            if(chainType != null) {
106                var chainName = spec.getChainName();
107                
108                chain = chainControl.getChainByName(chainType, chainName);
109
110                if(chain != null) {
111                    var chainActionSetName = spec.getChainActionSetName();
112
113                    if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
114                        chainActionSet = chainControl.getChainActionSetByName(chain, chainActionSetName);
115                    } else { // EditMode.UPDATE
116                        chainActionSet = chainControl.getChainActionSetByNameForUpdate(chain, chainActionSetName);
117                    }
118
119                    if(chainActionSet == null) {
120                        addExecutionError(ExecutionErrors.UnknownChainActionSetName.name(), chainKindName, chainTypeName, chainName, chainActionSetName);
121                    }
122                } else {
123                    addExecutionError(ExecutionErrors.UnknownChainName.name(), chainKindName, chainTypeName, chainName);
124                }
125            } else {
126                addExecutionError(ExecutionErrors.UnknownChainTypeName.name(), chainKindName, chainTypeName);
127            }
128        } else {
129            addExecutionError(ExecutionErrors.UnknownChainKindName.name(), chainKindName);
130        }
131
132        return chainActionSet;
133    }
134
135    @Override
136    public ChainActionSet getLockEntity(ChainActionSet chainActionSet) {
137        return chainActionSet;
138    }
139
140    @Override
141    public void fillInResult(EditChainActionSetResult result, ChainActionSet chainActionSet) {
142        var chainControl = Session.getModelController(ChainControl.class);
143
144        result.setChainActionSet(chainControl.getChainActionSetTransfer(getUserVisit(), chainActionSet));
145    }
146
147    @Override
148    public void doLock(ChainActionSetEdit edit, ChainActionSet chainActionSet) {
149        var chainControl = Session.getModelController(ChainControl.class);
150        var chainActionSetDescription = chainControl.getChainActionSetDescription(chainActionSet, getPreferredLanguage());
151        var chainActionSetDetail = chainActionSet.getLastDetail();
152
153        edit.setChainActionSetName(chainActionSetDetail.getChainActionSetName());
154        edit.setIsDefault(chainActionSetDetail.getIsDefault().toString());
155        edit.setSortOrder(chainActionSetDetail.getSortOrder().toString());
156
157        if(chainActionSetDescription != null) {
158            edit.setDescription(chainActionSetDescription.getDescription());
159        }
160    }
161
162    @Override
163    public void canUpdate(ChainActionSet chainActionSet) {
164        var chainControl = Session.getModelController(ChainControl.class);
165        var chainActionSetName = edit.getChainActionSetName();
166        var duplicateChainActionSet = chainControl.getChainActionSetByName(chain, chainActionSetName);
167
168        if(duplicateChainActionSet != null && !chainActionSet.equals(duplicateChainActionSet)) {
169            var chainDetail = chain.getLastDetail();
170            var chainTypeDetail = chainDetail.getChainType().getLastDetail();
171            var chainKindDetail = chainTypeDetail.getChainKind().getLastDetail();
172            
173            addExecutionError(ExecutionErrors.DuplicateChainActionSetName.name(), chainKindDetail.getChainKindName(), chainTypeDetail.getChainTypeName(),
174                    chainDetail.getChainName(), chainActionSetName);
175        }
176    }
177
178    @Override
179    public void doUpdate(ChainActionSet chainActionSet) {
180        var chainControl = Session.getModelController(ChainControl.class);
181        var partyPK = getPartyPK();
182        var chainActionSetDetailValue = chainControl.getChainActionSetDetailValueForUpdate(chainActionSet);
183        var chainActionSetDescription = chainControl.getChainActionSetDescriptionForUpdate(chainActionSet, getPreferredLanguage());
184        var description = edit.getDescription();
185
186        chainActionSetDetailValue.setChainActionSetName(edit.getChainActionSetName());
187        chainActionSetDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
188        chainActionSetDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
189
190        chainControl.updateChainActionSetFromValue(chainActionSetDetailValue, partyPK);
191
192        if(chainActionSetDescription == null && description != null) {
193            chainControl.createChainActionSetDescription(chainActionSet, getPreferredLanguage(), description, partyPK);
194        } else if(chainActionSetDescription != null && description == null) {
195            chainControl.deleteChainActionSetDescription(chainActionSetDescription, partyPK);
196        } else if(chainActionSetDescription != null && description != null) {
197            var chainActionSetDescriptionValue = chainControl.getChainActionSetDescriptionValue(chainActionSetDescription);
198
199            chainActionSetDescriptionValue.setDescription(description);
200            chainControl.updateChainActionSetDescriptionFromValue(chainActionSetDescriptionValue, partyPK);
201        }
202    }
203
204}