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
/
TranslationUpdateListener.java
226 lines (198 loc) · 8.45 KB
/
TranslationUpdateListener.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package org.zanata.webtrans.server;
import java.util.concurrent.TimeUnit;
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.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.core.Events;
import org.jboss.seam.util.Work;
import org.jboss.seam.web.ServletContexts;
import org.zanata.common.ContentState;
import org.zanata.common.LocaleId;
import org.zanata.common.ProjectType;
import org.zanata.events.TextFlowTargetUpdateContextEvent;
import org.zanata.events.TextFlowTargetUpdatedEvent;
import org.zanata.model.HDocument;
import org.zanata.model.HProjectIteration;
import org.zanata.model.HTextFlow;
import org.zanata.model.HTextFlowTarget;
import org.zanata.util.ServiceLocator;
import org.zanata.webtrans.server.rpc.TransUnitTransformer;
import org.zanata.webtrans.shared.auth.EditorClientId;
import org.zanata.webtrans.shared.model.DocumentId;
import org.zanata.webtrans.shared.model.ProjectIterationId;
import org.zanata.webtrans.shared.model.TransUnit;
import org.zanata.webtrans.shared.model.TransUnitId;
import org.zanata.webtrans.shared.model.TransUnitUpdateInfo;
import org.zanata.webtrans.shared.model.WorkspaceId;
import org.zanata.webtrans.shared.rpc.TransUnitUpdated;
import com.google.common.base.Optional;
import com.google.common.base.Predicates;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
/**
* Entity event listener for HTextFlowTarget.
*
* @author Patrick Huang <a
* href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
@Name("translationUpdateListener")
@Scope(ScopeType.APPLICATION)
@AutoCreate
@Slf4j
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).maximumSize(1000)
.build();
@In(create = true)
private TranslationWorkspaceManager translationWorkspaceManager;
@In
private ServiceLocator serviceLocator;
/**
* Event raised by Text flow target update initiator containing update
* context.
*
* @param event
*/
@Observer(TextFlowTargetUpdateContextEvent.EVENT_NAME)
public static void updateContext(TextFlowTargetUpdateContextEvent event) {
updateContext
.put(new CacheKey(event.getTransUnitId(), event.getLocaleId()),
new CacheValue(event.getEditorClientId(), event
.getUpdateType()));
}
@Override
public void onPostUpdate(final PostUpdateEvent event) {
Object entity = event.getEntity();
if (!(entity instanceof HTextFlowTarget)) {
return;
}
final HTextFlowTarget target =
HTextFlowTarget.class.cast(event.getEntity());
try {
new Work<Void>() {
@Override
protected Void work() throws Exception {
ContentState oldContentState =
(ContentState) Iterables.find(
Lists.newArrayList(event.getOldState()),
Predicates.instanceOf(ContentState.class));
prepareTransUnitUpdatedEvent(target.getVersionNum() - 1,
oldContentState, target);
return null;
}
}.workInTransaction();
} catch (Exception e) {
log.error("fail to publish TransUnitUpdate event", e);
}
}
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));
}
}
private static TransUnitUpdateInfo createTransUnitUpdateInfo(
TransUnit transUnit, DocumentId documentId, int wordCount,
int previousVersionNum, ContentState previousState) {
return new TransUnitUpdateInfo(true, true, documentId, transUnit,
wordCount, previousVersionNum, previousState, null);
}
@Override
public void onPostInsert(final PostInsertEvent event) {
Object entity = event.getEntity();
if (!(entity instanceof HTextFlowTarget)) {
return;
}
final HTextFlowTarget target =
HTextFlowTarget.class.cast(event.getEntity());
try {
new Work<Void>() {
@Override
protected Void work() throws Exception {
prepareTransUnitUpdatedEvent(0, ContentState.New, target);
return null;
}
}.workInTransaction();
} catch (Exception e) {
log.error("fail to publish TransUnitUpdate event", e);
}
}
@RequiredArgsConstructor
@EqualsAndHashCode
private static class CacheKey {
private final TransUnitId transUnitId;
private final LocaleId localeId;
}
@RequiredArgsConstructor
private static class CacheValue {
private final EditorClientId editorClientId;
private final TransUnitUpdated.UpdateType updateType;
}
}