Package org.huihoo.workflow.impl.store.spi

Source Code of org.huihoo.workflow.impl.store.spi.SpiUserDatabaseImpl

//----------------------------BEGIN LICENSE----------------------------
/*
* Willow : the Open Source WorkFlow Project
* Distributable under GNU LGPL license by gun.org
*
* Copyright (C) 2004-2010 huihoo.org
* Copyright (C) 2004-2010  ZosaTapo <dertyang@hotmail.com>
*
* ====================================================================
* Project Homepage : http://www.huihoo.org/willow
* Source Forge     : http://sourceforge.net/projects/huihoo
* Mailing list     : willow@lists.sourceforge.net
*/
//----------------------------END  LICENSE-----------------------------
package org.huihoo.workflow.impl.store.spi;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;

import org.huihoo.workflow.WorkflowException;
import org.huihoo.workflow.store.SchemaContext;
import org.huihoo.workflow.store.spi.AbstractSpiDatabase;
import org.huihoo.workflow.store.spi.SpiUserDatabase;
import org.huihoo.workflow.usermodel.WorkflowCategory;
import org.huihoo.workflow.usermodel.WorkflowDepartment;
import org.huihoo.workflow.usermodel.WorkflowGroup;
import org.huihoo.workflow.usermodel.WorkflowParticipant;
import org.huihoo.workflow.usermodel.WorkflowRank;
import org.huihoo.workflow.usermodel.WorkflowRole;

import com.zosatapo.commons.store.ConnUtils;
import com.zosatapo.commons.store.jResultSet;
import com.zosatapo.commons.store.jStatement;
import org.huihoo.workflow.impl.usermodel.WorkflowCategoryImpl;
import org.huihoo.workflow.impl.usermodel.WorkflowDepartmentImpl;
import org.huihoo.workflow.impl.usermodel.WorkflowGroupImpl;
import org.huihoo.workflow.impl.usermodel.WorkflowParticipantImpl;
import org.huihoo.workflow.impl.usermodel.WorkflowRankImpl;
import org.huihoo.workflow.impl.usermodel.WorkflowRoleImpl;
/**
* @author zosatapo
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public class SpiUserDatabaseImpl extends AbstractSpiDatabase implements SpiUserDatabase
{

  // ----------------------------------------------------- Instance Variables
  /**
   * The set of {@link Group}s defined in this database, keyed by
   * group id.
   */
  protected HashMap groups = new HashMap();

  /**
   * The set of {@link Departement}s defined in this database, keyed by
   * departement id.
   */
  protected HashMap departments = new HashMap();

  /**
   * The set of {@link WorkflowRole}s defined in this database, keyed by
   * role id.
   */
  protected HashMap roles = new HashMap();

  /**
   * The set of {@link WorkflowParticipant}s defined in this database, keyed by
   * user id.
   */
  protected HashMap users = new HashMap();

  /**
   * The set of {@link Rank}s defined in this database, keyed by
   * rank id.
   */
  protected HashMap ranks = new HashMap();

  /**
   * The set of {@link Category}s defined in this database, keyed by
   * category id.
   */
  protected HashMap categories = new HashMap();
  // ----------------------------------------------------------- Constructors
  /**
   * Create a new instance with default values.
   */
  public SpiUserDatabaseImpl()
  {
  }
  // ------------------------------------------------------------- Properties
  /**
   * Return the set of {@link Group}s defined in this user database.
   */
  public Iterator getGroups()
  {
    synchronized (groups)
    {
      return (groups.values().iterator());
    }
  }

  /**
   * Return the set of {@link Department}s defined in this user database.
   */
  public Iterator getDepartments()
  {
    synchronized (departments)
    {
      return (departments.values().iterator());
    }
  }

  /**
   * Return the set of {@link WorkflowRole}s defined in this user database.
   */
  public Iterator getRoles()
  {
    synchronized (roles)
    {
      return (roles.values().iterator());
    }
  }

  /**
   * Return the set of {@link WorkflowParticipant}s defined in this user database.
   */
  public Iterator getParticipants()
  {
    synchronized (users)
    {
      return (users.values().iterator());
    }
  }

  /**
     * Create and return a new {@link Group} defined in this user database.
     *
     * @param groupid     The group id of the new group (must be unique)
     * @param groupname   The group name of the new group
     * @param description The description of this group
     */
  public WorkflowGroup createGroup(String groupid, String groupname, String description)
  {
    WorkflowGroup group = new WorkflowGroupImpl(this, groupid, groupname, description);
    synchronized (groups)
    {
      groups.put(group.getUUID(), group);
    }
    return (group);
  }

  public WorkflowGroup createGroup(String groupid, String groupname, String description, WorkflowGroup parentGroup)
  {
    WorkflowGroup group = new WorkflowGroupImpl(this, groupid, groupname, description, parentGroup);

    synchronized (groups)
    {
      groups.put(group.getUUID(), group);
    }
    return (group);
  }

  /**
     * Create and return a new {@link Department} defined in this user database.
     *
     * @param departmentid     The department id of the new department (must be unique)
     * @param departmentname   The department name of the new department
     * @param description The description of this department
     */
  public WorkflowDepartment createDepartment(String departmentid, String departmentname, String description)
  {
    WorkflowDepartment department = new WorkflowDepartmentImpl(this, departmentid, departmentname, description);
    synchronized (departments)
    {
      departments.put(department.getUUID(), department);
    }
    return (department);
  }

  public WorkflowDepartment createDepartment(String departmentid, String departmentname, String description, WorkflowDepartment parentDepartment)
  {
    WorkflowDepartment department = new WorkflowDepartmentImpl(this, departmentid, departmentname, description, parentDepartment);

    synchronized (departments)
    {
      departments.put(department.getUUID(), department);
    }
    return (department);
  }

  /**
     * Create and return a new {@link WorkflowRole} defined in this user database.
     *
     * @param roleid     The role id of the new role (must be unique)
     * @param rolename   The role name of the new role
     * @param description The description of this role
     */
  public WorkflowRole createRole(String roleid, String rolename, String description)
  {
    WorkflowRole role = new WorkflowRoleImpl(this, roleid, rolename, description);
    synchronized (roles)
    {
      roles.put(role.getUUID(), role);
    }
    return (role);
  }
   
  /**
   * Create and return a new {@link WorkflowParticipant} defined in this user database.
   *
   * @param userid The logon userid of the new user
   * @param username The logon username of the new user (must be unique)
   * @param password The logon password of the new user
   * @param fullName The full name of the new user
   */
  public WorkflowParticipant createParticipant(
    String userid,
    String username,
    String password,
    String fullName)
  {
    WorkflowParticipantImpl user = new WorkflowParticipantImpl(this, userid, username, password, fullName);
    synchronized (users)
    {
      users.put(user.getUUID(), user);
    }
    return (user);
  }

  public WorkflowParticipant createParticipant(
    String userid,
    String username,
    String password,
    String fullName,
    String description)
  {
    WorkflowParticipantImpl user =
      new WorkflowParticipantImpl(this, userid, username, password, fullName, description);
    synchronized (users)
    {
      users.put(user.getUUID(), user);
    }
    return (user);
  }

  /**
   * Return the {@link Group} with the specified group id, if any;
   * otherwise return <code>null</code>.
   *
   * @param groupid id of the group to return
   */
  public WorkflowGroup findGroup(String groupid)
  {
    synchronized (groups)
    {
      return ((WorkflowGroup) groups.get(groupid));
    }
  }

  /**
   * Return the {@link Department} with the specified dept id, if any;
   * otherwise return <code>null</code>.
   *
   * @param deptid id of the dept to return
   */
  public WorkflowDepartment findDepartment(String deptid)
  {
    synchronized (departments)
    {
      return ((WorkflowDepartment) departments.get(deptid));
    }
  }

  /**
   * Return the {@link WorkflowRole} with the specified role id, if any;
   * otherwise return <code>null</code>.
   *
   * @param roleid id of the role to return
   */
  public WorkflowRole findRole(String roleid)
  {
    synchronized (roles)
    {
      return ((WorkflowRole) roles.get(roleid));
    }
  }
     
  /**
   * Return the {@link WorkflowParticipant} with the specified user id, if any;
   * otherwise return <code>null</code>.
   *
   * @param userid id of the user to return
   */
  public WorkflowParticipant findParticipant(String userid)
  {
    synchronized (users)
    {
      return ((WorkflowParticipant) users.get(userid));
    }
  }
 
  /**
   * Remove the specified {@link Group} from this user database.
   *
   * @param group The group to be removed
   */
  public void removeGroup(WorkflowGroup group)
  {
    synchronized (groups)
    {
      Iterator users = getParticipants();
      while (users.hasNext())
      {
        WorkflowParticipant user = (WorkflowParticipant) users.next();
        user.removeGroup(group);
      }

      Iterator depts = getDepartments();
      while (depts.hasNext())
      {
        WorkflowDepartment dept = (WorkflowDepartment) depts.next();
        dept.removeGroup(group);
      }
     
      groups.remove(group.getUUID());
    }
  }

  /** Remove the specified {@link Department} from this user database.
  *
  * @param dept The dept to be removed
  */
public void removeDepartment(WorkflowDepartment dept)
{
   synchronized (departments)
   {
     Iterator users = getParticipants();
     while (users.hasNext())
     {
       WorkflowParticipant user = (WorkflowParticipant) users.next();
       user.removeDepartment(dept);
     }
   
    Iterator groups = getGroups();
    while (groups.hasNext())
    {
      WorkflowGroup group = (WorkflowGroup) groups.next();
      group.setDepartment(null);
    }
   
    departments.remove(dept.getUUID());
   }
}
/** Remove the specified {@link WorkflowRole} from this user database.
   *
   * @param role The role to be removed
   */
  public void removeRole(WorkflowRole role)
  {
    synchronized (roles)
    {
      Iterator users = getParticipants();
      while (users.hasNext())
      {
        WorkflowParticipant user = (WorkflowParticipant) users.next();
        user.removeRole(role);
      }
     roles.remove(role.getUUID());
    }
  }

  /**
   * Remove the specified {@link WorkflowParticipant} from this user database.
   *
   * @param user The user to be removed
   */
  public void removeParticipant(WorkflowParticipant user)
  {
    synchronized (users)
    {   
     Iterator groups = getParticipants();
     while (groups.hasNext())
     {
       WorkflowGroup group = (WorkflowGroup) groups.next();
       group.removeParticipant(user);
     }

     Iterator roles = getRoles();
     while (roles.hasNext())
     {
      WorkflowRole role = (WorkflowRole) roles.next();
      role.removeParticipant(user);
     }
    
     Iterator depts = getDepartments();
     while (depts.hasNext())
     {
      WorkflowDepartment dept = (WorkflowDepartment) depts.next();
      dept.removeParticipant(user);
     }
         
     users.remove(user.getUUID());
   
    }
  }

  public void start() throws WorkflowException
  {
    if(opened)
    {
       return;
    }
    super.start();
   
    jResultSet jrs = null;
    String strSQL = null;
    try
    {
      //---------------------------------------------create rank
      strSQL =
        "SELECT  vc_uuid,vc_name,vc_description "
          + " FROM "
          + getSchemaContext().getTableName(SchemaContext.SCHEMA_RANK);

      jrs = jStatement.executeQuery(getStore(), strSQL);

      int sizeRank = jrs.getRowCount();
      for (int i = 1; i <= sizeRank; ++i)
      {
        jrs.absolute(i);
        ranks.put(
          jrs.getString("vc_uuid"),
          new WorkflowRankImpl(
            jrs.getString("vc_uuid"),
            jrs.getString("vc_name"),
            jrs.getString("vc_description")));
      }
      jrs.release();

      //---------------------------------------------create category
      strSQL =
        "SELECT  vc_uuid,vc_name,vc_description "
          + " FROM "
          + getSchemaContext().getTableName(SchemaContext.SCHEMA_CATEGORY);

      jrs = jStatement.executeQuery(getStore(), strSQL);

      int sizeCategory = jrs.getRowCount();
      for (int i = 1; i <= sizeCategory; ++i)
      {
        jrs.absolute(i);
        categories.put(
          jrs.getString("vc_uuid"),
          new WorkflowCategoryImpl(
            jrs.getString("vc_uuid"),
            jrs.getString("vc_name"),
            jrs.getString("vc_description")));
      }
      jrs.release();

      //---------------------------------------------create role
      strSQL =
        "SELECT  vc_uuid,vc_name,vc_description "
          + " FROM "
          + getSchemaContext().getTableName(SchemaContext.SCHEMA_ROLE);

      jrs = jStatement.executeQuery(getStore(), strSQL);

      int sizeRole = jrs.getRowCount();
      for (int i = 1; i <= sizeRole; ++i)
      {
        jrs.absolute(i);
        createRole(
            jrs.getString("vc_uuid"),
            jrs.getString("vc_name"),
            jrs.getString("vc_description"));
      }
      jrs.release();
     
      //---------------------------------------------create users
      strSQL =
        "SELECT vc_uuid,vc_name,vc_password,vc_fullname,vc_description,vc_rankid "
          + " "
          + " FROM "
          + getSchemaContext().getTableName(SchemaContext.SCHEMA_USER);

      jrs = jStatement.executeQuery(getStore(), strSQL);

      int sizeUser = jrs.getRowCount();
      WorkflowParticipant participant = null;
      for (int i = 1; i <= sizeUser; ++i)
      {
        jrs.absolute(i);
        participant =
          createParticipant(
            jrs.getString("vc_uuid"),
            jrs.getString("vc_name"),
            jrs.getString("vc_password"),
            jrs.getString("vc_fullname"),
            jrs.getString("vc_description"));

        participant.setRank((WorkflowRank) ranks.get(jrs.getString("vc_rankid")));
      }

      jrs.release();

      //---------------------------------------------set user administrator
      strSQL =
        "SELECT vc_userid,vc_packageId "
          + " "
          + " FROM "+ getSchemaContext().getTableName(SchemaContext.SCHEMA_ADMIN);

      jrs = jStatement.executeQuery(getStore(), strSQL);

      int sizeAdmin = jrs.getRowCount();
      for (int i = 1; i <= sizeAdmin; ++i)
      {
        jrs.absolute(i);
        WorkflowParticipantImpl participantImpl=(WorkflowParticipantImpl)this.findParticipant(jrs.getString("vc_userid"));     
       
        if("all".equalsIgnoreCase(jrs.getString("vc_packageId")))
        {
          participantImpl.setSuperAdministrator(true);
        }
        else
        {
          participantImpl.addPackage(jrs.getString("vc_packageId"));
        }
      }

      jrs.release();
     
      //---------------------------------------------create departments
      genDepartmentTree(null);     

      //---------------------------------------------create groups
      genGroupTree(null);
     
      //---------------------------------------------create user group relation
      strSQL =
        "SELECT vc_userid,vc_groupid FROM "
          + getSchemaContext().getTableName(SchemaContext.SCHEMA_USER_GROUP);

      jrs = jStatement.executeQuery(getStore(), strSQL);
      int sizeUserGroup = jrs.getRowCount();
      for (int i = 1; i <= sizeUserGroup; ++i)
      {
        jrs.absolute(i);
        String groupid = jrs.getString("vc_groupid");
        String userid = jrs.getString("vc_userid");
        WorkflowGroup workflowGroup = findGroup(groupid);
        WorkflowParticipant workflowUser = findParticipant(userid);
        workflowGroup.addPaticipant(workflowUser);
      }
      jrs.release();

      //---------------------------------------------create user department relation
      strSQL =
        "SELECT vc_userid,vc_departmenetid FROM "
          + getSchemaContext().getTableName(SchemaContext.SCHEMA_USER_DEPARTMENT);

      jrs = jStatement.executeQuery(getStore(), strSQL);
      int sizeUserDept = jrs.getRowCount();
      for (int i = 1; i <= sizeUserDept; ++i)
      {
        jrs.absolute(i);
        String deptid = jrs.getString("vc_departmenetid");
        String userid = jrs.getString("vc_userid");
        WorkflowDepartment workflowDept = findDepartment(deptid);
        WorkflowParticipant workflowUser = findParticipant(userid);
        workflowDept.addPaticipant(workflowUser);
      }
      jrs.release();

      //---------------------------------------------create user role relation
      strSQL =
        "SELECT vc_userid,vc_roleid FROM "
          + getSchemaContext().getTableName(SchemaContext.SCHEMA_USER_ROLE);

      jrs = jStatement.executeQuery(getStore(), strSQL);
      int sizeUserRole = jrs.getRowCount();
      for (int i = 1; i <= sizeUserRole; ++i)
      {
        jrs.absolute(i);
        String roleid = jrs.getString("vc_roleid");
        String userid = jrs.getString("vc_userid");
        WorkflowRole workflowRole = findRole(roleid);
        WorkflowParticipant workflowUser = findParticipant(userid);
        workflowRole.addPaticipant(workflowUser);
      }
      jrs.release();
     
    }
    catch (SQLException sqlex)
    {
      throw new WorkflowException(sqlex);
    }
  }

  //---------------------------------------------------------------------------------
  // generate department tree
  //--------------------------------------------------------------------------------- 
  private void genDepartmentTree(WorkflowDepartment parentDept) throws SQLException
  {
    String strSQL =
      "SELECT  vc_uuid,vc_name,vc_description,vc_categoryid FROM "
        + getSchemaContext().getTableName(SchemaContext.SCHEMA_DEPARTMENT);

    if (parentDept == null)
    {
      strSQL += " WHERE vc_parent IS NULL";
      jResultSet jrs = jStatement.executeQuery(getStore(), strSQL);

      int sizeGroup = jrs.getRowCount();
      for (int i = 1; i <= sizeGroup; ++i)
      {
        jrs.absolute(i);
        WorkflowDepartment workflowDepartment =
          createDepartment(
            jrs.getString("vc_uuid"),
            jrs.getString("vc_name"),
            jrs.getString("vc_description"),
            parentDept);
        workflowDepartment.setCategory((WorkflowCategory)categories.get(jrs.getString("vc_categoryid")));
        genDepartmentTree(workflowDepartment);
      }

      jrs.release();
    }
    else
    {
      Connection conn = null;
      PreparedStatement pstmt = null;
      ResultSet rs = null;

      try
      {
        strSQL += " WHERE vc_parent=?";

        conn = ConnUtils.getConnection(getStore());
        pstmt = conn.prepareStatement(strSQL);
        pstmt.setString(1, parentDept.getUUID());
        rs = pstmt.executeQuery();
        while (rs.next())
        {
          WorkflowDepartment workflowDepartment =
            createDepartment(
              rs.getString("vc_uuid"),
              rs.getString("vc_name"),
              rs.getString("vc_description"),
          parentDept);
          workflowDepartment.setCategory((WorkflowCategory)categories.get(rs.getString("vc_categoryid")));
          genDepartmentTree(workflowDepartment);
        }
      }
      finally
      {
        ConnUtils.cleanupNoThrow(conn, pstmt);
      }

    }

  }

  //---------------------------------------------------------------------------------
  // generate group tree
  //---------------------------------------------------------------------------------
  private void genGroupTree(WorkflowGroup parentGroup) throws SQLException
  {
    String strSQL =
      "SELECT  vc_uuid,vc_name,vc_description,vc_departmentid FROM "
        + getSchemaContext().getTableName(SchemaContext.SCHEMA_GROUP);

    if (parentGroup == null)
    {
      strSQL += " WHERE vc_parent IS NULL";
      jResultSet jrs = jStatement.executeQuery(getStore(), strSQL);

      int sizeGroup = jrs.getRowCount();
      for (int i = 1; i <= sizeGroup; ++i)
      {
        jrs.absolute(i);
        WorkflowGroup workflowGroup =
          createGroup(
            jrs.getString("vc_uuid"),
            jrs.getString("vc_name"),
            jrs.getString("vc_description"),
            parentGroup);
        workflowGroup.setDepartment((WorkflowDepartment)departments.get(jrs.getString("vc_departmentid")));
        genGroupTree(workflowGroup);
      }

      jrs.release();
    }
    else
    {
      Connection conn = null;
      PreparedStatement pstmt = null;
      ResultSet rs = null;

      try
      {
        strSQL += " WHERE vc_parent=?";

        conn = ConnUtils.getConnection(getStore());
        pstmt = conn.prepareStatement(strSQL);
        pstmt.setString(1, parentGroup.getName());
        rs = pstmt.executeQuery();
        while (rs.next())
        {
          WorkflowGroup workflowGroup =
            createGroup(
              rs.getString("vc_uuid"),
              rs.getString("vc_name"),
              rs.getString("vc_description"),
              parentGroup);
          workflowGroup.setDepartment((WorkflowDepartment)departments.get(rs.getString("vc_departmentid")));
          genGroupTree(workflowGroup);
        }
      }
      finally
      {
        ConnUtils.cleanupNoThrow(conn, pstmt);
      }

    }

  }
}
TOP

Related Classes of org.huihoo.workflow.impl.store.spi.SpiUserDatabaseImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.