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 java.util.List; 029import javax.enterprise.context.Dependent; 030import javax.inject.Inject; 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 @Inject 051 ScaleControl scaleControl; 052 053 @Inject 054 ServerControl serverControl; 055 056 /** Creates a new instance of CreateScaleCommand */ 057 public CreateScaleCommand() { 058 super(null, FORM_FIELD_DEFINITIONS, false); 059 } 060 061 @Override 062 protected BaseResult execute() { 063 var scaleName = form.getScaleName(); 064 var scale = scaleControl.getScaleByName(scaleName); 065 066 if(scale == null) { 067 var scaleTypeName = form.getScaleTypeName(); 068 var scaleType = scaleControl.getScaleTypeByName(scaleTypeName); 069 070 if(scaleType != null) { 071 var serverName = form.getServerName(); 072 var server = serverControl.getServerByName(serverName); 073 074 if(server != null) { 075 var serviceName = form.getServiceName(); 076 var service = serverControl.getServiceByName(serviceName); 077 078 if(service != null) { 079 var serverService = serverControl.getServerService(server, service); 080 081 if(serverService != null) { 082 var isDefault = Boolean.valueOf(form.getIsDefault()); 083 var sortOrder = Integer.valueOf(form.getSortOrder()); 084 var description = form.getDescription(); 085 var createdBy = getPartyPK(); 086 087 scale = scaleControl.createScale(scaleName, scaleType, serverService, isDefault, sortOrder, createdBy); 088 089 if(description != null) { 090 var language = getPreferredLanguage(); 091 092 scaleControl.createScaleDescription(scale, language, description, createdBy); 093 } 094 } else { 095 addExecutionError(ExecutionErrors.UnknownServerService.name(), serverName, serviceName); 096 } 097 } else { 098 addExecutionError(ExecutionErrors.UnknownServiceName.name(), serviceName); 099 } 100 } else { 101 addExecutionError(ExecutionErrors.UnknownServerName.name(), serverName); 102 } 103 } else { 104 addExecutionError(ExecutionErrors.UnknownScaleTypeName.name(), scaleTypeName); 105 } 106 } else { 107 addExecutionError(ExecutionErrors.DuplicateScaleName.name(), scaleName); 108 } 109 110 return null; 111 } 112 113}