teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5863: * Software engineering by Intevation GmbH teichmann@5863: * teichmann@5863: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5863: * documentation coming with Dive4Elements River for details. teichmann@5863: */ teichmann@5863: teichmann@5831: package org.dive4elements.river.artifacts.model; sascha@2561: teichmann@5831: import org.dive4elements.river.artifacts.cache.CacheFactory; sascha@2561: teichmann@5831: import org.dive4elements.river.backend.SessionHolder; sascha@2561: sascha@2561: import net.sf.ehcache.Cache; sascha@2561: import net.sf.ehcache.Element; sascha@2561: sascha@2561: import org.apache.log4j.Logger; sascha@2561: sascha@2561: import org.hibernate.Session; sascha@2561: sascha@2561: public class FixingsOverviewFactory sascha@2561: { sascha@2561: private static Logger log = Logger.getLogger(FixingsOverviewFactory.class); sascha@2561: sascha@2561: public static final String CACHE_NAME = "fixings-overviews"; sascha@2561: sascha@2561: private FixingsOverviewFactory() { sascha@2561: } sascha@2561: sascha@2561: sascha@2561: public static FixingsOverview getOverview(String river) { sascha@2561: sascha@2622: boolean debug = log.isDebugEnabled(); sascha@2622: sascha@2622: if (debug) { sascha@2622: log.debug( sascha@2622: "Looking for fixings overview for river '" + river + "'"); sascha@2622: } sascha@2561: sascha@2561: Cache cache = CacheFactory.getCache(CACHE_NAME); sascha@2561: sascha@2561: if (cache == null) { sascha@2622: if (debug) { sascha@2622: log.debug("Cache not configured."); sascha@2622: } sascha@2561: return getUncached(river); sascha@2561: } sascha@2561: sascha@2561: String key = "fix-over-" + river; sascha@2561: sascha@2561: Element element = cache.get(key); sascha@2561: sascha@2561: if (element != null) { sascha@2622: if (debug) { sascha@2622: log.debug("Overview found in cache"); sascha@2622: } sascha@2561: return (FixingsOverview)element.getValue(); sascha@2561: } sascha@2561: sascha@2561: FixingsOverview overview = getUncached(river); sascha@2561: sascha@2561: if (overview != null) { sascha@2622: if (debug) { sascha@2622: log.debug("Store overview in cache."); sascha@2622: } sascha@2622: cache.put(new Element(key, overview)); sascha@2561: } sascha@2561: sascha@2561: return overview; sascha@2561: } sascha@2561: sascha@2561: public static FixingsOverview getUncached(String river) { sascha@2561: FixingsOverview overview = new FixingsOverview(river); sascha@2561: sascha@2561: Session session = SessionHolder.HOLDER.get(); sascha@2561: sascha@2561: return overview.load(session) ? overview : null; sascha@2561: } sascha@2561: } sascha@2561: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :