Ejemplo n.º 1
0
  private void processWebHookDocumentMilestoneEvent(
      DocumentStatisticUpdatedEvent event,
      Collection<ContentState> contentStates,
      String message,
      int percentMilestone) {

    HProjectIteration version = projectIterationDAO.findById(event.getProjectIterationId());
    HProject project = version.getProject();

    if (!project.getWebHooks().isEmpty()) {
      WordStatistic stats =
          translationStateCacheImpl.getDocumentStatistics(
              event.getDocumentId(), event.getLocaleId());

      WordStatistic oldStats = StatisticsUtil.copyWordStatistic(stats);
      if (oldStats != null) {
        oldStats.decrement(event.getNewState(), event.getWordCount());
        oldStats.increment(event.getPreviousState(), event.getWordCount());

        boolean shouldPublish =
            hasContentStateReachedMilestone(oldStats, stats, contentStates, percentMilestone);

        if (shouldPublish) {
          HDocument document = documentDAO.getById(event.getDocumentId());

          String editorUrl =
              urlUtil.fullEditorDocumentUrl(
                  project.getSlug(),
                  version.getSlug(),
                  event.getLocaleId(),
                  LocaleId.EN_US,
                  document.getDocId());

          DocumentMilestoneEvent milestoneEvent =
              new DocumentMilestoneEvent(
                  project.getSlug(),
                  version.getSlug(),
                  document.getDocId(),
                  event.getLocaleId(),
                  message,
                  editorUrl);
          for (WebHook webHook : project.getWebHooks()) {
            publishDocumentMilestoneEvent(webHook, milestoneEvent);
          }
        }
      }
    }
  }
 /**
  * Check if sourceVersion or targetVersion has source document.
  *
  * @param sourceVersion
  * @param targetVersion
  */
 private boolean isVersionsEmpty(
     HProjectIteration sourceVersion, HProjectIteration targetVersion) {
   if (sourceVersion.getDocuments().isEmpty()) {
     log.error(
         "No documents in source version {}:{}",
         sourceVersion.getProject().getSlug(),
         sourceVersion.getSlug());
     return true;
   }
   if (targetVersion.getDocuments().isEmpty()) {
     log.error(
         "No documents in target version {}:{}",
         targetVersion.getProject().getSlug(),
         targetVersion.getSlug());
     return true;
   }
   return false;
 }
  @Override
  protected int getMaxProgress() {
    LocaleService localeService = (LocaleService) Component.getInstance(LocaleServiceImpl.class);
    List<HLocale> localeList =
        localeService.getSupportedLanguageByProjectIteration(
            projectIteration.getProject().getSlug(), projectIteration.getSlug());

    return projectIteration.getDocuments().size() * localeList.size();
  }
  private void prepareTransUnitUpdatedEvent(
      int previousVersionNum, ContentState previousState, HTextFlowTarget target) {
    LocaleId localeId = target.getLocaleId();
    HTextFlow textFlow = target.getTextFlow();
    HDocument document = textFlow.getDocument();
    HProjectIteration projectIteration = document.getProjectIteration();
    String iterationSlug = projectIteration.getSlug();
    String projectSlug = projectIteration.getProject().getSlug();
    ProjectType projectType = projectIteration.getProjectType();

    WorkspaceId workspaceId =
        new WorkspaceId(new ProjectIterationId(projectSlug, iterationSlug, projectType), localeId);
    Optional<TranslationWorkspace> workspaceOptional =
        translationWorkspaceManager.tryGetWorkspace(workspaceId);
    if (!workspaceOptional.isPresent()) {
      return;
    }

    TransUnitTransformer transUnitTransformer =
        serviceLocator.getInstance(TransUnitTransformer.class);
    TransUnit transUnit = transUnitTransformer.transform(textFlow, target, target.getLocale());

    DocumentId documentId = new DocumentId(document.getId(), document.getDocId());
    int wordCount = textFlow.getWordCount().intValue();

    TransUnitUpdateInfo updateInfo =
        createTransUnitUpdateInfo(
            transUnit, documentId, wordCount, previousVersionNum, previousState);

    CacheValue context =
        updateContext.getIfPresent(new CacheKey(transUnit.getId(), transUnit.getLocaleId()));
    TransUnitUpdated updated;
    if (context != null) {
      updated = new TransUnitUpdated(updateInfo, context.editorClientId, context.updateType);
      log.debug("about to publish trans unit updated event {}", updated);
    } else if (ServletContexts.instance().getRequest() != null) {

      String sessionId = ServletContexts.instance().getRequest().getSession().getId();
      EditorClientId editorClientId = new EditorClientId(sessionId, -1);
      updated =
          new TransUnitUpdated(
              updateInfo, editorClientId, TransUnitUpdated.UpdateType.NonEditorSave);
    } else {
      updated =
          new TransUnitUpdated(
              updateInfo,
              new EditorClientId("unknown", -1),
              TransUnitUpdated.UpdateType.NonEditorSave);
    }
    if (Events.exists()) {
      Events.instance()
          .raiseTransactionSuccessEvent(
              TextFlowTargetUpdatedEvent.EVENT_NAME,
              new TextFlowTargetUpdatedEvent(workspaceOptional.get(), target.getId(), updated));
    }
  }
  @Override
  public int getTotalProgressCount(
      HProjectIteration sourceVersion, HProjectIteration targetVersion) {
    int matchCount = getTotalMatchCount(sourceVersion.getId(), targetVersion.getId());

    List<HLocale> locales =
        getSupportedLocales(targetVersion.getProject().getSlug(), targetVersion.getSlug());

    return matchCount * locales.size();
  }
  @Override
  @Async
  public Future<Void> startMergeTranslations(
      String sourceProjectSlug,
      String sourceVersionSlug,
      String targetProjectSlug,
      String targetVersionSlug,
      boolean useNewerTranslation,
      MergeTranslationsTaskHandle handle) {

    HProjectIteration sourceVersion =
        projectIterationDAO.getBySlug(sourceProjectSlug, sourceVersionSlug);

    if (sourceVersion == null) {
      log.error("Cannot find source version of {}:{}", sourceProjectSlug, sourceVersionSlug);
      return AsyncTaskResult.taskResult();
    }

    HProjectIteration targetVersion =
        projectIterationDAO.getBySlug(targetProjectSlug, targetVersionSlug);

    if (targetVersion == null) {
      log.error("Cannot find target version of {}:{}", targetProjectSlug, targetVersionSlug);
      return AsyncTaskResult.taskResult();
    }

    if (isVersionsEmpty(sourceVersion, targetVersion)) {
      return AsyncTaskResult.taskResult();
    }

    if (getSupportedLocales(targetProjectSlug, targetVersionSlug).isEmpty()) {
      log.error(
          "No locales enabled in target version of {} [{}]", targetProjectSlug, targetVersionSlug);
      return AsyncTaskResult.taskResult();
    }

    Optional<MergeTranslationsTaskHandle> taskHandleOpt = Optional.fromNullable(handle);

    if (taskHandleOpt.isPresent()) {
      prepareMergeTranslationsHandle(sourceVersion, targetVersion, taskHandleOpt.get());
    }

    Stopwatch overallStopwatch = Stopwatch.createStarted();
    log.info(
        "merge translations start: from {} to {}",
        sourceProjectSlug + ":" + sourceVersionSlug,
        targetProjectSlug + ":" + targetVersionSlug);

    int startCount = 0;
    int totalCount = getTotalMatchCount(sourceVersion.getId(), targetVersion.getId());

    List<HLocale> supportedLocales =
        getSupportedLocales(targetVersion.getProject().getSlug(), targetVersion.getSlug());

    while (startCount < totalCount) {
      int processedCount =
          mergeTranslationBatch(
              sourceVersion,
              targetVersion,
              supportedLocales,
              useNewerTranslation,
              startCount,
              TEXTFLOWS_PER_BATCH);
      if (taskHandleOpt.isPresent()) {
        taskHandleOpt.get().increaseProgress(processedCount);
      }

      startCount += TEXTFLOWS_PER_BATCH;
      textFlowDAO.clear();
    }
    versionStateCacheImpl.clearVersionStatsCache(targetVersion.getId());
    log.info(
        "merge translation end: from {} to {}, {}",
        sourceProjectSlug + ":" + sourceVersionSlug,
        targetProjectSlug + ":" + targetVersionSlug,
        overallStopwatch);

    return AsyncTaskResult.taskResult();
  }
 public IterationCopyTransTask(HProjectIteration projectIteration, HCopyTransOptions options) {
   super(options, "IterationCopyTransTask: " + projectIteration.getSlug());
   this.projectIteration = projectIteration;
 }
  //    @Test(enabled = true, description = "this should only be executed manually in IDE")
  @Ignore
  @Test
  @PerformanceProfiling
  public void pushTranslation() {
    EntityMaker entityMaker =
        EntityMakerBuilder.builder()
            .addFieldOrPropertyMaker(
                HProject.class, "sourceViewURL", FixedValueMaker.EMPTY_STRING_MAKER)
            .build();
    HProjectIteration iteration = entityMaker.makeAndPersist(getEm(), HProjectIteration.class);
    HLocale srcLocale = createAndPersistLocale(LocaleId.EN_US, getEm());
    HLocale transLocale = createAndPersistLocale(LocaleId.DE, getEm());

    String versionSlug = iteration.getSlug();
    String projectSlug = iteration.getProject().getSlug();

    HDocument document = new HDocument("message", ContentType.PO, srcLocale);
    document.setProjectIteration(iteration);
    getEm().persist(document);
    getEm().flush();

    // adjust this number to suit testing purpose
    int numOfTextFlows = 50;
    int numOfTextFlowsHavingTarget =
        createSourceAndSomeTargets(document, transLocale, numOfTextFlows);
    getEm().getTransaction().commit();
    getEm().getTransaction().begin();

    Long targetsCountBefore =
        getEm()
            .createQuery("select count(*) from HTextFlowTarget where locale = :locale", Long.class)
            .setParameter("locale", transLocale)
            .getSingleResult();
    Assertions.assertThat(targetsCountBefore).isEqualTo(numOfTextFlowsHavingTarget);

    // ============ add targets =========
    TranslationsResource translations = new TranslationsResource();
    translations.setRevision(1);
    for (int i = 0; i < numOfTextFlows; i++) {
      addSampleTranslation(translations, "res" + i);
    }
    Monitor mon = MonitorFactory.start("");
    log.info("==== start translateAllInDoc");
    service.translateAllInDoc(
        projectSlug,
        versionSlug,
        document.getDocId(),
        transLocale.getLocaleId(),
        translations,
        extensions,
        MergeType.AUTO,
        false,
        TranslationSourceType.API_UPLOAD);
    log.info("==== stop translateAllInDoc: {}", mon.stop());
    getEm().getTransaction().commit();
    getEm().getTransaction().begin();

    Long targetsCount =
        getEm()
            .createQuery("select count(*) from HTextFlowTarget where locale = :locale", Long.class)
            .setParameter("locale", transLocale)
            .getSingleResult();
    Assertions.assertThat(targetsCount).isEqualTo(numOfTextFlows);

    List<HTextFlowTargetHistory> histories =
        getEm()
            .createQuery("from HTextFlowTargetHistory", HTextFlowTargetHistory.class)
            .getResultList();
    Assertions.assertThat(histories).hasSize(numOfTextFlowsHavingTarget);
  }