This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
ProjectIterationDAO.java
383 lines (334 loc) · 13.5 KB
/
ProjectIterationDAO.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
* Copyright 2010, Red Hat, Inc. and individual contributors as indicated by the
* @author tags. See the copyright.txt file in the distribution for a full
* listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this software; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
* site: http://www.fsf.org.
*/
package org.zanata.dao;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ws.rs.core.EntityTag;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.hibernate.search.jpa.FullTextEntityManager;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.zanata.common.ContentState;
import org.zanata.common.EntityStatus;
import org.zanata.common.LocaleId;
import org.zanata.common.TransUnitCount;
import org.zanata.common.TransUnitWords;
import org.zanata.model.HProject;
import org.zanata.model.HProjectIteration;
import org.zanata.model.StatusCount;
import org.zanata.util.HashUtil;
@Name("projectIterationDAO")
@AutoCreate
@Scope(ScopeType.STATELESS)
public class ProjectIterationDAO extends AbstractDAOImpl<HProjectIteration, Long>
{
@In
private FullTextEntityManager entityManager;
public ProjectIterationDAO()
{
super(HProjectIteration.class);
}
public ProjectIterationDAO(Session session)
{
super(HProjectIteration.class, session);
}
public HProjectIteration getBySlug(String projectSlug, String iterationSlug)
{
HProject project = (HProject)getSession().byNaturalId(HProject.class).using("slug", projectSlug).load();
return getBySlug(project, iterationSlug);
}
public HProjectIteration getBySlug(HProject project, String iterationSlug)
{
if( project == null || StringUtils.isEmpty(iterationSlug))
{
return null;
}
return (HProjectIteration)getSession().byNaturalId(HProjectIteration.class).using("slug", iterationSlug).using("project",project).load();
}
/**
* @see DocumentDAO#getStatistics(long, LocaleId)
* @param iterationId
* @param localeId
* @return
*/
public TransUnitCount getStatisticsForContainer(Long iterationId, LocaleId localeId)
{
// @formatter:off
Query q = getSession().createQuery("select new org.zanata.model.StatusCount(tft.state, count(tft)) " +
"from HTextFlowTarget tft " +
"where tft.textFlow.document.projectIteration.id = :id " +
" and tft.locale.localeId = :locale" +
" and tft.textFlow.obsolete = false" +
" and tft.textFlow.document.obsolete = false" +
" group by tft.state");
// @formatter:on
q.setParameter("id", iterationId)
.setParameter("locale", localeId);
q.setCacheable(true).setComment("ProjectIterationDAO.getStatisticsForContainer");
@SuppressWarnings("unchecked")
List<StatusCount> stats = q.list();
TransUnitCount stat = new TransUnitCount();
for (StatusCount count : stats)
{
stat.set(count.status, count.count.intValue());
}
Long totalCount = getTotalWordCountForIteration(iterationId);
stat.set(ContentState.New, totalCount.intValue() - (stat.getApproved() + stat.getNeedReview()));
return stat;
}
/**
* @see DocumentDAO#getStatistics(long, LocaleId)
* @param iterationId
* @param localeId
* @return
*/
public TransUnitWords getWordStatsForContainer(Long iterationId, LocaleId localeId)
{
// @formatter:off
Query q = getSession().createQuery("select new org.zanata.model.StatusCount(tft.state, sum(tft.textFlow.wordCount)) " +
"from HTextFlowTarget tft " +
"where tft.textFlow.document.projectIteration.id = :id " +
" and tft.locale.localeId = :locale" +
" and tft.textFlow.obsolete = false" +
" and tft.textFlow.document.obsolete = false" +
" group by tft.state");
// @formatter:on
q.setParameter("id", iterationId)
.setParameter("locale", localeId);
q.setCacheable(true).setComment("ProjectIterationDAO.getWordStatsForContainer");
@SuppressWarnings("unchecked")
List<StatusCount> stats = q.list();
TransUnitWords stat = new TransUnitWords();
for (StatusCount count : stats)
{
stat.set(count.status, count.count.intValue());
}
Long totalCount = getTotalWordCountForIteration(iterationId);
stat.set(ContentState.New, totalCount.intValue() - (stat.getApproved() + stat.getNeedReview()));
return stat;
}
public EntityTag getResourcesETag(HProjectIteration projectIteration)
{
// @formatter:off
Query q = getSession().createQuery(
"select d.revision from HDocument d " +
"where d.projectIteration =:iteration " +
"and d.obsolete = false")
.setParameter("iteration", projectIteration);
// @formatter:on
q.setCacheable(true).setComment("ProjectIterationDAO.getResourcesETag");
@SuppressWarnings("unchecked")
List<Integer> revisions = q.list();
int hashCode = 1;
for (int revision : revisions)
{
hashCode = 31 * hashCode + revision;
}
String hash = HashUtil.generateHash(String.valueOf(hashCode));
return EntityTag.valueOf(hash);
}
/**
* @param iterationId
* @return
*/
public Map<String, TransUnitWords> getAllWordStatsStatistics(Long iterationId)
{
// @formatter:off
Query q = getSession().createQuery("select tft.state, sum(tft.textFlow.wordCount), tft.locale.localeId " +
"from HTextFlowTarget tft where tft.textFlow.document.projectIteration.id = :id and tft.textFlow.obsolete = false" +
" and tft.textFlow.document.obsolete = false group by tft.state, tft.locale.localeId");
// @formatter:on
q.setParameter("id", iterationId);
q.setCacheable(true).setComment("ProjectIterationDAO.getAllWordStatsStatistics");
@SuppressWarnings("unchecked")
List<Object[]> stats = q.list();
Map<String, TransUnitWords> result = new HashMap<String, TransUnitWords>();
for (Object[] count : stats)
{
TransUnitWords stat;
ContentState state = (ContentState) count[0];
Long word = (Long) count[1];
LocaleId locale = (LocaleId) count[2];
if (!result.containsKey(locale.getId()))
{
stat = new TransUnitWords();
result.put(locale.getId(), stat);
}
else
{
stat = result.get(locale.getId());
}
stat.set(state, word.intValue());
}
Long totalCount = getTotalWordCountForIteration(iterationId);
for (TransUnitWords count : result.values())
{
count.set(ContentState.New, totalCount.intValue() - (count.getApproved() + count.getNeedReview()));
}
return result;
}
/**
* Retreives translation unit level statistics for a project iteration.
*
* @param iterationId The numeric id for a Project iteration.
* @return A map of translation unit counts indexed by a locale id string.
*/
public Map<String, TransUnitCount> getAllStatisticsForContainer(Long iterationId)
{
// @formatter:off
Query q = getSession().createQuery(
"select new map(tft.state as state, count(tft) as count, tft.locale.localeId as locale) " +
"from HTextFlowTarget tft " +
"where tft.textFlow.document.projectIteration.id = :id " +
" and tft.textFlow.obsolete = false" +
" and tft.textFlow.document.obsolete = false" +
" group by tft.state, tft.locale");
// @formatter:on
q.setParameter("id", iterationId);
q.setComment("ProjectIterationDAO.getAllStatisticsForContainer");
@SuppressWarnings("unchecked")
List<Map> stats = q.list();
Map<String, TransUnitCount> retVal = new HashMap<String, TransUnitCount>();
for( Map row : stats )
{
ContentState state = (ContentState)row.get("state");
Long count = (Long)row.get("count");
LocaleId localeId = (LocaleId)row.get("locale");
TransUnitCount transUnitCount = retVal.get( localeId.getId() );
if( transUnitCount == null )
{
transUnitCount = new TransUnitCount();
retVal.put(localeId.getId(), transUnitCount);
}
transUnitCount.set( state, count.intValue() );
}
for( TransUnitCount stat : retVal.values() )
{
Long totalCount = getTotalCountForIteration(iterationId);
stat.set(ContentState.New, totalCount.intValue() - (stat.getApproved() + stat.getNeedReview()));
}
return retVal;
}
public Long getTotalCountForIteration(Long iterationId)
{
// @formatter:off
Query q = getSession().createQuery(
"select count(tf) from HTextFlow tf " +
"where tf.document.projectIteration.id = :id" +
" and tf.obsolete = false" +
" and tf.document.obsolete = false");
// @formatter:on
q.setParameter("id", iterationId);
q.setCacheable(true).setComment("ProjectIterationDAO.getTotalCountForIteration");
Long totalCount = (Long) q.uniqueResult();
if (totalCount == null)
{
totalCount = 0L;
}
return totalCount;
}
public Long getTotalWordCountForIteration(Long iterationId)
{
// @formatter:off
Query q = getSession().createQuery(
"select sum(tf.wordCount) from HTextFlow tf " +
"where tf.document.projectIteration.id = :id" +
" and tf.obsolete = false" +
" and tf.document.obsolete = false");
// @formatter:on
q.setParameter("id", iterationId);
q.setCacheable(true).setComment("ProjectIterationDAO.getTotalWordCountForIteration");
Long totalCount = (Long) q.uniqueResult();
if (totalCount == null)
{
totalCount = 0L;
}
return totalCount;
}
public int getTotalProjectIterCount()
{
String query = "select count(*) from HProjectIteration";
Query q = getSession().createQuery(query.toString());
q.setCacheable(true).setComment("ProjectIterationDAO.getTotalProjectIterCount");
Long totalCount = (Long) q.uniqueResult();
if (totalCount == null)
return 0;
return totalCount.intValue();
}
public int getTotalActiveProjectIterCount()
{
Query q = getSession().createQuery("select count(*) from HProjectIteration t where t.status = :status");
q.setParameter("status", EntityStatus.ACTIVE);
q.setCacheable(true).setComment("ProjectIterationDAO.getTotalActiveProjectIterCount");
Long totalCount = (Long) q.uniqueResult();
if (totalCount == null)
return 0;
return totalCount.intValue();
}
public int getTotalReadOnlyProjectIterCount()
{
Query q = getSession().createQuery("select count(*) from HProjectIteration t where t.status = :status");
q.setParameter("status", EntityStatus.READONLY);
q.setCacheable(true).setComment("ProjectIterationDAO.getTotalReadOnlyProjectIterCount");
Long totalCount = (Long) q.uniqueResult();
if (totalCount == null)
return 0;
return totalCount.intValue();
}
public int getTotalObsoleteProjectIterCount()
{
Query q = getSession().createQuery("select count(*) from HProjectIteration t where t.status = :status");
q.setParameter("status", EntityStatus.OBSOLETE);
q.setCacheable(true).setComment("ProjectIterationDAO.getTotalObsoleteProjectIterCount");
Long totalCount = (Long) q.uniqueResult();
if (totalCount == null)
return 0;
return totalCount.intValue();
}
public List<HProjectIteration> searchLikeSlugOrProjectSlug(String searchTerm)
{
Query q = getSession().createQuery("from HProjectIteration t where lower(t.slug) LIKE :searchTerm OR lower(t.project.slug) LIKE :searchTerm OR lower(t.project.name) LIKE :searchTerm");
q.setParameter("searchTerm", "%" + searchTerm.toLowerCase() + "%");
q.setCacheable(false).setComment("ProjectIterationDAO.searchLikeSlugOrProjectSlug");
return q.list();
// final String FIELDS[] = { "slug", GroupSearchBridge.PROJECT_FIELD };
//
// MultiFieldQueryParser parser = new
// MultiFieldQueryParser(Version.LUCENE_29, FIELDS, new
// StandardAnalyzer(Version.LUCENE_29));
// org.apache.lucene.search.Query textQuery = parser.parse(searchTerm);
//
// org.hibernate.search.jpa.FullTextQuery ftQuery =
// entityManager.createFullTextQuery(textQuery, HProjectIteration.class);
// @SuppressWarnings("unchecked")
// List<HProjectIteration> matches = (List<HProjectIteration>)
// ftQuery.getResultList();
// return matches;
}
}