BaseService.java
资源名称:cindyblog.rar [点击查看]
上传用户:cdpainuo
上传日期:2022-07-12
资源大小:5257k
文件大小:5k
源码类别:
Jsp/Servlet
开发平台:
Java
- package com.appspot.cindyblog.service;
- import java.util.List;
- import java.util.Map;
- import javax.persistence.EntityManager;
- import org.json.JSONArray;
- import org.json.JSONObject;
- import com.appspot.cindyblog.dao.ArticleDAO;
- import com.appspot.cindyblog.dao.BaseDAO;
- import com.appspot.cindyblog.dao.DiscussDAO;
- import com.appspot.cindyblog.dao.LinkDAO;
- import com.appspot.cindyblog.dao.MessageDAO;
- import com.appspot.cindyblog.dao.TypeDAO;
- import com.appspot.cindyblog.pojo.BaseBean;
- import com.appspot.cindyblog.util.Singleton;
- public class BaseService implements IBaseService{
- BaseDAO baseDAO = null;
- ArticleDAO articleDAO = null;
- DiscussDAO discussDAO = null;
- LinkDAO linkDAO = null;
- MessageDAO messageDAO = null;
- TypeDAO typeDAO = null;
- protected BaseDAO getBaseDAO(){
- if(baseDAO == null){
- return (BaseDAO)Singleton.getInstance(BaseDAO.class);
- }else{
- return baseDAO;
- }
- }
- protected ArticleDAO getArticleDAO(){
- if(articleDAO == null){
- return (ArticleDAO)Singleton.getInstance(ArticleDAO.class);
- }else{
- return articleDAO;
- }
- }
- protected DiscussDAO getDiscussDAO(){
- if(discussDAO == null){
- return (DiscussDAO)Singleton.getInstance(DiscussDAO.class);
- }else{
- return discussDAO;
- }
- }
- protected MessageDAO getMessageDAO(){
- if(messageDAO == null){
- return (MessageDAO)Singleton.getInstance(MessageDAO.class);
- }else{
- return messageDAO;
- }
- }
- protected LinkDAO getLinkDAO(){
- if(linkDAO == null){
- return (LinkDAO)Singleton.getInstance(LinkDAO.class);
- }else{
- return linkDAO;
- }
- }
- protected TypeDAO getTypeDAO(){
- if(typeDAO == null){
- return (TypeDAO)Singleton.getInstance(TypeDAO.class);
- }else{
- return typeDAO;
- }
- }
- /* (non-Javadoc)
- * @see com.appspot.cindyblog.service.IBaseService#addObject(T, javax.persistence.EntityManager)
- */
- public <T extends BaseBean> boolean addObject(T o,EntityManager em) {
- boolean b = false;
- try{
- baseDAO = getBaseDAO();
- b = baseDAO.addObject(o, em);
- }catch(Exception ex){
- b = false;
- ex.printStackTrace();
- }finally{
- baseDAO = null;
- return b;
- }
- }
- /* (non-Javadoc)
- * @see com.appspot.cindyblog.service.IBaseService#deleteObject(java.lang.Class, java.lang.Long, javax.persistence.EntityManager)
- */
- public <T extends BaseBean> boolean deleteObject(Class<T> clazz,Long id,EntityManager em) {
- boolean b =false;
- try{
- baseDAO = getBaseDAO();
- b = baseDAO.deleteObject(clazz,id, em);
- }catch(Exception ex){
- b = false;
- ex.printStackTrace();
- }finally{
- baseDAO = null;
- return b;
- }
- }
- /* (non-Javadoc)
- * @see com.appspot.cindyblog.service.IBaseService#getObjectById(java.lang.Class, java.lang.Long, javax.persistence.EntityManager)
- */
- public <T extends BaseBean> String getObjectById(Class<T> clazz,Long id,EntityManager em) {
- String str = "";
- try{
- baseDAO = getBaseDAO();
- T t = baseDAO.getObjectById(clazz,id, em);
- JSONObject jsonObject = new JSONObject(t);
- str = jsonObject.toString();
- }catch(Exception ex){
- ex.printStackTrace();
- }finally{
- baseDAO = null;
- return str;
- }
- }
- /* (non-Javadoc)
- * @see com.appspot.cindyblog.service.IBaseService#getObjectCount(java.lang.Class, javax.persistence.EntityManager)
- */
- public <T extends BaseBean> int getObjectCount(Class<T> clazz,EntityManager em) {
- int count = -1;
- try{
- baseDAO = getBaseDAO();
- count = baseDAO.getObjectCount(clazz,em);
- }catch(Exception ex){
- ex.printStackTrace();
- }finally{
- baseDAO = null;
- return count;
- }
- }
- public <T extends BaseBean> String getObjectList(Class<T> clazz,EntityManager em,String orderBy, int start, int limit) {
- String str = "";
- try{
- baseDAO = getBaseDAO();
- List<T> objectList = (List<T>) baseDAO.getObjectList(clazz,em, orderBy, start, limit);
- JSONArray jsonArray = new JSONArray(objectList,false);
- if (objectList.isEmpty()) {
- str = "false";
- }else{
- str = jsonArray.toString();
- }
- }catch(Exception ex){
- ex.printStackTrace();
- }finally{
- baseDAO = null;
- return str;
- }
- }
- public <T extends BaseBean> String getObjectList(Class<T> clazz,EntityManager em,String orderBy,Map<String,String> prames, int start, int limit) {
- String str = "";
- try{
- baseDAO = getBaseDAO();
- List<T> objectList = (List<T>) baseDAO.getObjectList(clazz, em, orderBy, prames, start, limit);
- JSONArray jsonArray = new JSONArray(objectList,false);
- if (objectList.isEmpty()) {
- str = "false";
- }else{
- str = jsonArray.toString();
- }
- }catch(Exception ex){
- ex.printStackTrace();
- }finally{
- baseDAO = null;
- return str;
- }
- }
- /* (non-Javadoc)
- * @see com.appspot.cindyblog.service.IBaseService#getObjectList(java.lang.Class, javax.persistence.EntityManager)
- */
- public <T extends BaseBean> String getObjectList(Class<T> clazz,EntityManager em) {
- String str = "";
- try{
- baseDAO = getBaseDAO();
- List<T> objectList = (List<T>) baseDAO.getObjectList(clazz,em);
- JSONArray jsonArray = new JSONArray(objectList,false);
- if (objectList.isEmpty()) {
- str = "false";
- }else{
- str = jsonArray.toString();
- }
- }catch(Exception ex){
- ex.printStackTrace();
- }finally{
- baseDAO = null;
- return str;
- }
- }
- }