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.core.server.command; 018 019import com.echothree.control.user.core.common.edit.CoreEditFactory; 020import com.echothree.control.user.core.common.edit.ServiceEdit; 021import com.echothree.control.user.core.common.form.EditServiceForm; 022import com.echothree.control.user.core.common.result.CoreResultFactory; 023import com.echothree.control.user.core.common.result.EditServiceResult; 024import com.echothree.control.user.core.common.spec.ServiceSpec; 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.Protocol; 030import com.echothree.model.data.core.server.entity.Service; 031import com.echothree.model.data.user.common.pk.UserVisitPK; 032import com.echothree.util.common.command.EditMode; 033import com.echothree.util.common.message.ExecutionErrors; 034import com.echothree.util.common.validation.FieldDefinition; 035import com.echothree.util.common.validation.FieldType; 036import com.echothree.util.server.control.BaseAbstractEditCommand; 037import com.echothree.util.server.control.CommandSecurityDefinition; 038import com.echothree.util.server.control.PartyTypeDefinition; 039import com.echothree.util.server.control.SecurityRoleDefinition; 040import java.util.List; 041import javax.enterprise.context.Dependent; 042import javax.inject.Inject; 043 044@Dependent 045public class EditServiceCommand 046 extends BaseAbstractEditCommand<ServiceSpec, ServiceEdit, EditServiceResult, Service, Service> { 047 048 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 049 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 050 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 051 052 static { 053 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 054 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 055 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 056 new SecurityRoleDefinition(SecurityRoleGroups.Service.name(), SecurityRoles.Edit.name()) 057 )) 058 )); 059 060 SPEC_FIELD_DEFINITIONS = List.of( 061 new FieldDefinition("ServiceName", FieldType.ENTITY_NAME, true, null, null) 062 ); 063 064 EDIT_FIELD_DEFINITIONS = List.of( 065 new FieldDefinition("ServiceName", FieldType.ENTITY_NAME, true, null, null), 066 new FieldDefinition("Port", FieldType.UNSIGNED_INTEGER, true, 1L, 65535L), 067 new FieldDefinition("ProtocolName", FieldType.ENTITY_NAME, true, null, null), 068 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 069 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 070 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 071 ); 072 } 073 074 @Inject 075 ServerControl serverControl; 076 077 078 /** Creates a new instance of EditServiceCommand */ 079 public EditServiceCommand() { 080 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 081 } 082 083 @Override 084 public EditServiceResult getResult() { 085 return CoreResultFactory.getEditServiceResult(); 086 } 087 088 @Override 089 public ServiceEdit getEdit() { 090 return CoreEditFactory.getServiceEdit(); 091 } 092 093 @Override 094 public Service getEntity(EditServiceResult result) { 095 Service service; 096 var serviceName = spec.getServiceName(); 097 098 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 099 service = serverControl.getServiceByName(serviceName); 100 } else { // EditMode.UPDATE 101 service = serverControl.getServiceByNameForUpdate(serviceName); 102 } 103 104 if(service != null) { 105 result.setService(serverControl.getServiceTransfer(getUserVisit(), service)); 106 } else { 107 addExecutionError(ExecutionErrors.UnknownServiceName.name(), serviceName); 108 } 109 110 return service; 111 } 112 113 @Override 114 public Service getLockEntity(Service service) { 115 return service; 116 } 117 118 @Override 119 public void fillInResult(EditServiceResult result, Service service) { 120 result.setService(serverControl.getServiceTransfer(getUserVisit(), service)); 121 } 122 123 @Override 124 public void doLock(ServiceEdit edit, Service service) { 125 var serviceDescription = serverControl.getServiceDescription(service, getPreferredLanguage()); 126 var serviceDetail = service.getLastDetail(); 127 128 edit.setServiceName(serviceDetail.getServiceName()); 129 edit.setPort(serviceDetail.getPort().toString()); 130 edit.setProtocolName(serviceDetail.getProtocol().getLastDetail().getProtocolName()); 131 edit.setIsDefault(serviceDetail.getIsDefault().toString()); 132 edit.setSortOrder(serviceDetail.getSortOrder().toString()); 133 134 if(serviceDescription != null) { 135 edit.setDescription(serviceDescription.getDescription()); 136 } 137 } 138 139 Protocol protocol; 140 141 @Override 142 public void canUpdate(Service service) { 143 var serviceName = edit.getServiceName(); 144 var duplicateService = serverControl.getServiceByName(serviceName); 145 146 if(duplicateService != null && !service.equals(duplicateService)) { 147 addExecutionError(ExecutionErrors.DuplicateServiceName.name(), serviceName); 148 } else { 149 var protocolName = edit.getProtocolName(); 150 151 protocol = serverControl.getProtocolByName(protocolName); 152 153 if(protocol == null) { 154 addExecutionError(ExecutionErrors.UnknownProtocolName.name(), protocolName); 155 } 156 } 157 } 158 159 @Override 160 public void doUpdate(Service service) { 161 var partyPK = getPartyPK(); 162 var serviceDetailValue = serverControl.getServiceDetailValueForUpdate(service); 163 var serviceDescription = serverControl.getServiceDescriptionForUpdate(service, getPreferredLanguage()); 164 var description = edit.getDescription(); 165 166 serviceDetailValue.setServiceName(edit.getServiceName()); 167 serviceDetailValue.setPort(Integer.valueOf(edit.getPort())); 168 serviceDetailValue.setProtocolPK(protocol.getPrimaryKey()); 169 serviceDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 170 serviceDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 171 172 serverControl.updateServiceFromValue(serviceDetailValue, partyPK); 173 174 if(serviceDescription == null && description != null) { 175 serverControl.createServiceDescription(service, getPreferredLanguage(), description, partyPK); 176 } else { 177 if(serviceDescription != null && description == null) { 178 serverControl.deleteServiceDescription(serviceDescription, partyPK); 179 } else { 180 if(serviceDescription != null && description != null) { 181 var serviceDescriptionValue = serverControl.getServiceDescriptionValue(serviceDescription); 182 183 serviceDescriptionValue.setDescription(description); 184 serverControl.updateServiceDescriptionFromValue(serviceDescriptionValue, partyPK); 185 } 186 } 187 } 188 } 189 190}