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.scale.server.command;
018
019import com.echothree.control.user.scale.common.edit.PartyScaleUseEdit;
020import com.echothree.control.user.scale.common.edit.ScaleEditFactory;
021import com.echothree.control.user.scale.common.form.EditPartyScaleUseForm;
022import com.echothree.control.user.scale.common.result.EditPartyScaleUseResult;
023import com.echothree.control.user.scale.common.result.ScaleResultFactory;
024import com.echothree.control.user.scale.common.spec.PartyScaleUseSpec;
025import com.echothree.model.control.party.common.PartyTypes;
026import com.echothree.model.control.party.server.control.PartyControl;
027import com.echothree.model.control.scale.server.control.ScaleControl;
028import com.echothree.model.control.security.common.SecurityRoleGroups;
029import com.echothree.model.control.security.common.SecurityRoles;
030import com.echothree.model.data.party.server.entity.Party;
031import com.echothree.model.data.scale.server.entity.PartyScaleUse;
032import com.echothree.model.data.scale.server.entity.Scale;
033import com.echothree.model.data.user.common.pk.UserVisitPK;
034import com.echothree.util.common.message.ExecutionErrors;
035import com.echothree.util.common.validation.FieldDefinition;
036import com.echothree.util.common.validation.FieldType;
037import com.echothree.util.common.command.EditMode;
038import com.echothree.util.server.control.BaseAbstractEditCommand;
039import com.echothree.util.server.control.CommandSecurityDefinition;
040import com.echothree.util.server.control.PartyTypeDefinition;
041import com.echothree.util.server.control.SecurityRoleDefinition;
042import java.util.List;
043import javax.enterprise.context.Dependent;
044import javax.inject.Inject;
045
046@Dependent
047public class EditPartyScaleUseCommand
048        extends BaseAbstractEditCommand<PartyScaleUseSpec, PartyScaleUseEdit, EditPartyScaleUseResult, PartyScaleUse, Party> {
049    
050    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
051    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
052    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
053    
054    static {
055        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
056                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
057                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
058                        new SecurityRoleDefinition(SecurityRoleGroups.PartyScaleUse.name(), SecurityRoles.Edit.name())
059                ))
060        ));
061
062        SPEC_FIELD_DEFINITIONS = List.of(
063                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
064                new FieldDefinition("ScaleUseTypeName", FieldType.ENTITY_NAME, true, null, null)
065        );
066
067        EDIT_FIELD_DEFINITIONS = List.of(
068                new FieldDefinition("ScaleName", FieldType.ENTITY_NAME, true, null, null)
069        );
070    }
071
072    @Inject
073    PartyControl partyControl;
074
075    @Inject
076    ScaleControl scaleControl;
077
078    
079    /** Creates a new instance of EditPartyScaleUseCommand */
080    public EditPartyScaleUseCommand() {
081        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
082    }
083    
084    @Override
085    public EditPartyScaleUseResult getResult() {
086        return ScaleResultFactory.getEditPartyScaleUseResult();
087    }
088
089    @Override
090    public PartyScaleUseEdit getEdit() {
091        return ScaleEditFactory.getPartyScaleUseEdit();
092    }
093
094    @Override
095    public PartyScaleUse getEntity(EditPartyScaleUseResult result) {
096        PartyScaleUse partyScaleUse = null;
097        var partyName = spec.getPartyName();
098        Party party;
099
100        if(partyName != null) {
101            party = partyControl.getPartyByName(partyName);
102
103            if(party == null) {
104                addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName);
105            }
106        } else {
107            party = getParty();
108        }
109
110        if(!hasExecutionErrors()) {
111            var scaleUseTypeName = spec.getScaleUseTypeName();
112            var scaleUseType = scaleControl.getScaleUseTypeByName(scaleUseTypeName);
113
114            if(scaleUseType != null) {
115                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
116                    partyScaleUse = scaleControl.getPartyScaleUse(party, scaleUseType);
117                } else { // EditMode.UPDATE
118                    partyScaleUse = scaleControl.getPartyScaleUseForUpdate(party, scaleUseType);
119                }
120
121                if(partyScaleUse == null) {
122                    addExecutionError(ExecutionErrors.UnknownPartyScaleUse.name(), party.getLastDetail().getPartyName(), scaleUseTypeName);
123                }
124            } else {
125                addExecutionError(ExecutionErrors.UnknownScaleUseTypeName.name(), scaleUseTypeName);
126            }
127        }
128
129        return partyScaleUse;
130    }
131
132    @Override
133    public Party getLockEntity(PartyScaleUse partyScaleUse) {
134        return partyScaleUse.getParty();
135    }
136
137    @Override
138    public void fillInResult(EditPartyScaleUseResult result, PartyScaleUse partyScaleUse) {
139        result.setPartyScaleUse(scaleControl.getPartyScaleUseTransfer(getUserVisit(), partyScaleUse));
140    }
141
142    @Override
143    public void doLock(PartyScaleUseEdit edit, PartyScaleUse partyScaleUse) {
144        edit.setScaleName(partyScaleUse.getScale().getLastDetail().getScaleName());
145    }
146
147    Scale scale;
148
149    @Override
150    public void canUpdate(PartyScaleUse partyScaleUse) {
151        var scaleName = edit.getScaleName();
152
153        scale = scaleControl.getScaleByName(scaleName);
154
155        if(scale == null) {
156            addExecutionError(ExecutionErrors.DuplicateScaleName.name(), scaleName);
157        }
158    }
159
160    @Override
161    public void doUpdate(PartyScaleUse partyScaleUse) {
162        var partyScaleUseValue = scaleControl.getPartyScaleUseValue(partyScaleUse);
163
164        partyScaleUseValue.setScalePK(scale.getPrimaryKey());
165
166        scaleControl.updatePartyScaleUseFromValue(partyScaleUseValue, getPartyPK());
167    }
168    
169}