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
/
ProjectIterationFilesAction.java
370 lines (300 loc) · 10.6 KB
/
ProjectIterationFilesAction.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
* 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.action;
import java.io.InputStream;
import java.util.List;
import org.hibernate.validator.InvalidStateException;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.security.Restrict;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.international.StatusMessage.Severity;
import org.zanata.common.EntityStatus;
import org.zanata.common.LocaleId;
import org.zanata.common.MergeType;
import org.zanata.common.TransUnitWords;
import org.zanata.common.TranslationStats;
import org.zanata.dao.DocumentDAO;
import org.zanata.dao.LocaleDAO;
import org.zanata.dao.ProjectIterationDAO;
import org.zanata.exception.ZanataServiceException;
import org.zanata.model.HDocument;
import org.zanata.model.HLocale;
import org.zanata.model.HProjectIteration;
import org.zanata.rest.StringSet;
import org.zanata.rest.dto.extensions.ExtensionType;
import org.zanata.rest.dto.resource.Resource;
import org.zanata.rest.dto.resource.TranslationsResource;
import org.zanata.security.ZanataIdentity;
import org.zanata.service.DocumentService;
import org.zanata.service.TranslationFileService;
import org.zanata.service.TranslationService;
import javax.faces.context.FacesContext;
@Name("projectIterationFilesAction")
@Scope(ScopeType.PAGE)
public class ProjectIterationFilesAction
{
private String projectSlug;
private String iterationSlug;
private String localeId;
@In
private ZanataIdentity identity;
@In
private DocumentDAO documentDAO;
@In
private LocaleDAO localeDAO;
@In
private ProjectIterationDAO projectIterationDAO;
@In
private TranslationFileService translationFileServiceImpl;
@In
private TranslationService translationServiceImpl;
@In
private DocumentService documentServiceImpl;
private List<HDocument> iterationDocuments;
private String documentNameFilter;
private TranslationFileUploadHelper translationFileUpload;
private DocumentFileUploadHelper documentFileUpload;
public void initialize()
{
this.iterationDocuments = this.documentDAO.getAllByProjectIteration(this.projectSlug, this.iterationSlug);
this.translationFileUpload = new TranslationFileUploadHelper();
this.documentFileUpload = new DocumentFileUploadHelper();
}
public HLocale getLocale()
{
return localeDAO.findByLocaleId(new LocaleId(localeId));
}
public boolean filterDocumentByName( Object docObject )
{
final HDocument document = (HDocument)docObject;
if( this.documentNameFilter != null && this.documentNameFilter.length() > 0 )
{
return document.getName().toLowerCase().contains( this.documentNameFilter.toLowerCase() );
}
else
{
return true;
}
}
public TransUnitWords getTransUnitWordsForDocument(HDocument doc)
{
TranslationStats documentStats = this.documentDAO.getStatistics(doc.getId(), new LocaleId(this.localeId));
return documentStats.getWordCount();
}
@Restrict("#{projectIterationFilesAction.fileUploadAllowed}")
public String uploadTranslationFile()
{
TranslationsResource transRes = null;
try
{
// process the file
transRes = this.translationFileServiceImpl.parseTranslationFile(this.translationFileUpload.getFileContents(),
this.translationFileUpload.getFileName());
// translate it
List<String> warnings =
this.translationServiceImpl.translateAllInDoc(this.projectSlug, this.iterationSlug, this.translationFileUpload.getDocId(),
new LocaleId(this.localeId), transRes, new StringSet(ExtensionType.GetText.toString()),
this.translationFileUpload.getMergeTranslations() ? MergeType.AUTO : MergeType.IMPORT);
StringBuilder facesInfoMssg = new StringBuilder("File {0} uploaded.");
if (!warnings.isEmpty())
{
facesInfoMssg.append(" There were some warnings, see below.");
}
FacesMessages.instance().add(Severity.INFO, facesInfoMssg.toString(), this.translationFileUpload.getFileName());
for (String warning : warnings)
{
FacesMessages.instance().add(Severity.WARN, warning);
}
}
catch (ZanataServiceException zex)
{
FacesMessages.instance().add(Severity.ERROR, zex.getMessage(), this.translationFileUpload.getFileName());
}
// NB This needs to be done as for some reason seam is losing the parameters when redirecting
// This is efectively the same as returning void
return FacesContext.getCurrentInstance().getViewRoot().getViewId();
}
@Restrict("#{projectIterationFilesAction.documentUploadAllowed}")
public String uploadDocumentFile()
{
try
{
Resource doc = this.translationFileServiceImpl.parseDocumentFile(this.documentFileUpload.getFileContents(),
this.documentFileUpload.getDocumentPath(), this.documentFileUpload.getFileName());
// TODO Copy Trans values
// Extensions are hard-coded to GetText, since it is the only supported format at the time
this.documentServiceImpl.saveDocument(this.projectSlug, this.iterationSlug,
doc, new StringSet(ExtensionType.GetText.toString()),
false);
FacesMessages.instance().add(Severity.INFO, "Document file {0} uploaded.", this.documentFileUpload.getFileName());
}
catch (ZanataServiceException zex)
{
FacesMessages.instance().add(Severity.ERROR, zex.getMessage(), this.documentFileUpload.getFileName());
}
catch (InvalidStateException isex)
{
FacesMessages.instance().add(Severity.ERROR, "Invalid arguments");
}
// NB This needs to be done as for some reason seam is losing the parameters when redirecting
// This is efectively the same as returning void
return FacesContext.getCurrentInstance().getViewRoot().getViewId();
}
public boolean isFileUploadAllowed()
{
HProjectIteration projectIteration = this.projectIterationDAO.getBySlug(projectSlug, iterationSlug);
HLocale hLocale = this.localeDAO.findByLocaleId( new LocaleId(localeId) );
return projectIteration.getStatus() == EntityStatus.ACTIVE
&& identity != null && identity.hasPermission("modify-translation", projectIteration, hLocale);
}
public boolean isDocumentUploadAllowed()
{
HProjectIteration projectIteration = this.projectIterationDAO.getBySlug(projectSlug, iterationSlug);
return projectIteration.getStatus() == EntityStatus.ACTIVE
&& identity != null && identity.hasPermission("import-template", projectIteration);
}
public List<HDocument> getIterationDocuments()
{
return iterationDocuments;
}
public void setIterationDocuments(List<HDocument> iterationDocuments)
{
this.iterationDocuments = iterationDocuments;
}
public String getProjectSlug()
{
return projectSlug;
}
public void setProjectSlug(String projectSlug)
{
this.projectSlug = projectSlug;
}
public String getIterationSlug()
{
return iterationSlug;
}
public void setIterationSlug(String iterationSlug)
{
this.iterationSlug = iterationSlug;
}
public String getLocaleId()
{
return localeId;
}
public void setLocaleId(String localeId)
{
this.localeId = localeId;
}
public String getDocumentNameFilter()
{
return documentNameFilter;
}
public void setDocumentNameFilter(String documentNameFilter)
{
this.documentNameFilter = documentNameFilter;
}
public TranslationFileUploadHelper getTranslationFileUpload()
{
return translationFileUpload;
}
public DocumentFileUploadHelper getDocumentFileUpload()
{
return documentFileUpload;
}
/**
* Helper class to upload translation files.
*/
public static class TranslationFileUploadHelper
{
private String docId;
private InputStream fileContents;
private String fileName;
private boolean mergeTranslations = true; // Merge by default
public String getDocId()
{
return docId;
}
public void setDocId(String docId)
{
this.docId = docId;
}
public InputStream getFileContents()
{
return fileContents;
}
public void setFileContents(InputStream fileContents)
{
this.fileContents = fileContents;
}
public String getFileName()
{
return fileName;
}
public void setFileName(String fileName)
{
this.fileName = fileName;
}
public boolean getMergeTranslations()
{
return mergeTranslations;
}
public void setMergeTranslations(boolean mergeTranslations)
{
this.mergeTranslations = mergeTranslations;
}
}
/**
* Helper class to upload documents.
*/
public static class DocumentFileUploadHelper
{
private InputStream fileContents;
private String fileName;
private String documentPath;
public InputStream getFileContents()
{
return fileContents;
}
public void setFileContents(InputStream fileContents)
{
this.fileContents = fileContents;
}
public String getFileName()
{
return fileName;
}
public void setFileName(String fileName)
{
this.fileName = fileName;
}
public String getDocumentPath()
{
return documentPath;
}
public void setDocumentPath(String documentPath)
{
this.documentPath = documentPath;
}
}
}