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

Commit

Permalink
Merge branch 'integration/master' of github.com:zanata/zanata into in…
Browse files Browse the repository at this point in the history
…tegration/master
  • Loading branch information
Alex Eng committed Oct 2, 2012
2 parents 2d1d0cb + fb7abb1 commit ca75637
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 65 deletions.
47 changes: 47 additions & 0 deletions zanata-war/src/main/java/org/zanata/dao/TextFlowTargetDAO.java
Expand Up @@ -2,16 +2,21 @@

import java.util.List;

import javax.persistence.EntityManager;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.hibernate.exception.ConstraintViolationException;
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.jboss.seam.annotations.Transactional;
import org.zanata.common.ContentState;
import org.zanata.common.LocaleId;
import org.zanata.model.HDocument;
Expand All @@ -25,6 +30,9 @@
public class TextFlowTargetDAO extends AbstractDAOImpl<HTextFlowTarget, Long>
{

@In
private EntityManager entityManager;

public TextFlowTargetDAO()
{
super(HTextFlowTarget.class);
Expand Down Expand Up @@ -224,4 +232,43 @@ public ScrollableResults findMatchingTranslations(HDocument document, HLocale lo
q.setCacheable(false); // TODO does it make sense to cache scrollable results?
return q.scroll();
}

/**
* Look up the {@link HTextFlowTarget} for the given hLocale in hTextFlow,
* creating a new one if none is present.
*
* @param hTextFlow The parent text flow.
* @param hLocale The locale for the text flow target.
*/
public HTextFlowTarget getOrCreateTarget(HTextFlow hTextFlow, HLocale hLocale)
{
try
{
return tryGetOrCreateTarget(hTextFlow, hLocale);
}
catch (ConstraintViolationException e)
{
entityManager.refresh(hTextFlow);
return tryGetOrCreateTarget(hTextFlow, hLocale);
}
}

/**
* Throws a constraing Violation exception if for some reason the text flow target has already been created.
*
* @see TextFlowTargetDAO#getOrCreateTarget(org.zanata.model.HTextFlow, org.zanata.model.HLocale)
*/
private HTextFlowTarget tryGetOrCreateTarget(HTextFlow hTextFlow, HLocale hLocale)
{
HTextFlowTarget hTextFlowTarget = hTextFlow.getTargets().get(hLocale.getId());

if (hTextFlowTarget == null)
{
hTextFlowTarget = new HTextFlowTarget(hTextFlow, hLocale);
hTextFlowTarget.setVersionNum(0); // this will be incremented when content is set (below)
hTextFlow.getTargets().put(hLocale.getId(), hTextFlowTarget);
entityManager.flush();
}
return hTextFlowTarget;
}
}
54 changes: 36 additions & 18 deletions zanata-war/src/main/java/org/zanata/rest/service/ResourceUtils.java
Expand Up @@ -6,9 +6,9 @@
import org.jboss.seam.Component;
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.jboss.seam.annotations.intercept.BypassInterceptors;
import org.jboss.seam.log.Log;
import org.jboss.seam.log.Logging;
import org.zanata.ApplicationConfiguration;
Expand Down Expand Up @@ -46,6 +46,7 @@
import org.zanata.util.StringUtil;

import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
Expand Down Expand Up @@ -75,7 +76,6 @@
@Name("resourceUtils")
@Scope(ScopeType.STATELESS)
@AutoCreate
@BypassInterceptors
public class ResourceUtils
{
/**
Expand Down Expand Up @@ -109,6 +109,9 @@ public class ResourceUtils

private static Properties pluralForms;

@In
private EntityManager entityManager;

@PostConstruct
public void create()
{
Expand Down Expand Up @@ -331,14 +334,14 @@ private boolean transferFromResourceExtensions(ExtensionSet<AbstractResourceMeta
* a single locale
*
* @param from
* @param to
* @param doc
* @param enabledExtensions
* @param locale
* @param mergeType
* @return
* @see #transferToTranslationsResourceExtensions
*/
public boolean transferFromTranslationsResourceExtensions(ExtensionSet<TranslationsResourceExtension> from, HDocument to, Set<String> enabledExtensions, HLocale locale, MergeType mergeType)
public boolean transferFromTranslationsResourceExtensions(ExtensionSet<TranslationsResourceExtension> from, HDocument doc, Set<String> enabledExtensions, HLocale locale, MergeType mergeType)
{
boolean changed = false;
if (enabledExtensions.contains(PoTargetHeader.ID))
Expand All @@ -347,32 +350,47 @@ public boolean transferFromTranslationsResourceExtensions(ExtensionSet<Translati
if (fromTargetHeader != null)
{
log.debug("found PO header for locale: {0}", locale);
HPoTargetHeader toTargetHeader = to.getPoTargetHeaders().get(locale);
if (toTargetHeader == null)
try
{
changed = true;
toTargetHeader = new HPoTargetHeader();
toTargetHeader.setTargetLanguage(locale);
toTargetHeader.setDocument(to);
transferFromPoTargetHeader(fromTargetHeader, toTargetHeader, MergeType.IMPORT); // return
// value
// not
// needed
to.getPoTargetHeaders().put(locale, toTargetHeader);
changed = tryGetOrCreateTargetHeader(doc, locale, mergeType, changed, fromTargetHeader);
}
else
catch (org.hibernate.exception.ConstraintViolationException e)
{
changed |= transferFromPoTargetHeader(fromTargetHeader, toTargetHeader, mergeType);
entityManager.refresh(doc);
changed = tryGetOrCreateTargetHeader(doc, locale, mergeType, changed, fromTargetHeader);
}
}
else
{
changed |= to.getPoTargetHeaders().remove(locale) != null;
changed |= doc.getPoTargetHeaders().remove(locale) != null;
}
}
return changed;
}

private boolean tryGetOrCreateTargetHeader(HDocument doc, HLocale locale, MergeType mergeType, boolean changed, PoTargetHeader fromTargetHeader)
{
HPoTargetHeader toTargetHeader = doc.getPoTargetHeaders().get(locale);
if (toTargetHeader == null)
{
changed = true;
toTargetHeader = new HPoTargetHeader();
toTargetHeader.setTargetLanguage(locale);
toTargetHeader.setDocument(doc);
transferFromPoTargetHeader(fromTargetHeader, toTargetHeader, MergeType.IMPORT); // return
// value
// not
// needed
doc.getPoTargetHeaders().put(locale, toTargetHeader);
entityManager.flush();
}
else
{
changed |= transferFromPoTargetHeader(fromTargetHeader, toTargetHeader, mergeType);
}
return changed;
}

/**
* Transfers enabled extensions from DTO TextFlowTarget to HTextFlowTarget
*
Expand Down
Expand Up @@ -162,10 +162,6 @@ protected Void work() throws Exception
{
// Relax doc Id restriction
checkDocument = false;
if( options.getDocIdMismatchAction() == DOWNGRADE_TO_FUZZY )
{
copyState = NeedReview;
}
// Every result will match context, and project
// Assuming Phase 1 ran, results will have non-matching doc Ids
copyCount += copyTransPass(document, locale, checkContext, checkProject, checkDocument, options);
Expand All @@ -174,16 +170,22 @@ protected Void work() throws Exception
{
// Relax project restriction
checkProject = false;
if( options.getProjectMismatchAction() == DOWNGRADE_TO_FUZZY )
{
copyState = NeedReview;
}
// Every result will match context
// Assuming above phases, results will have non-matching project
// Assuming above phase: either doc Id didn't match, or the user explicitly rejected non-matching documents
copyCount += copyTransPass(document, locale, checkContext, checkProject, checkDocument, options);
}
if( options.getContextMismatchAction() != REJECT )
{
// Relax context restriction
checkContext = false;
// Assuming above phases:
// Context does not match
// either doc Id didn't match, or the user explicitly rejected non-matching documents
// and either Project didn't match, or the user explicitly rejected non-matching projects
copyCount += copyTransPass(document, locale, checkContext, checkProject, checkDocument, options);
}
if( options.getContextMismatchAction() != REJECT )
{
// Relax context restriction
checkContext = false;
Expand All @@ -200,6 +202,8 @@ protected Void work() throws Exception

log.info("copyTrans: {0} {1} translations for document \"{2}{3}\" ", copyCount, locale.getLocaleId(), document.getPath(), document.getName());

log.info("copyTrans: {0} {1} translations for document \"{2}{3}\" ", copyCount, locale.getLocaleId(), document.getPath(), document.getName());

return null;
}
}.workInTransaction();
Expand All @@ -225,7 +229,7 @@ private int copyTransPass( HDocument document, HLocale locale, boolean checkCont
{
HTextFlowTarget matchingTarget = (HTextFlowTarget)results.get(0);
HTextFlow originalTf = (HTextFlow)results.get(1);
HTextFlowTarget hTarget = originalTf.getTargets().get( locale.getId() );
HTextFlowTarget hTarget = textFlowTargetDAO.getOrCreateTarget(originalTf, locale);
ContentState copyState = determineContentState(
originalTf.getResId().equals(matchingTarget.getTextFlow().getResId()),
originalTf.getDocument().getProjectIteration().getProject().getId().equals( matchingTarget.getTextFlow().getDocument().getProjectIteration().getProject().getId() ),
Expand All @@ -234,18 +238,6 @@ private int copyTransPass( HDocument document, HLocale locale, boolean checkCont

if( shouldOverwrite(hTarget, copyState) )
{
// Create the new translation (or overwrite non-approved ones)
if (hTarget == null)
{
hTarget = new HTextFlowTarget(originalTf, locale);
hTarget.setVersionNum(1);
originalTf.getTargets().put(locale.getId(), hTarget);
}
else
{
// increase the versionNum
hTarget.setVersionNum(hTarget.getVersionNum() + 1);
}

// NB we don't touch creationDate
hTarget.setTextFlowRevision(originalTf.getRevision());
Expand Down
Expand Up @@ -25,6 +25,7 @@
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.core.Events;
import org.zanata.ApplicationConfiguration;
import org.zanata.dao.DocumentDAO;
import org.zanata.dao.ProjectIterationDAO;
Expand All @@ -33,6 +34,7 @@
import org.zanata.model.HProjectIteration;
import org.zanata.rest.dto.resource.Resource;
import org.zanata.rest.service.ResourceUtils;
import org.zanata.rest.service.TranslatedDocResourceService;
import org.zanata.service.CopyTransService;
import org.zanata.service.DocumentService;
import org.zanata.service.LocaleService;
Expand Down
Expand Up @@ -116,7 +116,7 @@ public TranslationResult translate(LocaleId localeId, TransUnitUpdateRequest tra
{
HTextFlow hTextFlow = entityManager.find(HTextFlow.class, translateRequest.getTransUnitId().getValue());
HLocale hLocale = validateLocale(localeId, hTextFlow);
HTextFlowTarget hTextFlowTarget = getOrCreateTarget(hTextFlow, hLocale);
HTextFlowTarget hTextFlowTarget = textFlowTargetDAO.getOrCreateTarget(hTextFlow, hLocale);

if (translateRequest.getBaseTranslationVersion() != hTextFlowTarget.getVersionNum())
{
Expand Down Expand Up @@ -154,7 +154,7 @@ public List<TranslationResult> translate(LocaleId localeId, List<TransUnitUpdate
for (TransUnitUpdateRequest request : translationRequests)
{
HTextFlow hTextFlow = entityManager.find(HTextFlow.class, request.getTransUnitId().getValue());
HTextFlowTarget hTextFlowTarget = getOrCreateTarget(hTextFlow, hLocale);
HTextFlowTarget hTextFlowTarget = textFlowTargetDAO.getOrCreateTarget(hTextFlow, hLocale);
if (request.hasTargetComment())
{
hTextFlowTarget.setComment(new HSimpleComment(request.getTargetComment()));
Expand Down Expand Up @@ -207,23 +207,6 @@ private HLocale validateLocale(LocaleId localeId, HTextFlow sampleHTextFlow) thr
return localeServiceImpl.validateLocaleByProjectIteration(localeId, projectSlug, projectIteration.getSlug());
}

/**
* Look up the {@link HTextFlowTarget} for the given hLocale in hTextFlow,
* creating a new one if none is present.
*/
private HTextFlowTarget getOrCreateTarget(HTextFlow hTextFlow, HLocale hLocale)
{
HTextFlowTarget hTextFlowTarget = hTextFlow.getTargets().get(hLocale.getId());

if (hTextFlowTarget == null)
{
hTextFlowTarget = new HTextFlowTarget(hTextFlow, hLocale);
hTextFlowTarget.setVersionNum(0); // this will be incremented when content is set (below)
hTextFlow.getTargets().put(hLocale.getId(), hTextFlowTarget);
}
return hTextFlowTarget;
}

private boolean translate(@Nonnull HTextFlowTarget hTextFlowTarget, @Nonnull List<String> contentsToSave, ContentState requestedState, int nPlurals)
{
boolean targetChanged = false;
Expand Down Expand Up @@ -399,7 +382,7 @@ public List<String> translateAllInDoc(String projectSlug, String iterationSlug,
}
else
{
HTextFlowTarget hTarget = textFlow.getTargets().get(hLocale.getId());
HTextFlowTarget hTarget = textFlowTargetDAO.getOrCreateTarget(textFlow, hLocale);
boolean targetChanged = false;
if (hTarget == null)
{
Expand Down Expand Up @@ -572,7 +555,7 @@ public List<TranslationResult> revertTranslations(LocaleId localeId, List<TransU

TransUnitId tuId = info.getTransUnit().getId();
HTextFlow hTextFlow = entityManager.find(HTextFlow.class, tuId.getValue());
HTextFlowTarget hTextFlowTarget = getOrCreateTarget(hTextFlow, hLocale);
HTextFlowTarget hTextFlowTarget = textFlowTargetDAO.getOrCreateTarget(hTextFlow, hLocale);

//check that version has not advanced
// TODO probably also want to check that source has not been updated
Expand Down
Expand Up @@ -50,6 +50,7 @@ protected void prepareResources()
{
seam.reset();
seam.ignoreNonResolvable()
.use("entityManager", getEm())
.use("session", getSession())
.use("identity", mockIdentity)
.useImpl(LocaleServiceImpl.class)
Expand Down
Expand Up @@ -82,11 +82,11 @@ protected void prepareDBUnitOperations()
public void initializeSeam()
{
seam.reset()
.use("entityManager", getEm())
.use("session", getSession())
.use(JpaIdentityStore.AUTHENTICATED_USER, seam.autowire(AccountDAO.class).getByUsername("demo"))
.useImpl(LocaleServiceImpl.class)
.ignoreNonResolvable();
.use("entityManager", getEm())
.use("session", getSession())
.use(JpaIdentityStore.AUTHENTICATED_USER, seam.autowire(AccountDAO.class).getByUsername("demo"))
.useImpl(LocaleServiceImpl.class)
.ignoreNonResolvable();
}

/**
Expand Down

0 comments on commit ca75637

Please sign in to comment.