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.GetEntityClobAttributeForm;
020import com.echothree.control.user.core.common.result.CoreResultFactory;
021import com.echothree.model.control.core.server.control.EntityInstanceControl;
022import com.echothree.model.control.core.server.logic.EntityAttributeLogic;
023import com.echothree.model.control.party.server.control.PartyControl;
024import com.echothree.model.data.core.server.entity.EntityClobAttribute;
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.BaseSingleEntityCommand;
030import java.util.List;
031import javax.enterprise.context.Dependent;
032import javax.inject.Inject;
033
034@Dependent
035public class GetEntityClobAttributeCommand
036        extends BaseSingleEntityCommand<EntityClobAttribute, GetEntityClobAttributeForm> {
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        );
046    }
047
048    @Inject
049    EntityInstanceControl entityInstanceControl;
050
051    @Inject
052    PartyControl partyControl;
053
054    @Inject
055    EntityAttributeLogic entityAttributeLogic;
056
057    /** Creates a new instance of GetEntityClobAttributeCommand */
058    public GetEntityClobAttributeCommand() {
059        super(null, FORM_FIELD_DEFINITIONS, false);
060    }
061    
062    @Override
063    protected EntityClobAttribute getEntity() {
064        EntityClobAttribute entityClobAttribute = null;
065        var entityRef = form.getEntityRef();
066        var entityInstance = entityInstanceControl.getEntityInstanceByEntityRef(entityRef);
067        
068        if(entityInstance != null) {
069            var entityAttributeName = form.getEntityAttributeName();
070            var entityAttribute = entityAttributeLogic.getEntityAttributeByName(this, entityInstance.getEntityType(), entityAttributeName);
071            
072            if(!hasExecutionErrors()) {
073                var languageIsoName = form.getLanguageIsoName();
074                var language = languageIsoName == null ? null : partyControl.getLanguageByIsoName(languageIsoName);
075                
076                if(languageIsoName == null || language != null) {
077                    entityClobAttribute = language == null ? coreControl.getBestEntityClobAttribute(entityAttribute, entityInstance, getPreferredLanguage())
078                            : coreControl.getEntityClobAttribute(entityAttribute, entityInstance, language);
079                    
080                    if(entityClobAttribute == null) {
081                        var entityTypeDetail = entityInstance.getEntityType().getLastDetail();
082
083                        addExecutionError(ExecutionErrors.UnknownEntityClobAttribute.name(), entityRef,
084                                entityTypeDetail.getComponentVendor().getLastDetail().getComponentVendorName(),
085                                entityTypeDetail.getEntityTypeName(), entityAttributeName, languageIsoName);
086                    }
087                } else {
088                    addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
089                }
090            }
091        } else {
092            addExecutionError(ExecutionErrors.UnknownEntityRef.name(), entityRef);
093        }
094        
095        return entityClobAttribute;
096    }
097
098    @Override
099    protected BaseResult getResult(EntityClobAttribute entityClobAttribute) {
100        var result = CoreResultFactory.getGetEntityClobAttributeResult();
101
102        if(entityClobAttribute != null) {
103            result.setEntityClobAttribute(coreControl.getEntityClobAttributeTransfer(getUserVisit(), entityClobAttribute,
104                    entityClobAttribute.getEntityInstance()));
105        }
106
107        return result;
108    }
109    
110}