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