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
/
HDocument.java
325 lines (290 loc) · 9.91 KB
/
HDocument.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
/*
* 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.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.MapKey;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PreUpdate;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.IndexColumn;
import org.hibernate.annotations.NaturalId;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.Where;
import org.hibernate.validator.constraints.NotEmpty;
import org.jboss.seam.Component;
import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.security.management.JpaIdentityStore;
import org.zanata.common.ContentType;
import org.zanata.common.LocaleId;
import org.zanata.model.po.HPoHeader;
import org.zanata.model.po.HPoTargetHeader;
import org.zanata.model.type.ContentTypeType;
import org.zanata.model.type.EntityType;
import org.zanata.rest.dto.resource.AbstractResourceMeta;
import org.zanata.rest.dto.resource.Resource;
import org.zanata.rest.dto.resource.ResourceMeta;
import org.zanata.rest.dto.resource.TranslationsResource;
import com.google.common.collect.ImmutableList;
/**
* @see AbstractResourceMeta
* @see Resource
* @see ResourceMeta
* @see TranslationsResource
*
*/
@Entity
@EntityListeners({HDocument.EntityListener.class})
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@TypeDef(name = "contentType", typeClass = ContentTypeType.class)
@Setter
@NoArgsConstructor
@ToString(of = { "name", "path", "docId", "locale", "revision" })
public class HDocument extends ModelEntityBase implements DocumentWithId,
IDocumentHistory, Serializable, Iterable<ITextFlow>, IsEntityWithType {
private static final long serialVersionUID = 5129552589912687504L;
private String docId;
private String name;
private String path;
private ContentType contentType;
private Integer revision = 1;
private HLocale locale;
private HPerson lastModifiedBy;
private HProjectIteration projectIteration;
private Map<String, HTextFlow> allTextFlows;
/**
* NB: Any elements which are removed from this list must have obsolete set
* to true, and any elements which are added to this list must have obsolete
* set to false.
*/
private List<HTextFlow> textFlows;
private boolean obsolete = false;
private HPoHeader poHeader;
private Map<HLocale, HPoTargetHeader> poTargetHeaders;
private HRawDocument rawDocument;
public HDocument(String fullPath, ContentType contentType, HLocale locale) {
this.contentType = contentType;
this.locale = locale;
setFullPath(fullPath);
}
public HDocument(String docId, String name, String path,
ContentType contentType, HLocale locale) {
this.docId = docId;
this.name = name;
this.path = path;
this.contentType = contentType;
this.locale = locale;
}
public void setFullPath(String fullPath) {
this.docId = fullPath;
int lastSepChar = fullPath.lastIndexOf('/');
switch (lastSepChar) {
case -1:
this.path = "";
this.name = fullPath;
break;
case 0:
this.path = "/";
this.name = fullPath.substring(1);
break;
default:
this.path = fullPath.substring(0, lastSepChar + 1);
this.name = fullPath.substring(lastSepChar + 1);
}
}
// TODO make this case sensitive
@NaturalId
@Size(max = 255)
@NotEmpty
public String getDocId() {
return docId;
}
@NotEmpty
public String getName() {
return name;
}
@Transient
@Override
public String getQualifiedDocId() {
HProjectIteration iter = getProjectIteration();
HProject proj = iter.getProject();
return proj.getSlug() + ":" + iter.getSlug() + ":" + getDocId();
}
@NotNull
public String getPath() {
return path;
}
@SuppressWarnings("null")
@ManyToOne
@JoinColumn(name = "locale", nullable = false)
@Override
public @Nonnull
HLocale getLocale() {
return this.locale;
}
@Transient
@Override
public @Nonnull
LocaleId getSourceLocaleId() {
return locale.getLocaleId();
}
@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "project_iteration_id", nullable = false)
@NaturalId
public HProjectIteration getProjectIteration() {
return projectIteration;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "last_modified_by_id", nullable = true)
@Override
public HPerson getLastModifiedBy() {
return lastModifiedBy;
}
protected void setLastModifiedBy(HPerson lastModifiedBy) {
this.lastModifiedBy = lastModifiedBy;
}
@NotNull
public Integer getRevision() {
return revision;
}
@Transient
public void incrementRevision() {
revision++;
}
@Type(type = "contentType")
@NotNull
public ContentType getContentType() {
return contentType;
}
@OneToMany
@JoinColumn(name = "document_id", insertable = false, updatable = false)
// , nullable = true
@MapKey(name = "resId")
/**
* NB Don't modify this collection. Add to the TextFlows list instead.
* TODO get ImmutableMap working here.
*/
public Map<String, HTextFlow> getAllTextFlows() {
if (allTextFlows == null) {
allTextFlows = new HashMap<String, HTextFlow>();
}
return allTextFlows;
}
@SuppressWarnings("unused")
// used only by Hibernate
private
void setAllTextFlows(Map<String, HTextFlow> allTextFlows) {
this.allTextFlows = allTextFlows;
}
@OneToMany(cascade = CascadeType.ALL)
@Where(clause = "obsolete=0")
@IndexColumn(name = "pos", base = 0, nullable = false)
@JoinColumn(name = "document_id", nullable = false)
/**
* NB: Any elements which are removed from this list must have obsolete set
* to true, and any elements which are added to this list must have obsolete
* set to false.
*/
public List<HTextFlow> getTextFlows() {
if (textFlows == null) {
textFlows = new ArrayList<HTextFlow>();
}
return textFlows;
}
@Override
public Iterator<ITextFlow> iterator() {
return ImmutableList.<ITextFlow> copyOf(getTextFlows()).iterator();
}
public boolean isObsolete() {
return obsolete;
}
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY,
optional = true)
public HPoHeader getPoHeader() {
return poHeader;
}
// private setter for Hibernate
@SuppressWarnings("unused")
private void setPoTargetHeaders(
Map<HLocale, HPoTargetHeader> poTargetHeaders) {
this.poTargetHeaders = poTargetHeaders;
}
// TODO use orphanRemoval=true: requires JPA 2.0
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY,
mappedBy = "document", orphanRemoval = true)
@MapKey(name = "targetLanguage")
public Map<HLocale, HPoTargetHeader> getPoTargetHeaders() {
if (poTargetHeaders == null) {
poTargetHeaders = new HashMap<HLocale, HPoTargetHeader>();
}
return poTargetHeaders;
}
@OneToOne(fetch = FetchType.LAZY, optional = true)
@JoinTable(name = "HDocument_RawDocument", joinColumns = @JoinColumn(
name = "documentId"), inverseJoinColumns = @JoinColumn(
name = "rawDocumentId"))
public HRawDocument getRawDocument() {
return rawDocument;
}
@Override
@Transient
public EntityType getEntityType() {
return EntityType.HDocument;
}
public static class EntityListener {
@PreUpdate
private void onUpdate(HDocument doc) {
if (Contexts.isSessionContextActive()) {
HAccount account =
(HAccount) Component.getInstance(
JpaIdentityStore.AUTHENTICATED_USER,
ScopeType.SESSION);
// TODO In some cases there is no session ( such as when pushing
// async )
if (account != null) {
doc.setLastModifiedBy(account.getPerson());
}
}
}
}
}