1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.mobility.writer.dao;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import javax.persistence.EntityManager;
22 import javax.persistence.PersistenceContext;
23 import javax.persistence.Query;
24
25 import org.kuali.mobility.writer.entity.Article;
26 import org.kuali.mobility.writer.entity.ArticleRejection;
27 import org.springframework.stereotype.Repository;
28 import org.springframework.transaction.annotation.Transactional;
29
30
31
32
33
34
35 @Repository
36 public class ArticleDaoImpl implements ArticleDao {
37
38 @PersistenceContext
39 private EntityManager entityManager;
40
41 public EntityManager getEntityManager() {
42 return entityManager;
43 }
44
45 public void setEntityManager(EntityManager entityManager) {
46 this.entityManager = entityManager;
47 }
48
49 public Article getArticle(long articleId) {
50 return getEntityManager().find(Article.class, articleId);
51 }
52
53
54 @Override
55 @Transactional
56 public Article maintainArticle(Article article) {
57 Article a = article;
58 if (article.getId() == null){
59 getEntityManager().persist(article);
60 }
61 else {
62 a = getEntityManager().merge(article);
63 }
64 return a;
65 }
66
67 @Override
68 public List<Article> getRejectedArticles(String tool, String userId) {
69 Query query = getEntityManager().createNamedQuery("Article.getRejectedArticles");
70 query.setParameter("userId", userId);
71 query.setParameter("tool", tool);
72 return query.getResultList();
73 }
74
75 @Override
76 public List<Article> getSavedArticles(String tool, String userId, boolean isEditor) {
77 String nq = (isEditor ? "Article.getSavedArticlesEditor" : "Article.getSavedArticles");
78 Query query = getEntityManager().createNamedQuery(nq);
79 query.setParameter("userId", userId);
80 query.setParameter("tool", tool);
81 return query.getResultList();
82 }
83
84
85 @Override
86 public List<Article> getSubmittedArticles(String tool) {
87 Query query = getEntityManager().createNamedQuery("Article.getSubmittedArticles");
88 query.setParameter("tool", tool);
89 return query.getResultList();
90 }
91
92 @Override
93 public ArticleRejection getArticleRejection(long rejectionId) {
94 return getEntityManager().find(ArticleRejection.class, rejectionId);
95 }
96
97
98 @Override
99 public long getNumArticles(String tool, long topicId) {
100 String nq = (topicId == 0 ? "Article.getNumArticlesTopNews" : "Article.getNumArticles");
101 Query query = getEntityManager().createNamedQuery(nq);
102 if (topicId != 0) {
103 query.setParameter("topicId", topicId);
104 }
105 query.setParameter("tool", tool);
106 return (Long)query.getSingleResult();
107 }
108
109 @Override
110 public List<Article> getArticles(String tool, long topicId, int from, int fetchSize) {
111 String nq = (topicId == 0 ? "Article.getTopNewsRange" : "Article.getNewsRange");
112 Query query = getEntityManager().createNamedQuery(nq);
113 query.setFirstResult(from);
114 query.setMaxResults(fetchSize);
115 if (topicId != 0) {
116 query.setParameter("topicId", topicId);
117 }
118 query.setParameter("tool", tool);
119 List<Article> resultList = new ArrayList<Article>();
120 resultList.addAll(query.getResultList());
121 return resultList;
122 }
123
124 @Override
125 public List<Article> searchArticles(String tool, String searchText, int from, int fetchSize) {
126 Query query = this.getEntityManager().createNamedQuery("Article.searchNews");
127 query.setParameter("text","%"+ searchText+"%");
128 query.setParameter("tool", tool);
129 query.setFirstResult(from);
130 query.setMaxResults(fetchSize);
131 return query.getResultList();
132 }
133
134 @Override
135 public long searchArticlesCount(String tool, String searchText) {
136 Query query = this.getEntityManager().createNamedQuery("Article.searchNewsCount");
137 query.setParameter("text","%"+ searchText+"%");
138 query.setParameter("tool", tool);
139 return (Long)query.getSingleResult();
140 }
141
142 @Override
143 public long getNumberRejectedArticles(String tool, String userId) {
144 Query query = getEntityManager().createNamedQuery("Article.getNumRejectedArticles");
145 query.setParameter("userId", userId);
146 query.setParameter("tool", tool);
147 return (Long)query.getSingleResult();
148 }
149
150 @Override
151 public long getNumberSavedArticles(String instance, String userId, boolean isEditor) {
152 String nq = (isEditor ? "Article.getNumSavedArticlesEditor" : "Article.getNumSavedArticles");
153 Query query = getEntityManager().createNamedQuery(nq);
154 query.setParameter("userId", userId);
155 query.setParameter("tool", instance);
156 return (Long)query.getSingleResult();
157 }
158
159 @Override
160 public long getNumberSubmittedArticles(String instance) {
161 Query query = getEntityManager().createNamedQuery("Article.getNumSubmittedArticles");
162 query.setParameter("tool", instance);
163 return (Long)query.getSingleResult();
164 }
165 }