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.core.server.command; 018 019import com.echothree.control.user.core.common.edit.CoreEditFactory; 020import com.echothree.control.user.core.common.edit.ServerEdit; 021import com.echothree.control.user.core.common.form.EditServerForm; 022import com.echothree.control.user.core.common.result.CoreResultFactory; 023import com.echothree.control.user.core.common.result.EditServerResult; 024import com.echothree.control.user.core.common.spec.ServerSpec; 025import com.echothree.model.control.core.server.control.ServerControl; 026import com.echothree.model.control.party.common.PartyTypes; 027import com.echothree.model.control.security.common.SecurityRoleGroups; 028import com.echothree.model.control.security.common.SecurityRoles; 029import com.echothree.model.data.core.server.entity.Server; 030import com.echothree.model.data.user.common.pk.UserVisitPK; 031import com.echothree.util.common.command.EditMode; 032import com.echothree.util.common.message.ExecutionErrors; 033import com.echothree.util.common.validation.FieldDefinition; 034import com.echothree.util.common.validation.FieldType; 035import com.echothree.util.server.control.BaseAbstractEditCommand; 036import com.echothree.util.server.control.CommandSecurityDefinition; 037import com.echothree.util.server.control.PartyTypeDefinition; 038import com.echothree.util.server.control.SecurityRoleDefinition; 039import com.echothree.util.server.persistence.Session; 040import java.util.Arrays; 041import java.util.Collections; 042import java.util.List; 043import javax.enterprise.context.RequestScoped; 044 045@RequestScoped 046public class EditServerCommand 047 extends BaseAbstractEditCommand<ServerSpec, ServerEdit, EditServerResult, Server, Server> { 048 049 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 050 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 051 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 052 053 static { 054 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 055 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 056 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 057 new SecurityRoleDefinition(SecurityRoleGroups.Server.name(), SecurityRoles.Edit.name()) 058 ))) 059 ))); 060 061 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 062 new FieldDefinition("ServerName", FieldType.HOST_NAME, true, null, null) 063 )); 064 065 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 066 new FieldDefinition("ServerName", FieldType.HOST_NAME, true, null, null), 067 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 068 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 069 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 070 )); 071 } 072 073 /** Creates a new instance of EditServerCommand */ 074 public EditServerCommand() { 075 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 076 } 077 078 @Override 079 public EditServerResult getResult() { 080 return CoreResultFactory.getEditServerResult(); 081 } 082 083 @Override 084 public ServerEdit getEdit() { 085 return CoreEditFactory.getServerEdit(); 086 } 087 088 @Override 089 public Server getEntity(EditServerResult result) { 090 var serverControl = Session.getModelController(ServerControl.class); 091 Server server; 092 var serverName = spec.getServerName(); 093 094 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 095 server = serverControl.getServerByName(serverName); 096 } else { // EditMode.UPDATE 097 server = serverControl.getServerByNameForUpdate(serverName); 098 } 099 100 if(server != null) { 101 result.setServer(serverControl.getServerTransfer(getUserVisit(), server)); 102 } else { 103 addExecutionError(ExecutionErrors.UnknownServerName.name(), serverName); 104 } 105 106 return server; 107 } 108 109 @Override 110 public Server getLockEntity(Server server) { 111 return server; 112 } 113 114 @Override 115 public void fillInResult(EditServerResult result, Server server) { 116 var serverControl = Session.getModelController(ServerControl.class); 117 118 result.setServer(serverControl.getServerTransfer(getUserVisit(), server)); 119 } 120 121 @Override 122 public void doLock(ServerEdit edit, Server server) { 123 var serverControl = Session.getModelController(ServerControl.class); 124 var serverDescription = serverControl.getServerDescription(server, getPreferredLanguage()); 125 var serverDetail = server.getLastDetail(); 126 127 edit.setServerName(serverDetail.getServerName()); 128 edit.setIsDefault(serverDetail.getIsDefault().toString()); 129 edit.setSortOrder(serverDetail.getSortOrder().toString()); 130 131 if(serverDescription != null) { 132 edit.setDescription(serverDescription.getDescription()); 133 } 134 } 135 136 @Override 137 public void canUpdate(Server server) { 138 var serverControl = Session.getModelController(ServerControl.class); 139 var serverName = edit.getServerName(); 140 var duplicateServer = serverControl.getServerByName(serverName); 141 142 if(duplicateServer != null && !server.equals(duplicateServer)) { 143 addExecutionError(ExecutionErrors.DuplicateServerName.name(), serverName); 144 } 145 } 146 147 @Override 148 public void doUpdate(Server server) { 149 var serverControl = Session.getModelController(ServerControl.class); 150 var partyPK = getPartyPK(); 151 var serverDetailValue = serverControl.getServerDetailValueForUpdate(server); 152 var serverDescription = serverControl.getServerDescriptionForUpdate(server, getPreferredLanguage()); 153 var description = edit.getDescription(); 154 155 serverDetailValue.setServerName(edit.getServerName()); 156 serverDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 157 serverDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 158 159 serverControl.updateServerFromValue(serverDetailValue, partyPK); 160 161 if(serverDescription == null && description != null) { 162 serverControl.createServerDescription(server, getPreferredLanguage(), description, partyPK); 163 } else { 164 if(serverDescription != null && description == null) { 165 serverControl.deleteServerDescription(serverDescription, partyPK); 166 } else { 167 if(serverDescription != null && description != null) { 168 var serverDescriptionValue = serverControl.getServerDescriptionValue(serverDescription); 169 170 serverDescriptionValue.setDescription(description); 171 serverControl.updateServerDescriptionFromValue(serverDescriptionValue, partyPK); 172 } 173 } 174 } 175 } 176 177}