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
350
351
352
353
354
355
356
357
358
359
360
package com.vci.server.framework.volume.delegate;
 
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
 
import com.vci.common.objects.UserEntity;
import com.vci.common.utility.ObjectUtility;
import com.vci.corba.common.VCIError;
import com.vci.corba.common.data.UserEntityInfo;
import com.vci.corba.framework.data.PvolumeInfo;
import com.vci.corba.log.data.LogType;
import com.vci.server.base.delegate.BaseDelegate;
import com.vci.server.base.delegate.UserEntityDelegate;
import com.vci.server.base.utility.LogRecordUtil;
import com.vci.server.cache.VolumeCacheProvider;
import com.vci.server.framework.cache.VolumeCatch;
import com.vci.server.framework.volume.dao.Pvolume;
import com.vci.server.framework.volume.service.PvolumeService;
 
public class PvolumeDelegate extends BaseDelegate {    
 
    private PvolumeService pvoSer = new PvolumeService();    
    
    public PvolumeDelegate(UserEntityInfo userEntityInfo){
        super(userEntityInfo);
    }
    public PvolumeDelegate(){
        
    }
    
    /**
     * 卷创�?
     * @param pvolumeInfo
     * @param userEntityInfo
     * @return
     */
    public String savePvolume(PvolumeInfo pvolumeInfo,UserEntityInfo userEntityInfo)throws VCIError{
        
        UserEntityDelegate.setUserEntityToService(pvoSer, userEntityInfo);
        
        if (StringUtils.isBlank(pvolumeInfo.id)) {
            String id = ObjectUtility.getNewObjectID36();
            pvolumeInfo.id = id;
        }
        
        Pvolume pvolume = changePvolumeInfoToPvolume(pvolumeInfo);
        //UserEntity userEntity = changeUserEntityInfoToUserEntity(userEntityInfo);
        try {
            //createFileCabinet(pvolume.getPath());
            boolean success = pvoSer.savePvolume(pvolume);
            if (success) {
                VolumeCatch.setVolume(pvolumeInfo);
            }
            LogRecordUtil.writeLog(userEntityInfo, "添加", success ? "添加成功" : "添加失败", pvolume.getLogInfo(),LogType.General,pvolume.getId());
        } catch (Exception e) {
            throw new VCIError("310100",new String[0]);
        }
        
 
        return pvolume.getId();
 
    }
    
    /**
     * 读取所有卷
     * @return
     * @throws VCIError
     */
    public PvolumeInfo[] getAllPvolumes() throws VCIError{
        return VolumeCacheProvider.getAllVolumes();
//        List<Pvolume> list = null;
//        try {
//            list = pvoSer.getAllPvolumes();
//        } catch (Exception e) {
//            throw new VCIError("310101",new String[0]); //获取卷信息出错,请重新获取�?
//        }
//        int size = list.size();
//        PvolumeInfo[] infos = new PvolumeInfo[size];
//        for (int i = 0; i < size; i++) {
//            infos[i] = changePvolumeToPvolumeInfo(list.get(i));
//        }
//        return infos;
        
    }
    //分页读取数据
    public PvolumeInfo[] getPvolumesPage(int pageSize, int pageIndex)throws VCIError{
        List<Pvolume> list = null;
        try {
            list = pvoSer.getPvolumesPage(pageSize,pageIndex);
        } catch (Exception e) {
            throw new VCIError("310101",new String[0]); //获取卷信息出错,请重新获取�?
        }
        int size = list.size();
        PvolumeInfo[] infos = new PvolumeInfo[size];
        for (int i = 0; i < size; i++) {
            infos[i] = changePvolumeToPvolumeInfo(list.get(i));
        }
        return infos;
    }
    
//    public PvolumeInfo getDocumentVolumn(String id) throws VCIError {
//        PvolumeInfo info = null;
//        try {
//            Pvolume pvolume = pvoSer.getDocumentVolumn(id);
//            info = changePvolumeToPvolumeInfo(pvolume);
//        } catch (Exception e) {
//            throw new VCIError("310102",new String[0]); 
//        }
//        
//        return info;
//    }
    
    public PvolumeInfo getVolumnByName(String name) throws VCIError {
        return VolumeCacheProvider.getVolume(name);
//        Pvolume pvolume = null;
//        try {
//            pvolume = pvoSer.getVolumnByName(name);
//            
//            return changePvolumeToPvolumeInfo(pvolume);
//        } catch (Exception e) {
//            throw new VCIError("310102",new String[0]); 
//        }
    }
 
    /**
     * 修改�?
     * @param pvoInfo
     * @param userEntityInfo
     * @return
     * @throws VCIError
     */
    public boolean updatePvolume(PvolumeInfo pvoInfo,UserEntityInfo userEntityInfo) throws VCIError{
        
        UserEntityDelegate.setUserEntityToService(pvoSer, userEntityInfo);
        Pvolume  pvolume = changePvolumeInfoToPvolume(pvoInfo);
        //UserEntity userEntity = changeUserEntityInfoToUserEntity(userEntityInfo);
        try{
            Pvolume  pvolumeBefore = pvoSer.getProlumeById(pvolume.getId());
            boolean success = pvoSer.updatePvolume(pvolume);
            if (success) {
                VolumeCatch.setVolume(pvoInfo);
            }
            StringBuilder logres = new StringBuilder();
            logres.append("更新前:"+pvolumeBefore.getLogInfo());
            logres.append("更新后:"+pvolume.getLogInfo());
            LogRecordUtil.writeLog(userEntityInfo, "更新", success ? "修改成功" : "修改失败", logres.toString(), LogType.General,pvolumeBefore.getId());
        }catch(Exception e){
            LogRecordUtil.writeLog(userEntityInfo, "更新", "修改失败", e.getMessage(), LogType.General, pvoInfo.id);
            throw new VCIError("310103", new String[0]); 
        }
 
        return true;
    }
    
    /**
     * 卷的删除
     * @param id
     * @param userEntityInfo
     * @return
     * @throws VCIError
     */
    public boolean deletePvolume(String[] ids, UserEntityInfo userEntityInfo) throws VCIError {
        UserEntityDelegate.setUserEntityToService(pvoSer, userEntityInfo);
        //UserEntity userEntity = changeUserEntityInfoToUserEntity(userEntityInfo);
        try {
 
            List<Pvolume> list = new ArrayList<Pvolume>();
            for (String id : ids) {
                Pvolume pvolume = pvoSer.getProlumeById(id);
                list.add(pvolume);
            }
            pvoSer.deletePvolume(ids);
            //Iterator it = map.();
            for (Pvolume vol : list) {
                String volId = vol.getId();
                VolumeCatch.delVolume(vol.getName());
                LogRecordUtil.writeLog(userEntityInfo, "删除", "删除成功", volId, LogType.General, vol.getLogInfo());
            }
        } catch (Exception e) {
            throw new VCIError("310104", new String[0]);
        }
        return true;
    }
    /**
     * 将其他卷更改为非首选路�?0
     * @param userEntityInfo
     * @throws VCIError
     */
    public void updatePvolumeInvalid(UserEntityInfo userEntityInfo)throws VCIError{
        
        UserEntityDelegate.setUserEntityToService(pvoSer, userEntityInfo);
        //UserEntity userEntity = changeUserEntityInfoToUserEntity(userEntityInfo);
        Pvolume pvo = pvoSer.getIsvalidVolumeName();
        try{
            
            
            if (pvo.getId() != null && pvo.getId() != ""){            
                pvoSer.updatePvolumeInvalid();
                StringBuilder logres = new StringBuilder();
                logres.append("更新前:"+pvo.getLogInfo()+"为首选路径!");
                pvo.setIsvalid(false);
                logres.append(" 更新后:"+pvo.getLogInfo()+"不为首选路径");
                LogRecordUtil.writeLog(userEntityInfo, "更新", "修改成功", logres.toString(), LogType.General,pvo.getId());
            }
        }catch(Exception e){
            LogRecordUtil.writeLog(userEntityInfo, "更新", "修改失败", e.getMessage(), LogType.General,pvo.getId());
            throw new VCIError("310105", new String[0]); 
        }
    }
    
    /**
     * 查看卷是否被引用
     * @param ids
     * @return
     * @throws VCIError
     */
    public int fetchVolumnInfoByIds(String id) throws VCIError{
        int count = 0;
        try {
            count = pvoSer.fetchVolumnInfoByIds(id);
        } catch (Exception e) {
            throw new VCIError("310106",new String[0]); //获取卷信息出错,请重新获取�?
        }
        
        return count;
    }
    
    /**
     * 检查要删除的卷是否为首选路�?
     * @param id
     * @return
     * @throws VCIError
     */
    public boolean checkDelIsvalid(String id) throws VCIError{
        boolean res = false;
        try {
            res = pvoSer.checkDelIsvalid(id);
        } catch (Exception e) {
            throw new VCIError("310106",new String[0]); //获取卷信息出错,请重新获取�?
        }
        return res;
    }
 
    public PvolumeInfo getIsvalidVolumeName() throws VCIError{
        PvolumeInfo info = null;
        try {
            Pvolume pvolume = pvoSer.getIsvalidVolumeName();
            info = this.changePvolumeToPvolumeInfo(pvolume);
        } catch (Exception e) {
            throw new VCIError("310106",new String[0]); //获取卷信息出错,请重新获取�?
        }
        return info;
    }
    /**
     * 返回一个在服务器端有效的卷对象
     * <p>此卷对象只有卷路径值,其它值均为空,其卷路径值是 ${user.dir}\attachments\</p>
     * <p>其中${user.dir}将用System.getProperty("user.dir")的值替�?/p>
     * <p>Description: </p>
     * @author xchao
     * @time 2013-8-6
     * @return
     * @throws VCIError
     */
    public PvolumeInfo getIsvalidVolumeNameInServerEnv() throws VCIError{
        PvolumeInfo info = null;
        try {
            Pvolume pvolume = pvoSer.getIsvalidVolumeNameInServerEnv();
            info = this.changePvolumeToPvolumeInfo(pvolume);
        } catch (Exception e) {
            throw new VCIError("310106",new String[0]); //获取卷信息出错,请重新获取�?
        }
        return info;
    }
    
    /***
     *    卷对象从corba对象到业务对�?
     * @param pvolumeInfo
     * @return
     */
    public Pvolume changePvolumeInfoToPvolume (PvolumeInfo pvolumeInfo) {
        Pvolume pvolume = new Pvolume();
        pvolume.setId(pvolumeInfo.id  == "" ? null : pvolumeInfo.id);
        pvolume.setName(pvolumeInfo.name == "" ? null : pvolumeInfo.name);
        pvolume.setService(pvolumeInfo.service == "" ? null : pvolumeInfo.service);
        pvolume.setHost(pvolumeInfo.host == "" ? null : pvolumeInfo.host);
        pvolume.setType(pvolumeInfo.type == 0 ? 0 : 1 );
        pvolume.setPath(pvolumeInfo.path == "" ? null : pvolumeInfo.path);
        pvolume.setIsvalid(pvolumeInfo.isvalid);
        return pvolume;
    }
    
    private PvolumeInfo changePvolumeToPvolumeInfo(Pvolume pvolume) {
        PvolumeInfo pvoInfo = new PvolumeInfo();
        pvoInfo.id = pvolume.getId() == null ? "" : pvolume.getId();
        pvoInfo.name = pvolume.getName() == null ? "" : pvolume.getName();
        pvoInfo.service = pvolume.getService() == null ? "" : pvolume.getService();
        pvoInfo.host = pvolume.getHost() == null ? "" : pvolume.getHost();
        pvoInfo.type = (short)(pvolume.getType() == 0 ? 0 : 1);
        pvoInfo.path = pvolume.getPath() == null ? "" : pvolume.getPath();
        pvoInfo.isvalid = pvolume.getIsvalid();
        return pvoInfo;
    }
    
    public UserEntity changeUserEntityInfoToUserEntity(UserEntityInfo info){
        UserEntity entity = new UserEntity();
        entity.setModule(info.modules);
        entity.setIp(info.ip);
        entity.setUserName(info.userName);
        return entity;
    }
    
//    public void createFileCabinet(String levelDirectory){
//        String temp1Child = null;
//        for (int level1 = 0; level1 < 256; level1++) {
//            String temp1 = TransformDelegate.sl(16, level1);
//            if (temp1.length() <= 1) {
//                temp1 = "0" + temp1;
//
//            }
//            temp1 = levelDirectory + "/" + temp1;
//            createDir(temp1);
//            // 创建二级子目�?
//            for (int level1Child = 0; level1Child < 100; level1Child++) {
//                if (level1Child < 10) {
//                    temp1Child = "0" + level1Child;
//                } else {
//                    temp1Child = level1Child + "";
//                }
//                temp1Child = temp1 + "/" + temp1Child;
//                createDir(temp1Child);
//            }
//        }
//    }
    
    /**
    * 创建目录
    * @param destDirName 目标目录�?
    * @return 目录创建成功返回true,否则返回false
    */
    public  boolean createDir(String destDirName) {
        File dir = new File(destDirName);
        if(dir.exists()) {
       //  System.out.println("创建目录" + destDirName + "失败,目标目录已存在�?);
         return false;
        }
        if(!destDirName.endsWith(File.separator))
         destDirName = destDirName + File.separator;
        // 创建单个目录
        if(dir.mkdirs()) {
       //  System.out.println("创建目录" + destDirName + "成功�?);
         return true;
        } else {
      //   System.out.println("创建目录" + destDirName + "成功�?);
         return false;
        }
    }
    
}