Examples of HhDatumRaw


Examples of net.sf.chellow.hhimport.HhDatumRaw

                  Character status = null;
                  String statusString = vals[i + 1].trim();
                  if (statusString.length() > 0) {
                    status = statusString.charAt(0);
                  }
                  hhData.add(new HhDatumRaw(mpanCore,
                      isImport, isKwh, startDate,
                      new BigDecimal(bigDecimal), status));
                }
                startDate = startDate.getNext();
              }
View Full Code Here

Examples of net.sf.chellow.hhimport.HhDatumRaw

              hhStartDate.getDate()).uniqueResult() != null) {
        throw new UserException(doc(inv),
            "There's already an HH datum with this time.");
      }
      List<HhDatumRaw> data = new ArrayList<HhDatumRaw>();
      data.add(new HhDatumRaw(channel.getSupplyGeneration().getMpans()
          .iterator().next().getCore().toString(), channel
          .getIsImport(), channel.getIsKwh(), hhStartDate, value,
          status));
      HhDatum.insert(data.iterator(), Arrays
          .asList(new Boolean[] { Boolean.FALSE }));
View Full Code Here

Examples of net.sf.chellow.hhimport.HhDatumRaw

  }

  @SuppressWarnings({ "unchecked", "deprecation" })
  public void addHhData(List<HhDatumRaw> dataRaw) throws HttpException {
    // long now = System.currentTimeMillis();
    HhDatumRaw firstRawDatum = dataRaw.get(0);
    HhDatumRaw lastRawDatum = dataRaw.get(dataRaw.size() - 1);
    // Debug.print("First dr = " + firstRawDatum + " cond dr " +
    // lastRawDatum);
    List<HhDatum> data = (List<HhDatum>) Hiber
        .session()
        .createQuery(
            "from HhDatum datum where datum.channel = :channel and "
                + "datum.startDate.date >= :startDate and datum.startDate.date <= :finishDate order by datum.startDate.date")
        .setEntity("channel", this)
        .setTimestamp("startDate",
            firstRawDatum.getStartDate().getDate())
        .setTimestamp("finishDate",
            lastRawDatum.getStartDate().getDate()).list();
    HhStartDate siteCheckFrom = null;
    HhStartDate siteCheckTo = null;
    HhStartDate notActualFrom = null;
    HhStartDate notActualTo = null;
    HhStartDate deleteMissingFrom = null;
    HhStartDate deleteMissingTo = null;
    HhStartDate lastAdditionDate = null;
    HhStartDate prevStartDate = null;
    int missing = 0;
    BigDecimal originalDatumValue = new BigDecimal(0);
    char originalDatumStatus = Character.UNASSIGNED;
    Connection con = Hiber.session().connection();
    PreparedStatement stmt;
    try {
      stmt = con
          .prepareStatement("INSERT INTO hh_datum VALUES (nextval('hh_datum_id_sequence'), ?, ?, ?, ?)");
    } catch (SQLException e1) {
      throw new InternalException(e1);
    }
    int batchSize = 0;
    for (int i = 0; i < dataRaw.size(); i++) {
      // Debug.print("Start processing hh: " + (System.currentTimeMillis()
      // - now));
      boolean added = false;
      boolean altered = false;
      HhDatumRaw datumRaw = dataRaw.get(i);
      HhDatum datum = null;

      if (i - missing < data.size()) {
        datum = data.get(i - missing);
        if (!datumRaw.getStartDate().equals(datum.getStartDate())) {
          datum = null;
        }
      }
      if (datum == null) {
        // Debug.print("About to save datum: "
        // + (System.currentTimeMillis() - now));
        try {
          stmt.setLong(1, getId());

          stmt.setTimestamp(2, new Timestamp(datumRaw.getStartDate()
              .getDate().getTime()));
          stmt.setBigDecimal(3, datumRaw.getValue());
          stmt.setString(4, Character.toString(datumRaw.getStatus()));
          stmt.addBatch();
          batchSize++;
        } catch (SQLException e) {
          throw new InternalException(e);
        }
        // Debug.print("Saved datum: "
        // + (System.currentTimeMillis() - now));
        // Hiber.flush();
        lastAdditionDate = datumRaw.getStartDate();
        added = true;
        missing++;
        if (deleteMissingFrom == null) {
          deleteMissingFrom = datumRaw.getStartDate();
        }
        deleteMissingTo = datumRaw.getStartDate();
        // Debug.print("Resolved missing: "
        // + (System.currentTimeMillis() - now));
      } else if (datumRaw.getValue().doubleValue() != datum.getValue()
          .doubleValue() || datumRaw.getStatus() != datum.getStatus()) {
        // Debug.print("About to update datum: " + datum + " with " +
        // datumRaw + " "
        // + (System.currentTimeMillis() - now));
        originalDatumValue = datum.getValue();
        originalDatumStatus = datum.getStatus();
        datum.update(datumRaw.getValue(), datumRaw.getStatus());
        Hiber.flush();
        altered = true;
      }
      // Debug.print("About to see if changed: "
      // + (System.currentTimeMillis() - now));
      if (added || altered) {
        if (siteCheckFrom == null) {
          siteCheckFrom = datumRaw.getStartDate();
        }
        siteCheckTo = datumRaw.getStartDate();
        if (datumRaw.getValue().doubleValue() < 0) {
          addSnag(ChannelSnag.SNAG_NEGATIVE, datumRaw.getStartDate(),
              datumRaw.getStartDate());
        } else if (altered && originalDatumValue.doubleValue() < 0) {
          deleteSnag(ChannelSnag.SNAG_NEGATIVE,
              datumRaw.getStartDate());
        }
        if (HhDatum.ACTUAL != datumRaw.getStatus()) {
          if (notActualFrom == null) {
            notActualFrom = datumRaw.getStartDate();
          }
          notActualTo = datumRaw.getStartDate();
        } else if (altered && originalDatumStatus != HhDatum.ACTUAL) {
          deleteSnag(ChannelSnag.SNAG_ESTIMATED,
              datumRaw.getStartDate());
        }
      }
      if (lastAdditionDate != null
          && (lastAdditionDate.equals(prevStartDate) || batchSize > 100)) {
        // Debug.print("About to execute batch "
        // + (System.currentTimeMillis() - now));
        try {
          stmt.executeBatch();
          // Debug.print("Added  lines.");
          batchSize = 0;
        } catch (SQLException e) {
          throw new InternalException(e);
        }
        lastAdditionDate = null;
      }
      if (siteCheckTo != null && siteCheckTo.equals(prevStartDate)) {
        // Debug.print("About to do site check: "
        // + (System.currentTimeMillis() - now));
        siteCheck(siteCheckFrom, siteCheckTo);
        siteCheckFrom = null;
        siteCheckTo = null;
        // Debug.print("Finished site check: "
        // + (System.currentTimeMillis() - now));
      }
      if (notActualTo != null && notActualTo.equals(prevStartDate)) {
        // Debug.print("Started not actual: "
        // + (System.currentTimeMillis() - now));
        addSnag(ChannelSnag.SNAG_ESTIMATED, notActualFrom, notActualTo);
        // Debug.print("Finished not actual: "
        // + (System.currentTimeMillis() - now));
        notActualFrom = null;
        notActualTo = null;
      }
      if (deleteMissingTo != null
          && deleteMissingTo.equals(prevStartDate)) {
        // Debug.print("Starting resolvedMissing: "
        // + (System.currentTimeMillis() - now));
        deleteSnag(ChannelSnag.SNAG_MISSING, deleteMissingFrom,
            deleteMissingTo);
        deleteMissingFrom = null;
        deleteMissingTo = null;
        // Debug.print("Finished resolveMissing: "
        // + (System.currentTimeMillis() - now));
      }
      prevStartDate = datumRaw.getStartDate();
    }
    if (lastAdditionDate != null && lastAdditionDate.equals(prevStartDate)) {
      // Debug.print("About to execute batch 2: "
      // + (System.currentTimeMillis() - now));
      try {
View Full Code Here

Examples of net.sf.chellow.hhimport.HhDatumRaw

    }
 
    @SuppressWarnings("unchecked")
    public void execute(Connection con) throws HttpException {
      // long now = System.currentTimeMillis();
      HhDatumRaw firstRawDatum = dataRaw.get(0);
      HhDatumRaw lastRawDatum = dataRaw.get(dataRaw.size() - 1);
      // Debug.print("First dr = " + firstRawDatum + " cond dr " +
      // lastRawDatum);
      List<HhDatum> data = (List<HhDatum>) Hiber
          .session()
          .createQuery(
              "from HhDatum datum where datum.channel = :channel and "
                  + "datum.startDate.date >= :startDate and datum.startDate.date <= :finishDate order by datum.startDate.date")
          .setEntity("channel", channel)
          .setTimestamp("startDate",
              firstRawDatum.getStartDate().getDate())
          .setTimestamp("finishDate",
              lastRawDatum.getStartDate().getDate()).list();
      HhStartDate siteCheckFrom = null;
      HhStartDate siteCheckTo = null;
      HhStartDate notActualFrom = null;
      HhStartDate notActualTo = null;
      HhStartDate deleteMissingFrom = null;
      HhStartDate deleteMissingTo = null;
      HhStartDate lastAdditionDate = null;
      HhStartDate prevStartDate = null;
      int missing = 0;
      BigDecimal originalDatumValue = new BigDecimal(0);
      char originalDatumStatus = Character.UNASSIGNED;
        PreparedStatement stmt;
        try {
          stmt = con
              .prepareStatement("INSERT INTO hh_datum VALUES (nextval('hh_datum_id_sequence'), ?, ?, ?, ?)");
          Statement st = con.createStatement();
        st.executeUpdate("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE READ WRITE");        } catch (SQLException e1) {
          throw new InternalException(e1);
        }
        int batchSize = 0;
        for (int i = 0; i < dataRaw.size(); i++) {
          // Debug.print("Start processing hh: " + (System.currentTimeMillis()
          // - now));
          boolean added = false;
          boolean altered = false;
          HhDatumRaw datumRaw = dataRaw.get(i);
          HhDatum datum = null;

          if (i - missing < data.size()) {
            datum = data.get(i - missing);
            if (!datumRaw.getStartDate().equals(datum.getStartDate())) {
              datum = null;
            }
          }
          if (datum == null) {
            // Debug.print("About to save datum: "
            // + (System.currentTimeMillis() - now));
            try {
              stmt.setLong(1, channel.getId());

              stmt.setTimestamp(2, new Timestamp(datumRaw.getStartDate()
                  .getDate().getTime()));
              stmt.setBigDecimal(3, datumRaw.getValue());
              stmt.setString(4, Character.toString(datumRaw.getStatus()));
              stmt.addBatch();
              batchSize++;
            } catch (SQLException e) {
              throw new InternalException(e);
            }
            // Debug.print("Saved datum: "
            // + (System.currentTimeMillis() - now));
            // Hiber.flush();
            lastAdditionDate = datumRaw.getStartDate();
            added = true;
            missing++;
            if (deleteMissingFrom == null) {
              deleteMissingFrom = datumRaw.getStartDate();
            }
            deleteMissingTo = datumRaw.getStartDate();
            // Debug.print("Resolved missing: "
            // + (System.currentTimeMillis() - now));
          } else if (datumRaw.getValue().doubleValue() != datum.getValue()
              .doubleValue() || datumRaw.getStatus() != datum.getStatus()) {
            // Debug.print("About to update datum: " + datum + " with " +
            // datumRaw + " "
            // + (System.currentTimeMillis() - now));
            originalDatumValue = datum.getValue();
            originalDatumStatus = datum.getStatus();
            datum.update(datumRaw.getValue(), datumRaw.getStatus());
            Hiber.flush();
            altered = true;
          }
          // Debug.print("About to see if changed: "
          // + (System.currentTimeMillis() - now));
          if (added || altered) {
            if (siteCheckFrom == null) {
              siteCheckFrom = datumRaw.getStartDate();
            }
            siteCheckTo = datumRaw.getStartDate();
            if (datumRaw.getValue().doubleValue() < 0) {
              channel.addSnag(ChannelSnag.SNAG_NEGATIVE, datumRaw.getStartDate(),
                  datumRaw.getStartDate());
            } else if (altered && originalDatumValue.doubleValue() < 0) {
              channel.deleteSnag(ChannelSnag.SNAG_NEGATIVE,
                  datumRaw.getStartDate());
            }
            if (HhDatum.ACTUAL != datumRaw.getStatus()) {
              if (notActualFrom == null) {
                notActualFrom = datumRaw.getStartDate();
              }
              notActualTo = datumRaw.getStartDate();
            } else if (altered && originalDatumStatus != HhDatum.ACTUAL) {
              channel.deleteSnag(ChannelSnag.SNAG_ESTIMATED,
                  datumRaw.getStartDate());
            }
          }
          if (lastAdditionDate != null
              && (lastAdditionDate.equals(prevStartDate) || batchSize > 100)) {
            // Debug.print("About to execute batch "
            // + (System.currentTimeMillis() - now));
            try {
              stmt.executeBatch();
              Statement st = con.createStatement();
              st.executeUpdate("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE READ WRITE");   
              // Debug.print("Added  lines.");
              batchSize = 0;
            } catch (SQLException e) {
              throw new InternalException(e);
            }
            lastAdditionDate = null;
          }
          if (siteCheckTo != null && siteCheckTo.equals(prevStartDate)) {
            // Debug.print("About to do site check: "
            // + (System.currentTimeMillis() - now));
            channel.siteCheck(siteCheckFrom, siteCheckTo);
            siteCheckFrom = null;
            siteCheckTo = null;
            // Debug.print("Finished site check: "
            // + (System.currentTimeMillis() - now));
          }
          if (notActualTo != null && notActualTo.equals(prevStartDate)) {
            // Debug.print("Started not actual: "
            // + (System.currentTimeMillis() - now));
            channel.addSnag(ChannelSnag.SNAG_ESTIMATED, notActualFrom, notActualTo);
            // Debug.print("Finished not actual: "
            // + (System.currentTimeMillis() - now));
            notActualFrom = null;
            notActualTo = null;
          }
          if (deleteMissingTo != null
              && deleteMissingTo.equals(prevStartDate)) {
            // Debug.print("Starting resolvedMissing: "
            // + (System.currentTimeMillis() - now));
            channel.deleteSnag(ChannelSnag.SNAG_MISSING, deleteMissingFrom,
                deleteMissingTo);
            deleteMissingFrom = null;
            deleteMissingTo = null;
            // Debug.print("Finished resolveMissing: "
            // + (System.currentTimeMillis() - now));
          }
          prevStartDate = datumRaw.getStartDate();
        }
        if (lastAdditionDate != null && lastAdditionDate.equals(prevStartDate)) {
          // Debug.print("About to execute batch 2: "
          // + (System.currentTimeMillis() - now));
          try {
View Full Code Here

Examples of net.sf.chellow.hhimport.HhDatumRaw

      throws HttpException {
    if (!rawData.hasNext()) {
      return;
    }
    Calendar cal = MonadDate.getCalendar();
    HhDatumRaw datum = rawData.next();
    String mpanCoreStr = datum.getMpanCore();
    MpanCore mpanCore = MpanCore.getMpanCore(mpanCoreStr);
    SupplyGeneration generation = mpanCore.getSupply().getGeneration(
        datum.getStartDate());
    if (generation == null) {
      throw new UserException(
          "This datum is either before or after the supply: "
              + datum.toString() + ".");
    }
    long previousDate = datum.getStartDate().getDate().getTime();
    boolean isImport = datum.getIsImport();
    boolean isKwh = datum.getIsKwh();
    Channel channel = generation.getChannel(isImport, isKwh);
    if (channel == null) {
      throw new UserException("There is no channel for the datum: "
          + datum.toString() + ".");
    }
    HhStartDate genFinishDate = generation.getFinishDate();
    List<HhDatumRaw> data = new ArrayList<HhDatumRaw>();
    data.add(datum);
    // HhDatumRaw firstDatum = datum;
    if (!rawData.hasNext()) {
      // batchSize = data.size();
      channel.addHhData(data);
    }
    while (rawData.hasNext() && !halt.get(0)) {
      datum = rawData.next();
      Date startDate = datum.getStartDate().getDate();
      if (data.size() > 1000
          || !(mpanCoreStr.equals(datum.getMpanCore())
              && datum.getIsImport() == isImport
              && datum.getIsKwh() == isKwh && startDate.getTime() == HhStartDate
              .getNext(cal, previousDate))
          || (genFinishDate != null && genFinishDate.getDate()
              .before(startDate))) {
        // batchSize = data.size();
        channel.addHhData(data);
        Hiber.commit();
        Hiber.close();
        Hiber.setReadWrite();
        data.clear();
        mpanCoreStr = datum.getMpanCore();
        mpanCore = MpanCore.getMpanCore(mpanCoreStr);
        generation = mpanCore.getSupply().getGeneration(
            datum.getStartDate());
        if (generation == null) {
          throw new UserException(
              "This datum is either before or after the supply: "
                  + datum.toString() + ".");
        }
        isImport = datum.getIsImport();
        isKwh = datum.getIsKwh();
        channel = generation.getChannel(isImport, isKwh);
        if (channel == null) {
          throw new UserException(
              "There is no channel for the datum: "
                  + datum.toString() + ".");
        }
        genFinishDate = generation.getFinishDate();
      }
      data.add(datum);
      previousDate = startDate.getTime();
View Full Code Here

Examples of net.sf.chellow.hhimport.HhDatumRaw

      if (!inv.isValid()) {
        throw new UserException();
      }
      try {
        List<HhDatumRaw> dataRaw = new ArrayList<HhDatumRaw>();
        dataRaw.add(new HhDatumRaw(channel.getSupplyGeneration()
            .getMpans().iterator().next().getCore().toString(),
            channel.getIsImport(), channel.getIsKwh(), startDate,
            value, status));
        channel.addHhData(dataRaw);
        Hiber.commit();
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.