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
/
TranslationStateCacheImpl.java
304 lines (260 loc) · 11 KB
/
TranslationStateCacheImpl.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* Copyright 2010, 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.service.impl;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import net.sf.ehcache.CacheManager;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.zanata.cache.CacheWrapper;
import org.zanata.cache.EhcacheWrapper;
import org.zanata.common.LocaleId;
import org.zanata.dao.DocumentDAO;
import org.zanata.dao.LocaleDAO;
import org.zanata.dao.TextFlowDAO;
import org.zanata.dao.TextFlowTargetDAO;
import org.zanata.events.TextFlowTargetStateEvent;
import org.zanata.model.HDocument;
import org.zanata.model.HLocale;
import org.zanata.model.HTextFlow;
import org.zanata.model.HTextFlowTarget;
import org.zanata.service.TranslationStateCache;
import org.zanata.service.ValidationFactoryProvider;
import org.zanata.ui.model.statistic.WordStatistic;
import org.zanata.util.ServiceLocator;
import org.zanata.webtrans.shared.model.DocumentId;
import org.zanata.webtrans.shared.model.DocumentStatus;
import org.zanata.webtrans.shared.model.ValidationAction;
import org.zanata.webtrans.shared.model.ValidationId;
import com.google.common.cache.CacheLoader;
/**
* Default Implementation of the Translation State Cache.
*
* @author Carlos Munoz <a
* href="mailto:camunoz@redhat.com">camunoz@redhat.com</a>
*/
@Name("translationStateCacheImpl")
// TODO split into APPLICATION and STATELESS beans
@Scope(ScopeType.APPLICATION)
@AutoCreate
public class TranslationStateCacheImpl implements TranslationStateCache {
private static final String BASE = TranslationStateCacheImpl.class.getName();
private static final String DOC_STATISTIC_CACHE_NAME = BASE
+ ".documentStatisticCache";
private static final String DOC_STATUS_CACHE_NAME = BASE
+ ".docStatusCache";
private static final String TFT_VALIDATION_CACHE_NAME = BASE
+ ".targetValidationCache";
private CacheManager cacheManager;
private CacheWrapper<DocumentLocaleKey, WordStatistic> documentStatisticCache;
private CacheLoader<DocumentLocaleKey, WordStatistic> documentStatisticLoader;
private CacheWrapper<DocumentLocaleKey, DocumentStatus> docStatusCache;
private CacheLoader<DocumentLocaleKey, DocumentStatus> docStatusLoader;
private CacheWrapper<Long, Map<ValidationId, Boolean>> targetValidationCache;
private CacheLoader<Long, Map<ValidationId, Boolean>> targetValidationLoader;
@In
private ServiceLocator serviceLocator;
// constructor for Seam
public TranslationStateCacheImpl() {
this(new DocumentStatisticLoader(), new HTextFlowTargetIdLoader(),
new HTextFlowTargetValidationLoader());
}
// Constructor for testing
public TranslationStateCacheImpl(
CacheLoader<DocumentLocaleKey, WordStatistic> documentStatisticLoader,
CacheLoader<DocumentLocaleKey, DocumentStatus> docStatsLoader,
CacheLoader<Long, Map<ValidationId, Boolean>> targetValidationLoader) {
this.documentStatisticLoader = documentStatisticLoader;
this.docStatusLoader = docStatsLoader;
this.targetValidationLoader = targetValidationLoader;
}
@Create
public void create() {
cacheManager = CacheManager.create();
documentStatisticCache =
EhcacheWrapper.create(DOC_STATISTIC_CACHE_NAME,
cacheManager, documentStatisticLoader);
docStatusCache =
EhcacheWrapper.create(DOC_STATUS_CACHE_NAME, cacheManager,
docStatusLoader);
targetValidationCache =
EhcacheWrapper.create(TFT_VALIDATION_CACHE_NAME, cacheManager,
targetValidationLoader);
}
@Destroy
public void destroy() {
cacheManager.shutdown();
}
@Override
public WordStatistic getDocumentStatistics(Long documentId,
LocaleId localeId) {
return documentStatisticCache.getWithLoader(new DocumentLocaleKey(
documentId, localeId));
}
@Override
public void clearDocumentStatistics(Long documentId) {
LocaleDAO localeDAO = serviceLocator.getInstance(LocaleDAO.class);
for (HLocale locale : localeDAO.findAll()) {
DocumentLocaleKey key =
new DocumentLocaleKey(documentId, locale.getLocaleId());
documentStatisticCache.remove(key);
}
}
@Override
public void clearDocumentStatistics(Long documentId, LocaleId localeId) {
documentStatisticCache.remove(new DocumentLocaleKey(documentId,
localeId));
}
public DocumentStatus getDocumentStatus(Long documentId, LocaleId localeId) {
return docStatusCache.getWithLoader(new DocumentLocaleKey(
documentId, localeId));
}
@Override
public Boolean textFlowTargetHasWarningOrError(Long targetId,
ValidationId validationId) {
Map<ValidationId, Boolean> cacheEntry =
targetValidationCache.getWithLoader(targetId);
synchronized (cacheEntry) {
if (!cacheEntry.containsKey(validationId)) {
Boolean result = loadTargetValidation(targetId, validationId);
cacheEntry.put(validationId, result);
}
return cacheEntry.get(validationId);
}
}
@Override
public void textFlowStateUpdated(TextFlowTargetStateEvent event) {
DocumentLocaleKey key =
new DocumentLocaleKey(event.getDocumentId(),
event.getLocaleId());
//invalidate document statistic cache
clearDocumentStatistics(event.getDocumentId(), event.getLocaleId());
// update document status information
updateDocStatusCache(key, event.getTextFlowTargetId());
// invalidate target validation
targetValidationCache.remove(event.getTextFlowTargetId());
}
private void updateDocStatusCache(DocumentLocaleKey key,
Long updatedTargetId) {
DocumentStatus documentStatus = docStatusCache.get(key);
if (documentStatus != null) {
HTextFlowTarget target =
getTextFlowTargetDAO().findById(updatedTargetId, false);
updateDocumentStatus(getDocumentDAO(), documentStatus,
key.getDocumentId(), target);
}
}
private Boolean loadTargetValidation(Long textFlowTargetId,
ValidationId validationId) {
HTextFlowTarget tft =
getTextFlowTargetDAO().findById(textFlowTargetId, false);
if (tft != null) {
ValidationAction action =
ValidationFactoryProvider.getFactoryInstance()
.getValidationAction(validationId);
List<String> errorList =
action.validate(tft.getTextFlow().getContents().get(0), tft
.getContents().get(0));
return !errorList.isEmpty();
}
return null;
}
DocumentDAO getDocumentDAO() {
return serviceLocator.getInstance(DocumentDAO.class);
}
TextFlowTargetDAO getTextFlowTargetDAO() {
return serviceLocator.getInstance(TextFlowTargetDAO.class);
}
TextFlowDAO getTextFlowDAO() {
return serviceLocator.getInstance(TextFlowDAO.class);
}
private static class DocumentStatisticLoader extends
CacheLoader<DocumentLocaleKey, WordStatistic> {
private DocumentDAO getDocumentDAO() {
return ServiceLocator.instance().getInstance(DocumentDAO.class);
}
@Override
public WordStatistic load(DocumentLocaleKey key) throws Exception {
WordStatistic wordStatistic = getDocumentDAO().getWordStatistics(
key.getDocumentId(), key.getLocaleId());
return wordStatistic;
}
}
private static class HTextFlowTargetIdLoader extends
CacheLoader<DocumentLocaleKey, DocumentStatus> {
DocumentDAO getDocumentDAO() {
return ServiceLocator.instance().getInstance(DocumentDAO.class);
}
@Override
public DocumentStatus load(DocumentLocaleKey key) throws Exception {
DocumentDAO documentDAO = getDocumentDAO();
HTextFlowTarget target =
documentDAO.getLastTranslatedTarget(key.getDocumentId(),
key.getLocaleId());
DocumentStatus documentStatus = new DocumentStatus();
return updateDocumentStatus(documentDAO, documentStatus,
key.getDocumentId(), target);
}
}
private static class HTextFlowTargetValidationLoader extends
CacheLoader<Long, Map<ValidationId, Boolean>> {
@Override
public Map<ValidationId, Boolean> load(Long key) throws Exception {
return new HashMap<ValidationId, Boolean>();
}
}
private static DocumentStatus updateDocumentStatus(DocumentDAO documentDAO,
DocumentStatus documentStatus, Long documentId,
HTextFlowTarget target) {
Date lastTranslatedDate = null;
String lastTranslatedBy = "";
if (target != null) {
lastTranslatedDate = target.getLastChanged();
if (target.getLastModifiedBy() != null) {
lastTranslatedBy =
target.getLastModifiedBy().getAccount().getUsername();
}
}
HDocument document = documentDAO.findById(documentId, false);
documentStatus.update(
new DocumentId(document.getId(), document.getDocId()),
lastTranslatedDate, lastTranslatedBy);
return documentStatus;
}
@AllArgsConstructor
@EqualsAndHashCode
@Getter
public static final class DocumentLocaleKey implements Serializable {
private Long documentId;
private LocaleId localeId;
}
}