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.EntityAttributeTransfer;
021import com.echothree.model.control.core.common.transfer.EntityClobAttributeTransfer;
022import com.echothree.model.control.core.common.transfer.EntityInstanceTransfer;
023import com.echothree.model.control.core.common.transfer.EntityTimeTransfer;
024import com.echothree.model.control.core.common.transfer.MimeTypeTransfer;
025import com.echothree.model.control.core.server.control.CoreControl;
026import com.echothree.model.control.party.common.transfer.LanguageTransfer;
027import com.echothree.model.control.party.server.control.PartyControl;
028import com.echothree.model.data.core.server.entity.EntityClobAttribute;
029import com.echothree.model.data.core.server.entity.EntityInstance;
030import com.echothree.model.data.user.server.entity.UserVisit;
031import com.echothree.util.server.persistence.Session;
032import java.util.Set;
033
034public class EntityClobAttributeTransferCache
035        extends BaseCoreTransferCache<EntityClobAttribute, EntityClobAttributeTransfer> {
036
037    CoreControl coreControl = Session.getModelController(CoreControl.class);
038    PartyControl partyControl = Session.getModelController(PartyControl.class);
039
040    boolean includeClob;
041    boolean includeETag;
042    
043    /** Creates a new instance of EntityClobAttributeTransferCache */
044    public EntityClobAttributeTransferCache(UserVisit userVisit) {
045        super(userVisit);
046        
047        var options = session.getOptions();
048        if(options != null) {
049            includeClob = options.contains(CoreOptions.EntityClobAttributeIncludeClob);
050            includeETag = options.contains(CoreOptions.EntityClobAttributeIncludeETag);
051        }
052    }
053    
054    public EntityClobAttributeTransfer getEntityClobAttributeTransfer(EntityClobAttribute entityClobAttribute, EntityInstance entityInstance) {
055        EntityClobAttributeTransfer entityClobAttributeTransfer = get(entityClobAttribute);
056        
057        if(entityClobAttributeTransfer == null) {
058            EntityAttributeTransfer entityAttribute = entityInstance == null ? coreControl.getEntityAttributeTransfer(userVisit, entityClobAttribute.getEntityAttribute(), entityInstance) : null;
059            EntityInstanceTransfer entityInstanceTransfer = coreControl.getEntityInstanceTransfer(userVisit, entityClobAttribute.getEntityInstance(), false, false, false, false, false, false);
060            LanguageTransfer language = partyControl.getLanguageTransfer(userVisit, entityClobAttribute.getLanguage());
061            String clobAttribute = includeClob ? entityClobAttribute.getClobAttribute() : null;
062            MimeTypeTransfer mimeType = coreControl.getMimeTypeTransfer(userVisit, entityClobAttribute.getMimeType());
063            String eTag = null;
064            
065            if(includeETag) {
066                // Item Descriptions do not have their own EntityTime, fall back on the Item's EntityTime.
067                EntityTimeTransfer entityTimeTransfer = entityInstanceTransfer.getEntityTime();
068                Long modifiedTime = entityTimeTransfer.getUnformattedModifiedTime();
069                long maxTime = modifiedTime == null ? entityTimeTransfer.getUnformattedCreatedTime() : modifiedTime;
070                long eTagEntityId = entityClobAttribute.getPrimaryKey().getEntityId();
071                int eTagSize = entityClobAttribute.getClobAttribute().length();
072
073                // EntityId-Size-ModifiedTime
074                eTag = new StringBuilder(Long.toHexString(eTagEntityId)).append('-').append(Integer.toHexString(eTagSize)).append('-').append(Long.toHexString(maxTime)).toString();
075            }
076            
077            entityClobAttributeTransfer = new EntityClobAttributeTransfer(entityAttribute, entityInstanceTransfer, language, clobAttribute, mimeType, eTag);
078            put(entityClobAttribute, entityClobAttributeTransfer);
079        }
080        
081        return entityClobAttributeTransfer;
082    }
083    
084}