ActionBatchDownloadWithWorkOrWorkCompletedStream.java 5.5 KB
Newer Older
R
update  
roo00 已提交
1 2
package com.x.processplatform.assemble.surface.jaxrs.attachment;

Z
zhourui 已提交
3 4 5 6 7 8 9 10 11 12 13
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;

R
update  
roo00 已提交
14 15
import com.x.base.core.container.EntityManagerContainer;
import com.x.base.core.container.factory.EntityManagerContainerFactory;
R
Ray 已提交
16
import com.x.base.core.project.config.StorageMapping;
R
update  
roo00 已提交
17
import com.x.base.core.project.exception.ExceptionAccessDenied;
Z
zhourui 已提交
18
import com.x.base.core.project.exception.ExceptionEntityExist;
R
update  
roo00 已提交
19 20 21 22 23 24
import com.x.base.core.project.http.ActionResult;
import com.x.base.core.project.http.EffectivePerson;
import com.x.base.core.project.jaxrs.WoFile;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import com.x.base.core.project.tools.DateTools;
O
o2null 已提交
25
import com.x.general.core.entity.GeneralFile;
R
update  
roo00 已提交
26
import com.x.processplatform.assemble.surface.Business;
R
Ray 已提交
27
import com.x.processplatform.assemble.surface.ThisApplication;
R
update  
roo00 已提交
28 29 30 31 32 33 34 35
import com.x.processplatform.core.entity.content.Attachment;
import com.x.processplatform.core.entity.content.Work;
import com.x.processplatform.core.entity.content.WorkCompleted;

class ActionBatchDownloadWithWorkOrWorkCompletedStream extends BaseAction {

	private static Logger logger = LoggerFactory.getLogger(ActionBatchDownloadWithWorkOrWorkCompletedStream.class);

Z
zhourui 已提交
36 37
	ActionResult<Wo> execute(EffectivePerson effectivePerson, String workId, String site, String fileName, String flag)
			throws Exception {
R
update  
roo00 已提交
38 39 40 41 42 43
		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
			ActionResult<Wo> result = new ActionResult<>();
			Business business = new Business(emc);
			String title = "";
			String job = "";
			Work work = emc.find(workId, Work.class);
Z
zhourui 已提交
44
			if (work == null) {
R
update  
roo00 已提交
45 46
				WorkCompleted workCompleted = emc.find(workId, WorkCompleted.class);
				if (null == workCompleted) {
Z
zhourui 已提交
47
					throw new ExceptionEntityExist(workId);
R
update  
roo00 已提交
48
				}
Z
zhourui 已提交
49
				if (!business.readable(effectivePerson, workCompleted)) {
R
update  
roo00 已提交
50 51 52 53 54
					throw new ExceptionWorkCompletedAccessDenied(effectivePerson.getDistinguishedName(),
							workCompleted.getTitle(), workCompleted.getId());
				}
				title = workCompleted.getTitle();
				job = workCompleted.getJob();
Z
zhourui 已提交
55 56
			} else {
				if (!business.readable(effectivePerson, work)) {
R
update  
roo00 已提交
57 58 59 60 61 62 63 64 65
					throw new ExceptionAccessDenied(effectivePerson, work);
				}
				title = work.getTitle();
				job = work.getJob();
			}

			List<Attachment> attachmentList;
			if (StringUtils.isBlank(site) || EMPTY_SYMBOL.equals(site)) {
				attachmentList = business.attachment().listWithJobObject(job);
66
			} else if (site.indexOf(SITE_SEPARATOR) == -1) {
Z
zhourui 已提交
67 68 69 70
				attachmentList = emc.listEqualAndEqual(Attachment.class, Attachment.job_FIELDNAME, job,
						Attachment.site_FIELDNAME, site);
			} else {
				attachmentList = emc.listEqualAndIn(Attachment.class, Attachment.job_FIELDNAME, job,
71
						Attachment.site_FIELDNAME, Arrays.asList(site.split(SITE_SEPARATOR)));
R
update  
roo00 已提交
72
			}
R
update  
roo00 已提交
73 74 75 76
			List<String> identities = business.organization().identity().listWithPerson(effectivePerson);
			List<String> units = business.organization().unit().listWithPerson(effectivePerson);
			List<Attachment> readableAttachmentList = new ArrayList<>();
			for (Attachment attachment : attachmentList) {
O
o2sword 已提交
77
				if (this.read(attachment, effectivePerson, identities, units, business)) {
R
update  
roo00 已提交
78 79 80
					readableAttachmentList.add(attachment);
				}
			}
Z
zhourui 已提交
81
			if (StringUtils.isBlank(fileName)) {
Z
zhourui 已提交
82
				if (title.length() > 60) {
O
o2sword 已提交
83 84
					title = title.substring(0, 60);
				}
Z
zhourui 已提交
85 86
				fileName = title + DateTools.format(new Date(), DateTools.formatCompact_yyyyMMddHHmmss) + ".zip";
			} else {
R
roo00 已提交
87
				String extension = FilenameUtils.getExtension(fileName);
Z
zhourui 已提交
88
				if (StringUtils.isEmpty(extension)) {
R
roo00 已提交
89 90 91
					fileName = fileName + ".zip";
				}
			}
NoSubject's avatar
NoSubject 已提交
92 93

			Map<String, byte[]> map = new HashMap<>();
94
			this.assembleFile(business, map, flag);
NoSubject's avatar
NoSubject 已提交
95

O
o2sword 已提交
96
			fileName = StringUtils.replaceEach(fileName,
Z
zhourui 已提交
97 98
					new String[] { "/", ":", "*", "?", "<<", ">>", "|", "<", ">", "\\" },
					new String[] { "", "", "", "", "", "", "", "", "", "" });
Z
zhourui 已提交
99 100
			logger.info("batchDown to {},att size {}, from work {}, has form {}", fileName, attachmentList.size(),
					workId, map.size());
R
update  
roo00 已提交
101
			try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
NoSubject's avatar
NoSubject 已提交
102
				business.downToZip(readableAttachmentList, os, map);
R
update  
roo00 已提交
103
				byte[] bs = os.toByteArray();
Z
zhourui 已提交
104
				Wo wo = new Wo(bs, this.contentType(true, fileName), this.contentDisposition(true, fileName));
R
update  
roo00 已提交
105 106 107 108 109 110 111
				result.setData(wo);
			}

			return result;
		}
	}

112 113 114
	private void assembleFile(Business business, Map<String, byte[]> map, String files) throws Exception {
		EntityManagerContainer emc = business.entityManagerContainer();
		if (StringUtils.isNotEmpty(files)) {
115
			String[] flagList = files.split(FILE_SEPARATOR);
116
			for (String flag : flagList) {
Z
zhourui 已提交
117
				if (StringUtils.isNotBlank(flag)) {
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
					GeneralFile generalFile = emc.find(flag.trim(), GeneralFile.class);
					if (generalFile != null) {
						StorageMapping gfMapping = ThisApplication.context().storageMappings().get(GeneralFile.class,
								generalFile.getStorage());
						map.put(generalFile.getName(), generalFile.readContent(gfMapping));

						generalFile.deleteContent(gfMapping);
						emc.beginTransaction(GeneralFile.class);
						emc.delete(GeneralFile.class, generalFile.getId());
						emc.commit();
					}
				}
			}
		}
	}

R
update  
roo00 已提交
134 135 136 137 138 139 140 141 142
	public static class Wo extends WoFile {

		public Wo(byte[] bytes, String contentType, String contentDisposition) {
			super(bytes, contentType, contentDisposition);
		}

	}

}