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