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
245 lines (212 loc) · 8.77 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
/*
* 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.Create;
import org.jboss.seam.annotations.Destroy;
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.zanata.cache.CacheWrapper;
import org.zanata.cache.EhcacheWrapper;
import org.zanata.common.ContentState;
import org.zanata.common.LocaleId;
import org.zanata.dao.DocumentDAO;
import org.zanata.dao.TextFlowDAO;
import org.zanata.dao.TextFlowTargetDAO;
import org.zanata.events.TextFlowTargetStateEvent;
import org.zanata.model.HDocument;
import org.zanata.model.HTextFlowTarget;
import org.zanata.service.TranslationStateCache;
import org.zanata.service.ValidationFactoryProvider;
import org.zanata.service.ValidationService;
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")
@Scope(ScopeType.APPLICATION)
public class TranslationStateCacheImpl implements TranslationStateCache
{
private static final String BASE = TranslationStateCacheImpl.class.getName();
private static final String DOC_STATUS_CACHE_NAME = BASE + ".docStatusCache";
private static final String TFT_VALIDATION_CACHE_NAME = BASE + ".targetValidationCache";
@In
private TextFlowDAO textFlowDAO;
@In
private DocumentDAO documentDAO;
@In
private TextFlowTargetDAO textFlowTargetDAO;
@In
private ValidationService validationServiceImpl;
private CacheManager cacheManager;
private CacheWrapper<TranslatedDocumentKey, DocumentStatus> docStatusCache;
private CacheWrapper<Long, Map<ValidationId, Boolean>> targetValidationCache;
private CacheLoader<TranslatedDocumentKey, DocumentStatus> docStatusLoader;
private CacheLoader<Long, Map<ValidationId, Boolean>> targetValidationLoader;
public TranslationStateCacheImpl()
{
// constructor for Seam
this.docStatusLoader = new HTextFlowTargetIdLoader();
this.targetValidationLoader = new HTextFlowTargetValidationLoader();
}
public TranslationStateCacheImpl(
CacheLoader<TranslatedDocumentKey, DocumentStatus> docStatsLoader,
CacheLoader<Long, Map<ValidationId, Boolean>> targetValidationLoader,
TextFlowTargetDAO textFlowTargetDAO,
ValidationService validationServiceImpl)
{
// constructor for testing
this.docStatusLoader = docStatsLoader;
this.targetValidationLoader = targetValidationLoader;
this.textFlowTargetDAO = textFlowTargetDAO;
this.validationServiceImpl = validationServiceImpl;
}
@Create
public void create()
{
cacheManager = CacheManager.create();
docStatusCache = EhcacheWrapper.create(DOC_STATUS_CACHE_NAME, cacheManager, docStatusLoader);
targetValidationCache = EhcacheWrapper.create(TFT_VALIDATION_CACHE_NAME, cacheManager, targetValidationLoader);
}
@Destroy
public void destroy()
{
cacheManager.shutdown();
}
/**
* This method contains all logic to be run immediately after a Text Flow Target has
* been successfully translated.
*/
@Observer(TextFlowTargetStateEvent.EVENT_NAME)
@Override
public void textFlowStateUpdated(TextFlowTargetStateEvent event)
{
// updateDocStatusCache(event.getDocumentId(), event.getLocaleId(), event.getTextFlowTargetId());
// TODO enhance TextFlowTargetStateEvent and update this cache directly (no database I/O) rather than invalidating
invalidateDocLastTranslatedCache(event.getDocumentId(), event.getLocaleId());
invalidateTargetValidationCache(event.getTextFlowTargetId());
}
@Override
public DocumentStatus getDocumentStatus(Long documentId, LocaleId localeId)
{
return docStatusCache.getWithLoader(new TranslatedDocumentKey(documentId, localeId));
}
private void invalidateDocLastTranslatedCache(Long documentId, LocaleId localeId)
{
docStatusCache.remove(new TranslatedDocumentKey(documentId, localeId));
}
@Override
public Boolean textFlowTargetHasWarningOrError(Long targetId, ValidationId validationId)
{
Map<ValidationId, Boolean> cacheEntry = targetValidationCache.getWithLoader(targetId);
if (!cacheEntry.containsKey(validationId))
{
Boolean result = loadTargetValidation(targetId, validationId);
cacheEntry.put(validationId, result);
}
return cacheEntry.get(validationId);
}
private void updateDocStatusCache(Long documentId, LocaleId localeId, Long updatedTargetId)
{
DocumentStatus documentStatus = docStatusCache.get(new TranslatedDocumentKey(documentId, localeId));
HTextFlowTarget target = textFlowTargetDAO.findById(updatedTargetId, false);
updateDocumentStatus(documentStatus, documentId, localeId, target);
}
private void invalidateTargetValidationCache(Long textFlowTargetId)
{
targetValidationCache.remove(textFlowTargetId);
}
private final class HTextFlowTargetIdLoader extends CacheLoader<TranslatedDocumentKey, DocumentStatus>
{
@Override
public DocumentStatus load(TranslatedDocumentKey key) throws Exception
{
HTextFlowTarget target = documentDAO.getLastTranslatedTarget(key.getDocumentId(), key.getLocaleId());
DocumentStatus documentStatus = new DocumentStatus();
return updateDocumentStatus(documentStatus, key.getDocumentId(), key.getLocaleId(), target);
}
}
private final class HTextFlowTargetValidationLoader extends CacheLoader<Long, Map<ValidationId, Boolean>>
{
@Override
public Map<ValidationId, Boolean> load(Long key) throws Exception
{
return new HashMap<ValidationId, Boolean>();
}
}
private Boolean loadTargetValidation(Long textFlowTargetId, ValidationId validationId)
{
HTextFlowTarget tft = textFlowTargetDAO.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;
}
private DocumentStatus updateDocumentStatus(DocumentStatus documentStatus, Long documentId, LocaleId localeId,
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);
boolean hasError = validationServiceImpl.runDocValidationsWithServerRules(document, localeId);
documentStatus.update(new DocumentId(document.getId(), document.getDocId()), lastTranslatedDate, lastTranslatedBy, hasError);
return documentStatus;
}
@AllArgsConstructor
@EqualsAndHashCode
public static final class TranslatedDocumentKey implements Serializable
{
private static final long serialVersionUID = 1L;
@Getter
private Long documentId;
@Getter
private LocaleId localeId;
}
}