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.minfo; raimund@3751: raimund@3751: import net.sf.ehcache.Cache; raimund@3751: import net.sf.ehcache.Element; raimund@3751: raimund@3751: import org.apache.log4j.Logger; raimund@3751: import org.hibernate.Session; raimund@3751: teichmann@5831: import org.dive4elements.river.artifacts.cache.CacheFactory; teichmann@5831: import org.dive4elements.river.backend.SedDBSessionHolder; raimund@3751: raimund@3751: public class BedloadOverviewFactory { raimund@3751: raimund@3751: private static Logger log = Logger.getLogger(BedloadOverviewFactory.class); raimund@3751: raimund@3751: public static final String CACHE_NAME = "sq-overviews"; raimund@3751: raimund@3751: private BedloadOverviewFactory() { raimund@3751: } raimund@3751: raimund@3751: raimund@3751: public static BedloadOverview getOverview(String river) { raimund@3751: raimund@3751: boolean debug = log.isDebugEnabled(); raimund@3751: raimund@3751: if (debug) { raimund@3751: log.debug( raimund@3751: "Looking for bedload overview for river '" + river + "'"); raimund@3751: } raimund@3751: raimund@3751: Cache cache = CacheFactory.getCache(CACHE_NAME); raimund@3751: raimund@3751: if (cache == null) { raimund@3751: if (debug) { raimund@3751: log.debug("Cache not configured."); raimund@3751: } raimund@3751: return getUncached(river); raimund@3751: } raimund@3751: raimund@3751: String key = "bedload-over-" + river; raimund@3751: raimund@3751: Element element = cache.get(key); raimund@3751: raimund@3751: if (element != null) { raimund@3751: if (debug) { raimund@3751: log.debug("Overview found in cache"); raimund@3751: } raimund@3751: return (BedloadOverview)element.getValue(); raimund@3751: } raimund@3751: raimund@3751: BedloadOverview overview = getUncached(river); raimund@3751: raimund@3751: if (overview != null) { raimund@3751: if (debug) { raimund@3751: log.debug("Store overview in cache."); raimund@3751: } raimund@3751: cache.put(new Element(key, overview)); raimund@3751: } raimund@3751: raimund@3751: return overview; raimund@3751: } raimund@3751: raimund@3751: public static BedloadOverview getUncached(String river) { raimund@3751: BedloadOverview overview = new BedloadOverview(river); raimund@3751: raimund@3751: Session session = SedDBSessionHolder.HOLDER.get(); raimund@3751: raimund@3751: return overview.load(session) ? overview : null; raimund@3751: } raimund@3751: }