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