This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
SlugEntityUpdatedListener.java
129 lines (116 loc) · 5.05 KB
/
SlugEntityUpdatedListener.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package org.zanata.service.impl;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.event.spi.PostUpdateEvent;
import org.hibernate.event.spi.PostUpdateEventListener;
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.Scope;
import org.zanata.async.AsyncTaskHandle;
import org.zanata.async.AsyncTaskHandleManager;
import org.zanata.events.ProjectIterationUpdate;
import org.zanata.events.ProjectUpdate;
import org.zanata.model.HProject;
import org.zanata.model.HProjectIteration;
import org.zanata.model.SlugEntityBase;
import org.zanata.service.IndexingService;
import org.zanata.util.Event;
/**
* This class is a hibernate event listener which listens on post commit events
* for HProject and HProjectIteration. If it detects a HProject slug change, it
* will perform re-indexing for all HTextFlowTargets under that project. It will
* also fire update event for HProject and HProjectIteration with their old slug
* in payload.
*
* @see org.zanata.webtrans.server.HibernateIntegrator
* @see org.zanata.webtrans.server.TranslationWorkspaceManagerImpl
* @see IndexingServiceImpl
*
* @author Patrick Huang <a
* href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
@Name("slugEntityUpdatedListener")
@Scope(ScopeType.APPLICATION)
@AutoCreate
@Slf4j
public class SlugEntityUpdatedListener implements PostUpdateEventListener {
private static final long serialVersionUID = -1L;
@In
private AsyncTaskHandleManager asyncTaskHandleManager;
@In
private IndexingService indexingServiceImpl;
@In("event")
private Event<ProjectUpdate> projectUpdateEvent;
@In("event")
private Event<ProjectIterationUpdate> projectIterationUpdateEvent;
private static Integer slugFieldIndexInProject;
private static Integer slugFieldIndexInIteration;
@Override
public void onPostUpdate(PostUpdateEvent event) {
Class<?> entityClass = event.getEntity().getClass();
if (!entityClass.equals(HProject.class) && !entityClass.equals(
HProjectIteration.class)) {
return;
}
SlugEntityBase slugEntityBase = SlugEntityBase.class.cast(
event.getEntity());
if (slugEntityBase instanceof HProject) {
HProject project = (HProject) slugEntityBase;
slugFieldIndexInProject = getSlugFieldIndex(slugFieldIndexInProject, event);
String oldSlug = event.getOldState()[slugFieldIndexInProject].toString();
String newSlug = event.getState()[slugFieldIndexInProject].toString();
projectUpdateEvent.fire(new ProjectUpdate(project, oldSlug));
reindexIfProjectSlugHasChanged(oldSlug, newSlug, project);
} else if (slugEntityBase instanceof HProjectIteration) {
HProjectIteration iteration =
(HProjectIteration) slugEntityBase;
slugFieldIndexInIteration = getSlugFieldIndex(slugFieldIndexInIteration, event);
String oldSlug = event.getOldState()[slugFieldIndexInIteration].toString();
projectIterationUpdateEvent.fire(new ProjectIterationUpdate(
iteration, oldSlug));
}
}
public void reindexIfProjectSlugHasChanged(String oldSlug, String newSlug,
HProject project) {
if (!oldSlug.equals(newSlug)) {
log.debug("HProject [{}] changed slug. old slug: {}, new slug: {}",
project, oldSlug, newSlug);
AsyncTaskHandle<Void> handle = new AsyncTaskHandle<>();
asyncTaskHandleManager.registerTaskHandle(handle);
try {
indexingServiceImpl.reindexHTextFlowTargetsForProject(
project, handle);
} catch (Exception e) {
log.error("exception happen in async framework", e);
}
}
}
/**
* Try to locate index for field slug in the entity. We try to optimize a
* bit here since the index should be consistent and only need to be looked
* up once. If the given index is not null, it means it has been looked up
* and set already so we just return that value. Otherwise it will look it
* up in hibernate persister and return the index value.
*
* @param slugFieldIndex
* if not null it will be the index to use
* @param event
* post update event for an entity
* @return looked up index for slug field for the entity
*/
private static Integer getSlugFieldIndex(Integer slugFieldIndex, PostUpdateEvent event) {
if (slugFieldIndex != null) {
return slugFieldIndex;
}
String[] propertyNames = event.getPersister().getPropertyNames();
int i;
for (i = 0; i < propertyNames.length; i++) {
String propertyName = propertyNames[i];
if (propertyName.equals("slug")) {
return i;
}
}
throw new IllegalStateException("can not find slug index in entity properties");
}
}