EAS、WAF附件相关处理工具类

/**
	 * 删除单据对应的所有附件,操作成功时返回ture,用于前端
	 * @param billId 单据主键
	 * 
	 */
	public static boolean delete(String billId) throws BOSException, EASBizException{
		
		IBoAttchAsso iBoAttchAsso = BoAttchAssoFactory.getRemoteInstance(); //附件与业务对象关联接口
	    	EntityViewInfo view = new EntityViewInfo();
	    	FilterInfo filter = new FilterInfo();
	    	filter.getFilterItems().add(new FilterItemInfo("boID", billId));
	    	view.setFilter(filter);
	    	BoAttchAssoCollection coll = iBoAttchAsso.getBoAttchAssoCollection(view); //查询所关联附件
	    	if(CommonHelper.isEmpty(coll)){
	    		return false;
	    	}
	    	IAttachment iAttachment = AttachmentFactory.getRemoteInstance();
	    	IObjectPK[] pks = new ObjectUuidPK[coll.size()];
	    	for(int i = 0; i < coll.size(); i++){
	    		
	    		BoAttchAssoInfo bo = coll.get(i); //附件关联对象
	        	AttachmentInfo attachment = bo.getAttachment(); //附件对象
	    		pks[i] = new ObjectUuidPK(attachment.getId());
	    		
	    	}
	    	iAttachment.delete(pks); //删除附件
	    	iBoAttchAsso.delete(filter); //删除附件关联
    	
		return true;
		
	}
	
	/**
	 * 删除单据对应的所有附件,操作成功时返回ture,用于后台
	 * @param ctx 上下文
	 * @param billId 单据主键
	 * 
	 */
	public static boolean delete(Context ctx, String billId) throws BOSException, EASBizException{
		
		IBoAttchAsso iBoAttchAsso = BoAttchAssoFactory.getLocalInstance(ctx); //附件与业务对象关联接口
	    	EntityViewInfo view = new EntityViewInfo();
	    	FilterInfo filter = new FilterInfo();
	    	filter.getFilterItems().add(new FilterItemInfo("boID", billId));
	    	view.setFilter(filter);
	    	BoAttchAssoCollection coll = iBoAttchAsso.getBoAttchAssoCollection(view); //查询所关联附件
	    	if(CommonHelper.isEmpty(coll)){
	    		return false;
	    	}
	    	IAttachment iAttachment = AttachmentFactory.getLocalInstance(ctx);
	    	IObjectPK[] pks = new ObjectUuidPK[coll.size()];
	    	for(int i = 0; i < coll.size(); i++){
	    		
	    		BoAttchAssoInfo bo = coll.get(i); //附件关联对象
	        	AttachmentInfo attachment = bo.getAttachment(); //附件对象
	    		pks[i] = new ObjectUuidPK(attachment.getId());
	    		
	    	}
	    	iAttachment.delete(pks); //删除附件
	    	iBoAttchAsso.delete(filter); //删除附件关联
    	
		return true;
		
	}
	
	/**
	 * 附件上传,金蝶EAS标准产品功能(用于前端)
	 * @param billId 单据主键
	 * @param filePath 附件路径(不含文件名)
	 * @param fileName 附件名称(含后缀)
	 * @return 附件ID
	 * 
	 */
	public static String upload(String billId, String filePath, String fileName){
		
		String attachId = null;
		if(!filePath.endsWith("/")) filePath += "/";
		AttachmentClientManager manager = AttachmentManagerFactory.getClientManager();
		try {
			byte[] bytes = getBytes(filePath + fileName); //获取字节流
			SimpleAttachmentInfo simple = new SimpleAttachmentInfo();
			simple.setContent(bytes);
			simple.setMainName(fileName.substring(0, fileName.indexOf("."))); //文件名
			simple.setExtName(fileName.substring(fileName.indexOf(".") + 1, fileName.length())); //后缀
			attachId = manager.addNewAttachment(billId, simple);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return attachId;
		
	}
	
	/**
	 * 附件复制(已经存在服务器的附件,从一张单据复制到另一张单),金蝶EAS标准产品功能(用于前端)
	 * @param billId 单据主键
	 * @param remotePath 附件路径(含文件名和文件类型)
	 * @param fileName 附件名称(原有附件名称,非服务器路径下的附件名)
	 * @return 附件ID
	 * 
	 */
	public static String upload(String billId, String remotePath, String fileName,String fileType){
		
		String attachId = null;
		AttachmentClientManager manager = AttachmentManagerFactory.getClientManager();
		try {
			byte[] bytes = getBytes(remotePath); //获取字节流
			SimpleAttachmentInfo simple = new SimpleAttachmentInfo();
			simple.setContent(bytes);
			simple.setMainName(fileName); //文件名
			simple.setExtName(fileType); //后缀
			attachId = manager.addNewAttachment(billId, simple);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return attachId;
		
	}
	
	/**
	 * 附件上传,金蝶EAS标准产品功能(用于后台)
	 * @param ctx 上下文
	 * @param billId 单据主键
	 * @param filePath 附件路径(不含文件名)
	 * @param fileName 附件名称(含后缀)
	 * @return 附件ID
	 * 
	 */
	public static String upload(Context ctx, String billId, String filePath, String fileName){
		
		String attachId = null;
		if(!filePath.endsWith("/")) filePath += "/";
		AttachmentServerManager manager = AttachmentManagerFactory.getServerManager(ctx);
		try {
			byte[] bytes = getBytes(filePath + fileName); //获取字节流
			SimpleAttachmentInfo simple = new SimpleAttachmentInfo();
			simple.setContent(bytes);
			simple.setMainName(fileName.substring(0, fileName.indexOf("."))); //文件名
			simple.setExtName(fileName.substring(fileName.indexOf(".") + 1, fileName.length())); //后缀
			attachId = manager.addNewAttachment(ctx, billId, simple);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return attachId;
		
	}
	
	
	public static String upload(Context ctx, String billId, String remotePath, String fileName,String fileType){
		
		String attachId = null;
		AttachmentServerManager manager = AttachmentManagerFactory.getServerManager(ctx);
		try {
			byte[] bytes = getBytes(remotePath); //获取字节流
			SimpleAttachmentInfo simple = new SimpleAttachmentInfo();
			simple.setContent(bytes);
			simple.setMainName(fileName); //文件名
			simple.setExtName(fileType); //后缀
			simple.setCode(null);//单据页面默认加载时,附件编码过滤使用number = null而不是''
			attachId = manager.addNewAttachment(ctx, billId, simple);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return attachId;
		
	}
	
	/**
	 * 根据文件路径读取字节流
	 * @param filePath 文件路径
	 * @return byte[]
	 * 
	 */
	public static byte[] getBytes(String filePath) throws Exception {
		
	        File file = new File(filePath);
	        long fileSize = file.length();
	        if (fileSize > Integer.MAX_VALUE) {
	            return null;
	        }
	        FileInputStream in = new FileInputStream(file);
	        byte[] bytes = new byte[(int) fileSize];
	        int offset = 0;
	        int read = 0;
	        while (offset < bytes.length && (read = in.read(bytes, offset, bytes.length - offset)) >= 0) {
	            offset += read;
	        }
	        //确保所有数据均被读取
	        if (offset != bytes.length) {
	        	throw new Exception("Could not completely read file " + file.getName());
	        }
	        in.close();
	        
	        return bytes;
        
    }
	
	/**
	 * 获取单据对应的单一附件,用于前端
	 * @param billId 单据主键
	 * @return 附件对象
	 * 
	 */
	public static AttachmentInfo getAttach(String billId) throws BOSException, EASBizException{
		
		AttachmentInfo attachment = null;
		IBoAttchAsso iBoAttchAsso = BoAttchAssoFactory.getRemoteInstance(); //附件与业务对象关联接口
	    	EntityViewInfo view = new EntityViewInfo();
	    	FilterInfo filter = new FilterInfo();
	    	filter.getFilterItems().add(new FilterItemInfo("boID", billId));
	    	view.setFilter(filter);
	    	BoAttchAssoCollection coll = iBoAttchAsso.getBoAttchAssoCollection(view); //查询所关联附件
	    	if(CommonHelper.isEmpty(coll)){
	    		return attachment;
	    	}
	    	IAttachment iAttachment = AttachmentFactory.getRemoteInstance();
		BoAttchAssoInfo bo = coll.get(0); //附件关联对象
    		attachment = bo.getAttachment(); //附件对象
		attachment = iAttachment.getAttachmentInfo(new ObjectUuidPK(attachment.getId()));
    	
		return attachment;
		
	}
	
	/**
	 * 获取单据对应的单一附件,用于后台
	 * @param ctx 上下文
	 * @param billId 单据主键
	 * @return 附件对象
	 * 
	 */
	public static AttachmentInfo getAttach(Context ctx, String billId) throws BOSException, EASBizException{
		
		AttachmentInfo attachment = null;
		IBoAttchAsso iBoAttchAsso = BoAttchAssoFactory.getLocalInstance(ctx); //附件与业务对象关联接口
	    	EntityViewInfo view = new EntityViewInfo();
	    	FilterInfo filter = new FilterInfo();
	    	filter.getFilterItems().add(new FilterItemInfo("boID", billId));
	    	view.setFilter(filter);
	    	BoAttchAssoCollection coll = iBoAttchAsso.getBoAttchAssoCollection(view); //查询所关联附件
	    	if(CommonHelper.isEmpty(coll)){
	    		return attachment;
	    	}
	    	IAttachment iAttachment = AttachmentFactory.getLocalInstance(ctx);
		BoAttchAssoInfo bo = coll.get(0); //附件关联对象
		
	    	attachment = bo.getAttachment(); //附件对象
	    	SelectorItemCollection selectors = new SelectorItemCollection();
	    	selectors.add(new SelectorItemInfo("id"));
	    	selectors.add(new SelectorItemInfo("number"));
	    	selectors.add(new SelectorItemInfo("name"));
	    	selectors.add(new SelectorItemInfo("file"));
		attachment = iAttachment.getAttachmentInfo(new ObjectUuidPK(attachment.getId()), selectors);
    	
		return attachment;
		
	}
	
	/**
	 * 获取附件的文件名,包含名称和后缀
	 * @param attach 附件对象
	 * @return 文件名
	 * 
	 */
	public static String getAttach(AttachmentInfo attach){
		
		String fileName = null;
		fileName = attach.getName() + "." + attach.getSimpleName();
		
		return fileName;
		
	}
	
	/**
	 * 获取单据对应的所有附件,用于前台
	 * @param billId 单据主键
	 * @return 附件对象集合
	 * 
	 */
	public static AttachmentCollection getAttaches(String billId) throws BOSException, EASBizException{
		
		AttachmentCollection attaches = null; //附件集合
		IBoAttchAsso iBoAttchAsso = BoAttchAssoFactory.getRemoteInstance(); //附件与业务对象关联接口
	    	EntityViewInfo view = new EntityViewInfo();
	    	FilterInfo filter = new FilterInfo();
	    	filter.getFilterItems().add(new FilterItemInfo("boID", billId));
	    	view.setFilter(filter);
	    	BoAttchAssoCollection coll = iBoAttchAsso.getBoAttchAssoCollection(view); //查询所关联附件
	    	if(CommonHelper.isEmpty(coll)){
	    		return attaches;
	    	}
	    	attaches = new AttachmentCollection();
	    	IAttachment iAttachment = AttachmentFactory.getRemoteInstance();
	    	for(int i = 0; i < coll.size(); i++){
	    		
	    		BoAttchAssoInfo bo = coll.get(i); //附件关联对象
	        	AttachmentInfo attachment = bo.getAttachment(); //附件对象
	        	attachment = iAttachment.getAttachmentInfo("select * where id = '" + attachment.getId().toString() + "'");
	    		attaches.add(attachment);
	    		
	    	}
    	
		return attaches;
		
	}
	
	/**
	 * 获取单据对应的所有附件,用于后台
	 * @param ctx 上下文
	 * @param billId 单据主键
	 * @return 附件对象集合
	 * 
	 */
	public static AttachmentCollection getAttaches(Context ctx, String billId) throws BOSException, EASBizException{
		
		AttachmentCollection attaches = null; //附件集合
		IBoAttchAsso iBoAttchAsso = BoAttchAssoFactory.getLocalInstance(ctx); //附件与业务对象关联接口
	    	EntityViewInfo view = new EntityViewInfo();
	    	FilterInfo filter = new FilterInfo();
	    	filter.getFilterItems().add(new FilterItemInfo("boID", billId));
	    	view.setFilter(filter);
	    	BoAttchAssoCollection coll = iBoAttchAsso.getBoAttchAssoCollection(view); //查询所关联附件
	    	if(CommonHelper.isEmpty(coll)){
	    		return attaches;
	    	}
	    	attaches = new AttachmentCollection();
	    	IAttachment iAttachment = AttachmentFactory.getLocalInstance(ctx);
	    	for(int i = 0; i < coll.size(); i++){
	    		
	    		BoAttchAssoInfo bo = coll.get(i); //附件关联对象
	        	AttachmentInfo attachment = bo.getAttachment(); //附件对象
	        	attachment = iAttachment.getAttachmentInfo("select * where id = '" + attachment.getId().toString() + "'");
	    		attaches.add(attachment);
	    		
	    	}
    	
		return attaches;
		
	}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值