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.EntityLongRangeDescriptionEdit; 021import com.echothree.control.user.core.common.form.EditEntityLongRangeDescriptionForm; 022import com.echothree.control.user.core.common.result.CoreResultFactory; 023import com.echothree.control.user.core.common.result.EditEntityLongRangeDescriptionResult; 024import com.echothree.control.user.core.common.spec.EntityLongRangeDescriptionSpec; 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.EntityLongRange; 030import com.echothree.model.data.core.server.entity.EntityLongRangeDescription; 031import com.echothree.model.data.user.common.pk.UserVisitPK; 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.common.command.EditMode; 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 com.echothree.util.server.persistence.Session; 041import java.util.List; 042import javax.enterprise.context.Dependent; 043 044@Dependent 045public class EditEntityLongRangeDescriptionCommand 046 extends BaseAbstractEditCommand<EntityLongRangeDescriptionSpec, EntityLongRangeDescriptionEdit, EditEntityLongRangeDescriptionResult, EntityLongRangeDescription, EntityLongRange> { 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.EntityLongRange.name(), SecurityRoles.Description.name()) 057 )) 058 )); 059 060 SPEC_FIELD_DEFINITIONS = List.of( 061 new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, true, null, null), 062 new FieldDefinition("EntityTypeName", FieldType.ENTITY_TYPE_NAME, true, null, null), 063 new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, true, null, null), 064 new FieldDefinition("EntityLongRangeName", FieldType.ENTITY_NAME, true, null, null), 065 new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null) 066 ); 067 068 EDIT_FIELD_DEFINITIONS = List.of( 069 new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L) 070 ); 071 } 072 073 /** Creates a new instance of EditEntityLongRangeDescriptionCommand */ 074 public EditEntityLongRangeDescriptionCommand() { 075 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 076 } 077 078 @Override 079 public EditEntityLongRangeDescriptionResult getResult() { 080 return CoreResultFactory.getEditEntityLongRangeDescriptionResult(); 081 } 082 083 @Override 084 public EntityLongRangeDescriptionEdit getEdit() { 085 return CoreEditFactory.getEntityLongRangeDescriptionEdit(); 086 } 087 088 @Override 089 public EntityLongRangeDescription getEntity(EditEntityLongRangeDescriptionResult result) { 090 EntityLongRangeDescription entityLongRangeDescription = null; 091 var componentVendorName = spec.getComponentVendorName(); 092 var componentVendor = componentControl.getComponentVendorByName(componentVendorName); 093 094 if(componentVendor != null) { 095 var entityTypeName = spec.getEntityTypeName(); 096 var entityType = entityTypeControl.getEntityTypeByName(componentVendor, entityTypeName); 097 098 if(entityType != null) { 099 var entityAttributeName = spec.getEntityAttributeName(); 100 var entityAttribute = coreControl.getEntityAttributeByName(entityType, entityAttributeName); 101 102 if(entityAttribute != null) { 103 var entityLongRangeName = spec.getEntityLongRangeName(); 104 var entityLongRange = coreControl.getEntityLongRangeByName(entityAttribute, entityLongRangeName); 105 106 if(entityLongRange != null) { 107 var partyControl = Session.getModelController(PartyControl.class); 108 var languageIsoName = spec.getLanguageIsoName(); 109 var language = partyControl.getLanguageByIsoName(languageIsoName); 110 111 if(language != null) { 112 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 113 entityLongRangeDescription = coreControl.getEntityLongRangeDescription(entityLongRange, language); 114 } else { // EditMode.UPDATE 115 entityLongRangeDescription = coreControl.getEntityLongRangeDescriptionForUpdate(entityLongRange, language); 116 } 117 118 if(entityLongRangeDescription == null) { 119 addExecutionError(ExecutionErrors.UnknownEntityLongRangeDescription.name(), componentVendorName, entityTypeName, entityAttributeName, 120 entityLongRangeName, languageIsoName); 121 } 122 } else { 123 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName); 124 } 125 } else { 126 addExecutionError(ExecutionErrors.UnknownEntityLongRangeName.name(), componentVendorName, entityTypeName, entityAttributeName, entityLongRangeName); 127 } 128 } else { 129 addExecutionError(ExecutionErrors.UnknownEntityAttributeName.name(), componentVendorName, entityTypeName, entityAttributeName); 130 } 131 } else { 132 addExecutionError(ExecutionErrors.UnknownEntityTypeName.name(), componentVendorName, entityTypeName); 133 } 134 } else { 135 addExecutionError(ExecutionErrors.UnknownComponentVendorName.name(), componentVendorName); 136 } 137 138 return entityLongRangeDescription; 139 } 140 141 @Override 142 public EntityLongRange getLockEntity(EntityLongRangeDescription entityLongRangeDescription) { 143 return entityLongRangeDescription.getEntityLongRange(); 144 } 145 146 @Override 147 public void fillInResult(EditEntityLongRangeDescriptionResult result, EntityLongRangeDescription entityLongRangeDescription) { 148 149 result.setEntityLongRangeDescription(coreControl.getEntityLongRangeDescriptionTransfer(getUserVisit(), entityLongRangeDescription, null)); 150 } 151 152 @Override 153 public void doLock(EntityLongRangeDescriptionEdit edit, EntityLongRangeDescription entityLongRangeDescription) { 154 edit.setDescription(entityLongRangeDescription.getDescription()); 155 } 156 157 @Override 158 public void doUpdate(EntityLongRangeDescription entityLongRangeDescription) { 159 var entityLongRangeDescriptionValue = coreControl.getEntityLongRangeDescriptionValue(entityLongRangeDescription); 160 161 entityLongRangeDescriptionValue.setDescription(edit.getDescription()); 162 163 coreControl.updateEntityLongRangeDescriptionFromValue(entityLongRangeDescriptionValue, getPartyPK()); 164 } 165 166}