Migrate to the latest Aspose.Tasks for Java

Aspose.Tasks for Java 8.1.0 is a revamped version of the API that includes considerable changes from usage perspective. Key differences of both the implementations include:

  • Legacy API allowed to set public fields of various project data classes such as Project, Tasks, Resource, etc. while the new implementation introduces Set and Get methods to achieve the same
  • Recalculate methods required to be called manually by user in the legacy API after certain operations such as Resource Assignments. The new Aspose.Tasks for Java API performs most of such calculations automatically without user intervention required.
  • The new API provides manual as well as automatic recalculation modes similar to Microsoft Project (MSP). Manual mode calculates only the necessary fields whereas automatic mode recalculates everything. This eliminates the need of calling recalculating methods manually and improves overall API usage.

This new API is incompatible with the Legacy API and the objective of this article is to help you migrate your code to the new API. The article shows comparative code samples of the Legacy and the new API implementation, and includes:

Setting Default Project Properties

Aspose.Tasks’ legacy API allowed setting default properties of a project by directly setting the public attributes of the Project class. In the new API implementation, however, it exposes Set and Get methods to achieve the same.

Legacy API Code Sample

 1// Create a project instance
 2Project prj = new Project();
 3// Set properties default
 4prj.setScheduleFromStart(true);
 5prj.setStartDate(prj.getStartDate());
 6prj.setDefaultStartTime(prj.getStartDate());
 7prj.setDefaultTaskType(TaskType.FixedDuration);
 8prj.setDefaultStandardRate(15);
 9prj.setDefaultOvertimeRate(12);
10prj.setDefaultTaskEVMethod(EarnedValueMethodType.PercentComplete);
11prj.setDefaultFixedCostAccrual(CostAccrualType.Prorated);
12// Save the project to XML format
13prj.save( "Project4.xml" , SaveFileFormat.XML);

New Aspose.Tasks for Java 8.x Approach

 1Project project = new Project();
 2// Set default properties
 3project.set(Prj.SCHEDULE_FROM_START,new NullableBool(true));
 4project.set(Prj.DEFAULT_START_TIME,project.get(Prj.START_DATE));
 5project.set(Prj.DEFAULT_TASK_TYPE, TaskType.FixedDuration);
 6project.set(Prj.DEFAULT_STANDARD_RATE, 15d);
 7project.set(Prj.DEFAULT_OVERTIME_RATE, 12d);
 8project.set(Prj.DEFAULT_TASK_EV_METHOD, EarnedValueMethodType.PercentComplete);
 9project.set(Prj.DEFAULT_FIXED_COST_ACCRUAL, CostAccrualType.Prorated);
10project.save("ProjectDefaultProps.xml", SaveFileFormat.XML);

Creating and Adding Calendar to Project

In order for proper functionality of Calendar items added to the Project, the legacy API required to recalculate Calendar UIDs. In the new API, however, UIDs recalculation are automatically dealt and there is no need to call the calculations manually.

Legacy API Code Sample

 1// Create a project instance
 2Project prj =  new Project();
 3// Define Calendar
 4Calendar cal1 = new  Calendar();
 5cal1.setName("no info");
 6Calendar cal2 = new  Calendar(1);
 7cal2.setName("no name");
 8Calendar cal3 = new Calendar( "cal3" );
 9prj.getCalendars().add(cal1);
10prj.getCalendars().add(cal2);
11prj.getCalendars().add(cal3);
12prj.calcCalendarUids();
13prj.save( "Project.Xml", SaveFileFormat.XML);

New Aspose.Tasks for Java 8.x Approach

1Project prj = new Project();
2Calendar cal1 = prj.getCalendars().add("no info");
3Calendar cal2 = prj.getCalendars().add("no name");
4Calendar cal3 = prj.getCalendars().add("cal3");
5prj.save("Project.Xml", SaveFileFormat.XML);

Making a Standard Calendar

Recalculating Calendar UIDs are no more required in the new Aspose.Tasks’ API as compared to the legacy API.

Legacy API Code Sample

1Project prj = new  Project();
2//Define Calendar and make it standard
3Calendar cal1 = new Calendar("My Cal");
4Calendar.makeStandardCalendar(cal1);
5prj.getCalendars().add(cal1);
6prj.calcCalendarUids();
7prj.save( "Project.Xml" , SaveFileFormat.XML);

New Aspose.Tasks for Java 8.x Approach

1//Create a project instance
2Project project = new Project();
3//Define Calendar and make it standard
4Calendar cal1 = project.getCalendars().add("My Cal");
5Calendar.makeStandardCalendar(cal1);
6project.save("Project.Xml", SaveFileFormat.XML);

Create and Adding Task to Project

Creating a new task in the legacy API, it required to define a root task, add it to the project as its root task, and then add a new task to this root task as a child. The new API, however, doesn’t need you to define the root task and takes care of it automatically while adding the first task to the project.

Legacy API Code Sample

 1Project project = new Project();
 2Task rootTask = new Task();
 3Task tskGen = new Task("Task1");
 4tskGen.setId(1);
 5tskGen.setUid (1);
 6java.util.Calendar cal = java.util.Calendar.getInstance();
 7cal.set(2014, 04, 23, 0, 0, 0);
 8tskGen.setStart(cal.getTime());
 9cal.set(2014, 04, 25, 0, 0, 0);
10tskGen.setFinish(cal.getTime());
11project.setRootTask (rootTask);
12project.getRootTask().getChildren().add(tskGen);
13project.save("Project.xml", SaveFileFormat.XML);

New Aspose.Tasks for Java 8.x Approach

1Project project = new Project();
2Task task = project.getRootTask().getChildren().add("Task 1");
3java.util.Calendar cal = java.util.Calendar.getInstance();
4cal.set(2014,3,4,0,0,0);
5task.set(Tsk.START, cal.getTime());
6//set task start day
7project.save("CreateTasks.xml", SaveFileFormat.XML);

Create and Add Resource to Project

The set and get methods have been added to the Resource class of the API for manipulating attributes related to a project’s Resource.

Legacy API Code Sample

 1Project project = new Project();
 2Resource res =   new Resource("Res1");
 3res.setId (1);
 4res.setUid(1);
 5java.util.Calendar cal = java.util.Calendar.getInstance();
 6cal.set(2014, 04, 23, 0, 0, 0);
 7res.setStart(cal.getTime());
 8cal.set(2014, 04, 25, 0, 0, 0);
 9res.setFinish(cal.getTime());
10project.getResources().add(res);
11project.save("Project.xml", SaveFileFormat.XML);

New Aspose.Tasks for Java 8.x Approach

1Project project = new Project();
2Resource rsc = project.getResources().add("R1");
3java.util.Calendar cal = java.util.Calendar.getInstance();
4cal.set(2014,3,4,0,0,0);
5rsc.set(Rsc.START, cal.getTime());
6cal.set(2014,3,10,0,0,0);
7rsc.set(Rsc.FINISH, cal.getTime());
8project.save("CreateResource.xml", SaveFileFormat.XML);

Create and Add Resource Assignment to Project

The ResourceAssignment class also introduces the set and get methods similar to other data collections of the API such as Tasks, Resources and Tasks Links.

Legacy API Code Sample

 1Project project = new Project();
 2Task tskRoot =  new Task();
 3Task task = new Task("Task");
 4Resource rsc = new Resource();
 5rsc.setStandardRate(BigDecimal.valueOf(10));
 6rsc.setOvertimeRate(BigDecimal.valueOf(15));
 7ResourceAssignment assignment = new ResourceAssignment(task, rsc);
 8assignment.setUid(1);
 9java.util.Calendar cal = java.util.Calendar.getInstance();
10cal.set(2009, 8, 18, 0, 0, 0);
11assignment.setStart(cal.getTime());
12cal.set(2009,8,20,0,0,0);
13assignment.setFinish(cal.getTime() );
14project.setRootTask(tskRoot);
15project.getRootTask().getChildren().add(task);
16project.getResources().add(rsc);
17project.getResourceAssignments().add(assignment);
18project.save("project.xml", SaveFileFormat.XML);

New Aspose.Tasks for Java 8.x Approach

 1Project project = new Project();
 2Task task = project.getRootTask().getChildren().add("Task");
 3Resource rsc = project.getResources().add("Rsc");
 4rsc.set(Rsc.STANDARD_RATE, BigDecimal.valueOf(10));
 5rsc.set(Rsc.OVERTIME_RATE, BigDecimal.valueOf(15));
 6ResourceAssignment assignment = project.getResourceAssignments().add(task, rsc);
 7java.util.Calendar cal = java.util.Calendar.getInstance();
 8cal.set(2014,3,4,0,0,0);
 9assignment.set(Asn.START,cal.getTime());
10cal.set(2014,3,4,0,0,0);
11assignment.set(Asn.FINISH, cal.getTime());
12project.save("ResourceAssignments.xml", SaveFileFormat.XML);
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.