This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
VersionGroupHomeAction.java
457 lines (389 loc) · 15.6 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
/*
* Copyright 2012, 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.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
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.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.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 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 OverallStatistic overallStatistic;
private List<HLocale> activeLocales;
private List<HProjectIteration> projectIterations;
private Map<VersionLocaleKey, WordStatistic> statisticMap;
private Map<LocaleId, List<HProjectIteration>> missingLocaleVersionMap;
private SortingType languageSortingList;
private SortingType projectSortingList;
private final LanguageComparator languageComparator =
new LanguageComparator(getLanguageSortingList());
private final VersionComparator versionComparator = new VersionComparator(
getProjectSortingList());
public void setPageRendered(boolean pageRendered) {
if (pageRendered) {
loadStatistic();
}
this.pageRendered = pageRendered;
}
public SortingType getLanguageSortingList() {
if (languageSortingList == null) {
languageSortingList =
new SortingType(Lists.newArrayList(
SortingType.ALPHABETICAL, SortingType.HOURS,
SortingType.PERCENTAGE));
}
return languageSortingList;
}
public SortingType getProjectSortingList() {
if (projectSortingList == null) {
projectSortingList =
new SortingType(Lists.newArrayList(
SortingType.ALPHABETICAL, SortingType.HOURS,
SortingType.PERCENTAGE, SortingType.WORDS));
}
return projectSortingList;
}
private class LanguageComparator implements Comparator<HLocale> {
private SortingType sortingType;
public LanguageComparator(SortingType sortingType) {
this.sortingType = sortingType;
}
@Override
public int compare(HLocale locale, HLocale locale2) {
final HLocale item1, item2;
if (sortingType.isDescending()) {
item1 = locale;
item2 = locale2;
} else {
item1 = locale2;
item2 = locale;
}
String selectedSortOption = sortingType.getSelectedSortOption();
// Need to get statistic for comparison
if (!selectedSortOption.equals(SortingType.ALPHABETICAL)) {
WordStatistic wordStatistic1 =
getStatisticForLocale(item1.getLocaleId());
WordStatistic wordStatistic2 =
getStatisticForLocale(item2.getLocaleId());
if (selectedSortOption.equals(SortingType.PERCENTAGE)) {
return Double.compare(
wordStatistic1.getPercentTranslated(),
wordStatistic2.getPercentTranslated());
} else if (selectedSortOption.equals(SortingType.HOURS)) {
return Double.compare(wordStatistic1.getRemainingHours(),
wordStatistic2.getRemainingHours());
}
} else {
return item1.retrieveDisplayName().compareTo(
item2.retrieveDisplayName());
}
return 0;
}
}
private class VersionComparator implements Comparator<HProjectIteration> {
private SortingType sortingType;
public VersionComparator(SortingType sortingType) {
this.sortingType = sortingType;
}
@Override
public int compare(HProjectIteration locale, HProjectIteration locale2) {
final HProjectIteration item1, item2;
if (sortingType.isDescending()) {
item1 = locale;
item2 = locale2;
} else {
item1 = locale2;
item2 = locale;
}
String selectedSortOption = sortingType.getSelectedSortOption();
// Need to get statistic for comparison
if (!selectedSortOption.equals(SortingType.ALPHABETICAL)) {
WordStatistic wordStatistic1 =
statisticMap.get(new VersionLocaleKey(item1.getId(),
selectedLocale.getLocaleId()));
WordStatistic wordStatistic2 =
statisticMap.get(new VersionLocaleKey(item2.getId(),
selectedLocale.getLocaleId()));
if (selectedSortOption.equals(SortingType.PERCENTAGE)) {
return Double.compare(
wordStatistic1.getPercentTranslated(),
wordStatistic2.getPercentTranslated());
} else if (selectedSortOption.equals(SortingType.HOURS)) {
return Double.compare(wordStatistic1.getRemainingHours(),
wordStatistic2.getRemainingHours());
} else if (selectedSortOption.equals(SortingType.WORDS)) {
return Integer.compare(wordStatistic1.getTotal(),
wordStatistic2.getTotal());
}
} else {
return item1.getProject().getName()
.compareTo(item2.getProject().getName());
}
return 0;
}
}
public boolean isUserProjectMaintainer() {
return authenticatedAccount != null
&& authenticatedAccount.getPerson().isMaintainerOfProjects();
}
public void sortLanguageList() {
Collections.sort(activeLocales, languageComparator);
}
public void sortProjectList() {
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;
}
@CachedMethodResult
public String getStatisticFigureForLocale(String sortOption,
LocaleId localeId) {
WordStatistic statistic = getStatisticForLocale(localeId);
return getStatisticFigure(sortOption, statistic);
}
@CachedMethodResult
public String getStatisticFigureForProjectWithLocale(String sortOption,
LocaleId localeId, Long versionId) {
WordStatistic statistic =
statisticMap.get(new VersionLocaleKey(versionId, localeId));
return getStatisticFigure(sortOption, statistic);
}
@CachedMethodResult
public String
getStatisticFigureForProject(String sortOption, Long versionId) {
WordStatistic statistic = getStatisticForProject(versionId);
return getStatisticFigure(sortOption, statistic);
}
private String
getStatisticFigure(String sortOption, WordStatistic statistic) {
if (sortOption.equals(SortingType.HOURS)) {
return StatisticsUtil.formatHours(statistic.getRemainingHours());
} else if (sortOption.equals(SortingType.WORDS)) {
return String.valueOf(statistic.getTotal());
} else {
return StatisticsUtil.formatPercentage(statistic
.getPercentTranslated()) + "%";
}
}
public String getStatisticUnit(String sortOption) {
if (sortOption.equals(SortingType.HOURS)) {
return zanataMessages.getMessage("jsf.stats.HoursRemaining");
} else if (sortOption.equals(SortingType.WORDS)) {
return zanataMessages.getMessage("jsf.Words");
} else {
return zanataMessages.getMessage("jsf.Translated");
}
}
@CachedMethodResult
public WordStatistic getStatisticForLocale(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 versionId) {
WordStatistic statistic = new WordStatistic();
for (Map.Entry<VersionLocaleKey, WordStatistic> entry : statisticMap
.entrySet()) {
if (entry.getKey().getVersionId().equals(versionId)) {
statistic.add(entry.getValue());
}
}
statistic
.setRemainingHours(StatisticsUtil.getRemainingHours(statistic));
return statistic;
}
@CachedMethodResult
public WordStatistic getSelectedLocaleStatistic(Long versionId) {
return statisticMap.get(new VersionLocaleKey(versionId, selectedLocale
.getLocaleId()));
}
@CachedMethodResult
public WordStatistic getSelectedVersionStatistic(LocaleId localeId) {
return statisticMap.get(new VersionLocaleKey(selectedVersion.getId(),
localeId));
}
public List<HPerson> getMaintainers() {
return versionGroupDAO.getMaintainersBySlug(getSlug());
}
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
* @return
*/
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
* @return
*/
public List<HProjectIteration> getMissingVersion(LocaleId localeId) {
return getMissingLocaleVersionMap().get(localeId);
}
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 isLocaleActivateInVersion(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.
*/
private void loadStatistic() {
statisticMap = Maps.newHashMap();
for (HLocale locale : getActiveLocales()) {
statisticMap.putAll(versionGroupServiceImpl.getLocaleStatistic(
getSlug(), locale.getLocaleId()));
}
WordStatistic overallWordStatistic = new WordStatistic();
int totalWordCount = 0;
for (Map.Entry<VersionLocaleKey, WordStatistic> entry : statisticMap
.entrySet()) {
overallWordStatistic.add(entry.getValue());
totalWordCount += entry.getValue().getTotal();
}
overallWordStatistic.setRemainingHours(StatisticsUtil
.getRemainingHours(overallWordStatistic));
int totalMessageCount =
versionGroupServiceImpl.getTotalMessageCount(getSlug());
overallStatistic =
new OverallStatistic(totalWordCount, totalMessageCount,
overallWordStatistic);
}
@AllArgsConstructor
@Getter
@Setter
public final class OverallStatistic {
private int totalWordCount;
private int totalMessageCount;
private WordStatistic statistic;
}
public List<HProjectIteration> getProjectIterations() {
if (projectIterations == null) {
projectIterations = versionGroupDAO.getVersionsBySlug(slug);
}
Collections.sort(projectIterations, versionComparator);
return projectIterations;
}
// reset all page cached statistics
public void resetPageData() {
projectIterations = null;
activeLocales = null;
loadStatistic();
}
}