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.GetEntityIntegerRangesForm;
020import com.echothree.control.user.core.common.result.CoreResultFactory;
021import com.echothree.model.control.core.common.EntityAttributeTypes;
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.EntityIntegerRange;
027import com.echothree.model.data.core.server.factory.EntityIntegerRangeFactory;
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.BaseMultipleEntitiesCommand;
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 GetEntityIntegerRangesCommand
043        extends BaseMultipleEntitiesCommand<EntityIntegerRange, GetEntityIntegerRangesForm> {
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.EntityIntegerRange.name(), SecurityRoles.List.name())
053                ))
054        ));
055        
056        FORM_FIELD_DEFINITIONS = List.of(
057                new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, true, null, null),
058                new FieldDefinition("EntityTypeName", FieldType.ENTITY_TYPE_NAME, true, null, null),
059                new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, true, null, null)
060        );
061    }
062    
063    /** Creates a new instance of GetEntityIntegerRangesCommand */
064    public GetEntityIntegerRangesCommand() {
065        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
066    }
067
068    EntityAttribute entityAttribute;
069
070    @Override
071    protected Collection<EntityIntegerRange> getEntities() {
072        var componentVendorName = form.getComponentVendorName();
073        var componentVendor = componentControl.getComponentVendorByName(componentVendorName);
074        Collection<EntityIntegerRange> entityIntegerRanges = null;
075
076        if(componentVendor != null) {
077            var entityTypeName = form.getEntityTypeName();
078            var entityType = entityTypeControl.getEntityTypeByName(componentVendor, entityTypeName);
079
080            if(entityType != null) {
081                var entityAttributeName = form.getEntityAttributeName();
082
083                entityAttribute = coreControl.getEntityAttributeByName(entityType, entityAttributeName);
084
085                if(entityAttribute != null) {
086                    var entityAttributeType = entityAttribute.getLastDetail().getEntityAttributeType();
087                    var entityAttributeTypeName = entityAttributeType.getEntityAttributeTypeName();
088
089                    if(entityAttributeTypeName.equals(EntityAttributeTypes.INTEGER.name())) {
090                        entityIntegerRanges = coreControl.getEntityIntegerRanges(entityAttribute);
091                    } else {
092                        addExecutionError(ExecutionErrors.InvalidEntityAttributeType.name(), componentVendorName, entityTypeName, entityAttributeName,
093                                entityAttributeTypeName);
094                    }
095                } else {
096                    addExecutionError(ExecutionErrors.UnknownEntityAttributeName.name(), componentVendorName, entityTypeName, entityAttributeName);
097                }
098            } else {
099                addExecutionError(ExecutionErrors.UnknownEntityTypeName.name(), componentVendorName, entityTypeName);
100            }
101        } else {
102            addExecutionError(ExecutionErrors.UnknownComponentVendorName.name(), componentVendorName);
103        }
104
105        return entityIntegerRanges;
106    }
107
108    @Override
109    protected BaseResult getResult(Collection<EntityIntegerRange> entities) {
110        var result = CoreResultFactory.getGetEntityIntegerRangesResult();
111
112        if(entities != null) {
113            var userVisit = getUserVisit();
114
115            if(session.hasLimit(EntityIntegerRangeFactory.class)) {
116                result.setEntityIntegerRangeCount(coreControl.countEntityIntegerRanges(entityAttribute));
117            }
118
119            result.setEntityAttribute(coreControl.getEntityAttributeTransfer(userVisit, entityAttribute, null));
120            result.setEntityIntegerRanges(coreControl.getEntityIntegerRangeTransfers(userVisit, entities, null));
121        }
122
123        return result;
124    }
125
126}