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
/
SampleDataResourceImpl.java
155 lines (139 loc) · 5.03 KB
/
SampleDataResourceImpl.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
package org.zanata.rest;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.persistence.EntityManager;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import lombok.extern.slf4j.Slf4j;
import javax.inject.Inject;
import javax.inject.Named;
import org.jboss.seam.annotations.Transactional;
import org.jboss.seam.security.RunAsOperation;
import org.zanata.common.LocaleId;
import org.zanata.model.HLocale;
import org.zanata.model.HPerson;
import org.zanata.util.SampleProjectProfile;
import com.google.common.base.Function;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.Uninterruptibles;
/**
* @author Patrick Huang <a
* href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
@Path("/test/data/sample")
@Named("sampleProjectResourceImpl")
@Slf4j
public class SampleDataResourceImpl implements SampleDataResource {
@Inject
private SampleProjectProfile sampleProjectProfile;
@Inject
private EntityManager entityManager;
@Override
public Response makeSampleLanguages() {
new RunAsOperation() {
@Override
public void execute() {
sampleProjectProfile.makeSampleLanguages();
}
}.addRole("admin").run();
return Response.ok().build();
}
@Override
@Transactional
public Response addLanguage(final String localeId) {
new RunAsOperation() {
@Override
public void execute() {
sampleProjectProfile.makeLanguage(true, new LocaleId(localeId));
}
}.addRole("admin").run();
return Response.ok().build();
}
@Override
public Response makeSampleUsers() {
new RunAsOperation() {
public void execute() {
sampleProjectProfile.makeSampleUsers();
}
}.addRole("admin").run();
return Response.ok().build();
}
@Override
@Transactional
public Response userJoinsLanguageTeams(
@PathParam("username") String username,
@QueryParam("locales") String localesCSV) {
List<String> localesIds = Lists.newArrayList(
Splitter.on(",").omitEmptyStrings().trimResults().split(
localesCSV));
List<LocaleId> locales =
Lists.transform(localesIds, new Function<String, LocaleId>() {
@Override
public LocaleId apply(String input) {
return new LocaleId(input);
}
});
final HPerson hPerson = entityManager
.createQuery("from HPerson p where p.account.username = :username",
HPerson.class).setParameter("username", username).getSingleResult();
final List<HLocale> hLocales = entityManager
.createQuery("from HLocale where localeId in (:locales)",
HLocale.class).setParameter("locales", locales).getResultList();
new RunAsOperation() {
public void execute() {
sampleProjectProfile.addUsersToLanguage(hPerson, hLocales);
}
}.addRole("admin").run();
return Response.ok().build();
}
@Override
public Response makeSampleProject() {
new RunAsOperation() {
public void execute() {
sampleProjectProfile.makeSampleProject();
}
}.addRole("admin").run();
return Response.ok().build();
}
@Override
public Response deleteExceptEssentialData() {
new RunAsOperation() {
public void execute() {
sampleProjectProfile.deleteExceptEssentialData();
}
}.addRole("admin").run();
return Response.ok().build();
}
@Override
public Response dummyService(long timeInMillis,
String qualifiedExceptionClass) throws Throwable {
if (timeInMillis > 0) {
log.info("I am going to take a nap for {} ms", timeInMillis);
Uninterruptibles.sleepUninterruptibly(timeInMillis,
TimeUnit.MILLISECONDS);
}
if (Strings.isNullOrEmpty(qualifiedExceptionClass)) {
return Response.ok().build();
}
try {
Class<?> exceptionClass = Class.forName(qualifiedExceptionClass);
boolean isThrowable =
Throwable.class.isAssignableFrom(exceptionClass);
if (!isThrowable) {
return Response
.status(Response.Status.BAD_REQUEST)
.entity(qualifiedExceptionClass + " is not a Throwable")
.build();
}
log.info("about to throw exception: {}", exceptionClass);
throw (Throwable) exceptionClass.newInstance();
} catch (Exception e) {
return Response.status(Response.Status.BAD_REQUEST)
.entity(e.getMessage()).build();
}
}
}