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.GetEntityIntegerRangeForm;
020import com.echothree.control.user.core.common.result.CoreResultFactory;
021import com.echothree.model.control.core.common.EntityAttributeTypes;
022import com.echothree.model.data.core.server.entity.EntityIntegerRange;
023import com.echothree.model.data.user.common.pk.UserVisitPK;
024import com.echothree.util.common.command.BaseResult;
025import com.echothree.util.common.message.ExecutionErrors;
026import com.echothree.util.common.validation.FieldDefinition;
027import com.echothree.util.common.validation.FieldType;
028import com.echothree.util.server.control.BaseSingleEntityCommand;
029import java.util.List;
030
031public class GetEntityIntegerRangeCommand
032        extends BaseSingleEntityCommand<EntityIntegerRange, GetEntityIntegerRangeForm> {
033
034    // No COMMAND_SECURITY_DEFINITION, anyone may execute this command.
035    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
036    
037    static {
038        FORM_FIELD_DEFINITIONS = List.of(
039                new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, true, null, null),
040                new FieldDefinition("EntityTypeName", FieldType.ENTITY_TYPE_NAME, true, null, null),
041                new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, true, null, null),
042                new FieldDefinition("EntityIntegerRangeName", FieldType.ENTITY_NAME, true, null, null)
043        );
044    }
045    
046    /** Creates a new instance of GetEntityIntegerRangeCommand */
047    public GetEntityIntegerRangeCommand(UserVisitPK userVisitPK, GetEntityIntegerRangeForm form) {
048        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, true);
049    }
050
051    @Override
052    protected EntityIntegerRange getEntity() {
053        var coreControl = getCoreControl();
054        EntityIntegerRange entityIntegerRange = null;
055        var componentVendorName = form.getComponentVendorName();
056        var componentVendor = coreControl.getComponentVendorByName(componentVendorName);
057
058        if(componentVendor != null) {
059            var entityTypeName = form.getEntityTypeName();
060            var entityType = coreControl.getEntityTypeByName(componentVendor, entityTypeName);
061
062            if(entityType != null) {
063                var entityAttributeName = form.getEntityAttributeName();
064                var entityAttribute = coreControl.getEntityAttributeByName(entityType, entityAttributeName);
065
066                if(entityAttribute != null) {
067                    var entityAttributeType = entityAttribute.getLastDetail().getEntityAttributeType();
068                    var entityAttributeTypeName = entityAttributeType.getEntityAttributeTypeName();
069
070                    if(entityAttributeTypeName.equals(EntityAttributeTypes.INTEGER.name())) {
071                        var entityIntegerRangeName = form.getEntityIntegerRangeName();
072
073                        entityIntegerRange = coreControl.getEntityIntegerRangeByName(entityAttribute, entityIntegerRangeName);
074
075                        if(entityIntegerRange == null) {
076                            addExecutionError(ExecutionErrors.UnknownEntityIntegerRangeName.name(), componentVendorName, entityTypeName, entityAttributeName, entityIntegerRangeName);
077                        }
078                    } else {
079                        addExecutionError(ExecutionErrors.InvalidEntityAttributeType.name(), componentVendorName, entityTypeName, entityAttributeName, entityAttributeTypeName);
080                    }
081                } else {
082                    addExecutionError(ExecutionErrors.UnknownEntityAttributeName.name(), componentVendorName, entityTypeName, entityAttributeName);
083                }
084            } else {
085                addExecutionError(ExecutionErrors.UnknownEntityTypeName.name(), componentVendorName, entityTypeName);
086            }
087        } else {
088            addExecutionError(ExecutionErrors.UnknownComponentVendorName.name(), componentVendorName);
089        }
090
091        return entityIntegerRange;
092    }
093
094    @Override
095    protected BaseResult getResult(EntityIntegerRange entityIntegerRange) {
096        var result = CoreResultFactory.getGetEntityIntegerRangeResult();
097
098        if(entityIntegerRange != null) {
099            var coreControl = getCoreControl();
100
101            result.setEntityIntegerRange(coreControl.getEntityIntegerRangeTransfer(getUserVisit(), entityIntegerRange, null));
102        }
103
104        return result;
105    }
106
107}