This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
HTextFlowTarget.java
427 lines (363 loc) · 12 KB
/
HTextFlowTarget.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
/*
* 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.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MapKey;
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 lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.NaturalId;
import org.hibernate.search.annotations.Analyze;
import org.hibernate.search.annotations.AnalyzerDiscriminator;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.FieldBridge;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.IndexedEmbedded;
import org.hibernate.search.annotations.Parameter;
import org.hibernate.validator.constraints.NotEmpty;
import org.zanata.common.ContentState;
import org.zanata.common.HasContents;
import org.zanata.common.LocaleId;
import org.zanata.hibernate.search.ContentStateBridge;
import org.zanata.hibernate.search.IndexFieldLabels;
import org.zanata.hibernate.search.LocaleIdBridge;
import org.zanata.hibernate.search.StringListBridge;
import org.zanata.hibernate.search.TextContainerAnalyzerDiscriminator;
import org.zanata.model.type.EntityType;
import com.google.common.base.Objects;
import com.google.common.collect.Lists;
/**
* Represents a flow of translated text that should be processed as a
* stand-alone structural unit.
*
* @see org.zanata.rest.dto.resource.TextFlowTarget
* @author Asgeir Frimannsson <asgeirf@redhat.com>
*
*/
@Entity
@EntityListeners({ HTextFlowTarget.EntityListener.class })
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Indexed
@Setter
@NoArgsConstructor
public class HTextFlowTarget extends ModelEntityBase implements HasContents,
HasSimpleComment, ITextFlowTargetHistory, Serializable,
ITextFlowTarget, IsEntityWithType {
private static final long serialVersionUID = 302308010797605435L;
private HTextFlow textFlow;
private @Nonnull
HLocale locale;
private String content0;
private String content1;
private String content2;
private String content3;
private String content4;
private String content5;
private ContentState state = ContentState.New;
private Integer textFlowRevision;
private HPerson lastModifiedBy;
private HPerson translator;
private HPerson reviewer;
private HSimpleComment comment;
private Map<Integer, HTextFlowTargetHistory> history;
private List<HTextFlowTargetReviewComment> reviewComments;
// Only for internal use (persistence transient)
@Setter(AccessLevel.PRIVATE)
private Integer oldVersionNum;
// Only for internal use (persistence transient)
@Setter(AccessLevel.PRIVATE)
private HTextFlowTargetHistory initialState;
public HTextFlowTarget(HTextFlow textFlow, @Nonnull HLocale locale) {
this.locale = locale;
this.textFlow = textFlow;
this.textFlowRevision = textFlow.getRevision();
}
// TODO PERF @NaturalId(mutable=false) for better criteria caching
@NaturalId
@ManyToOne
@JoinColumn(name = "locale", nullable = false)
@Field(analyze = Analyze.NO)
@FieldBridge(impl = LocaleIdBridge.class)
public @Nonnull
HLocale getLocale() {
return locale;
}
@Transient
@Override
public @Nonnull
LocaleId getLocaleId() {
return locale.getLocaleId();
}
@NotNull
@Field(analyze = Analyze.NO)
@FieldBridge(impl = ContentStateBridge.class)
@Override
public @Nonnull
ContentState getState() {
return state;
}
@NotNull
@Column(name = "tf_revision")
@Override
public Integer getTextFlowRevision() {
return textFlowRevision;
}
@ManyToOne(cascade = { CascadeType.MERGE }, fetch = FetchType.LAZY)
@JoinColumn(name = "last_modified_by_id", nullable = true)
@Override
public HPerson getLastModifiedBy() {
return lastModifiedBy;
}
public void setLastModifiedBy(HPerson date) {
lastModifiedBy = date;
}
@Override
@ManyToOne(cascade = { CascadeType.MERGE }, fetch = FetchType.LAZY)
@JoinColumn(name = "translated_by_id", nullable = true)
public HPerson getTranslator() {
return translator;
}
@Override
@ManyToOne(cascade = { CascadeType.MERGE }, fetch = FetchType.LAZY)
@JoinColumn(name = "reviewed_by_id", nullable = true)
public HPerson getReviewer() {
return reviewer;
}
public boolean hasReviewer() {
return reviewer != null;
}
// TODO PERF @NaturalId(mutable=false) for better criteria caching
@NaturalId
@ManyToOne
@JoinColumn(name = "tf_id")
// @Field(index = Index.UN_TOKENIZED)
// @FieldBridge(impl = ContainingWorkspaceBridge.class)
@IndexedEmbedded
public
HTextFlow getTextFlow() {
return textFlow;
}
/**
* As of release 1.6, replaced by {@link #getContents()}
*
* @return
*/
@Deprecated
@Transient
public String getContent() {
if (this.getContents().size() > 0) {
return this.getContents().get(0);
}
return null;
}
@Deprecated
@Transient
public void setContent(String content) {
this.setContents(Arrays.asList(content));
}
@Override
@Transient
@NotEmpty
// TODO extend HTextContainer and remove this
@Field(name = IndexFieldLabels.CONTENT, bridge = @FieldBridge(
impl = StringListBridge.class, params = {
@Parameter(name = "case", value = "fold"),
@Parameter(name = "ngrams", value = "multisize") }))
@AnalyzerDiscriminator(impl = TextContainerAnalyzerDiscriminator.class)
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> contents) {
if (!Objects.equal(contents, this.getContents())) {
for (int i = 0; i < contents.size(); i++) {
this.setContent(i, contents.get(i));
}
}
}
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;
}
public void setContents(String... contents) {
this.setContents(Arrays.asList(contents));
}
@OneToOne(optional = true, fetch = FetchType.LAZY,
cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "comment_id")
public HSimpleComment getComment() {
return comment;
}
@OneToMany(cascade = { CascadeType.REMOVE, CascadeType.MERGE,
CascadeType.PERSIST }, mappedBy = "textFlowTarget")
@MapKey(name = "versionNum")
public Map<Integer, HTextFlowTargetHistory> getHistory() {
if (this.history == null) {
this.history = new HashMap<Integer, HTextFlowTargetHistory>();
}
return history;
}
@OneToMany(cascade = { CascadeType.REMOVE, CascadeType.MERGE,
CascadeType.PERSIST }, mappedBy = "textFlowTarget")
public List<HTextFlowTargetReviewComment> getReviewComments() {
if (reviewComments == null) {
reviewComments = Lists.newArrayList();
}
return reviewComments;
}
public HTextFlowTargetReviewComment addReviewComment(String comment,
HPerson commenter) {
HTextFlowTargetReviewComment reviewComment =
new HTextFlowTargetReviewComment(this, comment, commenter);
getReviewComments().add(reviewComment);
return reviewComment;
}
@Override
public String toString() {
return Objects.toStringHelper(this).add("contents", getContents())
.add("locale", getLocale()).add("state", getState())
.add("comment", getComment())
.add("textFlow", getTextFlow().getContents()).toString();
}
@Transient
public void clear() {
setContents();
setState(ContentState.New);
setComment(null);
setLastModifiedBy(null);
setTranslator(null);
setReviewer(null);
}
protected boolean logPersistence() {
return false;
}
@Override
@Transient
public EntityType getEntityType() {
return EntityType.HTexFlowTarget;
}
public static class EntityListener {
@PreUpdate
private void preUpdate(HTextFlowTarget tft) {
// insert history if this has changed from its initial state
if (tft.initialState != null && tft.initialState.hasChanged(tft)) {
tft.getHistory().put(tft.oldVersionNum, tft.initialState);
}
}
@PostUpdate
@PostPersist
@PostLoad
private void updateInternalHistory(HTextFlowTarget tft) {
tft.oldVersionNum = tft.getVersionNum();
tft.initialState = new HTextFlowTargetHistory(tft);
}
}
}