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

Commit

Permalink
use instance helpers in source upload methods (mikado method)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmason committed Jul 16, 2013
1 parent 425a325 commit 48bf83b
Showing 1 changed file with 19 additions and 36 deletions.
55 changes: 19 additions & 36 deletions zanata-war/src/main/java/org/zanata/file/SourceDocumentUpload.java
Expand Up @@ -91,8 +91,7 @@ public Response tryUploadSourceFile(String projectSlug, String iterationSlug, St
try
{
GlobalDocumentId id = new GlobalDocumentId(projectSlug, iterationSlug, docId);
checkSourceUploadPreconditions(projectSlug, iterationSlug, docId, uploadForm, identity,
session, projectIterationDAO, translationFileServiceImpl);
checkSourceUploadPreconditions(projectSlug, iterationSlug, docId, uploadForm);

Optional<File> tempFile;
int totalChunks;
Expand Down Expand Up @@ -124,18 +123,15 @@ public Response tryUploadSourceFile(String projectSlug, String iterationSlug, St
if (uploadForm.getFileType().equals(".pot"))
{
InputStream potStream = getInputStream(tempFile, uploadForm);
parsePotFile(potStream, projectSlug, iterationSlug, docId, uploadForm,
translationFileServiceImpl, documentServiceImpl, documentDAO);
parsePotFile(potStream, projectSlug, iterationSlug, docId, uploadForm);
}
else
{
if (!tempFile.isPresent())
{
tempFile = Optional.of(persistTempFileFromUpload(uploadForm, translationFileServiceImpl));
}
processAdapterFile(tempFile.get(), projectSlug, iterationSlug, docId, uploadForm,
virusScanner, documentDAO, documentServiceImpl, translationFileServiceImpl,
identity);
processAdapterFile(tempFile.get(), projectSlug, iterationSlug, docId, uploadForm);
}
if (tempFile.isPresent())
{
Expand All @@ -157,46 +153,42 @@ public Response tryUploadSourceFile(String projectSlug, String iterationSlug, St
}
}

private static void checkSourceUploadPreconditions(String projectSlug, String iterationSlug, String docId,
DocumentFileUploadForm uploadForm,
ZanataIdentity identity,
Session session,
ProjectIterationDAO projectIterationDAO,
TranslationFileService translationFileServiceImpl)
private void checkSourceUploadPreconditions(String projectSlug, String iterationSlug, String docId,
DocumentFileUploadForm uploadForm)
{
try
{
checkUploadPreconditions(projectSlug, iterationSlug, docId, uploadForm, identity, projectIterationDAO, session);
checkSourceUploadAllowed(projectSlug, iterationSlug, identity, projectIterationDAO);
checkSourceUploadAllowed(projectSlug, iterationSlug);
}
catch (AuthorizationException e)
{
throw new ChunkUploadException(Status.UNAUTHORIZED, e.getMessage());
}
checkValidSourceUploadType(uploadForm, translationFileServiceImpl);
checkValidSourceUploadType(uploadForm);
}

private static void checkSourceUploadAllowed(String projectSlug, String iterationSlug, ZanataIdentity identity, ProjectIterationDAO projIterDAO)
private void checkSourceUploadAllowed(String projectSlug, String iterationSlug)
{
if (!isDocumentUploadAllowed(projectSlug, iterationSlug, identity, projIterDAO))
if (!isDocumentUploadAllowed(projectSlug, iterationSlug))
{
throw new ChunkUploadException(Status.FORBIDDEN,
"You do not have permission to upload source documents to project-version \""
+ projectSlug + ":" + iterationSlug + "\".");
}
}

private static boolean isDocumentUploadAllowed(String projectSlug, String iterationSlug, ZanataIdentity identity, ProjectIterationDAO projIterDAO)
private boolean isDocumentUploadAllowed(String projectSlug, String iterationSlug)
{
HProjectIteration projectIteration = projIterDAO.getBySlug(projectSlug, iterationSlug);
HProjectIteration projectIteration = projectIterationDAO.getBySlug(projectSlug, iterationSlug);
return projectIteration.getStatus() == EntityStatus.ACTIVE && projectIteration.getProject().getStatus() == EntityStatus.ACTIVE
&& identity != null && identity.hasPermission("import-template", projectIteration);
}

private static void checkValidSourceUploadType(DocumentFileUploadForm uploadForm, TranslationFileService transFileService)
private void checkValidSourceUploadType(DocumentFileUploadForm uploadForm)
{
if (!uploadForm.getFileType().equals(".pot")
&& !transFileService.hasAdapterFor(DocumentType.typeFor(uploadForm.getFileType())))
&& !translationFileServiceImpl.hasAdapterFor(DocumentType.typeFor(uploadForm.getFileType())))
{
throw new ChunkUploadException(Status.BAD_REQUEST,
"The type \"" + uploadForm.getFileType() + "\" specified in form parameter 'type' "
Expand Down Expand Up @@ -227,13 +219,8 @@ private static Response sourceUploadSuccessResponse(boolean isNewDocument, int a
return response;
}

private static void processAdapterFile(@Nonnull File tempFile, String projectSlug, String iterationSlug,
String docId, DocumentFileUploadForm uploadForm,
VirusScanner virusScanner,
DocumentDAO documentDAO,
DocumentService documentServiceImpl,
TranslationFileService translationFileServiceImpl,
ZanataIdentity identity)
private void processAdapterFile(@Nonnull File tempFile, String projectSlug, String iterationSlug,
String docId, DocumentFileUploadForm uploadForm)
{
String name = projectSlug + ":" + iterationSlug + ":" + docId;
try
Expand Down Expand Up @@ -299,22 +286,18 @@ private static void processAdapterFile(@Nonnull File tempFile, String projectSlu
translationFileServiceImpl.removeTempFile(tempFile);
}

private static void parsePotFile(InputStream potStream, String projectSlug, String iterationSlug,
String docId, DocumentFileUploadForm uploadForm,
TranslationFileService translationFileServiceImpl,
DocumentService documentServiceImpl,
DocumentDAO documentDAO)
private void parsePotFile(InputStream potStream, String projectSlug, String iterationSlug,
String docId, DocumentFileUploadForm uploadForm)
{
Resource doc;
doc = translationFileServiceImpl.parseUpdatedPotFile(potStream, docId, uploadForm.getFileType(),
useOfflinePo(projectSlug, iterationSlug, docId, documentDAO, translationFileServiceImpl));
useOfflinePo(projectSlug, iterationSlug, docId));
doc.setLang(new LocaleId("en-US"));
// TODO Copy Trans values
documentServiceImpl.saveDocument(projectSlug, iterationSlug, doc, new StringSet(ExtensionType.GetText.toString()), false);
}

private static boolean useOfflinePo(String projectSlug, String iterationSlug, String docId,
DocumentDAO documentDAO, TranslationFileService translationFileServiceImpl)
private boolean useOfflinePo(String projectSlug, String iterationSlug, String docId)
{
return !isNewDocument(projectSlug, iterationSlug, docId, documentDAO) && !translationFileServiceImpl.isPoDocument(projectSlug, iterationSlug, docId);
}
Expand Down

0 comments on commit 48bf83b

Please sign in to comment.