}
public void updateConnectorData(UpdateInfo updateInfo) throws Exception {
<<<<<<< HEAD
}
@Override
public int importFile(final ApiKey apiKey, final File f) throws Exception {
ZipFile zipFile = new ZipFile(f);
final Enumeration extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
final ZipEntry zipEntry = entries.nextElement();
if (zipEntry.isDirectory()) continue;
if (zipEntry.getName().endsWith("LocationHistory.json"))
return parseLocations(apiKey, zipFile.getInputStream(zipEntry));
}
throw new RuntimeException("Couldn't find LocationHistory.json in the uploaded zip file");
}
private int parseLocations(final ApiKey apiKey, final InputStream inputStream) throws IOException {
JsonFactory jfactory = new JsonFactory();
JsonParser jParser = jfactory.createJsonParser(inputStream);
JsonToken token;
List locations = new ArrayList();
getToLocationData(jParser);
int nLocations = 0;
while ((token = jParser.nextToken())!=null) {
if (token==JsonToken.START_OBJECT) {
jParser.nextToken();
parseLocation(apiKey, jParser, locations);
if (locations.size()==1000) {
nLocations += 1000;
if (apiKey!=null) apiDataService.addGuestLocations(apiKey.getGuestId(), locations);
locations.clear();
}
}
}
nLocations += locations.size();
if (apiKey!=null) apiDataService.addGuestLocations(apiKey.getGuestId(), locations);
return nLocations;
}
private void getToLocationData(final JsonParser jParser) throws IOException {
// get to the first object
while (jParser.nextToken()!=JsonToken.START_OBJECT);
String currentName;
while ((currentName=jParser.getCurrentName())==null)
jParser.nextToken();
// if it's one of the location fields, we're good
if (Arrays.asList("timestampMs", "accuracy", "latitudeE7", "longitudeE7").contains(currentName))
return;
// else skip everything until we reach the "locations" array
while (!jParser.getCurrentName().equals("locations"))
jParser.nextToken();
// this recursion should only happen once
getToLocationData(jParser);
}
void parseLocation(final ApiKey apiKey, final JsonParser jParser, final List locations) throws IOException {
LocationFacet locationFacet = new LocationFacet();
if (apiKey!=null) {
locationFacet.apiKeyId = apiKey.getId();
locationFacet.guestId = apiKey.getGuestId();
}
locationFacet.timeUpdated = System.currentTimeMillis();
locationFacet.source = LocationFacet.Source.GOOGLE_LATITUDE;
do {
String fieldName = jParser.getCurrentName();
jParser.nextToken();
if (fieldName.equals("timestampMs")) {
long ts = Long.valueOf(jParser.getText());
locationFacet.timestampMs = ts;
locationFacet.start = ts;
locationFacet.end = ts;
locationFacet.api = 2;
} else if (fieldName.equals("accuracy")) {
int accuracy = jParser.getIntValue();
locationFacet.accuracy = accuracy;
} else if (fieldName.equals("latitudeE7")) {
int lat = jParser.getIntValue();
locationFacet.latitude = lat/1E7f;
} else if (fieldName.equals("longitudeE7")) {
int lon = jParser.getIntValue();
locationFacet.longitude = lon/1E7f;
}
} while (jParser.nextToken() != JsonToken.END_OBJECT);
locations.add(locationFacet);
}
=======
//final LocationFacet newest = jpaDaoService.findOne("location.newest", LocationFacet.class, updateInfo.getGuestId(), updateInfo.apiKey.getId());
//ApiUpdate lastSuccessfulUpdate = connectorUpdateService
// .getLastSuccessfulUpdate(updateInfo.apiKey);
//loadHistory(updateInfo, newest!=null?newest.start:lastSuccessfulUpdate.ts,
// System.currentTimeMillis());
}
private void loadHistory(UpdateInfo updateInfo, long from, long to)
throws Exception {
//String accessToken = oAuth2Helper.getAccessToken(updateInfo.apiKey);
//HttpTransport transport = this.getTransport(updateInfo.apiKey);
//String key = env.get("google_latitudeApiKey");
//List locationList = executeList(updateInfo, transport,
// key, 1000, from, to, accessToken);
//if (locationList != null && locationList.size() > 0) {
//List storedLocations = new ArrayList();
//for (LocationFacet locationResource : locationList) {
// if (locationResource.timestampMs==0)
// continue;
// locationResource.guestId = updateInfo.getGuestId();
// locationResource.apiKeyId = updateInfo.apiKey.getId();
// locationResource.start = locationResource.timestampMs;
// locationResource.end = locationResource.timestampMs;
// locationResource.source = LocationFacet.Source.GOOGLE_LATITUDE;
// locationResource.apiKeyId = updateInfo.apiKey.getId();
// locationResource.api= updateInfo.apiKey.getConnector().value();
//
// storedLocations.add(locationResource);
//}
// apiDataService.addGuestLocations(updateInfo.getGuestId(), storedLocations);
//Collections.sort(storedLocations);
//LocationFacet oldest = storedLocations.get(0);
// // Check if there is potentially a second or more of data left to get. If so,
// // recurse with a new to time of a second before the oldest location we currently have.
// // Otherwise, end now
// if(oldest.timestampMs-1000 >= from) {
// loadHistory(updateInfo, from, oldest.timestampMs-1000);
// }
//}
}
private List executeList(UpdateInfo updateInfo,
HttpTransport transport, String key, int maxResults, long minTime,
long maxTime, String accessToken) throws Exception {
//long then = System.currentTimeMillis();
//String requestUrl = "request url not set yet";
//try {
// transport.addParser(new JsonCParser());
// HttpRequest request = transport.buildGetRequest();
// LatitudeUrl latitudeUrl = LatitudeUrl.forLocation();
// latitudeUrl.maxResults = String.valueOf(maxResults);
// latitudeUrl.granularity = "best";
// latitudeUrl.minTime = String.valueOf(minTime);
// latitudeUrl.maxTime = String.valueOf(maxTime);
// latitudeUrl.put("location", "all");
// latitudeUrl.put("key", key);
// latitudeUrl.put("access_token", accessToken);
// request.url = latitudeUrl;
// requestUrl = latitudeUrl.build();
// HttpResponse response = request.execute();
// List result = response.parseAs(LocationList.class).items;
// countSuccessfulApiCall(updateInfo.apiKey,
// updateInfo.objectTypes, then, requestUrl);
// return result;
//} catch (Exception e) {
// countFailedApiCall(updateInfo.apiKey,
// updateInfo.objectTypes, then, requestUrl, Utils.stackTrace(e));
// throw e;
//}
return null;
}
>>>>>>> 9e09b3e5648365cade6196c7c1872857a4c25454
} |