• 使用mpxj读取MSPrjoect


    
    

     

    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");

        }

     

       

         /**

          * MPPTask读数据到数据库的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();

        }

     

    }

  • 相关阅读:
    黄聪:获取当天最新信息的mysql语句php
    黄聪:开源PHP智能中文分词扩展:PHPCWS
    黄聪:Wordpress用get_current_screen函数来选择性加载插件中的JS和CSS
    黄聪: Javascript跨域访问解决方案
    黄聪:WordPress 角色和权限指南
    黄聪:PHP自动判断字符串是gb2312还是utf8编码
    黄聪:mysql查询今天,昨天,近7天,近30天,本月,上一月数据的方法
    黄聪:python中defaultdict标准字典的使用
    黄聪:IE6下css大bug:文字神秘消失,鼠标选择了才出现
    黄聪:C#模拟网站页面POST数据提交表单(一)WebClient (转)
  • 原文地址:https://www.cnblogs.com/lxjshuju/p/6917654.html
Copyright © 2020-2023  润新知