Skip to content

Commit 34b8a08

Browse files
initial commit
1 parent 4425515 commit 34b8a08

16 files changed

Lines changed: 982 additions & 0 deletions

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,10 @@
1010

1111
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
1212
hs_err_pid*
13+
/bin/
14+
/target/
15+
16+
.classpath
17+
.project
18+
.settings/
19+

pom.xml

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>apereo</groupId>
7+
<artifactId>data-loader-library</artifactId>
8+
<version>1.0.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>apereo-data-loader-library</name>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
16+
<java.version>1.8</java.version>
17+
<jackson.version>2.8.3</jackson.version>
18+
<hibernate-validator.version>5.2.4.Final</hibernate-validator.version>
19+
<commons-lang3.version>3.3.2</commons-lang3.version>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>com.fasterxml.jackson.core</groupId>
25+
<artifactId>jackson-databind</artifactId>
26+
<version>${jackson.version}</version>
27+
<scope>compile</scope>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.hibernate</groupId>
31+
<artifactId>hibernate-validator</artifactId>
32+
<version>${hibernate-validator.version}</version>
33+
<scope>compile</scope>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.apache.commons</groupId>
37+
<artifactId>commons-lang3</artifactId>
38+
<version>${commons-lang3.version}</version>
39+
<scope>compile</scope>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.springframework</groupId>
43+
<artifactId>spring-web</artifactId>
44+
<version>4.3.3.RELEASE</version>
45+
</dependency>
46+
<dependency>
47+
<groupId>apereo</groupId>
48+
<artifactId>lai-event</artifactId>
49+
<version>1.0-SNAPSHOT</version>
50+
</dependency>
51+
<dependency>
52+
<groupId>apereo</groupId>
53+
<artifactId>lai-model</artifactId>
54+
<version>1.0.2</version>
55+
</dependency>
56+
<dependency>
57+
<groupId>com.google.code.gson</groupId>
58+
<artifactId>gson</artifactId>
59+
<version>2.7</version>
60+
</dependency>
61+
</dependencies>
62+
63+
<repositories>
64+
<repository>
65+
<id>io.spring.repo.maven.release</id>
66+
<url>http://repo.spring.io/release/</url>
67+
<snapshots>
68+
<enabled>false</enabled>
69+
</snapshots>
70+
</repository>
71+
<repository>
72+
<id>git-lai</id>
73+
<name>LAI Git based repo</name>
74+
<url>https://raw.github.com/Apereo-Learning-Analytics-Initiative/maven-repo/master/</url>
75+
<snapshots>
76+
<checksumPolicy>ignore</checksumPolicy>
77+
</snapshots>
78+
</repository>
79+
</repositories>
80+
81+
<build>
82+
<plugins>
83+
<plugin>
84+
<groupId>org.apache.maven.plugins</groupId>
85+
<artifactId>maven-compiler-plugin</artifactId>
86+
<version>3.3</version>
87+
<configuration>
88+
<source>1.8</source>
89+
<target>1.8</target>
90+
</configuration>
91+
</plugin>
92+
</plugins>
93+
</build>
94+
95+
</project>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package unicon.matthews.dataloader;
2+
3+
public interface DataLoader {
4+
void run();
5+
}
Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
/**
2+
*
3+
*/
4+
package unicon.matthews.dataloader;
5+
6+
import java.time.LocalDateTime;
7+
import java.util.ArrayList;
8+
import java.util.Collection;
9+
import java.util.Collections;
10+
import java.util.Map;
11+
12+
import org.apache.commons.lang3.StringUtils;
13+
import org.springframework.http.HttpEntity;
14+
import org.springframework.http.HttpHeaders;
15+
import org.springframework.http.HttpMethod;
16+
import org.springframework.http.HttpStatus;
17+
import org.springframework.http.MediaType;
18+
import org.springframework.http.ResponseEntity;
19+
import org.springframework.web.client.RestClientException;
20+
import org.springframework.web.client.RestTemplate;
21+
22+
import unicon.matthews.caliper.Envelope;
23+
import unicon.matthews.caliper.Event;
24+
import unicon.matthews.entity.ClassMapping;
25+
import unicon.matthews.entity.DataSync;
26+
import unicon.matthews.entity.UserMapping;
27+
import unicon.matthews.oneroster.Enrollment;
28+
import unicon.matthews.oneroster.LineItem;
29+
import unicon.matthews.oneroster.User;
30+
31+
import com.google.gson.JsonObject;
32+
import com.google.gson.JsonPrimitive;
33+
34+
/**
35+
* @author ggilbert
36+
*
37+
*/
38+
public class MatthewsClient {
39+
40+
private RestTemplate restTemplate;
41+
private HttpHeaders httpHeaders;
42+
43+
private String key;
44+
private String secret;
45+
private String baseUrl;
46+
47+
private static final String LOGIN_URL = "/api/auth/login";
48+
49+
private MatthewsClient() {}
50+
51+
public static class Builder {
52+
MatthewsClient _matthewsClient = new MatthewsClient();
53+
54+
public Builder withRestTemplate(RestTemplate restTemplate) {
55+
_matthewsClient.restTemplate = restTemplate;
56+
return this;
57+
}
58+
59+
public Builder withHttpHeaders(HttpHeaders httpHeaders) {
60+
_matthewsClient.httpHeaders = httpHeaders;
61+
return this;
62+
}
63+
64+
public Builder withBaseUrl(String baseUrl) {
65+
if (StringUtils.endsWith(baseUrl, "/")) {
66+
baseUrl = StringUtils.substringBeforeLast(baseUrl, "/");
67+
}
68+
69+
_matthewsClient.baseUrl = baseUrl;
70+
71+
return this;
72+
}
73+
74+
public Builder withKey(String key) {
75+
_matthewsClient.key = key;
76+
return this;
77+
}
78+
79+
public Builder withSecret(String secret) {
80+
_matthewsClient.secret = secret;
81+
return this;
82+
}
83+
84+
public MatthewsClient build() {
85+
if (StringUtils.isBlank(_matthewsClient.key) ||
86+
StringUtils.isBlank(_matthewsClient.secret) ||
87+
StringUtils.isBlank(_matthewsClient.baseUrl) ||
88+
_matthewsClient.restTemplate == null ||
89+
_matthewsClient.httpHeaders == null) {
90+
throw new IllegalStateException();
91+
}
92+
93+
_matthewsClient.httpHeaders.setContentType(MediaType.APPLICATION_JSON);
94+
_matthewsClient.httpHeaders.add("X-Requested-With", "XMLHttpRequest");
95+
96+
_matthewsClient.httpHeaders.add("Authorization", "Bearer "+_matthewsClient.getToken());
97+
98+
return _matthewsClient;
99+
}
100+
}
101+
102+
@SuppressWarnings("rawtypes")
103+
public String getToken() {
104+
105+
JsonObject request = new JsonObject();
106+
request.add("username", new JsonPrimitive(this.key));
107+
request.add("password", new JsonPrimitive(this.secret));
108+
109+
HttpEntity<String> entity = new HttpEntity<String>(request.toString(), httpHeaders);
110+
111+
ResponseEntity<Map> loginResponse
112+
= restTemplate
113+
.exchange(this.baseUrl + LOGIN_URL,
114+
HttpMethod.POST,
115+
entity,
116+
Map.class);
117+
118+
return (String)loginResponse.getBody().get("token");
119+
}
120+
121+
public void postEnrollment(Enrollment enrollment) {
122+
HttpEntity<Enrollment> he = new HttpEntity<Enrollment>(enrollment, this.httpHeaders);
123+
124+
String path = "/api/classes/{classSourcedId}/enrollments";
125+
String url = this.baseUrl + StringUtils.replace(path, "{classSourcedId}", enrollment.getKlass().getSourcedId());
126+
127+
restTemplate
128+
.exchange(url, HttpMethod.POST, he, JsonObject.class);
129+
}
130+
131+
public void postUser(User user) {
132+
HttpEntity<User> he = new HttpEntity<User>(user, this.httpHeaders);
133+
134+
String path = "/api/users";
135+
String url = this.baseUrl + path;
136+
137+
restTemplate
138+
.exchange(url, HttpMethod.POST, he, JsonObject.class);
139+
}
140+
141+
public void postUserMapping(UserMapping userMapping) {
142+
HttpEntity<UserMapping> he = new HttpEntity<UserMapping>(userMapping, this.httpHeaders);
143+
144+
String path = "/api/users/mapping";
145+
String url = this.baseUrl + path;
146+
147+
restTemplate
148+
.exchange(url, HttpMethod.POST, he, JsonObject.class);
149+
150+
}
151+
152+
public void postLineItem(LineItem lineItem) {
153+
HttpEntity<LineItem> he = new HttpEntity<LineItem>(lineItem, this.httpHeaders);
154+
155+
String path = "/api/classes/{classSourcedId}/lineitems";
156+
String url = this.baseUrl + StringUtils.replace(path, "{classSourcedId}", lineItem.getKlass().getSourcedId());
157+
158+
restTemplate
159+
.exchange(url, HttpMethod.POST, he, JsonObject.class);
160+
161+
}
162+
163+
public void postClass(unicon.matthews.oneroster.Class klass) {
164+
HttpEntity<unicon.matthews.oneroster.Class> he = new HttpEntity<unicon.matthews.oneroster.Class>(klass, this.httpHeaders);
165+
166+
String path = "/api/classes";
167+
String url = this.baseUrl + path;
168+
169+
restTemplate
170+
.exchange(url, HttpMethod.POST, he, JsonObject.class);
171+
}
172+
173+
public void postClassMapping(ClassMapping classMapping) {
174+
HttpEntity<ClassMapping> he = new HttpEntity<ClassMapping>(classMapping, this.httpHeaders);
175+
176+
String path = "/api/classes/mapping";
177+
String url = this.baseUrl + path;
178+
179+
restTemplate
180+
.exchange(url, HttpMethod.POST, he, JsonObject.class);
181+
182+
}
183+
184+
public void postEvent(Event event, String sensorName) {
185+
Envelope envelope
186+
= new Envelope.Builder()
187+
.withData(Collections.singletonList(event))
188+
.withSendTime(LocalDateTime.now())
189+
.withSensor(sensorName)
190+
.build();
191+
192+
HttpEntity<Envelope> h = new HttpEntity<Envelope>(envelope, this.httpHeaders);
193+
194+
String path = "/api/caliper";
195+
String url = this.baseUrl + path;
196+
197+
restTemplate
198+
.exchange(url, HttpMethod.POST, h, String.class);
199+
}
200+
201+
public void postEvents(Collection<Event> events, String sensorName) {
202+
Envelope envelope
203+
= new Envelope.Builder()
204+
.withData(new ArrayList<>(events))
205+
.withSendTime(LocalDateTime.now())
206+
.withSensor(sensorName)
207+
.build();
208+
209+
HttpEntity<Envelope> h = new HttpEntity<Envelope>(envelope, this.httpHeaders);
210+
211+
String path = "/api/caliper";
212+
String url = this.baseUrl + path;
213+
214+
restTemplate
215+
.exchange(url, HttpMethod.POST, h, String.class);
216+
}
217+
218+
public void postDataSync(DataSync dataSync) {
219+
HttpEntity<DataSync> he = new HttpEntity<DataSync>(dataSync, this.httpHeaders);
220+
221+
String path = "/api/sync";
222+
String url = this.baseUrl + path;
223+
224+
restTemplate
225+
.exchange(url, HttpMethod.POST, he, JsonObject.class);
226+
227+
}
228+
229+
public DataSync getLatestDataSyncForType(unicon.matthews.entity.DataSync.DataSyncType dataSyncType) {
230+
DataSync dataSync = null;
231+
232+
String path = "/api/sync/".concat(dataSyncType.toString()).concat("/latest");
233+
String url = this.baseUrl + path;
234+
235+
ResponseEntity<DataSync> response = null;
236+
try {
237+
response = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(this.httpHeaders), DataSync.class);
238+
}
239+
catch (RestClientException e) {
240+
// TODO Auto-generated catch block
241+
e.printStackTrace();
242+
}
243+
244+
if (response != null && response.getStatusCode() == HttpStatus.OK) {
245+
dataSync = response.getBody();
246+
}
247+
248+
return dataSync;
249+
}
250+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package unicon.matthews.dataloader.converter;
2+
3+
import unicon.matthews.caliper.Event;
4+
5+
/**
6+
* General interface for type converters. The Type parameters &lt;S, T &gt; will allow the converters to be type safe
7+
* and allow them to be grouped by type for distinct use in the conversion service, further filtered by the supports
8+
* method.
9+
*/
10+
public interface Converter<S, T> {
11+
12+
boolean supports(S source);
13+
14+
T convert(S source, SupportingEntities supportingEntities);
15+
16+
}

0 commit comments

Comments
 (0)