ActionRowCountWhere.java 1.9 KB
Newer Older
R
roo00 已提交
1 2 3 4 5 6 7 8 9
package com.x.query.assemble.designer.jaxrs.table;

import javax.persistence.EntityManager;

import org.apache.commons.lang3.StringUtils;

import com.x.base.core.container.EntityManagerContainer;
import com.x.base.core.container.factory.EntityManagerContainerFactory;
import com.x.base.core.entity.JpaObject;
R
roo00 已提交
10
import com.x.base.core.entity.dynamic.DynamicEntity;
R
roo00 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
import com.x.base.core.project.exception.ExceptionAccessDenied;
import com.x.base.core.project.exception.ExceptionEntityNotExist;
import com.x.base.core.project.http.ActionResult;
import com.x.base.core.project.http.EffectivePerson;
import com.x.base.core.project.jaxrs.WrapLong;
import com.x.query.assemble.designer.Business;
import com.x.query.core.entity.schema.Table;

class ActionRowCountWhere extends BaseAction {

	ActionResult<Wo> execute(EffectivePerson effectivePerson, String tableFlag, String where)
			throws Exception {
		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
			ActionResult<Wo> result = new ActionResult<>();
			Table table = emc.flag(tableFlag, Table.class);
			Business business = new Business(emc);
			if (null == table) {
				throw new ExceptionEntityNotExist(tableFlag, Table.class);
			}
			if (!business.editable(effectivePerson, table)) {
				throw new ExceptionAccessDenied(effectivePerson, table);
			}
			DynamicEntity dynamicEntity = new DynamicEntity(table.getName());
R
roo00 已提交
34 35 36 37
			@SuppressWarnings("unchecked")
			Class<? extends JpaObject> cls = (Class<JpaObject>) Class.forName(dynamicEntity.className());
			EntityManager em = emc.get(cls);
			String sql = "SELECT count(o) FROM " + cls.getName() + " o";
R
roo00 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
			if (StringUtils.isNotBlank(where) && (!StringUtils.equals(where, EMPTY_SYMBOL))) {
				sql += " where (" + where + ")";
			}
			Long count = (Long) em.createQuery(sql).getSingleResult();
			Wo wo = new Wo();
			wo.setValue(count);
			result.setData(wo);
			return result;
		}
	}

	public static class Wo extends WrapLong {

	}

}