package org.CLIENT.everywhere.api; import java.util.ArrayList; import java.util.HashMap; import javax.jcr.Session; import javax.jcr.RepositoryException; import javax.jcr.Node; import javax.jcr.NodeIterator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.CLIENT.utils.LanguageUtil; /** * Singleton access to lookup tables used by the Article Feed servlet. * * Reference: http://stackoverflow.com/questions/16106260/thread-safe-singleton-class */ public class ArticleFeedStore { private static Logger log = LoggerFactory.getLogger(ArticleFeedStore.class); private HashMap selectorsToPaths; private HashMap selectorsToPlaylistIds; private HashMap pathsToSelectors; private ArrayList enPaths; private ArrayList esPaths; private ArrayList enSelectorsAndSlugs; private ArrayList esSelectorsAndSlugs; private static class Singleton { static final ArticleFeedStore INSTANCE = new ArticleFeedStore(); } private ArticleFeedStore() { ; } /** * @return The singleton instance of this class. */ public static ArticleFeedStore getInstance() { return Singleton.INSTANCE; } private void load(Session session) throws RepositoryException { log.debug("reloading topic store; this.hashCode: " + this.hashCode()); this.selectorsToPaths = new HashMap(); this.selectorsToPlaylistIds = new HashMap(); this.pathsToSelectors = new HashMap(); this.enPaths = new ArrayList(); this.esPaths = new ArrayList(); this.enSelectorsAndSlugs = new ArrayList(); this.esSelectorsAndSlugs = new ArrayList(); Node datashareNode = (Node) session.getItem(ArticleUtil.DATASHARE_LOCATION); EverywhereDataShareUtil shareUtil = new EverywhereDataShareUtil(datashareNode, ArticleUtil.DATASHARE_SHARE); NodeIterator nodeIterator = shareUtil.sortByOrder(shareUtil.getRecords()); while (nodeIterator.hasNext()) { Node recordNode = nodeIterator.nextNode(); Node parentNode = recordNode.getParent(); Node keyNode = parentNode.getParent(); if (!parentNode.hasProperty("cq:lastReplicationAction")) { continue; } String activationStatus = parentNode.getProperty("cq:lastReplicationAction").getString(); if (!activationStatus.equals("Activate")) { continue; } if (recordNode.hasProperty(ArticleUtil.DATASHARE_RECORD_PROPERTY_PLAYLIST_ID)) { //optional property javax.jcr.Property playlistProperty = recordNode.getProperty(ArticleUtil.DATASHARE_RECORD_PROPERTY_PLAYLIST_ID); String playlistId = playlistProperty.getString(); this.selectorsToPlaylistIds.put(keyNode.getName(), playlistId); } if (!recordNode.hasProperty(ArticleUtil.DATASHARE_RECORD_PROPERTY_PATH)) { continue; } javax.jcr.Property property = recordNode.getProperty(ArticleUtil.DATASHARE_RECORD_PROPERTY_PATH); String path = property.getString(); String slug = ArticleUtil.makeTopicSlug(keyNode); String [] selectorAndSlug = {keyNode.getName(), slug}; this.selectorsToPaths.put(keyNode.getName(), path); this.pathsToSelectors.put(path, keyNode.getName()); if (path.startsWith("/" + LanguageUtil.ENGLISH)) { this.enPaths.add(path); this.enSelectorsAndSlugs.add(selectorAndSlug); } else if (path.startsWith("/" + LanguageUtil.SPANISH)) { this.esPaths.add(path); this.esSelectorsAndSlugs.add(selectorAndSlug); } } } /** * Reloads the lookup tables. * @param session * @throws RepositoryException */ public synchronized void reload(Session session) throws RepositoryException { load(session); } /** * @return The mapping table of selectors to paths. */ public HashMap getSelectorsToPaths() { return this.selectorsToPaths; } /** * @return The mapping table of selectors to playlist IDs. */ public HashMap getSelectorsToPlaylistIds() { return this.selectorsToPlaylistIds; } /** * @return The mapping table of paths to selectors. */ public HashMap getPathsToSelectors() { return this.pathsToSelectors; } /** * @return The list of paths that start with /en, in the order specified by the "order" attribute of the datashare record. */ public ArrayList getEnPaths() { return this.enPaths; } /** * @return The list of paths that start with /es, in the order specified by the "order" attribute of the datashare record. */ public ArrayList getEsPaths() { return this.esPaths; } /** * @return For paths that start with /en, a list of their respective selectors and slugs, * in the order specified by the "order" attribute of the datashare record. */ public ArrayList getEnSelectorsAndSlugs() { return this.enSelectorsAndSlugs; } /** * @return For paths that start with /es, a list of their respective selectors and slugs, * in the order specified by the "order" attribute of the datashare record. */ public ArrayList getEsSelectorsAndSlugs() { return this.esSelectorsAndSlugs; } }