1 /* 2 * Copyright 2010 FatWire Corporation. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.fatwire.gst.foundation.tagging; 17 18 import java.util.ArrayList; 19 import java.util.Collection; 20 import java.util.List; 21 22 import COM.FutureTense.Interfaces.ICS; 23 24 import com.fatwire.assetapi.data.AssetId; 25 import com.fatwire.realtime.PageCacheUpdaterImpl; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 import static com.fatwire.gst.foundation.tagging.TagUtils.convertTagToCacheDepString; 31 32 /** 33 * RealTime publishing includes an API entitled RealTime CacheUpdater. We will 34 * override the default com.fatwire.realtime.PageCacheUpdaterImpl to override 35 * the beforeSelect() method. The flush and regen keys will be extended, to 36 * automatically include all pagelets containing the GSTTag attribute value. 37 * This ensures that even though parent has not changed, pagelets that reference 38 * it are automatically flushed. This ensures that by simply tagging an asset, 39 * it automatically and instantly appears in pages that render it. 40 * <p/> 41 * The GSTTagRegistry table is read for the specific assets before the new 42 * values are inserted. This is to make sure that pagelets are also flushed with 43 * the old tag values for the cases where the tag is deleted or the values have 44 * changed. Implementation examples can be found in the guide Customizing 45 * RealTime Publishing Cache Management. 46 * 47 * @author Tony Field 48 * @since Jul 30, 2010 49 */ 50 public final class TaggedAssetRealtimeCacheUpdater extends PageCacheUpdaterImpl { 51 52 private static final Log LOG = LogFactory.getLog("com.fatwire.gst.foundation.tagging"); 53 54 @Override 55 protected void beforeSelect(ICS ics, Collection<String> invalKeys, Collection<String> regenKeys, 56 Collection<AssetId> assetIds) { 57 AssetTaggingService svc = AssetTaggingServiceFactory.getService(ics); 58 List<AssetId> tagged = new ArrayList<AssetId>(); 59 for (AssetId id : assetIds) { 60 if (svc.isTagged(id)) { 61 tagged.add(id); 62 } 63 } 64 for (Tag tag : svc.getTags(tagged)) { 65 if (LOG.isDebugEnabled()) 66 LOG.debug("AssetTag found in beforeSelect: " + tag 67 + ". Adding this to the list of compositional dependencies to be flushed."); 68 String sTag = convertTagToCacheDepString(tag); 69 invalKeys.add(sTag); 70 } 71 super.beforeSelect(ics, invalKeys, regenKeys, assetIds); 72 } 73 }