ActionGetWithWork.java 5.3 KB
Newer Older
R
roo00 已提交
1 2 3 4 5 6 7 8 9
package com.x.processplatform.assemble.surface.jaxrs.attachment;

import java.util.List;

import com.x.base.core.container.EntityManagerContainer;
import com.x.base.core.container.factory.EntityManagerContainerFactory;
import com.x.base.core.entity.JpaObject;
import com.x.base.core.project.bean.WrapCopier;
import com.x.base.core.project.bean.WrapCopierFactory;
R
roo00 已提交
10 11 12
import com.x.base.core.project.exception.ExceptionAccessDenied;
import com.x.base.core.project.exception.ExceptionEntityNotExist;
import com.x.base.core.project.gson.GsonPropertyObject;
R
roo00 已提交
13 14
import com.x.base.core.project.http.ActionResult;
import com.x.base.core.project.http.EffectivePerson;
Z
zhourui 已提交
15 16
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
R
roo00 已提交
17
import com.x.base.core.project.tools.ListTools;
R
roo00 已提交
18 19 20 21 22
import com.x.processplatform.assemble.surface.Business;
import com.x.processplatform.core.entity.content.Attachment;
import com.x.processplatform.core.entity.content.Work;

class ActionGetWithWork extends BaseAction {
Z
zhourui 已提交
23 24 25

	private static final Logger LOGGER = LoggerFactory.getLogger(ActionGetWithWork.class);

R
roo00 已提交
26
	ActionResult<Wo> execute(EffectivePerson effectivePerson, String id, String workId) throws Exception {
Z
zhourui 已提交
27 28 29

		LOGGER.debug("execute:{}, id:{}, workId:{}.", effectivePerson::getDistinguishedName, () -> id, () -> workId);

R
roo00 已提交
30 31 32 33 34
		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
			ActionResult<Wo> result = new ActionResult<>();
			Business business = new Business(emc);
			Work work = emc.find(workId, Work.class);
			if (null == work) {
R
roo00 已提交
35
				throw new ExceptionEntityNotExist(workId, Work.class);
R
roo00 已提交
36 37 38
			}
			Attachment attachment = emc.find(id, Attachment.class);
			if (null == attachment) {
R
roo00 已提交
39
				throw new ExceptionEntityNotExist(id, Attachment.class);
R
roo00 已提交
40
			}
R
roo00 已提交
41

Z
zhourui 已提交
42
			if (!business.readableWithWorkOrWorkCompleted(effectivePerson, work.getId())) {
R
roo00 已提交
43
				throw new ExceptionAccessDenied(effectivePerson);
R
roo00 已提交
44
			}
R
roo00 已提交
45

R
roo00 已提交
46
			Wo wo = Wo.copier.copy(attachment);
R
roo00 已提交
47 48 49 50 51

			List<String> identities = business.organization().identity().listWithPerson(effectivePerson);

			List<String> units = business.organization().unit().listWithPerson(effectivePerson);

O
o2sword 已提交
52 53 54
			boolean canControl = this.control(attachment, effectivePerson, identities, units, business);
			boolean canEdit = this.edit(attachment, effectivePerson, identities, units, business);
			boolean canRead = this.read(attachment, effectivePerson, identities, units, business);
O
o2sword 已提交
55
			if (canRead) {
R
roo00 已提交
56
				wo.getControl().setAllowRead(true);
O
o2sword 已提交
57 58
				wo.getControl().setAllowEdit(canEdit);
				wo.getControl().setAllowControl(canControl);
R
roo00 已提交
59 60
			}

R
roo00 已提交
61 62 63 64 65 66 67 68 69
			result.setData(wo);
			return result;
		}
	}

	public static class Wo extends Attachment {

		private static final long serialVersionUID = 1954637399762611493L;

R
roo00 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
		static WrapCopier<Attachment, Wo> copier = WrapCopierFactory.wo(Attachment.class, Wo.class, null,
				JpaObject.FieldsInvisible);

		private WoControl control = new WoControl();

		public WoControl getControl() {
			return control;
		}

		public void setControl(WoControl control) {
			this.control = control;
		}

	}

	public static class WoControl extends GsonPropertyObject {

		private Boolean allowRead = false;
		private Boolean allowEdit = false;
		private Boolean allowControl = false;

		public Boolean getAllowRead() {
			return allowRead;
		}

		public void setAllowRead(Boolean allowRead) {
			this.allowRead = allowRead;
		}

		public Boolean getAllowEdit() {
			return allowEdit;
		}

		public void setAllowEdit(Boolean allowEdit) {
			this.allowEdit = allowEdit;
		}
R
roo00 已提交
106

R
roo00 已提交
107 108 109 110 111 112 113
		public Boolean getAllowControl() {
			return allowControl;
		}

		public void setAllowControl(Boolean allowControl) {
			this.allowControl = allowControl;
		}
R
roo00 已提交
114 115 116

	}

R
roo00 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
	private boolean read(Wo wo, EffectivePerson effectivePerson, List<String> identities, List<String> units)
			throws Exception {
		boolean value = false;
		if (effectivePerson.isPerson(wo.getPerson())) {
			value = true;
		} else if (ListTools.isEmpty(wo.getReadIdentityList()) && ListTools.isEmpty(wo.getReadUnitList())) {
			value = true;
		} else {
			if (ListTools.containsAny(identities, wo.getReadIdentityList())
					|| ListTools.containsAny(identities, wo.getReadUnitList())) {
				value = true;
			}
		}
		wo.getControl().setAllowRead(value);
		return value;
	}

	private boolean edit(Wo wo, EffectivePerson effectivePerson, List<String> identities, List<String> units)
			throws Exception {
		boolean value = false;
		if (effectivePerson.isPerson(wo.getPerson())) {
			value = true;
		} else if (ListTools.isEmpty(wo.getEditIdentityList()) && ListTools.isEmpty(wo.getEditUnitList())) {
			value = true;
		} else {
			if (ListTools.containsAny(identities, wo.getEditIdentityList())
					|| ListTools.containsAny(identities, wo.getEditUnitList())) {
				value = true;
			}
		}
		return value;
	}

	private boolean control(Wo wo, EffectivePerson effectivePerson, List<String> identities, List<String> units)
			throws Exception {
		boolean value = false;
		if (effectivePerson.isPerson(wo.getPerson())) {
			value = true;
		} else if (ListTools.isEmpty(wo.getControllerUnitList()) && ListTools.isEmpty(wo.getControllerIdentityList())) {
			value = true;
		} else {
			if (ListTools.containsAny(identities, wo.getControllerIdentityList())
					|| ListTools.containsAny(identities, wo.getControllerUnitList())) {
				value = true;
			}
		}
		return value;
R
roo00 已提交
164
	}
R
roo00 已提交
165
}