Search in sources :

Example 1 with CloseableFile

use of com.runwaysdk.resource.CloseableFile in project geoprism-registry by terraframe.

the class DHIS2PluginZipManager method extractAndReplace.

private CloseableFile extractAndReplace() {
    // create a buffer to improve copy performance later.
    byte[] buffer = new byte[2048];
    CloseableFile directory = GeoprismProperties.newTempDirectory();
    try {
        InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("cgr-dhis2-app.zip");
        ZipInputStream zstream = new ZipInputStream(is);
        ZipEntry entry;
        while ((entry = zstream.getNextEntry()) != null) {
            File file = new File(directory, entry.getName());
            logger.info("Writing to [" + file.getAbsolutePath() + "].");
            if (!entry.isDirectory()) {
                FileOutputStream output = null;
                try {
                    output = new FileOutputStream(file);
                    int len = 0;
                    while ((len = zstream.read(buffer)) > 0) {
                        output.write(buffer, 0, len);
                    }
                } finally {
                    if (output != null) {
                        output.close();
                    }
                }
                if (file.getName().equals(REPLACE_FILENAME)) {
                    String indexHtml = FileUtils.readFileToString(file, "UTF-8");
                    indexHtml = indexHtml.replace(REPLACE_TOKEN, GeoregistryProperties.getRemoteServerUrl());
                    FileUtils.writeStringToFile(file, indexHtml, Charset.forName("UTF-8"));
                }
            } else {
                file.mkdir();
            }
        }
    } catch (IOException e1) {
        throw new RuntimeException(e1);
    }
    return directory;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) FileOutputStream(java.io.FileOutputStream) CloseableFile(com.runwaysdk.resource.CloseableFile) IOException(java.io.IOException) CloseableFile(com.runwaysdk.resource.CloseableFile) File(java.io.File)

Example 2 with CloseableFile

use of com.runwaysdk.resource.CloseableFile in project geoprism-registry by terraframe.

the class ExcelImporter method getExcelFileFromResource.

public static CloseableFile getExcelFileFromResource(ApplicationResource res) {
    final String extension = "xlsx";
    try {
        if (res.getNameExtension().equals("zip")) {
            try (InputStream is = res.openNewStream()) {
                File dir = Files.createTempDirectory(res.getBaseName()).toFile();
                extract(is, dir);
                File[] files = dir.listFiles(new FilenameFilter() {

                    @Override
                    public boolean accept(File dir, String name) {
                        return name.endsWith("." + extension);
                    }
                });
                if (files.length > 0) {
                    return new CloseableDelegateFile(files[0], dir);
                } else {
                    throw new ImportFileFormatException();
                }
            }
        } else if (res.getNameExtension().equals(extension)) {
            return res.openNewFile();
        }
    } catch (IOException e) {
        throw new ProgrammingErrorException(e);
    }
    throw new ImportFileFormatException();
}
Also used : FilenameFilter(java.io.FilenameFilter) CloseableDelegateFile(net.geoprism.registry.etl.CloseableDelegateFile) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) CloseableFile(com.runwaysdk.resource.CloseableFile) File(java.io.File) CloseableDelegateFile(net.geoprism.registry.etl.CloseableDelegateFile) ImportFileFormatException(net.geoprism.registry.etl.ImportFileFormatException) ProgrammingErrorException(com.runwaysdk.dataaccess.ProgrammingErrorException)

Example 3 with CloseableFile

use of com.runwaysdk.resource.CloseableFile in project geoprism-registry by terraframe.

the class ShapefileImporter method getShapefileFromResource.

public static CloseableFile getShapefileFromResource(ApplicationResource res, String extension) {
    try {
        if (res.getNameExtension().equals("zip")) {
            try (InputStream is = res.openNewStream()) {
                File dir = Files.createTempDirectory(res.getBaseName()).toFile();
                extract(is, dir);
                File[] dbfs = dir.listFiles(new FilenameFilter() {

                    @Override
                    public boolean accept(File dir, String name) {
                        return name.endsWith("." + extension);
                    }
                });
                if (dbfs.length > 0) {
                    return new CloseableDelegateFile(dbfs[0], dir);
                } else {
                    throw new ImportFileFormatException();
                }
            }
        }
    } catch (IOException e) {
        throw new ProgrammingErrorException(e);
    }
    throw new ImportFileFormatException();
}
Also used : FilenameFilter(java.io.FilenameFilter) CloseableDelegateFile(net.geoprism.registry.etl.CloseableDelegateFile) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) LineString(com.vividsolutions.jts.geom.LineString) IOException(java.io.IOException) CloseableFile(com.runwaysdk.resource.CloseableFile) File(java.io.File) CloseableDelegateFile(net.geoprism.registry.etl.CloseableDelegateFile) ImportFileFormatException(net.geoprism.registry.etl.ImportFileFormatException) ProgrammingErrorException(com.runwaysdk.dataaccess.ProgrammingErrorException)

Example 4 with CloseableFile

use of com.runwaysdk.resource.CloseableFile in project geoprism-registry by terraframe.

the class ShapefileService method getShapefileConfiguration.

@Request(RequestType.SESSION)
public JSONObject getShapefileConfiguration(String sessionId, String type, Date startDate, Date endDate, String fileName, InputStream fileStream, ImportStrategy strategy, Boolean copyBlank) {
    // Save the file to the file system
    try {
        ServerGeoObjectType geoObjectType = ServerGeoObjectType.get(type);
        VaultFile vf = VaultFile.createAndApply(fileName, fileStream);
        try (CloseableFile dbf = ShapefileImporter.getShapefileFromResource(vf, "dbf")) {
            SimpleDateFormat format = new SimpleDateFormat(GeoObjectImportConfiguration.DATE_FORMAT);
            format.setTimeZone(GeoRegistryUtil.SYSTEM_TIMEZONE);
            JSONObject object = new JSONObject();
            object.put(GeoObjectImportConfiguration.TYPE, this.getType(geoObjectType));
            object.put(GeoObjectImportConfiguration.SHEET, this.getSheetInformation(dbf));
            object.put(ImportConfiguration.VAULT_FILE_ID, vf.getOid());
            object.put(ImportConfiguration.FILE_NAME, fileName);
            object.put(GeoObjectImportConfiguration.HAS_POSTAL_CODE, PostalCodeFactory.isAvailable(geoObjectType));
            object.put(ImportConfiguration.IMPORT_STRATEGY, strategy.name());
            object.put(ImportConfiguration.FORMAT_TYPE, FormatImporterType.SHAPEFILE.name());
            object.put(ImportConfiguration.OBJECT_TYPE, ObjectImporterFactory.ObjectImportType.GEO_OBJECT.name());
            object.put(ImportConfiguration.COPY_BLANK, copyBlank);
            if (startDate != null) {
                object.put(GeoObjectImportConfiguration.START_DATE, format.format(startDate));
            }
            if (endDate != null) {
                object.put(GeoObjectImportConfiguration.END_DATE, format.format(endDate));
            }
            return object;
        }
    } catch (RunwayException | SmartException e) {
        throw e;
    } catch (Exception e) {
        throw new ProgrammingErrorException(e);
    }
}
Also used : JSONObject(org.json.JSONObject) SmartException(com.runwaysdk.business.SmartException) ServerGeoObjectType(net.geoprism.registry.model.ServerGeoObjectType) VaultFile(com.runwaysdk.system.VaultFile) CloseableFile(com.runwaysdk.resource.CloseableFile) SimpleDateFormat(java.text.SimpleDateFormat) RunwayException(com.runwaysdk.RunwayException) SmartException(com.runwaysdk.business.SmartException) ProgrammingErrorException(com.runwaysdk.dataaccess.ProgrammingErrorException) RunwayException(com.runwaysdk.RunwayException) ShapefileFormatException(net.geoprism.registry.etl.ShapefileFormatException) IOException(java.io.IOException) ProgrammingErrorException(com.runwaysdk.dataaccess.ProgrammingErrorException) Request(com.runwaysdk.session.Request)

Example 5 with CloseableFile

use of com.runwaysdk.resource.CloseableFile in project geoprism-registry by terraframe.

the class GeotoolsLoopTest method runInReq.

@Request
public static Set<String> runInReq(ApplicationResource res, boolean sort) throws Exception {
    Set<String> featureIdSet = new HashSet<String>();
    Set<String> nonUniqueFeatureIds = new HashSet<String>();
    try (CloseableFile shp = ShapefileImporter.getShapefileFromResource(res, "shp")) {
        FileDataStore myData = FileDataStoreFinder.getDataStore(shp);
        try {
            SimpleFeatureSource source = myData.getFeatureSource();
            Query query = new Query();
            if (sort) {
                // Enforce predictable ordering based on alphabetical Feature Ids
                query.setSortBy(new SortBy[] { SortBy.NATURAL_ORDER });
            }
            long total = source.getFeatures(query).size();
            SimpleFeatureIterator iterator = source.getFeatures(query).features();
            long count = 0;
            try {
                while (iterator.hasNext()) {
                    SimpleFeature feature = iterator.next();
                    String featureId = feature.getID();
                    if (!featureIdSet.add(featureId)) {
                        nonUniqueFeatureIds.add(featureId);
                    }
                    count++;
                }
            } finally {
                iterator.close();
            }
            System.out.println("Imported " + count + " of total " + total);
        } finally {
            myData.dispose();
        }
    }
    if (nonUniqueFeatureIds.size() > 0) {
        throw new RuntimeException("Detected non-unique feature Ids [" + StringUtils.join(nonUniqueFeatureIds, ", ") + "]");
    }
    return featureIdSet;
}
Also used : SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) Query(org.geotools.data.Query) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) CloseableFile(com.runwaysdk.resource.CloseableFile) FileDataStore(org.geotools.data.FileDataStore) SimpleFeature(org.opengis.feature.simple.SimpleFeature) HashSet(java.util.HashSet) Request(com.runwaysdk.session.Request)

Aggregations

CloseableFile (com.runwaysdk.resource.CloseableFile)5 IOException (java.io.IOException)4 ProgrammingErrorException (com.runwaysdk.dataaccess.ProgrammingErrorException)3 File (java.io.File)3 InputStream (java.io.InputStream)3 ZipInputStream (java.util.zip.ZipInputStream)3 Request (com.runwaysdk.session.Request)2 FilenameFilter (java.io.FilenameFilter)2 CloseableDelegateFile (net.geoprism.registry.etl.CloseableDelegateFile)2 ImportFileFormatException (net.geoprism.registry.etl.ImportFileFormatException)2 RunwayException (com.runwaysdk.RunwayException)1 SmartException (com.runwaysdk.business.SmartException)1 VaultFile (com.runwaysdk.system.VaultFile)1 LineString (com.vividsolutions.jts.geom.LineString)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 SimpleDateFormat (java.text.SimpleDateFormat)1 HashSet (java.util.HashSet)1 ZipEntry (java.util.zip.ZipEntry)1 ShapefileFormatException (net.geoprism.registry.etl.ShapefileFormatException)1