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.GetEntityLongRangeChoicesForm;
020import com.echothree.control.user.core.common.result.CoreResultFactory;
021import com.echothree.model.control.core.common.EntityAttributeTypes;
022import com.echothree.model.control.core.server.control.EntityInstanceControl;
023import com.echothree.model.control.party.common.PartyTypes;
024import com.echothree.model.control.security.common.SecurityRoleGroups;
025import com.echothree.model.control.security.common.SecurityRoles;
026import com.echothree.model.data.core.server.entity.EntityType;
027import com.echothree.model.data.user.common.pk.UserVisitPK;
028import com.echothree.util.common.command.BaseResult;
029import com.echothree.util.common.message.ExecutionErrors;
030import com.echothree.util.common.validation.FieldDefinition;
031import com.echothree.util.common.validation.FieldType;
032import com.echothree.util.server.control.BaseSimpleCommand;
033import com.echothree.util.server.control.CommandSecurityDefinition;
034import com.echothree.util.server.control.PartyTypeDefinition;
035import com.echothree.util.server.control.SecurityRoleDefinition;
036import com.echothree.util.server.persistence.Session;
037import java.util.Arrays;
038import java.util.Collections;
039import java.util.List;
040import javax.enterprise.context.RequestScoped;
041
042@RequestScoped
043public class GetEntityLongRangeChoicesCommand
044        extends BaseSimpleCommand<GetEntityLongRangeChoicesForm> {
045    
046    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
047    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
048    
049    static {
050        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
051                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
052                        new SecurityRoleDefinition(SecurityRoleGroups.EntityLongRange.name(), SecurityRoles.Choices.name())
053                        )))
054                )));
055        
056        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
057                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
058                new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, false, null, null),
059                new FieldDefinition("EntityTypeName", FieldType.ENTITY_TYPE_NAME, false, null, null),
060                new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, true, null, null)
061                ));
062    }
063    
064    /** Creates a new instance of GetEntityLongRangeChoicesCommand */
065    public GetEntityLongRangeChoicesCommand() {
066        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
067    }
068    
069    @Override
070    protected BaseResult execute() {
071        var result = CoreResultFactory.getGetEntityLongRangeChoicesResult();
072        var entityRef = form.getEntityRef();
073        var componentVendorName = form.getComponentVendorName();
074        var entityTypeName = form.getEntityTypeName();
075        var parameterCount = (entityRef != null && componentVendorName == null && entityTypeName == null? 1: 0)
076                + (entityRef == null && componentVendorName != null && entityTypeName != null? 1: 0);
077        
078        if(parameterCount == 1) {
079            var entityInstanceControl = Session.getModelController(EntityInstanceControl.class);
080            EntityType entityType = null;
081            
082            if(entityRef == null) {
083                var componentVendor = componentControl.getComponentVendorByName(componentVendorName);
084                
085                if(componentVendor != null) {
086                    entityType = entityTypeControl.getEntityTypeByName(componentVendor, entityTypeName);
087                    
088                    if(entityType == null) {
089                        addExecutionError(ExecutionErrors.UnknownEntityTypeName.name(), componentVendorName, entityTypeName);
090                    }
091                } else {
092                    addExecutionError(ExecutionErrors.UnknownComponentVendorName.name(), componentVendorName);
093                }
094            } else {
095                var entityInstance = entityInstanceControl.getEntityInstanceByEntityRef(entityRef);
096                
097                if(entityInstance != null) {
098                    entityType = entityInstance.getEntityType();
099                } else {
100                    addExecutionError(ExecutionErrors.UnknownEntityRef.name(), entityRef);
101                }
102            }
103            
104            if(!hasExecutionErrors()) {
105                var entityAttributeName = form.getEntityAttributeName();
106                var entityAttribute = coreControl.getEntityAttributeByName(entityType, entityAttributeName);
107                
108                if(entityAttribute != null) {
109                    var entityAttributeType = entityAttribute.getLastDetail().getEntityAttributeType();
110                    var entityAttributeTypeName = entityAttributeType.getEntityAttributeTypeName();
111                    
112                    if(entityAttributeTypeName.equals(EntityAttributeTypes.LONG.name())) {
113                        var defaultEntityLongRangeChoice = form.getDefaultEntityLongRangeChoice();
114                        var allowNullChoice = Boolean.valueOf(form.getAllowNullChoice());
115                        
116                        result.setEntityLongRangeChoices(coreControl.getEntityLongRangeChoices(defaultEntityLongRangeChoice,
117                                getPreferredLanguage(), allowNullChoice, entityAttribute));
118                    } else {
119                        addExecutionError(ExecutionErrors.InvalidEntityAttributeType.name());
120                    }
121                } else {
122                    addExecutionError(ExecutionErrors.UnknownEntityAttributeName.name(), entityAttributeName);
123                }
124            }
125        } else {
126            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
127        }
128        
129        return result;
130    }
131    
132}