001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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;
039
040public class GetEntityIntegerRangesCommand
041        extends BaseMultipleEntitiesCommand<EntityIntegerRange, GetEntityIntegerRangesForm> {
042
043    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
044    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
045    
046    static {
047        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
048                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
049                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
050                        new SecurityRoleDefinition(SecurityRoleGroups.EntityIntegerRange.name(), SecurityRoles.List.name())
051                ))
052        ));
053        
054        FORM_FIELD_DEFINITIONS = List.of(
055                new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, true, null, null),
056                new FieldDefinition("EntityTypeName", FieldType.ENTITY_TYPE_NAME, true, null, null),
057                new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, true, null, null)
058        );
059    }
060    
061    /** Creates a new instance of GetEntityIntegerRangesCommand */
062    public GetEntityIntegerRangesCommand(UserVisitPK userVisitPK, GetEntityIntegerRangesForm form) {
063        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
064    }
065
066    EntityAttribute entityAttribute;
067
068    @Override
069    protected Collection<EntityIntegerRange> getEntities() {
070        var coreControl = getCoreControl();
071        var componentVendorName = form.getComponentVendorName();
072        var componentVendor = coreControl.getComponentVendorByName(componentVendorName);
073        Collection<EntityIntegerRange> entityIntegerRanges = null;
074
075        if(componentVendor != null) {
076            var entityTypeName = form.getEntityTypeName();
077            var entityType = coreControl.getEntityTypeByName(componentVendor, entityTypeName);
078
079            if(entityType != null) {
080                var entityAttributeName = form.getEntityAttributeName();
081
082                entityAttribute = coreControl.getEntityAttributeByName(entityType, entityAttributeName);
083
084                if(entityAttribute != null) {
085                    var entityAttributeType = entityAttribute.getLastDetail().getEntityAttributeType();
086                    var entityAttributeTypeName = entityAttributeType.getEntityAttributeTypeName();
087
088                    if(entityAttributeTypeName.equals(EntityAttributeTypes.INTEGER.name())) {
089                        entityIntegerRanges = coreControl.getEntityIntegerRanges(entityAttribute);
090                    } else {
091                        addExecutionError(ExecutionErrors.InvalidEntityAttributeType.name(), componentVendorName, entityTypeName, entityAttributeName,
092                                entityAttributeTypeName);
093                    }
094                } else {
095                    addExecutionError(ExecutionErrors.UnknownEntityAttributeName.name(), componentVendorName, entityTypeName, entityAttributeName);
096                }
097            } else {
098                addExecutionError(ExecutionErrors.UnknownEntityTypeName.name(), componentVendorName, entityTypeName);
099            }
100        } else {
101            addExecutionError(ExecutionErrors.UnknownComponentVendorName.name(), componentVendorName);
102        }
103
104        return entityIntegerRanges;
105    }
106
107    @Override
108    protected BaseResult getResult(Collection<EntityIntegerRange> entities) {
109        var result = CoreResultFactory.getGetEntityIntegerRangesResult();
110
111        if(entities != null) {
112            var coreControl = getCoreControl();
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}