Request request = null;
try {
request = RequestJDOMReader.parseFile(xmlFile);
} catch (Exception e) {
throw new ActionException(this, "Exception parsing input file " + xmlFile.getName());
}
String layerName = request.getLayername();
String year = request.getYear();
String month = request.getMonth();
String day = request.getDay();
String filename = NameUtils.buildTifFileName(layerName, year, month, day);
LOGGER.info("Input parameters : [layer=" + layerName + ", year=" + year + ", month=" + month + "]");
// ****************************************
// Load source Layer info
//
// ****************************************
LOGGER.info("Searching source Layer " + layerName);
Resource srcLayer = srcGeostore.searchLayer(layerName);
if(srcLayer == null) {
throw new ActionException(this, "Source Layer not found [" + layerName+ "]");
}
UNREDDLayer layerResource = new UNREDDLayer(srcLayer);
String srcPath = layerResource.getAttribute(UNREDDLayer.Attributes.MOSAICPATH);
String dstPath = layerResource.getAttribute(UNREDDLayer.Attributes.DISSMOSAICPATH);
LOGGER.info(layerName + " found in the Staging Area Geostore");
// TODO ***** add layer in destination if it does not exist
LOGGER.error("TODO: add layer in destination if it does not exist");
// ****************************************
// check source layer update
//
// ****************************************
LOGGER.info("Searching source LayerUpdate [" + layerName + ", " + year + "," + month + "]");
Resource srcLayerUpdatesSA = srcGeostore.searchLayerUpdate(layerName, year, month, day);
if (srcLayerUpdatesSA == null) {
throw new ActionException(this, "Source LayerUpdate not found [" + layerName + ", " + year + "," + month + "]");
}
LOGGER.info("Source LayerUpdate found [" + layerName + ", " + year + "," + month + "]");
boolean isVector = request.getFormat().equals(UNREDDFormat.VECTOR);
DataStore srcDS=null,destDS=null;
try {
srcDS=PostGISUtils.createDatastore(conf.getSrcPostGisConfig());
destDS=PostGISUtils.createDatastore(conf.getDstPostGisConfig());
if (isVector) {
// ****************************************
// Copy the features and raster data
// ----------
// Copy feats identified by layer, year and month
// from the staging area postgis db to the dissemination db
// in case update is set on true we should
// firstly remove the old features from the dissemination db.
// This controls will be made directly in the updatePostGIS method.
//
// After features copy copy the raster data from staging mosaic path to dissemination mosaic path.
// Altought the mosaic_path destination This tiff is not a mosaic granule but is needed for dynamic stats.
// ****************************************
LOGGER.info("The request is VECTOR format based:");
LOGGER.info("Updating PostGIS...");
updatePostGIS(srcDS, destDS, layerName, year, month, day);
LOGGER.info("Copy raster data used for dynamic stats...");
this.copyRaster(srcPath, dstPath, layerName, year, month, day);
}
else {
// ****************************************
// Copy the raster
// ----------
// copies the raster located into the mosaic
// staging area directory to the dissemination mosaic.
// This operation is performed both in case of a first publishing
// and successive ones
//
// ****************************************
LOGGER.info("The request is RASTER format based:");
LOGGER.info("Update the mosaic...");
File srcRasterFile = new File(srcPath, filename);
File mosaicDir = new File(dstPath);
String style = layerResource.getAttribute(UNREDDLayer.Attributes.LAYERSTYLE);
if(style==null || style.isEmpty()){
style = DEFAULT_MOSAIC_STYLE;
}
StringBuilder msg = new StringBuilder();
msg.append("Publishing the Mosaic Granule with Style -> ");
msg.append(style);
LOGGER.info(msg.toString());
//create bounding box with values setted on GeoStore
double [] bbox = new double[4];
bbox[0] = Double.valueOf(layerResource.getAttribute(Attributes.RASTERX0));
bbox[1] = Double.valueOf(layerResource.getAttribute(Attributes.RASTERY0));
bbox[2] = Double.valueOf(layerResource.getAttribute(Attributes.RASTERX1));
bbox[3] = Double.valueOf(layerResource.getAttribute(Attributes.RASTERY1));
Mosaic mosaic = new Mosaic(conf.getDstGeoServerConfig(), mosaicDir, getTempDir(), getConfigDir());
mosaic.add(conf.getDstGeoServerConfig().getWorkspace(), layerName, srcRasterFile, "EPSG:4326", bbox, style, conf.getDatastorePath());
}
} catch (PostGisException e) {
LOGGER.debug("Property settings : [Layer = " + layerName + ", year = " + year + ", month=" + month + "]");
throw new ActionException(this, "Error while copying features", e );
} catch(IOException e) {
throw new ActionException(this, "Error while copying raster", e );
} finally {
PostGISUtils.quietDisposeStore(srcDS);
PostGISUtils.quietDisposeStore(destDS);
}
// ****************************************
// Copy GeoStoreUtil data
// ----------
// copy the resources related to the layer update
// resource identified by layer, year and month to the
// dissemination geostore
// ****************************************
// TODO: to complete using the setData
try {
LOGGER.info("Copying Geostore content");
this.copyGeostoreData(srcGeostore, dstGeostore, layerName, year, month, day);
LOGGER.debug("Geostore copy successfully completed");
} catch (Exception e) {
throw new ActionException(this, "Error while copying GeoStore content",e );
}
// ********************
// Run stats
// ********************
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Starting statistic processing");
}
this.listenerForwarder.progressing(80, "Starting statistic processing");
FlowUtil flowUtil= new FlowUtil(getTempDir(), getConfigDir());
try {
File dissRasterFile = new File(dstPath, filename);
flowUtil.runStatsAndScripts(layerName, year, month, day, dissRasterFile, dstGeostore);
} catch (FlowException e) {
throw new ActionException(this, e.getMessage(), e);
}
// ****************************************
// Copy original data
// ----------