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