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.form.CreateScaleForm;
020import com.echothree.model.control.core.server.control.ServerControl;
021import com.echothree.model.control.scale.server.control.ScaleControl;
022import com.echothree.model.data.user.common.pk.UserVisitPK;
023import com.echothree.util.common.command.BaseResult;
024import com.echothree.util.common.message.ExecutionErrors;
025import com.echothree.util.common.validation.FieldDefinition;
026import com.echothree.util.common.validation.FieldType;
027import com.echothree.util.server.control.BaseSimpleCommand;
028import com.echothree.util.server.persistence.Session;
029import java.util.List;
030import javax.enterprise.context.Dependent;
031
032@Dependent
033public class CreateScaleCommand
034        extends BaseSimpleCommand<CreateScaleForm> {
035    
036   private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
037
038    static {
039        FORM_FIELD_DEFINITIONS = List.of(
040                new FieldDefinition("ScaleName", FieldType.ENTITY_NAME, true, null, null),
041                new FieldDefinition("ScaleTypeName", FieldType.ENTITY_NAME, true, null, null),
042                new FieldDefinition("ServerName", FieldType.HOST_NAME, true, null, null),
043                new FieldDefinition("ServiceName", FieldType.ENTITY_NAME, true, null, null),
044                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
045                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
046                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
047                );
048    }
049
050    /** Creates a new instance of CreateScaleCommand */
051    public CreateScaleCommand() {
052        super(null, FORM_FIELD_DEFINITIONS, false);
053    }
054    
055   @Override
056    protected BaseResult execute() {
057        var scaleControl = Session.getModelController(ScaleControl.class);
058       var scaleName = form.getScaleName();
059       var scale = scaleControl.getScaleByName(scaleName);
060
061        if(scale == null) {
062            var scaleTypeName = form.getScaleTypeName();
063            var scaleType = scaleControl.getScaleTypeByName(scaleTypeName);
064
065            if(scaleType != null) {
066                var serverControl = Session.getModelController(ServerControl.class);
067                var serverName = form.getServerName();
068                var server = serverControl.getServerByName(serverName);
069
070                if(server != null) {
071                    var serviceName = form.getServiceName();
072                    var service = serverControl.getServiceByName(serviceName);
073
074                    if(service != null) {
075                        var serverService = serverControl.getServerService(server, service);
076
077                        if(serverService != null) {
078                            var isDefault = Boolean.valueOf(form.getIsDefault());
079                            var sortOrder = Integer.valueOf(form.getSortOrder());
080                            var description = form.getDescription();
081                            var createdBy = getPartyPK();
082
083                            scale = scaleControl.createScale(scaleName, scaleType, serverService, isDefault, sortOrder, createdBy);
084
085                            if(description != null) {
086                                var language = getPreferredLanguage();
087
088                                scaleControl.createScaleDescription(scale, language, description, createdBy);
089                            }
090                        } else {
091                            addExecutionError(ExecutionErrors.UnknownServerService.name(), serverName, serviceName);
092                        }
093                    } else {
094                        addExecutionError(ExecutionErrors.UnknownServiceName.name(), serviceName);
095                    }
096                } else {
097                    addExecutionError(ExecutionErrors.UnknownServerName.name(), serverName);
098                }
099            } else {
100                addExecutionError(ExecutionErrors.UnknownScaleTypeName.name(), scaleTypeName);
101            }
102        } else {
103            addExecutionError(ExecutionErrors.DuplicateScaleName.name(), scaleName);
104        }
105        
106        return null;
107    }
108    
109}