From 8f2350f6604e7b3c567fb264738c6678466a4852 Mon Sep 17 00:00:00 2001 From: Mike Duff Date: Mon, 2 Feb 2015 10:16:45 -0600 Subject: [PATCH] Issue #4030 - Added functionality to convert NDM menu files to A2 menu files and have them droped into the NDM endpoint. Change-Id: Ieb87acafed0d9e756be49c3111ba97a921ca425e Former-commit-id: 3f5edb033f8b54908adcd3a751eabe189570d338 --- .../build.edex/esb/bin/ndmMenuIngester.sh | 71 ++++ .../META-INF/MANIFEST.MF | 3 +- .../redbook/ingest/RedbookMenuSubscriber.java | 13 +- .../plugin/redbook/ingest/xml/MenuEntry.java | 15 +- .../redbook/ingest/xml/RedbookMenusXML.java | 5 + .../plugin/redbook/menu/NdmMenuConverter.java | 360 ++++++++++++++++++ 6 files changed, 459 insertions(+), 8 deletions(-) create mode 100644 edexOsgi/build.edex/esb/bin/ndmMenuIngester.sh create mode 100644 edexOsgi/com.raytheon.uf.edex.plugin.redbook/src/com/raytheon/uf/edex/plugin/redbook/menu/NdmMenuConverter.java diff --git a/edexOsgi/build.edex/esb/bin/ndmMenuIngester.sh b/edexOsgi/build.edex/esb/bin/ndmMenuIngester.sh new file mode 100644 index 0000000000..7ce8a1028d --- /dev/null +++ b/edexOsgi/build.edex/esb/bin/ndmMenuIngester.sh @@ -0,0 +1,71 @@ +#!/bin/bash +## +# This software was developed and / or modified by Raytheon Company, +# pursuant to Contract DG133W-05-CQ-1067 with the US Government. +# +# U.S. EXPORT CONTROLLED TECHNICAL DATA +# This software product contains export-restricted data whose +# export/transfer/disclosure is restricted by U.S. law. Dissemination +# to non-U.S. persons whether in the United States or abroad requires +# an export license or other authorization. +# +# Contractor Name: Raytheon Company +# Contractor Address: 6825 Pine Street, Suite 340 +# Mail Stop B8 +# Omaha, NE 68106 +# 402.291.0100 +# +# See the AWIPS II Master Rights File ("Master Rights File.pdf") for +# further licensing information. +## +# NDM Menu File Ingester + +if [ "$1" == "-help" ]; +then + echo "" + echo "ndmMenuIngester.sh " + echo " ndmFileDirectory is the directory holding the ndm files" + echo " ndmMenuFileName is the name of the ndm menu file to convert" + echo "" + echo " Required files: redbookDataKeys.txt, redbookDepictKeys.txt," + echo " redbookProductButtons.txt, and the menu file to convert" + exit; +fi +if [ ! -d "$1" ]; +then + echo "Directory [$1] does not exist!" + exit +fi + +if [ ! -f "$1/$2" ]; +then + echo "File [$2] does not exist!" + exit +fi +SRC_DIR=$1 +MENU_FILE=$2 +path_to_script=`readlink -f $0` +dir=$(dirname $path_to_script) +dir=$(dirname $dir) +AWIPS_HOME=$(dirname $dir) + +# Search for jars +EDEX_PLUGINS="$AWIPS_HOME/edex/lib/" +JARS=$(find $EDEX_PLUGINS -name "*.jar") + +# Add jars to classpath +addSep=false +for i in $JARS +do + if [[ "$addSep" == true ]]; + then + LOCAL_CLASSPATH=$LOCAL_CLASSPATH":"$i + else + LOCAL_CLASSPATH=$i + addSep=true + fi +done + +JAVA_INSTALL=/awips2/java/ +${JAVA_INSTALL}/bin/java -classpath $LOCAL_CLASSPATH com.raytheon.uf.edex.plugin.redbook.menu.NdmMenuConverter $SRC_DIR $MENU_FILE + diff --git a/edexOsgi/com.raytheon.uf.edex.plugin.redbook/META-INF/MANIFEST.MF b/edexOsgi/com.raytheon.uf.edex.plugin.redbook/META-INF/MANIFEST.MF index 1dbdd18505..8cfd6c28cb 100644 --- a/edexOsgi/com.raytheon.uf.edex.plugin.redbook/META-INF/MANIFEST.MF +++ b/edexOsgi/com.raytheon.uf.edex.plugin.redbook/META-INF/MANIFEST.MF @@ -12,7 +12,8 @@ Require-Bundle: com.raytheon.edex.common, com.raytheon.uf.common.status;bundle-version="1.12.1174", com.raytheon.uf.edex.ndm;bundle-version="1.14.0", com.raytheon.uf.edex.menus;bundle-version="1.0.0", - com.raytheon.uf.common.dataplugin.redbook;bundle-version="1.14.0" + com.raytheon.uf.common.dataplugin.redbook;bundle-version="1.14.0", + org.apache.commons.lang;bundle-version="2.3.0" Export-Package: com.raytheon.uf.edex.plugin.redbook, com.raytheon.uf.edex.plugin.redbook.decoder Bundle-RequiredExecutionEnvironment: JavaSE-1.6 diff --git a/edexOsgi/com.raytheon.uf.edex.plugin.redbook/src/com/raytheon/uf/edex/plugin/redbook/ingest/RedbookMenuSubscriber.java b/edexOsgi/com.raytheon.uf.edex.plugin.redbook/src/com/raytheon/uf/edex/plugin/redbook/ingest/RedbookMenuSubscriber.java index 2c6e47864c..bf1fa4a877 100644 --- a/edexOsgi/com.raytheon.uf.edex.plugin.redbook/src/com/raytheon/uf/edex/plugin/redbook/ingest/RedbookMenuSubscriber.java +++ b/edexOsgi/com.raytheon.uf.edex.plugin.redbook/src/com/raytheon/uf/edex/plugin/redbook/ingest/RedbookMenuSubscriber.java @@ -47,6 +47,7 @@ import com.raytheon.uf.edex.plugin.redbook.menu.RedbookUaMenuUtil; * Mar 19, 2014 2857 mpduff Implement NCO. * Mar 19, 2014 2859 mpduff Implement MPC. * Mar 19, 2014 2860 mpduff Implement Upper Air. + * Jan 28, 2015 4030 mpduff Changed constants to public. * * * @@ -60,22 +61,22 @@ public class RedbookMenuSubscriber implements INationalDatasetSubscriber { .getHandler(RedbookMenuSubscriber.class); /** Hazard menu file */ - private static final String HAZARD_MENU_FILE = "RedbookHazardMenus.xml"; + public static final String HAZARD_MENU_FILE = "RedbookHazardMenus.xml"; /** HPC menu file */ - private static final String HPC_MENU_FILE = "RedbookHPCMenus.xml"; + public static final String HPC_MENU_FILE = "RedbookHPCMenus.xml"; /** CPC menu file */ - private static final String CPC_MENU_FILE = "RedbookCPCMenus.xml"; + public static final String CPC_MENU_FILE = "RedbookCPCMenus.xml"; /** MPC menu file */ - private static final String MPC_MENU_FILE = "RedbookMPCMenus.xml"; + public static final String MPC_MENU_FILE = "RedbookMPCMenus.xml"; /** NCO menu file */ - private static final String NCO_MENU_FILE = "RedbookNCOMenus.xml"; + public static final String NCO_MENU_FILE = "RedbookNCOMenus.xml"; /** Upper air menu file */ - private static final String UA_MENU_FILE = "RedbookUAMenus.xml"; + public static final String UA_MENU_FILE = "RedbookUAMenus.xml"; /** * {@inheritDoc} diff --git a/edexOsgi/com.raytheon.uf.edex.plugin.redbook/src/com/raytheon/uf/edex/plugin/redbook/ingest/xml/MenuEntry.java b/edexOsgi/com.raytheon.uf.edex.plugin.redbook/src/com/raytheon/uf/edex/plugin/redbook/ingest/xml/MenuEntry.java index 7a08a9b1ce..6ff7ae392a 100644 --- a/edexOsgi/com.raytheon.uf.edex.plugin.redbook/src/com/raytheon/uf/edex/plugin/redbook/ingest/xml/MenuEntry.java +++ b/edexOsgi/com.raytheon.uf.edex.plugin.redbook/src/com/raytheon/uf/edex/plugin/redbook/ingest/xml/MenuEntry.java @@ -39,6 +39,7 @@ import javax.xml.bind.annotation.XmlElements; * ------------ ---------- ----------- -------------------------- * Mar 12, 2014 2855 mpduff Initial creation * Mar 19, 2014 2860 mpduff Added DataUri. + * Jan 28, 2015 4030 mpduff Added addMenuEntry method. * * * @@ -208,7 +209,7 @@ public class MenuEntry { * @param menuEntryList * the menuEntryList to set */ - public void setMenuEntryyList(List menuEntryList) { + public void setMenuEntryList(List menuEntryList) { this.menuEntryList = menuEntryList; } @@ -227,6 +228,18 @@ public class MenuEntry { this.dataUri = dataUri; } + /** + * Add a {@link MenuEntry} + * + * @param entry + */ + public void addMenuEntry(MenuEntry entry) { + if (this.menuEntryList == null) { + menuEntryList = new ArrayList(); + } + menuEntryList.add(entry); + } + /* * (non-Javadoc) * diff --git a/edexOsgi/com.raytheon.uf.edex.plugin.redbook/src/com/raytheon/uf/edex/plugin/redbook/ingest/xml/RedbookMenusXML.java b/edexOsgi/com.raytheon.uf.edex.plugin.redbook/src/com/raytheon/uf/edex/plugin/redbook/ingest/xml/RedbookMenusXML.java index 1aeba28642..1079d5cc33 100644 --- a/edexOsgi/com.raytheon.uf.edex.plugin.redbook/src/com/raytheon/uf/edex/plugin/redbook/ingest/xml/RedbookMenusXML.java +++ b/edexOsgi/com.raytheon.uf.edex.plugin.redbook/src/com/raytheon/uf/edex/plugin/redbook/ingest/xml/RedbookMenusXML.java @@ -39,6 +39,7 @@ import javax.xml.bind.annotation.XmlRootElement; * ------------ ---------- ----------- -------------------------- * Mar 07, 2014 2858 mpduff Initial creation. * Mar 17, 2014 2855 mpduff Renamed to RedbookMenusXML.java. + * Jan 28, 2015 4030 mpduff Added addMenuEntry method. * * * @@ -78,6 +79,10 @@ public class RedbookMenusXML { this.menuEntryList = menuEntryList; } + public void addMenuEntry(MenuEntry entry) { + menuEntryList.add(entry); + } + /* * (non-Javadoc) * diff --git a/edexOsgi/com.raytheon.uf.edex.plugin.redbook/src/com/raytheon/uf/edex/plugin/redbook/menu/NdmMenuConverter.java b/edexOsgi/com.raytheon.uf.edex.plugin.redbook/src/com/raytheon/uf/edex/plugin/redbook/menu/NdmMenuConverter.java new file mode 100644 index 0000000000..2420560859 --- /dev/null +++ b/edexOsgi/com.raytheon.uf.edex.plugin.redbook/src/com/raytheon/uf/edex/plugin/redbook/menu/NdmMenuConverter.java @@ -0,0 +1,360 @@ +/** + * This software was developed and / or modified by Raytheon Company, + * pursuant to Contract DG133W-05-CQ-1067 with the US Government. + * + * U.S. EXPORT CONTROLLED TECHNICAL DATA + * This software product contains export-restricted data whose + * export/transfer/disclosure is restricted by U.S. law. Dissemination + * to non-U.S. persons whether in the United States or abroad requires + * an export license or other authorization. + * + * Contractor Name: Raytheon Company + * Contractor Address: 6825 Pine Street, Suite 340 + * Mail Stop B8 + * Omaha, NE 68106 + * 402.291.0100 + * + * See the AWIPS II Master Rights File ("Master Rights File.pdf") for + * further licensing information. + **/ +package com.raytheon.uf.edex.plugin.redbook.menu; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.Marshaller; + +import org.apache.commons.lang.StringUtils; + +import com.raytheon.uf.edex.plugin.redbook.ingest.RedbookMenuSubscriber; +import com.raytheon.uf.edex.plugin.redbook.ingest.xml.MenuEntry; +import com.raytheon.uf.edex.plugin.redbook.ingest.xml.MenuEntryType; +import com.raytheon.uf.edex.plugin.redbook.ingest.xml.RedbookMenusXML; + +/** + * This class is called from /awips2/edex/bin/ndmMenuIngester.sh. + * + * It reads in the NDM menu files and outputs an A2 version into the NDM + * endpoint on edex. + * + *
+ * 
+ * SOFTWARE HISTORY
+ * 
+ * Date         Ticket#    Engineer    Description
+ * ------------ ---------- ----------- --------------------------
+ * Jan 30, 2015   4030     mpduff      Initial creation
+ * 
+ * 
+ * + * @author mpduff + * @version 1.0 + */ + +public class NdmMenuConverter { + private static final String WMO = "wmo"; + + private static final String OUTPUT_PATH = File.separator + "awips2" + + File.separator + "edex" + File.separator + "data" + + File.separator + "ndm" + File.separator; + + private static final String HPC_FILE = "redbookHPCMenus.txt"; + + private static final String CPC_FILE = "redbookCPCMenus.txt"; + + private static final String NCO_FILE = "redbookNCOMenus.txt"; + + private static final String HAZARDS_FILE = "redbookHazardMenus.txt"; + + private static final String MARINE_FILE = "redbookMarineMenus.txt"; + + private static final String UPPER_AIR_FILE = "redbookUpperAirMenus.txt"; + + private String dataKeysPath; + + private String depictKeysPath; + + private String menuFilePath; + + private String productButtonPath; + + private JAXBContext jax; + + private Marshaller marshaller; + + private File depictFile; + + public NdmMenuConverter() { + createContext(); + } + + public void convert() { + RedbookMenusXML menuXml = new RedbookMenusXML(); + MenuEntry titleMenuEntry; + int sepCounter = 0; + List subMenuList = new ArrayList(); + + try { + File dataFile = new File(this.dataKeysPath); + File menuFile = new File(this.menuFilePath); + depictFile = new File(this.depictKeysPath); + File productButtonFile = new File(this.productButtonPath); + + List dataKeys = Files.readAllLines(dataFile.toPath(), + Charset.defaultCharset()); + List depictKeys = Files.readAllLines(depictFile.toPath(), + Charset.defaultCharset()); + List lines = Files.readAllLines(menuFile.toPath(), + Charset.defaultCharset()); + List productButtonKeys = Files.readAllLines( + productButtonFile.toPath(), Charset.defaultCharset()); + Map menuTextMap = getMenuTextMap(productButtonKeys); + Map dataKeyMap = getSubstitutionMap(dataKeys); + + MenuEntry subMenuEntry = null; + + int subMenuCount = -1; + for (String line : lines) { + line = line.trim(); + if (line.startsWith("submenu")) { + subMenuCount++; + if (line.contains("&")) { + line = line.replace("&", "&&"); + } + + String[] parts = line.split(":"); + String text = parts[1].replace("\"", ""); + + subMenuEntry = new MenuEntry(); + subMenuEntry.setFile(null); + subMenuEntry.setType(MenuEntryType.Submenu); + subMenuEntry.setText(text.trim()); + + subMenuList.add(subMenuEntry); + } else if (line.startsWith("title")) { + String[] parts = line.split(":"); + String text = parts[1].replace("\"", ""); + titleMenuEntry = new MenuEntry(); + titleMenuEntry.setFile(null); + titleMenuEntry.setType(MenuEntryType.Title); + titleMenuEntry.setText(text); + titleMenuEntry.setId(text); + subMenuList.get(subMenuCount).addMenuEntry(titleMenuEntry); + } else if (line.startsWith("productButton")) { + String[] parts = line.split(":"); + MenuEntry me = new MenuEntry(); + me.setFile(null); + me.setType(MenuEntryType.ProductButton); + /* + * There are certain productButtons in the NCO menu data + * keys in the (25000 range) that have data keys that don't + * map to anything. This results in those menu items not + * being created. The site will need to fix this after + * generating the new menus. + */ + String dataKey = parts[1].trim().substring(0, 4); + StringBuilder subValue = new StringBuilder(); + // Find the matching value + for (String depictKeyLine : depictKeys) { + if (depictKeyLine.trim().startsWith(dataKey)) { + String[] depictKeyParts = depictKeyLine + .split("\\|"); + me.setText(menuTextMap.get(dataKey)); + me.setId(depictKeyParts[6].trim()); + subMenuList.get(subMenuCount).addMenuEntry(me); + + String[] subParts = depictKeyParts[2].split(","); + MenuEntry subEntry = new MenuEntry(); + subEntry.setFile(null); + subEntry.setType(MenuEntryType.Substitute); + subEntry.setKey(WMO); + + for (String subPart : subParts) { + for (String key : dataKeyMap.keySet()) { + if (key.startsWith(subPart)) { + subValue.append(dataKeyMap.get(key)) + .append(","); + break; + } + } + } + + String subValueStr = subValue.toString(); + subValueStr = StringUtils.removeEnd(subValueStr, + ","); + subEntry.setValue(subValueStr); + me.addMenuEntry(subEntry); + break; + } + } + } else if (line.startsWith("endSubmenu")) { + // subMenuList.add(subMenuEntry); + MenuEntry closedSubMenu = subMenuList.remove(subMenuCount); + subMenuCount--; + if (subMenuCount == -1) { + menuXml.addMenuEntry(closedSubMenu); + } else { + subMenuList.get(subMenuCount).addMenuEntry( + closedSubMenu); + } + } else if (line.startsWith("separator")) { + MenuEntry sep = new MenuEntry(); + sep.setFile(null); + sep.setType(MenuEntryType.Separator); + sep.setId("Separator" + sepCounter++); + subMenuList.get(subMenuCount).addMenuEntry(sep); + } + } + + // Set output file name + String inputFileName = menuFile.getName(); + String outputFileName = null; + if (inputFileName.equals(CPC_FILE)) { + outputFileName = RedbookMenuSubscriber.CPC_MENU_FILE; + } else if (inputFileName.equals(HPC_FILE)) { + outputFileName = RedbookMenuSubscriber.HPC_MENU_FILE; + } else if (inputFileName.equals(NCO_FILE)) { + outputFileName = RedbookMenuSubscriber.NCO_MENU_FILE; + } else if (inputFileName.equals(HAZARDS_FILE)) { + outputFileName = RedbookMenuSubscriber.HAZARD_MENU_FILE; + } else if (inputFileName.equals(MARINE_FILE)) { + outputFileName = RedbookMenuSubscriber.MPC_MENU_FILE; + } else if (inputFileName.equals(UPPER_AIR_FILE)) { + outputFileName = RedbookMenuSubscriber.UA_MENU_FILE; + } else { + throw new IOException("Error processing file"); + } + + marshaller.marshal(menuXml, new File(OUTPUT_PATH + outputFileName)); + } catch (Exception e) { + System.err.println("Error occurred processing file: " + + menuFilePath); + e.printStackTrace(); + } + } + + /** + * Get a map of menu keys to menu text. + * + * @param productButtonKeys + * List of strings from redbookProductButtons.txt + * + * @return Map for key -> menu text + */ + private Map getMenuTextMap(List productButtonKeys) { + Map menuTextMap = new HashMap(); + for (String line : productButtonKeys) { + line = line.trim(); + // Skip comment lines + if (line.startsWith("#")) { + continue; + } + + String[] parts = line.split("\\|"); + menuTextMap.put(parts[0].trim(), parts[2].trim()); + } + + return menuTextMap; + } + + /** + * Get a map of key to substitution values. + * + * @param dataKeys + * List of strings from the redbookDataKeys.txt file + * @returnMap for key -> substitution string + */ + private Map getSubstitutionMap(List dataKeys) { + Map dataKeyMap = new HashMap(); + for (String line : dataKeys) { + line = line.trim(); + // Skip comment lines + if (line.startsWith("#")) { + continue; + } + String[] parts = line.split("\\|"); + dataKeyMap.put(parts[0].trim(), parts[10].substring(0, 6)); + } + + return dataKeyMap; + } + + public String getDataKeysPath() { + return dataKeysPath; + } + + public void setDataKeysPath(String dataKeysPath) { + this.dataKeysPath = dataKeysPath; + } + + public String getDepictKeysPath() { + return depictKeysPath; + } + + public void setDepictKeysPath(String depictKeysPath) { + this.depictKeysPath = depictKeysPath; + } + + public String getMenuFilePath() { + return menuFilePath; + } + + public void setMenuFilePath(String menuFilePath) { + this.menuFilePath = menuFilePath; + } + + public void setProductButtonPath(String productButtonPath) { + this.productButtonPath = productButtonPath; + } + + public String getProductButtonPath() { + return this.productButtonPath; + } + + private void createContext() { + Class[] classes = new Class[] { MenuEntry.class, MenuEntryType.class, + RedbookMenusXML.class }; + + try { + jax = JAXBContext.newInstance(classes); + this.marshaller = jax.createMarshaller(); + this.marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) { + + if (args.length != 2) { + System.err.println("Unexpected Arguments"); + System.err + .println("Expecting local NDM directory and NDM File Menu Name"); + return; + } + + String dirPath = args[0]; + if (!dirPath.endsWith(File.separator)) { + dirPath = dirPath.concat(File.separator); + } + String menuFile = dirPath + args[1]; + String dataKeysFile = dirPath + "redbookDataKeys.txt"; + String depictKeysFile = dirPath + "redbookDepictKeys.txt"; + String productButtonFile = dirPath + "redbookProductButtons.txt"; + + NdmMenuConverter converter = new NdmMenuConverter(); + converter.setDataKeysPath(dataKeysFile); + converter.setMenuFilePath(menuFile); + converter.setDepictKeysPath(depictKeysFile); + converter.setProductButtonPath(productButtonFile); + converter.convert(); + + } +}