Skip to content

Instantly share code, notes, and snippets.

@chrismrgn
Created July 14, 2016 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrismrgn/53ca833a7d65d358c497fc40ee1749db to your computer and use it in GitHub Desktop.
Save chrismrgn/53ca833a7d65d358c497fc40ee1749db to your computer and use it in GitHub Desktop.
Factory interface and class to integrate the CustomDataBind (Sitemap) functionality into DD4T 2 Java
public interface SitemapFactory extends Factory {
<T extends Sitemap> T findSitemapByUrl(String url, int publicationId, final Class<? extends T> aClass) throws FactoryException;
<T extends Sitemap> T deserialize(String source, final Class<? extends T> aClass) throws FactoryException;
}
public class SitemapFactoryImpl extends BaseFactory implements SitemapFactory {
private static final Logger LOG = LoggerFactory.getLogger(PageFactoryImpl.class);
private static final SitemapFactoryImpl INSTANCE = new SitemapFactoryImpl();
protected SitemapFactoryImpl() {
LOG.debug("Create new instance");
}
public static SitemapFactoryImpl getInstance () {
return INSTANCE;
}
@Resource
protected PageProvider pageProvider;
@Override
public <T extends Sitemap> T findSitemapByUrl(String url, int publicationId, final Class<? extends T> aClass) throws FactoryException {
LOG.debug("Enter findPageByUrl with url: {} and publicationId: {}", url, publicationId);
String cacheKey = publicationId + "-" + url.toLowerCase();
CacheElement<T> cacheElement = cacheProvider.loadPayloadFromLocalCache(cacheKey);
T sitemap = null;
if (cacheElement.isExpired() || cacheElement.getPayload() == null) {
//noinspection SynchronizationOnLocalVariableOrMethodParameter
synchronized (cacheElement) {
if (cacheElement.isExpired() || cacheElement.getPayload() == null) {
cacheElement.setExpired(false);
String pageSource;
ProviderResultItem<String> resultItem;
resultItem = pageProvider.getPageByURL(url, publicationId);
pageSource = resultItem.getSourceContent();
if (StringUtils.isEmpty(pageSource)) {
cacheElement.setPayload(null);
cacheElement.setExpired(true);
cacheProvider.storeInItemCache(cacheKey, cacheElement);
throw new ItemNotFoundException("Page with url: " + url + " not found.");
}
sitemap = deserialize(pageSource, aClass);
// TODO: replace DCPs here with the real DCPs?
//TODO: Add these properties
//navigation.setLastPublishedDate(resultItem.getLastPublishDate());
//navigation.setRevisionDate(resultItem.getRevisionDate());
//final TCMURI tcmUri = new TCMURI(navigation.getCurrentPageId());
LOG.debug("Running pre caching processors");
cacheElement.setPayload(sitemap);
//TODO: not currently passing a dependant item, put page id
//TODO: navigation make inherit Item
cacheProvider.storeInItemCache(cacheKey, cacheElement, publicationId, 800);
LOG.debug("Added navigation with uri: {} and publicationId: {} to cache", url, publicationId);
} else {
LOG.debug("Return a navigation with url: {} and publicationId: {} from cache", url, publicationId);
sitemap = cacheElement.getPayload();
}
}
} else {
LOG.debug("Return page with url: {} and publicationId: {} from cache", url, publicationId);
sitemap = cacheElement.getPayload();
}
executePostCacheProcessors(sitemap);
return sitemap;
}
@Override
public <T extends Sitemap> T deserialize(String source, final Class<? extends T> aClass) throws FactoryException {
return CustomDataBindFactory.buildSitemap(source, aClass);
}
private void executePostCacheProcessors (final Sitemap navigation) {
//TODO: Review this section and implement accordingly
//if (navigation != null) {
// LOG.debug("Running Post caching Processors");
// try {
// this.executeProcessors(navigation, RunPhase.AFTER_CACHING, getRequestContext());
// } catch (ProcessorException e) {
// LOG.error(e.getLocalizedMessage(), e);
// }
//}
}
public PageProvider getPageProvider () {
return pageProvider;
}
public void setPageProvider (final PageProvider pageProvider) {
this.pageProvider = pageProvider;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment