Skip to content

Instantly share code, notes, and snippets.

@martinm1000
Created September 15, 2016 14:13
Show Gist options
  • Save martinm1000/08f020407c8ca83e61e94289ac3d48ec to your computer and use it in GitHub Desktop.
Save martinm1000/08f020407c8ca83e61e94289ac3d48ec to your computer and use it in GitHub Desktop.
import net.sf.mpxj.Duration;
import net.sf.mpxj.ProjectCalendar;
import net.sf.mpxj.ProjectFile;
import net.sf.mpxj.ProjectProperties;
import net.sf.mpxj.Resource;
import net.sf.mpxj.ResourceAssignment;
import net.sf.mpxj.Task;
import net.sf.mpxj.TaskMode;
import net.sf.mpxj.TimeUnit;
import net.sf.mpxj.TimephasedWork;
import net.sf.mpxj.common.DefaultTimephasedWorkContainer;
import net.sf.mpxj.mpp.MPPTimephasedWorkNormaliser;
import net.sf.mpxj.mspdi.MSPDITimephasedWorkNormaliser;
import net.sf.mpxj.mspdi.MSPDIWriter;
import net.sf.mpxj.mspdi.schema.TimephasedDataType;
import net.sf.mpxj.utility.TimephasedUtility;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @author Martin Miller
* @version 0.1 2016-09-14
*/
public class Write {
public static void main(String[] args) throws IOException {
ProjectFile pf = new ProjectFile();
ProjectCalendar pc = pf.getDefaultCalendar();
ProjectProperties pp = pf.getProjectProperties();
pp.setMinutesPerDay(8 * 60);
pp.setMinutesPerWeek(pp.getMinutesPerDay().intValue() * 5);
pp.setDaysPerMonth(20);
pp.setDefaultDurationUnits(net.sf.mpxj.TimeUnit.DAYS);
pp.setDefaultWorkUnits(net.sf.mpxj.TimeUnit.HOURS);
Task task = pf.addTask();
task.setName("Miller");
task.setID(0);
task.setTaskMode(TaskMode.MANUALLY_SCHEDULED);
task.setStart(Date.from(LocalDateTime.of(2016,9,1, 8,0).atZone(ZoneId.systemDefault()).toInstant()));
task.setFinish(Date.from(LocalDateTime.of(2016,9,2, 17,0).atZone(ZoneId.systemDefault()).toInstant()));
task.setDuration(Duration.getInstance(2, TimeUnit.DAYS));
task.setWork(Duration.getInstance(13, TimeUnit.HOURS));
Resource r = pf.addResource();
r.setName("Martin Miller");
r.setInitials("MM");
ResourceAssignment ra = pf.newResourceAssignment(task);
ra.setResourceUniqueID(r.getUniqueID());
ra.setStart(task.getStart());
ra.setFinish(task.getFinish());
ra.setWork(task.getWork());
ra.setRemainingWork(task.getWork()); // <------------------------------------ IMPORTANT
ra.setUnits(100);
TimephasedWork w = new TimephasedWork();
w.setStart(Date.from(LocalDateTime.of(2016,9,1, 8,0).atZone(ZoneId.systemDefault()).toInstant()));
w.setFinish(Date.from(LocalDateTime.of(2016,9,1, 16,0).atZone(ZoneId.systemDefault()).toInstant()));
w.setAmountPerDay(Duration.getInstance(7, TimeUnit.HOURS));
w.setTotalAmount(Duration.getInstance(7, TimeUnit.HOURS));
TimephasedWork w2 = new TimephasedWork();
w2.setStart(Date.from(LocalDateTime.of(2016,9,2, 8,0).atZone(ZoneId.systemDefault()).toInstant()));
w2.setFinish(Date.from(LocalDateTime.of(2016,9,2, 15,0).atZone(ZoneId.systemDefault()).toInstant()));
w2.setAmountPerDay(Duration.getInstance(6, TimeUnit.HOURS));
w2.setTotalAmount(Duration.getInstance(6, TimeUnit.HOURS));
List<TimephasedWork> works = new ArrayList<>();
works.add(w);
works.add(w2);
ra.setTimephasedWork(new DefaultTimephasedWorkContainer(pc, new MSPDITimephasedWorkNormaliser(), works, false));
task.addResourceAssignment(ra);
MSPDIWriter writer = new MSPDIWriter();
writer.write(pf, "c:\\test.xml");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment