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