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