001// -------------------------------------------------------------------------------- 002// Copyright 2002-2025 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.GetEntityAttributesForm; 020import com.echothree.control.user.core.common.result.CoreResultFactory; 021import com.echothree.model.control.core.server.logic.EntityTypeLogic; 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.EntityAttributeType; 027import com.echothree.model.data.core.server.entity.EntityType; 028import com.echothree.model.data.user.common.pk.UserVisitPK; 029import com.echothree.util.common.command.BaseResult; 030import com.echothree.util.common.message.ExecutionErrors; 031import com.echothree.util.common.validation.FieldDefinition; 032import com.echothree.util.common.validation.FieldType; 033import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand; 034import com.echothree.util.server.control.CommandSecurityDefinition; 035import com.echothree.util.server.control.PartyTypeDefinition; 036import com.echothree.util.server.control.SecurityRoleDefinition; 037import com.google.common.base.Splitter; 038import java.util.ArrayList; 039import java.util.Collection; 040import java.util.List; 041import javax.enterprise.context.RequestScoped; 042 043@RequestScoped 044public class GetEntityAttributesCommand 045 extends BasePaginatedMultipleEntitiesCommand<EntityAttribute, GetEntityAttributesForm> { 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(List.of( 052 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 053 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 054 new SecurityRoleDefinition(SecurityRoleGroups.EntityAttribute.name(), SecurityRoles.List.name()) 055 )) 056 )); 057 058 FORM_FIELD_DEFINITIONS = List.of( 059 new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, false, null, null), 060 new FieldDefinition("EntityTypeName", FieldType.ENTITY_TYPE_NAME, false, null, null), 061 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 062 new FieldDefinition("Uuid", FieldType.UUID, false, null, null), 063 new FieldDefinition("EntityAttributeTypeNames", FieldType.STRING, false, null, null) 064 ); 065 } 066 067 /** Creates a new instance of GetEntityAttributesCommand */ 068 public GetEntityAttributesCommand() { 069 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 070 } 071 072 EntityType entityType; 073 Collection<EntityAttributeType> entityAttributeTypes; 074 075 @Override 076 protected void handleForm() { 077 var entityAttributeTypeNames = form.getEntityAttributeTypeNames(); 078 079 entityType = EntityTypeLogic.getInstance().getEntityTypeByUniversalSpec(this, form); 080 081 if(!hasExecutionErrors() && entityAttributeTypeNames != null) { 082 var entityAttributeTypeNamesToCheck = Splitter.on(':').trimResults().omitEmptyStrings().splitToList(entityAttributeTypeNames).toArray(new String[0]); 083 var entityAttributeTypeNamesToCheckLength = entityAttributeTypeNamesToCheck.length; 084 085 entityAttributeTypes = new ArrayList<>(); 086 087 for(var i = 0; i < entityAttributeTypeNamesToCheckLength && !hasExecutionErrors(); i++) { 088 var entityAttributeTypeName = entityAttributeTypeNamesToCheck[i]; 089 var entityAttributeType = coreControl.getEntityAttributeTypeByName(entityAttributeTypeName); 090 091 if(entityAttributeType != null) { 092 entityAttributeTypes.add(entityAttributeType); 093 } else { 094 addExecutionError(ExecutionErrors.UnknownEntityAttributeTypeName.name(), entityAttributeTypeName); 095 } 096 } 097 } 098 } 099 100 @Override 101 protected Long getTotalEntities() { 102 Long totalEntities = null; 103 104 if(!hasExecutionErrors()) { 105 if(entityAttributeTypes == null) { 106 totalEntities = coreControl.countEntityAttributesByEntityType(entityType); 107 } else { 108 var totalEntitiesTally = 0L; 109 110 for(var entityAttributeType : entityAttributeTypes) { 111 totalEntitiesTally += coreControl.countEntityAttributesByEntityTypeAndEntityAttributeType( 112 entityType, entityAttributeType); 113 } 114 115 totalEntities = totalEntitiesTally; 116 } 117 } 118 119 return totalEntities; 120 } 121 122 @Override 123 protected Collection<EntityAttribute> getEntities() { 124 Collection<EntityAttribute> entityAttributes = null; 125 126 if(!hasExecutionErrors()) { 127 if(entityAttributeTypes == null) { 128 entityAttributes = coreControl.getEntityAttributesByEntityType(entityType); 129 } else { 130 131 entityAttributes = new ArrayList<>(); 132 133 for(var entityAttributeType : entityAttributeTypes) { 134 entityAttributes.addAll(coreControl.getEntityAttributesByEntityTypeAndEntityAttributeType( 135 entityType, entityAttributeType)); 136 137 } 138 } 139 } 140 141 return entityAttributes; 142 } 143 144 @Override 145 protected BaseResult getResult(Collection<EntityAttribute> entities) { 146 var result = CoreResultFactory.getGetEntityAttributesResult(); 147 148 if(entities != null) { 149 150 result.setEntityAttributes(coreControl.getEntityAttributeTransfers(getUserVisit(), entities, null)); 151 } 152 153 return result; 154 } 155 156}