📄 hibernatedocumentdao.java
字号:
package cn.myapps.core.dynaform.document.dao;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import org.hibernate.Query;
import org.hibernate.Session;
import cn.myapps.base.action.ParamsTable;
import cn.myapps.base.dao.DataPackage;
import cn.myapps.base.dao.HibernateBaseDAO;
import cn.myapps.core.dynaform.document.dql.DQLASTUtil;
import cn.myapps.core.dynaform.document.ejb.Document;
import cn.myapps.core.user.action.WebUser;
import cn.myapps.util.sequence.Sequence;
public class HibernateDocumentDAO extends HibernateBaseDAO implements
DocumentDAO {
public HibernateDocumentDAO(String voClassName) {
super(voClassName);
}
public void store(Document doc, WebUser user) throws Exception {
doc.setId(Sequence.getSequence());
super.create(doc, user);
}
public DataPackage queryByDQL(String dql, String application)
throws Exception {
if (dql != null && dql.trim().length() > 0) {
// Formula formula = Formula.parser(dql);
StringBuffer hql = new StringBuffer();
hql.append("FROM " + _voClazzName
+ " WHERE istmp <> true AND id in ( select doc.id from "
+ _voClazzName + " doc where ");
// hql.append(formula.toSqlString());
hql.append(DQLASTUtil.parseToHQL(dql));
hql.append(")");
// hql.append(" order by id");
hql.append(" order by sortId");
System.out.println(hql);
return this.getDatapackage(hql.toString());
} else {
return null;
}
}
public DataPackage queryByDQL(String dql, ParamsTable params,
String application) throws Exception {
if (dql != null && dql.trim().length() > 0) {
StringBuffer hql = new StringBuffer();
hql.append("FROM " + _voClazzName
+ " WHERE istmp <> true AND id in ( select doc.id from "
+ _voClazzName + " doc where ");
hql.append(DQLASTUtil.parseToHQL(dql, params));
hql.append(")");
// hql.append(" order by id");
hql.append(" order by sortId");
System.out.println(hql);
return this.getDatapackage(hql.toString());
} else {
return null;
}
}
public DataPackage queryByDQLPage(String dql, int page, int lines,
String application) throws Exception {
return queryByDQLPage(dql, new ParamsTable(), page, lines, application);
}
public DataPackage queryByDQLPage(String dql, ParamsTable params, int page,
int lines, String application) throws Exception {
if (dql != null && dql.trim().length() > 0) {
StringBuffer hql = new StringBuffer();
hql.append("FROM " + _voClazzName
+ " WHERE istmp <> true AND id in ( select doc.id from "
+ _voClazzName + " doc where ");
hql.append(DQLASTUtil.parseToHQL(dql, params));
hql.append(")");
if (application != null && application.length() > 0) {
hql.append(" and applicationid = '" + application + "' ");
}
hql.append(" order by sortId");
System.out.println(hql);
return this.getDatapackage(hql.toString(), page, lines);
} else {
return null;
}
}
public DataPackage queryByHQL(String hql, String application)
throws Exception {
StringBuffer query = new StringBuffer(hql);
if (hql != null && hql.trim().length() > 0) {
if (application != null && application.length() > 0) {
query.append(" and applicationid = '" + application + "' ");
}
query.append(" order by sortId");
return getDatapackage(query.toString());
} else {
return null;
}
}
public DataPackage queryByHQLPage(String hql, int page, int lines,
String application) throws Exception {
StringBuffer query = new StringBuffer(hql);
if (hql != null && hql.trim().length() > 0) {
if (application != null && application.length() > 0) {
query.append(" and applicationid = '" + application + "' ");
}
query.append(" order by sortId");
return getDatapackage(query.toString(), page, lines);
} else {
return null;
}
}
public DataPackage queryByFormName(String formname, String application)
throws Exception {
String hql = "from Document doc where doc.istmp <> true AND doc.formname="
+ "'" + formname + "'";
ParamsTable params = new ParamsTable();
params.setParameter("application", application);
return this.getDatapackage(hql, params);
}
public Document findByItemAndFormname(String itemName, String itemValue,
String formname, String application) throws Exception {
String hql = "FROM Document doc WHERE doc.istmp <> true AND doc.formname='"
+ formname
+ "' AND doc.id in ( SELECT item.document FROM Item item where item.name='"
+ itemName + "' AND item.varcharvalue='" + itemValue + "')";
if (application != null && application.length() > 0) {
hql += " and applicationid = '" + application + "' ";
}
Document doc = (Document) this.getData(hql);
return doc;
}
public Collection findDocsByItemAndFormname(String itemName,
String itemValue, String formname, String application)
throws Exception {
String hql = "FROM Document doc WHERE doc.istmp <> true AND doc.formname='"
+ formname
+ "' AND doc.id in ( SELECT item.document FROM Item item where item.name='"
+ itemName + "' AND item.varcharvalue='" + itemValue + "')";
ParamsTable params = new ParamsTable();
params.setParameter("application", application);
return getDatas(hql, params);
}
public Document findByItem(String itemName, double itemValue,
String application) throws Exception {
String hql = "FROM Document doc WHERE doc.istmp <> true AND doc.id in ( SELECT item.document FROM Item item where item.name='"
+ itemName + "' AND item.numbervalue='" + itemValue + "')";
if (application != null && application.length() > 0) {
hql += " and applicationid = '" + application + "' ";
}
Document doc = (Document) this.getData(hql);
return doc;
}
public Document findByItem(String itemName, Date itemValue,
String application) throws Exception {
String hql = "FROM Document doc WHERE doc.istmp <> true AND doc.id in ( SELECT item.document FROM Item item where item.name='"
+ itemName + "' AND item.datevalue='" + itemValue + "')";
if (application != null && application.length() > 0) {
hql += " and applicationid = '" + application + "' ";
}
Document doc = (Document) this.getData(hql);
return doc;
}
public void removeByItemAndFormname(String itemName, String itemValue,
String formname, String application) throws Exception {
Collection col = findDocsByItemAndFormname(itemName, itemValue,
formname, application);
for (Iterator iter = col.iterator(); iter.hasNext();) {
Document doc = (Document) iter.next();
this.remove(doc.getId());
}
}
public DataPackage queryByDQLAndDocumentLastModifyDate(String dql,
Date date, int page, int lines, String application)
throws Exception {
Session session = currentSession();
DataPackage pack = null;
if (dql != null && dql.trim().length() > 0) {
StringBuffer hql = new StringBuffer();
hql
.append("FROM "
+ _voClazzName
+ " WHERE istmp <> true and lastmodified >:date AND id in ( select doc.id from "
+ _voClazzName + " doc where ");
hql.append(DQLASTUtil.parseToHQL(dql));
hql.append(")");
if (application != null && application.length() > 0) {
hql.append(" and applicationid = '" + application + "' ");
}
hql.append(" order by id");
System.out.println(hql);
Query query = session.createQuery(hql.toString()).setTimestamp(
"date", date);
query.setFirstResult((page - 1) * lines);
query.setMaxResults(lines);
Collection coll = query.list();
pack = new DataPackage();
if (coll != null && coll.size() > 0)
pack.datas = coll;
}
return pack;
}
public int getNeedExportDocumentTotal(String dql, Date date,
String application) throws Exception {
int totalline = 0;
if (dql != null && dql.trim().length() > 0 && date == null) {
StringBuffer hql = new StringBuffer();
hql.append("FROM " + _voClazzName
+ " WHERE istmp <> true AND id in ( select doc.id from "
+ _voClazzName + " doc where ");
hql.append(DQLASTUtil.parseToHQL(dql));
hql.append(")");
if (application != null && application.length() > 0) {
hql.append(" and applicationid = '" + application + "' ");
}
hql.append(" order by sortId");
totalline = getTotalLines(hql.toString());
} else if ((dql != null && dql.trim().length() > 0 && date != null)) {
Session session = currentSession();
StringBuffer sb = new StringBuffer();
sb
.append("FROM "
+ _voClazzName
+ " WHERE istmp <> true and lastmodified >:date AND id in ( select doc.id from "
+ _voClazzName + " doc where ");
sb.append(DQLASTUtil.parseToHQL(dql));
sb.append(")");
if (application != null && application.length() > 0) {
sb.append(" and applicationid = '" + application + "' ");
}
sb.append(" order by sortId");
System.out.println(sb);
Long amount = new Long(0);
String hql = sb.toString();
int from = hql.toLowerCase().indexOf("from");
int order = hql.toLowerCase().indexOf("order by");
String newhql = (order > 0) ? "select count(*) "
+ hql.substring(from, order) : "select count(*) "
+ hql.substring(from);
Query query = session.createQuery(newhql)
.setTimestamp("date", date);
if (!query.list().isEmpty())
amount = (Long) query.list().get(0);
else
return 0;
return amount.intValue();
}
return totalline;
}
public String findFormFullName(String formid, String application)
throws Exception {
String hql = "SELECT DISTINCT vo.formname FROM " + _voClazzName
+ " vo WHERE vo.formid='" + formid + "'";
if (application != null && application.length() > 0) {
hql += " and vo.applicationid = '" + application + "' ";
}
return (String) super.getData(hql);
}
public Collection queryField(String fieldName, String moduleid,
String application) throws Exception {
String hql = "SELECT DISTINCT vo." + fieldName + " FROM "
+ _voClazzName + " vo, Form f "
+ " WHERE vo.formid=f.id AND f.module.id='" + moduleid + "'";
ParamsTable params = new ParamsTable();
params.setParameter("application", application);
return getDatas(hql, params);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -