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.core.server.command; 018 019import com.echothree.control.user.core.common.edit.CoreEditFactory; 020import com.echothree.control.user.core.common.edit.ServiceDescriptionEdit; 021import com.echothree.control.user.core.common.form.EditServiceDescriptionForm; 022import com.echothree.control.user.core.common.result.CoreResultFactory; 023import com.echothree.control.user.core.common.result.EditServiceDescriptionResult; 024import com.echothree.control.user.core.common.spec.ServiceDescriptionSpec; 025import com.echothree.model.control.party.common.PartyTypes; 026import com.echothree.model.control.party.server.control.PartyControl; 027import com.echothree.model.control.security.common.SecurityRoleGroups; 028import com.echothree.model.control.security.common.SecurityRoles; 029import com.echothree.model.data.core.server.entity.Service; 030import com.echothree.model.data.core.server.entity.ServiceDescription; 031import com.echothree.model.data.core.server.value.ServiceDescriptionValue; 032import com.echothree.model.data.party.server.entity.Language; 033import com.echothree.model.data.user.common.pk.UserVisitPK; 034import com.echothree.util.common.message.ExecutionErrors; 035import com.echothree.util.common.validation.FieldDefinition; 036import com.echothree.util.common.validation.FieldType; 037import com.echothree.util.common.command.EditMode; 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.Arrays; 044import java.util.Collections; 045import java.util.List; 046 047public class EditServiceDescriptionCommand 048 extends BaseAbstractEditCommand<ServiceDescriptionSpec, ServiceDescriptionEdit, EditServiceDescriptionResult, ServiceDescription, Service> { 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(Collections.unmodifiableList(Arrays.asList( 056 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 057 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 058 new SecurityRoleDefinition(SecurityRoleGroups.Service.name(), SecurityRoles.Description.name()) 059 ))) 060 ))); 061 062 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 063 new FieldDefinition("ServiceName", FieldType.ENTITY_NAME, true, null, null), 064 new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null) 065 )); 066 067 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 068 new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L) 069 )); 070 } 071 072 /** Creates a new instance of EditServiceDescriptionCommand */ 073 public EditServiceDescriptionCommand(UserVisitPK userVisitPK, EditServiceDescriptionForm form) { 074 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 075 } 076 077 @Override 078 public EditServiceDescriptionResult getResult() { 079 return CoreResultFactory.getEditServiceDescriptionResult(); 080 } 081 082 @Override 083 public ServiceDescriptionEdit getEdit() { 084 return CoreEditFactory.getServiceDescriptionEdit(); 085 } 086 087 @Override 088 public ServiceDescription getEntity(EditServiceDescriptionResult result) { 089 var coreControl = getCoreControl(); 090 ServiceDescription serviceDescription = null; 091 String serviceName = spec.getServiceName(); 092 Service service = coreControl.getServiceByName(serviceName); 093 094 if(service != null) { 095 var partyControl = Session.getModelController(PartyControl.class); 096 String languageIsoName = spec.getLanguageIsoName(); 097 Language language = partyControl.getLanguageByIsoName(languageIsoName); 098 099 if(language != null) { 100 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 101 serviceDescription = coreControl.getServiceDescription(service, language); 102 } else { // EditMode.UPDATE 103 serviceDescription = coreControl.getServiceDescriptionForUpdate(service, language); 104 } 105 106 if(serviceDescription == null) { 107 addExecutionError(ExecutionErrors.UnknownServiceDescription.name(), serviceName, languageIsoName); 108 } 109 } else { 110 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName); 111 } 112 } else { 113 addExecutionError(ExecutionErrors.UnknownServiceName.name(), serviceName); 114 } 115 116 return serviceDescription; 117 } 118 119 @Override 120 public Service getLockEntity(ServiceDescription serviceDescription) { 121 return serviceDescription.getService(); 122 } 123 124 @Override 125 public void fillInResult(EditServiceDescriptionResult result, ServiceDescription serviceDescription) { 126 var coreControl = getCoreControl(); 127 128 result.setServiceDescription(coreControl.getServiceDescriptionTransfer(getUserVisit(), serviceDescription)); 129 } 130 131 @Override 132 public void doLock(ServiceDescriptionEdit edit, ServiceDescription serviceDescription) { 133 edit.setDescription(serviceDescription.getDescription()); 134 } 135 136 @Override 137 public void doUpdate(ServiceDescription serviceDescription) { 138 var coreControl = getCoreControl(); 139 ServiceDescriptionValue serviceDescriptionValue = coreControl.getServiceDescriptionValue(serviceDescription); 140 serviceDescriptionValue.setDescription(edit.getDescription()); 141 142 coreControl.updateServiceDescriptionFromValue(serviceDescriptionValue, getPartyPK()); 143 } 144 145}