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 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 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 list = new ArrayList(); 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; } /** * 返回一个在服务器端有效的卷对象 *

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

*

其中${user.dir}将用System.getProperty("user.dir")的值替�?/p> *

Description:

* @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; } } }