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.edit.ScaleEdit; 020import com.echothree.control.user.scale.common.edit.ScaleEditFactory; 021import com.echothree.control.user.scale.common.form.EditScaleForm; 022import com.echothree.control.user.scale.common.result.EditScaleResult; 023import com.echothree.control.user.scale.common.result.ScaleResultFactory; 024import com.echothree.control.user.scale.common.spec.ScaleSpec; 025import com.echothree.model.control.core.server.control.ServerControl; 026import com.echothree.model.control.party.common.PartyTypes; 027import com.echothree.model.control.scale.server.control.ScaleControl; 028import com.echothree.model.control.security.common.SecurityRoleGroups; 029import com.echothree.model.control.security.common.SecurityRoles; 030import com.echothree.model.data.core.server.entity.ServerService; 031import com.echothree.model.data.scale.server.entity.Scale; 032import com.echothree.model.data.scale.server.entity.ScaleType; 033import com.echothree.model.data.user.common.pk.UserVisitPK; 034import com.echothree.util.common.command.EditMode; 035import com.echothree.util.common.message.ExecutionErrors; 036import com.echothree.util.common.validation.FieldDefinition; 037import com.echothree.util.common.validation.FieldType; 038import com.echothree.util.server.control.BaseAbstractEditCommand; 039import com.echothree.util.server.control.CommandSecurityDefinition; 040import com.echothree.util.server.control.PartyTypeDefinition; 041import com.echothree.util.server.control.SecurityRoleDefinition; 042import com.echothree.util.server.persistence.Session; 043import java.util.List; 044import javax.enterprise.context.Dependent; 045 046@Dependent 047public class EditScaleCommand 048 extends BaseAbstractEditCommand<ScaleSpec, ScaleEdit, EditScaleResult, Scale, Scale> { 049 050 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 051 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 052 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 053 054 static { 055 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 056 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 057 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 058 new SecurityRoleDefinition(SecurityRoleGroups.Scale.name(), SecurityRoles.Edit.name()) 059 )) 060 )); 061 062 SPEC_FIELD_DEFINITIONS = List.of( 063 new FieldDefinition("ScaleName", FieldType.ENTITY_NAME, true, null, null) 064 ); 065 066 EDIT_FIELD_DEFINITIONS = List.of( 067 new FieldDefinition("ScaleName", FieldType.ENTITY_NAME, true, null, null), 068 new FieldDefinition("ScaleTypeName", FieldType.ENTITY_NAME, true, null, null), 069 new FieldDefinition("ServerName", FieldType.HOST_NAME, true, null, null), 070 new FieldDefinition("ServiceName", FieldType.ENTITY_NAME, true, null, null), 071 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 072 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 073 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 074 ); 075 } 076 077 /** Creates a new instance of EditScaleCommand */ 078 public EditScaleCommand() { 079 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 080 } 081 082 @Override 083 public EditScaleResult getResult() { 084 return ScaleResultFactory.getEditScaleResult(); 085 } 086 087 @Override 088 public ScaleEdit getEdit() { 089 return ScaleEditFactory.getScaleEdit(); 090 } 091 092 @Override 093 public Scale getEntity(EditScaleResult result) { 094 var scaleControl = Session.getModelController(ScaleControl.class); 095 Scale scale; 096 var scaleName = spec.getScaleName(); 097 098 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 099 scale = scaleControl.getScaleByName(scaleName); 100 } else { // EditMode.UPDATE 101 scale = scaleControl.getScaleByNameForUpdate(scaleName); 102 } 103 104 if(scale != null) { 105 result.setScale(scaleControl.getScaleTransfer(getUserVisit(), scale)); 106 } else { 107 addExecutionError(ExecutionErrors.UnknownScaleName.name(), scaleName); 108 } 109 110 return scale; 111 } 112 113 @Override 114 public Scale getLockEntity(Scale scale) { 115 return scale; 116 } 117 118 @Override 119 public void fillInResult(EditScaleResult result, Scale scale) { 120 var scaleControl = Session.getModelController(ScaleControl.class); 121 122 result.setScale(scaleControl.getScaleTransfer(getUserVisit(), scale)); 123 } 124 125 ServerService serverService; 126 127 @Override 128 public void doLock(ScaleEdit edit, Scale scale) { 129 var scaleControl = Session.getModelController(ScaleControl.class); 130 var scaleDescription = scaleControl.getScaleDescription(scale, getPreferredLanguage()); 131 var scaleDetail = scale.getLastDetail(); 132 133 serverService = scaleDetail.getServerService(); 134 135 edit.setScaleName(scaleDetail.getScaleName()); 136 edit.setScaleTypeName(scaleDetail.getScaleType().getLastDetail().getScaleTypeName()); 137 edit.setServerName(serverService.getServer().getLastDetail().getServerName()); 138 edit.setServiceName(serverService.getService().getLastDetail().getServiceName()); 139 edit.setIsDefault(scaleDetail.getIsDefault().toString()); 140 edit.setSortOrder(scaleDetail.getSortOrder().toString()); 141 142 if(scaleDescription != null) { 143 edit.setDescription(scaleDescription.getDescription()); 144 } 145 } 146 147 ScaleType scaleType; 148 149 @Override 150 public void canUpdate(Scale scale) { 151 var scaleControl = Session.getModelController(ScaleControl.class); 152 var scaleName = edit.getScaleName(); 153 var duplicateScale = scaleControl.getScaleByName(scaleName); 154 155 if(duplicateScale != null && !scale.equals(duplicateScale)) { 156 addExecutionError(ExecutionErrors.DuplicateScaleName.name(), scaleName); 157 } else { 158 var scaleTypeName = edit.getScaleTypeName(); 159 160 scaleType = scaleControl.getScaleTypeByName(scaleTypeName); 161 162 if(scaleType == null) { 163 addExecutionError(ExecutionErrors.UnknownScaleTypeName.name(), scaleTypeName); 164 } else { 165 var serverControl = Session.getModelController(ServerControl.class); 166 var serverName = edit.getServerName(); 167 var server = serverControl.getServerByName(serverName); 168 169 if(server == null) { 170 addExecutionError(ExecutionErrors.UnknownServerName.name(), serverName); 171 } else { 172 var serviceName = edit.getServiceName(); 173 var service = serverControl.getServiceByName(serviceName); 174 175 if(service == null) { 176 addExecutionError(ExecutionErrors.UnknownServiceName.name(), serviceName); 177 } else { 178 serverService = serverControl.getServerService(server, service); 179 180 if(serverService == null) { 181 addExecutionError(ExecutionErrors.UnknownServerService.name(), serverName, serviceName); 182 } 183 } 184 } 185 } 186 } 187 } 188 189 @Override 190 public void doUpdate(Scale scale) { 191 var scaleControl = Session.getModelController(ScaleControl.class); 192 var partyPK = getPartyPK(); 193 var scaleDetailValue = scaleControl.getScaleDetailValueForUpdate(scale); 194 var scaleDescription = scaleControl.getScaleDescriptionForUpdate(scale, getPreferredLanguage()); 195 var description = edit.getDescription(); 196 197 scaleDetailValue.setScaleName(edit.getScaleName()); 198 scaleDetailValue.setScaleTypePK(scaleType.getPrimaryKey()); 199 scaleDetailValue.setServerServicePK(serverService.getPrimaryKey()); 200 scaleDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 201 scaleDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 202 203 scaleControl.updateScaleFromValue(scaleDetailValue, partyPK); 204 205 if(scaleDescription == null && description != null) { 206 scaleControl.createScaleDescription(scale, getPreferredLanguage(), description, partyPK); 207 } else if(scaleDescription != null && description == null) { 208 scaleControl.deleteScaleDescription(scaleDescription, partyPK); 209 } else if(scaleDescription != null && description != null) { 210 var scaleDescriptionValue = scaleControl.getScaleDescriptionValue(scaleDescription); 211 212 scaleDescriptionValue.setDescription(description); 213 scaleControl.updateScaleDescriptionFromValue(scaleDescriptionValue, partyPK); 214 } 215 } 216 217}