teichmann@5831: package org.dive4elements.river.artifacts.model.minfo; rrenkert@5764: rrenkert@5764: import java.util.List; rrenkert@5764: rrenkert@5764: import net.sf.ehcache.Cache; rrenkert@5764: import net.sf.ehcache.Element; rrenkert@5764: rrenkert@5764: import org.apache.log4j.Logger; rrenkert@5764: import org.hibernate.SQLQuery; rrenkert@5764: import org.hibernate.Session; rrenkert@5764: import org.hibernate.type.StandardBasicTypes; rrenkert@5764: teichmann@5831: import org.dive4elements.river.artifacts.cache.CacheFactory; teichmann@5831: import org.dive4elements.river.artifacts.model.StaticMorphoWidthCacheKey; teichmann@5831: import org.dive4elements.river.backend.SessionHolder; rrenkert@5764: rrenkert@5764: rrenkert@5764: public class MorphologicWidthFactory rrenkert@5764: { rrenkert@5764: /** Private logger to use here. */ rrenkert@5764: private static Logger log = Logger.getLogger(MorphologicWidthFactory.class); rrenkert@5764: rrenkert@5764: public static final String SQL_SELECT = rrenkert@5764: "SELECT mwv.station AS station, mwv.width AS width " + rrenkert@5764: " FROM morphologic_width mw" + rrenkert@5764: " JOIN morphologic_width_values mwv on mwv.morphologic_width_id = mw.id" + rrenkert@5764: " WHERE mw.id = :width_id"; rrenkert@5764: rrenkert@5764: private MorphologicWidthFactory() { rrenkert@5764: } rrenkert@5764: rrenkert@5764: rrenkert@5764: /** rrenkert@5764: * Get WKms for given column and wst_id, caring about the cache. rrenkert@5764: */ rrenkert@5764: public static MorphologicWidth getWidth(int width_id) { rrenkert@5764: log.debug("MorphologicWidthFactory.getWidth"); rrenkert@5764: Cache cache = CacheFactory.getCache(StaticMorphoWidthCacheKey.CACHE_NAME); rrenkert@5764: rrenkert@5764: StaticMorphoWidthCacheKey cacheKey; rrenkert@5764: rrenkert@5764: if (cache != null) { rrenkert@5764: cacheKey = new StaticMorphoWidthCacheKey(width_id); rrenkert@5764: Element element = cache.get(cacheKey); rrenkert@5764: if (element != null) { rrenkert@5764: log.debug("Got static bedheight values from cache"); rrenkert@5764: return (MorphologicWidth)element.getValue(); rrenkert@5764: } rrenkert@5764: } rrenkert@5764: else { rrenkert@5764: cacheKey = null; rrenkert@5764: } rrenkert@5764: rrenkert@5764: MorphologicWidth values = getWidthUncached(width_id); rrenkert@5764: rrenkert@5764: if (values != null && cacheKey != null) { rrenkert@5764: log.debug("Store static morphologic width values in cache."); rrenkert@5764: Element element = new Element(cacheKey, values); rrenkert@5764: cache.put(element); rrenkert@5764: } rrenkert@5764: return values; rrenkert@5764: } rrenkert@5764: rrenkert@5764: private static MorphologicWidth getWidthUncached(int width_id) { rrenkert@5764: if (log.isDebugEnabled()) { rrenkert@5764: log.debug("MorphologicWidthFactory.getWidthUncached"); rrenkert@5764: } rrenkert@5764: rrenkert@5764: Session session = SessionHolder.HOLDER.get(); rrenkert@5764: SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT) rrenkert@5764: .addScalar("station", StandardBasicTypes.DOUBLE) rrenkert@5764: .addScalar("width", StandardBasicTypes.DOUBLE); rrenkert@5764: sqlQuery.setInteger("width_id", width_id); rrenkert@5764: List results = sqlQuery.list(); rrenkert@5764: rrenkert@5764: MorphologicWidth widths = new MorphologicWidth(); rrenkert@5764: for (int i = 0; i < results.size(); i++) { rrenkert@5764: Object[] row = results.get(i); rrenkert@5764: log.debug("got station: " + (Double)row[0]); rrenkert@5764: widths.add( rrenkert@5764: (Double) row[0], rrenkert@5764: (Double) row[1]); rrenkert@5764: } rrenkert@5764: return widths; rrenkert@5764: } rrenkert@5764: } rrenkert@5764: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :