use of org.onebusaway.admin.bundle.model.Bundle in project onebusaway-application-modules by camsys.
the class DirectoryBundleSource method loadBundleDirectory.
/**
* Load the directory at masterBundleDirectory/dirName, check if it is a
* bundle directory, and if so, fill the bundle object and return a map item
* relating the bundle name to the bundle.
*
* @param dirName The name of the bundle directory, which corresponds to the
* id of the bundle.
* @return A Map<String, Bundle> mapping the bundle id to its Bundle object,
* or null if dirName does not contain a legit bundle.
* @throws IOException
*/
private Bundle loadBundleDirectory(String dirName) throws IOException {
Bundle resultBundle = null;
File bundleFile = new File(getMasterBundleDirectory(), dirName);
if (bundleFile.isDirectory()) {
// List the contents of the directory.
String[] dirList = bundleFile.list();
// named 'data'
if (arrayContainsItem(dirList, BUNDLE_METADATA_FILENAME) && arrayContainsItem(dirList, AbstractBundleSource.BUNDLE_DATA_DIRNAME)) {
File bundleMetadataFile = new File(bundleFile, BUNDLE_METADATA_FILENAME);
File dataDir = new File(bundleFile, AbstractBundleSource.BUNDLE_DATA_DIRNAME);
if (bundleMetadataFile.isFile() && dataDir.isDirectory()) {
Bundle bundle;
try {
bundle = loadBundleMetadata(bundleMetadataFile);
} catch (FileNotFoundException e) {
throw new IOException("Could not load Bundle metadata", e);
}
if (bundle != null) {
if (dirName.equals(bundle.getName())) {
resultBundle = bundle;
} else {
_log.info("Invalid individual bundle directory " + dirName + ": Direcorty name does not match name '" + bundle.getName() + "' in metadata.");
}
} else {
_log.info("Invalid individual bundle directory " + dirName + ": Could not parse metadata file as json.");
}
}
} else {
_log.info("Invalid individual bundle directory " + dirName + ": Individual bundle directory " + dirName + " should contain " + BUNDLE_METADATA_FILENAME + " json file and a directory named " + BUNDLE_DATA_DIRNAME + " to be a valid bundle.");
}
}
return resultBundle;
}
use of org.onebusaway.admin.bundle.model.Bundle in project onebusaway-application-modules by camsys.
the class DirectoryBundleSource method getBundles.
@Override
public List<Bundle> getBundles() {
init();
List<Bundle> bundles = new ArrayList<Bundle>();
/* Start with a list of the directories in our bundleDirectory */
List<String> potentialBundles = getSubDirectoryNamesOfBundleDirectory();
_log.info("Found " + potentialBundles.size() + " potential individual bundle directories in master bundle directory.");
/*
* Check each directory to verify that it contains a bundle, and if so, add
* it to the list of bundles.
*/
for (String potentialBundle : potentialBundles) {
Bundle loadedBundle;
try {
loadedBundle = loadBundleDirectory(potentialBundle);
if (loadedBundle != null) {
bundles.add(loadedBundle);
}
} catch (IOException e) {
_log.info("Invalid Individual Bundle Directory: Exception loading individual bundle directory " + potentialBundle);
}
}
_log.info("Returning " + bundles.size() + " valid individual bundles in master bundle directory.");
return bundles;
}
use of org.onebusaway.admin.bundle.model.Bundle in project onebusaway-application-modules by camsys.
the class DirectoryStagingBundleSource method loadStagedBundleDirectory.
private Bundle loadStagedBundleDirectory(String dirName) throws IOException {
Bundle resultBundle = null;
File bundleFile = new File(getStagedBundleDirectory(), dirName);
// File bundleFile = getStagedBundleDirectory();
if (bundleFile.isDirectory()) {
// List the contents of the directory.
String[] dirList = bundleFile.list();
if (arrayContainsItem(dirList, AbstractBundleSource.BUNDLE_DATA_DIRNAME) && arrayContainsItem(dirList, AbstractBundleSource.BUNDLE_INPUT_DIRNAME) && arrayContainsItem(dirList, AbstractBundleSource.BUNDLE_OUTPUT_DIRNAME)) {
Bundle bundle = new Bundle();
bundle.setName(dirName);
bundle.setFiles(new ArrayList<BundleFile>());
for (String s : dirList) {
File fileCheck = new File(bundleFile, s);
if (fileCheck.isDirectory()) {
addSubdirectoryFiles(bundle.getFiles(), bundleFile, fileCheck);
} else {
BundleFile bf = new BundleFile();
bf.setFilename(s);
bundle.getFiles().add(bf);
}
}
resultBundle = bundle;
} else {
_log.error("unexpected format of bundle dir=" + dirName);
}
}
return resultBundle;
}
use of org.onebusaway.admin.bundle.model.Bundle in project onebusaway-application-modules by camsys.
the class DirectoryStagingBundleSource method checkIsValidBundleFile.
@Override
public boolean checkIsValidBundleFile(String bundleId, String relativeFilePath) {
boolean isValid = false;
Bundle requestedBundle;
try {
requestedBundle = loadStagedBundleDirectory(bundleId);
if (requestedBundle != null) {
if (requestedBundle.containsFile(relativeFilePath)) {
isValid = true;
}
}
} catch (IOException e) {
isValid = false;
}
return isValid;
}
use of org.onebusaway.admin.bundle.model.Bundle in project onebusaway-application-modules by camsys.
the class BundleBuildingUtil method generateJsonMetadata.
public String generateJsonMetadata(BundleBuildRequest request, BundleBuildResponse response, String bundleId) {
File bundleDir = new File(response.getBundleDataDirectory());
List<BundleFile> files = getBundleFilesWithSumsForDirectory(bundleDir, bundleDir);
Gson gson = new GsonBuilder().serializeNulls().registerTypeAdapter(DateTime.class, new JodaDateTimeAdapter()).registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter()).setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES).setPrettyPrinting().create();
Bundle bundle = new Bundle();
if (bundleId != null) {
// Use id from previous build
bundle.setId(bundleId);
} else {
bundle.setId(getBundleId(response.getBundleRootDirectory() + File.separator + "data" + File.separator + "metadata.json"));
}
bundle.setDataset(request.getBundleDirectory());
bundle.setName(request.getBundleName());
bundle.setServiceDateFrom(request.getBundleStartDate());
bundle.setServiceDateTo(request.getBundleEndDate());
DateTime now = new DateTime();
bundle.setCreated(now);
bundle.setUpdated(now);
List<String> applicableAgencyIds = new ArrayList<String>();
// TODO this should come from somewhere
// applicableAgencyIds.add("MTA NYCT");
bundle.setApplicableAgencyIds(applicableAgencyIds);
bundle.setFiles(files);
String output = gson.toJson(bundle);
String outputFilename = response.getBundleRootDirectory() + File.separator + META_DATA_FILE;
File outputFile = new File(outputFilename);
_log.info("creating metadata file=" + outputFilename);
PrintWriter writer = null;
try {
writer = new PrintWriter(outputFile);
writer.print(output);
} catch (Exception any) {
_log.error(any.toString(), any);
response.setException(any);
} finally {
writer.close();
}
return outputFilename;
}
Aggregations