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.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;
041import javax.enterprise.context.Dependent;
042import javax.inject.Inject;
043
044@Dependent
045public class GetEntityAttributesCommand
046        extends BasePaginatedMultipleEntitiesCommand<EntityAttribute, GetEntityAttributesForm> {
047
048    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
049    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
050    
051    static {
052        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
053                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
054                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
055                        new SecurityRoleDefinition(SecurityRoleGroups.EntityAttribute.name(), SecurityRoles.List.name())
056                ))
057        ));
058
059        FORM_FIELD_DEFINITIONS = List.of(
060                new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, false, null, null),
061                new FieldDefinition("EntityTypeName", FieldType.ENTITY_TYPE_NAME, false, null, null),
062                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
063                new FieldDefinition("Uuid", FieldType.UUID, false, null, null),
064                new FieldDefinition("EntityAttributeTypeNames", FieldType.STRING, false, null, null)
065        );
066    }
067
068    @Inject
069    EntityTypeLogic entityTypeLogic;
070
071    
072    /** Creates a new instance of GetEntityAttributesCommand */
073    public GetEntityAttributesCommand() {
074        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
075    }
076
077    EntityType entityType;
078    Collection<EntityAttributeType> entityAttributeTypes;
079
080    @Override
081    protected void handleForm() {
082        var entityAttributeTypeNames = form.getEntityAttributeTypeNames();
083
084        entityType = entityTypeLogic.getEntityTypeByUniversalSpec(this, form);
085
086        if(!hasExecutionErrors() && entityAttributeTypeNames != null) {
087            var entityAttributeTypeNamesToCheck = Splitter.on(':').trimResults().omitEmptyStrings().splitToList(entityAttributeTypeNames).toArray(new String[0]);
088            var entityAttributeTypeNamesToCheckLength = entityAttributeTypeNamesToCheck.length;
089
090            entityAttributeTypes = new ArrayList<>();
091
092            for(var i = 0; i < entityAttributeTypeNamesToCheckLength && !hasExecutionErrors(); i++) {
093                var entityAttributeTypeName = entityAttributeTypeNamesToCheck[i];
094                var entityAttributeType = coreControl.getEntityAttributeTypeByName(entityAttributeTypeName);
095
096                if(entityAttributeType != null) {
097                    entityAttributeTypes.add(entityAttributeType);
098                } else {
099                    addExecutionError(ExecutionErrors.UnknownEntityAttributeTypeName.name(), entityAttributeTypeName);
100                }
101            }
102        }
103    }
104
105    @Override
106    protected Long getTotalEntities() {
107        Long totalEntities = null;
108
109        if(!hasExecutionErrors()) {
110            if(entityAttributeTypes == null) {
111                totalEntities = coreControl.countEntityAttributesByEntityType(entityType);
112            } else {
113                var totalEntitiesTally = 0L;
114
115                for(var entityAttributeType : entityAttributeTypes) {
116                    totalEntitiesTally += coreControl.countEntityAttributesByEntityTypeAndEntityAttributeType(
117                            entityType, entityAttributeType);
118                }
119
120                totalEntities = totalEntitiesTally;
121            }
122        }
123
124        return totalEntities;
125    }
126
127    @Override
128    protected Collection<EntityAttribute> getEntities() {
129        Collection<EntityAttribute> entityAttributes = null;
130
131        if(!hasExecutionErrors()) {
132            if(entityAttributeTypes == null) {
133                entityAttributes = coreControl.getEntityAttributesByEntityType(entityType);
134            } else {
135
136                entityAttributes = new ArrayList<>();
137
138                for(var entityAttributeType : entityAttributeTypes) {
139                    entityAttributes.addAll(coreControl.getEntityAttributesByEntityTypeAndEntityAttributeType(
140                            entityType, entityAttributeType));
141
142                }
143            }
144        }
145
146        return entityAttributes;
147    }
148
149    @Override
150    protected BaseResult getResult(Collection<EntityAttribute> entities) {
151        var result = CoreResultFactory.getGetEntityAttributesResult();
152
153        if(entities != null) {
154
155            result.setEntityAttributes(coreControl.getEntityAttributeTransfers(getUserVisit(), entities, null));
156        }
157
158        return result;
159    }
160
161}