This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
TextFlowTargetDAO.java
355 lines (327 loc) · 14.9 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
package org.zanata.dao;
import java.util.List;
import com.google.common.base.Optional;
import org.hibernate.Query;
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.HTextFlow;
import org.zanata.model.HTextFlowTarget;
@Name("textFlowTargetDAO")
@AutoCreate
@Scope(ScopeType.STATELESS)
public class TextFlowTargetDAO extends AbstractDAOImpl<HTextFlowTarget, Long> {
public TextFlowTargetDAO() {
super(HTextFlowTarget.class);
}
public TextFlowTargetDAO(Session session) {
super(HTextFlowTarget.class, session);
}
/**
* @param textFlow
* @param localeId
* @return
*/
public HTextFlowTarget getByNaturalId(HTextFlow textFlow, HLocale locale) {
return (HTextFlowTarget) getSession()
.byNaturalId(HTextFlowTarget.class).using("textFlow", textFlow)
.using("locale", locale).load();
}
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 getTotalTranslatedTextFlowTargets() {
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.getTotalTranslatedTextFlowTargets");
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 locale
* @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 locale 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.
*/
public Optional<HTextFlowTarget> findBestMatchingTranslation(
HTextFlow textFlow, HLocale locale, boolean checkContext,
boolean checkDocument, boolean checkProject) {
StringBuilder queryStr =
new StringBuilder(
"select match "
+ "from HTextFlowTarget match, HTextFlow textFlow "
+ "where "
+ "textFlow.id = :textFlowId "
+ "and textFlow.contentHash = match.textFlow.contentHash "
+ "and match.locale = :locale "
+
// It's fine to reuse translation in Translated
// state even if it came from a reviewable
// project
"and match.state in (:approvedState, :translatedState) "
+
// Do not reuse its own translation
"and match.textFlow != textFlow "
+
// Do not reuse matches from obsolete entities
// (iteration, project)
// Obsolete document translations ARE reused
"and match.textFlow.document.projectIteration.status != :obsoleteEntityStatus "
+ "and match.textFlow.document.projectIteration.project.status != :obsoleteEntityStatus ");
if (checkContext) {
queryStr.append("and match.textFlow.resId = textFlow.resId ");
}
if (checkDocument) {
queryStr.append("and match.textFlow.document.docId = textFlow.document.docId ");
}
if (checkProject) {
queryStr.append("and match.textFlow.document.projectIteration.project = textFlow.document.projectIteration.project ");
}
queryStr.append("order by (case when match.textFlow.resId = textFlow.resId then 0 else 1 end),"
+ "(case when match.textFlow.document.docId = textFlow.document.docId then 0 else 1 end),"
+ "(case when match.textFlow.document.projectIteration.project = textFlow.document.projectIteration.project then 0 else 1 end),"
+ "match.lastChanged desc");
Query q = getSession().createQuery(queryStr.toString());
q.setParameter("textFlowId", textFlow.getId())
.setParameter("locale", locale)
.setParameter("approvedState", ContentState.Approved)
.setParameter("translatedState", ContentState.Translated)
.setParameter("obsoleteEntityStatus", EntityStatus.OBSOLETE);
q.setCacheable(false); // don't try to cache scrollable results
q.setComment("TextFlowTargetDAO.findMatchingTranslations");
q.setMaxResults(1); // Get the first one (should be the best match
// because of the order by clause)
List results = q.list();
return results.isEmpty() ? Optional.<HTextFlowTarget> absent()
: Optional.of((HTextFlowTarget) results.get(0));
}
/**
* 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)
hTextFlow.getTargets().put(hLocale.getId(), 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;
}
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();
}
}