001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.EntityAttributeGroupDetail;
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 com.echothree.util.server.persistence.Session;
029import java.util.List;
030import java.util.Set;
031
032public class EntityAttributeGroupTransferCache
033        extends BaseCoreTransferCache<EntityAttributeGroup, EntityAttributeGroupTransfer> {
034
035    CoreControl coreControl = Session.getModelController(CoreControl.class);
036
037    boolean includeEntityAttributes;
038    
039    /** Creates a new instance of EntityAttributeGroupTransferCache */
040    public EntityAttributeGroupTransferCache(UserVisit userVisit) {
041        super(userVisit);
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(EntityAttributeGroup entityAttributeGroup, EntityInstance entityInstance) {
052        EntityAttributeGroupTransfer entityAttributeGroupTransfer = get(entityAttributeGroup);
053        
054        if(entityAttributeGroupTransfer == null) {
055            EntityAttributeGroupDetail entityAttributeGroupDetail = entityAttributeGroup.getLastDetail();
056            String entityAttributeGroupName = entityAttributeGroupDetail.getEntityAttributeGroupName();
057            Boolean isDefault = entityAttributeGroupDetail.getIsDefault();
058            Integer sortOrder = entityAttributeGroupDetail.getSortOrder();
059            String description = coreControl.getBestEntityAttributeGroupDescription(entityAttributeGroup, getLanguage());
060            
061            entityAttributeGroupTransfer = new EntityAttributeGroupTransfer(entityAttributeGroupName, isDefault, sortOrder, description);
062            if(entityInstance == null) {
063                put(entityAttributeGroup, entityAttributeGroupTransfer);
064            } else {
065                setupEntityInstance(entityAttributeGroup, null, entityAttributeGroupTransfer);
066            }
067            
068            if(includeEntityAttributes) {
069                if(entityInstance != null) {
070                    List<EntityAttributeTransfer> entityAttributeTransfers = coreControl.getEntityAttributeTransfersByEntityAttributeGroupAndEntityType(userVisit,
071                            entityAttributeGroup, entityInstance.getEntityType(), entityInstance);
072                    MapWrapper<EntityAttributeTransfer> mapWrapper = new MapWrapper<>(entityAttributeTransfers.size());
073
074                    entityAttributeTransfers.forEach((entityAttributeTransfer) -> {
075                        mapWrapper.put(entityAttributeTransfer.getEntityAttributeName(), entityAttributeTransfer);
076                    });
077
078                    entityAttributeGroupTransfer.setEntityAttributes(mapWrapper);
079                } else {
080                    getLog().error("entityInstance is null");
081                }
082            }
083        }
084        
085        return entityAttributeGroupTransfer;
086    }
087    
088}