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

Commit

Permalink
Merge branch 'pot-upload' into integration/master
Browse files Browse the repository at this point in the history
Conflicts:
	server/zanata-war/src/main/java/org/zanata/rest/service/ResourceUtils.java
  • Loading branch information
Carlos Munoz committed May 4, 2012
2 parents cc082ee + ee9a134 commit 4d98d77
Show file tree
Hide file tree
Showing 25 changed files with 1,324 additions and 389 deletions.
15 changes: 5 additions & 10 deletions server/zanata-war/src/main/java/org/zanata/action/ProjectHome.java
Expand Up @@ -47,6 +47,7 @@
import org.zanata.model.HLocale;
import org.zanata.model.HProjectIteration;
import org.zanata.service.LocaleService;
import org.zanata.service.SlugEntityService;

@Name("projectHome")
public class ProjectHome extends SlugHome<HIterationProject>
Expand Down Expand Up @@ -75,6 +76,9 @@ public class ProjectHome extends SlugHome<HIterationProject>
@In
LocaleService localeServiceImpl;

@In
SlugEntityService slugEntityServiceImpl;

@In(create = true)
ProjectDAO projectDAO;

Expand Down Expand Up @@ -116,16 +120,7 @@ public boolean validateSlug(String slug, String componentId)

public boolean isSlugAvailable(String slug)
{
try
{
getEntityManager().createQuery("from HProject p where p.slug = :slug").setParameter("slug", slug).getSingleResult();
return false;
}
catch (NoResultException e)
{
// pass
}
return true;
return slugEntityServiceImpl.isSlugAvailable(slug, HIterationProject.class);
}

@Override
Expand Down
Expand Up @@ -40,12 +40,15 @@
import org.zanata.model.HProjectIteration;
import org.zanata.rest.StringSet;
import org.zanata.rest.dto.extensions.ExtensionType;
import org.zanata.rest.dto.resource.Resource;
import org.zanata.rest.dto.resource.TextFlowTarget;
import org.zanata.rest.dto.resource.TranslationsResource;
import org.zanata.security.ZanataIdentity;
import org.zanata.service.DocumentService;
import org.zanata.service.TranslationFileService;
import org.zanata.service.TranslationService;

import java.io.InputStream;
import java.util.Collection;
import java.util.List;

Expand Down Expand Up @@ -80,17 +83,23 @@ public class ProjectIterationFilesAction
@In
private TranslationService translationServiceImpl;

@In
private DocumentService documentServiceImpl;

private List<HDocument> iterationDocuments;

private String documentNameFilter;

private TranslationFileUploadHelper fileUploadHelper;
private TranslationFileUploadHelper translationFileUpload;

private DocumentFileUploadHelper documentFileUpload;


public void initialize()
{
this.iterationDocuments = this.documentDAO.getAllByProjectIteration(this.projectSlug, this.iterationSlug);
this.fileUploadHelper = new TranslationFileUploadHelper();
this.translationFileUpload = new TranslationFileUploadHelper();
this.documentFileUpload = new DocumentFileUploadHelper();
}

public HLocale getLocale()
Expand Down Expand Up @@ -119,36 +128,58 @@ public TransUnitWords getTransUnitWordsForDocument(HDocument doc)
}

@Restrict("#{projectIterationFilesAction.fileUploadAllowed}")
public void uploadFile()
public void uploadTranslationFile()
{
TranslationsResource transRes = null;
try
{
// process the file
transRes = this.translationFileServiceImpl.parseTranslationFile(this.fileUploadHelper.getFileContents(),
this.fileUploadHelper.getFileName());
transRes = this.translationFileServiceImpl.parseTranslationFile(this.translationFileUpload.getFileContents(),
this.translationFileUpload.getFileName());

// translate it
Collection<TextFlowTarget> resourcesNotFound =
this.translationServiceImpl.translateAll(this.projectSlug, this.iterationSlug, this.fileUploadHelper.getDocId(),
this.translationServiceImpl.translateAll(this.projectSlug, this.iterationSlug, this.translationFileUpload.getDocId(),
new LocaleId(this.localeId), transRes, new StringSet(ExtensionType.GetText.toString()),
this.fileUploadHelper.getMergeTranslations() ? MergeType.AUTO : MergeType.IMPORT);
this.translationFileUpload.getMergeTranslations() ? MergeType.AUTO : MergeType.IMPORT);

StringBuilder facesInfoMssg = new StringBuilder("File {0} uploaded.");
if( resourcesNotFound.size() > 0 )
{
facesInfoMssg.append(" There were some warnings, see below.");
}

FacesMessages.instance().add(Severity.INFO, facesInfoMssg.toString(), this.fileUploadHelper.getFileName());
FacesMessages.instance().add(Severity.INFO, facesInfoMssg.toString(), this.translationFileUpload.getFileName());
for( TextFlowTarget nf : resourcesNotFound )
{
FacesMessages.instance().add(Severity.WARN, "Could not find text flow for message: {0}", nf.getContents());
}
}
catch (ZanataServiceException zex)
{
FacesMessages.instance().add(Severity.ERROR, "Invalid file type: {0}", this.fileUploadHelper.getFileName());
FacesMessages.instance().add(Severity.ERROR, zex.getMessage(), this.translationFileUpload.getFileName());
}
}

@Restrict("#{projectIterationFilesAction.documentUploadAllowed}")
public void uploadDocumentFile()
{
try
{
Resource doc = this.translationFileServiceImpl.parseDocumentFile(this.documentFileUpload.getFileContents(),
this.documentFileUpload.getDocumentPath(), this.documentFileUpload.getFileName());

// TODO Copy Trans values
// Extensions are hard-coded to GetText, since it is the only supported format at the time
this.documentServiceImpl.saveDocument(this.projectSlug, this.iterationSlug,
this.documentFileUpload.getDocumentPath() + doc.getName(), doc, new StringSet(ExtensionType.GetText.toString()),
false);

FacesMessages.instance().add(Severity.INFO, "Document file {0} uploaded.", this.documentFileUpload.getFileName());
}
catch (ZanataServiceException zex)
{
FacesMessages.instance().add(Severity.ERROR, zex.getMessage(), this.documentFileUpload.getFileName());
}
}

Expand All @@ -161,6 +192,13 @@ public boolean isFileUploadAllowed()
&& identity != null && identity.hasPermission("modify-translation", projectIteration, hLocale);
}

public boolean isDocumentUploadAllowed()
{
HProjectIteration projectIteration = this.projectIterationDAO.getBySlug(projectSlug, iterationSlug);
return projectIteration.getStatus() == EntityStatus.ACTIVE
&& identity != null && identity.hasPermission("import-template", projectIteration);
}

public List<HDocument> getIterationDocuments()
{
return iterationDocuments;
Expand Down Expand Up @@ -211,8 +249,110 @@ public void setDocumentNameFilter(String documentNameFilter)
this.documentNameFilter = documentNameFilter;
}

public TranslationFileUploadHelper getFileUploadHelper()
public TranslationFileUploadHelper getTranslationFileUpload()
{
return translationFileUpload;
}

public DocumentFileUploadHelper getDocumentFileUpload()
{
return documentFileUpload;
}

/**
* Helper class to upload translation files.
*/
public static class TranslationFileUploadHelper
{
private String docId;

private InputStream fileContents;

private String fileName;

private boolean mergeTranslations = true; // Merge by default


public String getDocId()
{
return docId;
}

public void setDocId(String docId)
{
this.docId = docId;
}

public InputStream getFileContents()
{
return fileContents;
}

public void setFileContents(InputStream fileContents)
{
this.fileContents = fileContents;
}

public String getFileName()
{
return fileName;
}

public void setFileName(String fileName)
{
this.fileName = fileName;
}

public boolean getMergeTranslations()
{
return mergeTranslations;
}

public void setMergeTranslations(boolean mergeTranslations)
{
this.mergeTranslations = mergeTranslations;
}
}

/**
* Helper class to upload documents.
*/
public static class DocumentFileUploadHelper
{
return fileUploadHelper;
private InputStream fileContents;

private String fileName;

private String documentPath;

public InputStream getFileContents()
{
return fileContents;
}

public void setFileContents(InputStream fileContents)
{
this.fileContents = fileContents;
}

public String getFileName()
{
return fileName;
}

public void setFileName(String fileName)
{
this.fileName = fileName;
}

public String getDocumentPath()
{
return documentPath;
}

public void setDocumentPath(String documentPath)
{
this.documentPath = documentPath;
}
}
}
Expand Up @@ -40,6 +40,7 @@
import org.zanata.model.HLocale;
import org.zanata.model.HProjectIteration;
import org.zanata.service.LocaleService;
import org.zanata.service.SlugEntityService;

@Name("projectIterationHome")
public class ProjectIterationHome extends SlugHome<HProjectIteration>
Expand All @@ -51,13 +52,19 @@ public class ProjectIterationHome extends SlugHome<HProjectIteration>

private String slug;
private String projectSlug;

@In(required = false)
Map<String, String> iterationCustomizedItems;

@In(required = false)
private Boolean iterationOverrideLocales;

@In
LocaleService localeServiceImpl;

@In
SlugEntityService slugEntityServiceImpl;

@Logger
Log log;

Expand Down Expand Up @@ -125,16 +132,7 @@ public boolean validateSlug(String slug, String componentId)

public boolean isSlugAvailable(String slug)
{
try
{
getEntityManager().createQuery("from HProjectIteration t where t.slug = :slug and t.project.slug = :projectSlug").setParameter("slug", slug).setParameter("projectSlug", projectSlug).getSingleResult();
return false;
}
catch (NoResultException e)
{
// pass
}
return true;
return slugEntityServiceImpl.isSlugAvailable(slug, HProjectIteration.class);
}

@Override
Expand Down
Expand Up @@ -20,12 +20,6 @@
*/
package org.zanata.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.core.EntityTag;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
Expand All @@ -52,6 +46,11 @@
import org.zanata.model.StatusCount;
import org.zanata.util.HashUtil;

import javax.ws.rs.core.EntityTag;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Name("projectIterationDAO")
@AutoCreate
@Scope(ScopeType.STATELESS)
Expand Down

0 comments on commit 4d98d77

Please sign in to comment.