田源
2024-03-07 4b4083fd73dc27ece42f4835483565eef0e4f608
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
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<Pvolume> getPvolumes() {
//        return (List<Pvolume>) 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<Pvolume> 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<String,Type> map = new HashMap<String,Type>();
                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<String,Type> map = new HashMap<String,Type>();
                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;
            }
        });
        
    }
    /**
     * 返回一个在服务器端有效的卷对象
     * <p>此卷对象只有卷路径值,其它值均为空,其卷路径值是 ${user.dir}\attachments\</p>
     * <p>其中${user.dir}将用System.getProperty("user.dir")的值替换</p>
     * <p>Description: </p>
     * @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);
            }
        });
    }
    
}