This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
VersionGroupHomeAction.java
546 lines (469 loc) · 18.5 KB
/
VersionGroupHomeAction.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/*
* Copyright 2013, 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.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nullable;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang.StringUtils;
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.security.management.JpaIdentityStore;
import org.zanata.annotation.CachedMethodResult;
import org.zanata.common.LocaleId;
import org.zanata.dao.VersionGroupDAO;
import org.zanata.model.HAccount;
import org.zanata.model.HLocale;
import org.zanata.model.HPerson;
import org.zanata.model.HProject;
import org.zanata.model.HProjectIteration;
import org.zanata.service.VersionGroupService;
import org.zanata.service.VersionLocaleKey;
import org.zanata.ui.model.statistic.WordStatistic;
import org.zanata.util.StatisticsUtil;
import org.zanata.util.ZanataMessages;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
/**
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a>
*/
@Name("versionGroupHomeAction")
@Scope(ScopeType.PAGE)
public class VersionGroupHomeAction extends AbstractSortAction implements
Serializable {
private static final long serialVersionUID = 1L;
@In
private VersionGroupService versionGroupServiceImpl;
@In
private ZanataMessages zanataMessages;
@In(required = false, value = JpaIdentityStore.AUTHENTICATED_USER)
private HAccount authenticatedAccount;
@In
private VersionGroupDAO versionGroupDAO;
@Getter
@Setter
private String slug;
@Getter
private boolean pageRendered = false;
@Getter
@Setter
private HLocale selectedLocale;
@Getter
@Setter
private HProjectIteration selectedVersion;
@Getter
private WordStatistic overallStatistic;
@Setter
@Getter
private String projectQuery;
@Setter
@Getter
private String languageQuery;
private List<HLocale> activeLocales;
private List<HProjectIteration> projectIterations;
private Map<VersionLocaleKey, WordStatistic> statisticMap;
private Map<LocaleId, List<HProjectIteration>> missingLocaleVersionMap;
@Getter
private SortingType projectSortingList = new SortingType(
Lists.newArrayList(SortingType.SortOption.ALPHABETICAL,
SortingType.SortOption.HOURS,
SortingType.SortOption.PERCENTAGE,
SortingType.SortOption.WORDS));
private final LanguageComparator languageComparator =
new LanguageComparator(getLanguageSortingList());
private final VersionComparator versionComparator = new VersionComparator(
getProjectSortingList());
public void setPageRendered(boolean pageRendered) {
if (pageRendered) {
loadStatistics();
}
this.pageRendered = pageRendered;
}
private class LanguageComparator implements Comparator<HLocale> {
private SortingType sortingType;
@Setter
private Long selectedVersionId;
public LanguageComparator(SortingType sortingType) {
this.sortingType = sortingType;
}
@Override
public int compare(HLocale compareFrom, HLocale compareTo) {
final HLocale item1, item2;
if (sortingType.isDescending()) {
item1 = compareFrom;
item2 = compareTo;
} else {
item1 = compareTo;
item2 = compareFrom;
}
SortingType.SortOption selectedSortOption =
sortingType.getSelectedSortOption();
// Need to get statistic for comparison
if (!selectedSortOption.equals(SortingType.SortOption.ALPHABETICAL)) {
WordStatistic wordStatistic1;
WordStatistic wordStatistic2;
if (selectedVersionId == null) {
wordStatistic1 =
getStatisticsForLocale(item1.getLocaleId());
wordStatistic2 =
getStatisticsForLocale(item2.getLocaleId());
} else {
wordStatistic1 =
statisticMap.get(new VersionLocaleKey(
selectedVersionId, item1.getLocaleId()));
wordStatistic2 =
statisticMap.get(new VersionLocaleKey(
selectedVersionId, item2.getLocaleId()));
}
switch (selectedSortOption) {
case PERCENTAGE:
return Double.compare(
wordStatistic1.getPercentTranslated(),
wordStatistic2.getPercentTranslated());
case HOURS:
return Double.compare(wordStatistic1.getRemainingHours(),
wordStatistic2.getRemainingHours());
case WORDS:
return Double.compare(wordStatistic1.getUntranslated(),
wordStatistic2.getUntranslated());
}
} else {
return item1.retrieveDisplayName().compareTo(
item2.retrieveDisplayName());
}
return 0;
}
}
private class VersionComparator implements Comparator<HProjectIteration> {
private SortingType sortingType;
@Setter
private LocaleId selectedLocaleId;
public VersionComparator(SortingType sortingType) {
this.sortingType = sortingType;
}
@Override
public int compare(HProjectIteration compareFrom,
HProjectIteration compareTo) {
final HProjectIteration item1, item2;
if (sortingType.isDescending()) {
item1 = compareFrom;
item2 = compareTo;
} else {
item1 = compareTo;
item2 = compareFrom;
}
SortingType.SortOption selectedSortOption =
sortingType.getSelectedSortOption();
// Need to get statistic for comparison
if (!selectedSortOption.equals(SortingType.SortOption.ALPHABETICAL)) {
WordStatistic wordStatistic1;
WordStatistic wordStatistic2;
if (selectedLocaleId != null) {
wordStatistic1 =
statisticMap.get(new VersionLocaleKey(
item1.getId(), selectedLocaleId));
wordStatistic2 =
statisticMap.get(new VersionLocaleKey(
item2.getId(), selectedLocaleId));
} else {
wordStatistic1 = getStatisticForProject(item1.getId());
wordStatistic2 = getStatisticForProject(item2.getId());
}
switch (selectedSortOption) {
case PERCENTAGE:
return Double.compare(
wordStatistic1.getPercentTranslated(),
wordStatistic2.getPercentTranslated());
case HOURS:
return Double.compare(wordStatistic1.getRemainingHours(),
wordStatistic2.getRemainingHours());
case WORDS:
return Double.compare(wordStatistic1.getUntranslated(),
wordStatistic2.getUntranslated());
}
} else {
return item1.getProject().getName().toLowerCase()
.compareTo(item2.getProject().getName().toLowerCase());
}
return 0;
}
}
public boolean isUserProjectMaintainer() {
return authenticatedAccount != null
&& authenticatedAccount.getPerson().isMaintainerOfProjects();
}
public int getFilteredProjectSize() {
if (getSelectedLocale() == null) {
return 0;
} else {
return getFilteredProjectIterations().size();
}
}
public int getFilteredLocalesSize() {
if (getSelectedVersion() == null) {
return 0;
} else {
return getFilteredLocales().size();
}
}
/**
* Sort language list based on overall locale statistic for the group
*/
public void sortLanguageList() {
languageComparator.setSelectedVersionId(null);
Collections.sort(activeLocales, languageComparator);
}
/**
* Sort language list based on statistics of the version on selected locale
*/
public void sortLanguageList(Long versionId) {
languageComparator.setSelectedVersionId(versionId);
Collections.sort(activeLocales, languageComparator);
}
/**
* Sort project list based on selected locale - language tab
*
* @param localeId
*/
public void sortProjectList(LocaleId localeId) {
versionComparator.setSelectedLocaleId(localeId);
Collections.sort(projectIterations, versionComparator);
}
/**
* Sort project list based on version's overall statistics
*/
public void sortProjectList() {
versionComparator.setSelectedLocaleId(null);
Collections.sort(projectIterations, versionComparator);
}
public List<HLocale> getActiveLocales() {
if (activeLocales == null) {
Set<HLocale> groupActiveLocales =
versionGroupServiceImpl.getGroupActiveLocales(getSlug());
activeLocales = Lists.newArrayList(groupActiveLocales);
}
Collections.sort(activeLocales, languageComparator);
return activeLocales;
}
public List<HLocale> getFilteredLocales() {
List<HLocale> unfiltered = getActiveLocales();
if (StringUtils.isEmpty(languageQuery)) {
return unfiltered;
}
Collection<HLocale> filtered =
Collections2.filter(unfiltered, new Predicate<HLocale>() {
@Override
public boolean apply(@Nullable HLocale input) {
return input.retrieveDisplayName().toLowerCase()
.contains(languageQuery.toLowerCase());
}
});
return Lists.newArrayList(filtered);
}
@CachedMethodResult
public DisplayUnit getStatisticFigureForProjectWithLocale(
SortingType.SortOption sortOption, LocaleId localeId,
Long projectIterationId) {
WordStatistic statistic =
statisticMap.get(new VersionLocaleKey(projectIterationId,
localeId));
return getDisplayUnit(sortOption, statistic);
}
@CachedMethodResult
public DisplayUnit getStatisticFigureForLocale(
SortingType.SortOption sortOption, LocaleId localeId) {
WordStatistic statistic = getStatisticsForLocale(localeId);
return getDisplayUnit(sortOption, statistic);
}
@CachedMethodResult
public DisplayUnit getStatisticFigureForProject(
SortingType.SortOption sortOption, Long projectIterationId) {
WordStatistic statistic = getStatisticForProject(projectIterationId);
return getDisplayUnit(sortOption, statistic);
}
@CachedMethodResult
public WordStatistic getStatisticsForLocale(LocaleId localeId) {
WordStatistic statistic = new WordStatistic();
for (Map.Entry<VersionLocaleKey, WordStatistic> entry : statisticMap
.entrySet()) {
if (entry.getKey().getLocaleId().equals(localeId)) {
statistic.add(entry.getValue());
}
}
statistic
.setRemainingHours(StatisticsUtil.getRemainingHours(statistic));
return statistic;
}
@CachedMethodResult
public WordStatistic getStatisticForProject(Long projectIterationId) {
WordStatistic statistic = new WordStatistic();
for (Map.Entry<VersionLocaleKey, WordStatistic> entry : statisticMap
.entrySet()) {
if (entry.getKey().getProjectIterationId()
.equals(projectIterationId)) {
statistic.add(entry.getValue());
}
}
statistic
.setRemainingHours(StatisticsUtil.getRemainingHours(statistic));
return statistic;
}
@CachedMethodResult
public WordStatistic getSelectedLocaleStatistic(Long projectIterationId) {
return statisticMap.get(new VersionLocaleKey(projectIterationId,
selectedLocale.getLocaleId()));
}
@CachedMethodResult
public WordStatistic getSelectedVersionStatistic(LocaleId localeId) {
return statisticMap.get(new VersionLocaleKey(selectedVersion.getId(),
localeId));
}
public List<HPerson> getMaintainers() {
List<HPerson> list = versionGroupDAO.getMaintainersBySlug(getSlug());
Collections.sort(list, VersionGroupHome.PERSON_COMPARATOR);
return list;
}
private Map<LocaleId, List<HProjectIteration>> getMissingLocaleVersionMap() {
if (missingLocaleVersionMap == null) {
missingLocaleVersionMap =
versionGroupServiceImpl
.getMissingLocaleVersionMap(getSlug());
}
return missingLocaleVersionMap;
}
/**
* Search for locale that is not activated in given version
*
* @param version
*/
public List<LocaleId> getMissingLocale(HProjectIteration version) {
List<LocaleId> result = Lists.newArrayList();
for (Map.Entry<LocaleId, List<HProjectIteration>> entry : getMissingLocaleVersionMap()
.entrySet()) {
if (entry.getValue().contains(version)) {
result.add(entry.getKey());
}
}
return result;
}
public String getMissingLocaleTitle(HProjectIteration version) {
int size = getMissingLocale(version).size();
if (size > 1) {
return zanataMessages.getMessage("jsf.LanguagesMissingProject",
size);
}
return zanataMessages.getMessage("jsf.LanguageMissingProject", size);
}
/**
* Search for version that doesn't activate given locale
*
* @param localeId
*/
public List<HProjectIteration> getMissingVersion(LocaleId localeId) {
if (getMissingLocaleVersionMap().containsKey(localeId)) {
return getMissingLocaleVersionMap().get(localeId);
}
return Lists.newArrayList();
}
public String getMissingVersionTitle(LocaleId localeId) {
int size = getMissingVersion(localeId).size();
if (size > 1) {
return zanataMessages.getMessage("jsf.ProjectsMissingLanguage",
size);
}
return zanataMessages.getMessage("jsf.ProjectMissingLanguage", size);
}
public boolean isLocaleActivatedInVersion(HProjectIteration version,
LocaleId localeId) {
List<HProjectIteration> versionList =
getMissingLocaleVersionMap().get(localeId);
return !versionList.contains(version);
}
/**
* Load up statistics for all project versions in all active locales in the
* group.
*/
@Override
protected void loadStatistics() {
statisticMap = Maps.newHashMap();
for (HLocale locale : getActiveLocales()) {
statisticMap.putAll(versionGroupServiceImpl.getLocaleStatistic(
getSlug(), locale.getLocaleId()));
}
overallStatistic = new WordStatistic();
for (Map.Entry<VersionLocaleKey, WordStatistic> entry : statisticMap
.entrySet()) {
overallStatistic.add(entry.getValue());
}
overallStatistic.setRemainingHours(StatisticsUtil
.getRemainingHours(overallStatistic));
}
public List<HProjectIteration> getProjectIterations() {
if (projectIterations == null) {
projectIterations =
versionGroupServiceImpl
.getNonObsoleteProjectIterationsBySlug(slug);
}
Collections.sort(projectIterations, versionComparator);
return projectIterations;
}
public List<HProjectIteration> getFilteredProjectIterations() {
List<HProjectIteration> unfiltered = getProjectIterations();
if (StringUtils.isEmpty(projectQuery)) {
return unfiltered;
}
Collection<HProjectIteration> filtered =
Collections2.filter(unfiltered,
new Predicate<HProjectIteration>() {
@Override
public boolean apply(
@Nullable HProjectIteration input) {
HProject project = input.getProject();
return project.getName().toLowerCase()
.contains(projectQuery.toLowerCase());
}
});
return Lists.newArrayList(filtered);
}
@Override
public void resetPageData() {
projectIterations = null;
activeLocales = null;
selectedLocale = null;
selectedVersion = null;
missingLocaleVersionMap = null;
loadStatistics();
}
@Override
String getMessage(String key, Object... args) {
return zanataMessages.getMessage(key, args);
}
}