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

Commit

Permalink
Merge branch 'release' into integration/master
Browse files Browse the repository at this point in the history
Conflicts:
	zanata-war/src/main/java/org/zanata/service/impl/CopyTransServiceImpl.java
  • Loading branch information
Carlos A. Munoz committed Sep 5, 2013
2 parents 85cbb4c + 6166bea commit fd5944f
Show file tree
Hide file tree
Showing 4 changed files with 354 additions and 115 deletions.
Expand Up @@ -198,8 +198,8 @@ public ScrollableResults findMatchingTranslations(HDocument document, HLocale lo
"or :finalState != (select t.state from HTextFlowTarget t where t.textFlow = textFlow and t.locale = :locale) ) " +
// Do not reuse its own translations
"and match.textFlow != textFlow " +
// Do not reuse matches from obsolete entities (document, iteration, project)
"and match.textFlow.document.obsolete = false " +
// Do not reuse matches from obsolete entities (iteration, project)
// Obsolete document translations ARE reused
"and match.textFlow.document.projectIteration.status != :obsoleteEntityStatus " +
"and match.textFlow.document.projectIteration.project.status != :obsoleteEntityStatus "
);
Expand Down
Expand Up @@ -27,7 +27,6 @@

import java.util.List;

import javax.annotation.Nullable;
import javax.persistence.EntityManager;

import org.hibernate.HibernateException;
Expand Down Expand Up @@ -56,8 +55,12 @@
import org.zanata.rest.service.TranslatedDocResourceService;
import org.zanata.service.CopyTransService;
import org.zanata.service.LocaleService;
import com.google.common.collect.ImmutableList;
import com.google.common.base.Optional;

import lombok.AllArgsConstructor;
import lombok.Getter;

//TODO unit test suite for this class

@Name("copyTransServiceImpl")
Expand Down Expand Up @@ -281,69 +284,100 @@ private int copyTransPass( HDocument document, HLocale locale, boolean checkCont
}

/**
* Determines the content state a copied translation should have.
* Determines the content state for a translation given a list of rules and their evaluation result, and
* the initial state that it was copied as.
*
* @param contextMatches Whether the copied TextFlow's context matches the target TextFlow's context or not.
* @param projectMatches Whether the copied TextFlow's project matches the target TextFlow's context or not.
* @param docIdMatches Whether the copied TextFlow's docId matches the target TextFlow's context or not.
* @param options The copy trans options being used.
* @param requireTranslationReview Whether the project being copied to requires review or not.
* @param matchingTargetState The state of the match found by copy trans.
* @return The content state that the copied translation should have. May return null if the translation should not
* be copied at all.
* @param pairs List of evaluated rules and the match result.
* @param initialState The initial content state of the translation that the content was copied from.
* @return The content state that the copied translation should have. 'New' indicates that the translation
* should not copied.
*/
private @Nullable
ContentState determineContentState(boolean contextMatches, boolean projectMatches, boolean docIdMatches,
HCopyTransOptions options, boolean requireTranslationReview, ContentState matchingTargetState)
private static
ContentState determineContentStateFromMatchRules(List<MatchRulePair> pairs, ContentState initialState)
{
// Everything matches, and requires approval
if (requireTranslationReview && matchingTargetState.isApproved() && projectMatches && contextMatches && docIdMatches)
if( pairs.isEmpty() )
{
return initialState;
}

MatchRulePair p = pairs.get(0);
if( shouldReject(p.getMatchResult(), p.getRuleAction()) )
{
return Approved;
return New;
}
// Everything matches, and does not require approval
else if( matchingTargetState.isApproved() && projectMatches && contextMatches && docIdMatches )
else if( shouldDowngradeToFuzzy(p.getMatchResult(), p.getRuleAction()) )
{
return Translated;
return determineContentStateFromMatchRules(pairs.subList(1, pairs.size()), NeedReview);
}
else
{
return determineContentStateFromMatchRules(pairs.subList(1, pairs.size()), initialState);
}
// Everything else
ContentState state = Translated;
state = getExpectedContentState(contextMatches, options.getContextMismatchAction(), state);
state = getExpectedContentState(projectMatches, options.getProjectMismatchAction(), state);
state = getExpectedContentState(docIdMatches, options.getDocIdMismatchAction(), state);
return state;
}

/**
* Gets the content state that is expected for a translation when evaluated against a single copy trans
* condition.
* Copy Trans conditions are of the form: Is it the same project? Is it the same documentId? ... etc.
* Determines the content state for a translation given a list of rules and their evaluation result.
*
* @param match Whether the condition holds or not (is there a match?)
* @param action The action to take when the condition matches.
* @param currentState The current state of the translation.
* @return The content state that is expected the copied translation to have.
* @param pairs List of evaluated rules and their result.
* @param requireTranslationReview Whether the project to copy the translation to requires translations to be reviewed.
* @param matchingTargetState The initial state of the matching translation (the translation that will be copied over).
* @return The content state that the copied translation should have. 'New' indicates that the translation
* should not copied.
*/
public @Nullable
ContentState getExpectedContentState( boolean match, HCopyTransOptions.ConditionRuleAction action,
ContentState currentState )
static
ContentState determineContentStateFromRuleList(List<MatchRulePair> pairs,
boolean requireTranslationReview, ContentState matchingTargetState)
{
if( currentState == null )
{
return null;
}
else if( !match )
{
if( action == DOWNGRADE_TO_FUZZY )
{
return NeedReview;
}
else if( action == REJECT )
{
return null;
}
}
return currentState;
assert matchingTargetState == Translated || matchingTargetState == Approved;
return determineContentStateFromMatchRules(pairs, requireTranslationReview ? matchingTargetState : Translated);
}

/**
* Determines the content state that a copied translation should have.
*
* @param contextMatches Indicates if there is a context match between the match and copy-target text flows.
* @param projectMatches Indicates if there is a project match between the match and copy-target text flows.
* @param docIdMatches Indicates if there is a doc Id match between the match and copy-target text flows.
* @param options The copy trans options that are effective.
* @param requireTranslationReview Whether the project to copy the translation to requires translations to be reviewed.
* @param matchingTargetState he initial state of the matching translation (the translation that will be copied over).
* @return The content state that the copied translation should have. 'New' indicates that the translation
* should not copied.
*/
static
ContentState determineContentState(boolean contextMatches, boolean projectMatches, boolean docIdMatches,
HCopyTransOptions options, boolean requireTranslationReview, ContentState matchingTargetState)
{
List rules =
ImmutableList.of(new MatchRulePair(contextMatches, options.getContextMismatchAction()),
new MatchRulePair(projectMatches, options.getProjectMismatchAction()),
new MatchRulePair(docIdMatches, options.getDocIdMismatchAction()));

return determineContentStateFromRuleList(rules, requireTranslationReview, matchingTargetState);
}

/**
* Indicates if a copied translation should be rejected.
*
* @param match The result of a match evaluating condition.
* @param action The selected action to take based on the result of the condition evaluation.
* @return True, if the translation should be outright rejected based on the evaluated condition.
*/
static boolean shouldReject(boolean match, HCopyTransOptions.ConditionRuleAction action )
{
return !match && action == REJECT;
}

/**
* Indicates if a copied translation should be downgraded to fuzzy/
*
* @param match The result of a match evaluating condition.
* @param action The selected action to take based on the result of the condition evaluation.
* @return True, if the translation should be downgraded to fuzzy based on the evaluated condition.
*/
static boolean shouldDowngradeToFuzzy(boolean match, HCopyTransOptions.ConditionRuleAction action)
{
return !match && action == DOWNGRADE_TO_FUZZY;
}

@Override
Expand Down Expand Up @@ -410,7 +444,7 @@ public void copyTransForIteration(HProjectIteration iteration, HCopyTransOptions
*/
private static boolean shouldOverwrite(HTextFlowTarget currentlyStored, ContentState matchState)
{
if( matchState == null )
if( matchState == New )
{
return false;
}
Expand All @@ -435,4 +469,16 @@ else if( currentlyStored.getState() == New )
}
return true;
}

/**
* Holds the result of a match evaluation in the form of a boolean, and the corresponding action
* to be taken for the result.
*/
@AllArgsConstructor
@Getter
static final class MatchRulePair
{
private final Boolean matchResult;
private final HCopyTransOptions.ConditionRuleAction ruleAction;
}
}
37 changes: 37 additions & 0 deletions zanata-war/src/test/java/org/zanata/SlowTest.java
@@ -0,0 +1,37 @@
/*
* Copyright 2013, 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;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Marker annotation to identify tests that might be potentially slow to run.
*
* @author Carlos Munoz <a href="mailto:camunoz@redhat.com">camunoz@redhat.com</a>
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface SlowTest
{
}

0 comments on commit fd5944f

Please sign in to comment.