📄 taffair.java
字号:
package com.oa.module.affair.affairdao;
import java.io.File;
import java.io.FileInputStream;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.beanutils.BeanUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.springframework.orm.hibernate3.HibernateTemplate;
import com.oa.module.affair.affair.Transstep;
import com.oa.module.affair.affairreq.AffairReqForm;
import com.oa.module.affair.affairreq.Transreq;
import com.oa.module.affair.affairtype.AffairTypeForm;
import com.oa.module.affair.affairtype.Transtype;
import com.oa.util.ToolUtil;
import com.oa.util.XPage;
/**
* 事务定制DAO接口实现
* @author admin
*
*/
public class TAffair implements IAffair {
private SessionFactory sf;
Session session = null;
Transaction tx = null;
// private HibernateTemplate ht = null;
public SessionFactory getSf() {
return sf;
}
public void setSf(SessionFactory sf) {
this.sf = sf;
}
public boolean checkName(AffairTypeForm affairForm) {
session = sf.openSession();
Query query = session.createQuery("select a from Transtype a where a.tname=:tname");
query.setParameter("tname",affairForm.getTname());
int count = query.list().size();
if(count>0)return true;
else return false;
}
public boolean addAffairType(Transtype type, HttpServletRequest request) {
try {
session = sf.openSession();
tx = session.beginTransaction();
session.save(type);
tx.commit();
session.flush();
return true;
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
}finally{
session.close();
sf.close();
}
return false;
}
public boolean isused(String transID){
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String sql = null;
session = sf.openSession();
sql = "select * from transreq t where t.tno =" + transID;
session = sf.openSession();
conn =session.connection();
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if(rs.next()){
return true;
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}finally {
try {
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
return false;
}
public HashMap getrole(String transID) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String sql = null;
String roleid = "";
String rolename = "";
HashMap role = new HashMap();
session = sf.openSession();
conn =session.connection();
try {
stmt = conn.createStatement();
sql = "select t.trid from transtype t where t.rtid =" + transID;
rs = stmt.executeQuery(sql);
while (rs.next()) {
if (roleid.equals("")) {
roleid = rs.getString("trid");
} else {
roleid = roleid + "," + rs.getString("trid");
}
}
role.put("roleid", roleid);
sql = "select t.rid ,t.rname from trole t where t.rid in (" + roleid + ") order by t.rid";
rs = stmt.executeQuery(sql);
while (rs.next()) {
if (rolename.equals("")) {
rolename = rs.getString("rname");
} else {
rolename = rolename + "," + rs.getString("rname");
}
}
role.put("rolename", rolename);
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
return role;
}
public Map getaffair(String transID) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String sql = null;
HashMap affair = new HashMap();
session = sf.openSession();
sql = "select * from transtype t where t.rtid =" + transID;
conn =session.connection();
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if (rs.next()) {
affair.put("rtid", rs.getString("rtid"));
affair.put("tname", rs.getString("tname"));
affair.put("txmlpath", rs.getString("txmlpath"));
affair.put("tstep", rs.getString("tstep"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
return affair;
}
public List getSteps(String dir, int count) {
Map map = null;
List translist = null;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String sql = null;
session = sf.openSession();
conn =session.connection();
try{
stmt = conn.createStatement();
translist = new ArrayList();
String userids = "";
String roleids = "";
SAXBuilder sb = new SAXBuilder();
Document dos = sb.build(new FileInputStream(dir));
Element root = dos.getRootElement();
List list = root.getChildren();
for (int i = 0; i < count; i++) {
userids = "";//拥有的用户
roleids = "";//拥有的角色
Element allnode = (Element) list.get(i);
List nodeList = allnode.getChildren();
for (int j = 0; j < nodeList.size(); j++) {
Element node = (Element) nodeList.get(j);
if (node.getName().equals("userid")) {
if (j == 0 || userids.trim().equals("")) {
userids = userids + node.getText();
}
} else if (node.getName().equals("roleid")) {
if (j == 0 || roleids.trim().equals("")) {
roleids = roleids + node.getText();
}
}
}
map = new HashMap();
if (userids != null && !userids.trim().equals("")) {
// 获得用户名称
String usernames = "";
String[] users = userids.split(",");
for (int j=0 ;j<users.length;j++){
sql = "select UNO,UTRUENAME from tuser where UNO="+ users[j];
rs = stmt.executeQuery(sql);
while (rs.next()) {
if (usernames.equals("")) {
usernames = rs.getString("utruename");
} else {
usernames = usernames + ","+ rs.getString("utruename");
}
}
}
map.put("username", usernames);
// map.put("userids", "userid" + i);
map.put("userid", userids);
}
if (roleids != null && !roleids.trim().equals("")) {
// 获得角色名
String rolenames = "";
String[] roles = roleids.split(",");
for(int k=0;k<roles.length;k++){
sql = "select t.rid,t.rname from trole t where t.rid =" + roles[k];
rs = stmt.executeQuery(sql);
while (rs.next()) {
if (rolenames.equals("")) {
rolenames = rs.getString("rname");
} else {
rolenames = rolenames + "," + rs.getString("rname");
}
}
}
map.put("rolename", rolenames);
// map.put("roleids", "roleids" + i);
map.put("roleid", roleids);
}
translist.add(map);
}
}catch(Exception e){
// e.printStackTrace();
return null;
}
finally{
}
return translist;
}
public XPage gettypelist(int currentPage, int count, Transtype typebean) {
String hql="select a from Transtype a where 1=1";
String path="/oa/affairType.do?task=getlist&";
XPage page = new XPage();
//开始设置xpage
page.setCurrentPage(currentPage);
page.setCount(count);
//动态的构建hql语句和xpage的path
if(typebean!=null){
if(typebean.getTname()!=null&&!typebean.getTname().trim().equals("")){
hql+=" and a.tname like :name";
path+="tname="+typebean.getTname()+"&";
}
}
//设置分页路径
page.setPath(path);
Session session = null;
Query query =null;
List list = null;
try {
session = sf.openSession();
query = session.createQuery(hql);
//设置查询参数
if(typebean!=null){
if(typebean.getTname()!=null&&!typebean.getTname().trim().equals("")){
query.setParameter("name","%"+typebean.getTname()+"%");
}
}
//获取总记录数
int allcount = query.list().size();
page.setAllCount(allcount);
//得到分页显示数据
query.setFirstResult((currentPage-1)*count);
query.setMaxResults(count);
list = query.list();
page.setList(list);
session.flush();
} catch (Exception e) {
e.printStackTrace();
}finally{
session.close();
sf.close();
}
return page;
}
public boolean updateType(String transID ,HttpServletRequest request ) {
Transtype type = new Transtype();
int id = Integer.parseInt(transID);
try {
session = sf.openSession();
type = (Transtype)session.get(Transtype.class,new Long(id));
type.setTname(request.getParameter("tname"));
tx = session.beginTransaction();
session.update(type);
tx.commit();
session.flush();
return true;
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
}finally{
session.close();
sf.close();
}
return false;
}
public boolean deltyperole(String transID) {
String sqls[] =new String[transID.length()];
for(int i=0;i<sqls.length;i++){
sqls[i] = "delete from ttyperole where rtid = "+transID;
}
boolean flag = this.executeUpdates(sqls);
if(flag){
return true;
}
return false;
}
public boolean delaffair(String rtid, String dir) {
boolean flag = false;
Session session = null;
Transaction tx = null;
// 先删除XML文件
if (this.delXml(rtid, dir)) {
try {
int id = Integer.parseInt(rtid);
session = this.sf.openSession();
tx = session.beginTransaction();
Transtype transtype = (Transtype)session.get(Transtype.class, new Long(id));
session.delete(transtype);
tx.commit();
session.flush();
flag = true;
} catch (Exception e) {
tx.rollback();
e.printStackTrace();
} finally {
session.close();
this.sf.close();
}
}else{
try {
int id = Integer.parseInt(rtid);
session = this.sf.openSession();
tx = session.beginTransaction();
Transtype transtype = (Transtype)session.get(Transtype.class, new Long(id));
session.delete(transtype);
tx.commit();
session.flush();
flag = true;
} catch (Exception e) {
tx.rollback();
e.printStackTrace();
} finally {
session.close();
this.sf.close();
}
}
return flag;
}
private boolean delXml(String rtid, String dir) {
Session session = null;
Connection conn = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -