teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5863: * Software engineering by Intevation GmbH teichmann@5863: * teichmann@5994: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5994: * 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: { teichmann@8202: /** Private log 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@7686: /** Query to get name and kind for wst_id and column_pos. */ aheinecke@7703: public static final String SQL_SELECT_Q_NAME = aheinecke@7703: "SELECT wqr.q, wc.name " + aheinecke@7703: "FROM wst_column_q_ranges wcqr " + aheinecke@7703: "JOIN wst_q_ranges wqr ON wcqr.wst_q_range_id = wqr.id " + aheinecke@7703: "JOIN wst_columns wc ON wcqr.wst_column_id = wc.id " + aheinecke@7703: "JOIN wsts ON wc.wst_id = wsts.id " + aheinecke@7703: "WHERE wc.wst_id = :wst_id AND wc.position = :column_pos"; aheinecke@7703: /* aheinecke@7703: Test statement: aheinecke@7703: SELECT wqr.q, wc.name aheinecke@7703: FROM wst_column_q_ranges wcqr aheinecke@7703: JOIN wst_q_ranges wqr ON wcqr.wst_q_range_id = wqr.id aheinecke@7703: JOIN wst_columns wc ON wcqr.wst_column_id = wc.id aheinecke@7703: JOIN wsts ON wc.wst_id = wsts.id teichmann@7884: WHERE wc.wst_id = 1817 AND wc.position = 29; aheinecke@7703: */ aheinecke@7703: aheinecke@7703: felix@7686: 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@7686: /** Query to get name (description) and kind for wst_id. */ aheinecke@7703: public static final String SQL_SELECT_WST_Q_NAME = aheinecke@7703: "SELECT wqr.q, wc.name " + aheinecke@7703: "FROM wst_column_q_ranges wcqr " + aheinecke@7703: "JOIN wst_q_ranges wqr ON wcqr.wst_q_range_id = wqr.id " + aheinecke@7703: "JOIN wst_columns wc ON wcqr.wst_column_id = wc.id " + aheinecke@7703: "JOIN wsts ON wc.wst_id = wsts.id " + aheinecke@7703: "WHERE wc.wst_id = :wst_id"; 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@7686: /** Get name for a WKms wrapped in W, if suitable. */ felix@7686: public static String getWKmsNameWWrapped(int wst_id) { aheinecke@7703: return getWKmsNameWWrapped(-1, wst_id); felix@7686: } felix@7686: felix@7686: felix@7686: /** Get name for a WKms wrapped in W, if suitable. */ felix@7686: public static String getWKmsNameWWrapped(int column, int wst_id) { felix@7686: log.debug("WKmsFactory.getWKmsNameWWrapped c/" + column + ", wst_id/" + wst_id); felix@7686: felix@7686: String name = null; felix@7686: Session session = SessionHolder.HOLDER.get(); felix@7686: aheinecke@7703: SQLQuery nameQuery; aheinecke@7703: if (column != -1) { aheinecke@7703: nameQuery = session.createSQLQuery(SQL_SELECT_Q_NAME); aheinecke@7703: nameQuery.setInteger("column_pos", column); aheinecke@7703: } else { aheinecke@7703: nameQuery = session.createSQLQuery(SQL_SELECT_WST_Q_NAME); aheinecke@7703: } aheinecke@7703: aheinecke@7703: nameQuery.addScalar("q", StandardBasicTypes.DOUBLE) aheinecke@7703: .addScalar("name", StandardBasicTypes.STRING); felix@7686: nameQuery.setInteger("wst_id", wst_id); felix@7686: felix@7686: List names = nameQuery.list(); felix@7686: felix@7686: if (names.size() >= 1) { felix@7686: Object[] row = names.get(0); aheinecke@7703: Double q = (Double) row[0]; aheinecke@7703: name = (String) row[1]; aheinecke@7703: if (q >= 0) { felix@7686: name = "W(" + name + ")"; aheinecke@7703: } aheinecke@7703: } else { aheinecke@7703: // This should handle the case of Q = NULL aheinecke@7703: if (column != -1) { aheinecke@7703: name = getWKmsName(column, wst_id); aheinecke@7703: } else { aheinecke@7703: name = getWKmsName(wst_id); aheinecke@7703: } felix@7686: } felix@7686: aheinecke@7703: log.debug("WKmsFactory.getWKmsNameWWrapped c/" + column + aheinecke@7703: ", wst_id/" + wst_id + " = name/ " + name); aheinecke@7703: felix@7686: return name; felix@7686: } felix@7686: felix@7686: 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 :