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.form.CreateScaleForm; 020import com.echothree.model.control.scale.server.control.ScaleControl; 021import com.echothree.model.data.core.server.entity.Server; 022import com.echothree.model.data.core.server.entity.ServerService; 023import com.echothree.model.data.core.server.entity.Service; 024import com.echothree.model.data.party.common.pk.PartyPK; 025import com.echothree.model.data.party.server.entity.Language; 026import com.echothree.model.data.scale.server.entity.Scale; 027import com.echothree.model.data.scale.server.entity.ScaleType; 028import com.echothree.model.data.user.common.pk.UserVisitPK; 029import com.echothree.util.common.message.ExecutionErrors; 030import com.echothree.util.common.validation.FieldDefinition; 031import com.echothree.util.common.validation.FieldType; 032import com.echothree.util.common.command.BaseResult; 033import com.echothree.util.server.control.BaseSimpleCommand; 034import com.echothree.util.server.persistence.Session; 035import java.util.Arrays; 036import java.util.Collections; 037import java.util.List; 038 039public class CreateScaleCommand 040 extends BaseSimpleCommand<CreateScaleForm> { 041 042 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 043 044 static { 045 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 046 new FieldDefinition("ScaleName", FieldType.ENTITY_NAME, true, null, null), 047 new FieldDefinition("ScaleTypeName", FieldType.ENTITY_NAME, true, null, null), 048 new FieldDefinition("ServerName", FieldType.HOST_NAME, true, null, null), 049 new FieldDefinition("ServiceName", FieldType.ENTITY_NAME, true, null, null), 050 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 051 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 052 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 053 )); 054 } 055 056 /** Creates a new instance of CreateScaleCommand */ 057 public CreateScaleCommand(UserVisitPK userVisitPK, CreateScaleForm form) { 058 super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, false); 059 } 060 061 @Override 062 protected BaseResult execute() { 063 var scaleControl = Session.getModelController(ScaleControl.class); 064 String scaleName = form.getScaleName(); 065 Scale scale = scaleControl.getScaleByName(scaleName); 066 067 if(scale == null) { 068 String scaleTypeName = form.getScaleTypeName(); 069 ScaleType scaleType = scaleControl.getScaleTypeByName(scaleTypeName); 070 071 if(scaleType != null) { 072 var coreControl = getCoreControl(); 073 String serverName = form.getServerName(); 074 Server server = coreControl.getServerByName(serverName); 075 076 if(server != null) { 077 String serviceName = form.getServiceName(); 078 Service service = coreControl.getServiceByName(serviceName); 079 080 if(service != null) { 081 ServerService serverService = coreControl.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 PartyPK createdBy = getPartyPK(); 088 089 scale = scaleControl.createScale(scaleName, scaleType, serverService, isDefault, sortOrder, createdBy); 090 091 if(description != null) { 092 Language 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 return null; 113 } 114 115}