001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.Arrays;
045import java.util.Collections;
046import java.util.List;
047import javax.enterprise.context.RequestScoped;
048
049@RequestScoped
050public class EditChainCommand
051        extends BaseAbstractEditCommand<ChainSpec, ChainEdit, EditChainResult, Chain, 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.Edit.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                ));
070
071        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
072                new FieldDefinition("ChainName", FieldType.ENTITY_NAME, true, null, null),
073                new FieldDefinition("ChainInstanceSequenceName", FieldType.ENTITY_NAME, false, null, null),
074                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
075                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
076                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
077                ));
078    }
079
080    /** Creates a new instance of EditChainCommand */
081    public EditChainCommand() {
082        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
083    }
084
085    @Override
086    public EditChainResult getResult() {
087        return ChainResultFactory.getEditChainResult();
088    }
089
090    @Override
091    public ChainEdit getEdit() {
092        return ChainEditFactory.getChainEdit();
093    }
094
095    ChainType chainType;
096
097    @Override
098    public Chain getEntity(EditChainResult result) {
099        var chainControl = Session.getModelController(ChainControl.class);
100        Chain chain = null;
101        var chainKindName = spec.getChainKindName();
102        var chainKind = chainControl.getChainKindByName(chainKindName);
103
104        if(chainKind != null) {
105            var chainTypeName = spec.getChainTypeName();
106
107            chainType = chainControl.getChainTypeByName(chainKind, chainTypeName);
108
109            if(chainType != null) {
110                var chainName = spec.getChainName();
111
112                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
113                    chain = chainControl.getChainByName(chainType, chainName);
114                } else { // EditMode.UPDATE
115                    chain = chainControl.getChainByNameForUpdate(chainType, chainName);
116                }
117
118                if(chain == null) {
119                    addExecutionError(ExecutionErrors.UnknownChainName.name(), chainTypeName, chainTypeName, chainName);
120                }
121            } else {
122                addExecutionError(ExecutionErrors.UnknownChainTypeName.name(), chainKindName, chainTypeName);
123            }
124        } else {
125            addExecutionError(ExecutionErrors.UnknownChainKindName.name(), chainKindName);
126        }
127
128        return chain;
129    }
130
131    @Override
132    public Chain getLockEntity(Chain chain) {
133        return chain;
134    }
135
136    @Override
137    public void fillInResult(EditChainResult result, Chain chain) {
138        var chainControl = Session.getModelController(ChainControl.class);
139
140        result.setChain(chainControl.getChainTransfer(getUserVisit(), chain));
141    }
142
143    Sequence chainInstanceSequence;
144    
145    @Override
146    public void doLock(ChainEdit edit, Chain chain) {
147        var chainControl = Session.getModelController(ChainControl.class);
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 chainControl = Session.getModelController(ChainControl.class);
166        var chainTypeDetail = chainType.getLastDetail();
167        var chainName = edit.getChainName();
168        var duplicateChain = chainControl.getChainByName(chainType, chainName);
169
170        if(duplicateChain != null && !chain.equals(duplicateChain)) {
171            addExecutionError(ExecutionErrors.DuplicateChainName.name(), chainTypeDetail.getChainTypeName(), chainName);
172        } else {
173            var sequenceControl = Session.getModelController(SequenceControl.class);
174            var sequenceType = sequenceControl.getSequenceTypeByName(SequenceTypes.CHAIN_INSTANCE.name());
175            var chainInstanceSequenceName = edit.getChainInstanceSequenceName();
176
177            chainInstanceSequence = sequenceControl.getSequenceByName(sequenceType, chainInstanceSequenceName);
178            
179            if(chainInstanceSequenceName != null && chainInstanceSequence == null) {
180                addExecutionError(ExecutionErrors.UnknownChainInstanceSequenceName.name(), chainInstanceSequenceName);
181            }
182        }
183    }
184
185    @Override
186    public void doUpdate(Chain chain) {
187        var chainControl = Session.getModelController(ChainControl.class);
188        var partyPK = getPartyPK();
189        var chainDetailValue = chainControl.getChainDetailValueForUpdate(chain);
190        var chainDescription = chainControl.getChainDescriptionForUpdate(chain, getPreferredLanguage());
191        var description = edit.getDescription();
192
193        chainDetailValue.setChainName(edit.getChainName());
194        chainDetailValue.setChainInstanceSequencePK(chainInstanceSequence == null ? null : chainInstanceSequence.getPrimaryKey());
195        chainDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
196        chainDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
197
198        chainControl.updateChainFromValue(chainDetailValue, partyPK);
199
200        if(chainDescription == null && description != null) {
201            chainControl.createChainDescription(chain, getPreferredLanguage(), description, partyPK);
202        } else if(chainDescription != null && description == null) {
203            chainControl.deleteChainDescription(chainDescription, partyPK);
204        } else if(chainDescription != null && description != null) {
205            var chainDescriptionValue = chainControl.getChainDescriptionValue(chainDescription);
206
207            chainDescriptionValue.setDescription(description);
208            chainControl.updateChainDescriptionFromValue(chainDescriptionValue, partyPK);
209        }
210    }
211
212}