Mercurial > dive4elements > river
view flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WstValueTableFactory.java @ 965:79251b1d47da
Modified the CollectionMonitor to recommend artifacts for the current state's outputs only.
flys-artifacts/trunk@2385 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Thu, 21 Jul 2011 14:00:09 +0000 |
parents | c09c9e05ecfa |
children | b503d92dd709 |
line wrap: on
line source
package de.intevation.flys.artifacts.model; import java.util.List; import java.util.ArrayList; import net.sf.ehcache.Cache; import net.sf.ehcache.Element; import de.intevation.flys.artifacts.cache.CacheFactory; import de.intevation.flys.backend.SessionHolder; import org.apache.log4j.Logger; import de.intevation.flys.model.River; import de.intevation.flys.model.Wst; import org.hibernate.Session; import org.hibernate.Query; import org.hibernate.SQLQuery; import org.hibernate.type.StandardBasicTypes; public class WstValueTableFactory { private static Logger log = Logger.getLogger(WstValueTableFactory.class); public static final int DEFAULT_KIND = 0; // TODO: put this into a property file public static final String HQL_WST = "from Wst where river=:river and kind=:kind"; public static final String SQL_SELECT_NAMES_POS = "SELECT position, name FROM wst_columns " + "WHERE wst_id = :wst_id ORDER BY position"; public static final String SQL_SELECT_QS = "SELECT column_pos, q, a, b FROM wst_q_values " + "WHERE wst_id = :wst_id"; public static final String SQL_SELECT_WS = "SELECT km, w, column_pos FROM wst_w_values " + "WHERE wst_id = :wst_id"; private WstValueTableFactory() { } public static WstValueTable getTable(River river) { return getTable(river, DEFAULT_KIND); } public static WstValueTable getTable(River river, int kind) { Cache cache = CacheFactory.getCache(WstValueTableCacheKey.CACHE_NAME); WstValueTableCacheKey cacheKey; if (cache != null) { cacheKey = new WstValueTableCacheKey(river.getId(), kind); Element element = cache.get(cacheKey); if (element != null) { log.debug("got wst value table from cache"); return (WstValueTable)element.getValue(); } } else { cacheKey = null; } WstValueTable valueTable = getTableUncached(river, kind); if (valueTable != null && cacheKey != null) { log.debug("store wst value table in cache"); Element element = new Element(cacheKey, valueTable); cache.put(element); } return valueTable; } public static WstValueTable getTableUncached(River river) { return getTableUncached(river, DEFAULT_KIND); } public static WstValueTable getTableUncached(River river, int kind) { Session session = SessionHolder.HOLDER.get(); Wst wst = loadWst(session, river, kind); if (wst == null) { return null; } WstValueTable.Column [] columns = loadColumns(session, wst); loadQRanges(session, columns, wst); List<WstValueTable.Row> rows = loadRows(session, wst, columns.length); return new WstValueTable(columns, rows); } protected static Wst loadWst(Session session, River river, int kind) { Query query = session.createQuery(HQL_WST); query.setParameter("river", river); query.setInteger("kind", kind); List<Wst> wsts = query.list(); return wsts.isEmpty() ? null : wsts.get(0); } protected static List<WstValueTable.Row> loadRows( Session session, Wst wst, int numColumns ) { SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_WS) .addScalar("km", StandardBasicTypes.DOUBLE) .addScalar("w", StandardBasicTypes.DOUBLE) .addScalar("column_pos", StandardBasicTypes.INTEGER); sqlQuery.setInteger("wst_id", wst.getId()); List<Object []> results = sqlQuery.list(); int lastColumn = Integer.MAX_VALUE; double [] ws = null; ArrayList<WstValueTable.Row> rows = new ArrayList<WstValueTable.Row>(); for (Object [] result: results) { int column = (Integer)result[2]; if (column < lastColumn) { ws = new double[numColumns]; WstValueTable.Row row = new WstValueTable.Row((Double)result[0], ws); rows.add(row); } Double w = (Double)result[1]; ws[column] = w != null ? w : Double.NaN; lastColumn = column; } rows.trimToSize(); return rows; } protected static WstValueTable.Column [] loadColumns( Session session, Wst wst ) { SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_NAMES_POS) .addScalar("position", StandardBasicTypes.INTEGER) .addScalar("name", StandardBasicTypes.STRING); sqlQuery.setInteger("wst_id", wst.getId()); List<Object []> columnNames = sqlQuery.list(); WstValueTable.Column [] columns = new WstValueTable.Column[columnNames.size()]; for (int i = 0; i < columns.length; ++i) { columns[i] = new WstValueTable.Column( (String)columnNames.get(i)[1]); } return columns; } protected static void loadQRanges( Session session, WstValueTable.Column [] columns, Wst wst ) { SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_QS) .addScalar("column_pos", StandardBasicTypes.INTEGER) .addScalar("q", StandardBasicTypes.DOUBLE) .addScalar("a", StandardBasicTypes.DOUBLE) .addScalar("b", StandardBasicTypes.DOUBLE); sqlQuery.setInteger("wst_id", wst.getId()); List<Object []> qRanges = sqlQuery.list(); int start = -1; int Q = qRanges.size(); Integer lastColumn = null; for (int i = 0; i < Q; ++i) { Object [] qRange = qRanges.get(i); Integer columnId = (Integer)qRange[0]; if (lastColumn == null) { lastColumn = columnId; start = i; } else if (!lastColumn.equals(columnId)) { QRangeTree qRangeTree = new QRangeTree(qRanges, start, i); columns[lastColumn].setQRangeTree(qRangeTree); lastColumn = columnId; start = i; } } if (start != -1) { QRangeTree qRangeTree = new QRangeTree(qRanges, start, Q); columns[lastColumn].setQRangeTree(qRangeTree); } /* This is debug code to visualize the q ranges trees java.io.PrintWriter out = null; try { out = new java.io.PrintWriter( new java.io.FileWriter( "/tmp/qranges" + System.currentTimeMillis() + ".dot")); out.println("graph \"Q ranges trees\" {"); for (int i = 0; i < columns.length; ++i) { QRangeTree tree = columns[i].getQRangeTree(); out.println(tree.toGraph()); } out.println("}"); out.flush(); } catch (java.io.IOException ioe) { log.error(ioe); } finally { if (out != null) { out.close(); } } */ } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :