ActionListRowNext.java 3.4 KB
Newer Older
R
roo00 已提交
1 2 3 4 5 6
package com.x.query.assemble.designer.jaxrs.table;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
R
roo00 已提交
7
import javax.persistence.Query;
R
roo00 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

import org.apache.commons.lang3.StringUtils;

import com.google.gson.JsonObject;
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.exception.ExceptionAccessDenied;
import com.x.base.core.project.exception.ExceptionEntityNotExist;
import com.x.base.core.project.gson.XGsonBuilder;
import com.x.base.core.project.http.ActionResult;
import com.x.base.core.project.http.EffectivePerson;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import com.x.base.core.project.tools.ListTools;
import com.x.query.assemble.designer.Business;
import com.x.query.assemble.designer.DynamicEntity;
import com.x.query.core.entity.schema.Table;

class ActionListRowNext extends BaseAction {

	private static Logger logger = LoggerFactory.getLogger(ActionListRowNext.class);

	ActionResult<List<JsonObject>> execute(EffectivePerson effectivePerson, String tableFlag, String id, Integer count)
			throws Exception {
		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
R
roo00 已提交
34

R
roo00 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
			ActionResult<List<JsonObject>> result = new ActionResult<>();
			logger.debug(effectivePerson, "table:{}, id:{}, count:{}.", tableFlag, id, count);
			Business business = new Business(emc);
			Table table = emc.flag(tableFlag, Table.class);
			if (null == table) {
				throw new ExceptionEntityNotExist(tableFlag, Table.class);
			}
			if (!business.readable(effectivePerson, table)) {
				throw new ExceptionAccessDenied(effectivePerson.getDistinguishedName());
			}
			DynamicEntity dynamicEntity = new DynamicEntity(table.getName());
			Class<? extends JpaObject> cls = dynamicEntity.getObjectClass();
			EntityManager em = emc.get(cls);
			Object sequence = null;
			if (!StringUtils.equals(EMPTY_SYMBOL, id)) {
				JpaObject o = emc.fetch(id, cls, ListTools.toList(JpaObject.sequence_FIELDNAME));
				if (null != o) {
					sequence = o.getSequence();
				}
			}
R
roo00 已提交
55 56 57 58 59 60 61
			List<String> fields = JpaObject.singularAttributeField(cls, true, true);
			fields.add(JpaObject.sequence_FIELDNAME);
			List<String> selects = new ArrayList<>();
			for (String str : fields) {
				selects.add("o." + str);
			}
			String sql = "select " + StringUtils.join(selects, ", ") + " from " + cls.getName() + " o";
R
roo00 已提交
62 63
			Long rank = 0L;
			if (null != sequence) {
R
roo00 已提交
64 65
				sql += " where o." + JpaObject.sequence_FIELDNAME + " < ?1";
				rank = emc.countGreaterThanOrEqualTo(cls, JpaObject.sequence_FIELDNAME, sequence);
R
roo00 已提交
66 67
			}
			sql += " order by o." + JpaObject.sequence_FIELDNAME + " DESC";
R
roo00 已提交
68 69 70 71 72
			Query query = em.createQuery(sql, Object[].class);
			if (null != sequence) {
				query.setParameter(1, sequence);
			}
			List<Object[]> list = query.setMaxResults(Math.max(Math.min(count, list_max), list_min)).getResultList();
R
roo00 已提交
73 74
			List<JsonObject> wos = new ArrayList<>();
			result.setCount(emc.count(cls));
R
roo00 已提交
75 76 77
			for (Object[] os : list) {
				JsonObject jsonObject = XGsonBuilder.instance().toJsonTree(JpaObject.cast(cls, fields, os))
						.getAsJsonObject();
R
roo00 已提交
78 79 80 81 82 83 84 85
				jsonObject.getAsJsonObject().addProperty("rank", ++rank);
				wos.add(jsonObject);
			}
			result.setData(wos);
			return result;
		}
	}
}