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