To append tasks to an Microsoft XML file format i,e MSPDI using MPXJ.
I have used the below mentioned code.But with this code, The new task is not appending in the proper Order.. i,e the newly added Major task gets inserted after the first major task. instead of getting added after the last task.
can anyone help me to solve the above mentioned issue.
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import net.sf.mpxj.*;
import net.sf.mpxj.mpp.MPPReader;
import net.sf.mpxj.mpx.MPXReader;
import net.sf.mpxj.mpx.MPXWriter;
import net.sf.mpxj.mspdi.*;
import net.sf.mpxj.mspdi.MSPDIWriter;
import net.sf.mpxj.utility.NumberUtility;
import net.sf.mpxj.writer.ProjectWriter;
public class CreateXML
{
public CreateXML ()
{
}
public static void main(String args[])
{
try
{
create("d:\\output.XML");
}
catch(Exception ex)
{
ex.printStackTrace(System.out);
}
}
private static ProjectWriter getWriter(String filename)
{
String suffix;
if(filename.length() < 4)
suffix = ".MPX";
else
suffix = filename.substring(filename.length() - 4).toUpperCase();
ProjectWriter result;
if(suffix.equals(".XML"))
result = new MSPDIWriter();
else
result = new MPXWriter();
return result;
}
private static void create(String filename)
throws Exception
{
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
MSPDIReader r=new MSPDIReader();
ProjectFile file=r.read("d:\\input.XML");
file.setAutoTaskID(true);
file.setAutoTaskUniqueID(true);
file.setAutoResourceID(true);
file.setAutoResourceUniqueID(true);
file.setAutoOutlineLevel(true);
file.setAutoOutlineNumber(true);
file.setAutoWBS(true);
file.setAutoCalendarUniqueID(true);
ProjectCalendar calendar = file.addDefaultBaseCalendar();
ProjectCalendarException exception = calendar.addCalendarException();
exception.setFromDate(df.parse("13/06/2007"));
exception.setToDate(df.parse("13/06/2007"));
exception.setWorking(false);
ProjectHeader header = file.getProjectHeader();
header.setStartDate(df.parse("1/06/2007"));
Resource resource1 = file.addResource();
resource1.setName("Resource1");
Resource resource2 = file.addResource();
resource2.setName("Resource2");
resource2.setMaxUnits(new Double(50D));
Task task1 = file.addTask();
task1.setName("Main Task2");
task1.setStart(df.parse("12/07/2007"));
task1.setDuration(Duration.getInstance(100D, TimeUnit.DAYS));
Task task2 = task1.addTask();
task2.setName("T13");
task2.setDuration(Duration.getInstance(5, TimeUnit.DAYS));
task2.setStart(df.parse("6/10/2007"));
task2.setPercentageComplete(NumberUtility.getDouble(50D));
ProjectWriter writer = getWriter(filename);
writer.write(file, filename);
}
}