-
Notifications
You must be signed in to change notification settings - Fork 51
/
DashboardAction.java
250 lines (220 loc) · 8.39 KB
/
DashboardAction.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
/*
* 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.Serializable;
import java.util.Date;
import java.util.List;
import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import org.apache.commons.lang3.StringUtils;
import javax.annotation.Nonnull;
import javax.enterprise.inject.Model;
import javax.faces.bean.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import org.zanata.dao.VersionGroupDAO;
import org.apache.deltaspike.jpa.api.transaction.Transactional;
import org.zanata.model.HIterationGroup;
import org.zanata.seam.security.IdentityManager;
import org.zanata.security.annotations.Authenticated;
import org.zanata.security.annotations.CheckLoggedIn;
import org.zanata.dao.AccountDAO;
import org.zanata.dao.ProjectDAO;
import org.zanata.i18n.Messages;
import org.zanata.model.HAccount;
import org.zanata.model.HLocale;
import org.zanata.model.HPerson;
import org.zanata.model.HProject;
import org.zanata.security.ZanataIdentity;
import org.zanata.service.ActivityService;
import org.zanata.service.GravatarService;
import org.zanata.ui.AbstractListFilter;
import org.zanata.service.LanguageTeamService;
import org.zanata.util.DateUtil;
import javax.annotation.Nullable;
@Named("dashboardAction")
@ViewScoped
@CheckLoggedIn
@Model
@Transactional
public class DashboardAction implements Serializable {
private static final long serialVersionUID = 1L;
@Inject
private GravatarService gravatarServiceImpl;
@Inject
private ActivityService activityServiceImpl;
@Inject
private LanguageTeamService languageTeamServiceImpl;
@Inject
private AccountDAO accountDAO;
@Inject
private ProjectDAO projectDAO;
@Inject
private ZanataIdentity identity;
@Inject
private Messages msgs;
@Inject
@Authenticated
private HAccount authenticatedAccount;
@Inject
private ProjectFilter projectList;
@Inject
private GroupFilter groupList;
@Inject
private IdentityManager identityManager;
public String getUserImageUrl() {
return gravatarServiceImpl
.getUserImageUrl(GravatarService.USER_IMAGE_SIZE);
}
public String getUsername() {
return authenticatedAccount.getPerson().getAccount().getUsername();
}
public String getUserFullName() {
return authenticatedAccount.getPerson().getName();
}
public String getUserLanguageTeams() {
HAccount account = accountDAO.findById(authenticatedAccount.getId());
return StringUtils.join(Collections2.transform(
account.getPerson().getLanguageMemberships(),
new Function<HLocale, Object>() {
@Nullable
@Override
public Object apply(@Nonnull HLocale locale) {
if (locale == null) {
throw new NullPointerException("locale");
}
return locale.retrieveDisplayName();
}
}), ", ");
}
public String getUserRoles() {
HAccount account = accountDAO.findById(authenticatedAccount.getId());
List<String> roles =
identityManager.getGrantedRoles(account.getUsername());
return StringUtils.join(roles, ", ");
}
public String getLastTranslatedTimeLapseMessage(HProject project) {
Date lastTranslatedDate = projectDAO.getLastTranslatedDate(project);
// TODO i18n needed
return lastTranslatedDate == null ? "never"
: DateUtil.getHowLongAgoDescription(lastTranslatedDate);
}
public String getShortTime(Date date) {
return DateUtil.formatShortDate(date);
}
public DashboardUserStats getTodayStats() {
Date now = new Date();
return activityServiceImpl.getDashboardUserStatistic(
authenticatedAccount.getPerson().getId(),
DateUtil.getStartOfDay(now), DateUtil.getEndOfTheDay(now));
}
public DashboardUserStats getWeekStats() {
Date now = new Date();
return activityServiceImpl.getDashboardUserStatistic(
authenticatedAccount.getPerson().getId(),
DateUtil.getStartOfWeek(now), DateUtil.getEndOfTheWeek(now));
}
public DashboardUserStats getMonthStats() {
Date now = new Date();
return activityServiceImpl.getDashboardUserStatistic(
authenticatedAccount.getPerson().getId(),
DateUtil.getStartOfMonth(now), DateUtil.getEndOfTheMonth(now));
}
public boolean isUserReviewer() {
return languageTeamServiceImpl
.isUserReviewer(authenticatedAccount.getPerson().getId());
}
public String getLastTranslatorMessage(HProject project) {
HPerson lastTrans = projectDAO.getLastTranslator(project);
if (lastTrans != null) {
String username = lastTrans.getName();
if (username == null || username.trim().isEmpty()) {
if (lastTrans.getAccount() != null) {
username = lastTrans.getAccount().getUsername();
}
} else {
username = lastTrans.getName();
}
return msgs.format(
"jsf.dashboard.activity.lastTranslatedBy.message",
username);
}
return "";
}
public boolean canCreateProject() {
return identity.hasPermission(new HProject(), "insert");
}
public boolean canCreateGroup() {
return identity.hasPermission(new HIterationGroup(), "insert");
}
/**
* Project list filter. Pages its elements directly from the database.
*/
public static class ProjectFilter extends AbstractListFilter<HProject>
implements Serializable {
private static final long serialVersionUID = -2473428615946542483L;
@Inject
@Authenticated
private HAccount authenticatedAccount;
@Inject
private ProjectDAO projectDAO;
@Override
protected List<HProject> fetchRecords(int start, int max,
String filter) {
return projectDAO.getProjectsForMaintainer(
authenticatedAccount.getPerson(), filter, start, max);
}
@Override
protected long fetchTotalRecords(String filter) {
return projectDAO.getMaintainedProjectCount(
authenticatedAccount.getPerson(), filter);
}
}
/**
* Group list filter. Pages its elements directly from the database.
*/
public static class GroupFilter extends AbstractListFilter<HIterationGroup>
implements Serializable {
private static final long serialVersionUID = 5698872972627107893L;
@Inject
@Authenticated
private HAccount authenticatedAccount;
@Inject
private VersionGroupDAO versionGroupDAO;
@Override
protected List<HIterationGroup> fetchRecords(int start, int max,
String filter) {
return versionGroupDAO.getGroupsByMaintainer(
authenticatedAccount.getPerson(), filter, start, max);
}
@Override
protected long fetchTotalRecords(String filter) {
return versionGroupDAO.getMaintainedGroupCount(
authenticatedAccount.getPerson(), filter);
}
}
public ProjectFilter getProjectList() {
return this.projectList;
}
public GroupFilter getGroupList() {
return this.groupList;
}
}