/**
SpagoBI - The Business Intelligence Free Platform
Copyright (C) 2005-2009 Engineering Ingegneria Informatica S.p.A.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
**/
package it.eng.spagobi.engines.qbe.analysisstateloaders;
import it.eng.qbe.query.serializer.json.QuerySerializationConstants;
import it.eng.spagobi.utilities.assertion.Assert;
import it.eng.spagobi.utilities.engines.SpagoBIEngineRuntimeException;
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
/**
* @author Davide Zerbetto (davide.zerbetto@eng.it)
*/
public class Version3QbeEngineAnalysisStateLoader extends AbstractQbeEngineAnalysisStateLoader{
public final static String FROM_VERSION = "3";
public final static String TO_VERSION = "4";
/** Logger component. */
private static transient Logger logger = Logger.getLogger(Version3QbeEngineAnalysisStateLoader.class);
public Version3QbeEngineAnalysisStateLoader() {
super();
}
public Version3QbeEngineAnalysisStateLoader(IQbeEngineAnalysisStateLoader loader) {
super(loader);
}
public JSONObject convert(JSONObject data) {
JSONObject resultJSON;
JSONObject catalogueJSON;
JSONArray queriesJSON;
logger.debug( "IN" );
try {
Assert.assertNotNull(data, "Data to convert cannot be null");
logger.debug( "Converting from encoding version [" + FROM_VERSION + "] to encoding version [" + TO_VERSION + "] ..." );
logger.debug( "Data to convert [" + data.toString() + "]");
catalogueJSON = data.getJSONObject("catalogue");
// fix query encoding ...
queriesJSON = catalogueJSON.getJSONArray("queries");
logger.debug( "In the stored catalogue there are [" + queriesJSON.length() + "] to convert");
for(int i = 0; i < queriesJSON.length(); i++) {
convertQuery(queriesJSON.getJSONObject(i));
}
resultJSON = new JSONObject();
resultJSON.put("catalogue", catalogueJSON);
logger.debug( "Converted data [" + resultJSON.toString() + "]");
logger.debug( "Conversion from encoding version [" + FROM_VERSION + "] to encoding version [" + TO_VERSION + "] terminated succesfully" );
}catch(Throwable t) {
throw new SpagoBIEngineRuntimeException("Impossible to load from rowData [" + data + "]", t);
} finally {
logger.debug( "OUT" );
}
return resultJSON;
}
private void convertQuery(JSONObject queryJSON) {
JSONArray havingsJSON;
String queryId = null;
JSONArray subqueriesJSON;
logger.debug("IN");
try {
Assert.assertNotNull(queryJSON, "Query to be converted cannot be null");
queryId = queryJSON.getString("id");
logger.debug( "Converting query [" + queryId + "] ...");
logger.debug( "Query content to be converted [" + queryJSON.toString() + "]");
// adding having clause filters (empty)
havingsJSON = new JSONArray();
queryJSON.put(QuerySerializationConstants.HAVINGS, havingsJSON);
logger.debug( "Empty having clause filters array added");
// convert subqueries
subqueriesJSON = queryJSON.getJSONArray( "subqueries" );
logger.debug( "Query [" + queryId + "] have [" + subqueriesJSON.length() + "] subqueries to convert");
for(int j = 0; j < subqueriesJSON.length(); j++) {
logger.debug( "Converting subquery [" + (j+1)+ "] of query [" + queryId + "] ...");
convertQuery( subqueriesJSON.getJSONObject(j) );
}
logger.debug( "Query [" + queryId + "] converted succesfully");
}catch(Throwable t) {
throw new SpagoBIEngineRuntimeException("Impossible convert query [" + queryId + "]", t);
} finally {
logger.debug( "OUT" );
}
}
}