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

Commit

Permalink
add comment and clean up a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Huang committed Jan 31, 2014
1 parent 4f413a0 commit ec1a452
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 33 deletions.
Expand Up @@ -2,13 +2,7 @@

import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;

import javax.activation.CommandObject;
import javax.annotation.Nullable;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
Expand All @@ -18,7 +12,6 @@
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.base.Throwables;
import lombok.extern.slf4j.Slf4j;

/**
Expand Down
Expand Up @@ -59,7 +59,6 @@ public void editorReceivesRestServiceResults() {
restCaller.postSourceDocResource(projectSlug, iterationSlug,
sourceResource, false);

// open editor
// open editor
new LoginWorkFlow().signIn("admin", "admin");
// webTrans
Expand Down
Expand Up @@ -9,13 +9,20 @@
import lombok.RequiredArgsConstructor;

/**
* @author Patrick Huang
* <a href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
* This event is raised by text flow target update initiator before making a
* change. Hibernate entity listener will be triggered after the change is made
* and at that point this context information is retrieved from cache.
*
* @see org.zanata.webtrans.server.TranslationUpdateListener
*
* @author Patrick Huang <a
* href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
@RequiredArgsConstructor
@Getter
public class TextFlowTargetUpdateContextEvent {
public static final String EVENT_NAME = "org.zanata.event.TextFlowTargetUpdateContextEvent";
public static final String EVENT_NAME =
"org.zanata.event.TextFlowTargetUpdateContextEvent";

private final TransUnitId transUnitId;
private final LocaleId localeId;
Expand Down
Expand Up @@ -6,13 +6,22 @@
import lombok.RequiredArgsConstructor;

/**
* @author Patrick Huang
* <a href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
* This event is raised in Hibernate entity listener after change has been made.
*
* We then gather all relevant information required for TransUnitUpdated object
* creation.
*
* @see org.zanata.webtrans.server.HibernateIntegrator
* @see org.zanata.webtrans.shared.rpc.TransUnitUpdated
* @see org.zanata.webtrans.server.rpc.TransUnitUpdateHelper
* @author Patrick Huang <a
* href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
@RequiredArgsConstructor
@Getter
public class TextFlowTargetUpdatedEvent {
public static final String EVENT_NAME = "org.zanata.events.TextFlowTargetUpdatedEvent";
public static final String EVENT_NAME =
"org.zanata.events.TextFlowTargetUpdatedEvent";

private final TranslationWorkspace workspace;
private final Long textFlowTargetId;
Expand Down
Expand Up @@ -13,6 +13,8 @@
import lombok.extern.slf4j.Slf4j;

/**
* Hibernate SPI. Register event listener for entity lifecycle events.
*
* @author Patrick Huang <a
* href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
Expand All @@ -29,6 +31,9 @@ public void integrate(Configuration configuration,
(TranslationUpdateListener) Component
.getInstance(TranslationUpdateListener.class);
log.info("register event listener: {}", updateListener);
// We have to use POST_UPDATE not POST_UPDATE_COMMIT. Because we
// still need to access some other entities to make transunit. After
// commit the transaction is closed.
eventListenerRegistry.appendListeners(EventType.POST_UPDATE,
updateListener);
eventListenerRegistry.appendListeners(EventType.POST_INSERT,
Expand Down
Expand Up @@ -2,25 +2,19 @@

import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpSession;

import org.hibernate.event.spi.PostInsertEvent;
import org.hibernate.event.spi.PostInsertEventListener;
import org.hibernate.event.spi.PostUpdateEvent;
import org.hibernate.event.spi.PostUpdateEventListener;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Observer;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.contexts.SessionContext;
import org.jboss.seam.core.Events;
import org.jboss.seam.util.Work;
import org.jboss.seam.web.ServletContexts;
import org.jboss.seam.web.Session;
import org.zanata.common.ContentState;
import org.zanata.common.LocaleId;
import org.zanata.common.ProjectType;
Expand Down Expand Up @@ -50,6 +44,8 @@
import lombok.extern.slf4j.Slf4j;

/**
* Entity event listener for HTextFlowTarget.
*
* @author Patrick Huang <a
* href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
Expand All @@ -61,16 +57,24 @@ public class TranslationUpdateListener implements PostUpdateEventListener,
PostInsertEventListener {

private static final long serialVersionUID = 1L;

private static final Cache<CacheKey, CacheValue> updateContext =
CacheBuilder.newBuilder().softValues()
.expireAfterAccess(1, TimeUnit.SECONDS).build();
.expireAfterAccess(1, TimeUnit.SECONDS).maximumSize(1000)
.build();

@In(create = true)
private TranslationWorkspaceManager translationWorkspaceManager;

@In
private TransUnitTransformer transUnitTransformer;

/**
* Event raised by Text flow target update initiator containing update
* context.
*
* @param event
*/
@Observer(TextFlowTargetUpdateContextEvent.EVENT_NAME)
public static void updateContext(TextFlowTargetUpdateContextEvent event) {
updateContext
Expand All @@ -97,7 +101,7 @@ protected Void work() throws Exception {

HTextFlowTarget target =
HTextFlowTarget.class.cast(event.getEntity());
publishTransUnitUpdatedEvent(target.getVersionNum() - 1,
prepareTransUnitUpdatedEvent(target.getVersionNum() - 1,
oldContentState, target);
return null;
}
Expand All @@ -108,7 +112,7 @@ protected Void work() throws Exception {

}

private void publishTransUnitUpdatedEvent(int previousVersionNum,
private void prepareTransUnitUpdatedEvent(int previousVersionNum,
ContentState previousState, HTextFlowTarget target) {
LocaleId localeId = target.getLocaleId();
HTextFlow textFlow = target.getTextFlow();
Expand Down Expand Up @@ -144,25 +148,30 @@ private void publishTransUnitUpdatedEvent(int previousVersionNum,
transUnit.getLocaleId()));
TransUnitUpdated updated;
if (context != null) {
EditorClientId editorClientId = context.editorClientId;
TransUnitUpdated.UpdateType updateType = context.updateType;
updated =
new TransUnitUpdated(updateInfo, editorClientId, updateType);
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();
String sessionId =
ServletContexts.instance().getRequest().getSession()
.getId();
EditorClientId editorClientId = new EditorClientId(sessionId, -1);
updated = new TransUnitUpdated(updateInfo, editorClientId,
updated =
new TransUnitUpdated(updateInfo, editorClientId,
TransUnitUpdated.UpdateType.NonEditorSave);
} else {
updated = new TransUnitUpdated(updateInfo, new EditorClientId("unknown", -1),
TransUnitUpdated.UpdateType.NonEditorSave);
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));
TextFlowTargetUpdatedEvent.EVENT_NAME,
new TextFlowTargetUpdatedEvent(workspaceOptional.get(),
target.getId(), updated));
}
}

Expand All @@ -186,7 +195,7 @@ public void onPostInsert(final PostInsertEvent event) {
protected Void work() throws Exception {
HTextFlowTarget target =
HTextFlowTarget.class.cast(event.getEntity());
publishTransUnitUpdatedEvent(0, ContentState.New, target);
prepareTransUnitUpdatedEvent(0, ContentState.New, target);
return null;
}
}.workInTransaction();
Expand Down

0 comments on commit ec1a452

Please sign in to comment.