/**
  * Loads an schedule item from a datasource
  *
  * @param dataSource the datasource object where to look up the schedule item
  * @param rowKey the rowkey of the schedule item to retrieve
  * @return
  */
 public static ScheduleItem load(DataSource dataSource, Schedule schedule, String rowKey) {
   Table table = dataSource.getTable(ScheduleItem.tableName);
   Get get = ScheduleItem.fillGetObject(table.createGetObject(rowKey));
   Result result = table.get(get);
   ScheduleItem r = ScheduleItem.fromResultObject(schedule, result);
   return r;
 }
  /**
   * builds an empty SchedueItem object from the string representation of its rowkey. Leaves the
   * task and task container (null)
   *
   * @param k
   * @return
   */
  public static ScheduleItem fromRowKey(Schedule schedule, String k) {

    String[] s = k.split("\\.");
    if (s.length != 3) {
      throw new BIGSException("incorrect schedule item rowkey specification");
    }
    Integer pipelineNumber = new Integer(s[0]);
    Integer stageNumber = new Integer(s[1]);

    ScheduleItem r = null;
    if (schedule != null) {
      if (!schedule.pipelineStage.getStageNumber().equals(stageNumber)
          || !schedule.pipelineStage.pipeline.getPipelineNumber().equals(pipelineNumber)) {
        throw new BIGSException(
            "error reconstructing schedule item rowkey "
                + k
                + " does not correspond to pipeline "
                + schedule.pipelineStage.getPipeline().getPipelineNumber()
                + " / stage "
                + schedule.pipelineStage.getStageNumber());
      }
      r = new ScheduleItem(schedule);
    } else {
      r = new ScheduleItem();
    }

    r.setRowkey(k);
    return r;
  }
  /**
   * Checks if the schedule item is available to start working on it. Returns true if all the
   * schedule item parents are done.
   */
  public boolean canProcess() {
    if (this.isStatusPending()) {
      Boolean anyParentNotFinished = false;
      for (ScheduleItem parent : this.getParents()) {
        if (parent == null) {
          throw new BIGSException("parent for schedule item " + this.getRowkey() + " not found");
        }
        if (!parent.isStatusDone()) {
          anyParentNotFinished = true;
          break;
        }
      }

      return !anyParentNotFinished;
    }
    return false;
  }
 public List<ScheduleItem> getParents() {
   List<ScheduleItem> r = new ArrayList<ScheduleItem>();
   for (String parentRowkey : this.getParentsRowkeys()) {
     ScheduleItem item = this.getSchedule().get(parentRowkey);
     // if the item was not found, try to load it directly from DB as it
     // probably belongs to another stage
     if (item == null) {
       item = cachedParents.get(parentRowkey);
       if (item == null) {
         DataSource dataSource = BIGS.globalProperties.getPreparedDataSource();
         item = ScheduleItem.load(dataSource, null, parentRowkey);
         cachedParents.put(parentRowkey, item);
       }
     }
     r.add(item);
   }
   return r;
 }
 public ScheduleItem clone() {
   ScheduleItem r = new ScheduleItem(this.schedule);
   r.preparedTask = this.preparedTask;
   r.preparedTaskContainer = this.preparedTaskContainer;
   r.rowkey = this.rowkey;
   r.parentsRowkeys = this.parentsRowkeys;
   r.hostnameStored = this.hostnameStored;
   r.uuiStored = this.uuiStored;
   r.lastUpdate = this.lastUpdate;
   r.status = this.status;
   r.elapsedTime = this.elapsedTime;
   r.processState = this.processState;
   r.rowkey = this.rowkey;
   return r;
 }
  /**
   * builds an ScheduleItem object from a Result object obtained from a Scan or Get in the
   * underlying storage
   *
   * @param result
   * @return
   */
  public static ScheduleItem fromResultObject(Schedule schedule, Result result) {
    ScheduleItem r = ScheduleItem.fromRowKey(schedule, result.getRowKey());

    byte[] stat = result.getValue("bigs", "status");
    if (stat != null) {
      r.setStatusFromString(new String(stat));
    }

    byte[] etime = result.getValue("bigs", "elapsedtime");
    if (etime != null) {
      r.setElapsedTime(new Long(new String(etime)));
    }

    byte[] lastupdate = result.getValue("bigs", "lastupdate");
    if (lastupdate != null) {
      r.setLastUpdateFromString(new String(lastupdate));
    }

    byte[] suuid = result.getValue("bigs", "uuid");
    if (suuid != null) {
      r.setUuidStored(new String(suuid));
    }

    byte[] smethod = result.getValue("scheduling", "method");
    if (smethod != null) {
      r.setMethodName(new String(smethod));
    }

    byte[] shostname = result.getValue("bigs", "hostname");
    if (shostname != null) {
      r.setHostnameStored(new String(shostname));
    }

    String parentsIdsString = new String(result.getValue("scheduling", "parents"));

    if (parentsIdsString != null && !parentsIdsString.trim().isEmpty()) {
      r.parentsRowkeys = Text.parseObjectList(parentsIdsString, " ", String.class);
    }

    r.preparedTask = TaskHelper.fromResultObject(result, "scheduling", "task.class", "task.object");
    r.processState = State.fromResultObject(result, "content", "class", "data");
    r.preparedTaskContainer =
        TaskContainer.fromResultObject(
            result, "scheduling", "task.container.class", "task.container.object");
    if (schedule != null) {
      r.preparedTaskContainer.setPipelineStage(schedule.getPipelineStage());
    }
    r.tags = result.getFamilyMap("tags");

    return r;
  }