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

Commit

Permalink
#issue 265: copyTrans performance fix
Browse files Browse the repository at this point in the history
  • Loading branch information
hding committed May 13, 2011
1 parent 9891036 commit 221b1bb
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 84 deletions.
Expand Up @@ -25,6 +25,7 @@
import javax.persistence.Id;


import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Type;
import org.hibernate.validator.NotNull;

Expand All @@ -33,6 +34,7 @@
*
*/
@Entity
@BatchSize(size = 20)
public class HSimpleComment
{

Expand Down
Expand Up @@ -38,6 +38,7 @@


import org.hibernate.annotations.AccessType;
import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.NaturalId;
import org.hibernate.annotations.Type;
Expand Down Expand Up @@ -198,7 +199,7 @@ public void setDocument(HDocument document)
}

// TODO use orphanRemoval=true: requires JPA 2.0
@OneToOne(optional = true, cascade = CascadeType.ALL)
@OneToOne(optional = true, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
@JoinColumn(name = "comment_id")
public HSimpleComment getComment()
Expand Down Expand Up @@ -241,6 +242,7 @@ public void setHistory(Map<Integer, HTextFlowHistory> history)

@OneToMany(cascade = CascadeType.ALL, mappedBy = "textFlow")
@MapKey(name = "locale")
@BatchSize(size = 10)
public Map<HLocale, HTextFlowTarget> getTargets()
{
if (targets == null)
Expand Down
Expand Up @@ -23,6 +23,7 @@
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
Expand Down Expand Up @@ -177,7 +178,7 @@ public void setContent(String content)
}

// TODO use orphanRemoval=true: requires JPA 2.0
@OneToOne(optional = true, cascade = CascadeType.ALL)
@OneToOne(optional = true, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
@JoinColumn(name = "comment_id")
public HSimpleComment getComment()
Expand Down
Expand Up @@ -31,6 +31,7 @@
import javax.persistence.OneToOne;


import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.NaturalId;
import org.hibernate.annotations.Type;
import org.zanata.model.HSimpleComment;
Expand All @@ -43,6 +44,7 @@
* @see org.zanata.rest.dto.extensions.gettext.PotEntryHeader
*/
@Entity
@BatchSize(size = 20)
public class HPotEntryData implements Serializable
{

Expand Down
Expand Up @@ -86,15 +86,11 @@ public HTextFlowTarget findLatestEquivalentTranslation(HTextFlow textFlow, HLoca
"and t.textFlow.content = :content " +
"and t.textFlow.document.docId =:docId " +
"and t.locale = :locale " +
"and t.textFlow.document.projectIteration.project =:project " +
"and t.textFlow.document.projectIteration !=:iteration " +
"and t.state = :state " +
"order by t.lastChanged desc")
.setParameter("content", textFlow.getContent())
.setParameter("docId", textFlow.getDocument().getDocId())
.setParameter("locale", locale)
.setParameter("project", textFlow.getDocument().getProjectIteration().getProject())
.setParameter("iteration", textFlow.getDocument().getProjectIteration())
.setParameter("resid", textFlow.getResId())
.setParameter("state", ContentState.Approved)
.setMaxResults(1).uniqueResult();
Expand Down
Expand Up @@ -60,6 +60,7 @@
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Transactional;
import org.jboss.seam.core.Events;
import org.jboss.seam.log.Log;
import org.jboss.seam.log.Logging;
import org.jboss.seam.security.Identity;
Expand Down Expand Up @@ -105,7 +106,7 @@ public class TranslationResourcesService implements TranslationResourcesResource

public static final String SERVICE_PATH = ProjectIterationService.SERVICE_PATH + "/r";

private static boolean haveLoggedCopyTransWarning = false;
public static final String EVENT_COPY_TRANS = "org.zanata.rest.service.copyTrans";

@PathParam("projectSlug")
private String projectSlug;
Expand All @@ -118,7 +119,7 @@ public class TranslationResourcesService implements TranslationResourcesResource
private Set<String> extensions;

@QueryParam("copyTrans")
@DefaultValue("false")
@DefaultValue("true")
private boolean copytrans;


Expand Down Expand Up @@ -279,7 +280,7 @@ public Response post(InputStream messageBody)

if (copytrans)
{
copyClosestEquivalentTranslation(document);
copyClosestEquivalentTranslation(document.getId(), entity.getName(), projectSlug, iterationSlug);
}

EntityTag etag = eTagUtils.generateETagForDocument(hProjectIteration, document.getDocId(), extensions);
Expand Down Expand Up @@ -426,7 +427,7 @@ else if (document.isObsolete())

if (copytrans)
{
copyClosestEquivalentTranslation(document);
copyClosestEquivalentTranslation(document.getId(), entity.getName(), projectSlug, iterationSlug);
}

log.debug("put resource successfully");
Expand Down Expand Up @@ -887,82 +888,10 @@ private void validateExtensions(String... extensions)
}
}

@SuppressWarnings("unused")
private String createComment(HTextFlowTarget target)
{
String authorname;
HDocument document = target.getTextFlow().getDocument();
String projectname = document.getProjectIteration().getProject().getName();
String version = document.getProjectIteration().getSlug();
String documentid = document.getDocId();
if (target.getLastModifiedBy()!=null)
{
authorname = target.getLastModifiedBy().getName();
}
else
{
authorname = "";
}

return "translation auto-copied from project "+projectname+", version "+version+", document "+documentid+", author "+authorname;
}

public void copyClosestEquivalentTranslation(HDocument document)
public void copyClosestEquivalentTranslation(Long docId, String name, String projectSlug, String iterationSlug)
{
if (!haveLoggedCopyTransWarning)
{
log.warn("Automatic copying of translations is disabled in this version of Zanata. (This warning will only appear once per startup.)");
haveLoggedCopyTransWarning = true;
}
return;
/*
int copyCount = 0;
List<HLocale> localelist = localeDAO.findAllActive();
for (HTextFlow textFlow : document.getTextFlows())
{
// find closest equivalent textflowtarget
for (HLocale locale : localelist)
{
HTextFlowTarget hTarget = textFlow.getTargets().get(locale);
if (hTarget != null && hTarget.getState() == ContentState.Approved)
continue;
HTextFlowTarget oldTFT = textFlowTargetDAO.findLatestEquivalentTranslation(textFlow, locale);
if (oldTFT != null)
{
if (hTarget == null)
{
hTarget = new HTextFlowTarget(textFlow, locale);
hTarget.setVersionNum(1);
textFlow.getTargets().put(locale, hTarget);
}
else
{
// DB trigger will copy old value to history table, if we change the versionNum
hTarget.setVersionNum(hTarget.getVersionNum()+1);
}
// NB we don't touch creationDate
hTarget.setLastChanged(oldTFT.getLastChanged());
hTarget.setLastModifiedBy(oldTFT.getLastModifiedBy());
hTarget.setContent(oldTFT.getContent());
hTarget.setState(oldTFT.getState());
HSimpleComment hcomment = hTarget.getComment();
if (hcomment == null)
{
hcomment = new HSimpleComment();
hTarget.setComment(hcomment);
}
hcomment.setComment(createComment(oldTFT));
textFlowTargetDAO.makePersistent(hTarget);
++copyCount;
}
}
}
textFlowTargetDAO.flush();
log.info("copied {0} existing translations for document \"{1}{2}\"", copyCount, document.getPath(), document.getName());
*/
Events.instance().raiseTransactionSuccessEvent(EVENT_COPY_TRANS, docId, projectSlug, iterationSlug);
}

}
@@ -0,0 +1,156 @@
/*
* Copyright 2010, 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.service.impl;

import java.util.List;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Observer;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.log.Log;
import org.jboss.seam.transaction.Transaction;
import org.zanata.common.ContentState;
import org.zanata.dao.DocumentDAO;
import org.zanata.dao.TextFlowTargetDAO;
import org.zanata.model.HDocument;
import org.zanata.model.HLocale;
import org.zanata.model.HSimpleComment;
import org.zanata.model.HTextFlow;
import org.zanata.model.HTextFlowTarget;
import org.zanata.rest.service.TranslationResourcesService;
import org.zanata.service.LocaleService;

@Name("copyTransService")
@AutoCreate
@Scope(ScopeType.STATELESS)
public class CopyTransService
{
@In
private LocaleService localeServiceImpl;
@In
private TextFlowTargetDAO textFlowTargetDAO;
@In
private DocumentDAO documentDAO;
@Logger
Log log;


@Observer(TranslationResourcesService.EVENT_COPY_TRANS)
public void execute(Long docId, String project, String iterationSlug)
{
log.info("start copy trans for: " + docId);
List<HLocale> localelist = localeServiceImpl.getSupportedLangugeByProjectIteration(project, iterationSlug);
HDocument document = documentDAO.findById(docId, true);

for (HLocale locale : localelist)
{
log.info("locale:" + locale.getLocaleId().getId());
copyTransForLocale(document, locale);
}
}

private String createComment(HTextFlowTarget target)
{
String authorname;
HDocument document = target.getTextFlow().getDocument();
String projectname = document.getProjectIteration().getProject().getName();
String version = document.getProjectIteration().getSlug();
String documentid = document.getDocId();
if (target.getLastModifiedBy() != null)
{
authorname = target.getLastModifiedBy().getName();
}
else
{
authorname = "";
}

return "translation auto-copied from project " + projectname + ", version " + version + ", document " + documentid + ", author " + authorname;
}

private void copyTransForLocale(HDocument document, HLocale locale)
{
try
{
Transaction.instance().begin();
int copyCount = 0;
for (HTextFlow textFlow : document.getTextFlows())
{
HTextFlowTarget hTarget = textFlow.getTargets().get(locale);
if (hTarget != null && hTarget.getState() == ContentState.Approved)
continue;
HTextFlowTarget oldTFT = textFlowTargetDAO.findLatestEquivalentTranslation(textFlow, locale);
if (oldTFT != null)
{
log.info("find one:" + oldTFT.getContent());
if (hTarget == null)
{
hTarget = new HTextFlowTarget(textFlow, locale);
hTarget.setVersionNum(1);
textFlow.getTargets().put(locale, hTarget);
}
else
{
// DB trigger will copy old value to history table, if we
// change the versionNum
hTarget.setVersionNum(hTarget.getVersionNum() + 1);
}
// NB we don't touch creationDate
hTarget.setLastChanged(oldTFT.getLastChanged());
hTarget.setLastModifiedBy(oldTFT.getLastModifiedBy());
hTarget.setContent(oldTFT.getContent());
hTarget.setState(oldTFT.getState());
HSimpleComment hcomment = hTarget.getComment();
if (hcomment == null)
{
hcomment = new HSimpleComment();
hTarget.setComment(hcomment);
}
hcomment.setComment(createComment(oldTFT));
textFlowTargetDAO.makePersistent(hTarget);
++copyCount;
}
}
textFlowTargetDAO.flush();
Transaction.instance().commit();

log.info("copied {0} existing translations for document \"{1}{2}\" ", copyCount, document.getPath(), document.getName());

}
catch (Exception e)
{
log.warn(e, e);
try
{
Transaction.instance().rollback();
}
catch (Exception i)
{
log.warn(i, i);
}
}
}

}

0 comments on commit 221b1bb

Please sign in to comment.