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.model.control.core.server.transfer;
018
019import com.echothree.model.control.core.common.CoreProperties;
020import com.echothree.model.control.core.common.transfer.EntityLongRangeTransfer;
021import com.echothree.model.control.core.server.control.CoreControl;
022import com.echothree.model.data.core.server.entity.EntityInstance;
023import com.echothree.model.data.core.server.entity.EntityLongRange;
024import com.echothree.model.data.user.server.entity.UserVisit;
025import com.echothree.util.common.form.TransferProperties;
026import com.echothree.util.server.persistence.Session;
027import javax.enterprise.context.RequestScoped;
028
029@RequestScoped
030public class EntityLongRangeTransferCache
031        extends BaseCoreTransferCache<EntityLongRange, EntityLongRangeTransfer> {
032
033    CoreControl coreControl = Session.getModelController(CoreControl.class);
034
035    TransferProperties transferProperties;
036    boolean filterEntityAttribute;
037    boolean filterEntityLongRangeName;
038    boolean filterMinimumLongValue;
039    boolean filterMaximumLongValue;
040    boolean filterIsDefault;
041    boolean filterSortOrder;
042    boolean filterDescription;
043    boolean filterEntityInstance;
044
045    /** Creates a new instance of EntityLongRangeTransferCache */
046    protected EntityLongRangeTransferCache() {
047        super();
048        
049        transferProperties = session.getTransferProperties();
050        if(transferProperties != null) {
051            var properties = transferProperties.getProperties(EntityLongRangeTransfer.class);
052            
053            if(properties != null) {
054                filterEntityAttribute = !properties.contains(CoreProperties.ENTITY_ATTRIBUTE);
055                filterEntityLongRangeName = !properties.contains(CoreProperties.ENTITY_LONG_RANGE_NAME);
056                filterMinimumLongValue = !properties.contains(CoreProperties.MINIMUM_LONG_VALUE);
057                filterMaximumLongValue = !properties.contains(CoreProperties.MAXIMUM_LONG_VALUE);
058                filterIsDefault = !properties.contains(CoreProperties.IS_DEFAULT);
059                filterSortOrder = !properties.contains(CoreProperties.SORT_ORDER);
060                filterDescription = !properties.contains(CoreProperties.DESCRIPTION);
061                filterEntityInstance = !properties.contains(CoreProperties.ENTITY_INSTANCE);
062            }
063        }
064        
065        setIncludeEntityInstance(!filterEntityInstance);
066    }
067    
068    public EntityLongRangeTransfer getEntityLongRangeTransfer(final UserVisit userVisit, final EntityLongRange entityLongRange, final EntityInstance entityInstance) {
069        var entityLongRangeTransfer = get(entityLongRange);
070        
071        if(entityLongRangeTransfer == null) {
072            var entityLongRangeDetail = entityLongRange.getLastDetail();
073            var entityAttributeTransfer = filterEntityAttribute ? null : entityInstance == null ? coreControl.getEntityAttributeTransfer(userVisit, entityLongRangeDetail.getEntityAttribute(), entityInstance) : null;
074            var entityLongRangeName = filterEntityLongRangeName ? null : entityLongRangeDetail.getEntityLongRangeName();
075            var minimumLongValue = filterMinimumLongValue ? null : entityLongRangeDetail.getMinimumLongValue();
076            var maximumLongValue = filterMaximumLongValue ? null : entityLongRangeDetail.getMaximumLongValue();
077            var isDefault = filterIsDefault ? null : entityLongRangeDetail.getIsDefault();
078            var sortOrder = filterSortOrder ? null : entityLongRangeDetail.getSortOrder();
079            var description = coreControl.getBestEntityLongRangeDescription(entityLongRange, getLanguage(userVisit));
080            
081            entityLongRangeTransfer = new EntityLongRangeTransfer(entityAttributeTransfer, entityLongRangeName, minimumLongValue, maximumLongValue, isDefault,
082                    sortOrder, description);
083            put(userVisit, entityLongRange, entityLongRangeTransfer);
084        }
085        return entityLongRangeTransfer;
086    }
087    
088}