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