Utility Features Available in Aspose.Tasks for Java
Calculating Critical Path
The critical path is the tasks, or task, that ultimately determine when your project will end. Keeping track of the critical path and the resources assigned to them helps keep the project on time. Microsoft Project calculates the critical path based on tasks that have no slack, have specific date constraints (must start on, must finish on, as soon as possible or as late as possible) or have the same, or later, end date as the project. Aspose.Tasks allows you to calculate the critical path.
The Project class provides the getCriticalPath() method which is used to get the list of tasks that part of the critical path.
The following piece of code calculates and displays the tasks in the critical path.
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(CriticalPath.class);
4
5Project project = new Project(dataDir + "New project 2010.mpp");
6project.setCalculationMode(CalculationMode.Automatic);
7
8Task subtask1 = project.getRootTask().getChildren().add("1");
9Task subtask2 = project.getRootTask().getChildren().add("2");
10Task subtask3 = project.getRootTask().getChildren().add("3");
11
12project.getTaskLinks().add(subtask1, subtask2, TaskLinkType.FinishToStart);
13
14// Display the critical path now
15for (Task task : project.getCriticalPath()) {
16 System.out.println(task.get(Tsk.NAME));
17}
Printing the TaskWritingException
A project consists of a number of tasks and Aspose.Tasks for Java API allows adding, removing and updating task information. If there’s a problem when writing tasks, use TasksWritingException to catch them.
Aspose.Tasks for Java supports printing a message when there’s an exception in writing a task. This is done with TaskWritingException, in a similar way to how TaskReadingException is used. The log message is contained in the public property TasksWritingException.getLogText(), as shown in the following code example.
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(PrintTaskWritingExceptions.class);
4Project prj = new Project(dataDir + "Project5.mpp");
5
6try {
7 prj.save(dataDir + "project.MPP", SaveFileFormat.MPP);
8} catch (TasksWritingException ex) {
9 System.out.println(ex.getLogText());
10}
Working with Filter Data from MPP files
Aspose.Tasks allows to read Information about filters applied to an MPP file data. This topic shows retrieving Filter Definition and Filter Criteria data from a Microsoft Project MPP file.
Reading Filter Definition Data
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(FilterDataFromMppFile.class);
4
5Project project = new Project(dataDir + "Project2003.mpp");
6List<Filter> taskFilters = project.getTaskFilters().toList();
7
8System.out.println("Task Filters Count: " + taskFilters.size());
9System.out.println("All Tasks: " + taskFilters.get(0).getName());
10System.out.println("Task Item: " + taskFilters.get(0).getFilterType());
11System.out.println("Task Filters Show In Menu: " + taskFilters.get(0).getShowInMenu());
12System.out.println("Task filter ShowRelatedSummaryRows: " + taskFilters.get(0).getShowRelatedSummaryRows());
13
14System.out.println("Task filter type: " + taskFilters.get(1).getFilterType());
15System.out.println("Task Filters Show In Menu: " + taskFilters.get(1).getShowInMenu());
16System.out.println("Task filter ShowRelatedSummaryRows: " + taskFilters.get(1).getShowRelatedSummaryRows());
17
18System.out.println("NEW FILTER" + taskFilters.get(2).getName());
19System.out.println("Task filter type: " + taskFilters.get(2).getShowInMenu());
20System.out.println("Task Filters Show In Menu: " + taskFilters.get(2).getShowInMenu());
21System.out.println("Task filter ShowRelatedSummaryRows: " + taskFilters.get(2).getShowRelatedSummaryRows());
22
23System.out.println("Task FilterCriteria:m" + taskFilters.get(2).getCriteria());
24System.out.println("(TaskName Contains T)" + taskFilters.get(2).getCriteria().toString());
25
26List<Filter> rscFilters = project.getResourceFilters().toList();
27
28System.out.println("Project.ResourceFilters count: " + rscFilters.size());
29System.out.println("Resource Filter Item Type: Item.ResourceType: " + rscFilters.get(0).getFilterType());
30System.out.println("Resource filter ShowInMenu" + rscFilters.get(0).getShowInMenu());
31System.out.println("Resource filter ShowRelatedSummaryRows: " + rscFilters.get(0).getShowRelatedSummaryRows());
Reading Filter Criteria Data
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(FilterDataFromMppFile.class);
4
5Project project = new Project(dataDir + "Task name filter.mpp");
6
7Filter filter = project.getTaskFilters().toList().get(1);
8System.out.println(filter.getCriteria().getCriteriaRows().size());
9System.out.println(filter.getCriteria().getOperation());
10
11FilterCriteria criteria1 = filter.getCriteria().getCriteriaRows().get(0);
12System.out.println(criteria1.getTest());
13System.out.println(criteria1.getField());
14// System.out.println(criteria1.getValues().getValues().get(0).toString());
15
16FilterCriteria criteria2 = filter.getCriteria().getCriteriaRows().get(1);
17System.out.println(criteria2.getOperation());
18System.out.println(criteria2.getCriteriaRows().size());
19
20FilterCriteria criteria21 = criteria2.getCriteriaRows().get(0);
21System.out.println(criteria21.getTest());
22System.out.println(criteria21.getField());
23
24FilterCriteria criteria22 = criteria2.getCriteriaRows().get(1);
25System.out.println(criteria22.getTest());
26System.out.println(criteria22.getField());
27
28FilterCriteria criteria23 = criteria2.getCriteriaRows().get(2);
29System.out.println(criteria23.getTest());
30System.out.println(criteria23.getField());
31
32FilterCriteria criteria3 = filter.getCriteria().getCriteriaRows().get(2);
33System.out.println(criteria3.getCriteriaRows().size());
34System.out.println(criteria3.getOperation());
35
36System.out.println(filter.getCriteria());
Reading Group Definition Data
A Microsoft Project data file may contain data in groups. Aspose.Tasks for Java provides the facility to read the group definition data as shown in this topic.
1// For complete examples and data files, please go to https://github.com/aspose-tasks/Aspose.Tasks-for-Java
2String testDir = Utils.getDataDir(ReadGroupDefinitionData.class);
3
4Project project = new Project(testDir + "Task name filter.mpp");
5System.out.println("Task Groups Count: " + project.getTaskGroups().size());
6
7Group taskGroup = project.getTaskGroups().toList().get(1);
8System.out.println("Percent Complete:" + taskGroup.getName());
9System.out.println("Group Criteria count: " + taskGroup.getGroupCriteria().size());
10
11System.out.println("\n************* Retrieving Task Group's Criterion information *************");
12GroupCriterion criterion = taskGroup.getGroupCriteria().toList().get(0);
13System.out.println("Criterion Field: " + criterion.getField());
14System.out.println("Criterion GroupOn: " + criterion.getGroupOn());
15System.out.println("Criterion Cell Color: " + criterion.getCellColor());
16System.out.println("Criterion Pattern: " + criterion.getPattern());
17
18if (taskGroup == criterion.getParentGroup())
19 System.out.println("Parent Group is equval to task Group.");
20
21System.out.println("\n*********** Retreivnig Criterion's Font Information ***********");
22System.out.println("Font Name: " + criterion.getFont().getName());
23System.out.println("Font Size: " + criterion.getFont().getSize());
24System.out.println("Font Style: " + criterion.getFont().getStyle());
25System.out.println("Ascending/Dscending: " + criterion.getAscending());
Reading Table Data from a Project File
Aspose.Tasks for Java API supports reading Table data from Microsoft Project data files. The Project.Tables implements the ICollection interface to provide access to the Table data of MPP file. The feature is supported for all versions of Microsoft Project data files i.e. MPP 2003, 2007, 2010 and 2013.
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(ReadTableDataFromFile.class);
4;
5
6Project project = new Project(dataDir + "TASKS_33579\\Project2003.mpp");
7
8Table t1 = project.getTables().toList().get(0);
9System.out.println("Table Fields Count" + t1.getTableFields().size());
10
11TableField f = t1.getTableFields().get(0);
12System.out.println("Field width: " + f.getWidth());
13System.out.println("Field Title: " + f.getTitle());
14System.out.println("Field Title Alignment: " + f.getAlignTitle());
15System.out.println("Field Align Data: " + f.getAlignData());
16
17f = t1.getTableFields().get(1);
18System.out.println("Field width: " + f.getWidth());
19System.out.println("Field Title: " + f.getTitle());
20System.out.println("Field Title Alignment: " + f.getAlignTitle());
21System.out.println("Field Align Data: " + f.getAlignData());
22
23f = t1.getTableFields().get(2);
24System.out.println("Field width: " + f.getWidth());
25System.out.println("Field Title: " + f.getTitle());
26System.out.println("Field Title Alignment: " + f.getAlignTitle());
27System.out.println("Field Align Data: " + f.getAlignData());
28
29f = t1.getTableFields().get(3);
30System.out.println("Field width: " + f.getWidth());
31System.out.println("Field Title: " + f.getTitle());
32System.out.println("Field Title Alignment: " + f.getAlignTitle());
33System.out.println("Field Align Data: " + f.getAlignData());
34
35f = t1.getTableFields().get(4);
36System.out.println("Field width: " + f.getWidth());
37System.out.println("Field Title: " + f.getTitle());
38System.out.println("Field Title Alignment: " + f.getAlignTitle());
39System.out.println("Field Align Data: " + f.getAlignData());
40
41f = t1.getTableFields().get(5);
42System.out.println("Field width: " + f.getWidth());
43System.out.println("Field Title: " + f.getTitle());
44System.out.println("Field Title Alignment: " + f.getAlignTitle());
45System.out.println("Field Align Data: " + f.getAlignData());
46
47f = t1.getTableFields().get(6);
48System.out.println("Field width: " + f.getWidth());
49System.out.println("Field Title: " + f.getTitle());
50System.out.println("Field Title Alignment: " + f.getAlignTitle());
51System.out.println("Field Align Data: " + f.getAlignData());
52
53f = t1.getTableFields().get(7);
54System.out.println("Field width: " + f.getWidth());
55System.out.println("Field Title: " + f.getTitle());
56System.out.println("Field Title Alignment: " + f.getAlignTitle());
57System.out.println("Field Align Data: " + f.getAlignData());
Extracting Embedded Objects from Task or Resource View
Microsoft Project data files (MPP/XML) may contain embedded objects such as documents, excel sheets, PDF, images, etc. in Task or Resource views. Aspose.Tasks for Java API provides the capability to extract these from a project’s Task or Resource view as shown in this topic.
Embedded objects (those which were created from the file by selecting a file path) are packed into OLE Package inside MPP file. To extract the original file you can use Content and FullPath properties of an instance of the OleObject class.
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(ExtractingEmbeddedObjects.class);
4Project project = new Project(dataDir + "EmbeddedFile.mpp");
5OleObject ole = project.getOleObjects().toList().get(0);
6
7// we have to check this property because it can be null if the embedded
8// object was created inside the ms project application
9// Or, alternatively, you can use this check: if (ole.FileFormat ==
10// "Package")
11// if (!string.IsNullOrEmpty(ole.FullPath))
12if (ole.getFileFormat().equals("Package")) {
13
14 File file = new File(dataDir);
15 FileOutputStream fop = new FileOutputStream(file);
16 // get the content in bytes
17 byte[] contentInBytes = ole.getContent();
18
19 fop.write(contentInBytes);
20 fop.flush();
21 fop.close();
22}