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.GetEntityBlobAttributeForm;
020import com.echothree.control.user.core.common.result.CoreResultFactory;
021import com.echothree.control.user.core.common.result.GetEntityBlobAttributeResult;
022import com.echothree.model.control.content.server.logic.ContentLogic;
023import com.echothree.model.control.party.server.control.PartyControl;
024import com.echothree.model.data.core.server.entity.EntityAttribute;
025import com.echothree.model.data.core.server.entity.EntityAttributeBlob;
026import com.echothree.model.data.core.server.entity.EntityBlobAttribute;
027import com.echothree.model.data.core.server.entity.EntityInstance;
028import com.echothree.model.data.core.server.entity.EntityTypeDetail;
029import com.echothree.model.data.party.server.entity.Language;
030import com.echothree.model.data.user.common.pk.UserVisitPK;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.common.command.BaseResult;
035import com.echothree.util.server.control.BaseSimpleCommand;
036import com.echothree.util.server.persistence.Session;
037import java.util.Arrays;
038import java.util.Collections;
039import java.util.List;
040
041public class GetEntityBlobAttributeCommand
042        extends BaseSimpleCommand<GetEntityBlobAttributeForm> {
043    
044    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
045    
046    static {
047        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
048                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, true, null, null),
049                new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, true, null, null),
050                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, false, null, null),
051                new FieldDefinition("Referrer", FieldType.URL, false, null, null)
052                ));
053    }
054    
055    /** Creates a new instance of GetEntityBlobAttributeCommand */
056    public GetEntityBlobAttributeCommand(UserVisitPK userVisitPK, GetEntityBlobAttributeForm form) {
057        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, false);
058    }
059    
060    @Override
061    protected BaseResult execute() {
062        var coreControl = getCoreControl();
063        GetEntityBlobAttributeResult result = CoreResultFactory.getGetEntityBlobAttributeResult();
064        String entityRef = form.getEntityRef();
065        EntityInstance entityInstance = coreControl.getEntityInstanceByEntityRef(entityRef);
066        
067        if(entityInstance != null) {
068            String entityAttributeName = form.getEntityAttributeName();
069            EntityAttribute entityAttribute = coreControl.getEntityAttributeByName(entityInstance.getEntityType(), entityAttributeName);
070            
071            if(entityAttribute != null) {
072                var partyControl = Session.getModelController(PartyControl.class);
073                String languageIsoName = form.getLanguageIsoName();
074                Language language = languageIsoName == null ? null : partyControl.getLanguageByIsoName(languageIsoName);
075                
076                if(languageIsoName == null || language != null) {
077                    EntityBlobAttribute entityBlobAttribute = language == null ? coreControl.getBestEntityBlobAttribute(entityAttribute, entityInstance, getPreferredLanguage())
078                            : coreControl.getEntityBlobAttribute(entityAttribute, entityInstance, language);
079                    
080                    if(entityBlobAttribute != null) {
081                        EntityAttributeBlob entityAttributeBlob = coreControl.getEntityAttributeBlob(entityAttribute);
082                        
083                        if(entityAttributeBlob != null && entityAttributeBlob.getCheckContentWebAddress()) {
084                            ContentLogic.getInstance().checkReferrer(this, form.getReferrer());
085                        }
086                        
087                        if(!hasExecutionErrors()) {
088                            result.setEntityBlobAttribute(coreControl.getEntityBlobAttributeTransfer(getUserVisit(), entityBlobAttribute, entityInstance));
089                        }
090                    } else {
091                        EntityTypeDetail entityTypeDetail = entityInstance.getEntityType().getLastDetail();
092
093                        addExecutionError(ExecutionErrors.UnknownEntityBlobAttribute.name(), entityRef,
094                                entityTypeDetail.getComponentVendor().getLastDetail().getComponentVendorName(),
095                                entityTypeDetail.getEntityTypeName(), entityAttributeName, languageIsoName);
096                    }
097                } else {
098                    addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
099                }
100            } else {
101                EntityTypeDetail entityTypeDetail = entityInstance.getEntityType().getLastDetail();
102                
103                addExecutionError(ExecutionErrors.UnknownEntityAttributeName.name(), entityTypeDetail.getComponentVendor().getLastDetail().getComponentVendorName(),
104                        entityTypeDetail.getEntityTypeName(), entityAttributeName);
105            }
106        } else {
107            addExecutionError(ExecutionErrors.UnknownEntityRef.name(), entityRef);
108        }
109        
110        return result;
111    }
112    
113}