This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
VersionGroupHome.java
456 lines (385 loc) · 14.7 KB
/
VersionGroupHome.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
/*
* 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.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.annotation.Nullable;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;
import org.hibernate.criterion.NaturalIdentifier;
import org.hibernate.criterion.Restrictions;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.security.Restrict;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.international.StatusMessage;
import org.jboss.seam.security.management.JpaIdentityStore;
import org.zanata.common.EntityStatus;
import org.zanata.common.LocaleId;
import org.zanata.dao.PersonDAO;
import org.zanata.dao.ProjectIterationDAO;
import org.zanata.model.HAccount;
import org.zanata.model.HIterationGroup;
import org.zanata.model.HLocale;
import org.zanata.model.HPerson;
import org.zanata.model.HProjectIteration;
import org.zanata.seam.scope.FlashScopeBean;
import org.zanata.service.LocaleService;
import org.zanata.service.SlugEntityService;
import org.zanata.service.VersionGroupService;
import org.zanata.util.ZanataMessages;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
/**
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a>
*/
@Name("versionGroupHome")
public class VersionGroupHome extends SlugHome<HIterationGroup> {
private static final long serialVersionUID = 1L;
@Getter
@Setter
private String slug;
@In(required = false, value = JpaIdentityStore.AUTHENTICATED_USER)
private HAccount authenticatedAccount;
@In
private SlugEntityService slugEntityServiceImpl;
@In
private LocaleService localeServiceImpl;
@In
private PersonDAO personDAO;
@In
private VersionGroupService versionGroupServiceImpl;
@In
private ZanataMessages zanataMessages;
@In
private ProjectIterationDAO projectIterationDAO;
@In
private FlashScopeBean flashScope;
private List<HLocale> supportedLocales;
private List<SelectItem> statusList;
@Getter
@Setter
// locale id
private String newLanguage;
@Getter
@Setter
// Slug of project [space] version
private String newVersion;
@Getter
@Setter
// username
private String newMaintainer;
public void verifySlugAvailable(ValueChangeEvent e) {
String slug = (String) e.getNewValue();
validateSlug(slug, e.getComponent().getId());
}
public boolean validateSlug(String slug, String componentId) {
if (!isSlugAvailable(slug)) {
FacesMessages.instance().addToControl(componentId,
"This Group ID is not available");
return false;
}
return true;
}
public boolean isSlugAvailable(String slug) {
return slugEntityServiceImpl.isSlugAvailable(slug,
HIterationGroup.class);
}
@Override
@Restrict("#{s:hasPermission(versionGroupHome.instance, 'update')}")
public String persist() {
if (!validateSlug(getInstance().getSlug(), "slug"))
return null;
if (authenticatedAccount != null) {
getInstance().addMaintainer(authenticatedAccount.getPerson());
}
return super.persist();
}
@Override
@Restrict("#{s:hasPermission(versionGroupHome.instance, 'update')}")
public String update() {
return super.update();
}
@Override
public List<SelectItem> getStatusList() {
return getAvailableStatus();
}
public void setStatus(char initial) {
getInstance().setStatus(EntityStatus.valueOf(initial));
}
public List<HLocale> suggestLocales(final String query) {
if (supportedLocales == null) {
supportedLocales =
localeServiceImpl.getSupportedLocales();
}
Collection<HLocale> filtered =
Collections2.filter(supportedLocales, new Predicate<HLocale>() {
@Override
public boolean apply(@Nullable HLocale input) {
return input.getLocaleId().getId().startsWith(query)
|| input.retrieveDisplayName().toLowerCase()
.contains(query.toLowerCase());
}
});
return Lists.newArrayList(filtered);
}
public List<HProjectIteration> suggestVersions(final String query) {
return versionGroupServiceImpl.searchLikeSlugOrProjectSlug(query);
}
public List<HPerson> suggestMaintainers(final String query) {
return personDAO.findAllContainingName(query);
}
@Restrict("#{s:hasPermission(versionGroupHome.instance, 'update')}")
public void addLanguage() {
if (!validateLanguageName("newLanguage")) {
return; // not success
}
HLocale locale =
localeServiceImpl.getByLocaleId(new LocaleId(newLanguage));
getInstance().getActiveLocales().add(locale);
super.update();
this.newLanguage = "";
addMessage(
StatusMessage.Severity.INFO,
zanataMessages.getMessage("jsf.LanguageAddedToGroup",
locale.retrieveDisplayName()));
}
/**
* Use FlashScopeBean to store message in page. Multiple ajax requests for
* re-rendering statistics after updating will clear FacesMessages.
*
* @param severity
* @param message
*/
private void addMessage(StatusMessage.Severity severity, String message) {
StatusMessage statusMessage =
new StatusMessage(severity, null, null, message, null);
statusMessage.interpolate();
flashScope.setAttribute("message", statusMessage);
}
@Restrict("#{s:hasPermission(versionGroupHome.instance, 'update')}")
public void addVersion() {
if (!validateVersion("newVersion")) {
return; // not success
}
String[] slugs = newVersion.split(" ");
HProjectIteration version =
projectIterationDAO.getBySlug(slugs[0], slugs[1]);
getInstance().getProjectIterations().add(version);
super.update();
this.newVersion = "";
addMessage(
StatusMessage.Severity.INFO,
zanataMessages.getMessage("jsf.VersionAddedToGroup",
version.getSlug()));
}
@Restrict("#{s:hasPermission(versionGroupHome.instance, 'update')}")
public void addMaintainer() {
if (!validateMaintainer("newMaintainer")) {
return; // not success
}
HPerson maintainer = personDAO.findByUsername(newMaintainer);
getInstance().getMaintainers().add(maintainer);
super.update();
this.newMaintainer = "";
addMessage(StatusMessage.Severity.INFO, zanataMessages.getMessage(
"jsf.MaintainerAddedToGroup", maintainer.getName()));
}
@Restrict("#{s:hasPermission(versionGroupHome.instance, 'update')}")
public void removeLanguage(HLocale locale) {
getInstance().getActiveLocales().remove(locale);
super.update();
addMessage(
StatusMessage.Severity.INFO,
zanataMessages.getMessage("jsf.LanguageRemoveFromGroup",
locale.retrieveDisplayName()));
}
@Restrict("#{s:hasPermission(versionGroupHome.instance, 'update')}")
public void removeVersion(HProjectIteration version) {
getInstance().getProjectIterations().remove(version);
super.update();
addMessage(
StatusMessage.Severity.INFO,
zanataMessages.getMessage("jsf.VersionRemoveFromGroup",
version.getSlug()));
}
@Restrict("#{s:hasPermission(versionGroupHome.instance, 'update')}")
public void removeMaintainer(HPerson maintainer) {
getInstance().getMaintainers().remove(maintainer);
super.update();
addMessage(StatusMessage.Severity.INFO, zanataMessages.getMessage(
"jsf.MaintainerRemoveFromGroup", maintainer.getName()));
}
public List<HLocale> getInstanceActiveLocales() {
List<HLocale> activeLocales =
Lists.newArrayList(getInstance().getActiveLocales());
Collections.sort(activeLocales, new Comparator<HLocale>() {
@Override
public int compare(HLocale hLocale, HLocale hLocale2) {
return hLocale.retrieveDisplayName().compareTo(
hLocale2.retrieveDisplayName());
}
});
return activeLocales;
}
private List<SelectItem> getAvailableStatus() {
if (statusList == null) {
statusList =
ImmutableList.copyOf(Iterables.filter(
super.getStatusList(), new Predicate<SelectItem>() {
@Override
public boolean apply(SelectItem input) {
return !input.getValue().equals(
EntityStatus.READONLY);
}
}));
}
return statusList;
}
@Override
protected HIterationGroup loadInstance() {
Session session = (Session) getEntityManager().getDelegate();
return (HIterationGroup) session.byNaturalId(HIterationGroup.class)
.using("slug", getSlug()).load();
}
public List<HProjectIteration> getInstanceProjectIterations() {
return new ArrayList<HProjectIteration>(getInstance()
.getProjectIterations());
}
public List<HPerson> getInstanceMaintainers() {
return new ArrayList<HPerson>(getInstance().getMaintainers());
}
@Override
public NaturalIdentifier getNaturalId() {
return Restrictions.naturalId().set("slug", slug);
}
@Override
public boolean isIdDefined() {
return slug != null;
}
@Override
public Object getId() {
return slug;
}
public void validateSuppliedId() {
getInstance(); // this will raise an EntityNotFound exception
// when id is invalid and conversation will not
// start
}
public boolean validateLanguageName(String componentId) {
// Check that locale Id is syntactically valid
LocaleId localeId;
if (StringUtils.isEmpty(newLanguage)) {
return false;
}
try {
localeId = new LocaleId(newLanguage);
} catch (IllegalArgumentException iaex) {
FacesMessages.instance().addToControl(
componentId,
zanataMessages
.getMessage("jsf.language.validation.Invalid"));
return false;
}
// check if locale enabled in server
if (!localeServiceImpl.localeSupported(localeId)) {
FacesMessages.instance().addToControl(
componentId,
zanataMessages
.getMessage("jsf.language.validation.NotSupport"),
localeId);
return false;
}
// check for already added language
for (HLocale locale : getInstanceActiveLocales()) {
if (locale.getLocaleId().equals(localeId)) {
FacesMessages
.instance()
.addToControl(
componentId,
zanataMessages
.getMessage("jsf.LanguageAlreadyInGroup"),
localeId);
return false;
}
}
return true;
}
public boolean validateVersion(String componentId) {
newVersion = newVersion.trim();
if (StringUtils.isEmpty(newVersion)
|| !StringUtils.contains(newVersion, " ")) {
return false;
}
String[] slugs = newVersion.split(" ");
HProjectIteration version =
projectIterationDAO.getBySlug(slugs[0], slugs[1]);
// check if version exists
if (version == null) {
FacesMessages.instance().addToControl(componentId,
zanataMessages.getMessage("jsf.InvalidProjectVersion"));
return false;
}
// check if version is already in group
if (getInstanceProjectIterations().contains(version)) {
FacesMessages.instance().addToControl(
componentId,
zanataMessages.getMessage("jsf.VersionAlreadyInGroup",
version.getSlug()));
return false;
}
return true;
}
public boolean validateMaintainer(String componentId) {
if (StringUtils.isEmpty(newMaintainer)) {
return false;
}
// check if username exist
HPerson person = personDAO.findByUsername(newMaintainer);
if (person == null) {
FacesMessages.instance().addToControl(componentId,
zanataMessages.getMessage("jsf.InvalidUsername"));
return false;
}
// check if person is already a maintainer
if (getInstanceMaintainers().contains(person)) {
FacesMessages.instance().addToControl(
componentId,
zanataMessages.getMessage("jsf.UserIsAMaintainer",
newMaintainer));
return false;
}
return true;
}
public String cancel() {
return "cancel";
}
}