package com.vci.server.framework.volume.service; import java.io.File; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.hibernate.HibernateException; import org.hibernate.type.Type; import com.vci.server.base.persistence.dao.BaseService; import com.vci.server.base.persistence.dao.HibernateCallback; import com.vci.server.base.persistence.dao.HibernateCallbackExt; import com.vci.server.base.persistence.dao.HibernateTemplate; import com.vci.server.framework.volume.dao.Pvolume; import com.vci.server.framework.volume.dao.PvolumeDaoImp; public class PvolumeService extends BaseService { /** * 添加 卷 * @param pvolume * @return */ public boolean savePvolume(final Pvolume pvolume){ return (Boolean)new HibernateTemplate().run(new HibernateCallback(){ public Object execute() throws HibernateException { PvolumeDaoImp impl = new PvolumeDaoImp(); pvolume.setUserEntity(userEntity); impl.save(pvolume); return true; } }); } /** * 读取所有 卷 * @return */ /*public List getRoleList(){ return (List)new HibernateTemplate().run(new HibernateCallback() { public Object execute() throws HibernateException { PvolumeDaoImp impl = new PvolumeDaoImp(); String hsql = "from Pvolume p order by p.name"; String sql = getPageSql(hsql); return impl.findEntities(sql); } }); }*/ //读取所有的卷条数 // public List getAllPvolumes() { // return VolumeCatch.getInstance().getAllPvolumes(); // } // @SuppressWarnings("unchecked") // private List getPvolumes() { // return (List) new HibernateTemplate().run(new HibernateCallback() { // public Object execute() throws HibernateException { // PvolumeDaoImp impl = new PvolumeDaoImp(); // String hsql = "from Pvolume p order by p.name"; // return impl.findEntities(hsql); // } // }); // } //分页读取数据 public List getPvolumesPage( final int pageIndex, final int pageSize){ return (List)new HibernateTemplate().run(new HibernateCallback() { public Object execute() throws HibernateException { PvolumeDaoImp impl = new PvolumeDaoImp(); String hsql = "select * from PLVOLUME p order by p.plname"; String sql = getPageSQL(hsql,pageSize,pageIndex); Object [] o = {}; return impl.findEntites(sql,o, "", Pvolume.class); } }); } // public Pvolume getDocumentVolumn(final String id) { // return (Pvolume)new HibernateTemplate().run(new HibernateCallback() { // public Object execute() throws HibernateException { // PvolumeDaoImp impl = new PvolumeDaoImp(); // String sql = "select {p.*} from pvolume p inner join pedmobj pedm on p.puid = pedm.pvolumeid where pedm.puid = ?"; // String[] values = new String[1]; // values[0] = id; // List list = impl.findEntites(sql, values, "p", Pvolume.class); // int size = list.size(); // if (size == 0) { // return null; // } else { // return list.get(0); // } // } // }); // } // public Pvolume getVolumnByName(final String name) { // return VolumeCatch.getInstance().getVolumnByName(name); // //// return (Pvolume)new HibernateTemplate().run(new HibernateCallback() { //// public Object execute() throws HibernateException { //// PvolumeDaoImp impl = new PvolumeDaoImp(); //// String sql = "select * from PLVOLUME p where p.plname = ?"; //// String[] values = new String[1]; //// values[0] = name; //// //// List list = impl.findEntites(sql, values, "", Pvolume.class); //// int size = list.size(); //// if (size == 0) { //// return null; //// } else { //// return list.get(0); //// } //// } //// }); // } /** * 修改卷 * @param pvolume */ public boolean updatePvolume(final Pvolume pvolume){ //VolumeCatch.getInstance().clearCatch(); return (Boolean)new HibernateTemplate().run(new HibernateCallback() { public Object execute() throws HibernateException { PvolumeDaoImp impl = new PvolumeDaoImp(); Pvolume pvo = impl.getById(pvolume.getId()); if (pvo == null) { pvolume.setUserEntity(userEntity); impl.save(pvolume); return true; } else { pvo.setService(pvolume.getService()); pvo.setHost(pvolume.getHost()); pvo.setName(pvolume.getName()); pvo.setPath(pvolume.getPath()); pvo.setType(pvolume.getType()); pvo.setIsvalid(pvolume.getIsvalid()); pvo.setUserEntity(userEntity); impl.saveOrUpdate(pvo); return true; } } }); } /** * 卷的删除 * @param id * @return */ public boolean deletePvolume(final String[] ids ){ //VolumeCatch.getInstance().clearCatch(); return (Boolean)new HibernateTemplate().run(new HibernateCallback() { public Object execute() throws HibernateException { PvolumeDaoImp impl = new PvolumeDaoImp(); String hql = "delete Pvolume p where p.id in ("; int len = ids.length; for (int i = 0; i < len; i++) { hql += "?"; if (i != len - 1) { hql += ","; } } hql += ")"; impl.deleteQueryObject(hql, ids, userEntity); return true; } }); } public void updatePvolumeInvalid(){ new HibernateTemplate().run(new HibernateCallback() { public Object execute() throws HibernateException { PvolumeDaoImp impl = new PvolumeDaoImp(); String hql = "update Pvolume p set p.isvalid = 0 where p.isvalid = 1"; impl.createQuery(hql); return null; }}); } /** * 查看卷是否被引用 * @return */ public int fetchVolumnInfoByIds(final String id ){ return (Integer)new HibernateTemplate().run(new HibernateCallback() { public Object execute() throws HibernateException { int count = 0; PvolumeDaoImp impl = new PvolumeDaoImp(); String hsql = " select count(*) from plvolumefile pfile where pfile.PLVOLUMEOID = '"+id+"'"; List list = new ArrayList(); Object[] values = new Object[0]; Map map = new HashMap(); list = impl.findEntitesBySQL(hsql, values,map); if (list != null){ count = Integer.valueOf(String.valueOf(list.get(0))).intValue(); } return count; } }); } /** * 检查要删除的卷是否为首选路径 * @param id * @return */ public boolean checkDelIsvalid(final String id ){ return (Boolean)new HibernateTemplate().run(new HibernateCallback(){ public Object execute() throws HibernateException { boolean res = false; PvolumeDaoImp impl = new PvolumeDaoImp(); String hsql = "select p.* from plvolume p where p.ploid = '"+id+"' and p.plisvalid = 1"; List list = new ArrayList(); Object[] values = new Object[0]; Map map = new HashMap(); list = impl.findEntitesBySQL(hsql, values,map); if (list.size() != 0){ res = true; }else { res = false; } return res; } }); } public Pvolume getIsvalidVolumeName(){ return (Pvolume)new HibernateTemplate().run(new HibernateCallback() { public Object execute() throws HibernateException { String volumepath = ""; PvolumeDaoImp impl = new PvolumeDaoImp(); Pvolume pvolume = new Pvolume(); List list = new ArrayList(); String sql = " from Pvolume p where p.isvalid = 1"; list = impl.findEntities(sql); if (list != null && list.size() > 0){ pvolume = (Pvolume)list.get(0); } return pvolume; } }); } /** * 返回一个在服务器端有效的卷对象 *

此卷对象只有卷路径值,其它值均为空,其卷路径值是 ${user.dir}\attachments\

*

其中${user.dir}将用System.getProperty("user.dir")的值替换

*

Description:

* @author xchao * @time 2013-8-6 * @return */ public Pvolume getIsvalidVolumeNameInServerEnv(){ String userDir = System.getProperty("user.dir"); String fileSeparator = System.getProperty("file.separator"); Pvolume res = new Pvolume(); res.setPath(userDir + fileSeparator + "attachments" + fileSeparator); File f = new File(res.getPath()); if(!f.exists()){ f.mkdirs(); } return res; } public Pvolume getProlumeById(final String id){ return (Pvolume)new HibernateTemplate().run(new HibernateCallback() { public Object execute() throws HibernateException { PvolumeDaoImp impl = new PvolumeDaoImp(); return impl.getById(id); } }); } //分页查询 public String getPageSQL(String sql, int pageSize, int pageIndex ){ if(pageIndex <= 0){ pageIndex = 1; } int startRownum = (pageIndex - 1) * pageSize + 1; int endRownum = pageIndex * pageSize; String partionSql = "" + "SELECT * FROM( " + " SELECT A.*,ROWNUM RN FROM( " + sql + " ) A " + ") WHERE RN <= " + String.valueOf(endRownum) + " AND RN >= " + String.valueOf(startRownum); return partionSql; } public boolean savePvolumUser(final String pvolumeId , final String[] userIds ){ return (Boolean)new HibernateTemplate().runExt(new HibernateCallbackExt() { public Object execute(Connection conn) throws HibernateException, SQLException { String sql = "delete pluserpvolume up where up.plpvolumeuid = ? "; PreparedStatement pstmt = null; PreparedStatement pstmt1 = null; try { pstmt = conn.prepareStatement(sql); pstmt.setString(1, pvolumeId); pstmt.execute(); } catch (SQLException e1) { e1.printStackTrace(); } finally { if (pstmt != null){ pstmt.close(); } } try { for (int i = 0 ; i < userIds.length ; i ++){ String hql = "insert into pluserpvolume values (? , ?)"; pstmt1 = conn.prepareStatement(hql); pstmt1.setString(1, userIds[i]); pstmt1.setString(2, pvolumeId); pstmt1.execute(); } }catch (SQLException e1) { e1.printStackTrace(); } finally { if (pstmt1 != null){ pstmt1.close(); } } return true; } }); } public Pvolume selectPvolume(final String id) { return (Pvolume)new HibernateTemplate().run(new HibernateCallback() { public Object execute() throws HibernateException { PvolumeDaoImp impl = new PvolumeDaoImp(); String hsql = "from Pvolume plvolume where plvolume.id = :id"; return impl.findEntity(hsql, "id", id); } }); } }