Beispiel #1
0
  public GetTransUnitListHandler createGetTransUnitListHandlerWithBehavior(
      DocumentId documentId,
      List<HTextFlow> hTextFlows,
      HLocale hLocale,
      int startIndex,
      int count) {
    // @formatter:off
    GetTransUnitListHandler handler =
        SeamAutowire.instance()
            .use("identity", identity)
            .use("textFlowDAO", textFlowDAO)
            .use("textFlowSearchServiceImpl", textFlowSearchServiceImpl)
            .use("localeServiceImpl", localeServiceImpl)
            .use("resourceUtils", resourceUtils)
            .autowire(GetTransUnitListHandler.class);
    // @formatter:on

    int maxSize = Math.min(startIndex + count, hTextFlows.size());
    when(textFlowDAO.getTextFlows(documentId, startIndex, count))
        .thenReturn(hTextFlows.subList(startIndex, maxSize));
    when(localeServiceImpl.validateLocaleByProjectIteration(
            any(LocaleId.class), anyString(), anyString()))
        .thenReturn(hLocale);
    when(resourceUtils.getNumPlurals(any(HDocument.class), any(HLocale.class))).thenReturn(1);

    // trans unit navigation index handler
    when(textFlowDAO.getNavigationByDocumentId(
            eq(documentId.getId()),
            eq(hLocale),
            isA(ResultTransformer.class),
            isA(FilterConstraints.class)))
        .thenReturn(hTextFlows);
    return handler;
  }
  @Override
  @Transactional
  public HDocument saveDocument(
      String projectSlug,
      String iterationSlug,
      Resource sourceDoc,
      Set<String> extensions,
      boolean copyTrans) {
    // Only active iterations allow the addition of a document
    HProjectIteration hProjectIteration = projectIterationDAO.getBySlug(projectSlug, iterationSlug);

    // Check permission
    identity.checkPermission(hProjectIteration, "import-template");

    String docId = sourceDoc.getName();

    HDocument document = documentDAO.getByDocIdAndIteration(hProjectIteration, docId);
    HLocale hLocale = this.localeServiceImpl.validateSourceLocale(sourceDoc.getLang());

    boolean changed = false;
    int nextDocRev;
    if (document == null) { // must be a create operation
      nextDocRev = 1;
      changed = true;
      // TODO check that entity name matches id parameter
      document = new HDocument(sourceDoc.getName(), sourceDoc.getContentType(), hLocale);
      document.setProjectIteration(hProjectIteration);
      hProjectIteration.getDocuments().put(docId, document);
      document = documentDAO.makePersistent(document);
    } else if (document.isObsolete()) { // must also be a create operation
      nextDocRev = document.getRevision() + 1;
      changed = true;
      document.setObsolete(false);
      // not sure if this is needed
      hProjectIteration.getDocuments().put(docId, document);
    } else { // must be an update operation
      nextDocRev = document.getRevision() + 1;
    }

    changed |=
        resourceUtils.transferFromResource(sourceDoc, document, extensions, hLocale, nextDocRev);
    documentDAO.flush();

    long actorId = authenticatedAccount.getPerson().getId();
    if (changed) {
      documentUploadedEvent.fireAfterSuccess(
          new DocumentUploadedEvent(actorId, document.getId(), true, hLocale.getLocaleId()));
      clearStatsCacheForUpdatedDocument(document);
    }

    if (copyTrans && nextDocRev == 1) {
      copyTranslations(document);
    }

    return document;
  }
  public boolean isLanguageNameValid() {
    this.languageNameValidationMessage = null; // reset
    this.languageNameWarningMessage = null; // reset

    if (language.length() > LENGTH_LIMIT) {
      this.uLocale = null;
      this.languageNameValidationMessage = msgs.get("jsf.language.validation.Invalid");
      return false;
    }

    // Cannot use FacesMessages as they are request scoped.
    // Cannot use UI binding as they don't work in Page scoped beans
    // TODO Use the new (since 1.7) FlashScopeBean

    // Check that locale Id is syntactically valid
    LocaleId localeId;
    try {
      localeId = new LocaleId(language);
    } catch (IllegalArgumentException iaex) {
      this.languageNameValidationMessage = msgs.get("jsf.language.validation.Invalid");
      return false;
    }

    // check for already registered languages
    if (localeServiceImpl.localeExists(localeId)) {
      this.languageNameValidationMessage = msgs.get("jsf.language.validation.Existing");
      return false;
    }

    // Check for plural forms
    if (resourceUtils.getPluralForms(localeId, false) == null) {
      this.languageNameWarningMessage = msgs.get("jsf.language.validation.UnknownPluralForm");
    }

    // Check for similar already registered languages (warning)
    List<HLocale> similarLangs = localeDAO.findBySimilarLocaleId(localeId);
    if (similarLangs.size() > 0) {
      this.languageNameWarningMessage =
          msgs.get("jsf.language.validation.SimilarLocaleFound")
              + similarLangs.get(0).getLocaleId().getId();
    }

    return true;
  }
  @Override
  public String call() throws Exception {
    // Needed Components
    DocumentDAO documentDAO = (DocumentDAO) Component.getInstance(DocumentDAO.class);
    LocaleDAO localeDAO = (LocaleDAO) Component.getInstance(LocaleDAO.class);
    ResourceUtils resourceUtils = (ResourceUtils) Component.getInstance(ResourceUtils.class);
    TextFlowTargetDAO textFlowTargetDAO =
        (TextFlowTargetDAO) Component.getInstance(TextFlowTargetDAO.class);
    FileSystemService fileSystemService =
        (FileSystemService) Component.getInstance(FileSystemServiceImpl.class);
    ConfigurationService configurationService =
        (ConfigurationService) Component.getInstance(ConfigurationServiceImpl.class);

    final String projectDirectory = projectSlug + "-" + iterationSlug + "/";
    final HLocale hLocale = localeDAO.findByLocaleId(new LocaleId(localeId));
    final String mappedLocale = hLocale.getLocaleId().getId();
    final String localeDirectory = projectDirectory + mappedLocale + "/";

    final File downloadFile = fileSystemService.createDownloadStagingFile("zip");
    final FileOutputStream output = new FileOutputStream(downloadFile);
    final ZipOutputStream zipOutput = new ZipOutputStream(output);
    zipOutput.setMethod(ZipOutputStream.DEFLATED);
    final PoWriter2 poWriter = new PoWriter2(false, !isPoProject);
    final Set<String> extensions = new HashSet<String>();

    extensions.add("gettext");
    extensions.add("comment");

    // Generate the download descriptor file
    String downloadId =
        fileSystemService.createDownloadDescriptorFile(
            downloadFile, projectSlug + "_" + iterationSlug + "_" + localeId + ".zip", userName);

    // Add the config file at the root of the project directory
    String configFilename = projectDirectory + configurationService.getConfigurationFileName();
    zipOutput.putNextEntry(new ZipEntry(configFilename));
    zipOutput.write(
        configurationService
            .getConfigForOfflineTranslation(projectSlug, iterationSlug, hLocale)
            .getBytes());
    zipOutput.closeEntry();
    getHandle().increaseProgress(1);

    final List<HDocument> allIterationDocs =
        documentDAO.getAllByProjectIteration(projectSlug, iterationSlug);
    for (HDocument document : allIterationDocs) {
      // Stop the process if signaled to do so
      if (getHandle().isCancelled()) {
        zipOutput.close();
        downloadFile.delete();
        fileSystemService.deleteDownloadDescriptorFile(downloadId);
        return null;
      }

      TranslationsResource translationResource = new TranslationsResource();
      List<HTextFlowTarget> hTargets = textFlowTargetDAO.findTranslations(document, hLocale);
      resourceUtils.transferToTranslationsResource(
          translationResource, document, hLocale, extensions, hTargets, Optional.<String>absent());

      Resource res = resourceUtils.buildResource(document);

      String filename = localeDirectory + document.getDocId() + ".po";
      zipOutput.putNextEntry(new ZipEntry(filename));
      poWriter.writePo(zipOutput, "UTF-8", res, translationResource);
      zipOutput.closeEntry();

      getHandle().increaseProgress(1);
    }

    zipOutput.flush();
    zipOutput.close();

    return downloadId;
  }