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
2
3Project prj = new Project();
4
5// Set properties default
6
7prj.setScheduleFromStart(true);
8
9prj.setStartDate(prj.getStartDate());
10
11prj.setDefaultStartTime(prj.getStartDate());
12
13prj.setDefaultTaskType(TaskType.FixedDuration);
14
15prj.setDefaultStandardRate(15);
16
17prj.setDefaultOvertimeRate(12);
18
19prj.setDefaultTaskEVMethod(EarnedValueMethodType.PercentComplete);
20
21prj.setDefaultFixedCostAccrual(CostAccrualType.Prorated);
22
23// Save the project to XML format
24
25prj.save( "Project4.xml" , SaveFileFormat.XML);
New Aspose.Tasks for Java 8.x Approach
1Project project = new Project();
2
3// Set default properties
4
5project.set(Prj.SCHEDULE_FROM_START,new NullableBool(true));
6
7project.set(Prj.DEFAULT_START_TIME,project.get(Prj.START_DATE));
8
9project.set(Prj.DEFAULT_TASK_TYPE, TaskType.FixedDuration);
10
11project.set(Prj.DEFAULT_STANDARD_RATE, 15d);
12
13project.set(Prj.DEFAULT_OVERTIME_RATE, 12d);
14
15project.set(Prj.DEFAULT_TASK_EV_METHOD, EarnedValueMethodType.PercentComplete);
16
17project.set(Prj.DEFAULT_FIXED_COST_ACCRUAL, CostAccrualType.Prorated);
18
19project.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
2
3Project prj = new Project();
4
5// Define Calendar
6
7Calendar cal1 = new Calendar();
8
9cal1.setName("no info");
10
11Calendar cal2 = new Calendar(1);
12
13cal2.setName("no name");
14
15Calendar cal3 = new Calendar( "cal3" );
16
17prj.getCalendars().add(cal1);
18
19prj.getCalendars().add(cal2);
20
21prj.getCalendars().add(cal3);
22
23prj.calcCalendarUids();
24
25prj.save( "Project.Xml", SaveFileFormat.XML);
New Aspose.Tasks for Java 8.x Approach
1Project prj = new Project();
2
3Calendar cal1 = prj.getCalendars().add("no info");
4
5Calendar cal2 = prj.getCalendars().add("no name");
6
7Calendar cal3 = prj.getCalendars().add("cal3");
8
9prj.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
3//Define Calendar and make it standard
4
5Calendar cal1 = new Calendar("My Cal");
6
7Calendar.makeStandardCalendar(cal1);
8
9prj.getCalendars().add(cal1);
10
11prj.calcCalendarUids();
12
13prj.save( "Project.Xml" , SaveFileFormat.XML);
New Aspose.Tasks for Java 8.x Approach
1//Create a project instance
2
3Project project = new Project();
4
5//Define Calendar and make it standard
6
7Calendar cal1 = project.getCalendars().add("My Cal");
8
9Calendar.makeStandardCalendar(cal1);
10
11project.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();
2
3Task rootTask = new Task();
4
5Task tskGen = new Task("Task1");
6
7tskGen.setId(1);
8
9tskGen.setUid (1);
10
11java.util.Calendar cal = java.util.Calendar.getInstance();
12
13cal.set(2014, 04, 23, 0, 0, 0);
14
15tskGen.setStart(cal.getTime());
16
17cal.set(2014, 04, 25, 0, 0, 0);
18
19tskGen.setFinish(cal.getTime());
20
21project.setRootTask (rootTask);
22
23project.getRootTask().getChildren().add(tskGen);
24
25project.save("Project.xml", SaveFileFormat.XML);
New Aspose.Tasks for Java 8.x Approach
1Project project = new Project();
2
3Task task = project.getRootTask().getChildren().add("Task 1");
4
5java.util.Calendar cal = java.util.Calendar.getInstance();
6
7cal.set(2014,3,4,0,0,0);
8
9task.set(Tsk.START, cal.getTime());
10
11//set task start day
12
13project.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();
2
3Resource res = new Resource("Res1");
4
5res.setId (1);
6
7res.setUid(1);
8
9java.util.Calendar cal = java.util.Calendar.getInstance();
10
11cal.set(2014, 04, 23, 0, 0, 0);
12
13res.setStart(cal.getTime());
14
15cal.set(2014, 04, 25, 0, 0, 0);
16
17res.setFinish(cal.getTime());
18
19project.getResources().add(res);
20
21project.save("Project.xml", SaveFileFormat.XML);
New Aspose.Tasks for Java 8.x Approach
1Project project = new Project();
2
3Resource rsc = project.getResources().add("R1");
4
5java.util.Calendar cal = java.util.Calendar.getInstance();
6
7cal.set(2014,3,4,0,0,0);
8
9rsc.set(Rsc.START, cal.getTime());
10
11cal.set(2014,3,10,0,0,0);
12
13rsc.set(Rsc.FINISH, cal.getTime());
14
15project.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();
2
3Task tskRoot = new Task();
4
5Task task = new Task("Task");
6
7Resource rsc = new Resource();
8
9rsc.setStandardRate(BigDecimal.valueOf(10));
10
11rsc.setOvertimeRate(BigDecimal.valueOf(15));
12
13ResourceAssignment assignment = new ResourceAssignment(task, rsc);
14
15assignment.setUid(1);
16
17java.util.Calendar cal = java.util.Calendar.getInstance();
18
19cal.set(2009, 8, 18, 0, 0, 0);
20
21assignment.setStart(cal.getTime());
22
23cal.set(2009,8,20,0,0,0);
24
25assignment.setFinish(cal.getTime() );
26
27project.setRootTask(tskRoot);
28
29project.getRootTask().getChildren().add(task);
30
31project.getResources().add(rsc);
32
33project.getResourceAssignments().add(assignment);
34
35project.save("project.xml", SaveFileFormat.XML);
New Aspose.Tasks for Java 8.x Approach
1Project project = new Project();
2
3Task task = project.getRootTask().getChildren().add("Task");
4
5Resource rsc = project.getResources().add("Rsc");
6
7rsc.set(Rsc.STANDARD_RATE, BigDecimal.valueOf(10));
8
9rsc.set(Rsc.OVERTIME_RATE, BigDecimal.valueOf(15));
10
11ResourceAssignment assignment = project.getResourceAssignments().add(task, rsc);
12
13java.util.Calendar cal = java.util.Calendar.getInstance();
14
15cal.set(2014,3,4,0,0,0);
16
17assignment.set(Asn.START,cal.getTime());
18
19cal.set(2014,3,4,0,0,0);
20
21assignment.set(Asn.FINISH, cal.getTime());
22
23project.save("ResourceAssignments.xml", SaveFileFormat.XML);