Search in sources :

Example 6 with BundleItem

use of org.onebusaway.transit_data_federation.model.bundle.BundleItem in project onebusaway-application-modules by camsys.

the class LocalBundleStoreImpl method getBundles.

@Override
public List<BundleItem> getBundles() {
    ArrayList<BundleItem> output = new ArrayList<BundleItem>();
    File bundleRoot = new File(_bundleRootPath);
    if (!bundleRoot.isDirectory()) {
        return output;
    }
    for (String filename : bundleRoot.list()) {
        File possibleBundle = new File(bundleRoot, filename);
        if (possibleBundle.isDirectory()) {
            File calendarServiceObjectFile = new File(possibleBundle, CALENDAR_DATA);
            File metadataFile = new File(possibleBundle, METADATA);
            if (!calendarServiceObjectFile.exists()) {
                _log.warn("Could not find " + CALENDAR_DATA + " in local bundle '" + filename + "'; skipping. Not a bundle?");
                continue;
            }
            if (!metadataFile.exists()) {
                _log.warn("Could not find " + METADATA + " in local bundle '" + filename + "'; skipping. Not a bundle?");
                continue;
            }
            try {
                BundleItem validLocalBundle = createBundleItem(calendarServiceObjectFile, metadataFile, filename);
                output.add(validLocalBundle);
            } catch (Exception e) {
                continue;
            }
        } else if (possibleBundle.isFile() && possibleBundle.getName().equalsIgnoreCase(CALENDAR_DATA)) {
            try {
                String parentFilename = bundleRoot.getName();
                BundleItem validLocalBundle = createBundleItem(possibleBundle, parentFilename);
                output.add(validLocalBundle);
                setLegacyBundle(true);
                break;
            } catch (Exception e) {
                continue;
            }
        }
    }
    return output;
}
Also used : BundleItem(org.onebusaway.transit_data_federation.model.bundle.BundleItem) ArrayList(java.util.ArrayList) File(java.io.File)

Example 7 with BundleItem

use of org.onebusaway.transit_data_federation.model.bundle.BundleItem in project onebusaway-application-modules by camsys.

the class LocalBundleStoreImpl method createBundleItem.

private BundleItem createBundleItem(File calendarServiceObjectFile, File metadataFile, String filename) throws Exception {
    // get data to fill in the BundleItem for this bundle.
    ServiceDate minServiceDate = null;
    ServiceDate maxServiceDate = null;
    try {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        BundleMetadata meta = null;
        meta = mapper.readValue(metadataFile, BundleMetadata.class);
        // Convert metadata String Dates to Date Objects
        Date fromDate = new Date(Long.parseLong(meta.getServiceDateFrom()));
        Date toDate = new Date(Long.parseLong(meta.getServiceDateTo()));
        // Convert Date Objects to ServiceDate Objects
        minServiceDate = new ServiceDate(fromDate);
        maxServiceDate = new ServiceDate(toDate);
    } catch (Exception e) {
        _log.error(e.getMessage());
        _log.error("Deserialization of metadata.json in local bundle " + filename + "; skipping.");
        throw new Exception(e);
    }
    _log.info("Found local bundle " + filename + " with service range " + minServiceDate + " => " + maxServiceDate);
    BundleItem bundleItem = new BundleItem();
    bundleItem.setId(filename);
    bundleItem.setName(filename);
    bundleItem.setServiceDateFrom(minServiceDate);
    bundleItem.setServiceDateTo(maxServiceDate);
    DateTime lastModified = new DateTime(calendarServiceObjectFile.lastModified());
    bundleItem.setCreated(lastModified);
    bundleItem.setUpdated(lastModified);
    return bundleItem;
}
Also used : ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) BundleItem(org.onebusaway.transit_data_federation.model.bundle.BundleItem) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) Date(java.util.Date) DateTime(org.joda.time.DateTime) BundleMetadata(org.onebusaway.transit_data.model.config.BundleMetadata)

Aggregations

BundleItem (org.onebusaway.transit_data_federation.model.bundle.BundleItem)7 ArrayList (java.util.ArrayList)4 File (java.io.File)2 IOException (java.io.IOException)2 ParseException (java.text.ParseException)2 Date (java.util.Date)2 BundleFileItem (org.onebusaway.transit_data_federation.model.bundle.BundleFileItem)2 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 URL (java.net.URL)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1 DateTime (org.joda.time.DateTime)1 ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)1 BundleMetadata (org.onebusaway.transit_data.model.config.BundleMetadata)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1