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
/
VersionHome.java
563 lines (478 loc) · 19 KB
/
VersionHome.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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/*
*
* * Copyright 2014, 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 com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
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.core.Events;
import org.jboss.seam.faces.FacesMessages;
import org.zanata.common.EntityStatus;
import org.zanata.common.LocaleId;
import org.zanata.common.ProjectType;
import org.zanata.dao.ProjectDAO;
import org.zanata.dao.ProjectIterationDAO;
import org.zanata.i18n.Messages;
import org.zanata.model.HLocale;
import org.zanata.model.HProject;
import org.zanata.model.HProjectIteration;
import org.zanata.seam.scope.ConversationScopeMessages;
import org.zanata.service.LocaleService;
import org.zanata.service.SlugEntityService;
import org.zanata.service.ValidationService;
import org.zanata.ui.autocomplete.LocaleAutocomplete;
import org.zanata.util.ComparatorUtil;
import org.zanata.webtrans.shared.model.ValidationAction;
import org.zanata.webtrans.shared.model.ValidationId;
import org.zanata.webtrans.shared.validation.ValidationFactory;
import javax.faces.application.FacesMessage;
import javax.faces.event.ValueChangeEvent;
import javax.persistence.EntityNotFoundException;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@Name("versionHome")
@Slf4j
public class VersionHome extends SlugHome<HProjectIteration> {
private static final long serialVersionUID = 1L;
public static final String PROJECT_ITERATION_UPDATE =
"project.iteration.update";
@Getter
@Setter
private String slug;
@Getter
@Setter
private String projectSlug;
@In
private ProjectIterationDAO projectIterationDAO;
@In
private ConversationScopeMessages conversationScopeMessages;
@In
private LocaleService localeServiceImpl;
@In
private ValidationService validationServiceImpl;
@In
private SlugEntityService slugEntityServiceImpl;
@In(create = true)
private ProjectDAO projectDAO;
@In
private Messages msgs;
@In
private CopyVersionManager copyVersionManager;
private Map<ValidationId, ValidationAction> availableValidations = Maps
.newHashMap();
@Getter
@Setter
private boolean isNewInstance = false;
@Setter
@Getter
private String selectedProjectType;
@Getter
private VersionLocaleAutocomplete localeAutocomplete =
new VersionLocaleAutocomplete();
@Getter
@Setter
private boolean copyFromVersion = true;
@Getter
@Setter
private String copyFromVersionSlug;
private final Function<HProjectIteration, VersionItem> VERSION_ITEM_FN =
new Function<HProjectIteration, VersionItem>() {
@Override
public VersionItem apply(HProjectIteration input) {
boolean selected = StringUtils.isNotEmpty(
copyFromVersionSlug) && copyFromVersionSlug
.equals(input.getSlug()) ? true : false;
return new VersionItem(selected, input);
}
};
private void setDefaultCopyFromVersion() {
List<VersionItem> otherVersions = getOtherVersions();
if (!otherVersions.isEmpty()
&& StringUtils.isEmpty(copyFromVersionSlug)) {
this.copyFromVersionSlug =
otherVersions.get(0).getVersion().getSlug();
}
copyFromVersion = true;
}
public void init(boolean isNewInstance) {
this.isNewInstance = isNewInstance;
if (isNewInstance) {
ProjectType projectType = getProject().getDefaultProjectType();
if (projectType != null) {
selectedProjectType = projectType.name();
}
if(StringUtils.isEmpty(copyFromVersionSlug)) {
setDefaultCopyFromVersion();
}
} else {
copyFromVersion = false;
ProjectType versionProjectType = getInstance().getProjectType();
if (versionProjectType != null) {
selectedProjectType = versionProjectType.name();
}
copyFromVersionSlug = "";
}
}
public HProject getProject() {
return projectDAO.getBySlug(projectSlug);
}
public List<VersionItem> getOtherVersions() {
HProject project = getProject();
if (project != null) {
List<HProjectIteration> versionList =
projectIterationDAO.getByProjectSlug(projectSlug,
EntityStatus.ACTIVE, EntityStatus.READONLY);
Collections.sort(versionList,
ComparatorUtil.VERSION_CREATION_DATE_COMPARATOR);
List<VersionItem> versionItems =
Lists.transform(versionList, VERSION_ITEM_FN);
if (StringUtils.isEmpty(copyFromVersionSlug)
&& !versionItems.isEmpty()) {
versionItems.get(0).setSelected(true);
}
return versionItems;
}
return Collections.EMPTY_LIST;
}
@Getter
@Setter
@AllArgsConstructor
public class VersionItem implements Serializable {
private boolean selected;
private HProjectIteration version;
}
@Restrict("#{s:hasPermission(versionHome.instance, 'update')}")
public void updateCustomisedLocales(String key, boolean checked) {
getInstance().setOverrideLocales(!checked);
update();
}
@Override
protected HProjectIteration loadInstance() {
Session session = (Session) getEntityManager().getDelegate();
return (HProjectIteration) session.byNaturalId(HProjectIteration.class)
.using("slug", getSlug())
.using("project", projectDAO.getBySlug(projectSlug)).load();
}
@Restrict("#{s:hasPermission(versionHome.instance, 'update')}")
public void updateRequireTranslationReview(String key, boolean checked) {
getInstance().setRequireTranslationReview(checked);
update();
if (checked) {
conversationScopeMessages.setMessage(FacesMessage.SEVERITY_INFO,
msgs.get("jsf.iteration.requireReview.enabled"));
} else {
conversationScopeMessages
.setMessage(FacesMessage.SEVERITY_INFO,
msgs.get("jsf.iteration.requireReview.disabled"));
}
}
public List<ValidationAction> getValidationList() {
List<ValidationAction> sortedList =
Lists.newArrayList(getValidations().values());
Collections.sort(sortedList,
ValidationFactory.ValidationActionComparator);
return sortedList;
}
private Map<ValidationId, ValidationAction> getValidations() {
if (availableValidations.isEmpty()) {
Collection<ValidationAction> validationList =
validationServiceImpl.getValidationActions(projectSlug,
slug);
for (ValidationAction validationAction : validationList) {
availableValidations.put(validationAction.getId(),
validationAction);
}
}
return availableValidations;
}
public void validateSuppliedId() {
getInstance(); // this will raise an EntityNotFound exception
// when id is invalid and conversation will not
// start
}
public ProjectType getProjectType() {
if (getInstance().getProjectType() == null) {
getInstance().setProjectType(
getInstance().getProject().getDefaultProjectType());
}
return getInstance().getProjectType();
}
public void setProjectType(ProjectType projectType) {
getInstance().setProjectType(projectType);
}
public void validateProjectSlug() {
if (projectDAO.getBySlug(projectSlug) == null) {
throw new EntityNotFoundException("no entity with slug "
+ projectSlug);
}
}
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 Version ID has been used in this project");
return false;
}
return true;
}
public boolean isSlugAvailable(String slug) {
return slugEntityServiceImpl.isProjectIterationSlugAvailable(slug,
projectSlug);
}
public String createVersion() {
if (!validateSlug(getInstance().getSlug(), "slug"))
return "invalid-slug";
if (copyFromVersion) {
copyVersion();
return "copy-version";
} else {
return persist();
}
}
public void copyVersion() {
getInstance().setStatus(EntityStatus.READONLY);
// create basic version here
HProject project = getProject();
project.addIteration(getInstance());
super.persist();
copyVersionManager.startCopyVersion(projectSlug,
copyFromVersionSlug, getInstance().getSlug());
conversationScopeMessages
.setMessage(FacesMessage.SEVERITY_INFO, msgs.
format("jsf.copyVersion.started",
getInstance().getSlug(), copyFromVersionSlug));
}
@Override
public String persist() {
updateProjectType();
HProject project = getProject();
project.addIteration(getInstance());
List<HLocale> projectLocales =
localeServiceImpl
.getSupportedLanguageByProject(projectSlug);
getInstance().getCustomizedLocales().addAll(projectLocales);
getInstance().getCustomizedValidations().putAll(
project.getCustomizedValidations());
return super.persist();
}
@Override
public Object getId() {
return projectSlug + "/" + slug;
}
@Override
public NaturalIdentifier getNaturalId() {
return Restrictions.naturalId().set("slug", slug)
.set("project", projectDAO.getBySlug(projectSlug));
}
@Override
public boolean isIdDefined() {
return slug != null && projectSlug != null;
}
public List<HLocale> getInstanceActiveLocales() {
if (StringUtils.isNotEmpty(projectSlug) && StringUtils.isNotEmpty(slug)) {
List<HLocale> locales =
localeServiceImpl.getSupportedLanguageByProjectIteration(
projectSlug, slug);
Collections.sort(locales, ComparatorUtil.LOCALE_COMPARATOR);
return locales;
}
return localeServiceImpl.getSupportedAndEnabledLocales();
}
public boolean isValidationsSameAsProject() {
Collection<ValidationAction> versionValidations =
validationServiceImpl.getValidationActions(projectSlug,
slug);
Collection<ValidationAction> projectValidations =
validationServiceImpl.getValidationActions(projectSlug);
return versionValidations.equals(projectValidations);
}
public void copyValidationFromProject() {
getInstance().getCustomizedValidations().clear();
getInstance().getCustomizedValidations().putAll(
getInstance().getProject().getCustomizedValidations());
availableValidations.clear();
update();
conversationScopeMessages
.setMessage(
FacesMessage.SEVERITY_INFO,
msgs.get(
"jsf.iteration.CopyProjectValidations.message"));
}
@Override
@Restrict("#{s:hasPermission(versionHome.instance, 'update')}")
public String update() {
String state = super.update();
Events.instance().raiseEvent(PROJECT_ITERATION_UPDATE, getInstance());
return state;
}
/**
* This is for autocomplete components of which ConversationScopeMessages
* will be null
*
* @param conversationScopeMessages
* @return
*/
private String update(ConversationScopeMessages conversationScopeMessages) {
if (this.conversationScopeMessages == null) {
this.conversationScopeMessages = conversationScopeMessages;
}
return update();
}
@Restrict("#{s:hasPermission(versionHome.instance, 'update')}")
public void removeLanguage(LocaleId localeId) {
HLocale locale = localeServiceImpl.getByLocaleId(localeId);
getInstance().getCustomizedLocales().remove(locale);
update();
conversationScopeMessages.setMessage(FacesMessage.SEVERITY_INFO,
msgs.format("jsf.iteration.LanguageRemoved",
locale.retrieveDisplayName()));
}
@Restrict("#{s:hasPermission(versionHome.instance, 'update')}")
public void updateStatus(char initial) {
getInstance().setStatus(EntityStatus.valueOf(initial));
update();
conversationScopeMessages.setMessage(FacesMessage.SEVERITY_INFO,
msgs.format("jsf.iteration.status.updated",
EntityStatus.valueOf(initial)));
}
public void updateSelectedProjectType(ValueChangeEvent e) {
selectedProjectType = (String) e.getNewValue();
updateProjectType();
}
public void copyProjectTypeFromProject() {
getInstance().setProjectType(
getInstance().getProject().getDefaultProjectType());
update();
conversationScopeMessages.setMessage(FacesMessage.SEVERITY_INFO,
msgs.get("jsf.iteration.CopyProjectType.message"));
}
/**
* @return comma-separated list of accepted file extensions. May be an empty
* string
*/
public String getAcceptedSourceFileTypes() {
return Joiner
.on(", ")
.join(ProjectType.getSupportedSourceFileTypes(getProjectType()));
}
private void updateProjectType() {
if (!StringUtils.isEmpty(selectedProjectType)
&& !selectedProjectType.equals("null")) {
ProjectType projectType = ProjectType.valueOf(selectedProjectType);
getInstance().setProjectType(projectType);
} else {
getInstance().setProjectType(null);
}
}
public List<ValidationAction.State> getValidationStates() {
return Arrays.asList(ValidationAction.State.values());
}
@Restrict("#{s:hasPermission(versionHome.instance, 'update')}")
public void updateValidationOption(String name, String state) {
ValidationId validatationId = ValidationId.valueOf(name);
for (Map.Entry<ValidationId, ValidationAction> entry : getValidations()
.entrySet()) {
if (entry.getKey().name().equals(name)) {
getValidations().get(validatationId).setState(
ValidationAction.State.valueOf(state));
getInstance().getCustomizedValidations().put(
entry.getKey().name(),
entry.getValue().getState().name());
ensureMutualExclusivity(getValidations().get(validatationId));
break;
}
}
update();
conversationScopeMessages.setMessage(FacesMessage.SEVERITY_INFO,
msgs.format("jsf.validation.updated",
validatationId.getDisplayName(), state));
}
/**
* If this action is enabled(Warning or Error), then it's exclusive
* validation will be turn off
*
*/
private void ensureMutualExclusivity(
ValidationAction selectedValidationAction) {
if (selectedValidationAction.getState() != ValidationAction.State.Off) {
for (ValidationAction exclusiveValAction : selectedValidationAction
.getExclusiveValidations()) {
getInstance().getCustomizedValidations().put(
exclusiveValAction.getId().name(),
ValidationAction.State.Off.name());
getValidations().get(exclusiveValAction.getId()).setState(
ValidationAction.State.Off);
}
}
}
private class VersionLocaleAutocomplete extends LocaleAutocomplete {
@Override
protected Collection<HLocale> getLocales() {
if (StringUtils.isNotEmpty(projectSlug)
&& StringUtils.isNotEmpty(slug)) {
return localeServiceImpl
.getSupportedLanguageByProjectIteration(projectSlug,
slug);
}
return Collections.EMPTY_LIST;
}
/**
* Action when an item is selected
*/
@Override
public void onSelectItemAction() {
if (StringUtils.isEmpty(getSelectedItem())) {
return;
}
HLocale locale = localeServiceImpl.getByLocaleId(getSelectedItem());
getInstance().getCustomizedLocales().add(locale);
update(conversationScopeMessages);
reset();
conversationScopeMessages.setMessage(FacesMessage.SEVERITY_INFO,
msgs.format("jsf.iteration.LanguageAdded",
locale.retrieveDisplayName()));
}
}
public List<ProjectType> getProjectTypeList() {
List<ProjectType> projectTypes = Arrays.asList(ProjectType.values());
Collections.sort(projectTypes, ComparatorUtil.PROJECT_TYPE_COMPARATOR);
return projectTypes;
}
}