001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 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.user.common.pk.UserVisitPK;
033import com.echothree.util.common.message.ExecutionErrors;
034import com.echothree.util.common.validation.FieldDefinition;
035import com.echothree.util.common.validation.FieldType;
036import com.echothree.util.common.command.EditMode;
037import com.echothree.util.server.control.BaseAbstractEditCommand;
038import com.echothree.util.server.control.CommandSecurityDefinition;
039import com.echothree.util.server.control.PartyTypeDefinition;
040import com.echothree.util.server.control.SecurityRoleDefinition;
041import java.util.List;
042import javax.enterprise.context.Dependent;
043import javax.inject.Inject;
044
045@Dependent
046public class EditChainDescriptionCommand
047        extends BaseAbstractEditCommand<ChainDescriptionSpec, ChainDescriptionEdit, EditChainDescriptionResult, ChainDescription, Chain> {
048
049    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
050    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
051    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
052
053    static {
054        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
055                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
056                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
057                        new SecurityRoleDefinition(SecurityRoleGroups.Chain.name(), SecurityRoles.Description.name())
058                ))
059        ));
060
061        SPEC_FIELD_DEFINITIONS = List.of(
062                new FieldDefinition("ChainKindName", FieldType.ENTITY_NAME, true, null, null),
063                new FieldDefinition("ChainTypeName", FieldType.ENTITY_NAME, true, null, null),
064                new FieldDefinition("ChainName", FieldType.ENTITY_NAME, true, null, null),
065                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
066        );
067
068        EDIT_FIELD_DEFINITIONS = List.of(
069                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
070        );
071    }
072
073    @Inject
074    ChainControl chainControl;
075
076    @Inject
077    PartyControl partyControl;
078
079    /** Creates a new instance of EditChainDescriptionCommand */
080    public EditChainDescriptionCommand() {
081        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
082    }
083
084    @Override
085    public EditChainDescriptionResult getResult() {
086        return ChainResultFactory.getEditChainDescriptionResult();
087    }
088
089    @Override
090    public ChainDescriptionEdit getEdit() {
091        return ChainEditFactory.getChainDescriptionEdit();
092    }
093
094    @Override
095    public ChainDescription getEntity(EditChainDescriptionResult result) {
096        ChainDescription chainDescription = null;
097        var chainKindName = spec.getChainKindName();
098        var chainKind = chainControl.getChainKindByName(chainKindName);
099
100        if(chainKind != null) {
101            var chainTypeName = spec.getChainTypeName();
102            var chainType = chainControl.getChainTypeByName(chainKind, chainTypeName);
103
104            if(chainType != null) {
105                var chainName = spec.getChainName();
106                var chain = chainControl.getChainByName(chainType, chainName);
107
108                if(chain != null) {
109                    var languageIsoName = spec.getLanguageIsoName();
110                    var 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        result.setChainDescription(chainControl.getChainDescriptionTransfer(getUserVisit(), chainDescription));
146    }
147
148    @Override
149    public void doLock(ChainDescriptionEdit edit, ChainDescription chainDescription) {
150        edit.setDescription(chainDescription.getDescription());
151    }
152
153    @Override
154    public void doUpdate(ChainDescription chainDescription) {
155        var chainDescriptionValue = chainControl.getChainDescriptionValue(chainDescription);
156
157        chainDescriptionValue.setDescription(edit.getDescription());
158
159        chainControl.updateChainDescriptionFromValue(chainDescriptionValue, getPartyPK());
160    }
161
162}