package com.supporter.prj.ciecc.oa.doc;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Date;
import java.util.StringTokenizer;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.jbpm.taskmgmt.exe.TaskInstance;
import org.jbpm.graph.exe.Token;
import com.supporter.db.DBAgent;
import com.supporter.db.DBAgentBO;
import com.supporter.db.DataSet;
import com.supporter.db.SQLQuery;
import com.supporter.util.CodeTable;
import com.supporter.util.CommonUtil;
import com.supporter.util.XCalendar;
import com.supporter.prj.eip.dept.Department;
import com.supporter.prj.eip_service.EIPService;
import com.supporter.prj.eip_service.workflow.IWfRole;
import com.supporter.prj.hibernate.HibernateUtil;
import com.supporter.prj.jbpm.ProcStatus;
import com.supporter.prj.jbpm.TaskResult;
import com.supporter.prj.jbpm.WfClient;
import com.supporter.prj.log.LoggerClient;
import com.supporter.prj.org_interface.Dept;
import com.supporter.prj.org_interface.Person;
import com.supporter.security.UserProfile;
/**
* Doc 的BO(Manager)类.
* @author CodeGenerator
*/
public class DocBO {
private static final String LOG_DOC = "OA_DOC";
private static DocBO ibo_Manager = null;
private int ii_YearPrefix = 0; //前缀
private int ii_SerialNoIndex = 0; //公文流水号数字
private int ii_NewDocId = 0; //最新的公文编号
/**
* 构造方法.
*/
private DocBO() {
super();
ii_YearPrefix = CommonUtil.parseInt(CommonUtil.format((new Date()),"yyyy"));
//流水号
String ls_LatestSerialNo = getLatestSerialNoInDB();
this.setLatestSerialNo(ls_LatestSerialNo);
}
/**
* 获取唯一实例.
* @return
*/
public static synchronized DocBO getInstance(){
if (ibo_Manager != null)
return ibo_Manager;
else {
ibo_Manager = new DocBO();
return ibo_Manager;
}
}
public static Session getSession() {
return HibernateUtil.getCurrentSession();
}
/**
* 得到公文实例.
* @param id 唯一记录标识
* @return Doc实例. 如果al_Id小于0,那么返回一个新的实例.
* @return
*/
public static Doc getDoc(Integer id){
Session session = getSession();
return (Doc)session.get(Doc.class, id);
}
/**
* 得到公文流转实例.
* @param id 唯一记录标识
* @return DocFlow实例.
* @return
*/
public static DocFlow getDocFlow(Integer id){
Session session = getSession();
return (DocFlow)session.get(DocFlow.class, id);
}
/**
* 得到单个公文发文单位记录.
* @param id
* @return
*/
public static DocSentTo getDocSentTo(String id){
Session session = getSession();
return (DocSentTo)session.get(DocSentTo.class, id);
}
/**
* 根据公文和发文单位名称得到公文发文单位记录.
* @param doc
* @param unitName
* @return
*/
@SuppressWarnings("unchecked")
public static DocSentTo getDocSentTo(Doc doc,String unitName){
if (doc == null || CommonUtil.trim(unitName).length() == 0)return null;
String hql = "from DocSentTo where docId=" + doc.getDocId()
+ " and unitName='" + CommonUtil.trim(unitName) + "'";
Session session = getSession();
List < DocSentTo > units = HibernateUtil.retrieve(hql,session);
if (HibernateUtil.notInTransaction(session) && session.isOpen()) session.close();
if (units == null || units.size() == 0)return null;
return units.get(0);
}
/**
* 得到印刷后发文单位实例.
* @param id
* @return
*/
public static DocSentToUnit getDocSentToUnit(String id){
Session session = getSession();
return (DocSentToUnit)session.get(DocSentToUnit.class, id);
}
/**
* 根据单位名称得到印刷后发文单位实例.
* @param name
* @return
*/
@SuppressWarnings("unchecked")
public static DocSentToUnit getDocSentToUnitByName(String name){
String hql = "from DocSentToUnit where unitName='" + name + "'";
Session session = getSession();
List < DocSentToUnit > units = HibernateUtil.retrieve(hql,session);
if (HibernateUtil.notInTransaction(session) && session.isOpen()) session.close();
if (units == null || units.size() == 0)return null;
return units.get(0);
}
/**
* 得到来文单位基本信息实例.
* @param id 唯一记录标识
* @return DocFlow实例.
* @return
*/
public static DocSrcUnit getDocSrcUnit(String id){
Session session = getSession();
return (DocSrcUnit)session.get(DocSrcUnit.class, id);
}
/**
* 根据来文单位的名称得到来文单位对象.
* @param unitName
* @return
*/
@SuppressWarnings("unchecked")
public static DocSrcUnit getDocSrcUnitByName(String unitName){
String hql = "from DocSrcUnit where unitName='" + unitName + "'";
Session session = getSession();
List < DocSrcUnit > units = HibernateUtil.retrieve(hql,session);
if (HibernateUtil.notInTransaction(session) && session.isOpen()) session.close();
if (units == null || units.size() == 0)return null;
return units.get(0);
}
/**
* 判断该条形码是否存在.
* @param as_BarCode 条形码值
* @return
*/
public static boolean barCodeExists(String as_BarCode) {
if (as_BarCode.length() == 0) return false;
String hql = "from Doc doc where doc.barCode='" + as_BarCode + "'";
Session session = getSession();
Object obj = session.createQuery(hql).uniqueResult();
if (HibernateUtil.notInTransaction(session) && session.isOpen()) session.close();
if (obj == null) return false;
return ((Long)obj).intValue() > 0;
}
/**
* 扫描枪根据条形码取得DOC实例.
* @param as_BarCode 条形码值
* @return
*/
@SuppressWarnings("unchecked")
public static Doc getDoc(String as_BarCode, UserProfile asess_UserProfile) {
if (as_BarCode.length() == 0) return null;
if (asess_UserProfile == null) return null;
String hql = "from Doc doc where doc.barCode='" + as_BarCode + "'"
+ " and " + getBrowseFilter(asess_UserProfile);
Session session = getSession();
List < Doc > docs = HibernateUtil.retrieve(hql,session);
if (HibernateUtil.notInTransaction(session) && session.isOpen()) session.close();
if (docs == null || docs.size() == 0)return null;
return docs.get(0);
}
/**
* 获取数据授权方面的HQL过滤条件.
* @return
*/
private static String getBrowseFilter(UserProfile userProfile){
if (userProfile == null) return "1 = 0";
String filter = CommonUtil.trim(
EIPService.getAuthorityService().getFilterForAccessibleObjects(userProfile,"OA.DOC_VIEW"));
if (filter.length() == 0) filter = "1=0";
filter += " or doc.registeredById = '" + userProfile.getPersonId() + "'";
filter = " (" + filter + ")";
return filter;
}
/**
* 新增一个Doc实例.
*/
public Doc newInstance(String as_DocType, UserProfile asess_UserProfile) {
Doc ldoc_D = new Doc();
ldoc_D.setDocId(getNewDocId());
ldoc_D.setDocType(as_DocType);
ldoc_D.setDocStatus(Doc.Status.DOC);
ldoc_D.setSerialNo(getNewSerialNo());
ldoc_D.setDocNo2(CommonUtil.format((new Date()),"yyyy"));
if (as_DocType.equals(Doc.DocType.FOR_HANDLING)) {
ldoc_D.setDocNo1("办");
ldoc_D.setDocNo3(this.getLatestDocNoIndexInDB(Doc.DocType.FOR_HANDLING,""));
}
if (as_DocType.equals(Doc.DocType.SIGNED_DOC)) {
ldoc_D.setDocNo1("办签");