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;
}
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();
}
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();
}
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);
}
}
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;
}
Aggregations