This repository has been archived by the owner on Apr 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 941
/
NFJSLoader.java
144 lines (130 loc) · 6.66 KB
/
NFJSLoader.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
/*
* Copyright 2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.springsource.greenhouse.events.load;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class NFJSLoader {
private static final Logger logger = LoggerFactory.getLogger(NFJSLoader.class);
private final RestTemplate restTemplate;
private final EventLoaderRepository loaderRepository;
private final Map<Long, Long> timeSlotIdMap;
private final Map<Long, Long> leaderIdMap;
private final Map<Long, Long> topicSlotMap;
@Inject
public NFJSLoader(EventLoaderRepository loaderRepository) {
this.loaderRepository = loaderRepository;
this.restTemplate = new RestTemplate();
this.timeSlotIdMap = new HashMap<Long, Long>();
this.leaderIdMap = new HashMap<Long, Long>();
this.topicSlotMap = new HashMap<Long, Long>();
}
public void loadEventData(int showId) {
@SuppressWarnings("unchecked")
Map<String, Object> eventMap = (Map<String, Object>) restTemplate.getForObject("https://springone2gx.com/m/data/show_short.json?showId={showId}", Map.class, showId);
long sourceId = (Integer) eventMap.get("id");
String name = (String) eventMap.get("name");
String firstDay = (String) eventMap.get("firstDay");
String lastDay = (String) eventMap.get("lastDay");
String abbreviation = (String) eventMap.get("abreviation"); // yes, that's correct
@SuppressWarnings("unchecked")
Map<String, Object> locationMap = (Map<String, Object>) eventMap.get("location");
String timeZone = (String) locationMap.get("timeZoneName");
int utcOffset = (Integer) locationMap.get("utcOffset");
String locationAddress = locationMap.get("address1") + " " + locationMap.get("address2") + " " + locationMap.get("city") + ", " + locationMap.get("stateCode") + " " + locationMap.get("zip");
long eventId = loaderRepository.loadEvent(new EventData(MEMBER_GROUP_ID, name, null, abbreviation, firstDay, lastDay, timeZone, PROVIDER_ID, sourceId),
new VenueData((String) locationMap.get("description"), (String) locationAddress, (Double) locationMap.get("latitude"), (Double) locationMap.get("longitude"), (String) locationMap.get("metroArea")));
loadTimeSlotData(showId, eventId, utcOffset);
loadLeaderData(showId);
loadEventSessionData(showId, eventId, abbreviation);
}
private void loadLeaderData(int showId) {
@SuppressWarnings("unchecked")
List<Map<String, Object>> leaderMapList = (List<Map<String, Object>>) restTemplate.getForObject("https://springone2gx.com/m/data/show_speakers.json?showId={showId}", List.class, showId);
for (Map<String, Object> leaderMap : leaderMapList) {
long sourceId = (Integer) leaderMap.get("id");
String name = (String) leaderMap.get("name");
String bio = (String) leaderMap.get("bio");
String personalUrl = (String) leaderMap.get("blogLink");
String twitterId = (String) leaderMap.get("twitterId");
LeaderData leaderData = new LeaderData(name, bio, personalUrl, twitterId, PROVIDER_ID, sourceId);
long leaderId = loaderRepository.loadLeader(leaderData);
leaderIdMap.put(sourceId, leaderId);
}
}
@SuppressWarnings("unchecked")
private void loadTimeSlotData(int showId, long eventId, int utcOffset) {
List<Map<String, Object>> dayMapList = (List<Map<String, Object>>) restTemplate.getForObject("https://springone2gx.com/m/data/show_schedule.json?showId={showId}", List.class, showId);
for (Map<String, Object> dayMap : dayMapList) {
List<Map<String, Object>> slotMapList = (List<Map<String, Object>>) dayMap.get("slots");
for (Map<String, Object> slotMap : slotMapList) {
long sourceId = (Integer) slotMap.get("id");
String label = (String) slotMap.get("label");
String startTime = (String) slotMap.get("startTime");
String endTime = (String) slotMap.get("endTime");
TimeSlotData timeSlotData = new TimeSlotData(eventId, label, startTime, endTime, PROVIDER_ID, sourceId);
long timeSlotId = loaderRepository.loadTimeSlot(timeSlotData);
timeSlotIdMap.put(sourceId, timeSlotId);
List<Map<String, Object>> presentations = (List<Map<String, Object>>) slotMap.get("presentations");
for (Map<String, Object> presentation : presentations) {
if (presentation.get("topicId") != null && presentation.get("slotId") != null) {
long topicId = (Integer) presentation.get("topicId");
long slotId = (Integer) presentation.get("slotId");
topicSlotMap.put(topicId, slotId);
}
}
}
}
}
@SuppressWarnings("unchecked")
private void loadEventSessionData(int showId, long event, String abbreviation) {
List<Map<String, Object>> topicMapList = (List<Map<String, Object>>) restTemplate.getForObject("https://springone2gx.com/m/data/show_topics.json?showId={showId}", List.class, showId);
for (Map<String, Object> topicMap : topicMapList) {
long sourceId = (Integer) topicMap.get("id");
String title = (String) topicMap.get("title");
String description = (String) topicMap.get("summary");
String hashtag = "#" + abbreviation + sourceId;
Long venue = null; // TODO: Figure out how to get this from event
Long sourceTimeslot = topicSlotMap.get(sourceId);
Long timeslot = timeSlotIdMap.get(sourceTimeslot);
List<Integer> speakerIds = (List<Integer>) topicMap.get("speakerIds");
List<Long> leaderIds = new ArrayList<Long>();
for (Integer speakerId : speakerIds) {
Long leaderId = leaderIdMap.get(speakerId.longValue());
if (leaderId != null) {
leaderIds.add(leaderId);
} else {
logger.warn("Unknown speaker ID " + speakerId + ". Not in show_speakers.json feed.");
}
}
EventSessionData eventSessionData = new EventSessionData(event, -1, title, description, hashtag, venue, timeslot, PROVIDER_ID, sourceId, leaderIds);
loaderRepository.loadEventSession(eventSessionData);
}
}
// expose getter so that we can use Spring Test MVC to mock NFJS server
public RestTemplate getRestTemplate() {
return restTemplate;
}
private static final String PROVIDER_ID = "NFJS";
private static final long MEMBER_GROUP_ID = 1;
}