-
Notifications
You must be signed in to change notification settings - Fork 51
/
GetTranslationHistoryHandler.java
206 lines (189 loc) · 8.72 KB
/
GetTranslationHistoryHandler.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
package org.zanata.webtrans.server.rpc;
import java.util.List;
import java.util.Map;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import org.zanata.dao.TextFlowDAO;
import org.zanata.dao.TextFlowTargetReviewCommentsDAO;
import org.zanata.exception.ZanataServiceException;
import org.zanata.model.HLocale;
import org.zanata.model.HPerson;
import org.zanata.model.HTextFlow;
import org.zanata.model.HTextFlowTarget;
import org.zanata.model.HTextFlowTargetHistory;
import org.zanata.model.HTextFlowTargetReviewComment;
import org.zanata.rest.service.ResourceUtils;
import org.zanata.security.ZanataIdentity;
import org.zanata.service.LocaleService;
import org.zanata.webtrans.server.ActionHandlerFor;
import org.zanata.webtrans.shared.model.ReviewComment;
import org.zanata.webtrans.shared.model.ReviewCommentId;
import org.zanata.webtrans.shared.model.TransHistoryItem;
import org.zanata.webtrans.shared.model.TransUnitId;
import org.zanata.webtrans.shared.rpc.GetTranslationHistoryAction;
import org.zanata.webtrans.shared.rpc.GetTranslationHistoryResult;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import net.customware.gwt.dispatch.server.ExecutionContext;
import net.customware.gwt.dispatch.shared.ActionException;
/**
* @author Patrick Huang
* <a href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
@Named("webtrans.gwt.GetTranslationHistoryHandler")
@RequestScoped
@ActionHandlerFor(GetTranslationHistoryAction.class)
public class GetTranslationHistoryHandler extends
AbstractActionHandler<GetTranslationHistoryAction, GetTranslationHistoryResult> {
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory
.getLogger(GetTranslationHistoryHandler.class);
@Inject
private ZanataIdentity identity;
@Inject
private LocaleService localeServiceImpl;
@Inject
private TextFlowDAO textFlowDAO;
@Inject
private TextFlowTargetReviewCommentsDAO textFlowTargetReviewCommentsDAO;
@Inject
private ResourceUtils resourceUtils;
// TODO: Refactor duplicated code in getTranslationHistory
@Override
public GetTranslationHistoryResult execute(
GetTranslationHistoryAction action, ExecutionContext context)
throws ActionException {
identity.checkLoggedIn();
log.debug("get translation history for text flow id {}",
action.getTransUnitId());
HLocale hLocale;
try {
hLocale = localeServiceImpl.validateLocaleByProjectIteration(
action.getWorkspaceId().getLocaleId(),
action.getWorkspaceId().getProjectIterationId()
.getProjectSlug(),
action.getWorkspaceId().getProjectIterationId()
.getIterationSlug());
} catch (ZanataServiceException e) {
throw new ActionException(e);
}
HTextFlow hTextFlow =
textFlowDAO.findById(action.getTransUnitId().getId(), false);
HTextFlowTarget hTextFlowTarget =
hTextFlow.getTargets().get(hLocale.getId());
Map<Integer, HTextFlowTargetHistory> history = Maps.newHashMap();
TransHistoryItem latest =
getLatest(hTextFlowTarget, hTextFlow, hLocale);
if (hTextFlowTarget != null) {
history = hTextFlowTarget.getHistory();
}
Iterable<TransHistoryItem> historyItems =
Iterables.transform(history.values(),
new TargetHistoryToTransHistoryItemFunction());
log.debug("found {} history for text flow id {}",
Iterables.size(historyItems), action.getTransUnitId());
List<ReviewComment> reviewComments = getReviewComments(action);
log.debug("found {} review comments for text flow id {}",
reviewComments.size(), action.getTransUnitId());
// we re-wrap the list because gwt rpc doesn't like other list
// implementation
return new GetTranslationHistoryResult(Lists.newArrayList(historyItems),
latest, Lists.newArrayList(reviewComments));
}
/*
* Gets the Translation History Result for the React Editor
*/
public GetTranslationHistoryResult getTranslationHistory
(String localeId, Long transUnitId) {
HLocale hLocale = localeServiceImpl.getByLocaleId(localeId);
TransUnitId tUnitId = new TransUnitId(transUnitId);
HTextFlow hTextFlow =
textFlowDAO.findById(transUnitId, false);
HTextFlowTarget hTextFlowTarget =
hTextFlow.getTargets().get(hLocale.getId());
Map<Integer, HTextFlowTargetHistory> history =
hTextFlowTarget.getHistory();
Iterable<TransHistoryItem> historyItems =
Iterables.transform(history.values(),
new TargetHistoryToTransHistoryItemFunction());
List<ReviewComment> reviewComments =
getReviewComments(tUnitId, hLocale);
TransHistoryItem latest =
getLatest(hTextFlowTarget, hTextFlow, hLocale);
return new GetTranslationHistoryResult(Lists.newArrayList(historyItems),
latest, Lists.newArrayList(reviewComments));
}
private TransHistoryItem getLatest(HTextFlowTarget hTextFlowTarget,
HTextFlow hTextFlow, HLocale hLocale) {
TransHistoryItem latest = null;
if (hTextFlowTarget != null) {
String lastModifiedBy = GetTranslationHistoryHandler
.usernameOrEmptyString(hTextFlowTarget.getLastModifiedBy());
int nPlurals = resourceUtils.getNumPlurals(hTextFlow.getDocument(),
hLocale);
latest = new TransHistoryItem(
hTextFlowTarget.getVersionNum().toString(),
GwtRpcUtil.getTargetContentsWithPadding(hTextFlow,
hTextFlowTarget, nPlurals),
hTextFlowTarget.getState(), lastModifiedBy,
hTextFlowTarget.getLastChanged(),
hTextFlowTarget.getRevisionComment());
}
return latest;
}
protected List<ReviewComment>
getReviewComments(GetTranslationHistoryAction action) {
List<HTextFlowTargetReviewComment> hComments =
textFlowTargetReviewCommentsDAO.getReviewComments(
action.getTransUnitId(),
action.getWorkspaceId().getLocaleId());
return Lists.transform(hComments,
new Function<HTextFlowTargetReviewComment, ReviewComment>() {
@Override
public ReviewComment
apply(HTextFlowTargetReviewComment input) {
return new ReviewComment(
new ReviewCommentId(input.getId()),
input.getCommentText(), input.getCommenterName(),
input.getCreationDate()
);
}
});
}
// Overload for React Editor rest endpoint params
protected List<ReviewComment>
getReviewComments(TransUnitId transUnitId, HLocale hLocale) {
List<HTextFlowTargetReviewComment> hComments =
textFlowTargetReviewCommentsDAO.getReviewComments(
transUnitId,
hLocale.getLocaleId());
return Lists.transform(hComments, input -> new ReviewComment(
new ReviewCommentId(input.getId()),
input.getCommentText(), input.getCommenterName(),
input.getCreationDate()
));
}
private static String usernameOrEmptyString(HPerson lastModifiedBy) {
return lastModifiedBy != null && lastModifiedBy.hasAccount()
? lastModifiedBy.getAccount().getUsername() : "";
}
@Override
public void rollback(GetTranslationHistoryAction action,
GetTranslationHistoryResult result, ExecutionContext context)
throws ActionException {
}
private static class TargetHistoryToTransHistoryItemFunction
implements Function<HTextFlowTargetHistory, TransHistoryItem> {
@Override
public TransHistoryItem apply(HTextFlowTargetHistory targetHistory) {
return new TransHistoryItem(
targetHistory.getVersionNum().toString(),
targetHistory.getContents(), targetHistory.getState(),
usernameOrEmptyString(targetHistory.getLastModifiedBy()),
targetHistory.getLastChanged(),
targetHistory.getRevisionComment());
}
}
}