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 java.util.List;
044import javax.enterprise.context.Dependent;
045import javax.inject.Inject;
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    @Inject
079    ChainControl chainControl;
080
081    @Inject
082    SequenceControl sequenceControl;
083
084    /** Creates a new instance of EditChainCommand */
085    public EditChainCommand() {
086        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
087    }
088
089    @Override
090    public EditChainResult getResult() {
091        return ChainResultFactory.getEditChainResult();
092    }
093
094    @Override
095    public ChainEdit getEdit() {
096        return ChainEditFactory.getChainEdit();
097    }
098
099    ChainType chainType;
100
101    @Override
102    public Chain getEntity(EditChainResult result) {
103        Chain chain = null;
104        var chainKindName = spec.getChainKindName();
105        var chainKind = chainControl.getChainKindByName(chainKindName);
106
107        if(chainKind != null) {
108            var chainTypeName = spec.getChainTypeName();
109
110            chainType = chainControl.getChainTypeByName(chainKind, chainTypeName);
111
112            if(chainType != null) {
113                var chainName = spec.getChainName();
114
115                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
116                    chain = chainControl.getChainByName(chainType, chainName);
117                } else { // EditMode.UPDATE
118                    chain = chainControl.getChainByNameForUpdate(chainType, chainName);
119                }
120
121                if(chain == null) {
122                    addExecutionError(ExecutionErrors.UnknownChainName.name(), chainTypeName, chainTypeName, chainName);
123                }
124            } else {
125                addExecutionError(ExecutionErrors.UnknownChainTypeName.name(), chainKindName, chainTypeName);
126            }
127        } else {
128            addExecutionError(ExecutionErrors.UnknownChainKindName.name(), chainKindName);
129        }
130
131        return chain;
132    }
133
134    @Override
135    public Chain getLockEntity(Chain chain) {
136        return chain;
137    }
138
139    @Override
140    public void fillInResult(EditChainResult result, Chain chain) {
141        result.setChain(chainControl.getChainTransfer(getUserVisit(), chain));
142    }
143
144    Sequence chainInstanceSequence;
145    
146    @Override
147    public void doLock(ChainEdit edit, Chain chain) {
148        var chainDescription = chainControl.getChainDescription(chain, getPreferredLanguage());
149        var chainDetail = chain.getLastDetail();
150        
151        chainInstanceSequence = chainDetail.getChainInstanceSequence();
152
153        edit.setChainName(chainDetail.getChainName());
154        edit.setChainInstanceSequenceName(chainInstanceSequence == null ? null : chainInstanceSequence.getLastDetail().getSequenceName());
155        edit.setIsDefault(chainDetail.getIsDefault().toString());
156        edit.setSortOrder(chainDetail.getSortOrder().toString());
157
158        if(chainDescription != null) {
159            edit.setDescription(chainDescription.getDescription());
160        }
161    }
162
163    @Override
164    public void canUpdate(Chain chain) {
165        var chainTypeDetail = chainType.getLastDetail();
166        var chainName = edit.getChainName();
167        var duplicateChain = chainControl.getChainByName(chainType, chainName);
168
169        if(duplicateChain != null && !chain.equals(duplicateChain)) {
170            addExecutionError(ExecutionErrors.DuplicateChainName.name(), chainTypeDetail.getChainTypeName(), chainName);
171        } else {
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 partyPK = getPartyPK();
186        var chainDetailValue = chainControl.getChainDetailValueForUpdate(chain);
187        var chainDescription = chainControl.getChainDescriptionForUpdate(chain, getPreferredLanguage());
188        var description = edit.getDescription();
189
190        chainDetailValue.setChainName(edit.getChainName());
191        chainDetailValue.setChainInstanceSequencePK(chainInstanceSequence == null ? null : chainInstanceSequence.getPrimaryKey());
192        chainDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
193        chainDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
194
195        chainControl.updateChainFromValue(chainDetailValue, partyPK);
196
197        if(chainDescription == null && description != null) {
198            chainControl.createChainDescription(chain, getPreferredLanguage(), description, partyPK);
199        } else if(chainDescription != null && description == null) {
200            chainControl.deleteChainDescription(chainDescription, partyPK);
201        } else if(chainDescription != null && description != null) {
202            var chainDescriptionValue = chainControl.getChainDescriptionValue(chainDescription);
203
204            chainDescriptionValue.setDescription(description);
205            chainControl.updateChainDescriptionFromValue(chainDescriptionValue, partyPK);
206        }
207    }
208
209}