转:传智播客—jbpm与OA项目(五)

今日完成部门(department)和员工(employee)管理,由于昨天已经对增、删、改、查摸了遍,所以今日的内容相对就比较简单了。虽然简单,仍然有需要注意的地方。经验丰富的程序员,编写的代码十分优雅,这也是新人们严重不足的地方,当然我也应该加强这方面的能力。

 

不过说来也十分无聊,手动编写OA项目已二天了。这两天除了项目的架构与优雅的接口设计让我感觉好些外,其他没有什么让我感觉愉快的。因为剩下的就是对数据库的CRUD操作,无聊之极。但是我仍然需要努力学习,并在工作中总结经验。将这部分的操作变得更简洁一些。

 

一、部门管理

部门管理的难点在于,一个部门可以有一个“上级部门”与多个“下级部门”。对应的DispatchAciton有六个处理请求的方法。

 

1.显示部门列表的list方法

显示部门列表的方式:在页面中显示出顶级部门(没有上级部门的部门),点击其中的某个部门后显示出对应部门的所有子部门。方法如下:

DepartmentService deptServ = new DepartmentServiceImpl();

 

/**

 * 部门列表

 */

public ActionForward list(ActionMapping mapping, ActionForm form,

        HttpServletRequest request, HttpServletResponse response)

        throws Exception {

    // 获取页面传递过来的上级部门ID

    Long parentId = null;

    if(request.getParameter("parentId")!=null)

        parentId = Long.parseLong(request.getParameter("parentId"));

    // 如果上级部门ID不为null则获取对应部门的所有下级部门,否则获取所有顶级部门

    List<Department> list = null;

    if (parentId == null || this.deptServ.getById(parentId)==null)

        list = this.deptServ.getTopLevel();

    else {

        Department dept = this.deptServ.getById(parentId);

        request.setAttribute("parent",dept);

        list = new ArrayList<Department>(dept.getChildren());

    }

    // 将获取后的部门列表存放于request的属性中

    request.setAttribute("departmentList", list);

    // 跳转到显示部门列表页面

    return mapping.findForward("list"); // list.jsp

}

 

这里我有必要说一下我的实现思想,我受以前面向过程编程的思想的影响没有从完全面向对象的角度去解决这个实现。之前在老方的课堂上也是这样,我没有从完全面向对象的角度去思考他提出的问题,只想着最简洁、最快速的面向过程实现方法。

 

我在实现上面的方法时也忽略的面向对象的思想,直接使用hibernate去查询数据库。事实上完全没必要,因为部门之间的关系我们已经通过对象与映射文件描述出来了。直接使用对象的属性就可以了!比如,获取所有子部门。

 

2.跳转到添加部门的addUI方法

/**

 * 跳转至添加部门页面

 */

public ActionForward addUI(ActionMapping mapping, ActionForm form,

        HttpServletRequest request, HttpServletResponse response)

        throws Exception {

    // 获取部门列表,以备指定被添加部门的上级部门

    List<Department> list = this.deptServ.getTopLevel();

    // getAllDepartmentList方法将部门以树型的方法显示

    request.setAttribute("departmentList", DepartmentUtils.getAllDepartmentList(list));

    // 跳转至添加部门页面

    return mapping.findForward("saveUI"); // saveUI.jsp

}

 

这里只有以树型显示部门列表的方法是个小重点,我们使用了递归的方法实现的:

public static List<Department> getAllDepartmentList(

        List<Department> topLevelList) {

    List<Department> list = new ArrayList<Department>();

    formatDepartmentList(topLevelList, "", list);

    return list;

}

 

private static void formatDepartmentList(Collection<Department> deptList,

        String prefix, List<Department> reval) {

    for (Department dept : deptList) {

        // 新创建一个Department,防止修改hibernate缓存中的数据记录。

        Department newDept = new Department();

        newDept.setId(dept.getId());

        newDept.setName(prefix + dept.getName());

        // 添加到返回值接收的List

        reval.add(newDept);

        // 递归调用子部门

        formatDepartmentList(dept.getChildren(), " " + prefix, reval);

    }

}

 

3.添加部门的add方法

/**

 * 添加部门

 */

public ActionForward add(ActionMapping mapping, ActionForm form,

        HttpServletRequest request, HttpServletResponse response)

        throws Exception {

    // 获取表单提交过来的信息,转成DepartmentBean数据

    DepartmentActionForm deptForm = (DepartmentActionForm) form;

    Department dept = new Department();

    dept.setName(deptForm.getName());

    dept.setParent(this.deptServ.getById(deptForm.getParentId()));

    // 保存数据

    this.deptServ.save(dept);

    // 因为添加部门后我们需要显示与被添加部门的所有同级部门,所以传递给显示页面上级部门的ID参数。

    ActionForward af = mapping.findForward("toList");

    return new ActionForward(af.getPath() + "&parentId="

            + deptForm.getParentId(),af.getRedirect());

}

 

4.跳转到修改部门的editUI方法

/**

 * 跳转到修改部门页面

 */

public ActionForward editUI(ActionMapping mapping, ActionForm form,

        HttpServletRequest request, HttpServletResponse response)

        throws Exception {

    // 获取部门列表,以备指定被修改部门的上级部门

    List<Department> list = this.deptServ.getTopLevel();

    request.setAttribute("departmentList", DepartmentUtils.getAllDepartmentList(list));

    // 将被修改部门的信息封装到ActionForm

    DepartmentActionForm deptForm = (DepartmentActionForm) form;

    Department dept = this.deptServ.getById(deptForm.getId());

    deptForm.setName(dept.getName());

    if (dept.getParent() != null)

        deptForm.setParentId(dept.getParent().getId());

    // 跳转至修改部门页面

    return mapping.findForward("saveUI"); // saveUI.jsp

}

 

5.修改部门的edit方法

/**

 * 修改部门

 */

public ActionForward edit(ActionMapping mapping, ActionForm form,

        HttpServletRequest request, HttpServletResponse response)

        throws Exception {

    // 获取表单提交过来的信息,转成DepartmentBean数据

    DepartmentActionForm deptForm = (DepartmentActionForm) form;

    // 先到数据库中获取被修改部门的记录,是因为BeanActionForm属性不一致。

    Department dept = this.deptServ.getById(deptForm.getId());

    dept.setName(deptForm.getName());

    dept.setParent(this.deptServ.getById(deptForm.getParentId()));

    // 更新数据

    this.deptServ.update(dept);

    // 因为更新部门后我们需要显示与被添加部门的所有同级部门,所以在传递给显示页面上级部门的ID

    ActionForward af = mapping.findForward("toList");

    return new ActionForward(af.getPath() + "&parentId="

            + deptForm.getParentId(),af.getRedirect());

}

 

6.删除部门

/**

 * 删除部门

 */

public ActionForward del(ActionMapping mapping, ActionForm form,

        HttpServletRequest request, HttpServletResponse response)

<p class="MsoNo
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值