//=== COPY LAYER
Resource dstLayer = dstGeostore.searchLayer(layerName);
if( dstLayer == null) {
LOGGER.info("Copying Layer into destination: " + srcLayer);
RESTResource layer = FlowUtil.copyResource(srcLayer);
dstGeostore.insert(layer);
}
//==================================================
//=== LAYERUPDATE
//===
//=== Insert LayerUpdate if it does not exist
Resource srcLayerUpdate = srcGeostore.searchLayerUpdate(layerName, year, month, day);
if (srcLayerUpdate == null) {
throw new ActionException(this, "Source LayerUpdate not found [layer:" + layerName + " year:" + year + " month:" + month + "]");
}
//=== COPY LAYERUPDATE
Resource dstLayerUpdate = dstGeostore.searchLayerUpdate(layerName, year, month, day);
if ( dstLayerUpdate == null ) {
LOGGER.info("Copying LayerUpdate " + layerName+":"+year+":"+month);
RESTResource layerUpdate = FlowUtil.copyResource(srcLayerUpdate);
dstGeostore.insert(layerUpdate);
}
//==================================================
//=== STATS
//===
//=== loop on all the statistics associated with the layer
Map<Long, Resource> srcChartScripts = new HashMap<Long, Resource>();
List<ShortResource> statsDefList = srcGeostore.searchStatsDefByLayer(layerName, true); // may be empty, is always not null
for (ShortResource statDef : statsDefList) {
String statsDefName = statDef.getName();
LOGGER.info("Found stats def :" + statsDefName);
//=== OVERWRITE STATSDEF
Resource dstStatsDef = dstGeostore.searchStatsDefByName(statsDefName);
if(dstStatsDef != null) {
LOGGER.info("Removing previous StatsDef " + statsDefName);
dstGeostore.delete(dstStatsDef.getId());
}
LOGGER.info("Copying StatsDef " + statsDefName);
Resource srcStatsDef = srcGeostore.searchStatsDefByName(statsDefName);
RESTResource statsDef = FlowUtil.copyResource(srcStatsDef);
dstGeostore.insert(statsDef);
//=== OVERWRITE STATSDATA
Resource dstStatsData = dstGeostore.searchStatsData(statsDefName, year, month, day);
if(dstStatsData != null) {
LOGGER.info("Removing previous StatsData [statsdef:"+statsDefName+" year:"+year+" month:"+month+"]");
dstGeostore.delete(dstStatsData.getId());
}
Resource srcStatsData = srcGeostore.searchStatsData(statsDefName, year, month, day);
if (srcStatsData == null) {
LOGGER.warn("No StatsData found for [statsdef:"+statsDefName+" year:"+year+" month:"+month+"]");
} else {
LOGGER.info("Copying StatsData " + srcStatsData.getName());
RESTResource statsData = FlowUtil.copyResource(srcStatsData);
dstGeostore.insert(statsData);
// Add attribute Published=true to srcGeostore, usefull for staging admin application
LOGGER.info("Adding attribute published=true to resource " + srcStatsData.getName());
RESTResource statsDataTmp = FlowUtil.copyResource(srcStatsData);
ShortAttribute published = new ShortAttribute(UNREDDLayerUpdate.Attributes.PUBLISHED.getName(), "true", DataType.STRING);
//DamianoG workaround for search in list due to ShortAttribute don't override equals method
boolean flag = true;
for(ShortAttribute el : statsDataTmp.getAttribute()){
if(el.toString().equals(published.toString())){
flag = false;
break;
}
}
//if(!statsDataTmp.getAttribute().contains(published)){
if(flag){
List<ShortAttribute> l = new ArrayList<ShortAttribute>();
l.add(published);
l.addAll(statsDataTmp.getAttribute());
statsDataTmp.setAttribute(l);
srcGeostore.delete(srcStatsData.getId());
srcGeostore.insert(statsDataTmp);
}
}
// Collect dependant chartscript to be copied
List<Resource> csList = srcGeostore.searchChartScriptByStatsDef(statsDefName);
for (Resource cs : csList) {
LOGGER.info("Collecting ChartScript: adding " + cs.getName());
srcChartScripts.put(cs.getId(), cs);
}
}
//==================================================
//=== CHARTS
//===
//=== loop on all the collected charts
for (Resource srcChartScript : srcChartScripts.values()) {
String chartScriptName = srcChartScript.getName();
LOGGER.info("Removing previous charts stuff: " + chartScriptName);
//== remove chartScript
Resource dstChartScript = dstGeostore.searchChartScript(chartScriptName);
if(dstChartScript != null) {
LOGGER.info("Removing previous ChartScript :"+ chartScriptName);
dstGeostore.delete(dstChartScript.getId());
}
//== remove chartData
List<ShortResource> dstChartDataList = dstGeostore.searchChartDataByChartScript(chartScriptName);
for (ShortResource dstChartData : dstChartDataList) {
LOGGER.info("Removing previous ChartData :"+ dstChartData.getName() + " [cscript:" + chartScriptName+ ']');
dstGeostore.delete(dstChartData.getId());
}
//== Insert chartScript
LOGGER.info("Copying ChartScript " + chartScriptName);
RESTResource chartScript = FlowUtil.copyResource(srcChartScript);
dstGeostore.insert(chartScript);
//DamianoG commented due to chartData must be recalculated not copyed from staging
//== Insert chartData
// List<Resource> srcChartDataList = srcGeostore.searchChartDataPublished(chartScriptName);