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

Commit

Permalink
feat(translation update webhook): Add webhook event for translation u…
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Eng committed Apr 22, 2016
1 parent d77e080 commit dcaaf83
Show file tree
Hide file tree
Showing 19 changed files with 531 additions and 219 deletions.
8 changes: 4 additions & 4 deletions docs/release-notes.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
## 3.9
<h5>New Features</h5>
* [ZNTA-938](https://zanata.atlassian.net/browse/ZNTA-938) - Webhook event for translation update by user.

<h5>Infrastructure Changes</h5>
* Recommended platform: JBoss EAP 6 (6.4.6.GA or later).
* Alternative platform: WildFly version 10.x.
* [ZNTA-530](https://zanata.atlassian.net/browse/ZNTA-530) - Replace Seam 2 with CDI
* In WildFly or EAP `standalone.xml`, please make sure the Weld
extension is present in the `extensions` section like this:
Expand Down Expand Up @@ -33,10 +37,6 @@
* [ZNTA-928](https://zanata.atlassian.net/browse/ZNTA-928) - Readonly project doesn't have "lock" icon in UI
* [ZNTA-54](https://zanata.atlassian.net/browse/ZNTA-54) - Allow delete language

<h5>Infrastructure Changes</h5>
* Recommended platform: JBoss EAP 6 (6.4.6.GA or later).
* Alternative platform: WildFly version 10.x.

-----------------------
## 3.8.4
<h5>Bug fixes</h5>
Expand Down
28 changes: 28 additions & 0 deletions zanata-war/src/main/java/org/zanata/events/DocStatsEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.zanata.events;

import java.util.Map;

import org.zanata.common.ContentState;

import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;

/**
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a>
*/
@AllArgsConstructor
@Getter
@EqualsAndHashCode
public class DocStatsEvent {
private final DocumentLocaleKey key;

private final Long projectVersionId;

/**
* Updated content states with word counts
*/
private final Map<ContentState, Integer> contentStates;

private final Long lastModifiedTargetId;
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
*/
public abstract class WebhookEventType implements Serializable {

public abstract String getType();

@JsonIgnore
public String getJSON() {
ObjectMapper mapper = new ObjectMapper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
* site: http://www.fsf.org.
*/
package org.zanata.events;
package org.zanata.events.webhook;

import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
Expand All @@ -28,6 +28,7 @@

import org.codehaus.jackson.annotate.JsonPropertyOrder;
import org.zanata.common.LocaleId;
import org.zanata.events.WebhookEventType;

/**
*
Expand All @@ -39,7 +40,7 @@
@Setter
@AllArgsConstructor
@NoArgsConstructor
@JsonPropertyOrder({ "project", "version", "docId", "locale", "editorDocumentUrl", "milestone"})
@JsonPropertyOrder({"project", "version", "docId", "locale", "editorDocumentUrl", "milestone"})
@EqualsAndHashCode
public class DocumentMilestoneEvent extends WebhookEventType {
/**
Expand Down Expand Up @@ -77,4 +78,8 @@ public class DocumentMilestoneEvent extends WebhookEventType {
*/
private String editorDocumentUrl;

@Override
public String getType() {
return this.getClass().getName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright 2016, Red Hat, Inc. and individual contributors as indicated by the
* @author tags. See the copyright.txt file in the distribution for a full
* listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this software; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
* site: http://www.fsf.org.
*/
package org.zanata.events.webhook;

import java.util.HashMap;
import java.util.Map;

import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.codehaus.jackson.annotate.JsonPropertyOrder;
import org.zanata.common.ContentState;
import org.zanata.common.LocaleId;
import org.zanata.events.WebhookEventType;
import org.zanata.rest.dto.User;

/**
*
* Event for when a translation being updated in Zanata
*
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a>
*/
@Getter
@Setter
@AllArgsConstructor
@JsonPropertyOrder({"user", "project", "version", "docId", "locale", "contentStates"})
@EqualsAndHashCode
public class DocumentStatsEvent extends WebhookEventType {
/**
* User information
* {@link org.zanata.rest.dto.User}
*/
private final User user;

/**
* Target project slug.
* {@link org.zanata.model.HProject#slug}
*/
private final String project;

/**
* Target project version slug.
* {@link org.zanata.model.HProjectIteration#slug}
*/
private final String version;

/**
* Target document full path id.
* {@link org.zanata.model.HDocument#docId}
*/
private final String docId;

/**
* Target locale id.
* {@link LocaleId}
*/
private final LocaleId locale;

/**
* Updated content states with word counts
*/
private final Map<ContentState, Integer> contentStates;

@Override
public String getType() {
return this.getClass().getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@
package org.zanata.service;

import org.zanata.async.AsyncTaskHandle;
import org.zanata.events.DocumentStatisticUpdatedEvent;
import org.zanata.events.TextFlowTargetStateEvent;
import org.zanata.events.DocStatsEvent;
import org.zanata.model.HDocument;
import org.zanata.rest.dto.resource.Resource;
import org.zanata.ui.model.statistic.WordStatistic;

import java.util.Set;
import java.util.concurrent.Future;
Expand Down Expand Up @@ -113,9 +111,9 @@ public Future<HDocument> saveDocumentAsync(String projectSlug,

/**
* Post process when statistic in document changes
* (on DocumentStatisticUpdatedEvent)
* (on DocStatsEvent)
*
* @param event
*/
public void documentStatisticUpdated(DocumentStatisticUpdatedEvent event);
public void documentStatisticUpdated(DocStatsEvent event);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package org.zanata.service;

import org.zanata.common.LocaleId;
import org.zanata.events.DocStatsEvent;
import org.zanata.events.TextFlowTargetStateEvent;
import org.zanata.ui.model.statistic.WordStatistic;
import org.zanata.webtrans.shared.model.DocumentStatus;
Expand Down Expand Up @@ -90,4 +91,6 @@ WordStatistic getDocumentStatistics(Long documentId,
* @param localeId
*/
void clearDocumentStatistics(Long documentId, LocaleId localeId);

void docStatsUpdated(DocStatsEvent event);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
package org.zanata.service;

import org.zanata.common.LocaleId;
import org.zanata.events.TextFlowTargetStateEvent;
import org.zanata.events.DocStatsEvent;
import org.zanata.ui.model.statistic.WordStatistic;

/**
Expand All @@ -38,7 +38,7 @@ public interface VersionStateCache {
* locale. (It's really a Text Flow Target state)
*
*/
void textFlowStateUpdated(TextFlowTargetStateEvent event);
void textFlowStateUpdated(DocStatsEvent event);

WordStatistic getVersionStatistics(Long projectIterationId,
LocaleId localeId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,18 @@
import static org.zanata.transaction.TransactionUtil.runInTransaction;

import java.util.List;
import java.util.Map;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import javax.enterprise.event.Event;
import javax.inject.Inject;
import javax.inject.Named;
import org.zanata.common.ContentState;
import org.zanata.dao.TextFlowTargetDAO;
import org.zanata.events.DocStatsEvent;
import org.zanata.events.DocumentLocaleKey;
import org.zanata.events.TextFlowTargetStateEvent;
import org.zanata.model.HAccount;
Expand All @@ -55,9 +58,11 @@
import org.zanata.util.TranslationUtil;
import org.zanata.webtrans.shared.model.ValidationAction;

import com.beust.jcommander.internal.Maps;
import com.google.common.base.Optional;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

/**
Expand Down Expand Up @@ -315,7 +320,9 @@ public Boolean get() {

// TODO Maybe we should think about registering a Hibernate
// integrator for these updates
signalCopiedTranslation(actorId, hTarget, prevState);
int wordCount = originalTf.getWordCount() == null ? 0
: originalTf.getWordCount().intValue();
signalCopiedTranslation(hTarget, prevState, wordCount);
}
}

Expand Down Expand Up @@ -401,8 +408,8 @@ private static boolean shouldOverwrite(HTextFlowTarget currentlyStored,
return true;
}

private void signalCopiedTranslation(Long actorId, HTextFlowTarget target,
ContentState previousState) {
private void signalCopiedTranslation(HTextFlowTarget target,
ContentState previousState, int wordCount) {
/*
* Using a direct method call instead of an event because it's easier to
* read. Since these events are being called synchronously (as opposed
Expand All @@ -416,17 +423,15 @@ private void signalCopiedTranslation(Long actorId, HTextFlowTarget target,
DocumentLocaleKey key =
new DocumentLocaleKey(document.getId(), target.getLocaleId());

TextFlowTargetStateEvent.TextFlowTargetState state =
new TextFlowTargetStateEvent.TextFlowTargetState(
target.getTextFlow().getId(),
target.getId(), target.getState(),
previousState);
Map<ContentState, Integer> contentStates = Maps.newHashMap();
contentStates.put(target.getState(), wordCount);
contentStates.put(previousState, wordCount * -1);

TextFlowTargetStateEvent event =
new TextFlowTargetStateEvent(key,
document.getProjectIteration().getId(), actorId, state);
DocStatsEvent docEvent =
new DocStatsEvent(key, document.getProjectIteration().getId(),
contentStates, target.getId());

versionStateCacheImpl.textFlowStateUpdated(event);
versionStateCacheImpl.textFlowStateUpdated(docEvent);
}

/**
Expand Down
Loading

0 comments on commit dcaaf83

Please sign in to comment.