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

Commit

Permalink
Merge pull request #188 from zanata/rhbz959834
Browse files Browse the repository at this point in the history
Make config download maintainable
  • Loading branch information
Patrick Huang committed Sep 20, 2013
2 parents 8f8cd62 + 14d8093 commit 49b2450
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 178 deletions.
Expand Up @@ -51,48 +51,27 @@ public class ConfigurationAction implements Serializable
@In
private ConfigurationService configurationServiceImpl;

public void getData()
public void downloadGeneralConfig()
{
getData(false, null);
respondWithFile(configurationServiceImpl.getGeneralConfig(projectSlug, iterationSlug));
}

public void getData(HLocale locale)
public void downloadOfflineTranslationConfig(HLocale locale)
{
getData(false, locale);
respondWithFile(configurationServiceImpl.getConfigForOfflineTranslation(projectSlug, iterationSlug, locale));
}

public void getOfflinePoConfigData()
{
getOfflinePoConfigData(null);
}

public void getOfflinePoConfigData(HLocale locale)
{
getData(true, locale);
}


private void getData(boolean useOfflinePo, HLocale locale)
private void respondWithFile(String configFileContents)
{
HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
response.setContentType("application/xml");
response.addHeader("Content-disposition", "attachment; filename=\""
+ this.configurationServiceImpl.getConfigurationFileName() + "\"");
+ configurationServiceImpl.getConfigurationFileName() + "\"");
response.setCharacterEncoding("UTF-8");
try
{
ServletOutputStream os = response.getOutputStream();

if (locale == null)
{
os.write(configurationServiceImpl.getConfigurationFileContents(projectSlug,
iterationSlug, useOfflinePo).getBytes());
}
else
{
os.write(configurationServiceImpl.getConfigurationFileContents(projectSlug,
iterationSlug, locale, useOfflinePo).getBytes());
}
os.write(configFileContents.getBytes());
os.flush();
os.close();
FacesContext.getCurrentInstance().responseComplete();
Expand Down
Expand Up @@ -10,7 +10,6 @@
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.security.Restrict;
import org.jboss.seam.security.Identity;
import org.zanata.ApplicationConfiguration;
import org.zanata.async.AsyncTaskHandle;
import org.zanata.async.tasks.ZipFileBuildTask;
import org.zanata.dao.ProjectIterationDAO;
Expand All @@ -29,9 +28,6 @@ public class ProjectIterationZipFileAction implements Serializable
@In
AsyncTaskManagerService asyncTaskManagerServiceImpl;

@In
private ApplicationConfiguration applicationConfiguration;

private String projectSlug;

private String iterationSlug;
Expand All @@ -42,7 +38,7 @@ public class ProjectIterationZipFileAction implements Serializable

@Begin(join = true)
@Restrict("#{s:hasPermission(projectIterationZipFileAction.projectIteration, 'download-all')}")
public void prepareIterationZipFile(boolean offlinePo)
public void prepareIterationZipFile(boolean isPoProject)
{
if( this.zipFilePrepHandle != null && !this.zipFilePrepHandle.isDone() )
{
Expand All @@ -51,8 +47,8 @@ public void prepareIterationZipFile(boolean offlinePo)
}

// Start a zip file task
ZipFileBuildTask task = new ZipFileBuildTask(this.projectSlug, this.iterationSlug, this.localeId,
Identity.instance().getCredentials().getUsername(), offlinePo);
ZipFileBuildTask task = new ZipFileBuildTask(projectSlug, iterationSlug, localeId,
Identity.instance().getCredentials().getUsername(), isPoProject);

String taskId = asyncTaskManagerServiceImpl.startTask(task);
zipFilePrepHandle = asyncTaskManagerServiceImpl.getHandle(taskId);
Expand Down
Expand Up @@ -20,12 +20,18 @@
*/
package org.zanata.async.tasks;

import com.google.common.base.Optional;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.jboss.seam.Component;
import org.zanata.ApplicationConfiguration;
import org.zanata.adapter.po.PoWriter2;
import org.zanata.async.AsyncTaskHandle;
import org.zanata.async.AsyncTask;
import org.zanata.async.AsyncTaskHandle;
import org.zanata.common.LocaleId;
import org.zanata.dao.DocumentDAO;
import org.zanata.dao.LocaleDAO;
Expand All @@ -41,13 +47,7 @@
import org.zanata.service.impl.ConfigurationServiceImpl;
import org.zanata.service.impl.FileSystemServiceImpl;

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import com.google.common.base.Optional;

/**
* This is an asynchronous task to build a zip file containing all files for a Project
Expand All @@ -63,17 +63,17 @@ public class ZipFileBuildTask implements AsyncTask<String, AsyncTaskHandle<Strin
private final String iterationSlug;
private final String localeId;
private final String userName;
private final boolean isOfflinePo;
private final boolean isPoProject;

private AsyncTaskHandle<String> handle;

public ZipFileBuildTask(String projectSlug, String iterationSlug, String localeId, String userName, boolean offlinePo)
public ZipFileBuildTask(String projectSlug, String iterationSlug, String localeId, String userName, boolean isPoProject)
{
this.projectSlug = projectSlug;
this.iterationSlug = iterationSlug;
this.localeId = localeId;
this.userName = userName;
isOfflinePo = offlinePo;
this.isPoProject = isPoProject;
}

@Override
Expand Down Expand Up @@ -108,7 +108,6 @@ public String call() throws Exception
TextFlowTargetDAO textFlowTargetDAO = (TextFlowTargetDAO)Component.getInstance(TextFlowTargetDAO.class);
FileSystemService fileSystemService = (FileSystemService) Component.getInstance(FileSystemServiceImpl.class);
ConfigurationService configurationService = (ConfigurationService)Component.getInstance(ConfigurationServiceImpl.class);
ApplicationConfiguration applicationConfiguration = (ApplicationConfiguration)Component.getInstance(ApplicationConfiguration.class);

final String projectDirectory = projectSlug + "-" + iterationSlug + "/";
final HLocale hLocale = localeDAO.findByLocaleId(new LocaleId(localeId));
Expand All @@ -119,7 +118,7 @@ public String call() throws Exception
final FileOutputStream output = new FileOutputStream( downloadFile );
final ZipOutputStream zipOutput = new ZipOutputStream(output);
zipOutput.setMethod(ZipOutputStream.DEFLATED);
final PoWriter2 poWriter = new PoWriter2(false, isOfflinePo);
final PoWriter2 poWriter = new PoWriter2(false, !isPoProject);
final Set<String> extensions = new HashSet<String>();

extensions.add("gettext");
Expand All @@ -133,9 +132,8 @@ public String call() throws Exception
// Add the config file at the root of the project directory
String configFilename = projectDirectory + configurationService.getConfigurationFileName();
zipOutput.putNextEntry(new ZipEntry(configFilename));
zipOutput.write(configurationService.getConfigurationFileContents(
projectSlug, iterationSlug, hLocale, isOfflinePo, applicationConfiguration.getServerPath())
.getBytes());
zipOutput.write(configurationService.getConfigForOfflineTranslation(
projectSlug, iterationSlug, hLocale).getBytes());
zipOutput.closeEntry();
getHandle().increaseProgress(1);

Expand Down
Expand Up @@ -25,56 +25,26 @@
public interface ConfigurationService
{
/**
* Returns the contents of the configuration for a given project.
* Get a standard config file for a project-version.
*
* @param projectSlug
* @param iterationSlug
* @param useOfflinePo true to use 'offlinepo' instead of the configured project type
* @return The configuration file contents for the given project and
* iteration.
* @return contents of the config file
*/
String getConfigurationFileContents(String projectSlug, String iterationSlug, boolean useOfflinePo);
String getGeneralConfig(String projectSlug, String iterationSlug);

/**
* Returns the contents of the configuration for a given project. Use this method
* when the server path of the zanata server needs to be customized
*
* @param projectSlug
* @param iterationSlug
* @param useOfflinePo true to use 'offlinepo' instead of the configured project type
* @param serverPath
* @return The configuration file contents for the given project and
* iteration.
*/
String getConfigurationFileContents(String projectSlug, String iterationSlug, boolean useOfflinePo, String serverPath);

/**
* Returns the contents of the configuration for a given project with a
* single locale.
* Get a standard config file for dealing with a single locale for a project-version.
*
* @param projectSlug
* @param iterationSlug
* @param locale
* @param useOfflinePo
* @return The configuration file contents for the given project and
* iteration.
* @return contents of the config file
*/
String getConfigurationFileContents(String projectSlug, String iterationSlug, HLocale locale, boolean useOfflinePo);
String getSingleLocaleConfig(String projectSlug, String versionSlug, HLocale locale);

/**
* Returns the contents of the configuration for a given project with a
* single locale. Use this method when the server path of the zanata server
* needs to be customized.
* Get a config file for a single locale, with project type adjusted to be appropriate for
* offline translation.
*
* @param projectSlug
* @param iterationSlug
* @param locale
* @param useOfflinePo true to use 'offlinepo' instead of the configured project type
* @param serverPath
* @return The configuration file contents for the given project and
* iteration.
* @return contents of the config file
*/
String getConfigurationFileContents(String projectSlug, String iterationSlug, HLocale locale, boolean useOfflinePo, String serverPath);
String getConfigForOfflineTranslation(String projectSlug, String versionSlug, HLocale locale);

/**
* Returns the default configuration file Name.
Expand Down

0 comments on commit 49b2450

Please sign in to comment.