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.ChainEdit;
020import com.echothree.control.user.chain.common.edit.ChainEditFactory;
021import com.echothree.control.user.chain.common.form.EditChainForm;
022import com.echothree.control.user.chain.common.result.ChainResultFactory;
023import com.echothree.control.user.chain.common.result.EditChainResult;
024import com.echothree.control.user.chain.common.spec.ChainSpec;
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.control.sequence.common.SequenceTypes;
030import com.echothree.model.control.sequence.server.control.SequenceControl;
031import com.echothree.model.data.chain.server.entity.Chain;
032import com.echothree.model.data.chain.server.entity.ChainType;
033import com.echothree.model.data.sequence.server.entity.Sequence;
034import com.echothree.model.data.user.common.pk.UserVisitPK;
035import com.echothree.util.common.message.ExecutionErrors;
036import com.echothree.util.common.validation.FieldDefinition;
037import com.echothree.util.common.validation.FieldType;
038import com.echothree.util.common.command.EditMode;
039import com.echothree.util.server.control.BaseAbstractEditCommand;
040import com.echothree.util.server.control.CommandSecurityDefinition;
041import com.echothree.util.server.control.PartyTypeDefinition;
042import com.echothree.util.server.control.SecurityRoleDefinition;
043import com.echothree.util.server.persistence.Session;
044import java.util.List;
045import javax.enterprise.context.Dependent;
046
047@Dependent
048public class EditChainCommand
049        extends BaseAbstractEditCommand<ChainSpec, ChainEdit, EditChainResult, Chain, Chain> {
050
051    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
052    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
053    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
054
055    static {
056        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
057                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
058                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
059                        new SecurityRoleDefinition(SecurityRoleGroups.Chain.name(), SecurityRoles.Edit.name())
060                        ))
061                ));
062
063        SPEC_FIELD_DEFINITIONS = List.of(
064                new FieldDefinition("ChainKindName", FieldType.ENTITY_NAME, true, null, null),
065                new FieldDefinition("ChainTypeName", FieldType.ENTITY_NAME, true, null, null),
066                new FieldDefinition("ChainName", FieldType.ENTITY_NAME, true, null, null)
067                );
068
069        EDIT_FIELD_DEFINITIONS = List.of(
070                new FieldDefinition("ChainName", FieldType.ENTITY_NAME, true, null, null),
071                new FieldDefinition("ChainInstanceSequenceName", FieldType.ENTITY_NAME, false, null, null),
072                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
073                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
074                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
075                );
076    }
077
078    /** Creates a new instance of EditChainCommand */
079    public EditChainCommand() {
080        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
081    }
082
083    @Override
084    public EditChainResult getResult() {
085        return ChainResultFactory.getEditChainResult();
086    }
087
088    @Override
089    public ChainEdit getEdit() {
090        return ChainEditFactory.getChainEdit();
091    }
092
093    ChainType chainType;
094
095    @Override
096    public Chain getEntity(EditChainResult result) {
097        var chainControl = Session.getModelController(ChainControl.class);
098        Chain chain = null;
099        var chainKindName = spec.getChainKindName();
100        var chainKind = chainControl.getChainKindByName(chainKindName);
101
102        if(chainKind != null) {
103            var chainTypeName = spec.getChainTypeName();
104
105            chainType = chainControl.getChainTypeByName(chainKind, chainTypeName);
106
107            if(chainType != null) {
108                var chainName = spec.getChainName();
109
110                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
111                    chain = chainControl.getChainByName(chainType, chainName);
112                } else { // EditMode.UPDATE
113                    chain = chainControl.getChainByNameForUpdate(chainType, chainName);
114                }
115
116                if(chain == null) {
117                    addExecutionError(ExecutionErrors.UnknownChainName.name(), chainTypeName, chainTypeName, chainName);
118                }
119            } else {
120                addExecutionError(ExecutionErrors.UnknownChainTypeName.name(), chainKindName, chainTypeName);
121            }
122        } else {
123            addExecutionError(ExecutionErrors.UnknownChainKindName.name(), chainKindName);
124        }
125
126        return chain;
127    }
128
129    @Override
130    public Chain getLockEntity(Chain chain) {
131        return chain;
132    }
133
134    @Override
135    public void fillInResult(EditChainResult result, Chain chain) {
136        var chainControl = Session.getModelController(ChainControl.class);
137
138        result.setChain(chainControl.getChainTransfer(getUserVisit(), chain));
139    }
140
141    Sequence chainInstanceSequence;
142    
143    @Override
144    public void doLock(ChainEdit edit, Chain chain) {
145        var chainControl = Session.getModelController(ChainControl.class);
146        var chainDescription = chainControl.getChainDescription(chain, getPreferredLanguage());
147        var chainDetail = chain.getLastDetail();
148        
149        chainInstanceSequence = chainDetail.getChainInstanceSequence();
150
151        edit.setChainName(chainDetail.getChainName());
152        edit.setChainInstanceSequenceName(chainInstanceSequence == null ? null : chainInstanceSequence.getLastDetail().getSequenceName());
153        edit.setIsDefault(chainDetail.getIsDefault().toString());
154        edit.setSortOrder(chainDetail.getSortOrder().toString());
155
156        if(chainDescription != null) {
157            edit.setDescription(chainDescription.getDescription());
158        }
159    }
160
161    @Override
162    public void canUpdate(Chain chain) {
163        var chainControl = Session.getModelController(ChainControl.class);
164        var chainTypeDetail = chainType.getLastDetail();
165        var chainName = edit.getChainName();
166        var duplicateChain = chainControl.getChainByName(chainType, chainName);
167
168        if(duplicateChain != null && !chain.equals(duplicateChain)) {
169            addExecutionError(ExecutionErrors.DuplicateChainName.name(), chainTypeDetail.getChainTypeName(), chainName);
170        } else {
171            var sequenceControl = Session.getModelController(SequenceControl.class);
172            var sequenceType = sequenceControl.getSequenceTypeByName(SequenceTypes.CHAIN_INSTANCE.name());
173            var chainInstanceSequenceName = edit.getChainInstanceSequenceName();
174
175            chainInstanceSequence = sequenceControl.getSequenceByName(sequenceType, chainInstanceSequenceName);
176            
177            if(chainInstanceSequenceName != null && chainInstanceSequence == null) {
178                addExecutionError(ExecutionErrors.UnknownChainInstanceSequenceName.name(), chainInstanceSequenceName);
179            }
180        }
181    }
182
183    @Override
184    public void doUpdate(Chain chain) {
185        var chainControl = Session.getModelController(ChainControl.class);
186        var partyPK = getPartyPK();
187        var chainDetailValue = chainControl.getChainDetailValueForUpdate(chain);
188        var chainDescription = chainControl.getChainDescriptionForUpdate(chain, getPreferredLanguage());
189        var description = edit.getDescription();
190
191        chainDetailValue.setChainName(edit.getChainName());
192        chainDetailValue.setChainInstanceSequencePK(chainInstanceSequence == null ? null : chainInstanceSequence.getPrimaryKey());
193        chainDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
194        chainDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
195
196        chainControl.updateChainFromValue(chainDetailValue, partyPK);
197
198        if(chainDescription == null && description != null) {
199            chainControl.createChainDescription(chain, getPreferredLanguage(), description, partyPK);
200        } else if(chainDescription != null && description == null) {
201            chainControl.deleteChainDescription(chainDescription, partyPK);
202        } else if(chainDescription != null && description != null) {
203            var chainDescriptionValue = chainControl.getChainDescriptionValue(chainDescription);
204
205            chainDescriptionValue.setDescription(description);
206            chainControl.updateChainDescriptionFromValue(chainDescriptionValue, partyPK);
207        }
208    }
209
210}