Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
rhbz1215274 - refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Huang committed May 20, 2015
1 parent 5895ecd commit f8f7703
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
Expand Up @@ -15,6 +15,7 @@
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zanata.client.commands.OptionsUtil;
import org.zanata.client.commands.PushPullCommand;
import org.zanata.client.commands.PushPullType;
import org.zanata.client.config.LocaleList;
Expand All @@ -33,6 +34,7 @@
import org.zanata.util.HashUtil;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.sun.jersey.api.client.ClientResponse;
Expand Down Expand Up @@ -215,9 +217,15 @@ && getOpts().getPullType() == PushPullType.Source) {
eTagCache.clear();
}

Map<String, Map<LocaleId, TranslatedPercent>> statsMap = null;
// this stats map will have docId as key, the value is another map with
// localeId as key and translated percent as value. It's optional if we
// require statistics to determine which file to pull. In cases where
// statistics is not required, i.e. pull source only or minimum percent
// is set to 0, this will be Optional.absence().
Optional<Map<String, Map<LocaleId, TranslatedPercent>>> optionalStats =
Optional.absent();
if (pullTarget && getOpts().getMinDocPercent() > 0) {
statsMap = getDocsTranslatedPercent();
optionalStats = Optional.of(getDocsTranslatedPercent());
}

for (String qualifiedDocName : docsToPull) {
Expand Down Expand Up @@ -245,7 +253,7 @@ && getOpts().getPullType() == PushPullType.Source) {
strat.getTransFileToWrite(localDocName,
locMapping);

if (shouldPullThisLocale(statsMap, localDocName, locale)) {
if (shouldPullThisLocale(optionalStats, localDocName, locale)) {
pullDocForLocale(strat, doc, localDocName, docUri,
createSkeletons, locMapping, transFile);
} else {
Expand Down Expand Up @@ -368,16 +376,16 @@ protected void pullDocForLocale(PullStrategy strat, Resource doc,
}

private boolean shouldPullThisLocale(
Map<String, Map<LocaleId, TranslatedPercent>> statsMap,
Optional<Map<String, Map<LocaleId, TranslatedPercent>>> optinalStats,
String localDocName, LocaleId serverLocale) {
int minDocPercent = getOpts().getMinDocPercent();
if (log.isDebugEnabled() && statsMap != null) {
if (log.isDebugEnabled() && optinalStats.isPresent()) {
log.debug("{} for locale {} is translated {}%", localDocName,
serverLocale, statsMap.get(localDocName).get(serverLocale)
serverLocale, optinalStats.get().get(localDocName).get(serverLocale)
.translatedPercent);
}
return statsMap == null
|| statsMap.get(localDocName).get(serverLocale)
return !optinalStats.isPresent()
|| optinalStats.get().get(localDocName).get(serverLocale)
.isAboveThreshold(minDocPercent);
}

Expand Down Expand Up @@ -505,6 +513,8 @@ public TranslatedPercent(long total, long translated, long approved) {
}

public boolean isAboveThreshold(int minimumPercent) {
// if minimum percent is 100, we will compare exact number so that
// rounding issue won't affect the result
if (minimumPercent == 100) {
return total == translated + approved;
} else {
Expand Down
Expand Up @@ -26,6 +26,7 @@
import org.zanata.client.commands.BooleanValueHandler;
import org.zanata.client.commands.ZanataCommand;
import org.zanata.client.commands.PushPullType;
import com.google.common.base.Preconditions;

/**
* @author Sean Flanigan <a
Expand Down Expand Up @@ -171,8 +172,11 @@ public int getMinDocPercent() {
return this.minDocPercent;
}

@Option(name = "--min-doc-percent", aliases = "-m", usage = "When specified, will only pull down translation files that have translated or better statistics above the specified percentage")
@Option(name = "--min-doc-percent", metaVar = "PERCENT", usage = "Accepts integer number from 0 to 100. Only pull translation documents which are at least PERCENT % completed.")
public void setMinDocPercent(int minDocPercent) {
Preconditions
.checkArgument(minDocPercent >= 0 || minDocPercent <= 100,
"--min-doc-percent should be an integer from 0 to 100");
this.minDocPercent = minDocPercent;
}

Expand Down
Expand Up @@ -27,6 +27,7 @@
import org.zanata.client.commands.pull.PullOptions;
import org.zanata.client.commands.pull.RawPullCommand;

import com.google.common.base.Preconditions;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

/**
Expand Down Expand Up @@ -108,7 +109,8 @@ public abstract class AbstractPullMojo extends
private boolean continueAfterError = false;

/**
* When specified, will only pull down translation files that have translated or better statistics above the specified percentage.
* Accepts integer number from 0 to 100. Only pull translation documents
* which are at least PERCENT % completed.
*
* @parameter expression="$zanata.minDocPercent}" default-value="0"
*/
Expand All @@ -119,6 +121,9 @@ public abstract class AbstractPullMojo extends
*/
public AbstractPullMojo() {
super();
Preconditions
.checkArgument(minDocPercent >= 0 || minDocPercent <= 100,
"zanata.minDocPercent should be an integer from 0 to 100");
}

public PushPullCommand<PullOptions> initCommand() {
Expand Down

0 comments on commit f8f7703

Please sign in to comment.