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.scale.server.command;
018
019import com.echothree.control.user.scale.common.edit.ScaleEdit;
020import com.echothree.control.user.scale.common.edit.ScaleEditFactory;
021import com.echothree.control.user.scale.common.form.EditScaleForm;
022import com.echothree.control.user.scale.common.result.EditScaleResult;
023import com.echothree.control.user.scale.common.result.ScaleResultFactory;
024import com.echothree.control.user.scale.common.spec.ScaleSpec;
025import com.echothree.model.control.core.server.control.ServerControl;
026import com.echothree.model.control.party.common.PartyTypes;
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.core.server.entity.ServerService;
031import com.echothree.model.data.scale.server.entity.Scale;
032import com.echothree.model.data.scale.server.entity.ScaleType;
033import com.echothree.model.data.user.common.pk.UserVisitPK;
034import com.echothree.util.common.command.EditMode;
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.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 com.echothree.util.server.persistence.Session;
043import java.util.Arrays;
044import java.util.Collections;
045import java.util.List;
046import javax.enterprise.context.RequestScoped;
047
048@RequestScoped
049public class EditScaleCommand
050        extends BaseAbstractEditCommand<ScaleSpec, ScaleEdit, EditScaleResult, Scale, Scale> {
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.Scale.name(), SecurityRoles.Edit.name())
061                        )))
062                )));
063
064        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
065                new FieldDefinition("ScaleName", FieldType.ENTITY_NAME, true, null, null)
066                ));
067
068        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
069                new FieldDefinition("ScaleName", FieldType.ENTITY_NAME, true, null, null),
070                new FieldDefinition("ScaleTypeName", FieldType.ENTITY_NAME, true, null, null),
071                new FieldDefinition("ServerName", FieldType.HOST_NAME, true, null, null),
072                new FieldDefinition("ServiceName", FieldType.ENTITY_NAME, true, null, null),
073                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
074                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
075                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
076                ));
077    }
078
079    /** Creates a new instance of EditScaleCommand */
080    public EditScaleCommand() {
081        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
082    }
083
084    @Override
085    public EditScaleResult getResult() {
086        return ScaleResultFactory.getEditScaleResult();
087    }
088
089    @Override
090    public ScaleEdit getEdit() {
091        return ScaleEditFactory.getScaleEdit();
092    }
093
094    @Override
095    public Scale getEntity(EditScaleResult result) {
096        var scaleControl = Session.getModelController(ScaleControl.class);
097        Scale scale;
098        var scaleName = spec.getScaleName();
099
100        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
101            scale = scaleControl.getScaleByName(scaleName);
102        } else { // EditMode.UPDATE
103            scale = scaleControl.getScaleByNameForUpdate(scaleName);
104        }
105
106        if(scale != null) {
107            result.setScale(scaleControl.getScaleTransfer(getUserVisit(), scale));
108        } else {
109            addExecutionError(ExecutionErrors.UnknownScaleName.name(), scaleName);
110        }
111
112        return scale;
113    }
114
115    @Override
116    public Scale getLockEntity(Scale scale) {
117        return scale;
118    }
119
120    @Override
121    public void fillInResult(EditScaleResult result, Scale scale) {
122        var scaleControl = Session.getModelController(ScaleControl.class);
123
124        result.setScale(scaleControl.getScaleTransfer(getUserVisit(), scale));
125    }
126
127    ServerService serverService;
128
129    @Override
130    public void doLock(ScaleEdit edit, Scale scale) {
131        var scaleControl = Session.getModelController(ScaleControl.class);
132        var scaleDescription = scaleControl.getScaleDescription(scale, getPreferredLanguage());
133        var scaleDetail = scale.getLastDetail();
134
135        serverService = scaleDetail.getServerService();
136
137        edit.setScaleName(scaleDetail.getScaleName());
138        edit.setScaleTypeName(scaleDetail.getScaleType().getLastDetail().getScaleTypeName());
139        edit.setServerName(serverService.getServer().getLastDetail().getServerName());
140        edit.setServiceName(serverService.getService().getLastDetail().getServiceName());
141        edit.setIsDefault(scaleDetail.getIsDefault().toString());
142        edit.setSortOrder(scaleDetail.getSortOrder().toString());
143
144        if(scaleDescription != null) {
145            edit.setDescription(scaleDescription.getDescription());
146        }
147    }
148
149    ScaleType scaleType;
150
151    @Override
152    public void canUpdate(Scale scale) {
153        var scaleControl = Session.getModelController(ScaleControl.class);
154        var scaleName = edit.getScaleName();
155        var duplicateScale = scaleControl.getScaleByName(scaleName);
156
157        if(duplicateScale != null && !scale.equals(duplicateScale)) {
158            addExecutionError(ExecutionErrors.DuplicateScaleName.name(), scaleName);
159        } else {
160            var scaleTypeName = edit.getScaleTypeName();
161
162            scaleType = scaleControl.getScaleTypeByName(scaleTypeName);
163
164            if(scaleType == null) {
165                addExecutionError(ExecutionErrors.UnknownScaleTypeName.name(), scaleTypeName);
166            } else {
167                var serverControl = Session.getModelController(ServerControl.class);
168                var serverName = edit.getServerName();
169                var server = serverControl.getServerByName(serverName);
170
171                if(server == null) {
172                    addExecutionError(ExecutionErrors.UnknownServerName.name(), serverName);
173                } else {
174                    var serviceName = edit.getServiceName();
175                    var service = serverControl.getServiceByName(serviceName);
176
177                    if(service == null) {
178                        addExecutionError(ExecutionErrors.UnknownServiceName.name(), serviceName);
179                    } else {
180                        serverService = serverControl.getServerService(server, service);
181
182                        if(serverService == null) {
183                            addExecutionError(ExecutionErrors.UnknownServerService.name(), serverName, serviceName);
184                        }
185                    }
186                }
187            }
188        }
189    }
190
191    @Override
192    public void doUpdate(Scale scale) {
193        var scaleControl = Session.getModelController(ScaleControl.class);
194        var partyPK = getPartyPK();
195        var scaleDetailValue = scaleControl.getScaleDetailValueForUpdate(scale);
196        var scaleDescription = scaleControl.getScaleDescriptionForUpdate(scale, getPreferredLanguage());
197        var description = edit.getDescription();
198
199        scaleDetailValue.setScaleName(edit.getScaleName());
200        scaleDetailValue.setScaleTypePK(scaleType.getPrimaryKey());
201        scaleDetailValue.setServerServicePK(serverService.getPrimaryKey());
202        scaleDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
203        scaleDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
204
205        scaleControl.updateScaleFromValue(scaleDetailValue, partyPK);
206
207        if(scaleDescription == null && description != null) {
208            scaleControl.createScaleDescription(scale, getPreferredLanguage(), description, partyPK);
209        } else if(scaleDescription != null && description == null) {
210            scaleControl.deleteScaleDescription(scaleDescription, partyPK);
211        } else if(scaleDescription != null && description != null) {
212            var scaleDescriptionValue = scaleControl.getScaleDescriptionValue(scaleDescription);
213
214            scaleDescriptionValue.setDescription(description);
215            scaleControl.updateScaleDescriptionFromValue(scaleDescriptionValue, partyPK);
216        }
217    }
218
219}