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; felix@1722: felix@1722: import java.util.List; felix@1722: felix@1722: import net.sf.ehcache.Cache; felix@1722: import net.sf.ehcache.Element; felix@1722: felix@1722: import org.apache.log4j.Logger; felix@1722: felix@1722: import org.hibernate.Session; felix@1722: felix@1722: import org.hibernate.SQLQuery; felix@1722: import org.hibernate.type.StandardBasicTypes; felix@1722: teichmann@5831: import org.dive4elements.river.artifacts.cache.CacheFactory; felix@1722: teichmann@5831: import org.dive4elements.river.backend.SessionHolder; felix@1722: felix@3442: felix@1722: /** sascha@3076: * Factory to access ready-made WKms for other (than computed) 'kinds' of felix@1722: * WST-data. felix@1722: */ felix@1722: public class WKmsFactory felix@1722: { felix@3442: /** Private logger to use here. */ felix@1722: private static Logger log = Logger.getLogger(WKmsFactory.class); felix@1722: felix@1722: /** Query to get km and ws for wst_id and column_pos. */ felix@1722: public static final String SQL_SELECT_WS = felix@1722: "SELECT km, w FROM wst_w_values " + felix@1722: "WHERE wst_id = :wst_id AND column_pos = :column_pos"; felix@1722: felix@1729: /** Query to get name for wst_id and column_pos. */ felix@1726: public static final String SQL_SELECT_NAME = felix@1726: "SELECT name " + felix@1726: "FROM wst_columns "+ felix@1726: "WHERE wst_id = :wst_id AND position = :column_pos"; felix@1726: felix@1892: /** Query to get name (description) for wst_id. */ felix@1892: public static final String SQL_SELECT_WST_NAME = felix@1892: "SELECT description from wsts "+ felix@1897: "WHERE id = :wst_id"; felix@1892: felix@1722: felix@1722: private WKmsFactory() { felix@1722: } felix@1722: felix@1722: felix@1722: /** felix@1722: * Get WKms for given column and wst_id, caring about the cache. felix@1722: */ felix@1726: public static WKms getWKms(int column, int wst_id) { felix@1722: log.debug("WKmsFactory.getWKms"); felix@1722: Cache cache = CacheFactory.getCache(StaticWKmsCacheKey.CACHE_NAME); felix@1722: felix@1722: StaticWKmsCacheKey cacheKey; felix@1722: felix@1722: if (cache != null) { felix@1722: cacheKey = new StaticWKmsCacheKey(wst_id, column); felix@1722: Element element = cache.get(cacheKey); felix@1722: if (element != null) { felix@1722: log.debug("Got static wst values from cache"); felix@1722: return (WKms)element.getValue(); felix@1722: } felix@1722: } felix@1722: else { felix@1722: cacheKey = null; felix@1722: } felix@1722: felix@1726: WKms values = getWKmsUncached(column, wst_id); felix@1722: felix@1722: if (values != null && cacheKey != null) { felix@1722: log.debug("Store static wst values in cache."); felix@1722: Element element = new Element(cacheKey, values); felix@1722: cache.put(element); felix@1722: } felix@1722: return values; felix@1722: } felix@1722: felix@1892: /** Get name for a WKms. */ felix@1893: public static String getWKmsName(int wst_id) { felix@1892: log.debug("WKmsFactory.getWKmsName wst_id/" + wst_id); felix@1892: felix@1892: String name = null; felix@1892: Session session = SessionHolder.HOLDER.get(); felix@1892: felix@1897: SQLQuery nameQuery = session.createSQLQuery(SQL_SELECT_WST_NAME) felix@1897: .addScalar("description", StandardBasicTypes.STRING); felix@1892: nameQuery.setInteger("wst_id", wst_id); felix@1892: felix@1892: List names = nameQuery.list(); felix@1892: if (names.size() >= 1) { felix@1892: name = names.get(0); felix@1892: } felix@1892: felix@1892: return name; felix@1892: } felix@1722: felix@1729: /** Get name for a WKms. */ felix@1729: public static String getWKmsName(int column, int wst_id) { felix@1729: log.debug("WKmsFactory.getWKmsName c/" + column + ", wst_id/" + wst_id); felix@1729: felix@1729: String name = null; felix@1722: Session session = SessionHolder.HOLDER.get(); felix@1722: felix@1726: SQLQuery nameQuery = session.createSQLQuery(SQL_SELECT_NAME) felix@1726: .addScalar("name", StandardBasicTypes.STRING); felix@1726: nameQuery.setInteger("wst_id", wst_id); felix@1726: nameQuery.setInteger("column_pos", column); felix@1726: felix@1726: List names = nameQuery.list(); felix@1726: if (names.size() >= 1) { felix@1726: name = names.get(0); felix@1726: } felix@1726: felix@1729: return name; felix@1729: } felix@1726: felix@1729: felix@1729: /** felix@1729: * Get WKms from db. felix@1729: * @param column the position columns value felix@1729: * @param wst_id database id of the wst felix@1729: * @return according WKms. felix@1729: */ felix@1729: public static WKms getWKmsUncached(int column, int wst_id) { sascha@1796: sascha@1796: if (log.isDebugEnabled()) { sascha@1796: log.debug("WKmsFactory.getWKmsUncached c/" + column + ", wst_id/" + wst_id); sascha@1796: } felix@1729: felix@1729: WKmsImpl wkms = new WKmsImpl(getWKmsName(column, wst_id)); felix@1729: felix@1729: Session session = SessionHolder.HOLDER.get(); felix@1722: SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_WS) felix@1722: .addScalar("km", StandardBasicTypes.DOUBLE) felix@1722: .addScalar("w", StandardBasicTypes.DOUBLE); felix@1722: sqlQuery.setInteger("wst_id", wst_id); felix@1722: sqlQuery.setInteger("column_pos", column); felix@1722: felix@1722: List results = sqlQuery.list(); felix@1722: sascha@1796: for (int i = 0, N = results.size(); i < N; i++) { felix@1722: Object[] row = results.get(i); felix@1722: wkms.add((Double) row[0], (Double) row[1]); felix@1722: } felix@1722: felix@1722: return wkms; felix@1722: } felix@1722: } felix@1722: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :