FormFactory.java 5.1 KB
Newer Older
R
roo00 已提交
1 2
package com.x.cms.assemble.control.factory;

R
roo00 已提交
3 4 5 6 7 8 9 10
import com.x.base.core.project.exception.ExceptionWhen;
import com.x.base.core.project.tools.ListTools;
import com.x.cms.assemble.control.AbstractFactory;
import com.x.cms.assemble.control.Business;
import com.x.cms.core.entity.element.Form;
import com.x.cms.core.entity.element.Form_;
import com.x.cms.core.express.tools.CriteriaBuilderTools;
import org.apache.commons.lang3.StringUtils;
R
roo00 已提交
11 12 13 14 15 16

import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
R
roo00 已提交
17
import java.util.List;
R
roo00 已提交
18 19 20 21


/**
 * 分类表单模板信息管理表基础功能服务类
22
 *
R
roo00 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 * @author O2LEE
 */
public class FormFactory extends AbstractFactory {

	public FormFactory( Business business ) throws Exception {
		super(business);
	}

	/**
	 * @param id
	 * @return Form
	 * @throws Exception
	 */
	//@MethodDescribe("获取指定Id的Form文件附件信息对象")
	public Form get( String id ) throws Exception {
		return this.entityManagerContainer().find( id, Form.class, ExceptionWhen.none );
	}
40

R
roo00 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53
	/**
	 * @return List:String
	 * @throws Exception
	 */
	//@MethodDescribe("列示全部的Form文件附件信息ID列表")
	public List<String> listAll() throws Exception {
		EntityManager em = this.entityManagerContainer().get( Form.class );
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<String> cq = cb.createQuery(String.class);
		Root<Form> root = cq.from( Form.class );
		cq.select(root.get(Form_.id));
		return em.createQuery(cq).getResultList();
	}
54

R
roo00 已提交
55
	/**
56
	 *
R
roo00 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
	 * @param ids 需要查询的ID列表
	 * @return List:Form
	 * @throws Exception
	 */
	//@MethodDescribe("列示指定Id的Form文件附件信息ID列表")
//	public List<Form> list(List<String> ids) throws Exception {
//		if(ListTools.isEmpty( ids )) {
//			return null;
//		}
//		EntityManager em = this.entityManagerContainer().get( Form.class );
//		CriteriaBuilder cb = em.getCriteriaBuilder();
//		CriteriaQuery<Form> cq = cb.createQuery( Form.class );
//		Root<Form> root = cq.from( Form.class );
//		Predicate p = root.get(Form_.id).in(ids);
//		return em.createQuery(cq.where(p)).getResultList();
//	}
73

R
roo00 已提交
74 75
	/**
	 * 列示指定应用的所有表单模板信息ID列表
76
	 * @param appId 指定的文档ID
R
roo00 已提交
77
	 * @return
78
	 * @throws Exception
R
roo00 已提交
79 80
	 */
	//@MethodDescribe("列示指定分类的所有表单模板信息ID列表")
81
	public List<String> listByAppId( String appId ) throws Exception {
R
fix  
roo00 已提交
82
		if( StringUtils.isEmpty(appId) ){
R
roo00 已提交
83 84 85 86 87 88 89 90 91 92
			throw new Exception("内容管理listByAppId方法不接受appId为空的查询操作!");
		}
		EntityManager em = this.entityManagerContainer().get( Form.class );
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<String> cq = cb.createQuery(String.class);
		Root<Form> root = cq.from( Form.class );
		cq.select(root.get(Form_.id));
		Predicate p = cb.equal(root.get( Form_.appId ), appId);
		return em.createQuery(cq.where(p)).getResultList();
	}
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

	public List<String> listByAppIds(List<String> appIds) throws Exception {
		EntityManager em = this.entityManagerContainer().get(Form.class);
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<String> cq = cb.createQuery(String.class);
		Root<Form> root = cq.from(Form.class);
		Predicate p = cb.conjunction();
		if(ListTools.isNotEmpty(appIds)) {
			p = cb.isMember(root.get(Form_.appId), cb.literal(appIds));
		}
		cq.select(root.get(Form_.id)).where(p);
		return em.createQuery(cq).getResultList();
	}

	public List<Form> listFormByAppId( String appId ) throws Exception {
R
fix  
roo00 已提交
108
		if( StringUtils.isEmpty(appId) ){
R
roo00 已提交
109 110 111 112 113 114 115 116 117
			throw new Exception("内容管理listByAppId方法不接受appId为空的查询操作!");
		}
		EntityManager em = this.entityManagerContainer().get( Form.class );
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Form> cq = cb.createQuery(Form.class);
		Root<Form> root = cq.from( Form.class );
		Predicate p = cb.equal(root.get( Form_.appId ), appId);
		return em.createQuery(cq.where(p)).getResultList();
	}
R
roo00 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

	/**
	 * 根据栏目ID以及表单的标识 获取一个表单对象
	 * @param appId 栏目ID
	 * @param formFlag 表单标识
	 * @return
	 * @throws Exception
	 */
	public Form getWithAppInfo(String appId, String formFlag) throws Exception {
		if( StringUtils.isEmpty(appId) ){
			throw new Exception("appId can not empty!");
		}
		if( StringUtils.isEmpty(formFlag) ){
			throw new Exception("formFlag can not empty!");
		}
		EntityManager em = this.entityManagerContainer().get( Form.class );
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Form> cq = cb.createQuery(Form.class);
		Root<Form> root = cq.from( Form.class );
		Predicate appPre = cb.equal( root.get( Form_.appId ), appId );
		Predicate p = CriteriaBuilderTools.predicate_or( cb, cb.equal( root.get( Form_.id ), formFlag ), cb.equal( root.get( Form_.name ), formFlag ) );
139
//		p = CriteriaBuilderTools.predicate_or( cb, cb.equal( root.get( Form_.alias ), formFlag ), p );
R
roo00 已提交
140
		p = CriteriaBuilderTools.predicate_and( cb, appPre, p );
141
		List<Form> list = em.createQuery(cq.where(p)).getResultList();
R
roo00 已提交
142 143
		return ListTools.isEmpty( list ) ? null : list.get( 0 );
	}
144
}