/* * de.superx.etl - a package for controlling ETL routines * Copyright (C) 2021 Daniel Quathamer * * This package is licensed under the CampusSource License; * http://www.campussource.de/org/license/ */ package de.superx.elt; import java.io.IOException; import java.io.PrintStream; import java.io.StringWriter; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Iterator; import java.util.Properties; import javax.sql.DataSource; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowCallbackHandler; import org.springframework.jdbc.datasource.DriverManagerDataSource; import de.memtext.rights.RightsKeyEntry; import de.superx.common.SuperX_el; import de.superx.common.SxResultRow; import de.superx.common.SxResultSet; import de.superx.common.SxSqlHelper; import de.superx.etl.EtlUtils; import de.superx.servlet.SxPools; import de.superx.util.SqlStringUtils; import freemarker.template.TemplateBooleanModel; public class EtlActionJob extends EtlAction { private String mandantenID; private String jobTid; private String jobUniquename; private String jobLogfile; private String jobCaption; private Properties runTimeParams; private StringWriter sw; public EtlActionJob(String uniquename, String logfile, String propfile) { super(uniquename, logfile, propfile); sw=new StringWriter(); } public EtlActionJob(String uniquename) { super(uniquename); sw=new StringWriter(); } public StringWriter getSw() { return sw; } public void setSw(StringWriter sw) { this.sw = sw; } public void setRunTimeParams(String runTimeParamsArg) throws IOException { if(runTimeParamsArg!=null) this.runTimeParams = EtlUtils.convertStringToProperty(runTimeParamsArg); } public void initJob(String job,String runtimeParamsArg) throws Exception { long jetzt = new java.util.Date().getTime() ; EtlUtils.initJobEnvironment(); String paramName=null; String paramCaption; String paramDefault=""; String paramProperties=""; if(runtimeParamsArg==null) runtimeParamsArg=""; runtimeParamsArg="SUPERX_DIR="+EtlUtils.SUPERX_DIR+EtlUtils.NEWLINE+runtimeParamsArg; //super.addActionLog("runtimeParamsArg:"+runtimeParamsArg); this.setStarttime(jetzt); Connection myConnection=this.getDbconnection(); if(myConnection==null) myConnection=this.getConnection(myConnection); this.setDbconnection(myConnection); if(!SqlStringUtils.checkValidKeyEntry(job)) throw new SQLException("Invalid job "+job); String sql = "select J.tid,J.uniquename,J.caption,J.logfile, P.uniquename as param_name,P.name as param_caption, P.param_default from etl_job J left outer join etl_job_param P on (J.tid=P.etl_job_id) where J.uniquename='"+job+"';"; SuperX_el el = new SuperX_el(); SxSqlHelper sh=new SxSqlHelper(); sh.execute(sql, myConnection, el); if (el.getError_String() != null && !el.getError_String().trim().equals("")) throw new SQLException("\nProblem bei Job DETAILS:" + "\n\n Meldung:" + el.getError_String() + "\n sql:" + sql); SxResultSet result= el.getResultSet(); int rownr=0; for (Iterator it = result.iterator(); it.hasNext();) { rownr++; SxResultRow row = (SxResultRow) it.next(); jobTid=row.get(0).toString().trim(); jobUniquename=row.get(1).toString().trim(); jobCaption=row.get(2).toString().trim(); jobLogfile=row.get(3).toString().trim(); if(row.get(4)!=null) paramName=row.get(4).toString().trim(); if(row.get(5)!=null) paramCaption=row.get(5).toString().trim(); if(row.get(4)!=null) paramDefault=row.get(6).toString().trim(); if(paramName!=null) paramProperties+=(paramName+"="+paramDefault+EtlUtils.NEWLINE); } if(rownr==0) throw new Exception("Job "+job+" unbekannt"); //this.setLogfile(jobLogfile); //this.appendLog("Job gefunden: "+jobCaption+EtlUtils.NEWLINE); super.addActionLog("Job gefunden: "+jobCaption+EtlUtils.NEWLINE); if(!paramProperties.equals("")) this.setParams(EtlUtils.convertStringToProperty(paramProperties)); if(runtimeParamsArg!=null) { this.setRunTimeParams(runtimeParamsArg); super.addActionLog("Runtime Params: "+runtimeParamsArg); } if(runTimeParams!=null) this.setParams(de.superx.elt.EtlUtils.mergeParamProperties(this.getParams(), runTimeParams)); super.addActionLog("Job "+jobCaption+ " initialized"); } public int execute(String outfile) throws SQLException, Exception { Integer stepTid; String stepUniquename; String stepCaption; String stepLogfile; Integer stepSortnr; Integer stepForceContinue; Integer stepParentStepId; String stepTypeUniquename; String stepTypeCaption; String stepTypeHandler; int jobReturnCode=0; String sql = "select S.tid,"+ "S.uniquename,"+ "S.caption,"+ "S.systeminfo_id,"+ "R.sortnr,"+ "R.force_continue::integer as force_continue,"+ "R.parent_step_id,"+ "S.logfile,"+ "T.uniquename,"+ "T.caption,"+ "T.handler"; sql +=" FROM etl_step S, etl_step_type T, etl_step_relation R where T.tid=S.step_type_id and R.step_id=S.tid and R.job_id="+jobTid+" order by R.sortnr,R.tid;"; SuperX_el el = new SuperX_el(); SxSqlHelper sh=new SxSqlHelper(); try { sh.execute(sql, this.getConnection(null), el); } catch (Exception e1) { super.addActionLog("Fehler beim SQL: "+sql+EtlUtils.NEWLINE+el.getError_String().trim()); return 1; } if (el.getError_String() != null && !el.getError_String().trim().equals("")) throw new SQLException("\nProblem bei Steps DETAILS:" + "\n\n Meldung:" + el.getError_String() + "\n sql:" + sql); SxResultSet result= el.getResultSet(); int rownr=0; super.addActionLog("Steps found: "+ result.size()); //erstmal nicht mit Zeit: executionTime=\""+de.memtext.util.DateUtils.getTodayString()+" " + de.memtext.util.DateUtils.getNowString()+"\" sw.write("\n"); for (Iterator it = result.iterator(); it.hasNext();) { int stepReturnCode=0; rownr++; SxResultRow row = (SxResultRow) it.next(); stepTid=(Integer) row.get(0); stepUniquename=row.get(1).toString().trim(); stepCaption=row.get(2).toString().trim(); stepSortnr=(Integer) row.get(4); stepForceContinue=(Integer) row.get(5); stepParentStepId=(Integer) row.get(6); stepLogfile=row.get(7).toString().trim(); stepTypeUniquename=row.get(8).toString().trim();; stepTypeCaption=row.get(9).toString().trim();; stepTypeHandler=row.get(10).toString().trim();; EtlStep myStep=new EtlStep(stepUniquename,jobLogfile,propfile); myStep.setOutputStringWriter(sw); myStep.initStep(jobUniquename, this.getParams(), stepTid, stepUniquename, stepLogfile, stepCaption, stepSortnr, stepForceContinue, stepParentStepId, stepTypeUniquename, stepTypeCaption, stepTypeHandler); stepReturnCode=myStep.execute(); super.addActionLog(myStep.getActionLog().toString()); sw=myStep.getOutputStringWriter(); if(stepReturnCode!=0) { if(stepForceContinue.intValue()==0) jobReturnCode=1; break; } } if(rownr==0) throw new Exception("Steps unbekannt"); sw.write("\n\n"); if(outfile==null || outfile.equals("")) { System.out.println(sw.toString()); System.out.println(super.getActionLog()); } else { if(jobReturnCode==1) { System.err.println(super.getActionLog()); throw new Exception("Fehler bei Jobausführung"); } else System.out.println("Jobausführung Erfolg"); try { de.superx.elt.EtlUtils.saveFileContentsWithEncoding(outfile, sw.toString(), null); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); jobReturnCode=1; } } long jetzt = new java.util.Date().getTime() ; this.setEndtime(jetzt); return jobReturnCode; } }