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.form.GetEntityIntegerRangeChoicesForm; 020import com.echothree.control.user.core.common.result.CoreResultFactory; 021import com.echothree.control.user.core.common.result.GetEntityIntegerRangeChoicesResult; 022import com.echothree.model.control.core.common.EntityAttributeTypes; 023import com.echothree.model.control.party.common.PartyTypes; 024import com.echothree.model.control.security.common.SecurityRoleGroups; 025import com.echothree.model.control.security.common.SecurityRoles; 026import com.echothree.model.data.core.server.entity.ComponentVendor; 027import com.echothree.model.data.core.server.entity.EntityAttribute; 028import com.echothree.model.data.core.server.entity.EntityAttributeType; 029import com.echothree.model.data.core.server.entity.EntityInstance; 030import com.echothree.model.data.core.server.entity.EntityType; 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.BaseResult; 036import com.echothree.util.server.control.BaseSimpleCommand; 037import com.echothree.util.server.control.CommandSecurityDefinition; 038import com.echothree.util.server.control.PartyTypeDefinition; 039import com.echothree.util.server.control.SecurityRoleDefinition; 040import java.util.Arrays; 041import java.util.Collections; 042import java.util.List; 043 044public class GetEntityIntegerRangeChoicesCommand 045 extends BaseSimpleCommand<GetEntityIntegerRangeChoicesForm> { 046 047 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 048 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 049 050 static { 051 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 052 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 053 new SecurityRoleDefinition(SecurityRoleGroups.EntityIntegerRange.name(), SecurityRoles.Choices.name()) 054 ))) 055 ))); 056 057 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 058 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 059 new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, false, null, null), 060 new FieldDefinition("EntityTypeName", FieldType.ENTITY_TYPE_NAME, false, null, null), 061 new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, true, null, null) 062 )); 063 } 064 065 /** Creates a new instance of GetEntityIntegerRangeChoicesCommand */ 066 public GetEntityIntegerRangeChoicesCommand(UserVisitPK userVisitPK, GetEntityIntegerRangeChoicesForm form) { 067 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 068 } 069 070 @Override 071 protected BaseResult execute() { 072 GetEntityIntegerRangeChoicesResult result = CoreResultFactory.getGetEntityIntegerRangeChoicesResult(); 073 String entityRef = form.getEntityRef(); 074 String componentVendorName = form.getComponentVendorName(); 075 String entityTypeName = form.getEntityTypeName(); 076 var parameterCount = (entityRef != null && componentVendorName == null && entityTypeName == null? 1: 0) 077 + (entityRef == null && componentVendorName != null && entityTypeName != null? 1: 0); 078 079 if(parameterCount == 1) { 080 var coreControl = getCoreControl(); 081 EntityType entityType = null; 082 083 if(entityRef == null) { 084 ComponentVendor componentVendor = coreControl.getComponentVendorByName(componentVendorName); 085 086 if(componentVendor != null) { 087 entityType = coreControl.getEntityTypeByName(componentVendor, entityTypeName); 088 089 if(entityType == null) { 090 addExecutionError(ExecutionErrors.UnknownEntityTypeName.name(), componentVendorName, entityTypeName); 091 } 092 } else { 093 addExecutionError(ExecutionErrors.UnknownComponentVendorName.name(), componentVendorName); 094 } 095 } else { 096 EntityInstance entityInstance = coreControl.getEntityInstanceByEntityRef(entityRef); 097 098 if(entityInstance != null) { 099 entityType = entityInstance.getEntityType(); 100 } else { 101 addExecutionError(ExecutionErrors.UnknownEntityRef.name(), entityRef); 102 } 103 } 104 105 if(!hasExecutionErrors()) { 106 String entityAttributeName = form.getEntityAttributeName(); 107 EntityAttribute entityAttribute = coreControl.getEntityAttributeByName(entityType, entityAttributeName); 108 109 if(entityAttribute != null) { 110 EntityAttributeType entityAttributeType = entityAttribute.getLastDetail().getEntityAttributeType(); 111 String entityAttributeTypeName = entityAttributeType.getEntityAttributeTypeName(); 112 113 if(entityAttributeTypeName.equals(EntityAttributeTypes.INTEGER.name())) { 114 String defaultEntityIntegerRangeChoice = form.getDefaultEntityIntegerRangeChoice(); 115 Boolean allowNullChoice = Boolean.valueOf(form.getAllowNullChoice()); 116 117 result.setEntityIntegerRangeChoices(coreControl.getEntityIntegerRangeChoices(defaultEntityIntegerRangeChoice, 118 getPreferredLanguage(), allowNullChoice, entityAttribute)); 119 } else { 120 addExecutionError(ExecutionErrors.InvalidEntityAttributeType.name()); 121 } 122 } else { 123 addExecutionError(ExecutionErrors.UnknownEntityAttributeName.name(), entityAttributeName); 124 } 125 } 126 } else { 127 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 128 } 129 130 return result; 131 } 132 133}