Helllo,
public class Aspose {
private static Project project;
private static Task task;
private static ResourceAssignment assn;
private static Resource rsc;
private static Date PROJECT_START_DATE;
private static Date PROJECT_FINISH_DATE;
private static Date SUB_TASK1_START_DATE;
private static Date SUB_TASK1_FINISH_DATE;
public static void main(String[] args) throws Exception {
java.util.Calendar calTimeStart = java.util.Calendar.getInstance();
calTimeStart.set(2016,8,5,8,0,0);
PROJECT_START_DATE = calTimeStart.getTime();
SUB_TASK1_START_DATE = calTimeStart.getTime();
java.util.Calendar calTimeFinish = java.util.Calendar.getInstance();
calTimeFinish.set(2016,8,7,17,0,0);
PROJECT_FINISH_DATE = calTimeFinish.getTime();
SUB_TASK1_FINISH_DATE = calTimeFinish.getTime();
License license = new License();
license.setLicense(new java.io.FileInputStream(“Aspose.Tasks.lic”));
project = createProject();
Task task = addTasks(project);
ResourceAssignment ra = addResourcesAndAssignment(task);
printTask();
timephasedAssignmentDataGeneration();
timephasedTaskDataGeneration();
project.recalculate();
createFileMPP(project);
}
private static Project createProject() {
Project project = new Project();
project.set(Prj.START_DATE, PROJECT_START_DATE);
project.set(Prj.FINISH_DATE, PROJECT_FINISH_DATE);
project.set(Prj.NEW_TASKS_ARE_MANUAL, new NullableBool(false));
Calendar cal = project.getCalendars().add(“Default Calendar”);
//Add working days monday through thursday with default timings
cal.getWeekDays().add(WeekDay.createDefaultWorkingDay(DayType.Monday));
cal.getWeekDays().add(WeekDay.createDefaultWorkingDay(DayType.Tuesday));
cal.getWeekDays().add(WeekDay.createDefaultWorkingDay(DayType.Wednesday));
cal.getWeekDays().add(WeekDay.createDefaultWorkingDay(DayType.Thursday));
cal.getWeekDays().add(new WeekDay(DayType.Saturday));
cal.getWeekDays().add(new WeekDay(DayType.Sunday));
//Set friday as short working day
WeekDay myWeekDay = new WeekDay(DayType.Friday);
//Sets working time. Only time part of DateTime is important
WorkingTime wt1 = new WorkingTime();
java.util.Calendar calTime = java.util.Calendar.getInstance();
calTime.set(1,1,1,8,0,0);
Date date = calTime.getTime();
wt1.setFromTime(date);
calTime.set(1,1,1,12,0,0);
date = calTime.getTime();
wt1.setToTime(date);
WorkingTime wt2 = new WorkingTime();
calTime.set(1,1,1,13,0,0);
date = calTime.getTime();
wt2.setFromTime(date);
calTime.set(1,1,1,17,0,0);
date = calTime.getTime();
wt2.setToTime(date);
myWeekDay.getWorkingTimes().add(wt1);
myWeekDay.getWorkingTimes().add(wt2);
myWeekDay.setDayWorking(true);
cal.getWeekDays().add(myWeekDay);
Calendar.makeStandardCalendar(cal);
project.set(Prj.CALENDAR, cal);
return project;
}
private static Task addTasks(Project project) {
task = project.getRootTask().getChildren().add(“Summary1”);
task.set(Tsk.START, SUB_TASK1_START_DATE);
task.set(Tsk.FINISH, SUB_TASK1_FINISH_DATE);
// task.set(Tsk.ACTUAL_START, SUB_TASK1_START_DATE.toDate());
// task.set(Tsk.ACTUAL_FINISH, SUB_TASK1_FINISH_DATE.toDate());
// task.set(Tsk.DURATION, project.getDuration(3, TimeUnitType.Day));
// task.set(Tsk.START, SUB_TASK1_START_DATE);
// task.set(Tsk.FINISH, SUB_TASK1_FINISH_DATE);
// task.set(Tsk.TYPE, TaskType.FixedUnits);
return task;
}
private static ResourceAssignment addResourcesAndAssignment(Task task) {
rsc = project.getResources().add(“Billel Redouane”);
// task.set(Tsk.PERCENT_COMPLETE, 50);
rsc.set(Rsc.TYPE, ResourceType.Work);
assn = project.getResourceAssignments().add(task.getChildren().getById(1), rsc);
assn.set(Asn.WORK, project.getDuration(24, TimeUnitType.Hour)); // 4 + 4 + 16
//assn.set(Asn.UNITS, 0.5);//Half unit Per day i.e. 4 hours per day
return assn;
}
private static void timephasedAssignmentDataGeneration(){
// List td11 = assn.getTimephasedData(assn.get(Asn.START), assn.get(Asn.FINISH), TimephasedDataType.AssignmentWork).toList();
// //td.get(0).getTimephasedDataType();
//
// System.out.println(td11.size());
// System.out.println(td11.get(0).getValue());
// System.out.println(td11.get(1).getValue());
// System.out.println(td11.get(2).getValue());
// System.out.println(td11.get(3).getValue());
// System.out.println(td11.get(4).getValue());
////////////////////////////////////////
for (TimephasedData tph: assn.getTimephasedData(assn.get(Asn.START), assn.get(Asn.FINISH), TimephasedDataType.AssignmentWork).toList())
{
assn.getTimephasedData().remove(tph);
}
//Days by Days (Work + Actual…)
java.util.Calendar cal = java.util.Calendar.getInstance();
TimephasedData td = new TimephasedData();
td.setStart(SUB_TASK1_START_DATE);
cal.set(2016, 8, 5, 17, 0, 0); td.setFinish( cal.getTime() );
td.setValue(project.getDuration(4, TimeUnitType.Hour).toString()); //Work or actual
td.setUnit(1/2);
td.setTimephasedDataType(TimephasedDataType.AssignmentWork);
assn.getTimephasedData().add(td);
//tds.add(td);
TimephasedData td1 = new TimephasedData();
cal.set(2016, 8, 6, 8, 0, 0); td1.setStart( cal.getTime() );
cal.set(2016, 8, 6, 17, 0, 0); td1.setFinish( cal.getTime() );
td1.setValue(project.getDuration(4, TimeUnitType.Hour).toString()); //Work or actual
td1.setUnit(1/2);
td1.setTimephasedDataType(TimephasedDataType.AssignmentWork);
assn.getTimephasedData().add(td1);
TimephasedData td2 = new TimephasedData();
cal.set(2016, 8, 7, 8, 0, 0); td2.setStart( cal.getTime() );
cal.set(2016, 8, 7, 17, 0, 0); td2.setFinish( cal.getTime() );
td2.setValue(project.getDuration(16, TimeUnitType.Hour).toString()); //Work or actual
td2.setUnit(16);
td2.setTimephasedDataType(TimephasedDataType.AssignmentWork);
assn.getTimephasedData().add(td2);
// TimephasedDataCollection timephasedDataCollection = new TimephasedDataCollection(1, td); //uid
// timephasedDataCollection.add(td);
// assn.setTimephasedData((TimephasedDataCollection) tds);
}
private static void timephasedTaskDataGeneration(){
// List td11 = task.getTimephasedData(task.get(Tsk.START), task.get(Tsk.FINISH), TimephasedDataType.TaskWork).toList();
// //td.get(0).getTimephasedDataType();
//
// System.out.println("timephasedTaskDataGeneration : "+td11.size());
// System.out.println(td11.get(0).getValue());
// System.out.println(td11.get(1).getValue());
// System.out.println(td11.get(2).getValue());
// System.out.println(td11.get(3).getValue());
// System.out.println(td11.get(4).getValue());
////////////////////////////////////////
for (TimephasedData tph: task.getTimephasedData(task.get(Tsk.START), task.get(Tsk.FINISH), TimephasedDataType.TaskWork).toList())
{
task.getTimephasedData().remove(tph);
}
//Days by Days (Work + Actual…)
java.util.Calendar cal = java.util.Calendar.getInstance();
TimephasedData td = new TimephasedData();
td.setStart(SUB_TASK1_START_DATE);
cal.set(2016, 8, 5, 17, 0, 0); td.setFinish( cal.getTime() );
td.setValue(project.getDuration(4, TimeUnitType.Hour).toString()); //Work or actual
td.setUnit(1/2);
td.setTimephasedDataType(TimephasedDataType.TaskWork);
task.getTimephasedData().add(td);
//tds.add(td);
TimephasedData td1 = new TimephasedData();
cal.set(2016, 8, 6, 8, 0, 0); td1.setStart( cal.getTime() );
cal.set(2016, 8, 6, 17, 0, 0); td1.setFinish( cal.getTime() );
td1.setValue(project.getDuration(4, TimeUnitType.Hour).toString()); //Work or actual
td1.setUnit(1/2);
td1.setTimephasedDataType(TimephasedDataType.TaskWork);
task.getTimephasedData().add(td1);
TimephasedData td2 = new TimephasedData();
cal.set(2016, 8, 7, 8, 0, 0); td2.setStart( cal.getTime() );
cal.set(2016, 8, 7, 17, 0, 0); td2.setFinish( cal.getTime() );
td2.setValue(project.getDuration(16, TimeUnitType.Hour).toString()); //Work or actual
td2.setUnit(2);
td2.setTimephasedDataType(TimephasedDataType.TaskWork);
task.getTimephasedData().add(td2);
}
private static void createFileMPP(Project newProject) {
String dataDir = Utils.getDataDir(Aspose.class);
newProject.save(dataDir + “Project1.xml”, SaveFileFormat.XML); //working
// newProject.save(dataDir + “Project1.mpp”, SaveFileFormat.MPP); //Not working
System.out.println(“Project file generated Successfully”);
}
private static void printTask(){
System.out.println("START_DATE : "+task.get(Tsk.START));
System.out.println("FINISH_DATE : "+task.get(Tsk.FINISH));
System.out.println("DURATION : "+task.get(Tsk.DURATION));
System.out.println("Assignment UNITS : "+assn.get(Asn.UNITS));
//System.out.println("ACTUAL_START : "+task.get(Tsk.ACTUAL_START));
//System.out.println("ACTUAL_FINISH : "+task.get(Tsk.ACTUAL_FINISH));
System.out.println("Percent Complete : "+task.get(Tsk.PERCENT_COMPLETE));
}
}