Ejemplo n.º 1
0
  private static void parseExchangeRate(final XMLStreamReader reader) {
    Map<String, String> elementMap = new HashMap<>();

    /* still at start of the exchange rate.  Need to know when end is reached */
    QName parsingElement = reader.getName();

    try {
      while (reader.hasNext()) {

        int event = reader.next();

        switch (event) {
          case XMLStreamConstants.START_ELEMENT:
            String element = reader.getLocalName();
            elementMap.put(element.intern(), reader.getElementText());
            break;
          case XMLStreamConstants.END_ELEMENT:
            if (reader.getName().equals(parsingElement)) {
              logger.finest("Found the end of the exchange rate");

              try {
                String key = elementMap.get("key");

                if (key != null && key.length() == 6) {

                  Engine e = EngineFactory.getEngine(EngineFactory.DEFAULT);

                  CurrencyNode cOne = e.getCurrency(key.substring(0, 3));
                  CurrencyNode cTwo = e.getCurrency(key.substring(3, 6));

                  // jGnash 1.x would hold onto old exchange rates for deleted commodities
                  if (cOne != null && cTwo != null) {
                    BigDecimal rate = new BigDecimal(elementMap.get("rate"));

                    EngineFactory.getEngine(EngineFactory.DEFAULT)
                        .setExchangeRate(cOne, cTwo, rate);

                    logger.log(
                        Level.FINE,
                        "Set ExchangeRate {0}:{1}",
                        new Object[] {key, rate.toString()});
                  }
                }

              } catch (Exception e) {
                logger.log(Level.SEVERE, e.toString(), e);
              }
              return;
            }
            break;
          default:
            break;
        }
      }
    } catch (XMLStreamException e) {
      logger.severe("Error importing exchange rate");
      logger.log(Level.SEVERE, e.toString(), e);
    }
  }
Ejemplo n.º 2
0
    @Override
    public void run() {
      try {
        SecurityNode clone = (SecurityNode) sNode.clone();

        Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);

        CurrencyNode currencyNode = engine.getCurrency(cNode);

        if (currencyNode != null) {
          clone.setReportedCurrencyNode(currencyNode);
          engine.updateCommodity(sNode, clone);
        }
      } catch (CloneNotSupportedException e) {
        logger.log(Level.SEVERE, e.toString(), e);
      }
    }
Ejemplo n.º 3
0
  private static void parseCurrencyNode(final XMLStreamReader reader) {
    Map<String, String> elementMap = new HashMap<>();

    /* still at start of the element.  Need to know when end is reached */
    QName parsingElement = reader.getName();

    try {
      while (reader.hasNext()) {
        int event = reader.next();

        switch (event) {
          case XMLStreamConstants.START_ELEMENT:
            String element = reader.getLocalName();
            elementMap.put(element, reader.getElementText());
            break;
          case XMLStreamConstants.END_ELEMENT:
            if (reader.getName().equals(parsingElement)) {
              logger.finest("Found the end of a CurrencyNode");

              Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);

              if (engine.getCurrency(elementMap.get("symbol")) == null) {

                CurrencyNode node = new CurrencyNode();

                node.setSymbol(elementMap.get("symbol"));
                node.setDescription(elementMap.get("description"));
                node.setPrefix(elementMap.get("prefix"));
                node.setSuffix(elementMap.get("suffix"));
                node.setScale(Byte.parseByte(elementMap.get("scale")));

                engine.addCurrency(node);
              }
              return;
            }
            break;

          default:
            break;
        }
      }
    } catch (XMLStreamException e) {
      logger.log(Level.SEVERE, e.toString(), e);
    }
  }
Ejemplo n.º 4
0
  public static void doImport(final String filename) {

    Import imp = new Import();

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();

    try {
      Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);

      CurrencyNode defaultCurrency = DefaultCurrencies.getDefault();

      if (engine.getCurrency(defaultCurrency.getSymbol()) == null) {
        engine.addCurrency(defaultCurrency);
        engine.setDefaultCurrency(defaultCurrency);
      }

      /* read commodity data first */
      try (InputStream input = new BufferedInputStream(new FileInputStream(new File(filename)))) {
        XMLStreamReader reader = inputFactory.createXMLStreamReader(input, "UTF-8");
        imp.importCommodities(reader);
        reader.close();
      }

      /* Run commodity generation cleanup threads */
      ExecutorService es = Executors.newSingleThreadExecutor();

      for (Iterator<Runnable> i = workQueue.iterator(); i.hasNext(); ) {
        es.execute(i.next());
        i.remove();
      }

      es.shutdown(); // shutdown after threads are complete

      while (!es.isTerminated()) {
        imp.getLogger().info("Waiting for commodity cleanup threads to complete");
        try {
          es.awaitTermination(2, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
          imp.getLogger().log(Level.SEVERE, e.toString(), e);
        }
      }

      imp.getLogger().info("Commodity cleanup threads complete");

      /* read account data first */
      try (InputStream input = new BufferedInputStream(new FileInputStream(new File(filename)))) {
        XMLStreamReader reader = inputFactory.createXMLStreamReader(input, "UTF-8");
        imp.importAccounts(reader);
        reader.close();
      }

      imp.getLogger().info("Running account cleanup threads");

      /* Run account generation cleanup threads */
      es = Executors.newSingleThreadExecutor();

      for (Iterator<Runnable> i = workQueue.iterator(); i.hasNext(); ) {
        es.execute(i.next());
        i.remove();
      }

      es.shutdown(); // shutdown after threads are complete

      while (!es.isTerminated()) {
        imp.getLogger().info("Waiting for account cleanup threads to complete");
        try {
          es.awaitTermination(2, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
          imp.getLogger().log(Level.SEVERE, e.toString(), e);
        }
      }

      imp.getLogger().info("Account cleanup threads complete");

      /* import transactions */
      try (InputStream input = new BufferedInputStream(new FileInputStream(new File(filename)))) {
        XMLStreamReader reader = inputFactory.createXMLStreamReader(input, "UTF-8");
        imp.importTransactions(reader);
        reader.close();
      }

      /* Lock accounts after transactions have been added */
      imp.lockAccounts();

    } catch (XMLStreamException | IOException e) {
      imp.getLogger().log(Level.SEVERE, e.toString(), e);
    }
  }