Java Code Examples for net.sf.mpxj.ProjectFile#getTaskByUniqueID()

The following examples show how to use net.sf.mpxj.ProjectFile#getTaskByUniqueID() . 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: BasicTest.java    From mpxj with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Common task calendar tests.
 *
 * @param mpx project file
 */
private void validateTaskCalendars(ProjectFile mpx)
{
   Task task = mpx.getTaskByUniqueID(Integer.valueOf(2));
   ProjectCalendar calendar = task.getCalendar();
   assertNull(calendar);

   task = mpx.getTaskByUniqueID(Integer.valueOf(3));
   calendar = task.getCalendar();
   assertEquals("Standard", calendar.getName());
   assertFalse(calendar.isDerived());

   task = mpx.getTaskByUniqueID(Integer.valueOf(4));
   calendar = task.getCalendar();
   assertEquals("Base Calendar", calendar.getName());
   assertFalse(calendar.isDerived());
}
 
Example 2
Source File: ViewStateReader.java    From mpxj with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Entry point for processing saved view state.
 *
 * @param file project file
 * @param varData view state var data
 * @param fixedData view state fixed data
 * @throws IOException
 */
public void process(ProjectFile file, Var2Data varData, byte[] fixedData) throws IOException
{
   Props props = getProps(varData);
   //System.out.println(props);
   if (props != null)
   {
      String viewName = MPPUtility.removeAmpersands(props.getUnicodeString(VIEW_NAME));
      byte[] listData = props.getByteArray(VIEW_CONTENTS);
      List<Integer> uniqueIdList = new ArrayList<>();
      if (listData != null)
      {
         for (int index = 0; index < listData.length; index += 4)
         {
            Integer uniqueID = Integer.valueOf(MPPUtility.getInt(listData, index));

            //
            // Ensure that we have a valid task, and that if we have and
            // ID of zero, this is the first task shown.
            //
            if (file.getTaskByUniqueID(uniqueID) != null && (uniqueID.intValue() != 0 || index == 0))
            {
               uniqueIdList.add(uniqueID);
            }
         }
      }

      int filterID = MPPUtility.getShort(fixedData, 128);

      ViewState state = new ViewState(file, viewName, uniqueIdList, filterID);
      file.getViews().setViewState(state);
   }
}
 
Example 3
Source File: SplitTaskTest.java    From mpxj with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Exercise split task functionality.
 *
 * @throws Exception
 */
@Test public void testSplits1() throws Exception
{
   ProjectFile mpp = new MPPReader().read(MpxjTestData.filePath("splits9a.mpp"));

   Task task = mpp.getTaskByUniqueID(Integer.valueOf(1));
   assertNull(task.getSplits());

   List<DateRange> taskSplits;
   for (int taskID = 2; taskID <= 6; taskID++)
   {
      task = mpp.getTaskByUniqueID(Integer.valueOf(taskID));
      taskSplits = task.getSplits();
      assertEquals(3, taskSplits.size());
      testSplit(taskSplits.get(0), "06/01/2006 08:00", "11/01/2006 17:00");
      //testSplit(taskSplits.get(1), "12/01/2006 08:00", "12/01/2006 17:00");
      testSplit(taskSplits.get(2), "13/01/2006 08:00", "20/01/2006 17:00");
   }

   for (int taskID = 7; taskID <= 13; taskID++)
   {
      task = mpp.getTaskByUniqueID(Integer.valueOf(taskID));
      taskSplits = task.getSplits();
      assertEquals(5, taskSplits.size());
      testSplit(taskSplits.get(0), "06/01/2006 08:00", "09/01/2006 17:00");
      //testSplit(taskSplits.get(1), "10/01/2006 08:00", "13/01/2006 17:00");
      testSplit(taskSplits.get(2), "16/01/2006 08:00", "20/01/2006 17:00");
      //testSplit(taskSplits.get(3), "23/01/2006 08:00", "24/01/2006 17:00");
      testSplit(taskSplits.get(4), "25/01/2006 08:00", "27/01/2006 17:00");
   }
}
 
Example 4
Source File: SplitTaskTest.java    From mpxj with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Exercise split task functionality.
 *
 * @throws Exception
 */
@Test public void testSplits2() throws Exception
{
   ProjectFile mpp = new MPPReader().read(MpxjTestData.filePath("splits9b.mpp"));
   List<DateRange> taskSplits;
   Task task = mpp.getTaskByUniqueID(Integer.valueOf(1));
   taskSplits = task.getSplits();
   assertEquals(5, taskSplits.size());
   testSplit(taskSplits.get(0), "26/08/2005 08:00", "29/08/2005 17:00");
   testSplit(taskSplits.get(1), "30/08/2005 08:00", "01/09/2005 17:00");
   testSplit(taskSplits.get(2), "02/09/2005 08:00", "06/09/2005 17:00");
   testSplit(taskSplits.get(3), "07/09/2005 08:00", "09/09/2005 17:00");
   testSplit(taskSplits.get(4), "12/09/2005 08:00", "16/09/2005 17:00");

   task = mpp.getTaskByUniqueID(Integer.valueOf(3));
   assertNull(task.getSplits());

   task = mpp.getTaskByUniqueID(Integer.valueOf(4));
   taskSplits = task.getSplits();
   assertEquals(3, taskSplits.size());
   testSplit(taskSplits.get(0), "29/08/2005 08:00", "31/08/2005 17:00");
   testSplit(taskSplits.get(1), "01/09/2005 08:00", "02/09/2005 17:00");
   testSplit(taskSplits.get(2), "05/09/2005 08:00", "13/09/2005 17:00");

   task = mpp.getTaskByUniqueID(Integer.valueOf(5));
   taskSplits = task.getSplits();
   assertEquals(3, taskSplits.size());
   testSplit(taskSplits.get(0), "26/08/2005 08:00", "07/09/2005 17:00");
   testSplit(taskSplits.get(1), "08/09/2005 08:00", "09/09/2005 17:00");
   testSplit(taskSplits.get(2), "12/09/2005 08:00", "26/09/2005 17:00");
}
 
Example 5
Source File: BasicTest.java    From mpxj with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * The files sample.mpp, sample98.mpp and sample.xml contain identical
 * data produced by MS Project. This method contains validation tests
 * on that data to ensure that the three file formats are being read
 * consistently.
 *
 * @param file ProjectFile instance
 */
private void commonTests(ProjectFile file)
{
   //
   // Test the remaining work attribute
   //
   Task task = file.getTaskByUniqueID(Integer.valueOf(2));
   List<ResourceAssignment> assignments = task.getResourceAssignments();
   assertEquals(2, assignments.size());

   for (ResourceAssignment assignment : assignments)
   {
      switch (NumberHelper.getInt(assignment.getResource().getID()))
      {
         case 1:
         {
            assertEquals(200, (int) assignment.getRemainingWork().getDuration());
            assertEquals(TimeUnit.HOURS, assignment.getRemainingWork().getUnits());
            break;
         }

         case 2:
         {
            assertEquals(300, (int) assignment.getRemainingWork().getDuration());
            assertEquals(TimeUnit.HOURS, assignment.getRemainingWork().getUnits());
            break;
         }

         default:
         {
            assertTrue("Unexpected resource", false);
            break;
         }
      }
   }
}
 
Example 6
Source File: BasicTest.java    From mpxj with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Test retrieval of WBS information.
 *
 * @throws Exception
 */
@Test public void testWBS() throws Exception
{
   ProjectFile mpp = new MPPReader().read(MpxjTestData.filePath("legacy/sample98.mpp"));
   Task task = mpp.getTaskByUniqueID(Integer.valueOf(2));
   assertEquals("Second Task", task.getName());
   assertEquals("1.1", task.getWBS());

   mpp = new MPPReader().read(MpxjTestData.filePath("legacy/sample.mpp"));
   task = mpp.getTaskByUniqueID(Integer.valueOf(2));
   assertEquals("Second Task", task.getName());
   assertEquals("1.1", task.getWBS());
}
 
Example 7
Source File: MppSubprojectTest.java    From mpxj with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Tests the various fields needed to read in subprojects.
 *
 * @param mpp The ProjectFile being tested.
 * @param isMPP is the source an MPP file
 * @throws Exception
 */
private void testSubprojects(ProjectFile mpp, boolean isMPP) throws Exception
{
   Task taskNormal = mpp.getTaskByUniqueID(Integer.valueOf(1));
   Task taskSubprojectA = mpp.getTaskByUniqueID(Integer.valueOf(2));
   Task taskSubprojectB = mpp.getTaskByUniqueID(Integer.valueOf(3));

   assertEquals("Normal Task", taskNormal.getName());
   assertEquals("SubprojectA-9", taskSubprojectA.getName());
   assertEquals("SubprojectB-9", taskSubprojectB.getName());

   // Subproject A
   SubProject subprojectA = taskSubprojectA.getSubProject();
   assertNotNull(subprojectA);
   final String expectedFilenameA = "\\SubprojectA-9.mpp";
   //assertEquals(expectedFilenameA, subprojectA.getDosFileName());
   assertTrue(expectedFilenameA.indexOf(subprojectA.getFileName()) != -1);
   //subprojectA.getDosFullPath(); don't need to test
   assertTrue(subprojectA.getFullPath().indexOf(expectedFilenameA) != -1);
   assertEquals(Integer.valueOf(2), subprojectA.getTaskUniqueID());

   //assertEquals(null, taskSubprojectA.getSubprojectName());  // TODO: why is this null?
   assertFalse(taskSubprojectA.getSubprojectReadOnly());

   if (isMPP)
   {
      assertEquals(Integer.valueOf(8388608), subprojectA.getUniqueIDOffset()); // MPD needs to be fixed
      assertEquals(Integer.valueOf(8388608), taskSubprojectA.getSubprojectTasksUniqueIDOffset());
      assertEquals(Integer.valueOf(0), taskSubprojectA.getSubprojectTaskUniqueID());
   }
}
 
Example 8
Source File: PrimaveraXERFileReader.java    From mpxj with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * This is a convenience method which allows all projects in an
 * XER file to be read in a single pass.
 *
 * @param is input stream
 * @param linkCrossProjectRelations add Relation links that cross ProjectFile boundaries
 * @return list of ProjectFile instances
 * @throws MPXJException
 */
public List<ProjectFile> readAll(InputStream is, boolean linkCrossProjectRelations) throws MPXJException
{
   try
   {
      m_tables = new HashMap<>();
      m_numberFormat = new DecimalFormat();

      processFile(is);

      List<Row> rows = getRows("project", null, null);
      List<ProjectFile> result = new ArrayList<>(rows.size());
      List<ExternalPredecessorRelation> externalPredecessors = new ArrayList<>();
      for (Row row : rows)
      {
         setProjectID(row.getInt("proj_id"));
         m_reader = new PrimaveraReader(m_taskUdfCounters, m_resourceUdfCounters, m_assignmentUdfCounters, m_resourceFields, m_wbsFields, m_taskFields, m_assignmentFields, m_aliases, m_matchPrimaveraWBS, m_wbsIsFullPath);
         ProjectFile project = readProject();
         externalPredecessors.addAll(m_reader.getExternalPredecessors());

         result.add(project);
      }

      if (linkCrossProjectRelations)
      {
         for (ExternalPredecessorRelation externalRelation : externalPredecessors)
         {
            Task predecessorTask;
            // we could aggregate the project task id maps but that's likely more work
            // than just looping through the projects
            for (ProjectFile proj : result)
            {
               predecessorTask = proj.getTaskByUniqueID(externalRelation.getSourceUniqueID());
               if (predecessorTask != null)
               {
                  Relation relation = externalRelation.getTargetTask().addPredecessor(predecessorTask, externalRelation.getType(), externalRelation.getLag());
                  relation.setUniqueID(externalRelation.getUniqueID());
                  break;
               }
            }
            // if predecessorTask not found the external task is outside of the file so ignore
         }
      }

      return result;
   }

   finally
   {
      m_tables = null;
      m_numberFormat = null;
      m_reader = null;
   }
}
 
Example 9
Source File: BasicTest.java    From mpxj with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * This method exercises task notes, ensuring that
 * embedded commas and quotes are handled correctly.
 *
 * @throws Exception
 */
@Test public void testTaskNotes() throws Exception
{
   String notes1 = "Notes, containing a comma. Done.";
   String notes2 = "Notes \"containing embedded quotes\" Done.";
   String notes3 = "Notes, \"containing embedded quotes, and comma's too.\" Done.";
   String notes4 = "\"Notes containing embedded quotes as first and last chars. Done.\"";
   String notes5 = "Normal unquoted notes. Done.";

   ProjectFile file1 = new ProjectFile();

   Task task1 = file1.addTask();
   task1.setName("Test Task 1");
   task1.setDuration(Duration.getInstance(10, TimeUnit.DAYS));
   task1.setStart(new Date());
   task1.setNotes(notes1);

   Task task2 = file1.addTask();
   task2.setName("Test Task 2");
   task2.setDuration(Duration.getInstance(10, TimeUnit.DAYS));
   task2.setStart(new Date());
   task2.setNotes(notes2);

   Task task3 = file1.addTask();
   task3.setName("Test Task 3");
   task3.setDuration(Duration.getInstance(10, TimeUnit.DAYS));
   task3.setStart(new Date());
   task3.setNotes(notes3);

   Task task4 = file1.addTask();
   task4.setName("Test Task 4");
   task4.setDuration(Duration.getInstance(10, TimeUnit.DAYS));
   task4.setStart(new Date());
   task4.setNotes(notes4);

   Task task5 = file1.addTask();
   task5.setName("Test Task 5");
   task5.setDuration(Duration.getInstance(10, TimeUnit.DAYS));
   task5.setStart(new Date());
   task5.setNotes(notes5);

   File out = File.createTempFile("junit", ".mpx");
   new MPXWriter().write(file1, out);

   ProjectFile file2 = new MPXReader().read(out);
   String notes;
   Task task1a = file2.getTaskByUniqueID(task1.getUniqueID());
   notes = task1a.getNotes();
   assertEquals(notes1, notes);

   Task task2a = file2.getTaskByUniqueID(task2.getUniqueID());
   notes = task2a.getNotes();
   assertEquals(notes2, notes);

   Task task3a = file2.getTaskByUniqueID(task3.getUniqueID());
   notes = task3a.getNotes();
   assertEquals(notes3, notes);

   Task task4a = file2.getTaskByUniqueID(task4.getUniqueID());
   notes = task4a.getNotes();
   assertEquals(notes4, notes);

   Task task5a = file2.getTaskByUniqueID(task5.getUniqueID());
   notes = task5a.getNotes();
   assertEquals(notes5, notes);

   out.deleteOnExit();
}
 
Example 10
Source File: BasicTest.java    From mpxj with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Write a file with embedded line break (\r and \n) characters in
 * various text fields. Ensure that a valid file is written,
 * and that it can be read successfully.
 *
 * @throws Exception
 */
@Test public void testEmbeddedLineBreaks() throws Exception
{
   //
   // Create a simple MPX file
   //
   SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
   ProjectFile file = new ProjectFile();
   file.addDefaultBaseCalendar();

   ProjectProperties properties = file.getProjectProperties();
   properties.setComments("Project Header Comments: Some\rExample\nText\r\nWith\n\rBreaks");
   properties.setStartDate(df.parse("01/01/2003"));

   Resource resource1 = file.addResource();
   resource1.setName("Resource1: Some\rExample\nText\r\nWith\n\rBreaks");
   resource1.setNotes("Resource1 Notes: Some\rExample\nText\r\nWith\n\rBreaks");

   Task task1 = file.addTask();
   task1.setName("Task1: Some\rExample\nText\r\nWith\n\rBreaks");
   task1.setNotes("Task1 Notes: Some\rExample\nText\r\nWith\n\rBreaks");

   //
   // Write the file
   //
   File out = File.createTempFile("junit", ".mpx");
   new MPXWriter().write(file, out);

   //
   // Ensure we can read it successfully
   //
   file = new MPXReader().read(out);
   assertEquals(1, file.getTasks().size());
   assertEquals(1, file.getResources().size());

   properties = file.getProjectProperties();
   assertEquals("Project Header Comments: Some\nExample\nText\nWith\nBreaks", properties.getComments());

   task1 = file.getTaskByUniqueID(Integer.valueOf(1));
   assertEquals("Task1: Some\nExample\nText\nWith\nBreaks", task1.getName());
   assertEquals("Task1 Notes: Some\nExample\nText\nWith\nBreaks", task1.getNotes());

   resource1 = file.getResourceByUniqueID(Integer.valueOf(1));
   assertEquals("Resource1: Some\nExample\nText\nWith\nBreaks", resource1.getName());
   assertEquals("Resource1 Notes: Some\nExample\nText\nWith\nBreaks", resource1.getNotes());
   out.deleteOnExit();
}
 
Example 11
Source File: MppResourceTest.java    From mpxj with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Tests fields related to Resource Assignments.
 *
 * @param mpp The ProjectFile being tested.
 */
private void testResourceAssignments(ProjectFile mpp)
{
   Integer intOne = Integer.valueOf(1);
   DateFormat df = new SimpleDateFormat("dd/MM/yyyy");

   List<ResourceAssignment> listResourceAssignments = mpp.getResourceAssignments();

   ResourceAssignment ra = listResourceAssignments.get(0);
   // id
   assertEquals(intOne, ra.getResource().getID());
   assertEquals(intOne, ra.getResourceUniqueID());

   // start and finish
   assertEquals("25/08/2006", df.format(ra.getStart()));
   assertEquals("29/08/2006", df.format(ra.getFinish()));

   // task
   Task task = ra.getTask();
   assertEquals(intOne, task.getID());
   assertEquals(Integer.valueOf(2), task.getUniqueID());
   assertEquals("Task A", task.getName());

   // units
   assertEquals(Double.valueOf(100), ra.getUnits());

   // work and remaining work
   Duration dur24Hours = Duration.getInstance(24, TimeUnit.HOURS);
   assertEquals(dur24Hours, ra.getWork());
   assertEquals(dur24Hours, ra.getRemainingWork());

   //
   // Baseline values
   //
   assertEquals("01/01/2006", df.format(ra.getBaselineStart()));
   assertEquals("02/01/2006", df.format(ra.getBaselineFinish()));
   assertEquals(1, ra.getBaselineCost().intValue());
   assertEquals("2.0h", ra.getBaselineWork().toString());

   // Task 2
   // contour
   ResourceAssignment ra2 = listResourceAssignments.get(3);
   assertEquals(WorkContour.TURTLE, ra2.getWorkContour());

   // Task 3
   // completed
   task = mpp.getTaskByUniqueID(Integer.valueOf(4));
   assertEquals("Completed Task", task.getName());
   ResourceAssignment ra3 = task.getResourceAssignments().get(0);

   //
   // Actual values
   //
   // actual start 26/08/06
   assertEquals("26/08/2006", df.format(ra3.getActualStart()));
   // actual finish 29/08/06
   assertEquals("29/08/2006", df.format(ra3.getActualFinish()));
   // actual work 16h
   assertEquals("16.0h", ra3.getActualWork().toString());
   // actual cost $800
   assertEquals(800, ra3.getActualCost().intValue());

}