-
Notifications
You must be signed in to change notification settings - Fork 51
/
PushCommand.java
833 lines (745 loc) · 34 KB
/
PushCommand.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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
package org.zanata.client.commands.push;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.ws.rs.client.ResponseProcessingException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zanata.adapter.properties.PropWriter;
import org.zanata.adapter.xliff.XliffCommon.ValidationType;
import org.zanata.client.commands.PushPullCommand;
import org.zanata.client.commands.PushPullType;
import org.zanata.client.config.LocaleMapping;
import org.zanata.client.exceptions.ConfigException;
import org.zanata.client.util.ConsoleUtils;
import org.zanata.common.LocaleId;
import org.zanata.common.MergeType;
import org.zanata.rest.RestUtil;
import org.zanata.rest.StringSet;
import org.zanata.rest.client.AsyncProcessClient;
import org.zanata.rest.client.CopyTransClient;
import org.zanata.rest.client.RestClientFactory;
import org.zanata.rest.dto.CopyTransStatus;
import org.zanata.rest.dto.ProcessStatus;
import org.zanata.rest.dto.resource.Resource;
import org.zanata.rest.dto.resource.ResourceMeta;
import org.zanata.rest.dto.resource.TextFlowTarget;
import org.zanata.rest.dto.resource.TranslationsResource;
import com.google.common.base.Predicate;
import com.google.common.base.Strings;
import com.google.common.base.Throwables;
import static com.google.common.collect.Collections2.filter;
import static com.google.common.collect.Iterables.all;
/**
* @author Sean Flanigan <a
* href="mailto:sflaniga@redhat.com">sflaniga@redhat.com</a>
*
*/
public class PushCommand extends PushPullCommand<PushOptions> {
private static final Logger log = LoggerFactory
.getLogger(PushCommand.class);
private static final int POLL_PERIOD = 250;
private static final Map<String, AbstractPushStrategy> strategies =
new HashMap<String, AbstractPushStrategy>();
private CopyTransClient copyTransClient;
private AsyncProcessClient asyncProcessClient;
public interface TranslationResourcesVisitor {
void visit(LocaleMapping locale, TranslationsResource targetDoc);
}
{
strategies.put(PROJECT_TYPE_UTF8_PROPERTIES, new PropertiesStrategy(
PropWriter.CHARSET.UTF8));
strategies.put(PROJECT_TYPE_PROPERTIES, new PropertiesStrategy());
strategies.put(PROJECT_TYPE_GETTEXT, new GettextPushStrategy());
strategies.put(PROJECT_TYPE_PUBLICAN, new GettextDirStrategy());
strategies.put(PROJECT_TYPE_XLIFF, new XliffStrategy());
strategies.put(PROJECT_TYPE_XML, new XmlStrategy());
strategies.put(
PROJECT_TYPE_OFFLINE_PO,
new OfflinePoStrategy(getClientFactory()
.getSourceDocResourceClient(getOpts().getProj(),
getOpts()
.getProjectVersion())));
}
public PushCommand(PushOptions opts) {
super(opts);
copyTransClient = getClientFactory().getCopyTransClient();
asyncProcessClient = getClientFactory().getAsyncProcessClient();
}
public PushCommand(PushOptions opts,
CopyTransClient copyTransClient,
AsyncProcessClient asyncProcessClient,
RestClientFactory clientFactory) {
super(opts,
clientFactory);
this.copyTransClient = copyTransClient;
this.asyncProcessClient = asyncProcessClient;
}
public AbstractPushStrategy getStrategy(PushOptions pushOptions) {
AbstractPushStrategy strat =
strategies.get(pushOptions.getProjectType());
if (strat == null) {
throw new RuntimeException("unknown project type: "
+ pushOptions.getProjectType());
}
strat.setPushOptions(pushOptions);
strat.init();
return strat;
}
private void checkOptions() {
if (getOpts().getListFileTypes()) {
throw new RuntimeException("\"List file types\" is only supported for project type FILE");
}
}
public static void logOptions(Logger logger, PushOptions opts) {
if (!logger.isInfoEnabled()) {
return;
}
logger.info("Server: {}", opts.getUrl());
logger.info("Project: {}", opts.getProj());
logger.info("Version: {}", opts.getProjectVersion());
logger.info("Username: {}", opts.getUsername());
logger.info("Project type: {}", opts.getProjectType());
logger.info("Source language: {}", opts.getSourceLang());
String copyTransMssg = "" + opts.getCopyTrans();
if (opts.getCopyTrans() && opts.getPushType() == PushPullType.Trans) {
copyTransMssg = "disabled since pushType=Trans";
}
logger.info("Copy previous translations: {}", copyTransMssg);
if (!opts.getCopyTrans() && opts.getPushType() != PushPullType.Trans) {
logger.warn("As of Zanata Client 3.8.0, copyTrans is disabled by default.");
}
logger.info("Merge type: {}", opts.getMergeType());
logger.info("Enable modules: {}", opts.getEnableModules());
if (opts.getEnableModules()) {
logger.info("Current module: {}", opts.getCurrentModule());
if (opts.isRootModule()) {
logger.info("Root module: YES");
if (logger.isDebugEnabled()) {
logger.debug("Modules: {}",
StringUtils.join(opts.getAllModules(), ", "));
}
}
}
logger.info("Include patterns: {}",
StringUtils.join(opts.getIncludes(), " "));
logger.info("Exclude patterns: {}",
StringUtils.join(opts.getExcludes(), " "));
logger.info("Case sensitive: {}", opts.getCaseSensitive());
logger.info("Default excludes: {}", opts.getDefaultExcludes());
log.info("Exclude locale filenames: {}",
opts.getExcludeLocaleFilenames());
if (opts.getPushType() == PushPullType.Trans) {
logger.info("Pushing target documents only");
logger.info("Locales to push: {}", opts.getLocaleMapList());
} else if (opts.getPushType() == PushPullType.Source) {
logger.info("Pushing source documents only");
} else {
logger.info("Pushing source and target documents");
logger.info("Locales to push: {}", opts.getLocaleMapList());
}
logger.info("Current directory: {}", System.getProperty("user.dir"));
logger.info("Source directory (originals): {}", opts.getSrcDir());
if (opts.getPushType() == PushPullType.Both
|| opts.getPushType() == PushPullType.Trans) {
logger.info("Target base directory (translations): {}",
opts.getTransDir());
logger.info("Is my translations: {}", opts.isMyTrans());
}
if (opts.getFromDoc() != null) {
logger.info("From document: {}", opts.getFromDoc());
}
if (opts.isDryRun()) {
logger.info("DRY RUN: no permanent changes will be made");
}
if (opts.getProjectType().equalsIgnoreCase("xliff")) {
validateValidation(opts.getValidate());
log.info("Validate option: {}", opts.getValidate());
}
}
private static void validateValidation(String validate) {
if (!validate.equalsIgnoreCase(ValidationType.CONTENT.toString())
&& !validate.equalsIgnoreCase(ValidationType.XSD.toString())) {
throw new RuntimeException("unknown validate option: " + validate);
}
}
private boolean mergeAuto() {
return getOpts().getMergeType().toUpperCase()
.equals(MergeType.AUTO.name());
}
private boolean pushSource() {
return getOpts().getPushType() == PushPullType.Both
|| getOpts().getPushType() == PushPullType.Source;
}
private boolean pushTrans() {
return getOpts().getPushType() == PushPullType.Both
|| getOpts().getPushType() == PushPullType.Trans;
}
@Override
public void run() throws Exception {
checkOptions();
logOptions(log, getOpts());
pushCurrentModule();
if (pushSource() && getOpts().getEnableModules()
&& getOpts().isRootModule()) {
List<String> obsoleteDocs =
getObsoleteDocNamesForProjectIterationFromServer();
log.info("found {} docs in obsolete modules (or no module): {}",
obsoleteDocs.size(), obsoleteDocs);
if (getOpts().getDeleteObsoleteModules() && !obsoleteDocs.isEmpty()) {
// offer to delete obsolete documents
confirmWithUser("Do you want to delete all documents from the server which don't belong to any module in the Maven reactor?\n");
deleteSourceDocsFromServer(obsoleteDocs);
} else {
log.warn(
"found {} docs in obsolete modules (or no module). use -Dzanata.deleteObsoleteModules to delete them",
obsoleteDocs.size());
}
}
}
/**
* gets doc list from server, returns a list of qualified doc names from
* obsolete modules, or from no module.
*/
protected List<String> getObsoleteDocNamesForProjectIterationFromServer() {
if (!getOpts().getEnableModules())
return Collections.emptyList();
List<ResourceMeta> remoteDocList =
getDocListForProjectIterationFromServer();
Pattern p = Pattern.compile(getOpts().getDocNameRegex());
Set<String> modules = new HashSet<String>(getOpts().getAllModules());
List<String> obsoleteDocs = new ArrayList<String>();
for (ResourceMeta doc : remoteDocList) {
// NB ResourceMeta.name == HDocument.docId
String docName = doc.getName();
Matcher matcher = p.matcher(docName);
if (matcher.matches()) {
String module = matcher.group(1);
if (modules.contains(module)) {
log.debug("doc {} belongs to non-obsolete module {}",
docName, module);
} else {
obsoleteDocs.add(docName);
log.info("doc {} belongs to obsolete module {}", docName,
module);
}
} else {
obsoleteDocs.add(docName);
log.warn("doc {} doesn't belong to any module", docName);
}
}
return obsoleteDocs;
}
private void pushCurrentModule() throws IOException, RuntimeException {
AbstractPushStrategy strat = getStrategy(getOpts());
File sourceDir = getOpts().getSrcDir();
if (!pushSource() && !pushTrans()) {
throw new RuntimeException("Invalid option for push type");
}
if (!sourceDir.exists() && !strat.isTransOnly()) {
if (getOpts().getEnableModules()) {
log.info("source directory '" + sourceDir
+ "' not found; skipping docs push for module "
+ getOpts().getCurrentModule());
return;
} else {
throw new RuntimeException("directory '" + sourceDir
+ "' does not exist - check "
+ getOpts().getSrcDirParameterName() + " option");
}
}
final StringSet extensions = strat.getExtensions();
// to save memory, we don't load all the docs into a HashMap
Set<String> unsortedDocNames =
strat.findDocNames(sourceDir, getOpts().getIncludes(),
getOpts().getExcludes(),
getOpts().getDefaultExcludes(), getOpts()
.getCaseSensitive(), getOpts()
.getExcludeLocaleFilenames());
SortedSet<String> localDocNames = new TreeSet<String>(unsortedDocNames);
SortedSet<String> docsToPush = localDocNames;
if (getOpts().getFromDoc() != null) {
if (getOpts().getEnableModules()) {
if (belongsToCurrentModule(getOpts().getFromDoc())) {
docsToPush =
getDocsAfterFromDoc(unqualifiedDocName(getOpts()
.getFromDoc()), localDocNames);
}
// else fromDoc does not apply to this module
} else {
docsToPush =
getDocsAfterFromDoc(getOpts().getFromDoc(),
localDocNames);
}
}
if (localDocNames.isEmpty()) {
log.info("No source documents found.");
} else {
log.info("Found source documents:");
for (String docName : localDocNames) {
if (docsToPush.contains(docName)) {
log.info(" {}", docName);
} else {
log.info("(to skip) {}", docName);
}
}
}
List<String> obsoleteDocs = Collections.emptyList();
if (pushSource() && !strat.isTransOnly()) {
obsoleteDocs = getObsoleteDocsInModuleFromServer(localDocNames);
}
if (obsoleteDocs.isEmpty()) {
if (localDocNames.isEmpty()) {
log.info("no documents in module: {}; nothing to do", getOpts()
.getCurrentModule());
return;
} else {
// nop
}
} else {
log.warn(
"Found {} obsolete docs on the server which will be DELETED",
obsoleteDocs.size());
log.info("Obsolete docs: {}", obsoleteDocs);
}
if (pushTrans() && getOpts().getLocaleMapList() == null) {
throw new ConfigException("pushType set to '"
+ getOpts().getPushType()
+ "', but project has no locales configured");
}
if (pushTrans()) {
log.warn("pushType set to '"
+ getOpts().getPushType()
+ "': existing translations on server may be overwritten/deleted");
}
if (strat.isTransOnly()) {
switch (getOpts().getPushType()) {
case Source:
log.error("You are trying to push source only, but source is not available for this project type.\n");
log.info("Nothing to do. Aborting\n");
return;
case Both:
log.warn("Source is not available for this project type. Source will not be pushed.\n");
confirmWithUser("This will overwrite existing TRANSLATIONS on the server.\n");
break;
case Trans:
confirmWithUser("This will overwrite existing TRANSLATIONS on the server.\n");
break;
}
} else {
if (pushTrans()) {
if (getOpts().getPushType() == PushPullType.Both) {
confirmWithUser("This will overwrite existing documents AND TRANSLATIONS on the server, and delete obsolete documents.\n");
} else if (getOpts().getPushType() == PushPullType.Trans) {
confirmWithUser("This will overwrite existing TRANSLATIONS on the server.\n");
}
} else {
confirmWithUser("This will overwrite existing source documents on the server, and delete obsolete documents.\n");
}
}
for (final String localDocName : docsToPush) {
try {
final String qualifiedDocName = qualifiedDocName(localDocName);
final Resource srcDoc;
if (strat.isTransOnly()) {
srcDoc = null;
} else {
srcDoc = strat.loadSrcDoc(sourceDir, localDocName);
srcDoc.setName(qualifiedDocName);
debug(srcDoc);
if (pushSource()) {
pushSrcDocToServer(qualifiedDocName, srcDoc, extensions);
}
}
if (pushTrans()) {
strat.visitTranslationResources(localDocName, srcDoc,
new TranslationResourcesVisitor() {
@Override
public void visit(LocaleMapping locale,
TranslationsResource targetDoc) {
debug(targetDoc);
stripUntranslatedEntriesIfMergeTypeIsNotImport(getOpts(),
targetDoc);
if (targetDoc.getTextFlowTargets()
.isEmpty()) {
log.debug(
"Skip translation file {}({}) since it has no translation in it",
localDocName, locale);
return;
}
pushTargetDocToServer(qualifiedDocName, locale,
qualifiedDocName, targetDoc,
extensions);
}
});
}
// Copy Trans after pushing (only when pushing source)
if (getOpts().getCopyTrans() && (pushSource())) {
this.copyTransForDocument(qualifiedDocName);
}
} catch (Exception e) {
String message =
"Operation failed: " + e.getMessage() + "\n\n"
+ " To retry from the last document, please set the following option(s):\n\n"
+ " ";
if (getOpts().getEnableModules()) {
message +=
"--resume-from " + getOpts().getCurrentModule(true)
+ " ";
}
// Note: '.' is included after trailing newlines to prevent them
// being stripped,
// since stripping newlines can cause extra text to be appended
// to the options.
message +=
getOpts().buildFromDocArgument(
qualifiedDocName(localDocName))
+ "\n\n.";
log.error(message);
throw new RuntimeException(e.getMessage(), e);
}
}
deleteSourceDocsFromServer(obsoleteDocs);
}
private static void stripUntranslatedEntriesIfMergeTypeIsNotImport(
PushOptions opts, TranslationsResource translationResources) {
String mergeType = opts.getMergeType();
if (!MergeType.IMPORT.name().equalsIgnoreCase(mergeType)) {
List<TextFlowTarget> originalTargets =
translationResources.getTextFlowTargets();
final Predicate<String> blankStringPredicate =
new Predicate<String>() {
@Override
public boolean apply(String input) {
return Strings.isNullOrEmpty(input);
}
};
Collection<TextFlowTarget> untranslatedEntries =
filter(originalTargets,
new Predicate<TextFlowTarget>() {
@Override
public boolean apply(
TextFlowTarget input) {
// it's unsafe to rely on content state (plural entries)
return input == null
|| input.getContents().isEmpty()
|| all(input.getContents(),
blankStringPredicate);
}
});
log.debug(
"Remove {} untranslated entries from the payload since merge type is NOT import ({})",
untranslatedEntries.size(), mergeType);
translationResources.getTextFlowTargets()
.removeAll(untranslatedEntries);
}
}
/**
* Returns a list with all documents before fromDoc removed.
*
* @param fromDoc
* @param docNames
* @return a set with only the documents after fromDoc, inclusive
* @throws RuntimeException
* if no document with the specified name exists
*/
private SortedSet<String> getDocsAfterFromDoc(String fromDoc,
SortedSet<String> docNames) {
SortedSet<String> docsToPush;
if (!docNames.contains(fromDoc)) {
throw new RuntimeException(
"Document with id "
+ fromDoc
+ " not found, unable to start push from unknown document. Aborting.");
}
docsToPush = docNames.tailSet(fromDoc);
int numSkippedDocs = docNames.size() - docsToPush.size();
log.info("Skipping {} document(s) before {}.", numSkippedDocs, fromDoc);
return docsToPush;
}
/**
* Returns obsolete docs which belong to the current module. Returns any
* docs in the current module from the server, unless they are found in the
* localDocNames set.
*
* @param localDocNames
*/
private List<String> getObsoleteDocsInModuleFromServer(
Set<String> localDocNames) {
List<String> qualifiedDocNames =
getQualifiedDocNamesForCurrentModuleFromServer();
List<String> obsoleteDocs =
new ArrayList<String>(qualifiedDocNames.size());
for (String qualifiedDocName : qualifiedDocNames) {
String unqualifiedDocName = unqualifiedDocName(qualifiedDocName);
if (!localDocNames.contains(unqualifiedDocName)) {
obsoleteDocs.add(qualifiedDocName);
}
}
return obsoleteDocs;
}
/**
* @param qualifiedDocNames
*/
private void deleteSourceDocsFromServer(List<String> qualifiedDocNames) {
for (String qualifiedDocName : qualifiedDocNames) {
deleteSourceDocFromServer(qualifiedDocName);
}
}
private void pushSrcDocToServer(final String docId, final Resource srcDoc,
final StringSet extensions) {
if (!getOpts().isDryRun()) {
log.info("pushing source doc [name={} size={}] to server",
srcDoc.getName(), srcDoc.getTextFlows().size());
ConsoleUtils.startProgressFeedback();
ProcessStatus status =
asyncProcessClient.startSourceDocCreationOrUpdateWithDocId(
getOpts().getProj(), getOpts().getProjectVersion(),
srcDoc, extensions, docId);
boolean waitForCompletion = true;
while (waitForCompletion) {
switch (status.getStatusCode()) {
case Failed:
throw new RuntimeException(
"Failed while pushing document: "
+ status.getMessages());
case Finished:
waitForCompletion = false;
break;
case Running:
ConsoleUtils.setProgressFeedbackMessage("Pushing ...");
break;
case Waiting:
ConsoleUtils
.setProgressFeedbackMessage("Waiting to start ...");
break;
case NotAccepted:
// try to submit the process again
status =
asyncProcessClient
.startSourceDocCreationOrUpdateWithDocId(
getOpts().getProj(), getOpts()
.getProjectVersion(),
srcDoc, extensions, docId);
ConsoleUtils
.setProgressFeedbackMessage("Waiting for other clients ...");
break;
case Cancelled:
waitForCompletion = false;
ConsoleUtils.setProgressFeedbackMessage("Process is cancelled");
break;
}
// Wait before retrying
wait(POLL_PERIOD);
status = asyncProcessClient.getProcessStatus(status.getUrl());
}
ConsoleUtils.endProgressFeedback();
} else {
log.info(
"pushing source doc [name={} size={}] to server (skipped due to dry run)",
srcDoc.getName(), srcDoc.getTextFlows().size());
}
}
/**
* Split TranslationsResource into List<TranslationsResource>
* according to maxBatchSize, but only if mergeType=AUTO
*
* @param doc
* @param maxBatchSize
* @return list of TranslationsResource, each containing up to maxBatchSize
* TextFlowTargets
*/
public List<TranslationsResource> splitIntoBatch(TranslationsResource doc,
int maxBatchSize) {
List<TranslationsResource> targetDocList =
new ArrayList<TranslationsResource>();
int numTargets = doc.getTextFlowTargets().size();
if (numTargets > maxBatchSize && mergeAuto()) {
int numBatches = numTargets / maxBatchSize;
if (numTargets % maxBatchSize != 0) {
++numBatches;
}
int fromIndex = 0;
int toIndex;
for (int i = 1; i <= numBatches; i++) {
// make a dummy TranslationsResource to hold just the
// TextFlowTargets for each batch
TranslationsResource resource = new TranslationsResource();
resource.setExtensions(doc.getExtensions());
resource.setLinks(doc.getLinks());
resource.setRevision(doc.getRevision());
if ((i * maxBatchSize) > numTargets) {
toIndex = numTargets;
} else {
toIndex = i * maxBatchSize;
}
resource.getTextFlowTargets().addAll(
doc.getTextFlowTargets().subList(fromIndex, toIndex));
fromIndex = i * maxBatchSize;
targetDocList.add(resource);
}
} else {
targetDocList.add(doc);
}
return targetDocList;
}
private void pushTargetDocToServer(final String docId,
LocaleMapping locale, final String localDocName,
TranslationsResource targetDoc, final StringSet extensions) {
if (!getOpts().isDryRun()) {
log.info(
"Pushing target doc [name={} size={} client-locale={}] to server [locale={}]",
localDocName,
targetDoc.getTextFlowTargets().size(),
locale.getLocalLocale(), locale.getLocale());
ConsoleUtils.startProgressFeedback();
ProcessStatus status =
asyncProcessClient
.startTranslatedDocCreationOrUpdateWithDocId(
getOpts().getProj(), getOpts()
.getProjectVersion(),
new LocaleId(locale.getLocale()), targetDoc,
docId, extensions, getOpts().getMergeType(),
getOpts().isMyTrans());
boolean waitForCompletion = true;
while (waitForCompletion) {
switch (status.getStatusCode()) {
case Failed:
throw new RuntimeException(
"Failed while pushing document translations: "
+ status.getMessages());
case Finished:
waitForCompletion = false;
break;
case Running:
ConsoleUtils.setProgressFeedbackMessage(status
.getPercentageComplete() + "%");
break;
case Waiting:
ConsoleUtils
.setProgressFeedbackMessage("Waiting to start ...");
break;
case NotAccepted:
// try to submit the process again
status =
asyncProcessClient
.startTranslatedDocCreationOrUpdateWithDocId(
getOpts().getProj(), getOpts()
.getProjectVersion(),
new LocaleId(
locale.getLocale()),
targetDoc, docId, extensions,
getOpts().getMergeType(),
getOpts().isMyTrans());
ConsoleUtils
.setProgressFeedbackMessage("Waiting for other clients ...");
break;
case Cancelled:
waitForCompletion = false;
ConsoleUtils.setProgressFeedbackMessage("Process is cancelled");
break;
}
// Wait before retrying
wait(POLL_PERIOD);
status = asyncProcessClient.getProcessStatus(status.getUrl());
}
ConsoleUtils.endProgressFeedback();
// Show warning messages
if (status.getMessages().size() > 0) {
log.warn("Pushed translations with warnings:");
for (String mssg : status.getMessages()) {
log.warn(mssg);
}
}
} else {
log.info(
"pushing target doc [name={} size={} client-locale={}] to server [locale={}] (skipped due to dry run)",
localDocName, targetDoc.getTextFlowTargets().size(),
locale.getLocalLocale(), locale.getLocale());
}
}
private void deleteSourceDocFromServer(String qualifiedDocName) {
if (!getOpts().isDryRun()) {
log.info("deleting resource {} from server", qualifiedDocName);
sourceDocResourceClient.deleteResource(qualifiedDocName);
} else {
log.info(
"deleting resource {} from server (skipped due to dry run)",
qualifiedDocName);
}
}
private void copyTransForDocument(String docName) {
if (getOpts().isDryRun()) {
log.info("Skipping Copy Trans for " + docName + " (due to dry run)");
return;
}
log.info("Running Copy Trans for " + docName);
try {
this.copyTransClient.startCopyTrans(getOpts().getProj(),
getOpts().getProjectVersion(), docName);
} catch (Exception ex) {
log.warn("Could not start Copy Trans for above document. Proceeding");
return;
}
CopyTransStatus copyTransStatus;
try {
copyTransStatus =
this.copyTransClient.getCopyTransStatus(getOpts()
.getProj(), getOpts().getProjectVersion(), docName);
} catch (ResponseProcessingException e) {
// 404 - Probably because of an old server
if (e.getResponse().getStatus() == 404 && getClientFactory()
.compareToServerVersion("1.8.0-SNAPSHOT") < 0) {
log.warn("Copy Trans not started (Incompatible server version.)");
return;
} else {
throw new RuntimeException(
"Could not invoke copy trans. The service returned (" + e.getResponse().getStatus() + ")",
Throwables.getRootCause(e));
}
} catch (Exception failure) {
if (failure.getCause() != null) {
throw new RuntimeException("Problem invoking copy trans.",
failure.getCause());
}
throw failure;
}
ConsoleUtils.startProgressFeedback();
while (copyTransStatus.isInProgress()) {
try {
Thread.sleep(POLL_PERIOD);
} catch (InterruptedException e) {
log.warn("Interrupted while waiting for Copy Trans to finish.");
}
ConsoleUtils.setProgressFeedbackMessage(copyTransStatus
.getPercentageComplete() + "%");
copyTransStatus =
this.copyTransClient.getCopyTransStatus(getOpts()
.getProj(), getOpts().getProjectVersion(), docName);
}
ConsoleUtils.endProgressFeedback();
if (copyTransStatus.getPercentageComplete() < 100) {
log.warn("Copy Trans for the above document stopped unexpectedly.");
}
}
// TODO Perhaps move this to ConsoleUtils
private static void wait(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
log.warn("Interrupted while waiting");
}
}
}