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.GetEntityClobAttributeForm;
020import com.echothree.control.user.core.common.result.CoreResultFactory;
021import com.echothree.control.user.core.common.result.GetEntityClobAttributeResult;
022import com.echothree.model.control.party.server.control.PartyControl;
023import com.echothree.model.data.core.server.entity.EntityAttribute;
024import com.echothree.model.data.core.server.entity.EntityClobAttribute;
025import com.echothree.model.data.core.server.entity.EntityInstance;
026import com.echothree.model.data.core.server.entity.EntityTypeDetail;
027import com.echothree.model.data.party.server.entity.Language;
028import com.echothree.model.data.user.common.pk.UserVisitPK;
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.common.command.BaseResult;
033import com.echothree.util.server.control.BaseSimpleCommand;
034import com.echothree.util.server.persistence.Session;
035import java.util.Arrays;
036import java.util.Collections;
037import java.util.List;
038
039public class GetEntityClobAttributeCommand
040        extends BaseSimpleCommand<GetEntityClobAttributeForm> {
041    
042    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
043    
044    static {
045        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
046                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, true, null, null),
047                new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, true, null, null),
048                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, false, null, null)
049                ));
050    }
051    
052    /** Creates a new instance of GetEntityClobAttributeCommand */
053    public GetEntityClobAttributeCommand(UserVisitPK userVisitPK, GetEntityClobAttributeForm form) {
054        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, false);
055    }
056    
057    @Override
058    protected BaseResult execute() {
059        var coreControl = getCoreControl();
060        GetEntityClobAttributeResult result = CoreResultFactory.getGetEntityClobAttributeResult();
061        String entityRef = form.getEntityRef();
062        EntityInstance entityInstance = coreControl.getEntityInstanceByEntityRef(entityRef);
063        
064        if(entityInstance != null) {
065            String entityAttributeName = form.getEntityAttributeName();
066            EntityAttribute entityAttribute = coreControl.getEntityAttributeByName(entityInstance.getEntityType(), entityAttributeName);
067            
068            if(entityAttribute != null) {
069                var partyControl = Session.getModelController(PartyControl.class);
070                String languageIsoName = form.getLanguageIsoName();
071                Language language = languageIsoName == null ? null : partyControl.getLanguageByIsoName(languageIsoName);
072                
073                if(language != null) {
074                    EntityClobAttribute entityClobAttribute = language == null ? coreControl.getBestEntityClobAttribute(entityAttribute, entityInstance, getPreferredLanguage())
075                            : coreControl.getEntityClobAttribute(entityAttribute, entityInstance, language);
076                    
077                    if(entityClobAttribute != null) {
078                        result.setEntityClobAttribute(coreControl.getEntityClobAttributeTransfer(getUserVisit(), entityClobAttribute, entityInstance));
079                    } else {
080                        EntityTypeDetail entityTypeDetail = entityInstance.getEntityType().getLastDetail();
081
082                        addExecutionError(ExecutionErrors.UnknownEntityClobAttribute.name(), entityRef,
083                                entityTypeDetail.getComponentVendor().getLastDetail().getComponentVendorName(),
084                                entityTypeDetail.getEntityTypeName(), entityAttributeName, languageIsoName);
085                    }
086                } else {
087                    addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
088                }
089            } else {
090                EntityTypeDetail entityTypeDetail = entityInstance.getEntityType().getLastDetail();
091                
092                addExecutionError(ExecutionErrors.UnknownEntityAttributeName.name(), entityTypeDetail.getComponentVendor().getLastDetail().getComponentVendorName(),
093                        entityTypeDetail.getEntityTypeName(), entityAttributeName);
094            }
095        } else {
096            addExecutionError(ExecutionErrors.UnknownEntityRef.name(), entityRef);
097        }
098        
099        return result;
100    }
101    
102}