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
/
HTextFlow.java
496 lines (418 loc) · 13.7 KB
/
HTextFlow.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
/*
* 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.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MapKey;
import javax.persistence.MapKeyColumn;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PostLoad;
import javax.persistence.PostPersist;
import javax.persistence.PostUpdate;
import javax.persistence.PreUpdate;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.annotations.AccessType;
import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.NaturalId;
import org.hibernate.search.annotations.Analyze;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.FieldBridge;
import org.hibernate.validator.constraints.NotEmpty;
import org.zanata.common.HasContents;
import org.zanata.common.LocaleId;
import org.zanata.hibernate.search.ContainingWorkspaceBridge;
import org.zanata.model.po.HPotEntryData;
import org.zanata.util.HashUtil;
import org.zanata.util.OkapiUtil;
import org.zanata.util.StringUtil;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Objects;
import com.google.common.collect.ImmutableList;
/**
* Represents a flow of source text that should be processed as a stand-alone
* structural unit.
*
* @see org.zanata.rest.dto.resource.TextFlow
* @author Asgeir Frimannsson <asgeirf@redhat.com>
*
*/
@Entity
@EntityListeners({ HTextFlow.EntityListener.class })
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Setter
@NoArgsConstructor
@ToString(of = { "resId", "revision", "comment", "obsolete" })
@Slf4j
@NamedQueries({
@NamedQuery(
name = HTextFlow.QUERY_GET_BY_DOC_AND_RES_ID_BATCH,
query = "from HTextFlow tf left join fetch tf.targets tft left join fetch tft.history where tf.document = :document and tf.resId in (:resIds)")
})
public class HTextFlow extends HTextContainer implements Serializable,
ITextFlowHistory, HasSimpleComment, HasContents, ITextFlow {
public static final String QUERY_GET_BY_DOC_AND_RES_ID_BATCH = "HTextFlow.getByDocumentAndResIdBatch";
private static final long serialVersionUID = 3023080107971905435L;
private Long id;
private Integer revision = 1;
private String resId;
@Setter(AccessLevel.PROTECTED)
private Integer pos;
private HDocument document;
private boolean obsolete = false;
private Map<Long, HTextFlowTarget> targets;
private Map<Integer, HTextFlowHistory> history;
private HSimpleComment comment;
private HPotEntryData potEntryData;
private Long wordCount;
private String contentHash;
private boolean plural;
private String content0;
private String content1;
private String content2;
private String content3;
private String content4;
private String content5;
// Only for internal use (persistence transient)
@Setter(AccessLevel.PRIVATE)
private Integer oldRevision;
// Only for internal use (persistence transient)
@Setter(AccessLevel.PRIVATE)
private HTextFlowHistory initialState;
public HTextFlow(HDocument document, String resId, String content) {
setDocument(document);
setResId(resId);
setContents(content);
}
@Id
@GeneratedValue
public Long getId() {
return id;
}
@VisibleForTesting
public void setId(Long id) {
this.id = id;
}
@Transient
@Override
public LocaleId getLocale() {
return getDocument().getSourceLocaleId();
}
// we can't use @NotNull because the position isn't set until the object has
// been persisted
@Column(insertable = false, updatable = false, nullable = false)
// @Column(insertable=false, updatable=false)
@Override
public
Integer getPos() {
return pos;
}
@Transient
@Override
public String getQualifiedId() {
HDocument doc = getDocument();
HProjectIteration iter = doc.getProjectIteration();
HProject proj = iter.getProject();
return proj.getSlug() + ":" + iter.getSlug() + ":" + doc.getDocId()
+ ":" + getResId();
}
// TODO make this case sensitive
// TODO PERF @NaturalId(mutable=false) for better criteria caching
@NaturalId
@Size(max = 255)
@NotEmpty
@Field(analyze = Analyze.NO)
public String getResId() {
return resId;
}
/**
* @return whether this message supports plurals
*/
public boolean isPlural() {
return plural;
}
@NotNull
@Override
public Integer getRevision() {
return revision;
}
@Override
public boolean isObsolete() {
return obsolete;
}
/**
* Caller must ensure that textFlow is in document.textFlows if and only if
* obsolete = false
*
* @param obsolete
*/
public void setObsolete(boolean obsolete) {
this.obsolete = obsolete;
}
@ManyToOne
@JoinColumn(name = "document_id", insertable = false, updatable = false,
nullable = false)
// TODO PERF @NaturalId(mutable=false) for better criteria caching
@NaturalId
@AccessType("field")
@Field(analyze = Analyze.NO)
@FieldBridge(impl = ContainingWorkspaceBridge.class)
public
HDocument getDocument() {
return document;
}
public void setDocument(HDocument document) {
if (!Objects.equal(this.document, document)) {
this.document = document;
updateWordCount();
}
}
// TODO use orphanRemoval=true: requires JPA 2.0
@OneToOne(optional = true, fetch = FetchType.LAZY,
cascade = CascadeType.ALL)
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
@JoinColumn(name = "comment_id")
public HSimpleComment getComment() {
return comment;
}
@Override
@NotEmpty
@Transient
public List<String> getContents() {
List<String> contents = new ArrayList<String>();
boolean populating = false;
for (int i = MAX_PLURALS - 1; i >= 0; i--) {
String c = this.getContent(i);
if (c != null) {
populating = true;
}
if (populating) {
contents.add(0, c);
}
}
return contents;
}
public void setContents(List<String> newContents) {
if (!newContents.equals(this.getContents())) {
for (int i = 0; i < MAX_PLURALS; i++) {
String value =
i < newContents.size() ? newContents.get(i) : null;
this.setContent(i, value);
}
updateContentHash();
updateWordCount();
}
}
private String getContent(int idx) {
switch (idx) {
case 0:
return content0;
case 1:
return content1;
case 2:
return content2;
case 3:
return content3;
case 4:
return content4;
case 5:
return content5;
default:
throw new RuntimeException("Invalid Content index: " + idx);
}
}
private void setContent(int idx, String content) {
switch (idx) {
case 0:
content0 = content;
break;
case 1:
content1 = content;
break;
case 2:
content2 = content;
break;
case 3:
content3 = content;
break;
case 4:
content4 = content;
break;
case 5:
content5 = content;
break;
default:
throw new RuntimeException("Invalid Content index: " + idx);
}
}
protected String getContent0() {
return content0;
}
protected String getContent1() {
return content1;
}
protected String getContent2() {
return content2;
}
protected String getContent3() {
return content3;
}
protected String getContent4() {
return content4;
}
protected String getContent5() {
return content5;
}
@OneToMany(cascade = { CascadeType.REMOVE, CascadeType.MERGE,
CascadeType.PERSIST }, mappedBy = "textFlow")
@MapKey(name = "revision")
public Map<Integer, HTextFlowHistory> getHistory() {
if (this.history == null) {
this.history = new HashMap<Integer, HTextFlowHistory>();
}
return history;
}
@OneToMany(cascade = CascadeType.ALL, mappedBy = "textFlow")
@MapKeyColumn(name = "locale")
@BatchSize(size = 10)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public Map<Long, HTextFlowTarget> getTargets() {
if (targets == null) {
targets = new HashMap<Long, HTextFlowTarget>();
}
return targets;
}
@Override
public ITextFlowTarget getTargetContents(LocaleId localeId) {
// TODO performance: need efficient way to look up a target by LocaleId
Collection<HTextFlowTarget> targets = getTargets().values();
for (HTextFlowTarget tft : targets) {
if (tft.getLocaleId().equals(localeId))
return tft;
}
return null;
}
@Transient
@Override
public Iterable<ITextFlowTarget> getAllTargetContents() {
return ImmutableList.<ITextFlowTarget> copyOf(getTargets().values());
}
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY,
optional = true)
public HPotEntryData getPotEntryData() {
return potEntryData;
}
@NotNull
public Long getWordCount() {
return wordCount;
}
// this method is private because setContent(), and only setContent(),
// should
// be setting wordCount
private void setWordCount(Long wordCount) {
this.wordCount = wordCount;
}
@Field(analyze = Analyze.NO)
public String getContentHash() {
return contentHash;
}
// this method is private because setContent(), and only setContent(),
// should
// be setting the contentHash
private void setContentHash(String contentHash) {
this.contentHash = contentHash;
}
private void updateWordCount() {
if (document == null) {
// come back when the not-null constraints are satisfied!
return;
}
String locale = toBCP47(document.getLocale());
// TODO strip (eg) HTML tags before counting words. Needs more metadata
// about the content type.
long count = 0;
for (String content : this.getContents()) {
count += OkapiUtil.countWords(content, locale);
}
setWordCount(count);
}
private void updateContentHash() {
String contents = StringUtil.concat(getContents(), '|');
this.setContentHash(HashUtil.generateHash(contents));
}
private static String toBCP47(HLocale docLocale) {
if (docLocale == null) {
// *should* only happen in tests
log.warn("null locale, assuming 'en'");
return "en";
}
LocaleId docLocaleId = docLocale.getLocaleId();
return docLocaleId.getId();
}
public static class EntityListener {
@PreUpdate
private void preUpdate(HTextFlow tf) {
if (!tf.revision.equals(tf.oldRevision)) {
// there is an initial state
if (tf.initialState != null) {
tf.getHistory().put(tf.oldRevision, tf.initialState);
}
if (!tf.isPlural()) {
// if plural form has changed, we need to clear out obsolete
// contents
tf.setContents(tf.content0);
}
}
}
@PostUpdate
@PostPersist
@PostLoad
private void updateInternalHistory(HTextFlow tf) {
tf.oldRevision = tf.revision;
tf.initialState = new HTextFlowHistory(tf);
}
}
}