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.model.control.core.server.transfer;
018
019import com.echothree.model.control.core.common.CoreOptions;
020import com.echothree.model.control.core.common.transfer.EntityAttributeGroupTransfer;
021import com.echothree.model.control.core.common.transfer.EntityAttributeTransfer;
022import com.echothree.model.control.core.server.control.EntityTypeControl;
023import com.echothree.model.data.core.server.entity.EntityAttributeGroup;
024import com.echothree.model.data.core.server.entity.EntityInstance;
025import com.echothree.model.data.user.server.entity.UserVisit;
026import com.echothree.util.common.transfer.MapWrapper;
027import javax.enterprise.context.RequestScoped;
028import javax.inject.Inject;
029
030@RequestScoped
031public class EntityAttributeGroupTransferCache
032        extends BaseCoreTransferCache<EntityAttributeGroup, EntityAttributeGroupTransfer> {
033    
034    boolean includeEntityAttributes;
035    
036    @Inject
037    protected EntityTypeControl entityTypeControl;
038
039    /** Creates a new instance of EntityAttributeGroupTransferCache */
040    protected EntityAttributeGroupTransferCache() {
041        super();
042        
043        var options = session.getOptions();
044        if(options != null) {
045            includeEntityAttributes = options.contains(CoreOptions.EntityAttributeGroupIncludeEntityAttributes);
046        }
047
048        setIncludeEntityInstance(true);
049    }
050    
051    public EntityAttributeGroupTransfer getEntityAttributeGroupTransfer(final UserVisit userVisit, final EntityAttributeGroup entityAttributeGroup, final EntityInstance entityInstance) {
052        var entityAttributeGroupTransfer = get(entityAttributeGroup);
053        
054        if(entityAttributeGroupTransfer == null) {
055            var entityAttributeGroupDetail = entityAttributeGroup.getLastDetail();
056            var entityAttributeGroupName = entityAttributeGroupDetail.getEntityAttributeGroupName();
057            var isDefault = entityAttributeGroupDetail.getIsDefault();
058            var sortOrder = entityAttributeGroupDetail.getSortOrder();
059            var description = entityAttributeGroupControl.getBestEntityAttributeGroupDescription(entityAttributeGroup, getLanguage(userVisit));
060            
061            entityAttributeGroupTransfer = new EntityAttributeGroupTransfer(entityTypeControl.getEntityTypeTransfer(userVisit, entityAttributeGroupDetail.getEntityType()), entityAttributeGroupName, isDefault, sortOrder, description);
062
063            if(entityInstance == null) {
064                put(userVisit, entityAttributeGroup, entityAttributeGroupTransfer);
065            } else {
066                setupEntityInstance(userVisit, entityAttributeGroup, null, entityAttributeGroupTransfer);
067            }
068            
069            if(includeEntityAttributes) {
070                if(entityInstance != null) {
071                    var entityAttributeTransfers = coreControl.getEntityAttributeTransfersByEntityAttributeGroup(userVisit,
072                            entityAttributeGroup, entityInstance);
073                    var mapWrapper = new MapWrapper<EntityAttributeTransfer>(entityAttributeTransfers.size());
074
075                    entityAttributeTransfers.forEach((entityAttributeTransfer) -> {
076                        mapWrapper.put(entityAttributeTransfer.getEntityAttributeName(), entityAttributeTransfer);
077                    });
078
079                    entityAttributeGroupTransfer.setEntityAttributes(mapWrapper);
080                } else {
081                    log.error("entityInstance is null");
082                }
083            }
084        }
085        
086        return entityAttributeGroupTransfer;
087    }
088    
089}