com.alogient.cameleon.sdk.content.service.impl.CachingPageServiceImpl Maven / Gradle / Ivy
The newest version!
package com.alogient.cameleon.sdk.content.service.impl;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alogient.cameleon.sdk.content.model.AbstractCameleonModel;
import com.alogient.cameleon.sdk.content.service.PageService;
import com.alogient.cameleon.sdk.content.service.SiteService;
import com.alogient.cameleon.sdk.content.util.exception.PageNotFoundException;
import com.alogient.cameleon.sdk.content.vo.CultureVo;
public class CachingPageServiceImpl implements PageService {
/**
* Logger for this class
*/
private static transient Logger log = LoggerFactory.getLogger(CachingPageServiceImpl.class);
/**
* The real page service
*/
private PageService pageService;
/**
* The site service
*/
private SiteService siteService;
/**
* @param pageService the pageService to set
*/
public void setPageService(PageService pageService) {
this.pageService = pageService;
}
/**
* @param siteService the siteService to set
*/
public void setSiteService(SiteService siteService) {
this.siteService = siteService;
}
/**
* The last known modidification date
*/
private Date lastModificationDate;
/**
* A cached item
*
* @author alord
*/
private class PageCache implements Serializable {
/**
* Serial version unique identifier
*/
private static final long serialVersionUID = -3036484427527950824L;
private AbstractCameleonModel model;
private CultureVo culture;
}
private List liveCachedModels;
private List previewCachedModels;
/**
* Clears the cache
*/
private synchronized void clearCache() {
liveCachedModels = new ArrayList();
previewCachedModels = new ArrayList();
}
private synchronized void putInCache(CultureVo culture, boolean isLiveMode, AbstractCameleonModel model) {
PageCache item = new PageCache();
item.model = model;
item.culture = culture;
if (isLiveMode) {
liveCachedModels.add(item);
} else {
previewCachedModels.add(item);
}
}
@SuppressWarnings("unchecked")
private synchronized T getFromCacheByPageId(Integer pageId, boolean isLiveMode, Class modelClass) {
List cache = isLiveMode ? liveCachedModels : previewCachedModels;
for (PageCache cachedModel : cache) {
if (modelClass.isInstance(cachedModel.model)) {
if (pageId.equals(cachedModel.model.getPageId())) {
return (T) cachedModel.model;
}
}
}
return null;
}
@SuppressWarnings("unchecked")
private synchronized T getFromCacheByNavId(CultureVo culture, Integer navId, boolean isLiveMode, Class modelClass) {
List cache = isLiveMode ? liveCachedModels : previewCachedModels;
for (PageCache cachedModel : cache) {
if (modelClass.isInstance(cachedModel.model)) {
if (navId.equals(cachedModel.model.getNavId()) && cachedModel.culture != null && cachedModel.culture.getCultureCode().equals(culture.getCultureCode())) {
return (T) cachedModel.model;
}
}
}
return null;
}
private void checkCacheFreshness() {
Date trueLastModificationDate = siteService.getSiteLastModificationDate();
if (lastModificationDate == null || trueLastModificationDate.getTime() > lastModificationDate.getTime()) {
log.info("Cache is stale, clearing");
clearCache();
lastModificationDate = trueLastModificationDate;
}
}
/**
* {@inheritDoc}
*/
@Override
public boolean exist(CultureVo cultureVo, Integer pageId, Boolean isLiveMode) {
log.debug("This method is not cached");
return pageService.exist(cultureVo, pageId, isLiveMode);
}
/**
* {@inheritDoc}
*/
@Override
public T getPage(Integer pageId, Boolean isLiveMode, Boolean isSecurityAware, Class modelClass) throws PageNotFoundException {
log.debug("This method is cached");
checkCacheFreshness();
T model = getFromCacheByPageId(pageId, isLiveMode, modelClass);
if (model != null) {
return model;
}
model = pageService.getPage(pageId, isLiveMode, isSecurityAware, modelClass);
putInCache(null, isLiveMode, model);
return model;
}
/**
* {@inheritDoc}
*/
@Override
public T getPage(CultureVo cultureVo, Integer navId, Boolean isLiveMode, Boolean isSecurityAware, Class modelClass)
throws PageNotFoundException {
log.debug("This method is cached");
checkCacheFreshness();
T model = getFromCacheByNavId(cultureVo, navId, isLiveMode, modelClass);
if (model != null) {
return model;
}
model = pageService.getPage(cultureVo, navId, isLiveMode, isSecurityAware, modelClass);
putInCache(cultureVo, isLiveMode, model);
return model;
}
/**
* {@inheritDoc}
*/
@Override
public List getPages(CultureVo cultureVo, Boolean isLiveMode, Boolean isSecurityAware, Class modelClass) {
log.debug("This method is not cached");
return pageService.getPages(cultureVo, isLiveMode, isSecurityAware, modelClass);
}
/**
* {@inheritDoc}
*/
@Override
public List getPages(CultureVo cultureVo, String multiAssociationNavIds, Boolean isLiveMode, Boolean isSecurityAware, Class modelClass) {
log.debug("This method is not cached");
return pageService.getPages(cultureVo, multiAssociationNavIds, isLiveMode, isSecurityAware, modelClass);
}
/**
* {@inheritDoc}
*/
@Override
public List getSubPages(Integer pageId, Boolean isLiveMode, Boolean isSecurityAware, Class modelClass) throws PageNotFoundException {
log.debug("This method is not cached");
return pageService.getSubPages(pageId, isLiveMode, isSecurityAware, modelClass);
}
/**
* {@inheritDoc}
*/
@Override
public List getSubPages(Integer pageId, Boolean isLiveMode, Boolean isSecurityAware, Class extends AbstractCameleonModel>... modelClasses)
throws PageNotFoundException {
log.debug("This method is not cached");
return pageService.getSubPages(pageId, isLiveMode, isSecurityAware, modelClasses);
}
/**
* {@inheritDoc}
*/
@Override
public List getSubPages(CultureVo cultureVo, Integer navId, Boolean isLiveMode, Boolean isSecurityAware, Class modelClass)
throws PageNotFoundException {
log.debug("This method is not cached");
return pageService.getSubPages(cultureVo, navId, isLiveMode, isSecurityAware, modelClass);
}
/**
* {@inheritDoc}
*/
@Override
public List getSubPages(Class modelClass, Integer pageId, Boolean isLiveMode, Boolean isSecurityAware,
Class extends T>... modelClasses) throws PageNotFoundException {
log.debug("This method is not cached");
return pageService.getSubPages(modelClass, pageId, isLiveMode, isSecurityAware, modelClasses);
}
/**
* {@inheritDoc}
*/
@Override
public List getSubPages(CultureVo cultureVo, Integer navId, Boolean isLiveMode, Boolean isSecurityAware,
Class extends AbstractCameleonModel>... modelClasses) throws PageNotFoundException {
log.debug("This method is not cached");
return pageService.getSubPages(cultureVo, navId, isLiveMode, isSecurityAware, modelClasses);
}
/**
* {@inheritDoc}
*/
@Override
public T getFirstTemplatePage(String templateName, CultureVo cultureVo, Boolean isLiveMode, Boolean isSecurityAware, Class modelClass)
throws PageNotFoundException {
log.debug("This method is not cached");
return pageService.getFirstTemplatePage(templateName, cultureVo, isLiveMode, isSecurityAware, modelClass);
}
/**
* {@inheritDoc}
*/
@Override
public Integer createPage(Integer parentNavId, String templateName, CultureVo cultureVo, String pageName, String author) {
log.debug("This method is not cached");
return pageService.createPage(parentNavId, templateName, cultureVo, pageName, author);
}
/**
* {@inheritDoc}
*/
@Override
public void updatePage(Integer navId, CultureVo cultureVo, Map values, String pageName, String author) {
log.debug("This method is not cached");
pageService.updatePage(navId, cultureVo, values, pageName, author);
clearCache();
}
/**
* {@inheritDoc}
*/
@Override
public void approvePage(Integer navId, CultureVo cultureVo, String author) {
log.debug("This method is not cached");
pageService.approvePage(navId, cultureVo, author);
clearCache();
}
/**
* {@inheritDoc}
*/
@Override
public void changePageVisibility(Integer navId, CultureVo cultureVo, boolean isLive, boolean isVisible, String author) {
log.debug("This method is not cached");
pageService.changePageVisibility(navId, cultureVo, isLive, isVisible, author);
clearCache();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy