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