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.control.CoreControl;
022import com.echothree.model.control.core.server.logic.EntityAttributeLogic;
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.EntityAttribute;
027import com.echothree.model.data.core.server.entity.EntityAttributeEntityAttributeGroup;
028import com.echothree.model.data.core.server.entity.EntityAttributeGroup;
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;
039import javax.enterprise.context.Dependent;
040import javax.inject.Inject;
041
042@Dependent
043public class GetEntityAttributeEntityAttributeGroupsCommand
044        extends BasePaginatedMultipleEntitiesCommand<EntityAttributeEntityAttributeGroup, GetEntityAttributeEntityAttributeGroupsForm> {
045
046    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
047    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
048    
049    static {
050        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
051                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
052                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
053                        new SecurityRoleDefinition(SecurityRoleGroups.EntityAttribute.name(), SecurityRoles.EntityAttributeEntityAttributeGroup.name())
054                ))
055        ));
056        
057        FORM_FIELD_DEFINITIONS = List.of(
058                new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, false, null, null),
059                new FieldDefinition("EntityTypeName", FieldType.ENTITY_TYPE_NAME, false, null, null),
060                new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, false, null, null),
061                new FieldDefinition("EntityAttributeGroupName", FieldType.ENTITY_NAME, false, null, null)
062        );
063    }
064    
065    /** Creates a new instance of GetEntityAttributeEntityAttributeGroupsCommand */
066    public GetEntityAttributeEntityAttributeGroupsCommand() {
067        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
068    }
069    
070    @Inject
071    EntityAttributeLogic entityAttributeLogic;
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}