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.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;
039import javax.enterprise.context.RequestScoped;
040
041@RequestScoped
042public class GetEntityAttributeEntityAttributeGroupsCommand
043        extends BasePaginatedMultipleEntitiesCommand<EntityAttributeEntityAttributeGroup, GetEntityAttributeEntityAttributeGroupsForm> {
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.EntityAttributeEntityAttributeGroup.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("EntityAttributeName", FieldType.ENTITY_NAME, false, null, null),
060                new FieldDefinition("EntityAttributeGroupName", FieldType.ENTITY_NAME, false, null, null)
061        );
062    }
063    
064    /** Creates a new instance of GetEntityAttributeEntityAttributeGroupsCommand */
065    public GetEntityAttributeEntityAttributeGroupsCommand() {
066        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
067    }
068
069    EntityAttribute entityAttribute;
070    EntityAttributeGroup entityAttributeGroup;
071
072    @Override
073    protected void handleForm() {
074        var componentVendorName = form.getComponentVendorName();
075        var entityTypeName = form.getEntityTypeName();
076        var entityAttributeName = form.getEntityAttributeName();
077        var entityAttributeGroupName = form.getEntityAttributeGroupName();
078        var parameterCount = ((componentVendorName != null) && (entityTypeName != null) && (entityAttributeName != null) ? 1 : 0)
079                + (entityAttributeGroupName != null ? 1 : 0);
080
081        if(parameterCount == 1) {
082            if(entityAttributeGroupName == null) {
083                entityAttribute = EntityAttributeLogic.getInstance().getEntityAttributeByName(this, componentVendorName, entityTypeName, entityAttributeName);
084            } else { // entityAttributeGroup
085                entityAttributeGroup = EntityAttributeLogic.getInstance().getEntityAttributeGroupByName(this, entityAttributeGroupName);
086            }
087        } else {
088            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
089        }
090    }
091
092    @Override
093    protected Long getTotalEntities() {
094        Long totalEntities = null;
095
096        if(!hasExecutionErrors()) {
097
098            if(entityAttribute != null) {
099                totalEntities = coreControl.countEntityAttributeEntityAttributeGroupsByEntityAttribute(entityAttribute);
100            } else {
101                totalEntities = coreControl.countEntityAttributeEntityAttributeGroupsByEntityAttributeGroup(entityAttributeGroup);
102            }
103        }
104
105        return totalEntities;
106    }
107
108    @Override
109    protected Collection<EntityAttributeEntityAttributeGroup> getEntities() {
110        Collection<EntityAttributeEntityAttributeGroup> result = null;
111
112        if(!hasExecutionErrors()) {
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 userVisit = getUserVisit();
130
131            if(entityAttributeGroup == null) {
132                result.setEntityAttribute(coreControl.getEntityAttributeTransfer(userVisit, entityAttribute, null));
133            } else {
134                result.setEntityAttributeGroup(coreControl.getEntityAttributeGroupTransfer(userVisit, entityAttributeGroup, null));
135            }
136
137            result.setEntityAttributeEntityAttributeGroups(coreControl.getEntityAttributeEntityAttributeGroupTransfers(userVisit, entities, null));
138        }
139
140        return result;
141    }
142
143}