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));
    }
  }
Пример #2
0
  @Override
  public void getResource(final HttpServletRequest request, final HttpServletResponse response)
      throws ServletException, IOException {
    ServletLifecycle.beginRequest(request);
    try {
      ServletContexts.instance().setRequest(request);
      ConversationPropagation.instance().restoreConversationId(request.getParameterMap());
      Manager.instance().restoreConversation();
      ServletLifecycle.resumeConversation(request);

      final ReportResult reportResult =
          (ReportResult) Contexts.getConversationContext().get("user.reportResult");

      if (reportResult != null) {
        response.setContentType("text/csv");
        response.setCharacterEncoding("UTF-8");

        final PrintWriter out = response.getWriter();

        boolean first = true;

        for (final ColumnDefinition column : reportResult.getGroupByColumns()) {
          if (!first) {
            out.print(DELIM);
          }
          out.print(toCSVString(column.getHeader()));
          first = false;
        }
        for (final ColumnDefinition column : reportResult.getColumns()) {
          if (!first) {
            out.print(DELIM);
          }
          out.print(toCSVString(column.getHeader()));
          first = false;
        }
        out.println();

        for (final ReportResultGroup group : reportResult.getGroups()) {
          writeReportGroup(0, group, out);
        }
        for (final ReportRow row : reportResult.getRows()) {
          writeReportRow(row, reportResult.getColumns(), out);
        }
        out.close();
      }
    } finally {
      ServletLifecycle.endRequest(request);
    }
  }