ActionEdit.java 3.2 KB
Newer Older
caixiangyi's avatar
caixiangyi 已提交
1 2
package com.x.organization.assemble.control.jaxrs.unitduty;

O
o2sword 已提交
3
import com.x.base.core.project.cache.CacheManager;
caixiangyi's avatar
caixiangyi 已提交
4 5
import org.apache.commons.lang3.StringUtils;

NoSubject's avatar
NoSubject 已提交
6
import com.google.gson.Gson;
caixiangyi's avatar
caixiangyi 已提交
7 8 9 10 11 12 13 14 15 16 17 18
import com.google.gson.JsonElement;
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.entity.annotation.CheckPersistType;
import com.x.base.core.project.bean.WrapCopier;
import com.x.base.core.project.bean.WrapCopierFactory;
import com.x.base.core.project.http.ActionResult;
import com.x.base.core.project.http.EffectivePerson;
import com.x.base.core.project.jaxrs.WoId;
import com.x.base.core.project.tools.ListTools;
import com.x.organization.assemble.control.Business;
NoSubject's avatar
NoSubject 已提交
19
import com.x.organization.assemble.control.message.OrgMessageFactory;
caixiangyi's avatar
caixiangyi 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
import com.x.organization.core.entity.Unit;
import com.x.organization.core.entity.UnitDuty;

class ActionEdit extends BaseAction {

	ActionResult<Wo> execute(EffectivePerson effectivePerson, String id, JsonElement jsonElement) throws Exception {
		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
			ActionResult<Wo> result = new ActionResult<>();
			Wi wi = this.convertToWrapIn(jsonElement, Wi.class);
			Business business = new Business(emc);
			UnitDuty o = business.unitDuty().pick(id);
			if (null == o) {
				throw new ExceptionUnitDutyNotExist(id);
			}
			Unit unit = business.unit().pick(o.getUnit());
			if (null == unit) {
				throw new ExceptionUnitNotExist(o.getUnit());
			}
			if (!business.editable(effectivePerson, unit)) {
				throw new ExceptionDenyEditUnit(effectivePerson, unit.getName());
			}
			if (StringUtils.isEmpty(wi.getName())) {
				throw new ExceptionNameEmpty();
			}
			if (this.duplicateOnUnit(business, unit, wi.getName(), o)) {
				throw new ExceptionDuplicateOnUnit(wi.getName(), unit.getName());
			}
			/** pick出来的需要重新find */
			o = emc.find(o.getId(), UnitDuty.class);
NoSubject's avatar
NoSubject 已提交
49 50 51 52
			
			Gson gsontool = new Gson();
			String strDuty = gsontool.toJson(o);
			
caixiangyi's avatar
caixiangyi 已提交
53 54 55 56 57 58 59 60 61 62 63
			emc.beginTransaction(UnitDuty.class);
			Wi.copier.copy(wi, o);
			/** 如果唯一标识不为空,要检查唯一标识是否唯一 */
			if (uniqueDuplicateWhenNotEmpty(business, o)) {
				throw new ExceptionDuplicateUnique(o.getName(), o.getUnique());
			}
			o.setUnit(unit.getId());
			o.setIdentityList(ListTools.extractProperty(business.identity().pick(o.getIdentityList()), JpaObject.id_FIELDNAME,
					String.class, true, true));
			emc.check(o, CheckPersistType.all);
			emc.commit();
O
o2sword 已提交
64
			CacheManager.notify(UnitDuty.class);
NoSubject's avatar
NoSubject 已提交
65 66 67 68 69
			
			/**创建 组织变更org消息通信 */
			OrgMessageFactory  orgMessageFactory = new OrgMessageFactory();
			orgMessageFactory.createMessageCommunicate("modfiy", "duty",strDuty, o, effectivePerson);
			
caixiangyi's avatar
caixiangyi 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
			Wo wo = new Wo();
			wo.setId(o.getId());
			result.setData(wo);
			return result;
		}
	}

	public static class Wo extends WoId {
	}

	public static class Wi extends UnitDuty {

		private static final long serialVersionUID = -7527954993386512109L;

		static WrapCopier<Wi, UnitDuty> copier = WrapCopierFactory.wi(Wi.class, UnitDuty.class, null,
				ListTools.toList(JpaObject.FieldsUnmodify, "pinyin", "pinyinInitial", "unit"));

	}

}