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.ChainActionDescriptionEdit;
020import com.echothree.control.user.chain.common.edit.ChainEditFactory;
021import com.echothree.control.user.chain.common.form.EditChainActionDescriptionForm;
022import com.echothree.control.user.chain.common.result.ChainResultFactory;
023import com.echothree.control.user.chain.common.result.EditChainActionDescriptionResult;
024import com.echothree.control.user.chain.common.spec.ChainActionDescriptionSpec;
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.Chain;
031import com.echothree.model.data.chain.server.entity.ChainAction;
032import com.echothree.model.data.chain.server.entity.ChainActionDescription;
033import com.echothree.model.data.chain.server.entity.ChainActionSet;
034import com.echothree.model.data.chain.server.entity.ChainKind;
035import com.echothree.model.data.chain.server.entity.ChainType;
036import com.echothree.model.data.chain.server.value.ChainActionDescriptionValue;
037import com.echothree.model.data.party.server.entity.Language;
038import com.echothree.model.data.user.common.pk.UserVisitPK;
039import com.echothree.util.common.message.ExecutionErrors;
040import com.echothree.util.common.validation.FieldDefinition;
041import com.echothree.util.common.validation.FieldType;
042import com.echothree.util.common.command.EditMode;
043import com.echothree.util.server.control.BaseAbstractEditCommand;
044import com.echothree.util.server.control.CommandSecurityDefinition;
045import com.echothree.util.server.control.PartyTypeDefinition;
046import com.echothree.util.server.control.SecurityRoleDefinition;
047import com.echothree.util.server.persistence.Session;
048import java.util.Arrays;
049import java.util.Collections;
050import java.util.List;
051
052public class EditChainActionDescriptionCommand
053        extends BaseAbstractEditCommand<ChainActionDescriptionSpec, ChainActionDescriptionEdit, EditChainActionDescriptionResult, ChainActionDescription, ChainAction> {
054
055    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
056    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
057    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
058
059    static {
060        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
061                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
062                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
063                        new SecurityRoleDefinition(SecurityRoleGroups.ChainAction.name(), SecurityRoles.Description.name())
064                        )))
065                )));
066
067        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
068                new FieldDefinition("ChainKindName", FieldType.ENTITY_NAME, true, null, null),
069                new FieldDefinition("ChainTypeName", FieldType.ENTITY_NAME, true, null, null),
070                new FieldDefinition("ChainName", FieldType.ENTITY_NAME, true, null, null),
071                new FieldDefinition("ChainActionSetName", FieldType.ENTITY_NAME, true, null, null),
072                new FieldDefinition("ChainActionName", FieldType.ENTITY_NAME, true, null, null),
073                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
074                ));
075
076        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
077                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
078                ));
079    }
080
081    /** Creates a new instance of EditChainActionDescriptionCommand */
082    public EditChainActionDescriptionCommand(UserVisitPK userVisitPK, EditChainActionDescriptionForm form) {
083        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
084    }
085
086    @Override
087    public EditChainActionDescriptionResult getResult() {
088        return ChainResultFactory.getEditChainActionDescriptionResult();
089    }
090
091    @Override
092    public ChainActionDescriptionEdit getEdit() {
093        return ChainEditFactory.getChainActionDescriptionEdit();
094    }
095
096    @Override
097    public ChainActionDescription getEntity(EditChainActionDescriptionResult result) {
098        var chainControl = Session.getModelController(ChainControl.class);
099        ChainActionDescription chainActionDescription = null;
100        String chainKindName = spec.getChainKindName();
101        ChainKind chainKind = chainControl.getChainKindByName(chainKindName);
102
103        if(chainKind != null) {
104            String chainTypeName = spec.getChainTypeName();
105            ChainType chainType = chainControl.getChainTypeByName(chainKind, chainTypeName);
106
107            if(chainType != null) {
108                String chainName = spec.getChainName();
109                Chain chain = chainControl.getChainByName(chainType, chainName);
110
111                if(chain != null) {
112                    String chainActionSetName = spec.getChainActionSetName();
113                    ChainActionSet chainActionSet = chainControl.getChainActionSetByName(chain, chainActionSetName);
114
115                    if(chainActionSet != null) {
116                        String chainActionName = spec.getChainActionName();
117                        ChainAction chainAction = chainControl.getChainActionByName(chainActionSet, chainActionName);
118
119                        if(chainAction != null) {
120                            var partyControl = Session.getModelController(PartyControl.class);
121                            String languageIsoName = spec.getLanguageIsoName();
122                            Language language = partyControl.getLanguageByIsoName(languageIsoName);
123
124                            if(language != null) {
125                                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
126                                    chainActionDescription = chainControl.getChainActionDescription(chainAction, language);
127                                } else { // EditMode.UPDATE
128                                    chainActionDescription = chainControl.getChainActionDescriptionForUpdate(chainAction, language);
129                                }
130
131                                if(chainActionDescription == null) {
132                                    addExecutionError(ExecutionErrors.UnknownChainActionDescription.name(), chainKindName, chainTypeName, chainName,
133                                            chainActionSetName, chainActionName, languageIsoName);
134                                }
135                            } else {
136                                addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
137                            }
138                        } else {
139                            addExecutionError(ExecutionErrors.UnknownChainActionName.name(), chainKindName, chainTypeName, chainName, chainActionSetName,
140                                    chainActionName);
141                        }
142                    } else {
143                        addExecutionError(ExecutionErrors.UnknownChainActionSetName.name(), chainKindName, chainTypeName, chainName, chainActionSetName);
144                    }
145                } else {
146                    addExecutionError(ExecutionErrors.UnknownChainName.name(), chainKindName, chainTypeName, chainName);
147                }
148            } else {
149                addExecutionError(ExecutionErrors.UnknownChainTypeName.name(), chainKindName, chainTypeName);
150            }
151        } else {
152            addExecutionError(ExecutionErrors.UnknownChainKindName.name(), chainKindName);
153        }
154
155        return chainActionDescription;
156    }
157
158    @Override
159    public ChainAction getLockEntity(ChainActionDescription chainActionDescription) {
160        return chainActionDescription.getChainAction();
161    }
162
163    @Override
164    public void fillInResult(EditChainActionDescriptionResult result, ChainActionDescription chainActionDescription) {
165        var chainControl = Session.getModelController(ChainControl.class);
166
167        result.setChainActionDescription(chainControl.getChainActionDescriptionTransfer(getUserVisit(), chainActionDescription));
168    }
169
170    @Override
171    public void doLock(ChainActionDescriptionEdit edit, ChainActionDescription chainActionDescription) {
172        edit.setDescription(chainActionDescription.getDescription());
173    }
174
175    @Override
176    public void doUpdate(ChainActionDescription chainActionDescription) {
177        var chainControl = Session.getModelController(ChainControl.class);
178        ChainActionDescriptionValue chainActionDescriptionValue = chainControl.getChainActionDescriptionValue(chainActionDescription);
179
180        chainActionDescriptionValue.setDescription(edit.getDescription());
181
182        chainControl.updateChainActionDescriptionFromValue(chainActionDescriptionValue, getPartyPK());
183    }
184
185}