Writing Updated TaskLink Data to MPP
With Aspose.Tasks for Java, it is possible to link tasks in a project and save the updated task link data back to a Microsoft Project MPP file. This means that you can open an MPP file, update task links, and save the file back out.
Updating Task Links The code snippet below shows how to link tasks in a project and write the link data back to the MPP file. The steps involved in this activity are:
- Create an instance of the Project Reader.
- Read the source MPP file.
- Create tasks and add these to the project’s root task.
- Link the tasks using StartToFinish and FinishToStart link types.
- Save the project using the Project Writer.
The following code demonstrates how this feature works.
1// For complete examples and data files, please go to https://github.com/aspose-tasks/Aspose.Tasks-for-Java
2// The path to the documents directory.
3String dataDir = Utils.getDataDir(UpdatedTaskLinkDataToMpp.class);
4
5long OneSec = 10000000;// microsecond * 10
6long OneMin = 60 * OneSec;
7long OneHour = 60 * OneMin;
8
9String newFile = "Blank2010.mpp";
10// File to write test project
11String resultFile = "Output.mpp";
12
13// Read new empty file
14Project project = new Project(newFile);
15
16// Add eight tasks
17Task task1 = project.getRootTask().getChildren().add("1");
18task1.set(Tsk.DURATION, project.getDuration(8, TimeUnitType.Hour));
19task1.set(Tsk.START, project.get(Prj.START_DATE));
20task1.set(Tsk.FINISH,
21 project.get(Prj.CALENDAR).getTaskFinishDateFromDuration(task1, task1.get(Tsk.DURATION).toDouble()));
22
23Task task2 = project.getRootTask().getChildren().add("2");
24task2.set(Tsk.DURATION, project.getDuration(8, TimeUnitType.Hour));
25task2.set(Tsk.START, project.get(Prj.START_DATE));
26task2.set(Tsk.FINISH,
27 project.get(Prj.CALENDAR).getTaskFinishDateFromDuration(task2, task2.get(Tsk.DURATION).toDouble()));
28
29project.getRootTask().getChildren().add(task1);
30project.getRootTask().getChildren().add(task2);
31
32Task task3 = project.getRootTask().getChildren().add("3");
33task3.set(Tsk.DURATION, project.getDuration(8, TimeUnitType.Hour));
34task3.set(Tsk.START, project.get(Prj.START_DATE));
35task3.set(Tsk.FINISH,
36 project.get(Prj.CALENDAR).getTaskFinishDateFromDuration(task3, task3.get(Tsk.DURATION).toDouble()));
37
38Task task4 = project.getRootTask().getChildren().add("4");
39task4.set(Tsk.DURATION, project.getDuration(8, TimeUnitType.Hour));
40task4.set(Tsk.START, project.get(Prj.START_DATE));
41task4.set(Tsk.FINISH,
42 project.get(Prj.CALENDAR).getTaskFinishDateFromDuration(task4, task4.get(Tsk.DURATION).toDouble()));
43
44project.getRootTask().getChildren().add(task3);
45project.getRootTask().getChildren().add(task4);
46
47Task task5 = project.getRootTask().getChildren().add("5");
48task5.set(Tsk.DURATION, project.getDuration(8, TimeUnitType.Hour));
49java.util.Calendar cal = java.util.Calendar.getInstance();
50cal.setTime(project.get(Prj.START_DATE));
51cal.add(java.util.Calendar.DATE, 7);
52
53task5.set(Prj.START_DATE, cal.getTime());
54task5.set(Tsk.FINISH,
55 project.get(Prj.CALENDAR).getTaskFinishDateFromDuration(task5, task5.get(Tsk.DURATION).toDouble()));
56task5.set(Tsk.CONSTRAINT_TYPE, ConstraintType.MustStartOn);
57task5.set(Tsk.CONSTRAINT_DATE, task5.get(Tsk.START));
58
59Task task6 = project.getRootTask().getChildren().add("6");
60task6.set(Tsk.DURATION, project.getDuration(8, TimeUnitType.Hour));
61task6.set(Tsk.FINISH, task5.get(Tsk.START));
62task6.set(Tsk.START,
63 project.get(Prj.CALENDAR).getTaskFinishDateFromDuration(task6, task6.get(Tsk.DURATION).toDouble()));
64
65project.getRootTask().getChildren().add(task5);
66project.getRootTask().getChildren().add(task6);
67
68Task task7 = project.getRootTask().getChildren().add("7");
69task7.set(Tsk.DURATION, project.getDuration(8, TimeUnitType.Hour));
70
71Task task8 = project.getRootTask().getChildren().add("8");
72task8.set(Tsk.DURATION, project.getDuration(8, TimeUnitType.Hour));
73
74project.getRootTask().getChildren().add(task7);
75project.getRootTask().getChildren().add(task8);
76
77task7.set(Tsk.START, project.get(Prj.START_DATE));
78task7.set(Tsk.START,
79 project.get(Prj.CALENDAR).getTaskFinishDateFromDuration(task7, task7.get(Tsk.DURATION).toDouble()));
80
81cal.setTime(project.get(Prj.START_DATE));
82cal.add(java.util.Calendar.DATE, 1);
83task8.set(Tsk.START, cal.getTime());
84task8.set(Tsk.START,
85 project.get(Prj.CALENDAR).getTaskFinishDateFromDuration(task8, task8.get(Tsk.DURATION).toDouble()));
86
87// Add four TaskLinks with different TaskLinkTypes
88TaskLink link1 = project.getTaskLinks().add(task1, task2, TaskLinkType.StartToStart);
89
90TaskLink link2 = project.getTaskLinks().add(task3, task4, TaskLinkType.FinishToFinish);
91
92TaskLink link3 = project.getTaskLinks().add(task5, task6, TaskLinkType.StartToFinish);
93
94TaskLink link4 = project.getTaskLinks().add(task7, task8, TaskLinkType.FinishToStart);
95
96link4.setLagFormat(TimeUnitType.Day);
97link4.setLinkLag(60 * 8 * 10 * 10); // 10d
98task8.set(Tsk.START,
99 project.get(Prj.CALENDAR).getFinishDateByStartAndWork(task8.get(Tsk.FINISH), OneHour * 8 * 10));
100task8.set(Tsk.FINISH, project.get(Prj.CALENDAR).getFinishDateByStartAndWork(task8.get(Tsk.START),
101 task8.get(Tsk.DURATION).toDouble()));
102
103project.save(dataDir + "Project.Mpp", SaveFileFormat.MPP);