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@1825: felix@1825: import java.util.List; felix@1825: felix@1825: import net.sf.ehcache.Cache; felix@1825: import net.sf.ehcache.Element; felix@1825: felix@1825: import org.apache.log4j.Logger; felix@1825: felix@1825: import org.hibernate.Session; felix@1825: felix@1825: import org.hibernate.SQLQuery; felix@1825: import org.hibernate.type.StandardBasicTypes; felix@1825: teichmann@5831: import org.dive4elements.river.artifacts.cache.CacheFactory; felix@1825: teichmann@5831: import org.dive4elements.river.backend.SessionHolder; felix@1825: felix@1825: /** sascha@3076: * Factory to access ready-made WQKms for other (than computed) 'kinds' of felix@1825: * WST-data. felix@1825: */ felix@1825: public class WQKmsFactory felix@1825: { felix@1825: private static Logger log = Logger.getLogger(WQKmsFactory.class); felix@1825: felix@1825: /** Query to get km and wqs for wst_id and column_pos. */ felix@1825: public static final String SQL_SELECT_WQS = felix@1825: "SELECT position, w, q FROM wst_value_table " + felix@1825: "WHERE wst_id = :wst_id AND column_pos = :column_pos"; felix@1825: felix@5726: /** Get wst_id and position from wst_columns. */ felix@5726: public static final String SQL_SELECT_COLUMN = felix@5726: "SELECT wst_id, position FROM wst_columns WHERE id = :column_id"; felix@5726: felix@1825: /** Query to get name for wst_id and column_pos. */ felix@1825: public static final String SQL_SELECT_NAME = felix@1825: "SELECT name " + felix@1825: "FROM wst_columns "+ felix@5727: "WHERE id = :column_id"; felix@1825: felix@1825: felix@1825: /** Hidden constructor, use static methods instead. */ felix@1825: private WQKmsFactory() { felix@1825: } felix@1825: felix@1825: felix@1825: /** felix@5727: * Get WKms for given column (pos) and wst_id, caring about the cache. felix@1825: */ felix@5728: public static WQKms getWQKms(int columnPos, int wst_id) { felix@1825: log.debug("WQKmsFactory.getWQKms"); felix@1825: Cache cache = CacheFactory.getCache(StaticWQKmsCacheKey.CACHE_NAME); felix@1825: felix@1825: StaticWQKmsCacheKey cacheKey; felix@1825: felix@1825: if (cache != null) { felix@5728: cacheKey = new StaticWQKmsCacheKey(wst_id, columnPos); felix@1825: Element element = cache.get(cacheKey); felix@1825: if (element != null) { felix@1825: log.debug("Got static wst values from cache"); felix@1825: return (WQKms)element.getValue(); felix@1825: } felix@1825: } felix@1825: else { felix@1825: cacheKey = null; felix@1825: } felix@1825: felix@5728: WQKms values = getWQKmsUncached(columnPos, wst_id); felix@5728: felix@5728: if (values != null && cacheKey != null) { felix@5728: log.debug("Store static wst values in cache."); felix@5728: Element element = new Element(cacheKey, values); felix@5728: cache.put(element); felix@5728: } felix@5728: return values; felix@5728: } felix@5728: felix@5728: /** felix@5728: * Get WKms for given column (id), caring about the cache. felix@5728: */ felix@5728: public static WQKms getWQKmsCID(int columnID) { felix@5728: log.debug("WQKmsFactory.getWQKms"); felix@5728: Cache cache = CacheFactory.getCache(StaticWQKmsCacheKey.CACHE_NAME); felix@5728: felix@5728: StaticWQKmsCacheKey cacheKey; felix@5728: felix@5728: if (cache != null) { felix@5728: cacheKey = new StaticWQKmsCacheKey(-columnID, -columnID); felix@5728: Element element = cache.get(cacheKey); felix@5728: if (element != null) { felix@5728: log.debug("Got static wst values from cache"); felix@5728: return (WQKms)element.getValue(); felix@5728: } felix@5728: } felix@5728: else { felix@5728: cacheKey = null; felix@5728: } felix@5728: felix@5728: int[] cInfo = getColumn(columnID); felix@5728: if (cInfo == null) return null; felix@5728: WQKms values = getWQKmsUncached(cInfo[1], cInfo[0]); felix@5728: felix@1825: felix@1825: if (values != null && cacheKey != null) { felix@1825: log.debug("Store static wst values in cache."); felix@1825: Element element = new Element(cacheKey, values); felix@1825: cache.put(element); felix@1825: } felix@1825: return values; felix@1825: } felix@1825: felix@1825: felix@1825: /** felix@1825: * Get WQKms from db. felix@1825: * @param column the position columns value felix@1825: * @param wst_id database id of the wst felix@1825: * @return respective WQKms. felix@1825: */ felix@1825: public static WQKms getWQKmsUncached(int column, int wst_id) { felix@1825: felix@1825: if (log.isDebugEnabled()) { felix@1825: log.debug("WQKmsFactory.getWQKmsUncached, column " felix@1825: + column + ", wst_id " + wst_id); felix@1825: } felix@1825: felix@1825: WQKms wqkms = new WQKms(WKmsFactory.getWKmsName(column, wst_id)); felix@1825: felix@1825: Session session = SessionHolder.HOLDER.get(); felix@1825: SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_WQS) felix@1825: .addScalar("position", StandardBasicTypes.DOUBLE) felix@1825: .addScalar("w", StandardBasicTypes.DOUBLE) felix@1825: .addScalar("q", StandardBasicTypes.DOUBLE); felix@1825: sqlQuery.setInteger("wst_id", wst_id); felix@1825: sqlQuery.setInteger("column_pos", column); felix@1825: felix@1825: List results = sqlQuery.list(); felix@1825: felix@1825: for (int i = 0, N = results.size(); i < N; i++) { felix@1825: Object[] row = results.get(i); felix@1825: // add(w, q, km) felix@1825: wqkms.add((Double) row[1], (Double) row[2], (Double) row[0]); felix@1825: } felix@1825: felix@1825: return wqkms; felix@1825: } felix@5726: felix@5726: felix@5726: /** felix@5726: * Get WQKms from db. felix@5726: * @param columnID the columns database id value felix@5726: * @param wst_id database id of the wst felix@5726: * @return respective WQKms. felix@5726: */ felix@5726: public static int[] getColumn(int columnID) { felix@5726: felix@5726: if (log.isDebugEnabled()) { felix@5726: log.debug("WQKmsFactory.getColumn, columnID " felix@5726: + columnID); felix@5726: } felix@5726: felix@5726: Session session = SessionHolder.HOLDER.get(); felix@5726: felix@5726: SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_COLUMN) felix@5726: .addScalar("wst_id", StandardBasicTypes.INTEGER) felix@5726: .addScalar("position", StandardBasicTypes.INTEGER); felix@5726: sqlQuery.setInteger("column_id", columnID); felix@5726: felix@5726: List results = sqlQuery.list(); felix@5726: felix@5726: for (int i = 0, N = results.size(); i < N; i++) { felix@5726: Object[] row = results.get(i); felix@5726: return new int[] {(Integer)row[0], (Integer)row[1]}; felix@5726: } felix@5726: felix@5726: return null; felix@5726: } felix@5726: felix@5726: felix@5726: /** Get name for a WKms. */ felix@5726: public static String getWQKmsName(int columnID) { felix@5726: log.debug("WQKmsFactory.getWQKmsName c/" + columnID); felix@5726: felix@5726: String name = null; felix@5726: Session session = SessionHolder.HOLDER.get(); felix@5726: felix@5726: SQLQuery nameQuery = session.createSQLQuery(SQL_SELECT_NAME) felix@5726: .addScalar("name", StandardBasicTypes.STRING); felix@5726: nameQuery.setInteger("column_id", columnID); felix@5726: felix@5726: List names = nameQuery.list(); felix@5726: if (names.size() >= 1) { felix@5726: name = names.get(0); felix@5726: } felix@5726: felix@5726: return name; felix@5726: } felix@1825: } felix@1825: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :