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
/
CopyVersionServiceImpl.java
339 lines (297 loc) · 11.9 KB
/
CopyVersionServiceImpl.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
package org.zanata.service.impl;
import com.google.common.base.Optional;
import com.google.common.base.Stopwatch;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.zanata.async.AsyncUtils;
import org.zanata.async.tasks.CopyVersionTask;
import org.zanata.dao.DocumentDAO;
import org.zanata.dao.ProjectIterationDAO;
import org.zanata.dao.TextFlowDAO;
import org.zanata.dao.TextFlowTargetDAO;
import org.zanata.file.FilePersistService;
import org.zanata.model.HDocument;
import org.zanata.model.HLocale;
import org.zanata.model.HProjectIteration;
import org.zanata.model.HRawDocument;
import org.zanata.model.HTextFlow;
import org.zanata.model.HTextFlowTarget;
import org.zanata.model.HTextFlowTargetHistory;
import org.zanata.model.HTextFlowTargetReviewComment;
import org.zanata.model.po.HPoHeader;
import org.zanata.model.po.HPoTargetHeader;
import org.zanata.model.po.HPotEntryData;
import org.zanata.service.CopyVersionService;
import org.zanata.service.VersionStateCache;
import org.zanata.util.JPACopier;
import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.Map;
/**
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a>
*/
@Name("copyVersionServiceImpl")
@Scope(ScopeType.STATELESS)
@Slf4j
@AutoCreate
public class CopyVersionServiceImpl implements CopyVersionService {
// Document batch size
protected final static int DOC_BATCH_SIZE = 5;
// TextFlow batch size
protected final static int TF_BATCH_SIZE = 20;
// TextFlowTarget batch size
protected final static int TFT_BATCH_SIZE = 20;
@In
private ProjectIterationDAO projectIterationDAO;
@In
private DocumentDAO documentDAO;
@In
private TextFlowDAO textFlowDAO;
@In
private TextFlowTargetDAO textFlowTargetDAO;
@In
private VersionStateCache versionStateCacheImpl;
@In
private FilePersistService filePersistService;
// Stop watch for textFlow and target copy process
private Stopwatch copyTfAndTftStopWatch = new Stopwatch();
@Override
public void copyVersion(@Nonnull String projectSlug,
@Nonnull String versionSlug, @Nonnull String newVersionSlug) {
Stopwatch overallStopwatch = new Stopwatch().start();
log.info("copy version start: copy {} to {}",
projectSlug + ":" + versionSlug, projectSlug + ":"
+ newVersionSlug);
HProjectIteration version =
projectIterationDAO.getBySlug(projectSlug, versionSlug);
if (version == null) {
log.error("Cannot find project iteration of {}:{}", projectSlug,
versionSlug);
return;
}
// Copy of HProjectIteration
HProjectIteration newVersion =
projectIterationDAO.getBySlug(projectSlug, newVersionSlug);
newVersion = copyVersionSettings(version, newVersion);
newVersion = projectIterationDAO.makePersistent(newVersion);
// Copy of HDocument
Optional<CopyVersionTask.CopyVersionTaskHandle> taskHandleOpt =
AsyncUtils.getEventAsyncHandle(
CopyVersionTask.CopyVersionTaskHandle.class);
int docSize =
documentDAO.getDocCountByVersion(projectSlug, versionSlug);
int docStart = 0;
while (docStart < docSize) {
Map<Long, Long> docMap =
copyDocumentBatch(version.getId(), newVersion.getId(),
docStart, DOC_BATCH_SIZE);
docStart += DOC_BATCH_SIZE;
for (Map.Entry<Long, Long> entry : docMap.entrySet()) {
// Copy of HTextFlow and HTextFlowTarget
copyTextFlowAndTarget(entry.getKey(), entry.getValue());
if (taskHandleOpt.isPresent()) {
taskHandleOpt.get().incrementDocumentProcessed();
taskHandleOpt.get().increaseProgress(1);
}
}
}
// restore version.status after complete
newVersion = projectIterationDAO.getBySlug(projectSlug, newVersionSlug);
newVersion.setStatus(version.getStatus());
projectIterationDAO.makePersistent(newVersion);
projectIterationDAO.flush();
// clear any cache that has been loaded in this new version before copy
// completed
versionStateCacheImpl.clearVersionStatsCache(newVersion.getId());
log.info("copy version end: copy {} to {}, {}", projectSlug
+ ":" + versionSlug, projectSlug + ":" + newVersionSlug,
overallStopwatch);
}
@Override
public int getTotalDocCount(@Nonnull String projectSlug,
@Nonnull String versionSlug) {
return documentDAO.getDocCountByVersion(projectSlug, versionSlug);
}
/**
* Return map of old HDocument id, new HDocument id copied
*
* @param versionId
* @param newVersionId
* @param batchStart
* @param batchLength
*/
protected Map<Long, Long> copyDocumentBatch(Long versionId,
Long newVersionId, int batchStart, int batchLength) {
try {
return new CopyDocumentWork(versionId, newVersionId, documentDAO,
projectIterationDAO, filePersistService, this,
batchStart, batchLength).workInTransaction();
} catch (Exception e) {
log.warn("exception during copy document", e);
return Collections.emptyMap();
}
}
/**
* Copy text flows and targets of document with id=documentId to document
* with id=newDocumentId
*
* @param documentId
* @param newDocumentId
*/
private void copyTextFlowAndTarget(Long documentId, Long newDocumentId) {
copyTfAndTftStopWatch.start();
int tfStart = 0, tftStart = 0, totalTftCount = 0;
int tfCount = textFlowDAO.countActiveTextFlowsInDocument(documentId);
while (tfStart < tfCount) {
Map<Long, Long> tfMap =
copyTextFlowBatch(documentId, newDocumentId, tfStart,
TF_BATCH_SIZE);
tfStart += TF_BATCH_SIZE;
textFlowDAO.clear();
documentDAO.clear();
for (Map.Entry<Long, Long> entry : tfMap.entrySet()) {
tftStart = 0;
int tftCount =
textFlowTargetDAO.countTextFlowTargetsInTextFlow(
entry.getKey());
while (tftStart < tftCount) {
totalTftCount +=
copyTextFlowTargetBatch(entry.getKey(),
entry.getValue(), tftStart, TFT_BATCH_SIZE);
tftStart += TFT_BATCH_SIZE;
textFlowDAO.clear();
textFlowTargetDAO.clear();
}
}
}
copyTfAndTftStopWatch.stop();
log.info(
"copy document- textFlow:{}, textFlowTarget:{} copied for document:{} - {}",
tfCount, totalTftCount, newDocumentId, copyTfAndTftStopWatch);
copyTfAndTftStopWatch.reset();
}
/**
* Return map of old HTextFlow id, new HTextFlow id copied
*
* @param documentId
* @param newDocumentId
* @param batchStart
* @param batchLength
*/
protected Map<Long, Long> copyTextFlowBatch(Long documentId,
Long newDocumentId, int batchStart, int batchLength) {
try {
return new CopyTextFlowWork(documentId, newDocumentId, textFlowDAO,
documentDAO, this, batchStart, batchLength)
.workInTransaction();
} catch (Exception e) {
log.warn("exception during copy text flow", e);
return Collections.EMPTY_MAP;
}
}
/**
* Return number of HTextFlowTarget copied
*
* @param tfId
* @param newTfId
* @param batchStart
* @param batchLength
*/
protected int copyTextFlowTargetBatch(Long tfId, Long newTfId,
int batchStart, int batchLength) {
try {
return new CopyTextFlowTargetWork(tfId, newTfId, textFlowTargetDAO,
textFlowDAO, this, batchStart, batchLength)
.workInTransaction();
} catch (Exception e) {
log.warn("exception during copy text flow target", e);
return 0;
}
}
@Override
public HProjectIteration copyVersionSettings(
HProjectIteration version, HProjectIteration newVersion) {
try {
JPACopier.copyBean(version, newVersion, "slug", "status", "project",
"children", "documents", "allDocuments");
} catch (Exception e) {
log.warn("exception during copy version", e);
}
return newVersion;
}
@Override
public HDocument copyDocument(HProjectIteration newVersion,
HDocument document) throws Exception {
HDocument copy =
JPACopier.<HDocument> copyBean(document, "projectIteration",
"poHeader", "poTargetHeaders", "rawDocument",
"textFlows", "allTextFlows");
copy.setProjectIteration(newVersion);
if (document.getPoHeader() != null) {
HPoHeader poHeader =
JPACopier.<HPoHeader> copyBean(document.getPoHeader());
copy.setPoHeader(poHeader);
}
for (Map.Entry<HLocale, HPoTargetHeader> entry : document
.getPoTargetHeaders().entrySet()) {
HPoTargetHeader poTargetHeader =
JPACopier.<HPoTargetHeader> copyBean(entry.getValue(),
"document");
poTargetHeader.setDocument(copy);
copy.getPoTargetHeaders().put(entry.getKey(), poTargetHeader);
}
return copy;
}
@Override
public HRawDocument copyRawDocument(HDocument newDocument,
@Nonnull HRawDocument rawDocument) throws Exception {
HRawDocument copy =
JPACopier.<HRawDocument> copyBean(rawDocument, "document");
copy.setDocument(newDocument);
return copy;
}
@Override
public HTextFlow copyTextFlow(HDocument newDocument, HTextFlow textFlow)
throws Exception {
HTextFlow copy =
JPACopier.<HTextFlow> copyBean(textFlow, "document", "content",
"targets", "history", "potEntryData");
copy.setDocument(newDocument);
// copy PotEntryData
if (textFlow.getPotEntryData() != null) {
HPotEntryData potEntryData = JPACopier.<HPotEntryData> copyBean(
textFlow.getPotEntryData(), "textFlow");
copy.setPotEntryData(potEntryData);
}
return copy;
}
@Override
public HTextFlowTarget copyTextFlowTarget(HTextFlow newTf,
HTextFlowTarget tft) throws Exception {
HTextFlowTarget copy =
JPACopier.<HTextFlowTarget> copyBean(tft, "textFlow",
"reviewComments", "history");
copy.setTextFlow(newTf);
copy.setTextFlowRevision(newTf.getRevision());
// copy review comment
copy.setReviewComments(Lists
.<HTextFlowTargetReviewComment> newArrayList());
for (HTextFlowTargetReviewComment comment : tft.getReviewComments()) {
copy.addReviewComment(comment.getComment(), comment.getCommenter());
}
// copy history
for (HTextFlowTargetHistory history : tft.getHistory().values()) {
HTextFlowTargetHistory newHistory =
JPACopier.<HTextFlowTargetHistory> copyBean(history,
"textFlowTarget");
newHistory.setTextFlowTarget(copy);
copy.getHistory().put(newHistory.getVersionNum(), newHistory);
}
return copy;
}
}