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