Search in sources :

Example 1 with BundleFile

use of org.onebusaway.admin.bundle.model.BundleFile in project onebusaway-application-modules by camsys.

the class BundleBuildingUtil method getBundleFilesWithSumsForDirectory.

public List<BundleFile> getBundleFilesWithSumsForDirectory(File baseDir, File dir, File rootDir) throws IllegalArgumentException {
    List<BundleFile> files = new ArrayList<BundleFile>();
    if (!dir.isDirectory()) {
        throw new IllegalArgumentException(dir.getPath() + " is not a directory");
    } else {
        for (String filePath : dir.list()) {
            File listEntry = new File(dir, filePath);
            String listEntryFilename = null;
            try {
                listEntryFilename = listEntry.getCanonicalPath();
            } catch (Exception e) {
            // bury
            }
            // prevent lock files from insertion into json, they change
            if (listEntry.isFile() && listEntryFilename != null && !listEntryFilename.endsWith(".lck")) {
                BundleFile file = new BundleFile();
                String relPathToBase = null;
                if (rootDir == null) {
                    _log.debug("baseDir=" + baseDir + "; listEntry=" + listEntry);
                    relPathToBase = baseDir.toURI().relativize(listEntry.toURI()).getPath();
                    file.setFilename(relPathToBase);
                } else {
                    relPathToBase = rootDir.toURI().relativize(listEntry.toURI()).getPath();
                    file.setFilename(relPathToBase);
                }
                String sum = getMd5ForFile(listEntry);
                file.setMd5(sum);
                files.add(file);
                _log.debug("file:" + listEntry + " has Md5=" + sum);
            } else if (listEntry.isDirectory()) {
                files.addAll(getBundleFilesWithSumsForDirectory(baseDir, listEntry, rootDir));
            }
        }
    }
    return files;
}
Also used : ArrayList(java.util.ArrayList) 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) IOException(java.io.IOException)

Example 2 with BundleFile

use of org.onebusaway.admin.bundle.model.BundleFile 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 3 with BundleFile

use of org.onebusaway.admin.bundle.model.BundleFile in project onebusaway-application-modules by camsys.

the class DirectoryStagingBundleSource method addSubdirectoryFiles.

private void addSubdirectoryFiles(List<BundleFile> bundleFiles, File baseDirectory, File directory) {
    if (directory.isDirectory()) {
        File[] files = directory.listFiles();
        if (files == null)
            return;
        for (File file : files) {
            if (file.isDirectory()) {
                addSubdirectoryFiles(bundleFiles, baseDirectory, file);
            } else {
                BundleFile bf = new BundleFile();
                bf.setFilename(baseDirectory.toURI().relativize(file.toURI()).toString());
                bundleFiles.add(bf);
            }
        }
    }
}
Also used : BundleFile(org.onebusaway.admin.bundle.model.BundleFile) BundleFile(org.onebusaway.admin.bundle.model.BundleFile) File(java.io.File)

Example 4 with BundleFile

use of org.onebusaway.admin.bundle.model.BundleFile 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

File (java.io.File)4 BundleFile (org.onebusaway.admin.bundle.model.BundleFile)4 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Bundle (org.onebusaway.admin.bundle.model.Bundle)2 SourceFile (org.onebusaway.admin.bundle.model.SourceFile)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 PrintWriter (java.io.PrintWriter)1 DateTime (org.joda.time.DateTime)1 LocalDate (org.joda.time.LocalDate)1