
prerna.sablecc2.DbTranslationEditor Maven / Gradle / Ivy
The newest version!
package prerna.sablecc2;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import prerna.sablecc2.analysis.DepthFirstAdapter;
import prerna.sablecc2.node.AOperation;
import prerna.sablecc2.node.ARoutineConfiguration;
import prerna.sablecc2.node.PRoutine;
import prerna.util.Utility;
public class DbTranslationEditor extends DepthFirstAdapter {
private static final Logger LOGGER = LogManager.getLogger(DbTranslationEditor.class.getName());
// this will store the list of pixels that were passed in
private List pixels = new Vector();
// a replacement for the given input
private String origPixelPortion = null;
private String replacementPixel = null;
// set the engine name to find and replace
private String engineToReplace = null;
private String engineToFind = null;
private boolean neededModifcation = false;
@Override
public void caseARoutineConfiguration(ARoutineConfiguration node) {
List copy = new ArrayList(node.getRoutine());
for(PRoutine e : copy) {
String expression = e.toString();
LOGGER.info("Processing " + Utility.cleanLogString(expression));
e.apply(this);
// if we ended up making the modificaiton
// the replacement string will not be null
if(this.replacementPixel != null && this.origPixelPortion != null) {
String newExpression = expression.replace(this.origPixelPortion, this.replacementPixel);
this.pixels.add(newExpression);
// now we need to null replacement
this.replacementPixel = null;
this.origPixelPortion = null;
// set that we needed some kind of modication
this.neededModifcation = true;
} else {
this.pixels.add(expression);
}
}
}
@Override
public void inAOperation(AOperation node) {
defaultIn(node);
String reactorId = node.getId().toString().trim();
if(reactorId.equals("Database")) {
// right now, the only input for database is in the curRow
String dbInput = node.getOpInput().toString().trim();
if(dbInput.equals(this.engineToFind)) {
// okay, it is a match
// we need to replace
this.origPixelPortion = node.toString();
this.replacementPixel = node.toString().replace(this.engineToFind, this.engineToReplace);
}
}
}
public void setEngineToReplace(String engineToReplace) {
this.engineToReplace = engineToReplace;
}
public void setEngineToFind(String engineToFind) {
this.engineToFind = engineToFind;
}
public List getPixels() {
return pixels;
}
public boolean isNeededModifcation() {
return neededModifcation;
}
// public static void main(String[] args) throws Exception {
// // String expression = "CreateFrame(py); Database(Movie_RDBMS) | Select(Title, Title__Movie_Budget) | Import();";
// // DbTranslationEditor translation = new DbTranslationEditor();
// // translation.setEngineToFind("Movie_RDBMS");
// // translation.setEngineToReplace("MyMovie");
// //
// // try {
// // Parser p = new Parser(new Lexer(new PushbackReader(new InputStreamReader(new ByteArrayInputStream(expression.getBytes("UTF-8"))), expression.length())));
// // // parsing the pkql - this process also determines if expression is syntactically correct
// // Start tree = p.parse();
// // // apply the translation.
// // tree.apply(translation);
// // } catch (ParserException | LexerException | IOException e) {
// // classLogger.error(Constants.STACKTRACE, e);
// // }
// //
// // System.out.println(translation.pixels);
//
//
// TestUtilityMethods.loadDIHelper("C:\\workspace\\Semoss_Dev\\RDF_Map.prop");
//
// String engineProp = "C:\\workspace\\Semoss_Dev\\db\\LocalMasterDatabase.smss";
// IEngine coreEngine = new RDBMSNativeEngine();
// coreEngine.setEngineId(Constants.LOCAL_MASTER_DB_NAME);
// coreEngine.open(engineProp);
// DIHelper.getInstance().setLocalProperty(Constants.LOCAL_MASTER_DB_NAME, coreEngine);
//
// engineProp = "C:\\workspace\\Semoss_Dev\\db\\Movie.smss";
// coreEngine = new RDBMSNativeEngine();
// coreEngine.setEngineId("Movie");
// coreEngine.open(engineProp);
// DIHelper.getInstance().setLocalProperty("Movie", coreEngine);
//
// RDBMSNativeEngine insightEngine = coreEngine.getInsightDatabase();
// InsightAdministrator admin = new InsightAdministrator(insightEngine);
//
// // query the insights rdbms and get all the insights
// // also grab the layout... annoying
// String query = "select id, question_layout from question_id";
// IRawSelectWrapper idWrapper = WrapperManager.getInstance().getRawWrapper(insightEngine, query);
// while(idWrapper.hasNext()) {
// Object[] row = idWrapper.next().getValues();
// String insightId = row[0].toString();
// String layout = row[1].toString();
// Insight in = coreEngine.getInsight(insightId).get(0);
// if(in instanceof OldInsight) {
// // ignore
// continue;
// }
//
// // get the old
// List oldRecipe = in.getPixelList().getPixelRecipe();
// // store the new
// List newRecipe = new Vector();
//
// for(String expression : oldRecipe) {
// DbTranslationEditor translation = new DbTranslationEditor();
// translation.setEngineToFind("MovieDatabase");
// translation.setEngineToReplace("Movie");
//
// try {
// expression = PixelPreProcessor.preProcessPixel(expression.trim(), new ArrayList(), new HashMap());
// Parser p = new Parser(new Lexer(new PushbackReader(new InputStreamReader(new ByteArrayInputStream(expression.getBytes("UTF-8"))), expression.length())));
// // parsing the pkql - this process also determines if expression is syntactically correct
// Start tree = p.parse();
// // apply the translation.
// tree.apply(translation);
//
// // get the new recipe
// newRecipe.addAll(translation.pixels);
// } catch (ParserException | LexerException | IOException e) {
// classLogger.error(Constants.STACKTRACE, e);
// }
// }
//
// // now i am done looping through
// // so update the recipe for the insight
// LOGGER.info("UPDATING INSIGHT ID = " + in.getRdbmsId());
// admin.updateInsight(in.getRdbmsId(), in.getInsightName(), layout, newRecipe.toArray(new String[]{}));
// }
//
// LOGGER.info("DONE UPDATING ALL INSIGHT IDS");
// }
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy