net.sf.mpxj.sample.MpxjQuery Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mpxj Show documentation
Show all versions of mpxj Show documentation
Library that provides facilities to allow project information to be manipulated in Java and .Net. Supports a range of data formats: Microsoft Project Exchange (MPX), Microsoft Project (MPP,MPT), Microsoft Project Data Interchange (MSPDI XML), Microsoft Project Database (MPD), Planner (XML), Primavera (PM XML, XER, and database), Asta Powerproject (PP, MDB), Asta Easyplan (PP), Phoenix Project Manager (PPX), FastTrack Schedule (FTS), and the Standard Data Exchange Format (SDEF).
/*
* file: MpxjQuery.java
* author: Jon Iles
* copyright: (c) Packwood Software 2002-2003
* date: 13/02/2003
*/
/*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation; either version 2.1 of the License, or (at your
* option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
package net.sf.mpxj.sample;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import net.sf.mpxj.AssignmentField;
import net.sf.mpxj.ChildResourceContainer;
import net.sf.mpxj.ChildTaskContainer;
import net.sf.mpxj.LocalDateTimeRange;
import net.sf.mpxj.Duration;
import net.sf.mpxj.FieldType;
import net.sf.mpxj.ProjectCalendar;
import net.sf.mpxj.ProjectFile;
import net.sf.mpxj.ProjectProperties;
import net.sf.mpxj.Relation;
import net.sf.mpxj.RelationType;
import net.sf.mpxj.Resource;
import net.sf.mpxj.ResourceAssignment;
import net.sf.mpxj.ResourceField;
import net.sf.mpxj.Task;
import net.sf.mpxj.TaskField;
import net.sf.mpxj.mpp.TimescaleUnits;
import net.sf.mpxj.reader.UniversalProjectReader;
import net.sf.mpxj.utility.TimephasedUtility;
import net.sf.mpxj.utility.TimescaleUtility;
/**
* This example shows an MPP, MPX or MSPDI file being read, and basic
* task and resource data being extracted.
*/
public class MpxjQuery
{
/**
* Main method.
*
* @param args array of command line arguments
*/
public static void main(String[] args)
{
try
{
if (args.length != 1)
{
System.out.println("Usage: MpxQuery ");
}
else
{
query(args[0]);
}
}
catch (Exception ex)
{
ex.printStackTrace(System.out);
}
}
/**
* This method performs a set of queries to retrieve information
* from the an MPP or an MPX file.
*
* @param filename name of the MPX file
* @throws Exception on file read error
*/
private static void query(String filename) throws Exception
{
ProjectFile mpx = new UniversalProjectReader().read(filename);
if (mpx == null)
{
throw new Exception("Unable to read file");
}
listProjectProperties(mpx);
listResources(mpx);
listTasks(mpx);
listAssignments(mpx);
listAssignmentsByTask(mpx);
listAssignmentsByResource(mpx);
listTaskHierarchy(mpx, "");
listResourceHierarchy(mpx, "");
listTaskNotes(mpx);
listResourceNotes(mpx);
listRelationships(mpx);
listSlack(mpx);
listCalendars(mpx);
listPopulatedFields(mpx);
listTasksPercentComplete(mpx);
}
/**
* Reads basic summary details from the project properties.
*
* @param file MPX file
*/
private static void listProjectProperties(ProjectFile file)
{
DateTimeFormatter df = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
ProjectProperties properties = file.getProjectProperties();
LocalDateTime startDate = properties.getStartDate();
LocalDateTime finishDate = properties.getFinishDate();
String formattedStartDate = startDate == null ? "(none)" : df.format(startDate);
String formattedFinishDate = finishDate == null ? "(none)" : df.format(finishDate);
System.out.println("MPP file type: " + properties.getMppFileType());
System.out.println("Project Properties: StartDate=" + formattedStartDate + " FinishDate=" + formattedFinishDate);
System.out.println();
}
/**
* This method lists all resources defined in the file.
*
* @param file MPX file
*/
private static void listResources(ProjectFile file)
{
for (Resource resource : file.getResources())
{
System.out.println("Resource: " + resource.getName() + " (Unique ID=" + resource.getUniqueID() + ") Start=" + resource.getStart() + " Finish=" + resource.getFinish());
}
System.out.println();
}
/**
* This method lists all tasks defined in the file.
*
* @param file MPX file
*/
private static void listTasks(ProjectFile file)
{
DateTimeFormatter df = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
for (Task task : file.getTasks())
{
LocalDateTime date = task.getStart();
String text = task.getStartText();
String startDate = text != null ? text : (date != null ? df.format(date) : "(no start date supplied)");
date = task.getFinish();
text = task.getFinishText();
String finishDate = text != null ? text : (date != null ? df.format(date) : "(no finish date supplied)");
Duration dur = task.getDuration();
text = task.getDurationText();
String duration = text != null ? text : (dur != null ? dur.toString() : "(no duration supplied)");
dur = task.getActualDuration();
String actualDuration = dur != null ? dur.toString() : "(no actual duration supplied)";
String baselineDuration = task.getBaselineDurationText();
if (baselineDuration == null)
{
dur = task.getBaselineDuration();
if (dur != null)
{
baselineDuration = dur.toString();
}
else
{
baselineDuration = "(no duration supplied)";
}
}
System.out.println("Task: " + task.getName() + " ID=" + task.getID() + " Unique ID=" + task.getUniqueID() + " (Start Date=" + startDate + " Finish Date=" + finishDate + " Duration=" + duration + " Actual Duration" + actualDuration + " Baseline Duration=" + baselineDuration + " Outline Level=" + task.getOutlineLevel() + " Outline Number=" + task.getOutlineNumber() + " Recurring=" + task.getRecurring() + ")");
}
System.out.println();
}
/**
* List different percent complete types for the tasks.
*
* @param file project file
*/
private static void listTasksPercentComplete(ProjectFile file)
{
System.out.println("ID\tUniqueID\tActivity ID\tName\t%Complete Type\tDuration % Complete\tWork % Complete\tPhysical % Complete");
for (Task task : file.getTasks())
{
List