diff --git a/edexOsgi/com.raytheon.edex.plugin.gfe/src/com/raytheon/edex/plugin/gfe/config/IFPServerConfigManager.java b/edexOsgi/com.raytheon.edex.plugin.gfe/src/com/raytheon/edex/plugin/gfe/config/IFPServerConfigManager.java index c9e35e5a6e..06664fea40 100644 --- a/edexOsgi/com.raytheon.edex.plugin.gfe/src/com/raytheon/edex/plugin/gfe/config/IFPServerConfigManager.java +++ b/edexOsgi/com.raytheon.edex.plugin.gfe/src/com/raytheon/edex/plugin/gfe/config/IFPServerConfigManager.java @@ -90,12 +90,12 @@ public class IFPServerConfigManager { */ public static IFPServerConfig getServerConfig(String siteID) throws GfeConfigurationException { - if (configMap.containsKey(siteID)) { - return configMap.get(siteID); - } else { + IFPServerConfig config = configMap.get(siteID); + if (config == null) { throw new GfeConfigurationException("Site " + siteID + " has no active GFE configuration."); } + return config; } /** diff --git a/edexOsgi/com.raytheon.edex.plugin.gfe/src/com/raytheon/edex/plugin/gfe/db/dao/GFEDao.java b/edexOsgi/com.raytheon.edex.plugin.gfe/src/com/raytheon/edex/plugin/gfe/db/dao/GFEDao.java index 00d2e830cb..2fa414f63e 100644 --- a/edexOsgi/com.raytheon.edex.plugin.gfe/src/com/raytheon/edex/plugin/gfe/db/dao/GFEDao.java +++ b/edexOsgi/com.raytheon.edex.plugin.gfe/src/com/raytheon/edex/plugin/gfe/db/dao/GFEDao.java @@ -31,18 +31,16 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.SortedMap; +import java.util.TreeMap; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; -import org.hibernate.criterion.Criterion; import org.hibernate.criterion.DetachedCriteria; -import org.hibernate.criterion.Order; import org.hibernate.criterion.Property; -import org.hibernate.criterion.Restrictions; import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.support.TransactionCallback; import org.springframework.transaction.support.TransactionCallbackWithoutResult; @@ -74,7 +72,6 @@ import com.raytheon.uf.common.dataplugin.gfe.type.Pair; import com.raytheon.uf.common.dataplugin.gfe.util.GfeUtil; import com.raytheon.uf.common.dataplugin.grid.GridConstants; import com.raytheon.uf.common.dataplugin.grid.GridInfoConstants; -import com.raytheon.uf.common.dataplugin.grid.GridInfoRecord; import com.raytheon.uf.common.dataplugin.grid.GridRecord; import com.raytheon.uf.common.dataplugin.level.Level; import com.raytheon.uf.common.dataplugin.level.LevelFactory; @@ -106,14 +103,27 @@ import com.raytheon.uf.edex.database.query.DatabaseQuery; * 08/07/09 #2763 njensen Refactored queryByD2DParmId * 09/10/12 DR15137 ryu Changed for MOSGuide D2D mxt/mnt grids for consistency * with A1. - * 10/10/12 #1260 randerso Added check to ensure db can be created before + * 10/10/12 #1260 randerso Added check to ensure db can be created before * adding it to the inventory + * 12/06/12 #1394 rjpeter Optimized D2D grid access. * * * @author bphillip * @version 1.0 */ public class GFEDao extends DefaultPluginDao { + // hibernate query to find grid info record for the given datasetId and + // parameter + private static final String HQL_D2D_GRID_PARM_QUERY = "select parameter.abbreviation, id from GridInfoRecord " + + "where datasetId = :datasetId AND (lower(parameter.abbreviation) = :abbrev OR lower(parameter_abbreviation) like :hourAbbrev)"; + + // hibernate query to find the times for the GridRecord for the given + // info.id, id returned to allow easy lookup of the record associated with + // the time + private static final String HQL_D2D_GRID_TIME_QUERY = "select dataTime, id from GridRecord " + + "where info.id = :info_id AND dataTime.refTime = :refTime order by dataTime.fcstTime"; + + private static final Pattern WIND_PATTERN = Pattern.compile("wind"); public GFEDao() throws PluginException { super("gfe"); @@ -300,7 +310,12 @@ public class GFEDao extends DefaultPluginDao { } } if (sess != null) { - sess.close(); + try { + sess.close(); + } catch (Exception e) { + statusHandler.error( + "Error occurred closing database session", e); + } } } @@ -611,53 +626,75 @@ public class GFEDao extends DefaultPluginDao { } /** - * Retrieves a GridRecord from the grib metadata database based on a ParmID - * and a TimeRange + * Retrieves a GridRecord from the grib metadata database based on a ParmID, + * TimeRange, and GridParmInfo. * * @param id * The parmID of the desired GridRecord * @param timeRange * The timeRange of the desired GridRecord + * @param info + * The GridParmInfo for the requested d2d grid. * @return The GridRecord from the grib metadata database * @throws DataAccessLayerException * If errors occur while querying the metadata database */ public GridRecord getD2DGrid(ParmID id, TimeRange timeRange, GridParmInfo info) throws DataAccessLayerException { - List records = queryByD2DParmId(id); - List gribTimes = new ArrayList(); - for (GridRecord record : records) { - gribTimes.add(record.getDataTime().getValidPeriod()); - } + Session s = null; + + try { + s = getHibernateTemplate().getSessionFactory().openSession(); + // TODO: clean up so we only make one db query + SortedMap rawTimes = queryByD2DParmId(id, s); + List gribTimes = new ArrayList(); + for (DataTime dt : rawTimes.keySet()) { + gribTimes.add(dt.getValidPeriod()); + } - for (int i = 0; i < records.size(); i++) { - TimeRange gribTime = records.get(i).getDataTime().getValidPeriod(); try { if (isMos(id)) { - TimeRange time = info.getTimeConstraints().constraintTime( - gribTime.getEnd()); - if (timeRange.getEnd().equals(time.getEnd()) - || !info.getTimeConstraints().anyConstraints()) { - GridRecord retVal = records.get(i); - retVal.setPluginName(GridConstants.GRID); - return retVal; + for (Map.Entry timeEntry : rawTimes + .entrySet()) { + TimeRange gribTime = timeEntry.getKey() + .getValidPeriod(); + TimeRange time = info.getTimeConstraints() + .constraintTime(gribTime.getEnd()); + if (timeRange.getEnd().equals(time.getEnd()) + || !info.getTimeConstraints().anyConstraints()) { + GridRecord retVal = (GridRecord) s.get( + GridRecord.class, timeEntry.getValue()); + retVal.setPluginName(GridConstants.GRID); + return retVal; + } } } else if (D2DGridDatabase.isNonAccumDuration(id, gribTimes)) { - if (timeRange.getStart().equals(gribTime.getEnd()) - || timeRange.equals(gribTime)) { - GridRecord retVal = records.get(i); - retVal.setPluginName(GridConstants.GRID); - return retVal; + for (Map.Entry timeEntry : rawTimes + .entrySet()) { + TimeRange gribTime = timeEntry.getKey() + .getValidPeriod(); + if (timeRange.getStart().equals(gribTime.getEnd()) + || timeRange.equals(gribTime)) { + GridRecord retVal = (GridRecord) s.get( + GridRecord.class, timeEntry.getValue()); + retVal.setPluginName(GridConstants.GRID); + return retVal; + } } - } else { - TimeRange time = info.getTimeConstraints().constraintTime( - gribTime.getStart()); - if ((timeRange.getStart().equals(time.getStart()) || !info - .getTimeConstraints().anyConstraints())) { - GridRecord retVal = records.get(i); - retVal.setPluginName(GridConstants.GRID); - return retVal; + for (Map.Entry timeEntry : rawTimes + .entrySet()) { + TimeRange gribTime = timeEntry.getKey() + .getValidPeriod(); + TimeRange time = info.getTimeConstraints() + .constraintTime(gribTime.getStart()); + if ((timeRange.getStart().equals(time.getStart()) || !info + .getTimeConstraints().anyConstraints())) { + GridRecord retVal = (GridRecord) s.get( + GridRecord.class, timeEntry.getValue()); + retVal.setPluginName(GridConstants.GRID); + return retVal; + } } } } catch (GfeConfigurationException e) { @@ -665,219 +702,219 @@ public class GFEDao extends DefaultPluginDao { "Error getting configuration for " + id.getDbId().getSiteId(), e); } + } finally { + if (s != null) { + try { + s.close(); + } catch (Exception e) { + statusHandler.error( + "Error occurred closing database session", e); + } + } } return null; } /** - * Gets a list of GridRecords from the grib metadata database which match - * the given ParmID + * Gets a SortedMap of DataTime and GridRecord ids from the grib metadata + * database which match the given ParmID. Session passed to allow reuse + * across multiple calls. * * @param id * The ParmID to search with + * @param s + * The database session to use * @return The list of GridRecords from the grib metadata database which * match the given ParmID * @throws DataAccessLayerException * If errors occur while querying the metadata database */ @SuppressWarnings("unchecked") - public List queryByD2DParmId(ParmID id) + public SortedMap queryByD2DParmId(ParmID id, Session s) throws DataAccessLayerException { - Session s = null; + String levelName = GridTranslator.getLevelName(id.getParmLevel()); + + double[] levelValues = GridTranslator.getLevelValue(id.getParmLevel()); + boolean levelOnePresent = (levelValues[0] != Level + .getInvalidLevelValue()); + boolean levelTwoPresent = (levelValues[1] != Level + .getInvalidLevelValue()); + Level level = null; + + // to have a level 2, must have a level one try { - String levelName = GridTranslator.getLevelName(id.getParmLevel()); + if (levelOnePresent && levelTwoPresent) { + level = LevelFactory.getInstance().getLevel(levelName, + levelValues[0], levelValues[1]); + } else if (levelOnePresent) { + level = LevelFactory.getInstance().getLevel(levelName, + levelValues[0]); + } else { + level = LevelFactory.getInstance().getLevel(levelName, 0.0); + } + } catch (CommunicationException e) { + logger.error(e.getLocalizedMessage(), e); + } + if (level == null) { + logger.warn("Unable to query D2D parms, ParmID " + id + + " does not map to a level"); + return new TreeMap(); + } - double[] levelValues = GridTranslator.getLevelValue(id - .getParmLevel()); - boolean levelOnePresent = (levelValues[0] != Level - .getInvalidLevelValue()); - boolean levelTwoPresent = (levelValues[1] != Level - .getInvalidLevelValue()); - Level level = null; + Query modelQuery = s.createQuery(HQL_D2D_GRID_PARM_QUERY); - // to have a level 2, must have a level one - try { - if (levelOnePresent && levelTwoPresent) { - level = LevelFactory.getInstance().getLevel(levelName, - levelValues[0], levelValues[1]); - } else if (levelOnePresent) { - level = LevelFactory.getInstance().getLevel(levelName, - levelValues[0]); + DatabaseID dbId = id.getDbId(); + try { + IFPServerConfig config = IFPServerConfigManager + .getServerConfig(dbId.getSiteId()); + modelQuery.setString(GridInfoConstants.DATASET_ID, + config.d2dModelNameMapping(dbId.getModelName())); + } catch (GfeConfigurationException e) { + throw new DataAccessLayerException( + "Error occurred looking up model name mapping", e); + } + String abbreviation = DataFieldTableLookup.getInstance() + .lookupDataName(id.getParmName()); + if (abbreviation == null) { + abbreviation = id.getParmName(); + } + abbreviation = abbreviation.toLowerCase(); + modelQuery.setString("abbrev", abbreviation); + modelQuery.setString("hourAbbrev", abbreviation + "%hr"); + List results = modelQuery.list(); + Integer modelId = null; + if (results.size() == 0) { + return new TreeMap(); + } else if (results.size() > 1) { + // hours matched, take hour with least number that matches exact + // param + Pattern p = Pattern.compile("^" + abbreviation + "(\\d+)hr$"); + int lowestHr = -1; + for (Object[] rows : (List) results) { + String param = ((String) rows[0]).toLowerCase(); + if (param.equals(abbreviation) && (lowestHr < 0)) { + modelId = (Integer) rows[1]; } else { - level = LevelFactory.getInstance().getLevel(levelName, 0.0); - } - } catch (CommunicationException e) { - logger.error(e.getLocalizedMessage(), e); - } - if (level == null) { - logger.warn("Unable to query D2D parms, ParmID " + id - + " does not map to a level"); - return new ArrayList(); - } - - s = getHibernateTemplate().getSessionFactory().openSession(); - - Criteria modelCrit = s.createCriteria(GridInfoRecord.class); - Criterion baseCrit = Restrictions - .eq(GridInfoConstants.LEVEL, level); - - DatabaseID dbId = id.getDbId(); - try { - IFPServerConfig config = IFPServerConfigManager - .getServerConfig(dbId.getSiteId()); - baseCrit = Restrictions.and(baseCrit, Restrictions.eq( - GridInfoConstants.DATASET_ID, - config.d2dModelNameMapping(dbId.getModelName()))); - } catch (GfeConfigurationException e) { - throw new DataAccessLayerException( - "Error occurred looking up model name mapping", e); - } - String abbreviation = DataFieldTableLookup.getInstance() - .lookupDataName(id.getParmName()); - if (abbreviation == null) { - abbreviation = id.getParmName(); - } - abbreviation = abbreviation.toLowerCase(); - Criterion abbrevCrit = Restrictions - .and(baseCrit, - Restrictions.or( - Restrictions - .sqlRestriction("lower(parameter_abbreviation) = '" - + abbreviation + "'"), - Restrictions - .sqlRestriction("lower(parameter_abbreviation) like '" - + abbreviation + "%hr'"))); - - modelCrit.add(abbrevCrit); - List results = modelCrit.list(); - GridInfoRecord model = null; - if (results.size() == 0) { - return new ArrayList(0); - } else if (results.size() > 1) { - // hours matched, take hour with least number that matches exact - // param - Pattern p = Pattern.compile("^" + abbreviation + "(\\d+)hr$"); - int lowestHr = -1; - for (GridInfoRecord m : (List) results) { - String param = m.getParameter().getAbbreviation() - .toLowerCase(); - if (param.equals(abbreviation) && (lowestHr < 0)) { - model = m; - } else { - Matcher matcher = p.matcher(param); - if (matcher.matches()) { - int hr = Integer.parseInt(matcher.group(1)); - if ((lowestHr < 0) || (hr < lowestHr)) { - model = m; - lowestHr = hr; - } + Matcher matcher = p.matcher(param); + if (matcher.matches()) { + int hr = Integer.parseInt(matcher.group(1)); + if ((lowestHr < 0) || (hr < lowestHr)) { + modelId = (Integer) rows[1]; + lowestHr = hr; } } } - - } else { - model = (GridInfoRecord) results.get(0); - } - - Criteria recordCrit = s.createCriteria(GridRecord.class); - baseCrit = Restrictions.eq("info", model); - baseCrit = Restrictions.and( - baseCrit, - Restrictions.eq("dataTime.refTime", - dbId.getModelTimeAsDate())); - recordCrit.add(baseCrit); - recordCrit.addOrder(Order.asc("dataTime.fcstTime")); - return recordCrit.list(); - } finally { - if (s != null) { - s.flush(); - s.close(); } + } else { + modelId = (Integer) ((Object[]) results.get(0))[1]; } + + Query timeQuery = s.createQuery(HQL_D2D_GRID_TIME_QUERY); + timeQuery.setInteger("info_id", modelId); + timeQuery.setParameter("refTime", dbId.getModelTimeAsDate()); + List timeResults = timeQuery.list(); + if (timeResults.isEmpty()) { + return new TreeMap(); + } + + SortedMap dataTimes = new TreeMap(); + for (Object[] rows : timeResults) { + dataTimes.put((DataTime) rows[0], (Integer) rows[1]); + } + return dataTimes; } public List queryTimeByD2DParmId(ParmID id) throws DataAccessLayerException { List timeList = new ArrayList(); - if (id.getParmName().equalsIgnoreCase("wind")) { - ParmID uWindId = new ParmID(id.toString().replace("wind", "uW")); - List uTimeList = new ArrayList(); - List results = executeD2DParmQuery(uWindId); - for (DataTime o : results) { - uTimeList.add(new TimeRange(o.getValidPeriod().getStart(), - 3600 * 1000)); - } + Session s = null; + try { + s = getHibernateTemplate().getSessionFactory().openSession(); - ParmID vWindId = new ParmID(id.toString().replace("wind", "vW")); - List vTimeList = new ArrayList(); - results = executeD2DParmQuery(vWindId); - for (DataTime o : results) { - vTimeList.add(new TimeRange(o.getValidPeriod().getStart(), - 3600 * 1000)); - } + if (id.getParmName().equalsIgnoreCase("wind")) { + String idString = id.toString(); + Matcher idWindMatcher = WIND_PATTERN.matcher(idString); - for (TimeRange tr : uTimeList) { - if (vTimeList.contains(tr)) { - timeList.add(new TimeRange(tr.getStart(), tr.getStart())); + ParmID uWindId = new ParmID(idWindMatcher.replaceAll("uW")); + SortedMap results = queryByD2DParmId( + uWindId, s); + List uTimeList = new ArrayList( + results.size()); + for (DataTime o : results.keySet()) { + uTimeList.add(new TimeRange(o.getValidPeriod().getStart(), + 3600 * 1000)); } - } - if (!timeList.isEmpty()) { - return timeList; - } - - ParmID sWindId = new ParmID(id.toString().replace("wind", "ws")); - List sTimeList = new ArrayList(); - results = executeD2DParmQuery(sWindId); - for (DataTime o : results) { - sTimeList.add(new TimeRange(o.getValidPeriod().getStart(), - 3600 * 1000)); - } - - ParmID dWindId = new ParmID(id.toString().replace("wind", "wd")); - List dTimeList = new ArrayList(); - results = executeD2DParmQuery(dWindId); - for (DataTime o : results) { - dTimeList.add(new TimeRange(o.getValidPeriod().getStart(), - 3600 * 1000)); - } - - for (TimeRange tr : sTimeList) { - if (dTimeList.contains(tr)) { - timeList.add(new TimeRange(tr.getStart(), tr.getStart())); + ParmID vWindId = new ParmID(idWindMatcher.replaceAll("vW")); + results = queryByD2DParmId(vWindId, s); + Set vTimeList = new HashSet( + results.size(), 1); + for (DataTime o : results.keySet()) { + vTimeList.add(new TimeRange(o.getValidPeriod().getStart(), + 3600 * 1000)); } - } - if (!timeList.isEmpty()) { - return timeList; - } - } else { - List results = executeD2DParmQuery(id); - for (DataTime o : results) { + for (TimeRange tr : uTimeList) { + if (vTimeList.contains(tr)) { + timeList.add(new TimeRange(tr.getStart(), tr.getStart())); + } + } + + if (!timeList.isEmpty()) { + return timeList; + } + + ParmID sWindId = new ParmID(idWindMatcher.replaceAll("ws")); + results = queryByD2DParmId(sWindId, s); + List sTimeList = new ArrayList( + results.size()); + for (DataTime o : results.keySet()) { + sTimeList.add(new TimeRange(o.getValidPeriod().getStart(), + 3600 * 1000)); + } + + ParmID dWindId = new ParmID(idWindMatcher.replaceAll("wd")); + results = queryByD2DParmId(dWindId, s); + Set dTimeList = new HashSet( + results.size(), 1); + for (DataTime o : results.keySet()) { + dTimeList.add(new TimeRange(o.getValidPeriod().getStart(), + 3600 * 1000)); + } + + for (TimeRange tr : sTimeList) { + if (dTimeList.contains(tr)) { + timeList.add(new TimeRange(tr.getStart(), tr.getStart())); + } + } + } else { + SortedMap results = queryByD2DParmId(id, s); if (isMos(id)) { - timeList.add(new TimeRange(o.getValidPeriod().getEnd(), o - .getValidPeriod().getDuration())); + for (DataTime o : results.keySet()) { + timeList.add(new TimeRange(o.getValidPeriod().getEnd(), + o.getValidPeriod().getDuration())); + } } else { - timeList.add(o.getValidPeriod()); + for (DataTime o : results.keySet()) { + timeList.add(o.getValidPeriod()); + } + } + } + } finally { + if (s != null) { + try { + s.close(); + } catch (Exception e) { + statusHandler.error( + "Error occurred closing database session", e); } - } } return timeList; } - private List executeD2DParmQuery(ParmID id) - throws DataAccessLayerException { - List times = new ArrayList(); - List records = queryByD2DParmId(id); - for (GridRecord record : records) { - times.add(record.getDataTime()); - } - return times; - } - public void purgeGFEGrids(final DatabaseID dbId) { txTemplate.execute(new TransactionCallbackWithoutResult() { @Override diff --git a/edexOsgi/com.raytheon.edex.plugin.gfe/src/com/raytheon/edex/plugin/gfe/server/database/D2DGridDatabase.java b/edexOsgi/com.raytheon.edex.plugin.gfe/src/com/raytheon/edex/plugin/gfe/server/database/D2DGridDatabase.java index 119b6fc728..5e312d0f2f 100644 --- a/edexOsgi/com.raytheon.edex.plugin.gfe/src/com/raytheon/edex/plugin/gfe/server/database/D2DGridDatabase.java +++ b/edexOsgi/com.raytheon.edex.plugin.gfe/src/com/raytheon/edex/plugin/gfe/server/database/D2DGridDatabase.java @@ -108,12 +108,12 @@ public class D2DGridDatabase extends VGridDatabase { .getNamedHandler("GFEPerformanceLogger"); /** The remap object used for resampling grids */ - private Map remap = new HashMap(); + private final Map remap = new HashMap(); /** The destination GridLocation (The local GFE grid coverage) */ private GridLocation outputLoc; - private List parms; + private final List parms; public static final String GRID_LOCATION_CACHE_KEY = "GfeLocations"; @@ -394,7 +394,7 @@ public class D2DGridDatabase extends VGridDatabase { public ServerResponse> getGridData(ParmID id, List timeRanges) { - List data = new ArrayList(); + List data = new ArrayList(timeRanges.size()); ServerResponse> sr = new ServerResponse>(); for (TimeRange tr : timeRanges) { GridParmInfo gpi = getGridParmInfo(id).getPayload(); @@ -415,7 +415,7 @@ public class D2DGridDatabase extends VGridDatabase { public ServerResponse> getGridData(ParmID id, List timeRanges, boolean convertUnit) { - List data = new ArrayList(); + List data = new ArrayList(timeRanges.size()); ServerResponse> sr = new ServerResponse>(); for (TimeRange tr : timeRanges) { GridParmInfo gpi = getGridParmInfo(id).getPayload(); @@ -429,6 +429,7 @@ public class D2DGridDatabase extends VGridDatabase { + " TimeRange: " + tr, e); } } + sr.setPayload(data); return sr; } @@ -550,7 +551,7 @@ public class D2DGridDatabase extends VGridDatabase { RemapGrid remap = getOrCreateRemap(d2dRecord.getLocation()); retVal = remap.remap(bdata, fillV, gpi.getMaxValue(), gpi.getMinValue(), gpi.getMinValue()); - if (convertUnit && d2dRecord != null) { + if (convertUnit && (d2dRecord != null)) { convertUnits(d2dRecord, retVal, gpi.getUnitObject()); } } catch (Exception e) { @@ -651,7 +652,7 @@ public class D2DGridDatabase extends VGridDatabase { String mappedModel = config.d2dModelNameMapping(dbId.getModelName()); - if (uRecord != null && vRecord != null) { + if ((uRecord != null) && (vRecord != null)) { // Gets the raw grid data from the D2D grib HDF5 files Grid2DFloat uData = getRawGridData(uRecord); Grid2DFloat vData = getRawGridData(vRecord); @@ -688,7 +689,7 @@ public class D2DGridDatabase extends VGridDatabase { "Unable to retrieve wind grids from D2D database", e); } - if (sRecord != null && dRecord != null) { + if ((sRecord != null) && (dRecord != null)) { // Gets the raw grid data from the D2D grib HDF5 files Grid2DFloat sData = getRawGridData(sRecord); Grid2DFloat dData = getRawGridData(dRecord); @@ -914,8 +915,8 @@ public class D2DGridDatabase extends VGridDatabase { // no-op } - public static boolean isNonAccumDuration(ParmID id, List times) - throws GfeConfigurationException { + public static boolean isNonAccumDuration(ParmID id, + Collection times) throws GfeConfigurationException { boolean isAccum = false; try { isAccum = IFPServerConfigManager @@ -925,14 +926,19 @@ public class D2DGridDatabase extends VGridDatabase { } catch (GfeConfigurationException e) { throw e; } - boolean isDuration = false; - for (TimeRange time : times) { - if (time.getDuration() > 0) { - isDuration = true; - break; + + if (!isAccum) { + boolean isDuration = false; + for (TimeRange time : times) { + if (time.getDuration() > 0) { + isDuration = true; + break; + } } + return !isAccum && isDuration; } - return !isAccum && isDuration; + + return !isAccum; } } diff --git a/edexOsgi/com.raytheon.edex.plugin.gfe/src/com/raytheon/edex/plugin/gfe/util/GridTranslator.java b/edexOsgi/com.raytheon.edex.plugin.gfe/src/com/raytheon/edex/plugin/gfe/util/GridTranslator.java index 995b1c998e..2a8803628d 100644 --- a/edexOsgi/com.raytheon.edex.plugin.gfe/src/com/raytheon/edex/plugin/gfe/util/GridTranslator.java +++ b/edexOsgi/com.raytheon.edex.plugin.gfe/src/com/raytheon/edex/plugin/gfe/util/GridTranslator.java @@ -21,6 +21,7 @@ package com.raytheon.edex.plugin.gfe.util; import java.util.Arrays; import java.util.List; +import java.util.regex.Pattern; import com.raytheon.uf.common.dataplugin.level.Level; import com.raytheon.uf.common.dataplugin.level.LevelFactory; @@ -34,7 +35,7 @@ import com.raytheon.uf.common.dataplugin.level.LevelFactory; * ------------ ---------- ----------- -------------------------- * May 5, 2008 njensen Initial creation * Aug 22, 2008 1502 dglazesk Changed to JAXB unmarshalling - * + * Dec 06, 2012 1394 rjpeter Attend static compiled patterns. * * * @author njensen @@ -45,15 +46,19 @@ public class GridTranslator { private static final List NEEDS_ZERO = Arrays.asList("BLD", "WBZ"); + private static final Pattern ANY_NUMBER = Pattern.compile("[0-9]"); + + private static final Pattern NOT_NUMBER = Pattern.compile("[^0-9]"); + private GridTranslator() { } public static String getLevelName(String shorthand) { - return shorthand.replaceAll("[0-9]*", ""); + return ANY_NUMBER.matcher(shorthand).replaceAll(""); } public static double[] getLevelValue(String shorthand) { - String levelString = shorthand.replaceAll("[^0-9]", ""); + String levelString = NOT_NUMBER.matcher(shorthand).replaceAll(""); int length = levelString.length(); double[] retVal = new double[2]; retVal[0] = Level.getInvalidLevelValue(); @@ -126,9 +131,9 @@ public class GridTranslator { } else { tmp.append(name); - if (levelOne != Level.getInvalidLevelValue() - && (levelOne != 0 - || levelTwo != Level.getInvalidLevelValue() || NEEDS_ZERO + if ((levelOne != Level.getInvalidLevelValue()) + && ((levelOne != 0) + || (levelTwo != Level.getInvalidLevelValue()) || NEEDS_ZERO .contains(name))) { tmp.append(String.valueOf(Math.round(levelOne))); } @@ -166,7 +171,7 @@ public class GridTranslator { + level + " " + Arrays.toString(levels) - + (l1 == levels[0] && l2 == levels[1] ? " passed" + + ((l1 == levels[0]) && (l2 == levels[1]) ? " passed" : " failed")); } } @@ -187,7 +192,7 @@ public class GridTranslator { + level + " " + Arrays.toString(levels) - + (l1 == levels[0] && l2 == levels[1] ? " passed" + + ((l1 == levels[0]) && (l2 == levels[1]) ? " passed" : " failed")); } } @@ -197,26 +202,50 @@ public class GridTranslator { l2 = -999999; level = GridTranslator.getShortLevelName(levelName, l1, l2); levels = GridTranslator.getLevelValue(level); - System.out.println(levelName + " " + l1 + " " + l2 + ": " + level + " " + System.out.println(levelName + + " " + + l1 + + " " + + l2 + + ": " + + level + + " " + Arrays.toString(levels) - + (l1 == levels[0] && l2 == levels[1] ? " passed" : " failed")); + + ((l1 == levels[0]) && (l2 == levels[1]) ? " passed" + : " failed")); levelName = "BLD"; l1 = 0; l2 = -999999; level = GridTranslator.getShortLevelName(levelName, l1, l2); levels = GridTranslator.getLevelValue(level); - System.out.println(levelName + " " + l1 + " " + l2 + ": " + level + " " + System.out.println(levelName + + " " + + l1 + + " " + + l2 + + ": " + + level + + " " + Arrays.toString(levels) - + (l1 == levels[0] && l2 == levels[1] ? " passed" : " failed")); + + ((l1 == levels[0]) && (l2 == levels[1]) ? " passed" + : " failed")); levelName = "WBZ"; l1 = 0; l2 = -999999; level = GridTranslator.getShortLevelName(levelName, l1, l2); levels = GridTranslator.getLevelValue(level); - System.out.println(levelName + " " + l1 + " " + l2 + ": " + level + " " + System.out.println(levelName + + " " + + l1 + + " " + + l2 + + ": " + + level + + " " + Arrays.toString(levels) - + (l1 == levels[0] && l2 == levels[1] ? " passed" : " failed")); + + ((l1 == levels[0]) && (l2 == levels[1]) ? " passed" + : " failed")); } } diff --git a/edexOsgi/com.raytheon.edex.plugin.grib/src/com/raytheon/edex/plugin/grib/util/DataFieldTableLookup.java b/edexOsgi/com.raytheon.edex.plugin.grib/src/com/raytheon/edex/plugin/grib/util/DataFieldTableLookup.java index 887cb2e8ef..71c1479617 100644 --- a/edexOsgi/com.raytheon.edex.plugin.grib/src/com/raytheon/edex/plugin/grib/util/DataFieldTableLookup.java +++ b/edexOsgi/com.raytheon.edex.plugin.grib/src/com/raytheon/edex/plugin/grib/util/DataFieldTableLookup.java @@ -23,6 +23,7 @@ import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; +import java.util.regex.Pattern; import com.raytheon.uf.common.localization.LocalizationContext.LocalizationLevel; import com.raytheon.uf.common.localization.LocalizationContext.LocalizationType; @@ -56,11 +57,19 @@ public class DataFieldTableLookup { private static DataFieldTableLookup instance; - private Map data2cdl = new HashMap(); + private final Pattern DASH = Pattern.compile("-"); - private Map cdl2data = new HashMap(); + private final Pattern UNDERSCORE = Pattern.compile("_"); - private Map data2name = new HashMap(); + private final Pattern NEWLINE = Pattern.compile("\n"); + + private final Pattern PIPE = Pattern.compile("\\|"); + + private final Map data2cdl = new HashMap(); + + private final Map cdl2data = new HashMap(); + + private final Map data2name = new HashMap(); public static synchronized DataFieldTableLookup getInstance() { if (instance == null) { @@ -87,13 +96,13 @@ public class DataFieldTableLookup { } catch (IOException e) { statusHandler.handle(Priority.PROBLEM, e.getLocalizedMessage(), e); } - for (String line : contents.split("\n")) { + for (String line : NEWLINE.split(contents)) { line = line.trim(); if (line.startsWith("//") || line.startsWith("#")) { // ignore comments continue; } - String[] parts = line.split("\\|"); + String[] parts = PIPE.split(line); if (parts.length < 2) { // invalid line continue; @@ -121,7 +130,7 @@ public class DataFieldTableLookup { public String lookupCdlName(String dataField) { String retVal = data2cdl.get(dataField); if (retVal == null) { - retVal = data2cdl.get(dataField.replace("-", "_")); + retVal = data2cdl.get(DASH.matcher(dataField).replaceAll("_")); if (retVal == null) { retVal = dataField; } @@ -132,7 +141,7 @@ public class DataFieldTableLookup { public String lookupDataName(String cdlField) { String retVal = cdl2data.get(cdlField); if (retVal != null) { - retVal = retVal.replace("_", "-"); + retVal = UNDERSCORE.matcher(retVal).replaceAll("-"); } return retVal; } @@ -140,7 +149,7 @@ public class DataFieldTableLookup { public String lookupName(String dataField) { String retVal = data2name.get(dataField); if (retVal == null) { - retVal = data2name.get(dataField.replace("-", "_")); + retVal = data2name.get(DASH.matcher(dataField).replaceAll("_")); } return retVal; }