import java.util.ArrayList;
import java.util.Calendar;
import java.util.Hashtable;
import java.util.List;
import net.sf.mpxj.ProjectFile;
import net.sf.mpxj.Relation;
import net.sf.mpxj.Task;
/**
* MSProject工具类
* @author suhf
*
*/
publicclass MPPUtil {
static Hashtable<String,String> dependencyTypes = new Hashtable<String,String>();
publicstaticvoid init(){
dependencyTypes.put("FF", "3");
dependencyTypes.put("FS", "2");
dependencyTypes.put("SF", "1");
dependencyTypes.put("SS", "0");
}
/**
* 从MPP的Task读数据到数据库的BdTask
* @param task
* @param parentId
* @return
*/
privatestatic BdTask fetchTaskData(Task task, String parentId){
BdTask gttTask =new BdTask();
try{
gttTask.setPriority(1);
gttTask.setId(task.getUniqueID().toString());
gttTask.setNumbers(task.getID());
gttTask.setName(task.getName());
Double doub = Double.parseDouble(task.getDuration().toString().replaceAll("(?!\.)[\D]", ""));
gttTask.setDuration(doub.floatValue());
gttTask.setDurationUnit(task.getDuration().getUnits().toString());
Double effort = gttTask.getDuration()*8.0;
gttTask.setEffort(effort.intValue());
gttTask.setNote(task.getNotes());
gttTask.setPercentDone((task.getPercentageComplete().doubleValue()));
Calendar startDate = Calendar.getInstance();
Calendar endDate = Calendar.getInstance();
if(task.getMilestone()){//是里程碑
gttTask.setMilestone("1");
startDate.setTime(task.getStart());
//startDate.add(Calendar.DAY_OF_MONTH, 1);
endDate.setTime(task.getFinish());
//endDate.add(Calendar.DAY_OF_MONTH, 1);
}else{
gttTask.setMilestone("0");
startDate.setTime(task.getStart());
startDate.set(Calendar.HOUR_OF_DAY, 8);
startDate.set(Calendar.MINUTE, 30);
startDate.set(Calendar.SECOND, 0);
endDate.setTime(task.getFinish());
endDate.set(Calendar.HOUR_OF_DAY, 18);
endDate.set(Calendar.MINUTE, 0);
endDate.set(Calendar.SECOND, 0);
//endDate.add(Calendar.DAY_OF_MONTH, 1);
}
/*startDate.set(startDate.get(Calendar.YEAR),
startDate.get(Calendar.MONTH),
startDate.get(Calendar.DATE), 0, 0, 0);
endDate.set(endDate.get(Calendar.YEAR),
endDate.get(Calendar.MONTH),
endDate.get(Calendar.DATE), 0, 0, 0);*/
gttTask.setStartDate(DateFormatUtil.formatDateToString(startDate.getTime(), DateFormatUtil.V_LONG_DATE));
gttTask.setEndDate(DateFormatUtil.formatDateToString(endDate.getTime(), DateFormatUtil.V_LONG_DATE));
读取资源名称
/*List<ResourceAssignment> l = task.getResourceAssignments();
for (ResourceAssignment ra : l) {
Resource r = ra.getResource();
if(r != null)
System.out.println(r.getName()+"--"+task.getName());
}*/
gttTask.setParentId(task.getParentTask().getUniqueID().toString()) ;
}catch(Exception e){
e.printStackTrace();
// System.exit(0);
}
return gttTask;
}
/**
* 获取项目计划任务数据
* @param projectFile MPP文件对象
* @return
* @throws Exception
*/
publicstatic List<BdTask> getTaskData(ProjectFile projectFile) throws Exception{
List<BdTask> list = new ArrayList<BdTask>();
//这里有两层嵌套
for(Task tempTask :projectFile.getChildTasks())
{
int idx = 0;
for (Task task : tempTask.getChildTasks()) {
//加入最上层任务节点, 第三个參数parentId=0
BdTask gttTask = fetchTaskData(task,"");
//设置节点在姊妹节点下的顺序
gttTask.setIndexs(idx++);
//加入到数据库中
list.add(gttTask);
//递归调用
listHierarchy(task, list, gttTask.getId());
}
}
return list;
}
privatestatic List<BdTask> listHierarchy(Task task,List<BdTask> list, String parentId){
//节点的先后顺序
int idx = 0;
for (Task child : task.getChildTasks()) {
BdTask gttTask = fetchTaskData(child,parentId);
//设置任务的先后顺序
gttTask.setIndexs(idx++);
list.add(gttTask);
listHierarchy(child, list , parentId);
}
return list;
}
/**
* 获取项目计划任务关联数据
* @param projectFile MPP文件对象
* @return
* @throws Exception
*/
publicstatic List<BdDependency> getBdDependencyDate(ProjectFile projectFile){
List<BdDependency> list = new ArrayList<BdDependency>();
MPPUtil.init();//初始化MPP任务关联相应Gantt图的编号
//节点之间的关系
for (Task task : projectFile.getAllTasks()) {
List<Relation> predecessors = task.getPredecessors();
if (predecessors != null && predecessors.isEmpty() == false){
for (Relation relation : predecessors){
BdDependency dep = new BdDependency();
dep.setToId(relation.getSourceTask().getUniqueID().toString());
dep.setFromId(relation.getTargetTask().getUniqueID().toString());
dep.setType(getDependencyType(relation.getType().toString()));
list.add(dep);
}
}
}
return list;
}
static String getDependencyType(String content){
Object ret = dependencyTypes.get(content);
return ret.toString();
}
}