ActionGetWithWorkCompleted.java 5.1 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;
R
roo00 已提交
15
import com.x.base.core.project.tools.ListTools;
R
roo00 已提交
16 17 18 19 20 21 22 23 24 25 26
import com.x.processplatform.assemble.surface.Business;
import com.x.processplatform.core.entity.content.Attachment;
import com.x.processplatform.core.entity.content.WorkCompleted;

class ActionGetWithWorkCompleted extends BaseAction {
	ActionResult<Wo> execute(EffectivePerson effectivePerson, String id, String workCompletedId) throws Exception {
		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
			ActionResult<Wo> result = new ActionResult<>();
			Business business = new Business(emc);
			WorkCompleted workCompleted = emc.find(workCompletedId, WorkCompleted.class);
			if (null == workCompleted) {
R
roo00 已提交
27
				throw new ExceptionEntityNotExist(workCompletedId, WorkCompleted.class);
R
roo00 已提交
28 29 30
			}
			Attachment attachment = emc.find(id, Attachment.class);
			if (null == attachment) {
R
roo00 已提交
31
				throw new ExceptionEntityNotExist(id, Attachment.class);
R
roo00 已提交
32
			}
R
roo00 已提交
33 34 35 36

			if (!business.readableWithWorkOrWorkCompleted(effectivePerson, workCompleted.getId(),
					new ExceptionEntityNotExist(workCompleted.getId()))) {
				throw new ExceptionAccessDenied(effectivePerson);
R
roo00 已提交
37
			}
R
roo00 已提交
38

R
roo00 已提交
39
			Wo wo = Wo.copier.copy(attachment);
R
roo00 已提交
40 41 42 43 44

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

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

O
o2sword 已提交
45 46 47
			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 已提交
48
			if (canRead) {
R
roo00 已提交
49
				wo.getControl().setAllowRead(true);
O
o2sword 已提交
50 51
				wo.getControl().setAllowEdit(canEdit);
				wo.getControl().setAllowControl(canControl);
R
roo00 已提交
52 53
			}

R
roo00 已提交
54 55 56 57 58 59 60 61 62
			result.setData(wo);
			return result;
		}
	}

	public static class Wo extends Attachment {

		private static final long serialVersionUID = 1954637399762611493L;

R
roo00 已提交
63 64 65 66 67 68 69 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
		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;
		}
R
roo00 已提交
95

R
roo00 已提交
96 97 98 99 100 101 102 103 104 105 106
		public void setAllowEdit(Boolean allowEdit) {
			this.allowEdit = allowEdit;
		}

		public Boolean getAllowControl() {
			return allowControl;
		}

		public void setAllowControl(Boolean allowControl) {
			this.allowControl = allowControl;
		}
R
roo00 已提交
107 108 109

	}

R
roo00 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
	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;
	}
R
roo00 已提交
126

R
roo00 已提交
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
	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 已提交
157
	}
R
roo00 已提交
158
}