@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;
  }
示例#2
0
  @Test
  public void checkPositionsNotNull() throws Exception {
    EntityManager em = getEm();
    HIterationProject project = em.find(HIterationProject.class, 1l);
    // assertThat( project, notNullValue() );

    HDocument hdoc = new HDocument("fullpath", ContentType.TextPlain, en_US);
    hdoc.setProjectIteration(project.getProjectIterations().get(0));

    List<HTextFlow> textFlows = hdoc.getTextFlows();
    HTextFlow flow1 = new HTextFlow(hdoc, "textflow1", "some content");
    HTextFlow flow2 = new HTextFlow(hdoc, "textflow2", "more content");
    textFlows.add(flow1);
    textFlows.add(flow2);
    em.persist(hdoc);
    em.flush();
    // em.clear();
    // hdoc = em.find(HDocument.class, docId);
    em.refresh(hdoc);

    List<HTextFlow> textFlows2 = hdoc.getTextFlows();
    assertThat(textFlows2.size(), is(2));
    flow1 = textFlows2.get(0);
    assertThat(flow1, notNullValue());
    flow2 = textFlows2.get(1);
    assertThat(flow2, notNullValue());

    // TODO: we should automate this...
    hdoc.incrementRevision();

    textFlows2.remove(flow1);
    flow1.setObsolete(true);
    dao.syncRevisions(hdoc, flow1);

    // flow1.setPos(null);
    em.flush();
    em.refresh(hdoc);
    em.refresh(flow1);
    em.refresh(flow2);
    assertThat(hdoc.getTextFlows().size(), is(1));
    flow2 = hdoc.getTextFlows().get(0);
    assertThat(flow2.getResId(), equalTo("textflow2"));

    flow1 = hdoc.getAllTextFlows().get("textflow1");
    // assertThat(flow1.getPos(), nullValue());
    assertThat(flow1.isObsolete(), is(true));
    assertThat(flow1.getRevision(), is(2));
    flow2 = hdoc.getAllTextFlows().get("textflow2");
    // assertThat(flow1.getPos(), is(0));
    assertThat(flow2.isObsolete(), is(false));
  }
示例#3
0
  // FIXME this test only works if resources-dev is on the classpath
  @SuppressWarnings("unchecked")
  @Test
  public void ensureHistoryOnTextFlow() {
    EntityManager em = getEm();
    HIterationProject project = em.find(HIterationProject.class, 1l);
    // assertThat( project, notNullValue() );

    HDocument hdoc = new HDocument("fullpath", ContentType.TextPlain, en_US);
    hdoc.setProjectIteration(project.getProjectIterations().get(0));

    List<HTextFlow> textFlows = hdoc.getTextFlows();
    HTextFlow flow1 = new HTextFlow(hdoc, "textflow3", "some content");
    HTextFlow flow2 = new HTextFlow(hdoc, "textflow4", "more content");
    textFlows.add(flow1);
    textFlows.add(flow2);
    em.persist(hdoc);
    em.flush();

    hdoc.incrementRevision();

    flow1.setContent("nwe content!");

    dao.syncRevisions(hdoc, flow1);

    em.flush();

    HTextFlowTarget target = new HTextFlowTarget(flow1, de_DE);
    target.setContent("hello world");
    em.persist(target);
    em.flush();
    target.setContent("h2");
    em.flush();

    List<HTextFlowTargetHistory> hist =
        em.createQuery("from HTextFlowTargetHistory h where h.textFlowTarget =:target")
            .setParameter("target", target)
            .getResultList();
    assertThat(hist, notNullValue());
    assertThat(hist.size(), not(0));
  }
  //    @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);
  }