提交 93d586e0 编写于 作者: O o2sword

增加根据组织类型查询组织的接口

上级 730d0c49
package com.x.organization.assemble.express.jaxrs.unit;
import com.x.base.core.container.EntityManagerContainer;
import com.x.base.core.container.factory.EntityManagerContainerFactory;
import com.x.base.core.project.cache.Cache.CacheKey;
import com.x.base.core.project.cache.CacheManager;
import com.x.base.core.project.http.ActionResult;
import com.x.base.core.project.http.EffectivePerson;
import com.x.organization.assemble.express.Business;
import com.x.organization.core.entity.Unit;
import com.x.organization.core.entity.Unit_;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
class ActionListWithTypeObject extends BaseAction {
@SuppressWarnings("unchecked")
ActionResult<List<Wo>> execute(EffectivePerson effectivePerson, String type) throws Exception {
try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
ActionResult<List<Wo>> result = new ActionResult<>();
Business business = new Business(emc);
CacheKey cacheKey = new CacheKey(this.getClass(), type);
Optional<?> optional = CacheManager.get(cacheCategory, cacheKey);
if (optional.isPresent()) {
result.setData((List<Wo>) optional.get());
} else {
List<Wo> wos = this.list(business, type);
CacheManager.put(cacheCategory, cacheKey, wos);
result.setData(wos);
}
return result;
}
}
public static class Wo extends com.x.base.core.project.organization.Unit {
}
private List<Wo> list(Business business, String type) throws Exception {
List<Wo> wos = new ArrayList<>();
EntityManager em = business.entityManagerContainer().get(Unit.class);
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<String> cq = cb.createQuery(String.class);
Root<Unit> root = cq.from(Unit.class);
Predicate p = cb.isMember(type, root.get(Unit_.typeList));
List<String> unitIds = em.createQuery(cq.select(root.get(Unit_.id)).where(p)).getResultList();
List<Unit> units = business.unit().pick(unitIds);
units = business.unit().sort(units);
for (Unit o : units) {
wos.add(this.convert(business, o, Wo.class));
}
return wos;
}
}
package com.x.organization.assemble.express.jaxrs.unit;
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.project.annotation.FieldDescribe;
import com.x.base.core.project.cache.Cache.CacheKey;
import com.x.base.core.project.cache.CacheManager;
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.base.core.project.tools.ListTools;
import com.x.organization.assemble.express.Business;
import com.x.organization.core.entity.Unit;
import com.x.organization.core.entity.Unit_;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
class ActionListWithTypes extends BaseAction {
ActionResult<Wo> execute(EffectivePerson effectivePerson, JsonElement jsonElement) throws Exception {
try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
Wi wi = this.convertToWrapIn(jsonElement, Wi.class);
ActionResult<Wo> result = new ActionResult<>();
if(ListTools.isEmpty(wi.getTypeList())){
result.setData(new Wo());
}
Business business = new Business(emc);
CacheKey cacheKey = new CacheKey(this.getClass(), wi.getTypeList());
Optional<?> optional = CacheManager.get(cacheCategory, cacheKey);
if (optional.isPresent()) {
result.setData((Wo) optional.get());
} else {
Wo wo = this.list(business, wi);
CacheManager.put(cacheCategory, cacheKey, wo);
result.setData(wo);
}
return result;
}
}
public static class Wi extends GsonPropertyObject {
@FieldDescribe("组织类型")
private List<String> typeList = new ArrayList<>();
public List<String> getTypeList() {
return typeList;
}
public void setTypeList(List<String> typeList) {
this.typeList = typeList;
}
}
public static class Wo extends WoUnitListAbstract {
}
private Wo list(Business business, Wi wi) throws Exception {
EntityManager em = business.entityManagerContainer().get(Unit.class);
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<String> cq = cb.createQuery(String.class);
Root<Unit> root = cq.from(Unit.class);
Predicate p = root.get(Unit_.typeList).in(wi.getTypeList());
List<String> unitIds = em.createQuery(cq.select(root.get(Unit_.id)).where(p)).getResultList().stream().distinct().collect(Collectors.toList());
Wo wo = new Wo();
List<String> list = business.unit().listUnitDistinguishedNameSorted(unitIds);
wo.getUnitList().addAll(list);
return wo;
}
}
package com.x.organization.assemble.express.jaxrs.unit;
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.project.annotation.FieldDescribe;
import com.x.base.core.project.cache.Cache.CacheKey;
import com.x.base.core.project.cache.CacheManager;
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.base.core.project.tools.ListTools;
import com.x.organization.assemble.express.Business;
import com.x.organization.core.entity.Unit;
import com.x.organization.core.entity.Unit_;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
class ActionListWithTypesObject extends BaseAction {
@SuppressWarnings("unchecked")
ActionResult<List<Wo>> execute(EffectivePerson effectivePerson, JsonElement jsonElement) throws Exception {
try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
Wi wi = this.convertToWrapIn(jsonElement, Wi.class);
ActionResult<List<Wo>> result = new ActionResult<>();
Business business = new Business(emc);
if(ListTools.isEmpty(wi.getTypeList())){
result.setData(new ArrayList<>());
}
CacheKey cacheKey = new CacheKey(this.getClass(), wi.getTypeList());
Optional<?> optional = CacheManager.get(cacheCategory, cacheKey);
if (optional.isPresent()) {
result.setData((List<Wo>) optional.get());
} else {
List<Wo> wos = this.list(business, wi);
CacheManager.put(cacheCategory, cacheKey, wos);
result.setData(wos);
}
return result;
}
}
public static class Wi extends GsonPropertyObject {
@FieldDescribe("组织类型")
private List<String> typeList = new ArrayList<>();
public List<String> getTypeList() {
return typeList;
}
public void setTypeList(List<String> typeList) {
this.typeList = typeList;
}
}
public static class Wo extends com.x.base.core.project.organization.Unit {
}
private List<Wo> list(Business business, Wi wi) throws Exception {
List<Wo> wos = new ArrayList<>();
EntityManager em = business.entityManagerContainer().get(Unit.class);
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<String> cq = cb.createQuery(String.class);
Root<Unit> root = cq.from(Unit.class);
Predicate p = root.get(Unit_.typeList).in(wi.getTypeList());
List<String> unitIds = em.createQuery(cq.select(root.get(Unit_.id)).where(p)).getResultList();
List<Unit> units = business.unit().pick(unitIds);
units = business.unit().sort(units);
for (Unit o : units) {
wos.add(this.convert(business, o, Wo.class));
}
return wos;
}
}
......@@ -3,11 +3,7 @@ package com.x.organization.assemble.express.jaxrs.unit;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.*;
import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.container.Suspended;
import javax.ws.rs.core.Context;
......@@ -16,6 +12,7 @@ import javax.ws.rs.core.MediaType;
import com.google.gson.JsonElement;
import com.x.base.core.project.annotation.JaxrsDescribe;
import com.x.base.core.project.annotation.JaxrsMethodDescribe;
import com.x.base.core.project.annotation.JaxrsParameterDescribe;
import com.x.base.core.project.http.ActionResult;
import com.x.base.core.project.http.EffectivePerson;
import com.x.base.core.project.http.HttpMediaType;
......@@ -639,4 +636,58 @@ public class UnitAction extends StandardJaxrsAction {
}
asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result, jsonElement));
}
@JaxrsMethodDescribe(value = "批量查询指定组织类型的组织.", action = ActionListWithTypes.class)
@POST
@Path("list/types")
@Produces(HttpMediaType.APPLICATION_JSON_UTF_8)
@Consumes(MediaType.APPLICATION_JSON)
public void listWithTypes(@Suspended final AsyncResponse asyncResponse, @Context HttpServletRequest request,
JsonElement jsonElement) {
ActionResult<ActionListWithTypes.Wo> result = new ActionResult<>();
EffectivePerson effectivePerson = this.effectivePerson(request);
try {
result = new ActionListWithTypes().execute(effectivePerson, jsonElement);
} catch (Exception e) {
logger.error(e, effectivePerson, request, jsonElement);
result.error(e);
}
asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result, jsonElement));
}
@JaxrsMethodDescribe(value = "批量查询指定组织类型的组织对象.", action = ActionListWithTypesObject.class)
@POST
@Path("list/types/object")
@Produces(HttpMediaType.APPLICATION_JSON_UTF_8)
@Consumes(MediaType.APPLICATION_JSON)
public void listWithTypesObject(@Suspended final AsyncResponse asyncResponse, @Context HttpServletRequest request,
JsonElement jsonElement) {
ActionResult<List<ActionListWithTypesObject.Wo>> result = new ActionResult<>();
EffectivePerson effectivePerson = this.effectivePerson(request);
try {
result = new ActionListWithTypesObject().execute(effectivePerson, jsonElement);
} catch (Exception e) {
logger.error(e, effectivePerson, request, jsonElement);
result.error(e);
}
asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result, jsonElement));
}
@JaxrsMethodDescribe(value = "查询指定组织类型的组织对象.", action = ActionListWithTypeObject.class)
@GET
@Path("list/type/{type}/object")
@Produces(HttpMediaType.APPLICATION_JSON_UTF_8)
@Consumes(MediaType.APPLICATION_JSON)
public void listWithTypeObject(@Suspended final AsyncResponse asyncResponse, @Context HttpServletRequest request,
@JaxrsParameterDescribe("组织类型") @PathParam("type") String type) {
ActionResult<List<ActionListWithTypeObject.Wo>> result = new ActionResult<>();
EffectivePerson effectivePerson = this.effectivePerson(request);
try {
result = new ActionListWithTypeObject().execute(effectivePerson, type);
} catch (Exception e) {
logger.error(e, effectivePerson, request, null);
result.error(e);
}
asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result, null));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册