如何将动态表单的附件字段的附件保存到已有单据的附件面板原创
金蝶云社区-陈来珍
陈来珍
3人赞赏了该文章 2,708次浏览 未经作者许可,禁止转载编辑于2022年10月12日 17:32:57

关键字:附件面板、附件字段、是否落库

一、需求

     在单据E(kded_demoe_clz)列表的工具栏中,选择某些行,点击按钮,弹出一个动态表单(kded_attfieldupload),动态表单选择完附件之后,点击确定,返回选择的附件给已选的单据列表单据的附件面板中。

二、思路与方案

     从动态表单中上传的附件是一个放在缓存中的数据,还没保存到文件服务器中,所以需要先通过FileService接口持久化保存到远程服务器中,又因为要保存的是附件字段对象到附件面板中,所以需要重新构建附件面板的数据,然后再上传到附件面板中

三、实现过程

1、弹出动态表单

@Override
	public void itemClick(ItemClickEvent evt) {
		if (evt.getItemKey().equals("kded_uploadatt")) {
			FormShowParameter fsp = new FormShowParameter();
			fsp.getOpenStyle().setShowType(ShowType.Modal);
			fsp.setFormId("kded_attfieldupload");
			fsp.setCloseCallBack(new CloseCallBack(this, "kded_uploadatt"));
			this.getView().showForm(fsp);
		}
		super.itemClick(evt);
	}

2、动态表单回传数据

public class FormSelectPlugin extends AbstractFormPlugin {
	@Override
	public void registerListener(EventObject e) {
		Button btt = this.getView().getControl("btnok");
		btt.addClickListener(this);
		super.registerListener(e);
	}

	@Override
	public void click(EventObject evt) {
		if (evt.getSource() instanceof Button) {
			Button source = (Button) evt.getSource();
			if (source.getKey().equals("btnok")) {
				//获取附件字段值
				DynamicObjectCollection attCol = (DynamicObjectCollection) this.getModel().getValue("kded_attachmentfield");
				this.getView().returnDataToParent(attCol);
				this.getView().close();
			}
		}
		super.click(evt);
	}
}

3、持久化附件到服务器并写入单据的附件面板中

//回写事件,未落库附件面板到落库附件面板
	@Override
	public void closedCallBack(ClosedCallBackEvent ccb) {
		if (ccb.getActionId().equals("kded_uploadatt")) {
			List<Map<String, Object>> returnData = new ArrayList<Map<String, Object>>();
			if (returnData.isEmpty()) {
				return;
			}
			DynamicObjectCollection attcol = (DynamicObjectCollection) ccb.getReturnData();
			for (DynamicObject attFileCol : attcol) {
				DynamicObject attDoj = attFileCol.getDynamicObject("fbasedataid");
				returnData.add(createAttMap(attDoj));
			}
			BillList billList = this.getView().getControl("billlistap");
			ListSelectedRowCollection selectedRows = billList.getSelectedRows();
			for (ListSelectedRow listSelectedRow : selectedRows) {
				// 上传附件到单据的附件面板中
				AttachmentServiceHelper.upload(billList.getBillFormId(), listSelectedRow.getPrimaryKeyValue(),
						"attachmentpanel", returnData);
			}
		}
	}
//构建AttachmentServiceHelper.upload的附件面板数据
	private Map<String, Object> createAttMap(DynamicObject attDoj) {
		Map<String, Object> map = new HashMap<String, Object>();
		String url =attDoj.getString("url");
		String name = attDoj.getString("name");
		if (url.contains("configKey=redis.serversForCache&id=tempfile")) {
			// 持久化附件到服务器
			url = uploadTempfile(url, name);
			map.put("url", url);
		}
		map.put("creator", UserServiceHelper.getCurrentUserId());
		long time = new Date().getTime();
		map.put("modifytime", time);
		map.put("createdate", time);
		map.put("status", "success");
		map.put("type", attDoj.get("type"));
		map.put("name", name);
		StringBuffer uid = new StringBuffer();
		uid.append("rc-upload-");
		uid.append(time);
		uid.append("-");
		uid.append("1");
		map.put("uid", uid.toString());
		map.put("size", attDoj.get("size"));
		return map;
	}

	/**
	 * 上传临时文件到服务器中
	 * 
	 * @param url
	 * @param name
	 * @return
	 */
	private String uploadTempfile(String url, String name) {
		TempFileCache cache = CacheFactory.getCommonCacheFactory().getTempFileCache();
		InputStream in = cache.getInputStream(url);
		FileService service = FileServiceFactory.getAttachmentFileService();
		FileService fs = FileServiceFactory.getAttachmentFileService();
		RequestContext requestContext = RequestContext.get();
		String uuid = UUID.randomUUID().toString().replace("-", "");
		// 生成文件路径-上传附件时远程服务器需要存储文件的位置
		String pathParam = FileNameUtils.getAttachmentFileName(requestContext.getTenantId(),
				requestContext.getAccountId(), uuid, name);
		FileItem fileItem = new FileItem(name, pathParam, in);
		// cache.remove(url);
		// 上传附件到文件服务器 UrlService.getDomainContextUrl()+ "/attachment/download.do?path=/"
		// +
		String downUrl = service.upload(fileItem);
		return downUrl;
	}

四、效果图

1、点击插入附件,在动态表单的附件字段上传附件

image.png

2、点击预览单据附件面板上的附件

image.png

五、开发环境版本

V5.0.002

六、参考资料

【开发平台】指导手册

学习成长中心


 


赞 3