package com.x.processplatform.assemble.designer.jaxrs.querystat;

import com.x.base.core.cache.ApplicationCache;
import com.x.base.core.container.EntityManagerContainer;
import com.x.base.core.container.factory.EntityManagerContainerFactory;
import com.x.base.core.entity.annotation.CheckRemoveType;
import com.x.base.core.exception.ExceptionWhen;
import com.x.base.core.http.ActionResult;
import com.x.base.core.http.EffectivePerson;
import com.x.base.core.http.WrapOutId;
import com.x.processplatform.assemble.designer.Business;
import com.x.processplatform.core.entity.element.Application;
import com.x.processplatform.core.entity.element.QueryStat;

class ActionDelete extends ActionBase {
	ActionResult<WrapOutId> execute(EffectivePerson effectivePerson, String id) throws Exception {
		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
			ActionResult<WrapOutId> result = new ActionResult<>();
			Business business = new Business(emc);
			emc.beginTransaction(QueryStat.class);
			QueryStat queryStat = emc.find(id, QueryStat.class, ExceptionWhen.not_found);
			Application application = emc.find(queryStat.getApplication(), Application.class, ExceptionWhen.not_found);
			business.applicationEditAvailable(effectivePerson, application, ExceptionWhen.not_allow);
			emc.remove(queryStat, CheckRemoveType.all);
			emc.commit();
			ApplicationCache.notify(QueryStat.class);
			WrapOutId wrap = new WrapOutId(queryStat.getId());
			result.setData(wrap);
			return result;
		}
	}
}
