use of org.onebusaway.transit_data_federation.model.bundle.BundleItem in project onebusaway-application-modules by camsys.
the class BundleManagementServiceImpl method refreshApplicableBundles.
/**
* This method calculates which of the bundles available to us are valid for
* today, and updates the internal list appropriately. It does not switch any
* bundles.
*/
public synchronized void refreshApplicableBundles() {
_applicableBundles.clear();
for (BundleItem bundle : _allBundles) {
if (bundle.isApplicableToDate(getServiceDate())) {
_log.info("Bundle " + bundle.getName() + "(" + bundle.getId() + ")" + " is active for today; adding to list of active bundles.");
_applicableBundles.put(bundle.getId(), bundle);
}
}
}
use of org.onebusaway.transit_data_federation.model.bundle.BundleItem in project onebusaway-application-modules by camsys.
the class BundleManagementServiceImpl method reevaluateBundleAssignment.
/**
* Recalculate which of the bundles that are available and active for today we
* should be using. Switch to that bundle if not already active.
*/
public void reevaluateBundleAssignment() throws Exception {
if (_applicableBundles.size() == 0) {
_log.error("No valid and active bundles found!");
return;
}
// sort bundles by preference
ArrayList<BundleItem> bestBundleCandidates = new ArrayList<BundleItem>(_applicableBundles.values());
Collections.sort(bestBundleCandidates);
BundleItem bestBundle = bestBundleCandidates.get(bestBundleCandidates.size() - 1);
_log.info("Best bundle is " + bestBundle.getName() + " (" + bestBundle.getId() + ")");
changeBundle(bestBundle.getId(), bestBundle.getName());
}
use of org.onebusaway.transit_data_federation.model.bundle.BundleItem in project onebusaway-application-modules by camsys.
the class HttpBundleStoreImpl method getBundles.
@Override
public List<BundleItem> getBundles() throws Exception {
List<BundleItem> output = new ArrayList<BundleItem>();
List<BundleItem> bundlesFromHttp = getBundleListFromHttp();
for (BundleItem bundle : bundlesFromHttp) {
boolean bundleIsValid = true;
// ensure bundle path exists locally
File bundleRoot = new File(_bundleRootPath, bundle.getName());
if (!bundleRoot.exists()) {
if (!bundleRoot.mkdirs()) {
throw new Exception("Creation of bundle root for " + bundle.getName() + " at " + bundleRoot + " failed.");
}
}
for (BundleFileItem file : bundle.getFiles()) {
File fileInBundlePath = new File(bundleRoot, file.getFilename());
// see if file already exists locally and matches its MD5
if (fileInBundlePath.exists()) {
if (!file.verifyMd5(fileInBundlePath)) {
_log.warn("File " + fileInBundlePath + " is corrupted; removing.");
if (!fileInBundlePath.delete()) {
_log.error("Could not remove corrupted file " + fileInBundlePath);
bundleIsValid = false;
break;
}
}
}
// if the file is not there, or was removed above, try to download it again from the TDM.
if (!fileInBundlePath.exists()) {
int tries = _fileDownloadRetries;
while (tries > 0) {
URL fileDownloadUrl = _apiLibrary.buildUrl("bundle", "deploy", bundle.getName(), "file", file.getFilename(), "get");
try {
downloadUrlToLocalPath(fileDownloadUrl, fileInBundlePath, file.getMd5());
} catch (Exception e) {
tries--;
if (tries == 0) {
bundleIsValid = false;
}
_log.warn("Download of " + fileDownloadUrl + " failed (" + e.getMessage() + ");" + " retrying (retries left=" + tries + ")");
continue;
}
// file was downloaded successfully--break out of retry loop
break;
}
}
}
if (bundleIsValid) {
_log.info("Bundle " + bundle.getName() + " files pass checksums; added to list of local bundles.");
output.add(bundle);
} else {
_log.warn("Bundle " + bundle.getName() + " files do NOT pass checksums; skipped.");
}
}
return output;
}
use of org.onebusaway.transit_data_federation.model.bundle.BundleItem in project onebusaway-application-modules by camsys.
the class HttpBundleStoreImpl method getBundleListFromHttp.
private List<BundleItem> getBundleListFromHttp() {
ArrayList<BundleItem> output = new ArrayList<BundleItem>();
_log.info("Getting current bundle list from Server...");
List<JsonObject> bundles = null;
try {
bundles = _apiLibrary.getItemsForRequest("bundle", "list");
} catch (Exception e) {
_log.info("Error executing apiLibrary.getItemsForRequest", e);
}
if (bundles != null) {
for (JsonObject itemToAdd : bundles) {
try {
BundleItem item = new BundleItem();
if (itemToAdd.get("id") == null)
item.setId(getJsonObjAsString(itemToAdd, "name"));
else
item.setId(getJsonObjAsString(itemToAdd, "id"));
item.setName(getJsonObjAsString(itemToAdd, "name"));
item.setServiceDateFrom(ServiceDate.parseString(getJsonObjAsString(itemToAdd, "service-date-from").replace("-", "")));
item.setServiceDateTo(ServiceDate.parseString(getJsonObjAsString(itemToAdd, "service-date-to").replace("-", "")));
if (itemToAdd.get("created") != null)
item.setCreated(_updatedDateFormatter.parseDateTime(getJsonObjAsString(itemToAdd, "created")));
if (itemToAdd.get("updated") != null)
item.setUpdated(_updatedDateFormatter.parseDateTime(getJsonObjAsString(itemToAdd, "updated")));
ArrayList<BundleFileItem> files = new ArrayList<BundleFileItem>();
JsonElement filesElement = itemToAdd.get("files");
if (filesElement != null && filesElement.isJsonArray()) {
for (JsonElement _subitemToAdd : filesElement.getAsJsonArray()) {
if (_subitemToAdd.isJsonObject()) {
JsonObject subitemToAdd = _subitemToAdd.getAsJsonObject();
BundleFileItem fileItemToAdd = new BundleFileItem();
fileItemToAdd.setFilename(getJsonObjAsString(subitemToAdd, "filename"));
fileItemToAdd.setMd5(getJsonObjAsString(subitemToAdd, "md5"));
files.add(fileItemToAdd);
} else {
_log.warn("Unable to retreive file name/md5 as Json Object");
}
}
item.setFiles(files);
output.add(item);
} else {
_log.warn("Unable to get list of files for Bundle " + item.getName());
}
} catch (NullPointerException npe) {
_log.warn("Error retrieving bundle information");
continue;
} catch (ParseException e) {
_log.warn("Error parsing dates for Bundle Item");
continue;
}
}
_log.info("Found " + output.size() + " bundle(s) available from the Server.");
}
return output;
}
use of org.onebusaway.transit_data_federation.model.bundle.BundleItem in project onebusaway-application-modules by camsys.
the class BundleManagementController method change.
@RequestMapping("/bundles!change.do")
public ModelAndView change(@RequestParam String bundleId, @RequestParam(required = false) String time, @RequestParam(required = false) boolean automaticallySetDate) throws Exception {
if (time != null && !StringUtils.isEmpty(time)) {
_bundleManager.setTime(DateLibrary.getIso8601StringAsTime(time));
} else {
_bundleManager.setTime(new Date());
}
// change succeed
if (automaticallySetDate == true) {
List<BundleItem> bundles = _bundleManager.getAllKnownBundles();
for (BundleItem bundle : bundles) {
if (bundle.getId().equals(bundleId)) {
Date targetDate = bundle.getServiceDateFrom().getAsDate();
_bundleManager.setTime(targetDate);
break;
}
}
}
_bundleManager.changeBundle(bundleId);
return new ModelAndView("redirect:/bundles.do");
}
Aggregations