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