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

Commit

Permalink
Create different CopyTransTask implementations for different targets.
Browse files Browse the repository at this point in the history
Only for Document and Iteration so far, but could be extended to others i nthe future.
  • Loading branch information
Carlos A. Munoz committed Aug 26, 2013
1 parent eace4aa commit a5806f3
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 52 deletions.
Expand Up @@ -29,6 +29,8 @@
import org.jboss.seam.annotations.Startup;
import org.jboss.seam.security.Identity;
import org.zanata.async.tasks.CopyTransTask;
import org.zanata.async.tasks.DocumentCopyTransTask;
import org.zanata.async.tasks.IterationCopyTransTask;
import org.zanata.model.HCopyTransOptions;
import org.zanata.model.HDocument;
import org.zanata.model.HProjectIteration;
Expand Down Expand Up @@ -106,7 +108,7 @@ public void startCopyTrans( HDocument document, HCopyTransOptions options )
}

CopyTransProcessKey key = CopyTransProcessKey.getKey(document);
CopyTransTask task = new CopyTransTask(document, options);
CopyTransTask task = new DocumentCopyTransTask(document, options);
asyncTaskManagerServiceImpl.startTask(task, key);
}

Expand All @@ -122,7 +124,7 @@ public void startCopyTrans( HProjectIteration iteration, HCopyTransOptions optio
}

CopyTransProcessKey key = CopyTransProcessKey.getKey(iteration);
CopyTransTask task = new CopyTransTask(iteration, options);
CopyTransTask task = new IterationCopyTransTask(iteration, options);
asyncTaskManagerServiceImpl.startTask(task, key);
}

Expand Down
63 changes: 13 additions & 50 deletions zanata-war/src/main/java/org/zanata/async/tasks/CopyTransTask.java
Expand Up @@ -40,30 +40,19 @@

/**
* Asynchronous Task that runs copy trans.
* This task can run against a document or against a project iteration depending
* on which constructor is used.
* Subclasses should allow for running copy trans against different targets.
*
* @author Carlos Munoz <a href="mailto:camunoz@redhat.com">camunoz@redhat.com</a>
*/
public class CopyTransTask implements AsyncTask<Void, CopyTransTask.CopyTransTaskHandle>
public abstract class CopyTransTask implements AsyncTask<Void, CopyTransTask.CopyTransTaskHandle>
{
private HDocument document;

private HProjectIteration projectIteration;

private HCopyTransOptions copyTransOptions;
protected HCopyTransOptions copyTransOptions;

private final CopyTransTaskHandle handle = new CopyTransTaskHandle();

public CopyTransTask(HDocument document, HCopyTransOptions copyTransOptions)
{
this.document = document;
this.copyTransOptions = copyTransOptions;
}

public CopyTransTask(HProjectIteration projectIteration, HCopyTransOptions copyTransOptions)
public CopyTransTask(HCopyTransOptions copyTransOptions)
{
this.projectIteration = projectIteration;
this.copyTransOptions = copyTransOptions;
}

Expand All @@ -73,52 +62,26 @@ public CopyTransTaskHandle getHandle()
return handle;
}

/**
* @return The maximum progress for the copy trans task.
*/
protected abstract int getMaxProgress();

protected abstract void callCopyTrans();

@Override
public Void call() throws Exception
{
getHandle().startTiming();
getHandle().setTriggeredBy(Identity.instance().getPrincipal().getName());
calculateMaxProgress();

CopyTransService copyTransServiceImpl =
(CopyTransService) Component.getInstance(CopyTransServiceImpl.class);
getHandle().setMaxProgress( getMaxProgress() );

if( projectIteration != null )
{
copyTransServiceImpl.copyTransForIteration( projectIteration, copyTransOptions );
}
else
{
copyTransServiceImpl.copyTransForDocument( document );
}
callCopyTrans();

getHandle().finishTiming();
return null;
}

private void calculateMaxProgress()
{
LocaleService localeService = (LocaleService)Component.getInstance(LocaleServiceImpl.class);
if( projectIteration != null )
{
List<HLocale> localeList =
localeService.getSupportedLangugeByProjectIteration(projectIteration.getProject().getSlug(),
projectIteration.getSlug());

getHandle().setMaxProgress( projectIteration.getDocuments().size() * localeList.size() );
}
else
{
// Set the max progress only if it hasn't been set yet
List<HLocale> localeList =
localeService.getSupportedLangugeByProjectIteration(document.getProjectIteration().getProject().getSlug(),
document.getProjectIteration().getSlug());

getHandle().setMaxProgress(localeList.size());
}

}

public static class CopyTransTaskHandle extends TimedAsyncHandle<Void>
{
@Getter
Expand Down
@@ -0,0 +1,69 @@
/*
* 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.async.tasks;

import java.util.List;

import org.jboss.seam.Component;
import org.jboss.seam.security.Identity;
import org.zanata.model.HCopyTransOptions;
import org.zanata.model.HDocument;
import org.zanata.model.HLocale;
import org.zanata.service.CopyTransService;
import org.zanata.service.LocaleService;
import org.zanata.service.impl.CopyTransServiceImpl;
import org.zanata.service.impl.LocaleServiceImpl;

/**
* Copy Trans task that runs copy trans on a single document and all languages
* available to its iteration.
*
* @author Carlos Munoz <a href="mailto:camunoz@redhat.com">camunoz@redhat.com</a>
*/
public class DocumentCopyTransTask extends CopyTransTask
{
private HDocument document;

public DocumentCopyTransTask(HDocument document, HCopyTransOptions copyTransOptions)
{
super(copyTransOptions);
this.document = document;
}

@Override
protected int getMaxProgress()
{
LocaleService localeService = (LocaleService)Component.getInstance(LocaleServiceImpl.class);
List<HLocale> localeList =
localeService.getSupportedLangugeByProjectIteration(document.getProjectIteration().getProject().getSlug(),
document.getProjectIteration().getSlug());

return localeList.size();
}

@Override
protected void callCopyTrans()
{
CopyTransService copyTransServiceImpl =
(CopyTransService) Component.getInstance(CopyTransServiceImpl.class);
copyTransServiceImpl.copyTransForDocument(document, copyTransOptions);
}
}
@@ -0,0 +1,68 @@
/*
* 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.async.tasks;

import java.util.List;

import org.jboss.seam.Component;
import org.jboss.seam.security.Identity;
import org.zanata.model.HCopyTransOptions;
import org.zanata.model.HLocale;
import org.zanata.model.HProjectIteration;
import org.zanata.service.CopyTransService;
import org.zanata.service.LocaleService;
import org.zanata.service.impl.CopyTransServiceImpl;
import org.zanata.service.impl.LocaleServiceImpl;

/**
* Copy Trans task that runs copy trans for a whole project iteration and all languages.
*
* @author Carlos Munoz <a href="mailto:camunoz@redhat.com">camunoz@redhat.com</a>
*/
public class IterationCopyTransTask extends CopyTransTask
{
private HProjectIteration projectIteration;

public IterationCopyTransTask(HProjectIteration projectIteration, HCopyTransOptions options)
{
super(options);
this.projectIteration = projectIteration;
}

@Override
protected int getMaxProgress()
{
LocaleService localeService = (LocaleService)Component.getInstance(LocaleServiceImpl.class);
List<HLocale> localeList =
localeService.getSupportedLangugeByProjectIteration(projectIteration.getProject().getSlug(),
projectIteration.getSlug());

return projectIteration.getDocuments().size() * localeList.size();
}

@Override
protected void callCopyTrans()
{
CopyTransService copyTransServiceImpl =
(CopyTransService) Component.getInstance(CopyTransServiceImpl.class);
copyTransServiceImpl.copyTransForIteration(projectIteration, copyTransOptions);
}
}

0 comments on commit a5806f3

Please sign in to comment.