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 java.util.List;
031import javax.enterprise.context.Dependent;
032import javax.inject.Inject;
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    @Inject
050    EntityInstanceControl entityInstanceControl;
051
052    @Inject
053    PartyControl partyControl;
054
055    @Inject
056    ContentLogic contentLogic;
057
058    
059    /** Creates a new instance of GetEntityBlobAttributeCommand */
060    public GetEntityBlobAttributeCommand() {
061        super(null, FORM_FIELD_DEFINITIONS, false);
062    }
063    
064    @Override
065    protected BaseResult execute() {
066        var result = CoreResultFactory.getGetEntityBlobAttributeResult();
067        var entityRef = form.getEntityRef();
068        var entityInstance = entityInstanceControl.getEntityInstanceByEntityRef(entityRef);
069        
070        if(entityInstance != null) {
071            var entityAttributeName = form.getEntityAttributeName();
072            var entityAttribute = coreControl.getEntityAttributeByName(entityInstance.getEntityType(), entityAttributeName);
073            
074            if(entityAttribute != null) {
075                var languageIsoName = form.getLanguageIsoName();
076                var language = languageIsoName == null ? null : partyControl.getLanguageByIsoName(languageIsoName);
077                
078                if(languageIsoName == null || language != null) {
079                    var entityBlobAttribute = language == null ? coreControl.getBestEntityBlobAttribute(entityAttribute, entityInstance, getPreferredLanguage())
080                            : coreControl.getEntityBlobAttribute(entityAttribute, entityInstance, language);
081                    
082                    if(entityBlobAttribute != null) {
083                        var entityAttributeBlob = coreControl.getEntityAttributeBlob(entityAttribute);
084                        
085                        if(entityAttributeBlob != null && entityAttributeBlob.getCheckContentWebAddress()) {
086                            contentLogic.checkReferrer(this, form.getReferrer());
087                        }
088                        
089                        if(!hasExecutionErrors()) {
090                            result.setEntityBlobAttribute(coreControl.getEntityBlobAttributeTransfer(getUserVisit(), entityBlobAttribute, entityInstance));
091                        }
092                    } else {
093                        var entityTypeDetail = entityInstance.getEntityType().getLastDetail();
094
095                        addExecutionError(ExecutionErrors.UnknownEntityBlobAttribute.name(), entityRef,
096                                entityTypeDetail.getComponentVendor().getLastDetail().getComponentVendorName(),
097                                entityTypeDetail.getEntityTypeName(), entityAttributeName, languageIsoName);
098                    }
099                } else {
100                    addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
101                }
102            } else {
103                var entityTypeDetail = entityInstance.getEntityType().getLastDetail();
104                
105                addExecutionError(ExecutionErrors.UnknownEntityAttributeName.name(), entityTypeDetail.getComponentVendor().getLastDetail().getComponentVendorName(),
106                        entityTypeDetail.getEntityTypeName(), entityAttributeName);
107            }
108        } else {
109            addExecutionError(ExecutionErrors.UnknownEntityRef.name(), entityRef);
110        }
111        
112        return result;
113    }
114    
115}