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