This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
TextFlowTargetDAO.java
549 lines (511 loc) · 23.3 KB
/
TextFlowTargetDAO.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
package org.zanata.dao;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import com.google.common.collect.Lists;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
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.model.HDocument;
import org.zanata.model.HLocale;
import org.zanata.model.HProject;
import org.zanata.model.HProjectIteration;
import org.zanata.model.HTextFlow;
import org.zanata.model.HTextFlowTarget;
import org.zanata.service.TranslationFinder;
import com.google.common.base.Optional;
@Name("textFlowTargetDAO")
@AutoCreate
@Scope(ScopeType.STATELESS)
public class TextFlowTargetDAO extends AbstractDAOImpl<HTextFlowTarget, Long>
implements TranslationFinder {
public TextFlowTargetDAO() {
super(HTextFlowTarget.class);
}
public TextFlowTargetDAO(Session session) {
super(HTextFlowTarget.class, session);
}
/**
* @param textFlow
* @param locale
* @return
*/
public HTextFlowTarget getByNaturalId(HTextFlow textFlow, HLocale locale) {
return (HTextFlowTarget) getSession()
.byNaturalId(HTextFlowTarget.class).using("textFlow", textFlow)
.using("locale", locale).load();
}
public List<HTextFlowTarget> getByTextFlowId(Long tfId, int offset,
int maxResults) {
Query q =
getSession()
.createQuery(
"from HTextFlowTarget tft where tft.textFlow.obsolete=0 and tft.textFlow.id = :tfId");
q.setParameter("tfId", tfId);
q.setFirstResult(offset);
q.setMaxResults(maxResults);
q.setCacheable(true)
.setComment("TextFlowTargetDAO.getByTextFlowId");
return q.list();
}
public int countTextFlowTargetsInTextFlow(Long tfId) {
Query q =
getSession()
.createQuery(
"select count(*) from HTextFlowTarget tft where tft.textFlow.obsolete=0 and tft.textFlow.id = :tfId");
q.setParameter("tfId", tfId);
q.setCacheable(true).setComment(
"TextFlowTargetDAO.countTextFlowTargetsInTextFlow");
Long totalCount = (Long) q.uniqueResult();
return totalCount == null ? 0 : totalCount.intValue();
}
public int getTotalTextFlowTargets() {
Query q =
getSession()
.createQuery("select count(*) from HTextFlowTarget");
q.setCacheable(true);
q.setComment("TextFlowTargetDAO.getTotalTextFlowTargets");
Long totalCount = (Long) q.uniqueResult();
if (totalCount == null)
return 0;
return totalCount.intValue();
}
public int getTotalActiveTextFlowTargets() {
Query q =
getSession()
.createQuery(
"select count(*) from HTextFlowTarget t where t.textFlow.obsolete=0");
q.setCacheable(true);
q.setComment("TextFlowTargetDAO.getTotalActiveTextFlowTargets");
Long totalCount = (Long) q.uniqueResult();
if (totalCount == null)
return 0;
return totalCount.intValue();
}
public int getTotalObsoleteTextFlowTargets() {
Query q =
getSession()
.createQuery(
"select count(*) from HTextFlowTarget t where t.textFlow.obsolete=1");
q.setCacheable(true);
q.setComment("TextFlowTargetDAO.getTotalObsoleteTextFlowTargets");
Long totalCount = (Long) q.uniqueResult();
if (totalCount == null)
return 0;
return totalCount.intValue();
}
public int getTotalApprovedOrTranslatedTextFlowTargets() {
Query q =
getSession()
.createQuery(
"select count(*) from HTextFlowTarget t where t.state = :state or t.state = :state2 and t.textFlow.obsolete=0");
q.setCacheable(true);
q.setComment("TextFlowTargetDAO.getTotalApprovedOrTranslatedTextFlowTargets");
Long totalCount =
(Long) q.setParameter("state", ContentState.Approved)
.setParameter("state2", ContentState.Translated)
.uniqueResult();
if (totalCount == null)
return 0;
return totalCount.intValue();
}
public int getTotalRejectedOrFuzzyTextFlowTargets() {
Query q =
getSession()
.createQuery(
"select count(*) from HTextFlowTarget t where t.state = :state or t.state = :state2 and t.textFlow.obsolete=0");
q.setCacheable(true);
q.setComment("TextFlowTargetDAO.getTotalRejectedOrFuzzyTextFlowTargets");
Long totalCount =
(Long) q.setParameter("state", ContentState.NeedReview)
.setParameter("state2", ContentState.Rejected)
.uniqueResult();
if (totalCount == null)
return 0;
return totalCount.intValue();
}
public int getTotalNewTextFlowTargets() {
Query q =
getSession()
.createQuery(
"select count(*) from HTextFlowTarget t where t.state = :state and t.textFlow.obsolete=0");
q.setCacheable(true);
q.setComment("TextFlowTargetDAO.getTotalNewTextFlowTargets");
Long totalCount =
(Long) q.setParameter("state", ContentState.New).uniqueResult();
if (totalCount == null)
return 0;
return totalCount.intValue();
}
/**
* Finds all (including obsolete) translations for 'document' in 'locale'.
*
* @param document
* @param localeId
* @return
*/
@SuppressWarnings("unchecked")
public List<HTextFlowTarget> findAllTranslations(HDocument document,
LocaleId localeId) {
Query q =
getSession().createQuery(
"select t from HTextFlowTarget t where "
+ "t.textFlow.document =:document "
+ "and t.locale.localeId =:localeId "
+ "order by t.textFlow.pos");
q.setParameter("document", document);
q.setParameter("localeId", localeId);
q.setCacheable(false);
q.setComment("TextFlowTargetDAO.findAllTranslations");
return q.list();
}
/**
* Finds non-obsolete translations for 'document' in 'locale'.
*
* @param document
* @param locale
* @return
*/
@SuppressWarnings("unchecked")
public List<HTextFlowTarget> findTranslations(HDocument document,
HLocale locale) {
Query q =
getSession().createQuery(
"select t " + "from HTextFlowTarget t where "
+ "t.textFlow.document =:document "
+ "and t.locale =:locale "
+ "and t.textFlow.obsolete=false "
+ "order by t.textFlow.pos");
q.setParameter("document", document);
q.setParameter("locale", locale);
q.setCacheable(true);
q.setComment("TextFlowTargetDAO.findTranslations");
return q.list();
}
/**
* Finds the best matching translations for a single text flow and a locale.
* Other parameters (context, document id, and project) can also influence
* what the best match is. A matching translation is one where the source
* text is exactly the same as the translation within the document and in
* the same locale.
*
* @param textFlow
* The text flow for which to find a matching translation.
* @param targetLocaleId
* The locale in which to find matches.
* @param checkContext
* Whether to check for a matching context
* @param checkDocument
* Whether to check for a matching document id
* @param checkProject
* Whether to check for a matching project
* @return The single best possible match found in the system for the given
* text flow.
*/
@Override
@NativeQuery
public Optional<HTextFlowTarget> searchBestMatchTransMemory(
HTextFlow textFlow, LocaleId targetLocaleId,
LocaleId sourceLocaleId, boolean checkContext,
boolean checkDocument, boolean checkProject) {
StringBuilder queryBuilder = new StringBuilder();
queryBuilder
.append("SELECT tft.* FROM HTextFlowTarget tft ")
.append("JOIN HTextFlow tf ON tf.id = tft.tf_id ")
.append("JOIN HLocale locale ON locale.id = tft.locale ")
.append("JOIN HDocument hDoc ON hDoc.id = tf.document_id ")
.append("JOIN HProjectIteration iter ON iter.id = hDoc.project_iteration_id ")
.append("JOIN HProject project ON project.id = iter.project_id ")
.append("WHERE tf.contentHash = :contentHash AND locale.localeId = :localeId ")
.append("AND tft.tf_id = tf.id ")
.append("AND tft.locale = locale.id ")
.append("AND tf.document_id = hDoc.id ")
.append("AND hDoc.project_iteration_id = iter.id ")
.append("AND iter.project_id = project.id ")
.append("AND tft.state in (2, 3) AND tft.tf_id <> :textFlowId AND iter.status <> 'O' AND project.status <> 'O' ");
if (checkContext) {
queryBuilder.append("AND tf.resId = :resId ");
}
if (checkDocument) {
queryBuilder.append("AND hDoc.docId = :docId ");
}
if (checkProject) {
queryBuilder.append("AND project.id = :projectId ");
}
queryBuilder
.append("ORDER BY ")
.append(" CASE WHEN tf.resId = :resId THEN 0 ELSE 1 END, ")
.append(" CASE WHEN hDoc.docId = :docId THEN 0 ELSE 1 END, ")
.append(" CASE WHEN iter.project_id = :projectId THEN 0 ELSE 1 END, ")
.append(" tft.lastChanged DESC ")
.append("LIMIT 1");
SQLQuery sqlQuery =
getSession().createSQLQuery(queryBuilder.toString());
sqlQuery.setParameter("textFlowId", textFlow.getId());
sqlQuery.setParameter("contentHash", textFlow.getContentHash());
sqlQuery.setParameter("resId", textFlow.getResId());
sqlQuery.setParameter("docId", textFlow.getDocument().getDocId());
sqlQuery.setParameter("projectId", textFlow.getDocument()
.getProjectIteration().getProject().getId());
sqlQuery.setParameter("localeId", targetLocaleId.getId());
sqlQuery.addEntity(HTextFlowTarget.class);
return Optional.fromNullable((HTextFlowTarget) sqlQuery.uniqueResult());
}
/**
* Look up the {@link HTextFlowTarget} for the given hLocale in hTextFlow,
* creating a new one if none is present.
*
* @param hTextFlow
* The parent text flow.
* @param hLocale
* The locale for the text flow target.
*/
public HTextFlowTarget getOrCreateTarget(HTextFlow hTextFlow,
HLocale hLocale) {
HTextFlowTarget hTextFlowTarget = getTextFlowTarget(hTextFlow, hLocale);
if (hTextFlowTarget == null) {
hTextFlowTarget = new HTextFlowTarget(hTextFlow, hLocale);
hTextFlowTarget.setVersionNum(0); // this will be incremented when
// content is set (below)
// TODO getTargets just to make sure hTextFlowTarget is persisted in
// the end
hTextFlow.getTargets().put(hLocale.getId(), hTextFlowTarget);
// getSession().persist(hTextFlowTarget);
}
return hTextFlowTarget;
}
/**
* Look up the {@link HTextFlowTarget} for the given hLocale in hTextFlow.
* If none can be found, return null.
*
* @param hTextFlow
* The parent text flow.
* @param hLocale
* The locale for the text flow target.
*/
public HTextFlowTarget getTextFlowTarget(HTextFlow hTextFlow,
HLocale hLocale) {
HTextFlowTarget hTextFlowTarget =
(HTextFlowTarget) getSession()
.createQuery(
"select tft from HTextFlowTarget tft "
+ "where tft.textFlow = :textFlow "
+ "and tft.locale = :locale")
.setParameter("textFlow", hTextFlow)
.setParameter("locale", hLocale)
.setComment("TextFlowTargetDAO.getTextFlowTarget")
.uniqueResult();
return hTextFlowTarget;
}
public HTextFlowTarget
getTextFlowTarget(Long hTextFlowId, LocaleId localeId) {
HTextFlowTarget hTextFlowTarget =
(HTextFlowTarget) getSession()
.createQuery(
"select tft from HTextFlowTarget tft "
+ "where tft.textFlow.id = :hTextFlowId "
+ "and tft.locale.localeId = :localeId")
.setParameter("hTextFlowId", hTextFlowId)
.setParameter("localeId", localeId)
.setComment("TextFlowTargetDAO.getTextFlowTarget")
.uniqueResult();
return hTextFlowTarget;
}
@SuppressWarnings("unchecked")
public List<HTextFlowTarget> findByTextFlowIdList(List<Long> idList,
LocaleId localeId) {
if (idList == null || idList.isEmpty()) {
return Collections.EMPTY_LIST;
}
Query query =
getSession()
.createQuery(
"select tft from HTextFlowTarget tft "
+ "where tft.textFlow.id in (:idList) "
+ "and tft.locale.localeId = :localeId");
query.setParameterList("idList", idList);
query.setParameter("localeId", localeId);
query.setCacheable(false).setComment(
"TextFlowTargetDAO.findByTextFlowIdList");
return query.list();
}
public HTextFlowTarget getTextFlowTarget(HTextFlow hTextFlow,
LocaleId localeId) {
HTextFlowTarget hTextFlowTarget =
(HTextFlowTarget) getSession()
.createQuery(
"select tft from HTextFlowTarget tft "
+ "where tft.textFlow = :textFlow "
+ "and tft.locale.localeId = :localeId")
.setParameter("textFlow", hTextFlow)
.setParameter("localeId", localeId)
.setComment("TextFlowTargetDAO.getTextFlowTarget")
.uniqueResult();
return hTextFlowTarget;
}
public Long getTextFlowTargetId(HTextFlow hTextFlow, LocaleId localeId) {
Query q =
getSession().createQuery(
"select tft.id from HTextFlowTarget tft "
+ "where tft.textFlow = :textFlow "
+ "and tft.locale.localeId = :localeId");
q.setParameter("textFlow", hTextFlow);
q.setParameter("localeId", localeId);
q.setComment("TextFlowTargetDAO.getTextFlowTargetId");
Long id = (Long) q.uniqueResult();
return id;
}
public HTextFlowTarget getLastTranslated(String projectSlug,
String iterationSlug, LocaleId localeId) {
StringBuilder query = new StringBuilder();
query.append("from HTextFlowTarget tft ");
query.append("where tft.textFlow.document.projectIteration.slug = :iterationSlug ");
query.append("and tft.textFlow.document.projectIteration.project.slug = :projectSlug ");
query.append("and tft.locale.localeId = :localeId ");
query.append("order by tft.lastChanged DESC");
Query q = getSession().createQuery(query.toString());
q.setParameter("iterationSlug", iterationSlug);
q.setParameter("projectSlug", projectSlug);
q.setParameter("localeId", localeId);
q.setCacheable(true);
q.setMaxResults(1);
q.setComment("TextFlowTargetDAO.getLastTranslated");
return (HTextFlowTarget) q.uniqueResult();
}
/**
* @param document
* copyTrans copy target document
* @param targetLocale
* target locale
* @return number of translation candidates in given locale and is from
* given docId (exclude translations from target document's
* iteration)
*/
public long getTranslationCandidateCountWithDocIdAndLocale(
HDocument document,
HLocale targetLocale) {
String queryString =
"select count(*) from HTextFlowTarget tft " +
"where tft.textFlow.document.docId = :docId " +
"and tft.locale = :locale " +
"and tft.textFlow.obsolete = false " +
"and tft.textFlow.document.obsolete = false " +
"and tft.textFlow.document.projectIteration <> :self";
Query query =
getSession()
.createQuery(queryString)
.setParameter("docId", document.getDocId())
.setParameter("locale", targetLocale)
.setParameter("self", document.getProjectIteration())
.setCacheable(true)
.setComment(
"TextFlowTargetDAO.getTranslationCandidateCountWithDocIdAndLocale");
return (Long) query.uniqueResult();
}
/**
* @param document
* copyTrans copy target document
* @param targetLocale
* target locale
* @return number of translation candidates in given locale and is from
* given project (exclude translations from target document's
* iteration)
*/
public long getTranslationCandidateCountWithProjectAndLocale(
HDocument document, HLocale targetLocale) {
HProjectIteration projectIteration = document.getProjectIteration();
HProject project = projectIteration.getProject();
String queryString =
"select count(*) from HTextFlowTarget tft " +
"where tft.textFlow.document.projectIteration.project = :project " +
"and tft.locale = :locale " +
"and tft.textFlow.obsolete = false " +
"and tft.textFlow.document.obsolete = false " +
"and tft.textFlow.document.projectIteration.status <> :obsoleteStatus " +
"and tft.textFlow.document.projectIteration <> :self";
Query query =
getSession()
.createQuery(queryString)
.setParameter("project", project)
.setParameter("locale", targetLocale)
.setParameter("self", projectIteration)
.setParameter("obsoleteStatus", EntityStatus.OBSOLETE)
.setCacheable(true)
.setComment(
"TextFlowTargetDAO.getTranslationCandidateCountWithProjectAndLocale");
return (Long) query.uniqueResult();
}
/**
* Return translated/approved translations from HProjectIteration#id=sourceVersionId and
* HProjectIteration#id=targetVersionId which has matching source content
* hash and document id.
*
* @param sourceVersionId
* @param targetVersionId
* @param offset
* @param maxResults
* @return
*/
public List<HTextFlowTarget[]> getTranslationsByMatchedContext(Long sourceVersionId,
Long targetVersionId, int offset, int maxResults, Collection<ContentState> states) {
StringBuilder queryBuilder = new StringBuilder();
queryBuilder
.append("select tft, tft2 from HTextFlowTarget tft, HTextFlowTarget tft2 ")
.append("where tft.textFlow.document.projectIteration.id = :sourceVersionId ")
.append("and tft2.textFlow.document.projectIteration.id = :targetVersionId ")
.append("and tft.textFlow.obsolete = false ")
.append("and tft.textFlow.document.obsolete = false ")
.append("and tft.state in :states ")
.append("and tft2.textFlow.obsolete = false ")
.append("and tft2.textFlow.document.obsolete = false ")
.append("and tft.textFlow.contentHash = tft2.textFlow.contentHash ")
.append("and tft.textFlow.document.docId = tft2.textFlow.document.docId");
Query query = getSession()
.createQuery(queryBuilder.toString())
.setParameter("sourceVersionId", sourceVersionId)
.setParameter("targetVersionId", targetVersionId)
.setParameterList("states", states)
.setMaxResults(maxResults)
.setFirstResult(offset)
.setCacheable(true)
.setComment("TextFlowTargetDAO.getTranslationsByMatchedContext");
List<HTextFlowTarget[]> results = Lists.newArrayList();
for(Object result: query.list()) {
Object[] castResults = (Object[])result;
results.add(new HTextFlowTarget[] { (HTextFlowTarget) castResults[0],
(HTextFlowTarget) castResults[1] });
}
return results;
}
public int getTranslationsByMatchedContextCount(Long sourceVersionId,
Long targetVersionId, Collection<ContentState> states) {
StringBuilder queryBuilder = new StringBuilder();
queryBuilder
.append("select count(tft.id) from HTextFlowTarget tft, HTextFlowTarget tft2 ")
.append("where tft.textFlow.document.projectIteration.id = :sourceVersionId ")
.append("and tft2.textFlow.document.projectIteration.id = :targetVersionId ")
.append("and tft.textFlow.obsolete = false ")
.append("and tft.textFlow.document.obsolete = false ")
.append("and tft.state in :states ")
.append("and tft2.textFlow.obsolete = false ")
.append("and tft2.textFlow.document.obsolete = false ")
.append("and tft.textFlow.contentHash = tft2.textFlow.contentHash ")
.append("and tft.textFlow.document.docId = tft2.textFlow.document.docId");
Query query = getSession()
.createQuery(queryBuilder.toString())
.setParameter("sourceVersionId", sourceVersionId)
.setParameter("targetVersionId", targetVersionId)
.setParameterList("states", states)
.setCacheable(true)
.setComment("TextFlowTargetDAO.getTranslationsByMatchedContextCount");
Long count = (Long) query.uniqueResult();
return count == null ? 0 : count.intValue();
}
}