⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unitmanager.java

📁 OA典型例子
💻 JAVA
字号:
package com.sure.oa.orgnization;

import com.sure.businessmodel.Page;
import com.sure.businessmodel.UpdateException;
import com.sure.businesslogic.AlreadyExistsException;
import com.sure.businesslogic.NotFoundException;
import com.sure.dataabstraction.DBManager;
import com.sure.dataabstraction.DBPoolException;
import com.sure.util.StringUtils;
import com.sure.oa.role.*;
import java.sql.SQLException;
import java.sql.Connection;
import java.util.Vector;
import java.util.NoSuchElementException;
import java.io.*;
/**
 * <p>Title: OA</p>
 * <p>Description: 国办项目</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: sure</p>
 * @author mengzy
 * @version 1.0
 */

public class UnitManager {

  public UnitManager() {
  }

  /**
   * 根据单位ID获得单位详情
   * @param unitId
   * @return
   */
  public static  Unit getUnit(int unitId) throws SQLException,DBPoolException, NotFoundException {
      Connection cn = DBManager.getConnection();
      try {
          String where = "Where unitId = " + unitId + "";
          Vector beans = UnitPersistent.load(cn, where);
          Unit bean = (Unit)beans.firstElement();
          return bean;
      } catch (SQLException sqle) {
          throw new  NotFoundException();
      } finally {
          cn.close();
      }
  }
  public static  Unit getUnits(String unitId) throws SQLException,DBPoolException, NotFoundException {
     return  getUnit(Integer.parseInt(unitId));
  }

  public static String getUnitName(int unitId) throws SQLException,DBPoolException, NotFoundException {
      String unitName="";
      Connection cn = DBManager.getConnection();
      try {
          String where = "Where unitId = " + unitId + "";
          Vector beans = UnitPersistent.load(cn, where);
          Unit bean = (Unit)beans.firstElement();
          unitName =bean.getUnitName();
      } catch (SQLException sqle) {
          throw new  NotFoundException();
      } finally {
          cn.close();
          return unitName;
      }
  }

  public static  String  getUnitId(String unitName) throws SQLException,DBPoolException, NotFoundException {
     Connection cn = DBManager.getConnection();
     try {
         String where = "Where unitName = " + unitName + "";
         Vector beans = UnitPersistent.load(cn, where);
         Unit bean = (Unit)beans.firstElement();
         return  bean.getUnitId().toString();
     } catch (SQLException sqle) {
         throw new  NotFoundException();
     } finally {
         cn.close();
     }
 }

  public Vector getUnit(String unitId) throws SQLException, DBPoolException, NotFoundException {
        Connection cn = DBManager.getConnection();
        try {
            String where = "Where unitId = " + unitId + "";
            Vector beans = UnitPersistent.load(cn, where);
            return  beans;
        } catch (SQLException sqle) {
            throw new  NotFoundException();
        } finally {
            cn.close();
        }
    }

  /**
   * 获得直接下属单位列表
   * @param start
   * @param unitId
   * @修改日期:2004-5-13(海关丁工要求,每页显示16条记录)
   */
  public Page getLowerUnitList(int start,int unitId,String orderBy) throws SQLException,DBPoolException {
      Connection cn = DBManager.getConnection();
      try{
          String where = "where fatherId='"+unitId+"' and UnitType=0";
          if (!orderBy.equals("")){
            where = where + " order by " + orderBy;
          }
          //System.out.println(where);
          Page p = UnitPersistent.load(cn, start, 10, where);
          return p;
      }finally {
          cn.close();
      }
  }

  /**
   * 获得直接下属单位列表
   * @param unitId
   * @throws SQLException
   * @throws DBPoolException
   */
  public Vector getLowerUnitList(int unitId) throws SQLException,DBPoolException {
        Connection cn = DBManager.getConnection();
        try{
            String where = "where fatherId='"+unitId+"' and UnitType=0 order by unitXh";
            Vector v= UnitPersistent.load(cn,where);
            return  v;
        }finally {
            cn.close();
        }
  }

  /**
  * 获得某单位(部门)的直接下属部门(返回向量表)
  * @param unitId
  * @throws SQLException
  * @throws DBPoolException
  */
 public Page getLowerDeptList(int start,int unitId) throws SQLException,DBPoolException {
       Connection cn = DBManager.getConnection();
       try{
           String where = "where fatherId='"+unitId+"' and UnitType=1 order by unitId";
           Page p= UnitPersistent.load(cn,start,10,where);
           return  p;
       }finally {
           cn.close();
       }
   }

   /**
   * 获得某单位(部门)的直接下属部门(返回Page对象)
   * @param unitId
   * @throws SQLException
   * @throws DBPoolException
   */
  public Vector getLowerDeptList(int unitId) throws SQLException,DBPoolException {
        Connection cn = DBManager.getConnection();
        try{
            String where = "where fatherId='"+unitId+"' and UnitType=1 order by unitXh";
            Vector v= UnitPersistent.load(cn,where);
            return  v;
        }finally {
            cn.close();
        }
    }

    /**
     * 获得所有单位(除自己本单位)列表
     * 修改日期:2004-5-9
     * 修改原因:丁工提出可以给本单位发文,所以要能选出包括本单位在内的所有单位
     */
   public  Vector getAllUnitList(int unitId) throws SQLException,DBPoolException {
     Connection cn = DBManager.getConnection();
     try{
         //String where = "where unitId<>" + unitId + " and unitType=0";
         String where = "where unitType=0 and fatherId <> '0' order by unitXh";
         Vector v= UnitPersistent.load(cn,where);
         return  v;
     }finally {
         cn.close();
     }
   }

 /**
   * 保存单位信息
   * @param u
   * @return
   * @throws SQLException
   * @throws DBPoolException
   * @throws UpdateException
   * @throws NotFoundException
   */
  public void  saveUnit(Unit u) throws  UpdateException,SQLException,DBPoolException,NotFoundException{
   Connection cn = DBManager.getConnection();
   if (validateUnit(cn,u) == 1){
     try {
       cn.setAutoCommit(false);
       save(cn, u);
       cn.commit();
     }
     catch (Exception e) {
       cn.rollback();
       e.printStackTrace();
     }
     finally {
       cn.setAutoCommit(true);
       cn.close();
     }
   }
}

  /**
   * 删除单位
   * @param unit
   */
  public void  delUnit(Unit u) throws SQLException,DBPoolException, UpdateException, NotFoundException {
      Connection cn = DBManager.getConnection();
      String where="";
      int unitId=u.getUnitId().intValue();
      try{
        cn.setAutoCommit(false);
        if(u.getUnitType().equals("0")){
          UsersManager uMng = new UsersManager();
          uMng.delUsers(u.getManagerUserId());
          where = "where unitId=" + unitId + "";
          UnitPersistent.delete(cn, where);
        }
        cn.commit();
      } catch(Exception e) {
        cn.rollback();
        e.printStackTrace();
      } finally {
        cn.setAutoCommit(true);
        cn.close();
      }
  }

  /**
  * 删除部门
  * @param unit
  */
 public void  delUnit(int unitId) throws SQLException,DBPoolException, UpdateException, NotFoundException {
     Connection cn = DBManager.getConnection();
     String where="where unitId="+unitId+"";
     try{
       UnitPersistent.delete(cn,where);
     } catch(Exception e) {
       e.printStackTrace();
     } finally {
       cn.close();
     }
 }

  /**
   * 保存单位的信息
   * @param conn
   * @param u
   * @throws SQLException
   */
  private void  save(Connection conn, Unit u) throws SQLException,DBPoolException, UpdateException, NotFoundException
  {
    boolean exisit = !(u.getUnitId().intValue()==0);
    UnitPersistent unit = new UnitPersistent(u,exisit);
    unit.persist(conn);
    String unitId=unit.getBean().getUnitId().toString();
    if(u.getUnitType().equals("0")){
      Users user = u.getUnitManager();
      UsersManager um = new UsersManager();
      um.saveUsers(conn, user);
    }
    //如果是新建单位则需要初始化一个全能角色
    if(!exisit){
      role ro=createInitRole();
      ro.setUnitId(unitId);
      rolePersistent ros=new rolePersistent(ro);
      ros.persist(conn);
    }
  }

  /**
   * 验证Unit中的参数时候合格
   * @param u
   */
  public int validateUnit(Connection cn,Unit u) throws UpdateException,SQLException{
      int retval = 1;
      String sql = "";
      String sql1 = "";
      int unitId = u.getUnitId().intValue();
      String unitType = u.getUnitType();
      if (! (unitId == 0))
        sql = "  and unitId<>" + unitId + "";
      if (unitType.equals("1"))
        sql1 = "  and fatherId='" + u.getFatherId() + "'";
      String unitName = StringUtils.getSQLencode(u.getUnitName());
      String where = "Where unitName='" + unitName + "'" + sql + sql1;
      int intNameResult = UnitPersistent.getCount(cn, where);
      if (intNameResult > 0) {
        retval = 0;
        if (unitType.equals("1")) {
          throw new UpdateException("本单位已经存在" + unitName + "这个部门名称!");
        }
        else {
          throw new UpdateException("系统中已经存在" + unitName + "这个单位名称!");
        }
      }
      if (unitType.equals("0")) {
        String account = u.getManagerAccount();
        where = "Where  account = '" + account + "'  and userId<>" +
            u.getManagerUserId() + "";
        intNameResult = UsersPersistent.getCount(cn, where);
        if (intNameResult > 0) {
          retval = 0;
          throw new UpdateException("系统中已经存在" + account + "这个帐号");
        }
      }
     return retval;
 }

  public  role createInitRole(){
    role ro=new role();
    ro.setRoleId(0);
    ro.setRoleName("全能角色");
    ro.setContent("211,212,213,214,215,216,221,222,225,223,224,226,231,232,235,236,233,234,241,244,245,246,242,243,251,111,112,113,114,115,121,122,123,124,125,311,312,313,314,412");
    ro.setFileType("1");
    return ro;
  }

  /**
   * 保存单位排序信息
   */
  public  static void  saveUnitIndex(String unitList,String unitXhList) throws SQLException,DBPoolException, UpdateException, NotFoundException{
    Connection cn=DBManager.getConnection();
    try{
      cn.setAutoCommit(false);
      String strIds[] = StringUtils.split(unitList, ",");
      String strXhs[] = StringUtils.split(unitXhList, ",");
      Unit u;
      UnitPersistent us;
      String where="";
      for (int i = 0; i < strIds.length; i++) {
         where="where unitId="+strIds[i]+"";
         u=(Unit)UnitPersistent.load(cn,where).firstElement();
         u.setUnitXh(strXhs[i]);
         us=new UnitPersistent(u);
         us.setRecordExists(true);
         us.persist(cn);
      }
      cn.commit();
    }
    catch(Exception e)
    {
      cn.rollback();
      e.printStackTrace();
    }
    finally{
      cn.setAutoCommit(true);
      cn.close();
    }
  }

  /**
 * 根据用户输入的序号返回一个合法单位序号
 * @param strXuhao
 * @return
 */
 public static int validateUnitXuhao() throws DBPoolException,SQLException{
   Connection cn = DBManager.getConnection();
   int rtn = -1;
   try {
     String where = "where unitType='0'";
     Vector allUnitVector = UnitPersistent.load(cn, where);

     if(allUnitVector != null && allUnitVector.size()>0){
           for (int i = 0; i < allUnitVector.size(); i++) {
             Unit unitTemp = (Unit) allUnitVector.get(i);
             if (unitTemp.getUnitXh() != null &&
                 unitTemp.getUnitXh().intValue() > rtn) {
               rtn = unitTemp.getUnitXh().intValue();
             }
           }
           rtn++;
     }else{
       //如果没有单位,那么第一个单位的序号为0
       rtn = 0;
     }

   }
   finally{
     cn.close();
   }
   return rtn;
 }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -