提交 c32696ae 编写于 作者: O o2null

Merge branch 'fix/允许自建表查询条件增加order语句' into 'develop_java8'

[数据中心]允许自建表查询条件增加order语句,pic到重庆移动

See merge request o2oa/o2oa!3227

(cherry picked from commit e4a32fe5)

3ed607f5 允许自建表查询条件增加order语句
a946f2a1 优化自建表查询表数据接口,允许排序和结果集限制
上级 a7cde51b
package com.x.query.assemble.surface.jaxrs.table;
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.dynamic.DynamicEntity;
import com.x.base.core.project.annotation.FieldDescribe;
import com.x.base.core.project.exception.ExceptionAccessDenied;
import com.x.base.core.project.exception.ExceptionEntityNotExist;
import com.x.base.core.project.gson.GsonPropertyObject;
import com.x.base.core.project.http.ActionResult;
import com.x.base.core.project.http.EffectivePerson;
import com.x.query.assemble.surface.Business;
import com.x.query.core.entity.schema.Table;
import org.apache.commons.lang3.StringUtils;
import javax.persistence.EntityManager;
import java.util.List;
class ActionListRowSelect extends BaseAction {
ActionResult<List<?>> execute(EffectivePerson effectivePerson, String tableFlag, JsonElement jsonElement) throws Exception {
try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
ActionResult<List<?>> result = new ActionResult<>();
Wi wi = this.convertToWrapIn(jsonElement, Wi.class);
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());
@SuppressWarnings("unchecked")
Class<? extends JpaObject> clz = (Class<JpaObject>) Class.forName(dynamicEntity.className());
EntityManager em = emc.get(clz);
String sql = "SELECT o FROM " + clz.getName() + " o";
if (StringUtils.isNotBlank(wi.getWhere())) {
sql += " where " + wi.getWhere();
}
if (StringUtils.isNotBlank(wi.getOrderBy())) {
sql += " order by " + wi.getOrderBy();
}
List<?> list;
if(wi.getSize()!=null && wi.getSize()>0){
list = em.createQuery(sql).setMaxResults(wi.getSize()).getResultList();
}else {
list = em.createQuery(sql).getResultList();
}
result.setData(list);
return result;
}
}
public static class Wi extends GsonPropertyObject {
@FieldDescribe("查询条件,格式为jpql语法,o.name='zhangsan',允许为空")
private String where;
@FieldDescribe("排序条件,格式为:o.updateTime desc,允许为空")
private String orderBy;
@FieldDescribe("返回结果集数量,允许为空")
private Integer size;
public String getWhere() {
return where;
}
public void setWhere(String where) {
this.where = where;
}
public String getOrderBy() {
return orderBy;
}
public void setOrderBy(String orderBy) {
this.orderBy = orderBy;
}
public Integer getSize() {
return size;
}
public void setSize(Integer size) {
this.size = size;
}
}
}
......@@ -36,7 +36,7 @@ class ActionListRowSelectWhere extends BaseAction {
EntityManager em = emc.get(clz);
String sql = "SELECT o FROM " + clz.getName() + " o";
if (StringUtils.isNotBlank(where) && (!StringUtils.equals(where, EMPTY_SYMBOL))) {
sql += " where (" + where + ")";
sql += " where " + where;
}
List<?> list = em.createQuery(sql).getResultList();
result.setData(list);
......@@ -44,4 +44,4 @@ class ActionListRowSelectWhere extends BaseAction {
}
}
}
\ No newline at end of file
}
......@@ -111,6 +111,25 @@ public class TableAction extends StandardJaxrsAction {
asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result));
}
@JaxrsMethodDescribe(value = "根据条件获取表中的数据", action = ActionListRowSelect.class)
@POST
@Path("list/{tableFlag}/row/select")
@Produces(HttpMediaType.APPLICATION_JSON_UTF_8)
@Consumes(MediaType.APPLICATION_JSON)
public void listRowSelect(@Suspended final AsyncResponse asyncResponse, @Context HttpServletRequest request,
@JaxrsParameterDescribe("表标识") @PathParam("tableFlag") String tableFlag,
JsonElement jsonElement) {
ActionResult<List<?>> result = new ActionResult<>();
EffectivePerson effectivePerson = this.effectivePerson(request);
try {
result = new ActionListRowSelect().execute(effectivePerson, tableFlag, jsonElement);
} catch (Exception e) {
logger.error(e, effectivePerson, request, jsonElement);
result.error(e);
}
asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result));
}
@JaxrsMethodDescribe(value = "通过where 统计数量", action = ActionRowCountWhere.class)
@GET
@Path("{tableFlag}/row/count/where/{where}")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册