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.form.GetEntityAttributeEntityAttributeGroupsForm; 020import com.echothree.control.user.core.common.result.CoreResultFactory; 021import com.echothree.model.control.core.server.logic.EntityAttributeLogic; 022import com.echothree.model.control.party.common.PartyTypes; 023import com.echothree.model.control.security.common.SecurityRoleGroups; 024import com.echothree.model.control.security.common.SecurityRoles; 025import com.echothree.model.data.core.server.entity.EntityAttribute; 026import com.echothree.model.data.core.server.entity.EntityAttributeEntityAttributeGroup; 027import com.echothree.model.data.core.server.entity.EntityAttributeGroup; 028import com.echothree.util.common.command.BaseResult; 029import com.echothree.util.common.message.ExecutionErrors; 030import com.echothree.util.common.validation.FieldDefinition; 031import com.echothree.util.common.validation.FieldType; 032import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand; 033import com.echothree.util.server.control.CommandSecurityDefinition; 034import com.echothree.util.server.control.PartyTypeDefinition; 035import com.echothree.util.server.control.SecurityRoleDefinition; 036import java.util.Collection; 037import java.util.List; 038import javax.enterprise.context.Dependent; 039import javax.inject.Inject; 040 041@Dependent 042public class GetEntityAttributeEntityAttributeGroupsCommand 043 extends BasePaginatedMultipleEntitiesCommand<EntityAttributeEntityAttributeGroup, GetEntityAttributeEntityAttributeGroupsForm> { 044 045 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 046 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 047 048 static { 049 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 050 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 051 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 052 new SecurityRoleDefinition(SecurityRoleGroups.EntityAttribute.name(), SecurityRoles.EntityAttributeEntityAttributeGroup.name()) 053 )) 054 )); 055 056 FORM_FIELD_DEFINITIONS = List.of( 057 new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, false, null, null), 058 new FieldDefinition("EntityTypeName", FieldType.ENTITY_TYPE_NAME, false, null, null), 059 new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, false, null, null), 060 new FieldDefinition("EntityAttributeGroupName", FieldType.ENTITY_NAME, false, null, null) 061 ); 062 } 063 064 @Inject 065 EntityAttributeLogic entityAttributeLogic; 066 067 068 /** Creates a new instance of GetEntityAttributeEntityAttributeGroupsCommand */ 069 public GetEntityAttributeEntityAttributeGroupsCommand() { 070 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 071 } 072 073 private EntityAttribute entityAttribute; 074 private EntityAttributeGroup entityAttributeGroup; 075 076 @Override 077 protected void handleForm() { 078 var componentVendorName = form.getComponentVendorName(); 079 var entityTypeName = form.getEntityTypeName(); 080 var entityAttributeName = form.getEntityAttributeName(); 081 var entityAttributeGroupName = form.getEntityAttributeGroupName(); 082 var parameterCount = ((componentVendorName != null) && (entityTypeName != null) && (entityAttributeName != null) ? 1 : 0) 083 + (entityAttributeGroupName != null ? 1 : 0); 084 085 if(parameterCount == 1) { 086 if(entityAttributeGroupName == null) { 087 entityAttribute = entityAttributeLogic.getEntityAttributeByName(this, componentVendorName, entityTypeName, entityAttributeName); 088 } else { // entityAttributeGroup 089 entityAttributeGroup = entityAttributeLogic.getEntityAttributeGroupByName(this, entityAttributeGroupName); 090 } 091 } else { 092 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 093 } 094 } 095 096 @Override 097 protected Long getTotalEntities() { 098 Long totalEntities = null; 099 100 if(!hasExecutionErrors()) { 101 102 if(entityAttribute != null) { 103 totalEntities = coreControl.countEntityAttributeEntityAttributeGroupsByEntityAttribute(entityAttribute); 104 } else { 105 totalEntities = coreControl.countEntityAttributeEntityAttributeGroupsByEntityAttributeGroup(entityAttributeGroup); 106 } 107 } 108 109 return totalEntities; 110 } 111 112 @Override 113 protected Collection<EntityAttributeEntityAttributeGroup> getEntities() { 114 Collection<EntityAttributeEntityAttributeGroup> result = null; 115 116 if(!hasExecutionErrors()) { 117 118 if(entityAttribute != null) { 119 result = coreControl.getEntityAttributeEntityAttributeGroupsByEntityAttribute(entityAttribute); 120 } else { // entityAttributeGroup 121 result = coreControl.getEntityAttributeEntityAttributeGroupsByEntityAttributeGroup(entityAttributeGroup); 122 } 123 } 124 125 return result; 126 } 127 128 @Override 129 protected BaseResult getResult(Collection<EntityAttributeEntityAttributeGroup> entities) { 130 var result = CoreResultFactory.getGetEntityAttributeEntityAttributeGroupsResult(); 131 132 if(entities != null) { 133 var userVisit = getUserVisit(); 134 135 if(entityAttributeGroup == null) { 136 result.setEntityAttribute(coreControl.getEntityAttributeTransfer(userVisit, entityAttribute, null)); 137 } else { 138 result.setEntityAttributeGroup(coreControl.getEntityAttributeGroupTransfer(userVisit, entityAttributeGroup, null)); 139 } 140 141 result.setEntityAttributeEntityAttributeGroups(coreControl.getEntityAttributeEntityAttributeGroupTransfers(userVisit, entities, null)); 142 } 143 144 return result; 145 } 146 147}