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.GetEntityIntegerRangeDescriptionForm;
020import com.echothree.control.user.core.common.result.CoreResultFactory;
021import com.echothree.control.user.core.common.result.GetEntityIntegerRangeDescriptionResult;
022import com.echothree.model.control.core.common.EntityAttributeTypes;
023import com.echothree.model.control.party.common.PartyTypes;
024import com.echothree.model.control.party.server.control.PartyControl;
025import com.echothree.model.control.security.common.SecurityRoleGroups;
026import com.echothree.model.control.security.common.SecurityRoles;
027import com.echothree.model.data.core.server.entity.ComponentVendor;
028import com.echothree.model.data.core.server.entity.EntityAttribute;
029import com.echothree.model.data.core.server.entity.EntityAttributeType;
030import com.echothree.model.data.core.server.entity.EntityIntegerRange;
031import com.echothree.model.data.core.server.entity.EntityIntegerRangeDescription;
032import com.echothree.model.data.core.server.entity.EntityType;
033import com.echothree.model.data.party.server.entity.Language;
034import com.echothree.model.data.user.common.pk.UserVisitPK;
035import com.echothree.util.common.message.ExecutionErrors;
036import com.echothree.util.common.validation.FieldDefinition;
037import com.echothree.util.common.validation.FieldType;
038import com.echothree.util.common.command.BaseResult;
039import com.echothree.util.server.control.BaseSimpleCommand;
040import com.echothree.util.server.control.CommandSecurityDefinition;
041import com.echothree.util.server.control.PartyTypeDefinition;
042import com.echothree.util.server.control.SecurityRoleDefinition;
043import com.echothree.util.server.persistence.Session;
044import java.util.Arrays;
045import java.util.Collections;
046import java.util.List;
047
048public class GetEntityIntegerRangeDescriptionCommand
049        extends BaseSimpleCommand<GetEntityIntegerRangeDescriptionForm> {
050    
051    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
052    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
053    
054    static {
055        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
056                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
057                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
058                        new SecurityRoleDefinition(SecurityRoleGroups.EntityIntegerRange.name(), SecurityRoles.Description.name())
059                        )))
060                )));
061        
062        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
063                new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, true, null, null),
064                new FieldDefinition("EntityTypeName", FieldType.ENTITY_TYPE_NAME, true, null, null),
065                new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, true, null, null),
066                new FieldDefinition("EntityIntegerRangeName", FieldType.ENTITY_NAME, true, null, null)
067                ));
068    }
069    
070    /** Creates a new instance of GetEntityIntegerRangeDescriptionCommand */
071    public GetEntityIntegerRangeDescriptionCommand(UserVisitPK userVisitPK, GetEntityIntegerRangeDescriptionForm form) {
072        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
073    }
074    
075    @Override
076    protected BaseResult execute() {
077        var coreControl = getCoreControl();
078        GetEntityIntegerRangeDescriptionResult result = CoreResultFactory.getGetEntityIntegerRangeDescriptionResult();
079        String componentVendorName = form.getComponentVendorName();
080        ComponentVendor componentVendor = coreControl.getComponentVendorByName(componentVendorName);
081
082        if(componentVendor != null) {
083            String entityTypeName = form.getEntityTypeName();
084            EntityType entityType = coreControl.getEntityTypeByName(componentVendor, entityTypeName);
085
086            if(entityType != null) {
087                String entityAttributeName = form.getEntityAttributeName();
088                EntityAttribute entityAttribute = coreControl.getEntityAttributeByName(entityType, entityAttributeName);
089
090                if(entityAttribute != null) {
091                    EntityAttributeType entityAttributeType = entityAttribute.getLastDetail().getEntityAttributeType();
092                    String entityAttributeTypeName = entityAttributeType.getEntityAttributeTypeName();
093
094                    if(entityAttributeTypeName.equals(EntityAttributeTypes.INTEGER.name())) {
095                        String entityIntegerRangeName = form.getEntityIntegerRangeName();
096                        EntityIntegerRange entityIntegerRange = coreControl.getEntityIntegerRangeByName(entityAttribute, entityIntegerRangeName);
097
098                        if(entityIntegerRange != null) {
099                            var partyControl = Session.getModelController(PartyControl.class);
100                            String languageIsoName = form.getLanguageIsoName();
101                            Language language = partyControl.getLanguageByIsoName(languageIsoName);
102
103                            if(language != null) {
104                                EntityIntegerRangeDescription entityIntegerRangeDescription = coreControl.getEntityIntegerRangeDescription(entityIntegerRange, language);
105
106                                if(entityIntegerRangeDescription != null) {
107                                    result.setEntityIntegerRangeDescription(coreControl.getEntityIntegerRangeDescriptionTransfer(getUserVisit(), entityIntegerRangeDescription, null));
108                                } else {
109                                    addExecutionError(ExecutionErrors.UnknownEntityIntegerRangeDescription.name(), componentVendorName, entityTypeName, entityAttributeName,
110                                            entityIntegerRangeName, languageIsoName);
111                                }
112                            } else {
113                                addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
114                            }
115                        } else {
116                            addExecutionError(ExecutionErrors.UnknownEntityIntegerRangeName.name(), componentVendorName, entityTypeName, entityAttributeName, entityIntegerRangeName);
117                        }
118                    } else {
119                        addExecutionError(ExecutionErrors.InvalidEntityAttributeType.name(), componentVendorName, entityTypeName, entityAttributeName, entityAttributeTypeName);
120                    }
121                } else {
122                    addExecutionError(ExecutionErrors.UnknownEntityAttributeName.name(), componentVendorName, entityTypeName, entityAttributeName);
123                }
124            } else {
125                addExecutionError(ExecutionErrors.UnknownEntityTypeName.name(), componentVendorName, entityTypeName);
126            }
127        } else {
128            addExecutionError(ExecutionErrors.UnknownComponentVendorName.name(), componentVendorName);
129        }
130
131        return result;
132    }
133    
134}