Search in sources :

Example 6 with Bundle

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;
}
Also used : Bundle(org.onebusaway.admin.bundle.model.Bundle) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) File(java.io.File)

Example 7 with Bundle

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;
}
Also used : Bundle(org.onebusaway.admin.bundle.model.Bundle) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 8 with Bundle

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;
}
Also used : Bundle(org.onebusaway.admin.bundle.model.Bundle) BundleFile(org.onebusaway.admin.bundle.model.BundleFile) BundleFile(org.onebusaway.admin.bundle.model.BundleFile) File(java.io.File)

Example 9 with Bundle

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;
}
Also used : Bundle(org.onebusaway.admin.bundle.model.Bundle) IOException(java.io.IOException)

Example 10 with Bundle

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;
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) Bundle(org.onebusaway.admin.bundle.model.Bundle) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) IOException(java.io.IOException) BundleFile(org.onebusaway.admin.bundle.model.BundleFile) File(java.io.File) BundleFile(org.onebusaway.admin.bundle.model.BundleFile) SourceFile(org.onebusaway.admin.bundle.model.SourceFile) PrintWriter(java.io.PrintWriter)

Aggregations

Bundle (org.onebusaway.admin.bundle.model.Bundle)11 IOException (java.io.IOException)7 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 BufferedWriter (java.io.BufferedWriter)2 OutputStream (java.io.OutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 ArrayList (java.util.ArrayList)2 Response (javax.ws.rs.core.Response)2 StreamingOutput (javax.ws.rs.core.StreamingOutput)2 LocalDate (org.joda.time.LocalDate)2 BundlesListMessage (org.onebusaway.admin.bundle.BundlesListMessage)2 BundleFile (org.onebusaway.admin.bundle.model.BundleFile)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 FileReader (java.io.FileReader)1 PrintWriter (java.io.PrintWriter)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 DateTime (org.joda.time.DateTime)1 JSONObject (org.json.JSONObject)1