package com.vci.client.fm; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import com.vci.client.LogonApplication; import com.vci.client.bof.BOFactoryClient; import com.vci.client.bof.ClientBusinessObject; import com.vci.client.bof.RevisionValueDelegate; import com.vci.client.bof.RevisionValueObject; import com.vci.client.common.providers.ServiceProvider; import com.vci.client.omd.provider.BtmProvider; import com.vci.client.volumn.TransFileInfo; import com.vci.common.utility.ObjectUtility; import com.vci.corba.bofactory.BOFactoryService.UpdateBusinessObjectResult; import com.vci.corba.common.VCIError; import com.vci.corba.omd.atm.AttribItem; import com.vci.corba.omd.btm.BtmItem; import com.vci.corba.omd.data.BusinessObject; import com.vci.corba.omd.lcm.LifeCycle; import com.vci.corba.volume.VolumeServicePrx; import com.vci.corba.volume.data.FileInfo; import com.vci.corba.framework.data.PvolumeInfo; import com.vci.omd.constants.FileObjectType; public class ClientFileObjectOperation implements ClientFOOperationInterface { private static VolumeServicePrx _vs = null; public static String fileSeparetor = ":"; public static PvolumeInfo pvolumeInfo = null; public static VolumeServicePrx getVolumeDelegate(){ if(_vs == null){ _vs = ServiceProvider.getVolumeService(null); } return _vs; } // private int getTransmitBlockSize() { // int size = 0; // try { // size = (int)(ServiceProvider.getVolumeManager().getTransmitBlockSize() * 1024); // } catch (VCIError e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // // if (size == 0) // size = 32 * 1024; // // return size; // } @Override public FileObject createNewFile() throws VCIError{ String boName = FileObjectType.FILE_DATA_TABLE; BtmItem item = this.getTypeInfo(boName); FileObject bo = new FileObject(); bo.setOid(ObjectUtility.getNewObjectID36()); bo.setRevisionid(ObjectUtility.getNewObjectID36()); bo.setNameoid(ObjectUtility.getNewObjectID36()); bo.setBtmName(boName); bo.setIsLastR(true); bo.setIsFirstR(true); bo.setIsFirstV(true); bo.setIsLastV(true); bo.setCreator(LogonApplication.getUserEntityObject().getUserName()); bo.setCreateTime(System.currentTimeMillis()); bo.setLastModifier(LogonApplication.getUserEntityObject().getUserName()); bo.setLastModifyTime(System.currentTimeMillis()); bo.setRevisionRule(item.revRuleName); bo.setVersionRule(String.valueOf(item.verRuleName)); RevisionValueObject revisonObj = getRevisionSeqVal(item.revLevel, item.revRuleName, item.revInput); bo.setRevisionSeq(revisonObj.getRevisionSeq()); bo.setRevisionValue(revisonObj.getRevisionVal()); bo.setVersionSeq((short)1); bo.setVersionValue("1"); bo.setLctId(item.lifeCycle); bo.setLcStatus(getLcFirstStatus(item.lifeCycle)); bo.setId(""); bo.setName(""); bo.setDescription(""); bo.setOwner(LogonApplication.getUserEntityObject().getUserName()); //bo.setCheckinBy(LogonApplication.getUserEntityObject().getUserName()); initTypeAttributeValue(bo); bo.setCopyFromVersion(""); bo.setDocumentoid(""); bo.setFilePath(""); bo.setFileType(""); bo.setRevisionLimit(7); return bo; } private void initTypeAttributeValue(ClientBusinessObject bo) throws VCIError { try { AttribItem[] items = BtmProvider.getInstance().getBtAbItems(bo.getBtmName()); for (AttribItem item : items) { bo.setAttributeValue(item.name, item.defValue); } } catch (Exception e) { e.printStackTrace(); throw new VCIError(); } catch (Throwable e) { e.printStackTrace(); throw new VCIError(); } } @Override public FileObject createNewFile(String localfilePath, FileObject fileObject) throws VCIError{ PvolumeInfo pvolumeInfo = getServerFilePath(); String midFilePath = getStoreFilePath(fileObject.getOid(), fileObject.getFileType()); fileObject.setFilePath(new StringBuffer(pvolumeInfo.name).append(fileSeparetor).append(midFilePath).toString()); fileObject.dealFileObjectNullValue(); String uploadFilePath = new StringBuffer(pvolumeInfo.path).append(midFilePath).toString(); boolean rs = uploadFile(localfilePath, uploadFilePath); if (!rs) { return null; } try { //上传成功,存储结构化信息 fileObject.dealFileObjectNullValue(); BusinessObject bo = fileObject.getBusinessObject(); BusinessObject businessObject = BOFactoryClient.getBOFactoryService().createBusinessObject(bo,false,false); fileObject.setBusinessObject(businessObject); return fileObject; } catch (VCIError e) { try { _vs.deleteFile(fileObject.getFilePath()); } catch (VCIError ee) { ee.printStackTrace(); } throw new VCIError(e.code, e.messages); } } @Override public FileObject createNewFile(String localfilePath, FileObject fileObject, PvolumeInfo pvolumeInfo) throws VCIError{ if (pvolumeInfo == null || pvolumeInfo.path == null || pvolumeInfo.path.equals("")) { pvolumeInfo = getServerFilePath(); } String midFilePath = getStoreFilePath(fileObject.getOid(), fileObject.getFileType()); fileObject.setFilePath(new StringBuffer(pvolumeInfo.name).append(fileSeparetor).append(midFilePath).toString()); fileObject.dealFileObjectNullValue(); String uploadFilePath = new StringBuffer(pvolumeInfo.path).append(midFilePath).toString(); boolean rs = uploadFile(localfilePath, uploadFilePath); if (!rs) { return null; } try { //上传成功,存储结构化信息 fileObject.dealFileObjectNullValue(); BusinessObject bo = fileObject.getBusinessObject(); BusinessObject businessObject = BOFactoryClient.getBOFactoryService().createBusinessObject(bo,false,false); fileObject.setBusinessObject(businessObject); return fileObject; } catch (VCIError e) { try { _vs.deleteFile(fileObject.getFilePath()); } catch (VCIError ee) { ee.printStackTrace(); } throw new VCIError(e.code, e.messages); } } @Override public FileObject uploadFile(String localfilePath, FileObject fileObject) throws VCIError{ PvolumeInfo pvolumeInfo = getServerFilePath(); String midFilePath = getStoreFilePath(fileObject.getOid(), fileObject.getFileType()); fileObject.setFilePath(new StringBuffer(pvolumeInfo.name).append(fileSeparetor).append(midFilePath).toString()); fileObject.dealFileObjectNullValue(); String uploadFilePath = new StringBuffer(pvolumeInfo.path).append(midFilePath).toString(); boolean rs = uploadFile(localfilePath, uploadFilePath); if (!rs) { return null; } return fileObject; } /** * 获取的文件的卷服务配置 * @return * @throws VCIError */ public PvolumeInfo getServerFilePath() throws VCIError { if (pvolumeInfo != null) { return pvolumeInfo; } pvolumeInfo = ServiceProvider.getFrameService().getDefaultVolume(); return pvolumeInfo; } private PvolumeInfo[] getAllVolume() throws VCIError { return ServiceProvider.getFrameService().getAllPvolumes(); } @Override public String getFullFilePath(FileObject fileObject) throws VCIError { String str = fileObject.getFilePath(); if (str.indexOf(fileSeparetor) < 0) { return str; } PvolumeInfo[] volumes = getAllVolume(); if (volumes == null || volumes.length ==0) { return str; } PvolumeInfo volumeInfo = null; String volumnName = str.substring(0, str.indexOf(":")); for (int i = 0; i < volumes.length; i++) { if (volumnName.equals(volumes[i].name)) { volumeInfo = volumes[i]; break; } } if (volumeInfo == null) { return str; } str = str.replace(volumeInfo.name + fileSeparetor, volumeInfo.path); return str; } @Override public boolean uploadFile(String localfilePath, String uploadFilePath) throws VCIError{ boolean isSucess = true; try { File originalFile = new File(localfilePath); //存储非结构化信息 _vs = getVolumeDelegate(); isSucess = false; sendFile(_vs, originalFile, uploadFilePath); isSucess = true; } catch (VCIError e) { throw new VCIError(String.valueOf(e.code), e.messages); } finally { try { if (!isSucess) { _vs.deleteFile(uploadFilePath); } } catch (VCIError e) { e.printStackTrace(); } } return isSucess; } /** * 获取文件存储到服务器端的路径 * @param fileId * @param fileType * @return * @throws VCIError */ public String getStoreFilePath(String fileId, String fileType) throws VCIError { StringBuffer desFileName = new StringBuffer(); desFileName.append(FileCabinetPath.getRandomServerSaveFilePath()); desFileName.append("/"); desFileName.append(fileId); if (fileType.trim().length() > 0) { desFileName.append("."); desFileName.append(fileType); } String uploadFilePath = desFileName.toString(); return uploadFilePath; } @Override public boolean deleteFileObject(String oid) throws VCIError{ BusinessObject businessObject = BOFactoryClient.getBOFactoryService().readBusinessObject(oid, FileTypeConstants.FILE_DATA_TABLE); FileObject fileObject = new FileObject(); fileObject.setBusinessObject(businessObject); boolean rs = BOFactoryClient.getBOFactoryService().deleteBusinessObject(fileObject.getBusinessObject(), 1); if(!rs){ return false; } String serverPath = fileObject.getAttributeValue(FileTypeConstants.SELECT_FILEPATH); try { _vs = getVolumeDelegate(); boolean isSucess = _vs.deleteFile(serverPath); if(isSucess == true){ return true; } } catch (VCIError e) { throw new VCIError(String.valueOf(e.code), e.messages); } return false; } @Override public FileObject getFileObject(String id) throws VCIError { BusinessObject businessObject = BOFactoryClient.getBOFactoryService().readBusinessObject(id, FileTypeConstants.FILE_DATA_TABLE); FileObject fileObject = new FileObject(); fileObject.setBusinessObject(businessObject); return fileObject; } @Override public boolean checkOutFileObject(FileObject fileObject) throws VCIError { fileObject.dealBusinessObjectNullValue(); boolean rs = BOFactoryClient.getBOFactoryService().checkoutBusinessObject(fileObject.getBusinessObject()); return rs; } @Override public boolean downLoadFile(String clientPath, String serverPath) throws VCIError{ boolean rs = false; try { rs = receiveFile(_vs, new File(clientPath).getParent(), new File(clientPath).getName(), serverPath); } catch (VCIError e) { throw new VCIError(String.valueOf(e.code), e.messages); } return rs; } @Override public boolean downLoadFileByOid(String clientPath, String fileOid) throws VCIError { boolean rs = false; try { FileObject fileObject = this.getFileObject(fileOid); String serverPath = this.getFullFilePath(fileObject); rs = receiveFile(_vs, new File(clientPath).getParent(), new File(clientPath).getName(), serverPath); } catch (VCIError e) { throw new VCIError(String.valueOf(e.code), e.messages); } return rs; } @Override public boolean checkInFileObject(FileObject fileObject) throws VCIError{ fileObject.dealFileObjectNullValue(); boolean rs = BOFactoryClient.getBOFactoryService().checkInBusinessObject(fileObject.getBusinessObject()); return rs; } @Override public boolean updateFileObject(FileObject fileObject) throws VCIError { fileObject.dealFileObjectNullValue(); UpdateBusinessObjectResult result = BOFactoryClient.getBOFactoryService().updateBusinessObject(fileObject.getBusinessObject()); return result.returnValue; } @Override public boolean undCheckOutFileObject(FileObject fileObject) throws VCIError { fileObject.dealFileObjectNullValue(); boolean rs = BOFactoryClient.getBOFactoryService().unCheckOutBusinessObject(fileObject.getBusinessObject()); return rs; } @Override public FileObject reviseFileObject(FileObject fromFo,String revisionVal) throws VCIError { BtmItem item = this.getTypeInfo(fromFo.getBtmName()); if (item.revLevel == 0) { throw new VCIError("P0010SOF-00015", new String[0]); } FileObject toFo = new FileObject(); toFo.setOid(ObjectUtility.getNewObjectID36()); toFo.setRevisionid(ObjectUtility.getNewObjectID36()); toFo.setNameoid(fromFo.getNameoid()); toFo.setBtmName(fromFo.getBtmName()); toFo.setIsLastR(true); toFo.setIsFirstR(false); toFo.setIsFirstV(true); toFo.setIsLastV(true); toFo.setCreator(LogonApplication.getUserEntityObject().getUserName()); toFo.setCreateTime(System.currentTimeMillis()); toFo.setLastModifier(LogonApplication.getUserEntityObject().getUserName()); toFo.setLastModifyTime(System.currentTimeMillis()); toFo.setRevisionRule(item.revRuleName); toFo.setVersionRule(String.valueOf(item.verRuleName)); RevisionValueObject rvObj = getNextRevision(fromFo.getBtmName(), fromFo.getNameoid(), item.revRuleName, item.revInput, revisionVal); toFo.setRevisionSeq(rvObj.getRevisionSeq()); toFo.setRevisionValue(rvObj.getRevisionVal()); toFo.setVersionSeq((short)1); toFo.setVersionValue("1"); toFo.setLctId(fromFo.getLctId()); toFo.setLcStatus(getLcFirstStatus(item.lifeCycle)); toFo.setId(fromFo.getId()); toFo.setName(fromFo.getName()); toFo.setDescription(fromFo.getDescription()); toFo.setOwner(LogonApplication.getUserEntityObject().getUserName()); //toFo.setCheckinBy(LogonApplication.getUserEntityObject().getUserName()); toFo.setCopyFromVersion(fromFo.getOid()); String fromFilePath = fromFo.getFilePath(); String toFilePath = fromFilePath.replaceAll(fromFo.getOid(), toFo.getOid()); toFo.setFilePath(toFilePath); toFo.setFileType(fromFo.getFileType()); toFo.dealBusinessObjectNullValue(); //文件对象的升版需要同步复制一份源版本的物理文件作为新版本对应的物理文件。 try { _vs = getVolumeDelegate(); _vs.copyFile(fromFilePath, toFilePath); } catch (VCIError e) { throw new VCIError(String.valueOf(e.code), e.messages); } //存储生版对象 BusinessObject toBusinessObject = BOFactoryClient.getBOFactoryService().createBusinessObject(toFo.getBusinessObject(), true, false); toFo.setBusinessObject(toBusinessObject); return toFo; } /** * 获取下一个版本号 * @param boName * @param nameOid * @param revRuleName * @param revInput * @param revisionVal * @return * @throws VCIError */ private RevisionValueObject getNextRevision(String btName, String nameOid, String revRuleName, boolean revInput, String revisionVal) throws VCIError { RevisionValueDelegate delegate = new RevisionValueDelegate(); //String tableName = OmdTools.getBTTableName(boName); RevisionValueObject object = delegate.getNextRevisionValueObject(btName, nameOid, revRuleName, revInput, revisionVal); return object; } /** * 根据BO名称获取对象详细信息 * @param boName * @return */ private BtmItem getTypeInfo(String boName) { //BtmProvider provide = BtmProvider.getInstance(); BtmItem item = BtmProvider.getBtmItemByName(boName); return item; } /** * 获取初始的版本信息 * @param revLevel, 版本类型 * @param revRuleName,版本规则名称 * @return */ private RevisionValueObject getRevisionSeqVal(int revLevel, String revRuleName, boolean revInput) { RevisionValueObject obj = new RevisionValueObject(); obj.setRevisionSeq((short)1); if (revInput) { obj.setRevisionVal(""); return obj; } if (revLevel == 0) { obj.setRevisionVal("-"); } else { obj.setRevisionVal(getFirstRevision(revRuleName)); } return obj; } /** * 获取第一个版本值 * @param revRuleName * @return */ private String getFirstRevision(String revRuleName) { String initValue; try { initValue = ServiceProvider.getOMDService().getVerRuleService().getVersionRule(revRuleName).initialValue; return initValue; } catch (VCIError e) { // TODO Auto-generated catch block e.printStackTrace(); } return ""; } /** * 根据生命周期名称获取第一个生命周期节点 * @param lifeCycleName * @return */ private String getLcFirstStatus(String lifeCycleName) { LifeCycle lc; try { lc = ServiceProvider.getOMDService().getLifeCycleService().getLifeCycle(lifeCycleName); String firstState = lc.startState; return firstState; } catch (VCIError e) { // TODO Auto-generated catch block e.printStackTrace(); } return ""; } /** * *
Description: 上传文件
* * @author Administrator * @time 2011-7-3 * @param originalFile:客户端文件 * @param desFileName:服务器端文件名称 * @return * @throws VCIError */ private long sendFile(VolumeServicePrx vs, File originalFile, String desFileName) throws VCIError{ if (!originalFile.exists()) { throw new VCIError("710020", new String[0]); } try { long currentTime = vs.getCurrrentTimeMillions(); FileInputStream in = new FileInputStream(originalFile); long fileSize = originalFile.length(); int blockSize = (int)vs.getTransmitBlockSize(); if (blockSize < 0) { blockSize = 32 * 1024; } byte[] fileByte = new byte[blockSize]; long temp = 0; while (temp < (fileSize - blockSize)) { in.read(fileByte, 0, blockSize); vs.receiveFile(desFileName, fileByte, temp, currentTime, fileSize); //added by xiong fei 2011-06-07 TransFileInfo.setTotalSendedSize(blockSize); temp += blockSize; } int remainSize = (int)(fileSize - temp); fileByte = new byte[remainSize]; in.read(fileByte, 0, remainSize); vs.receiveFile(desFileName, fileByte, temp, currentTime, fileSize); //added by xiong fei 2011-06-07 TransFileInfo.setTotalSendedSize(remainSize); in.close(); return currentTime; } catch (VCIError rme) { throw rme; } catch (Exception e) { throw new VCIError("710021", new String[0]); } } /** * *Description: 下载文件
* * @author Administrator * @time 2011-7-3 * @param directory:客户端文件路径 * @param fileName:客户端文件名称 * @param dowloadFileName:服务器端文件名称 * @return * @throws VCIError */ private boolean receiveFile(VolumeServicePrx vs, String directory, String fileName, String dowloadFileName) throws VCIError{ boolean rs = false; try { long fileSize = vs.getFileSize(dowloadFileName); TransFileInfo.setFileLength(fileSize); File file = new File(directory, fileName); FileOutputStream filesave = new FileOutputStream(file); int blockSize = (int)vs.getTransmitBlockSize(); long temp = 0; while (fileSize - temp > blockSize) { filesave.write(vs.sendFile(dowloadFileName, temp)); //added by xiongfei 2011-06-09 TransFileInfo.setTotalSendedSize(blockSize); temp += blockSize; } //added by xiongfei 2011-06-09 TransFileInfo.setTotalSendedSize(blockSize); filesave.write(vs.sendFile(dowloadFileName, temp)); filesave.flush(); filesave.close(); rs=true; } catch (VCIError re) { throw re; } catch (Exception e) { e.printStackTrace(); throw new VCIError("710018", new String[0]); } return rs; } /** * *Description: 下载文件
* * @author Administrator * @time 2011-7-3 * @param info * @param fName * @throws VCIError */ private void receiveFile(VolumeServicePrx vs, FileInfo info, String fName) throws VCIError{ try { String saveFileName = info.fileSavePath; long fileSize = info.fileSize; File file = new File(saveFileName); if(!file.exists()){ file.mkdirs(); if (!info.isDirectory) { file.delete(); } } if (info.isDirectory) { return; } if (fileSize == 0) { return; } FileOutputStream filesave = new FileOutputStream(saveFileName); long blockSize = vs.getTransmitBlockSize(); long temp = 0; String fileName = info.filePath; while (fileSize - temp > blockSize) { filesave.write(vs.sendFile(fileName, temp)); temp += blockSize; } filesave.write(vs.sendFile(fileName, temp)); filesave.flush(); filesave.close(); } catch (VCIError re) { throw re; } catch (Exception e) { throw new VCIError("710018", new String[0]); } } }