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.EntityIntegerRangeDescriptionEdit; 021import com.echothree.control.user.core.common.form.EditEntityIntegerRangeDescriptionForm; 022import com.echothree.control.user.core.common.result.CoreResultFactory; 023import com.echothree.control.user.core.common.result.EditEntityIntegerRangeDescriptionResult; 024import com.echothree.control.user.core.common.spec.EntityIntegerRangeDescriptionSpec; 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.EntityIntegerRange; 030import com.echothree.model.data.core.server.entity.EntityIntegerRangeDescription; 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 java.util.List; 041import javax.enterprise.context.Dependent; 042import javax.inject.Inject; 043 044@Dependent 045public class EditEntityIntegerRangeDescriptionCommand 046 extends BaseAbstractEditCommand<EntityIntegerRangeDescriptionSpec, EntityIntegerRangeDescriptionEdit, EditEntityIntegerRangeDescriptionResult, EntityIntegerRangeDescription, EntityIntegerRange> { 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.EntityIntegerRange.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("EntityIntegerRangeName", 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 @Inject 074 PartyControl partyControl; 075 076 077 /** Creates a new instance of EditEntityIntegerRangeDescriptionCommand */ 078 public EditEntityIntegerRangeDescriptionCommand() { 079 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 080 } 081 082 @Override 083 public EditEntityIntegerRangeDescriptionResult getResult() { 084 return CoreResultFactory.getEditEntityIntegerRangeDescriptionResult(); 085 } 086 087 @Override 088 public EntityIntegerRangeDescriptionEdit getEdit() { 089 return CoreEditFactory.getEntityIntegerRangeDescriptionEdit(); 090 } 091 092 @Override 093 public EntityIntegerRangeDescription getEntity(EditEntityIntegerRangeDescriptionResult result) { 094 EntityIntegerRangeDescription entityIntegerRangeDescription = null; 095 var componentVendorName = spec.getComponentVendorName(); 096 var componentVendor = componentControl.getComponentVendorByName(componentVendorName); 097 098 if(componentVendor != null) { 099 var entityTypeName = spec.getEntityTypeName(); 100 var entityType = entityTypeControl.getEntityTypeByName(componentVendor, entityTypeName); 101 102 if(entityType != null) { 103 var entityAttributeName = spec.getEntityAttributeName(); 104 var entityAttribute = coreControl.getEntityAttributeByName(entityType, entityAttributeName); 105 106 if(entityAttribute != null) { 107 var entityIntegerRangeName = spec.getEntityIntegerRangeName(); 108 var entityIntegerRange = coreControl.getEntityIntegerRangeByName(entityAttribute, entityIntegerRangeName); 109 110 if(entityIntegerRange != null) { 111 var languageIsoName = spec.getLanguageIsoName(); 112 var language = partyControl.getLanguageByIsoName(languageIsoName); 113 114 if(language != null) { 115 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 116 entityIntegerRangeDescription = coreControl.getEntityIntegerRangeDescription(entityIntegerRange, language); 117 } else { // EditMode.UPDATE 118 entityIntegerRangeDescription = coreControl.getEntityIntegerRangeDescriptionForUpdate(entityIntegerRange, language); 119 } 120 121 if(entityIntegerRangeDescription == null) { 122 addExecutionError(ExecutionErrors.UnknownEntityIntegerRangeDescription.name(), componentVendorName, entityTypeName, entityAttributeName, 123 entityIntegerRangeName, languageIsoName); 124 } 125 } else { 126 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName); 127 } 128 } else { 129 addExecutionError(ExecutionErrors.UnknownEntityIntegerRangeName.name(), componentVendorName, entityTypeName, entityAttributeName, entityIntegerRangeName); 130 } 131 } else { 132 addExecutionError(ExecutionErrors.UnknownEntityAttributeName.name(), componentVendorName, entityTypeName, entityAttributeName); 133 } 134 } else { 135 addExecutionError(ExecutionErrors.UnknownEntityTypeName.name(), componentVendorName, entityTypeName); 136 } 137 } else { 138 addExecutionError(ExecutionErrors.UnknownComponentVendorName.name(), componentVendorName); 139 } 140 141 return entityIntegerRangeDescription; 142 } 143 144 @Override 145 public EntityIntegerRange getLockEntity(EntityIntegerRangeDescription entityIntegerRangeDescription) { 146 return entityIntegerRangeDescription.getEntityIntegerRange(); 147 } 148 149 @Override 150 public void fillInResult(EditEntityIntegerRangeDescriptionResult result, EntityIntegerRangeDescription entityIntegerRangeDescription) { 151 152 result.setEntityIntegerRangeDescription(coreControl.getEntityIntegerRangeDescriptionTransfer(getUserVisit(), entityIntegerRangeDescription, null)); 153 } 154 155 @Override 156 public void doLock(EntityIntegerRangeDescriptionEdit edit, EntityIntegerRangeDescription entityIntegerRangeDescription) { 157 edit.setDescription(entityIntegerRangeDescription.getDescription()); 158 } 159 160 @Override 161 public void doUpdate(EntityIntegerRangeDescription entityIntegerRangeDescription) { 162 var entityIntegerRangeDescriptionValue = coreControl.getEntityIntegerRangeDescriptionValue(entityIntegerRangeDescription); 163 164 entityIntegerRangeDescriptionValue.setDescription(edit.getDescription()); 165 166 coreControl.updateEntityIntegerRangeDescriptionFromValue(entityIntegerRangeDescriptionValue, getPartyPK()); 167 } 168 169}