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