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.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; 041 042public class GetEntityAttributesCommand 043 extends BasePaginatedMultipleEntitiesCommand<EntityAttribute, GetEntityAttributesForm> { 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.List.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("EntityRef", FieldType.ENTITY_REF, false, null, null), 060 new FieldDefinition("Key", FieldType.KEY, false, null, null), 061 new FieldDefinition("Guid", FieldType.GUID, false, null, null), 062 new FieldDefinition("Ulid", FieldType.ULID, 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(UserVisitPK userVisitPK, GetEntityAttributesForm form) { 069 super(userVisitPK, form, 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 coreControl = getCoreControl(); 083 var entityAttributeTypeNamesToCheck = Splitter.on(':').trimResults().omitEmptyStrings().splitToList(entityAttributeTypeNames).toArray(new String[0]); 084 var entityAttributeTypeNamesToCheckLength = entityAttributeTypeNamesToCheck.length; 085 086 entityAttributeTypes = new ArrayList<>(); 087 088 for(int i = 0; i < entityAttributeTypeNamesToCheckLength && !hasExecutionErrors(); i++) { 089 var entityAttributeTypeName = entityAttributeTypeNamesToCheck[i]; 090 var entityAttributeType = coreControl.getEntityAttributeTypeByName(entityAttributeTypeName); 091 092 if(entityAttributeType != null) { 093 entityAttributeTypes.add(entityAttributeType); 094 } else { 095 addExecutionError(ExecutionErrors.UnknownEntityAttributeTypeName.name(), entityAttributeTypeName); 096 } 097 } 098 } 099 } 100 101 @Override 102 protected Long getTotalEntities() { 103 Long totalEntities = null; 104 105 if(!hasExecutionErrors()) { 106 if(entityAttributeTypes == null) { 107 totalEntities = getCoreControl().countEntityAttributesByEntityType(entityType); 108 } else { 109 var coreControl = getCoreControl(); 110 var totalEntitiesTally = 0L; 111 112 for(var entityAttributeType : entityAttributeTypes) { 113 totalEntitiesTally += coreControl.countEntityAttributesByEntityTypeAndEntityAttributeType( 114 entityType, entityAttributeType); 115 } 116 117 totalEntities = totalEntitiesTally; 118 } 119 } 120 121 return totalEntities; 122 } 123 124 @Override 125 protected Collection<EntityAttribute> getEntities() { 126 Collection<EntityAttribute> entityAttributes = null; 127 128 if(!hasExecutionErrors()) { 129 if(entityAttributeTypes == null) { 130 entityAttributes = getCoreControl().getEntityAttributesByEntityType(entityType); 131 } else { 132 var coreControl = getCoreControl(); 133 134 entityAttributes = new ArrayList<>(); 135 136 for(var entityAttributeType : entityAttributeTypes) { 137 entityAttributes.addAll(coreControl.getEntityAttributesByEntityTypeAndEntityAttributeType( 138 entityType, entityAttributeType)); 139 140 } 141 } 142 } 143 144 return entityAttributes; 145 } 146 147 @Override 148 protected BaseResult getResult(Collection<EntityAttribute> entities) { 149 var result = CoreResultFactory.getGetEntityAttributesResult(); 150 151 if(entities != null) { 152 var coreControl = getCoreControl(); 153 154 result.setEntityAttributes(coreControl.getEntityAttributeTransfers(getUserVisit(), entities, null)); 155 } 156 157 return result; 158 } 159 160}