package custom; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.log4j.Logger; import com.jalios.jcms.AudienceRights; import com.jalios.jcms.Category; import com.jalios.jcms.Channel; import com.jalios.jcms.Publication; import com.jalios.jstore.Storable; import com.jalios.jstore.StoreListener; /** * This StoreListener listens to all Publication "create" and "update" operation. * It checks if at least one category is checked for each Audience Right * axes, in which case the Publication is declared audienced. * * TODO: Trigger a recheck of all Publication on AudienceRight configuration change. * => When AudienceRight is enabled or if an axe is changed after site starts, * some publication are not checked or may have an invalid check. * In this current implementation, you must restart the site if you change * enable AudienceRight or if you change audience right axes. */ public class AudiencedPublicationListener implements StoreListener { private static final Logger logger = Logger.getLogger(AudiencedPublicationListener.class); /** * Key of the attribute we store in the Publication's extraInfo to cache a Boolean * to know if the publication has been declared audienced or not. */ public static final String PUBLICATION_AUDIENCED_EXTRAINFO_KEY = "EI.isAudienced"; Channel channel = Channel.getChannel(); AudienceRights ar = AudienceRights.getInstance(); public AudiencedPublicationListener() { checkAllPublication(); } //--------------------------------------------------------------- // StoreListener methods public void handleCreate(Storable storable, boolean firstTime) { if (ar.isEnabled() && storable instanceof Publication) { checkPublication((Publication) storable); } } public void handlePrepareUpdate(Storable storable, Map attributes, boolean firstTime) { /* empty */ } public void handleCommitUpdate(Storable storable, Storable oldStorable, boolean firstTime) { if (ar.isEnabled() && storable instanceof Publication) { checkPublication((Publication) storable); } } public void handleDelete(Storable storable, boolean firstTime) { /* empty */ } //--------------------------------------------------------------- // Custom methods /** * Method to be called after store load (and therefore after audience right * has been properly loaded) to iterate on all publication and check their status. */ public void checkAllPublication() { if (!ar.isEnabled()) { return; } Set publicationSet = channel.getDataSet(Publication.class); for (Iterator it = publicationSet.iterator(); it.hasNext();) { Publication pub = (Publication) it.next(); checkPublication(pub); } } /** * Check if the given publication is audienced by checking its categories, * cache the result in the publiction's extra info. */ public void checkPublication(Publication pub) { boolean pubIsAudienced = true; // Iterate on all AudienceRight axes List axesList = ar.getCategoriesAxesList(); for (Iterator it = axesList.iterator(); it.hasNext();) { Category axeCat = (Category) it.next(); // Iterate on all publication's categories and check there is // at least one category descendant of the axe boolean pubHasAxeCat = false; Category[] categories = pub.getCategories(); for (int i = 0; categories != null && i < categories.length; i++) { Category pubCat = categories[i]; if (axeCat.containsDescendant(pubCat)) { pubHasAxeCat = true; break; } } // Publication has no selected category in this axe, end verification here. if (!pubHasAxeCat) { pubIsAudienced = false; break; } } if (logger.isDebugEnabled() && pubIsAudienced) { logger.debug(pub + " is audienced"); } pub.setExtraInfo(PUBLICATION_AUDIENCED_EXTRAINFO_KEY, Boolean.valueOf(pubIsAudienced)); } }