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