/** * Retrieve a duration in the form required by Primavera. * * @param duration Duration instance * @return formatted duration */ private Double getDuration(Duration duration) { if (duration.getUnits() != TimeUnit.HOURS) { duration = duration.convertUnits(TimeUnit.HOURS, m_projectFile.getProjectProperties()); } return Double.valueOf(duration.getDuration()); }
/** * This utility method calculates the difference in working time between two dates, given the * context of a task. * * @param task parent task * @param date1 first date * @param date2 second date * @param format required format for the resulting duration * @return difference in working time between the two dates */ public static Duration getVariance(Task task, Date date1, Date date2, TimeUnit format) { Duration variance = null; if (date1 != null && date2 != null) { ProjectCalendar calendar = task.getCalendar(); if (calendar == null) { calendar = task.getParentFile().getCalendar(); } if (calendar != null) { variance = calendar.getWork(date1, date2, format); } } if (variance == null) { variance = Duration.getInstance(0, format); } return (variance); }
/** * Exercise slack duration functionality. * * @throws Exception */ public void testSlack() throws Exception { ProjectFile mpp = new MPPReader().read(m_basedir + "/slack9.mpp"); Task task = mpp.getTaskByID(Integer.valueOf(1)); assertEquals("Task 1", task.getName()); assertEquals(Duration.getInstance(8, TimeUnit.HOURS), task.getDuration()); assertEquals(Duration.getInstance(40, TimeUnit.HOURS), task.getStartSlack()); assertEquals(Duration.getInstance(40, TimeUnit.HOURS), task.getFinishSlack()); assertEquals(Duration.getInstance(0, TimeUnit.HOURS), task.getFreeSlack()); assertEquals(Duration.getInstance(40, TimeUnit.HOURS), task.getTotalSlack()); task = mpp.getTaskByID(Integer.valueOf(2)); assertEquals("Task 2", task.getName()); assertEquals(Duration.getInstance(5, TimeUnit.DAYS), task.getDuration()); assertEquals(Duration.getInstance(5, TimeUnit.DAYS), task.getStartSlack()); assertEquals(Duration.getInstance(5, TimeUnit.DAYS), task.getFinishSlack()); assertEquals(Duration.getInstance(0, TimeUnit.DAYS), task.getFreeSlack()); assertEquals(Duration.getInstance(5, TimeUnit.DAYS), task.getTotalSlack()); task = mpp.getTaskByID(Integer.valueOf(3)); assertEquals("Task 3", task.getName()); assertEquals(Duration.getInstance(5, TimeUnit.DAYS), task.getDuration()); assertEquals(Duration.getInstance(10, TimeUnit.DAYS), task.getStartSlack()); assertEquals(Duration.getInstance(10, TimeUnit.DAYS), task.getFinishSlack()); assertEquals(Duration.getInstance(5, TimeUnit.DAYS), task.getFreeSlack()); assertEquals(Duration.getInstance(10, TimeUnit.DAYS), task.getTotalSlack()); task = mpp.getTaskByID(Integer.valueOf(4)); assertEquals("Task 4", task.getName()); assertEquals(Duration.getInstance(5, TimeUnit.DAYS), task.getDuration()); assertEquals(Duration.getInstance(5, TimeUnit.DAYS), task.getStartSlack()); assertEquals(Duration.getInstance(5, TimeUnit.DAYS), task.getFinishSlack()); assertEquals(Duration.getInstance(0, TimeUnit.DAYS), task.getFreeSlack()); assertEquals(Duration.getInstance(5, TimeUnit.DAYS), task.getTotalSlack()); task = mpp.getTaskByID(Integer.valueOf(5)); assertEquals("Milestone 1", task.getName()); assertEquals(Duration.getInstance(0, TimeUnit.DAYS), task.getDuration()); assertEquals(Duration.getInstance(5, TimeUnit.DAYS), task.getStartSlack()); assertEquals(Duration.getInstance(5, TimeUnit.DAYS), task.getFinishSlack()); assertEquals(Duration.getInstance(5, TimeUnit.DAYS), task.getFreeSlack()); assertEquals(Duration.getInstance(5, TimeUnit.DAYS), task.getTotalSlack()); task = mpp.getTaskByID(Integer.valueOf(6)); assertEquals("Task 5", task.getName()); assertEquals(Duration.getInstance(5, TimeUnit.DAYS), task.getDuration()); assertEquals(Duration.getInstance(-1, TimeUnit.DAYS), task.getStartSlack()); assertEquals(Duration.getInstance(-1, TimeUnit.DAYS), task.getFinishSlack()); assertEquals(Duration.getInstance(0, TimeUnit.DAYS), task.getFreeSlack()); assertEquals(Duration.getInstance(-1, TimeUnit.DAYS), task.getTotalSlack()); task = mpp.getTaskByID(Integer.valueOf(7)); assertEquals("Task 6", task.getName()); assertEquals(Duration.getInstance(5, TimeUnit.DAYS), task.getDuration()); assertEquals(Duration.getInstance(-1, TimeUnit.DAYS), task.getStartSlack()); assertEquals(Duration.getInstance(-1, TimeUnit.DAYS), task.getFinishSlack()); assertEquals(Duration.getInstance(0, TimeUnit.DAYS), task.getFreeSlack()); assertEquals(Duration.getInstance(-1, TimeUnit.DAYS), task.getTotalSlack()); task = mpp.getTaskByID(Integer.valueOf(8)); assertEquals("Task 7", task.getName()); assertEquals(Duration.getInstance(5, TimeUnit.DAYS), task.getDuration()); assertEquals(Duration.getInstance(-1, TimeUnit.DAYS), task.getStartSlack()); assertEquals(Duration.getInstance(-1, TimeUnit.DAYS), task.getFinishSlack()); assertEquals(Duration.getInstance(0, TimeUnit.DAYS), task.getFreeSlack()); assertEquals(Duration.getInstance(-1, TimeUnit.DAYS), task.getTotalSlack()); task = mpp.getTaskByID(Integer.valueOf(9)); assertEquals("Task 8", task.getName()); assertEquals(Duration.getInstance(5, TimeUnit.DAYS), task.getDuration()); assertEquals(Duration.getInstance(4, TimeUnit.DAYS), task.getStartSlack()); assertEquals(Duration.getInstance(4, TimeUnit.DAYS), task.getFinishSlack()); assertEquals(Duration.getInstance(4, TimeUnit.DAYS), task.getFreeSlack()); assertEquals(Duration.getInstance(4, TimeUnit.DAYS), task.getTotalSlack()); task = mpp.getTaskByID(Integer.valueOf(10)); assertEquals("Milestone 2", task.getName()); assertEquals(Duration.getInstance(0, TimeUnit.DAYS), task.getDuration()); assertEquals(Duration.getInstance(-1, TimeUnit.DAYS), task.getStartSlack()); assertEquals(Duration.getInstance(0, TimeUnit.DAYS), task.getFinishSlack()); assertEquals(Duration.getInstance(0, TimeUnit.DAYS), task.getFreeSlack()); assertEquals(Duration.getInstance(-1, TimeUnit.DAYS), task.getTotalSlack()); task = mpp.getTaskByID(Integer.valueOf(11)); assertEquals("Task 9", task.getName()); assertEquals(Duration.getInstance(5, TimeUnit.DAYS), task.getDuration()); assertEquals(Duration.getInstance(0, TimeUnit.DAYS), task.getStartSlack()); assertEquals(Duration.getInstance(0, TimeUnit.DAYS), task.getFinishSlack()); assertEquals(Duration.getInstance(0, TimeUnit.DAYS), task.getFreeSlack()); assertEquals(Duration.getInstance(0, TimeUnit.DAYS), task.getTotalSlack()); task = mpp.getTaskByID(Integer.valueOf(12)); assertEquals("Task 10", task.getName()); assertEquals(Duration.getInstance(5, TimeUnit.DAYS), task.getDuration()); assertEquals(Duration.getInstance(0, TimeUnit.DAYS), task.getStartSlack()); assertEquals(Duration.getInstance(0, TimeUnit.DAYS), task.getFinishSlack()); assertEquals(Duration.getInstance(0, TimeUnit.DAYS), task.getFreeSlack()); assertEquals(Duration.getInstance(0, TimeUnit.DAYS), task.getTotalSlack()); task = mpp.getTaskByID(Integer.valueOf(13)); assertEquals("Task 11", task.getName()); assertEquals(Duration.getInstance(5, TimeUnit.DAYS), task.getDuration()); assertEquals(Duration.getInstance(0, TimeUnit.DAYS), task.getStartSlack()); assertEquals(Duration.getInstance(0, TimeUnit.DAYS), task.getFinishSlack()); assertEquals(Duration.getInstance(0, TimeUnit.DAYS), task.getFreeSlack()); assertEquals(Duration.getInstance(0, TimeUnit.DAYS), task.getTotalSlack()); task = mpp.getTaskByID(Integer.valueOf(14)); assertEquals("Task 12", task.getName()); assertEquals(Duration.getInstance(5, TimeUnit.DAYS), task.getDuration()); assertEquals(Duration.getInstance(6, TimeUnit.DAYS), task.getStartSlack()); assertEquals(Duration.getInstance(6, TimeUnit.DAYS), task.getFinishSlack()); assertEquals(Duration.getInstance(6, TimeUnit.DAYS), task.getFreeSlack()); assertEquals(Duration.getInstance(6, TimeUnit.DAYS), task.getTotalSlack()); task = mpp.getTaskByID(Integer.valueOf(15)); assertEquals("Milestone 3", task.getName()); assertEquals(Duration.getInstance(0, TimeUnit.DAYS), task.getDuration()); assertEquals(Duration.getInstance(0, TimeUnit.DAYS), task.getStartSlack()); assertEquals(Duration.getInstance(0, TimeUnit.DAYS), task.getFinishSlack()); assertEquals(Duration.getInstance(0, TimeUnit.DAYS), task.getFreeSlack()); assertEquals(Duration.getInstance(0, TimeUnit.DAYS), task.getTotalSlack()); }