Java Code Examples for net.sf.mpxj.Task#getDuration()

The following examples show how to use net.sf.mpxj.Task#getDuration() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MSPDIReader.java    From mpxj with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * When projectmanager.com exports schedules as MSPDI (via Aspose tasks)
 * they do not have finish dates, just a start date and a duration.
 * This method populates finish dates.
 *
 * @param task task to validate
 */
private void validateFinishDate(Task task)
{
   if (task.getFinish() == null)
   {
      Date startDate = task.getStart();
      if (startDate != null)
      {
         if (task.getMilestone())
         {
            task.setFinish(startDate);
         }
         else
         {
            Duration duration = task.getDuration();
            if (duration != null)
            {
               ProjectCalendar calendar = task.getEffectiveCalendar();
               task.setFinish(calendar.getDate(startDate, duration, false));
            }
         }
      }
   }
}
 
Example 2
Source File: TaskDurationsTest.java    From mpxj with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Test duration units.
 *
 * @param file project file
 * @param reader reader used to parse the file
 * @param project project file
 */
private void testDurationUnits(File file, ProjectReader reader, ProjectFile project)
{
   TimeUnit[] units = (NumberHelper.getInt(project.getProjectProperties().getMppFileType()) == 8 || reader instanceof MPXReader) ? UNITS_PROJECT98 : UNITS_PROJECT2000;
   int maxIndex = reader instanceof MPXReader ? 3 : 10;

   int taskID = 11;
   for (int fieldIndex = 1; fieldIndex <= maxIndex; fieldIndex++)
   {
      for (int unitsIndex = 0; unitsIndex < units.length; unitsIndex++)
      {
         Task task = project.getTaskByID(Integer.valueOf(taskID));
         String expectedTaskName = "Duration" + fieldIndex + " - Task " + unitsIndex;
         assertEquals(expectedTaskName, task.getName());
         Duration duration = task.getDuration(fieldIndex);
         assertEquals(file.getName() + " " + expectedTaskName, units[unitsIndex], duration.getUnits());
         ++taskID;
      }
   }
}
 
Example 3
Source File: ProjectFileImporter.java    From ganttproject with GNU General Public License v3.0 5 votes vote down vote up
private Function<Task, Pair<TimeDuration, TimeDuration>> findDurationFunction(Task t, StringBuilder reportBuilder) {
  if (t.getStart() != null && t.getFinish() != null) {
    return DURATION_FROM_START_FINISH;
  }
  reportBuilder.append("start+finish not found");
  if (t.getStart() != null && t.getDuration() != null) {
    return DURATION_FROM_START_AND_DURATION;
  }
  reportBuilder.append(", start+duration not found");
  return null;
}
 
Example 4
Source File: MpxjQuery.java    From mpxj with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * This method lists all tasks defined in the file.
 *
 * @param file MPX file
 */
private static void listTasks(ProjectFile file)
{
   SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm z");

   for (Task task : file.getTasks())
   {
      Date date = task.getStart();
      String text = task.getStartText();
      String startDate = text != null ? text : (date != null ? df.format(date) : "(no start date supplied)");

      date = task.getFinish();
      text = task.getFinishText();
      String finishDate = text != null ? text : (date != null ? df.format(date) : "(no finish date supplied)");

      Duration dur = task.getDuration();
      text = task.getDurationText();
      String duration = text != null ? text : (dur != null ? dur.toString() : "(no duration supplied)");

      dur = task.getActualDuration();
      String actualDuration = dur != null ? dur.toString() : "(no actual duration supplied)";

      String baselineDuration = task.getBaselineDurationText();
      if (baselineDuration == null)
      {
         dur = task.getBaselineDuration();
         if (dur != null)
         {
            baselineDuration = dur.toString();
         }
         else
         {
            baselineDuration = "(no duration supplied)";
         }
      }

      System.out.println("Task: " + task.getName() + " ID=" + task.getID() + " Unique ID=" + task.getUniqueID() + " (Start Date=" + startDate + " Finish Date=" + finishDate + " Duration=" + duration + " Actual Duration" + actualDuration + " Baseline Duration=" + baselineDuration + " Outline Level=" + task.getOutlineLevel() + " Outline Number=" + task.getOutlineNumber() + " Recurring=" + task.getRecurring() + ")");
   }
   System.out.println();
}
 
Example 5
Source File: TaskDurationsTest.java    From mpxj with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Test the duration values for a task.
 *
 * @param file parent file
 * @param task task
 * @param testIndex index of number being tested
 * @param maxIndex maximum number of custom fields to expect in this file
 */
private void testTaskDurations(File file, Task task, int testIndex, int maxIndex)
{
   for (int index = 1; index <= maxIndex; index++)
   {
      String expectedValue = testIndex == index ? index + ".0d" : "0.0d";
      String actualValue = task.getDuration(index) == null ? "0.0d" : task.getDuration(index).toString();

      assertEquals(file.getName() + " " + task.getName() + " Duration" + index, expectedValue, actualValue);
   }
}
 
Example 6
Source File: MSPDIWriter.java    From mpxj with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * This method writes assignment data to an MSPDI file.
 *
 * @param project Root node of the MSPDI file
 */
private void writeAssignments(Project project)
{
   Project.Assignments assignments = m_factory.createProjectAssignments();
   project.setAssignments(assignments);
   List<Project.Assignments.Assignment> list = assignments.getAssignment();

   for (ResourceAssignment assignment : m_projectFile.getResourceAssignments())
   {
      list.add(writeAssignment(assignment));
   }

   //
   // Check to see if we have any tasks that have a percent complete value
   // but do not have resource assignments. If any exist, then we must
   // write a dummy resource assignment record to ensure that the MSPDI
   // file shows the correct percent complete amount for the task.
   //
   ProjectConfig config = m_projectFile.getProjectConfig();
   boolean autoUniqueID = config.getAutoAssignmentUniqueID();
   if (!autoUniqueID)
   {
      config.setAutoAssignmentUniqueID(true);
   }

   for (Task task : m_projectFile.getTasks())
   {
      double percentComplete = NumberHelper.getDouble(task.getPercentageComplete());
      if (percentComplete != 0 && task.getResourceAssignments().isEmpty() == true)
      {
         ResourceAssignment dummy = new ResourceAssignment(m_projectFile, task);
         Duration duration = task.getDuration();
         if (duration == null)
         {
            duration = Duration.getInstance(0, TimeUnit.HOURS);
         }
         double durationValue = duration.getDuration();
         TimeUnit durationUnits = duration.getUnits();
         double actualWork = (durationValue * percentComplete) / 100;
         double remainingWork = durationValue - actualWork;

         dummy.setResourceUniqueID(NULL_RESOURCE_ID);
         dummy.setWork(duration);
         dummy.setActualWork(Duration.getInstance(actualWork, durationUnits));
         dummy.setRemainingWork(Duration.getInstance(remainingWork, durationUnits));

         // Without this, MS Project will mark a 100% complete milestone as 99% complete
         if (percentComplete == 100 && duration.getDuration() == 0)
         {
            dummy.setActualFinish(task.getActualStart());
         }

         list.add(writeAssignment(dummy));
      }
   }

   config.setAutoAssignmentUniqueID(autoUniqueID);
}
 
Example 7
Source File: ProgressRecord.java    From mpxj with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override public void process(Context context)
{
   Double totalCost = getDouble(4);
   Double costToDate = getDouble(5);
   Double remainingCost = Double.valueOf(NumberHelper.getDouble(totalCost) - NumberHelper.getDouble(costToDate));
   Duration totalFloat = getDuration(12);
   if ("-".equals(getString(11)))
   {
      totalFloat = Duration.getInstance(-totalFloat.getDuration(), TimeUnit.DAYS);
   }

   Task task = context.getTask(getString(0));
   task.setActualStart(getDate(1));
   task.setActualFinish(getDate(2));
   task.setRemainingDuration(getDuration(3));
   task.setCost(getDouble(4));
   task.setRemainingCost(remainingCost);
   task.setCost(1, getDouble(6));
   task.setEarlyStart(getDate(7));
   task.setEarlyFinish(getDate(8));
   task.setLateStart(getDate(9));
   task.setLateFinish(getDate(10));
   task.setTotalSlack(totalFloat);

   Date start = task.getActualStart() == null ? task.getEarlyStart() : task.getActualStart();
   Date finish = task.getActualFinish() == null ? task.getEarlyFinish() : task.getActualFinish();
   double percentComplete = 0;

   if (task.getActualFinish() == null)
   {
      Duration duration = task.getDuration();
      Duration remainingDuration = task.getRemainingDuration();
      if (duration != null && remainingDuration != null)
      {
         double durationValue = duration.getDuration();
         double remainingDurationValue = remainingDuration.getDuration();
         if (durationValue != 0 && remainingDurationValue < durationValue)
         {
            percentComplete = ((durationValue - remainingDurationValue) * 100.0) / durationValue;
         }
      }
   }
   else
   {
      percentComplete = 100.0;
   }

   task.setStart(start);
   task.setFinish(finish);
   task.setPercentageComplete(Double.valueOf(percentComplete));
}
 
Example 8
Source File: SDEFWriter.java    From mpxj with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Write a task.
 *
 * @param record task instance
 * @throws IOException
 */
private void writeTask(Task record) throws IOException
{
   m_buffer.setLength(0);
   if (!record.getSummary())
   {
      m_buffer.append("ACTV ");
      m_buffer.append(SDEFmethods.rset(record.getUniqueID().toString(), 10) + " ");
      m_buffer.append(SDEFmethods.lset(record.getName(), 30) + " ");

      // Following just makes certain we have days for duration, as per USACE spec.
      Duration dd = record.getDuration();
      double duration = dd.getDuration();
      if (dd.getUnits() != TimeUnit.DAYS)
      {
         dd = Duration.convertUnits(duration, dd.getUnits(), TimeUnit.DAYS, m_minutesPerDay, m_minutesPerWeek, m_daysPerMonth);
      }
      Double days = Double.valueOf(dd.getDuration() + 0.5); // Add 0.5 so half day rounds up upon truncation
      Integer est = Integer.valueOf(days.intValue());
      m_buffer.append(SDEFmethods.rset(est.toString(), 3) + " "); // task duration in days required by USACE

      String conType = "ES "; // assume early start
      Date conDate = record.getEarlyStart();
      int test = record.getConstraintType().getValue(); // test for other types
      if (test == 1 || test == 3 || test == 6 || test == 7 || test == 9)
      {
         conType = "LF "; // see ConstraintType enum for definitions
         conDate = record.getLateFinish();
      }
      m_buffer.append(m_formatter.format(conDate).toUpperCase() + " "); // Constraint Date
      m_buffer.append(conType); // Constraint Type
      if (record.getCalendar() == null)
      {
         m_buffer.append("1 ");
      }
      else
      {
         m_buffer.append(SDEFmethods.lset(record.getCalendar().getUniqueID().toString(), 1) + " ");
      }
      // skipping hammock code in here
      // use of text fields for extra USACE data is suggested at my web site: www.geocomputer.com
      // not documented on how to do this here, so I need to comment out at present
      //	      m_buffer.append(SDEFmethods.Lset(record.getText1(), 3) + " ");
      //	      m_buffer.append(SDEFmethods.Lset(record.getText2(), 4) + " ");
      //	      m_buffer.append(SDEFmethods.Lset(record.getText3(), 4) + " ");
      //	      m_buffer.append(SDEFmethods.Lset(record.getText4(), 6) + " ");
      //	      m_buffer.append(SDEFmethods.Lset(record.getText5(), 6) + " ");
      //	      m_buffer.append(SDEFmethods.Lset(record.getText6(), 2) + " ");
      //	      m_buffer.append(SDEFmethods.Lset(record.getText7(), 1) + " ");
      //	      m_buffer.append(SDEFmethods.Lset(record.getText8(), 30) + " ");
      m_writer.println(m_buffer.toString());
      m_eventManager.fireTaskWrittenEvent(record);
   }
}
 
Example 9
Source File: SynchroReader.java    From mpxj with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Extract data for a single task.
 *
 * @param parent task parent
 * @param row Synchro task data
 */
private void processTask(ChildTaskContainer parent, MapRow row) throws IOException
{
   Task task = parent.addTask();
   task.setName(row.getString("NAME"));
   task.setGUID(row.getUUID("UUID"));
   task.setText(1, row.getString("ID"));
   task.setDuration(row.getDuration("PLANNED_DURATION"));
   task.setRemainingDuration(row.getDuration("REMAINING_DURATION"));
   task.setHyperlink(row.getString("URL"));
   task.setPercentageComplete(row.getDouble("PERCENT_COMPLETE"));
   task.setNotes(getNotes(row.getRows("COMMENTARY")));
   task.setMilestone(task.getDuration() != null && task.getDuration().getDuration() == 0);

   ProjectCalendar calendar = m_calendarMap.get(row.getUUID("CALENDAR_UUID"));
   if (calendar != m_project.getDefaultCalendar())
   {
      task.setCalendar(calendar);
   }

   switch (row.getInteger("STATUS").intValue())
   {
      case 1: // Planned
      {
         task.setStart(row.getDate("PLANNED_START"));
         if (task.getStart() != null && task.getDuration() != null)
         {
            task.setFinish(task.getEffectiveCalendar().getDate(task.getStart(), task.getDuration(), false));
         }
         break;
      }

      case 2: // Started
      {
         task.setActualStart(row.getDate("ACTUAL_START"));
         task.setStart(task.getActualStart());
         task.setFinish(row.getDate("ESTIMATED_FINISH"));
         if (task.getFinish() == null)
         {
            task.setFinish(row.getDate("PLANNED_FINISH"));
         }
         break;
      }

      case 3: // Finished
      {
         task.setActualStart(row.getDate("ACTUAL_START"));
         task.setActualFinish(row.getDate("ACTUAL_FINISH"));
         task.setPercentageComplete(Double.valueOf(100.0));
         task.setStart(task.getActualStart());
         task.setFinish(task.getActualFinish());
         break;
      }
   }

   setConstraints(task, row);

   processChildTasks(task, row);

   m_taskMap.put(task.getGUID(), task);

   List<MapRow> predecessors = row.getRows("PREDECESSORS");
   if (predecessors != null && !predecessors.isEmpty())
   {
      m_predecessorMap.put(task, predecessors);
   }

   List<MapRow> resourceAssignmnets = row.getRows("RESOURCE_ASSIGNMENTS");
   if (resourceAssignmnets != null && !resourceAssignmnets.isEmpty())
   {
      processResourceAssignments(task, resourceAssignmnets);
   }
}