You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
207 lines
6.2 KiB
207 lines
6.2 KiB
package de.superx.bin; |
|
|
|
import java.sql.Connection; |
|
import java.sql.PreparedStatement; |
|
import java.sql.SQLException; |
|
import java.sql.Statement; |
|
import java.util.logging.Level; |
|
import java.util.logging.Logger; |
|
|
|
import org.w3c.dom.Document; |
|
import org.w3c.dom.Node; |
|
import org.w3c.dom.NodeList; |
|
|
|
import de.memtext.util.ExceptionHandler; |
|
import de.memtext.util.StringUtils; |
|
import de.memtext.util.XMLUtils; |
|
import de.superx.common.DBServletException; |
|
|
|
public class XUpdater { |
|
/** |
|
* TODO Idee war einbau in SXSQLServer.executeAll aber Problem temp tables |
|
* sind im preparedstatement nicht bekannt müsste für Batchmode auch |
|
* angepasst werden if (sqlstmt.indexOf("<xupdate>") > -1) { try { |
|
* XUpdater.execute(mandantenID, sqlstmt); } catch (DBServletException e) { |
|
* errorstring = e.toString(); e.printStackTrace(); } } else { |
|
* |
|
* @param mandantenID |
|
* @param content |
|
* @param databaseAbbr |
|
* PG/IDS |
|
* @return |
|
* @throws SQLException |
|
* @throws DBServletException |
|
*/ |
|
public String execute(Connection con, String databaseAbbr, String content, |
|
Logger logger) throws SQLException { |
|
String sql=""; |
|
String comment = ""; |
|
try { |
|
content = content.trim(); |
|
// Altlast |
|
ExceptionHandler old = XMLUtils.getExceptionHandler(); |
|
ExceptionHandler2 newE = new ExceptionHandler2(); |
|
XMLUtils.setExceptionHandler(newE); |
|
Document doc = XMLUtils.buildDocumentFromString(content, false); |
|
if (doc == null) { |
|
XMLUtils.setExceptionHandler(old); |
|
throw new IllegalArgumentException("Ungültiges XML Dokument"); |
|
} |
|
if (newE.hasException()) { |
|
XMLUtils.setExceptionHandler(old); |
|
throw new IllegalArgumentException("Ungültiges XML Dokument" |
|
+ newE.getException()); |
|
} |
|
|
|
|
|
Statement stm = con.createStatement(); |
|
stm.execute("create temp table tmp_once(col1 integer)"); |
|
stm.execute("insert into tmp_once values (1)"); |
|
sql="Einzel-SQL"; |
|
executeSqlList("sql",databaseAbbr, logger,doc, stm); |
|
|
|
NodeList list2 = doc.getElementsByTagName("text"); |
|
for (int i = 0; i < list2.getLength(); i++) { |
|
Node n = list2.item(i); |
|
String table = XMLUtils.getAttribValue(n, "table"); |
|
String field = XMLUtils.getAttribValue(n, "field"); |
|
String where = XMLUtils.getAttribValue(n, "where"); |
|
// getChildNodeValues -- Inhalt aller Unterknoten, weil \n vor |
|
// CDATA als eigener Knoten interpretiert werden kann |
|
if (!XMLUtils.hasValue(n) |
|
|| XMLUtils.getChildNodeValues(n).trim().equals("")) { |
|
comment += "NOTIZ: Kein Inhalt für " |
|
+ table |
|
+ " " |
|
+ field |
|
+ " " |
|
+ where |
|
+ (XMLUtils.getChildNodeValues(n).trim().equals("") ? " (vor CDATA kein Leerzeichen o Umbruch!)" |
|
: "") + "<br> "; |
|
} else { |
|
String fieldcontent = XMLUtils.getChildNodeValues(n); |
|
if (fieldcontent.equals("null")) { |
|
sql="update " + table + " set " + field |
|
+ "=null where " + where; |
|
|
|
stm.executeUpdate(sql); |
|
} else { |
|
fieldcontent = StringUtils.replace(fieldcontent, |
|
"CDATASTART", "<![CDATA["); |
|
fieldcontent = StringUtils.replace(fieldcontent, |
|
"CDATAEND", "]]>"); |
|
|
|
sql="update " |
|
+ table + " set " + field + "=? where " + where; |
|
PreparedStatement pst = con.prepareStatement(sql); |
|
pst.setString(1, fieldcontent); |
|
pst.executeUpdate(); |
|
pst.close(); |
|
} |
|
} |
|
} |
|
NodeList list3 = doc.getElementsByTagName("themenbaum"); |
|
for (int i = 0; i < list3.getLength(); i++) { |
|
Node n = list3.item(i); |
|
String maskentid = XMLUtils.getAttribValue(n, "maskentid"); |
|
String knoten = XMLUtils.getAttribValue(n, "parentname"); |
|
|
|
sql = "create temp table tmp_themenbaum (tid serial, name char(255) not null, maskeninfo_id integer, parent integer, gueltig_seit date, gueltig_bis date, erlaeuterung char(240)); \n"; |
|
stm.executeUpdate(sql); |
|
sql = "insert into tmp_themenbaum (tid,name,maskeninfo_id,gueltig_seit,gueltig_bis) select max(T.tid)+1, M.name, " |
|
+ maskentid |
|
+ ", date_val('01.01.1900'), date_val('01.01.3000') from maskeninfo M, themenbaum T where M.tid=" |
|
+ maskentid + "::integer group by 2,3,4,5 ;\n"; |
|
stm.executeUpdate(sql); |
|
sql = "update tmp_themenbaum set parent=(select tid from themenbaum where name='" |
|
+ knoten + "'); \n"; |
|
stm.executeUpdate(sql); |
|
sql = "insert into themenbaum (tid,name,maskeninfo_id,parent,gueltig_seit,gueltig_bis) select tid,name,maskeninfo_id,parent,gueltig_seit,gueltig_bis from tmp_themenbaum; \n"; |
|
stm.executeUpdate(sql); |
|
sql = "drop table tmp_themenbaum"; |
|
|
|
stm.executeUpdate(sql); |
|
} |
|
|
|
// postsql |
|
executeSqlList("postsql",databaseAbbr, logger, doc, stm); |
|
|
|
sql="drop table tmp_once"; |
|
stm.executeUpdate(sql); |
|
stm.close(); |
|
con.close(); |
|
|
|
comment = "Keine Fehler aufgefallen ("+de.memtext.util.DateUtils.getNowString()+")<br>" + comment; |
|
} |
|
catch (SQLException e) |
|
{ |
|
|
|
throw new SQLException("Fehler bei "+sql+"\n"+e); |
|
|
|
} |
|
return comment; |
|
} |
|
|
|
/** |
|
* @param databaseAbbr |
|
* @param logger |
|
* @param sql |
|
* @param doc |
|
* @param stm |
|
* @return |
|
* @throws SQLException |
|
*/ |
|
private void executeSqlList(String sqlElem,String databaseAbbr, Logger logger, Document doc, Statement stm) throws SQLException { |
|
String sql; |
|
NodeList list = doc.getElementsByTagName(sqlElem); |
|
|
|
for (int i = 0; i < list.getLength(); i++) { |
|
Node n = list.item(i); |
|
String database = ""; |
|
if (XMLUtils.hasAttrib(n, "database")) |
|
database = XMLUtils.getAttribValue(n, "database"); |
|
if (database != null && !database.equals("") |
|
&& !database.equals("PG") && !database.equals("IDS")) |
|
throw new IllegalArgumentException( |
|
"als database-Attribut hier nur PG oder IDS erlaubt"); |
|
|
|
if (database.equals("")||database.equals(databaseAbbr)) { |
|
sql = XMLUtils.getTheValue(n); |
|
logger.log(Level.FINER, " execute SQL from XUpdater:" + sql); |
|
|
|
try |
|
{ |
|
stm.execute(sql); |
|
} |
|
catch (Exception e) |
|
{ |
|
throw new SQLException("Fehler bei SQL:"+sql); |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
} |
|
|
|
static class ExceptionHandler2 extends ExceptionHandler { |
|
private Throwable e = null; |
|
|
|
public void clear() { |
|
e = null; |
|
} |
|
|
|
public void handle(String txt, Throwable e) { |
|
this.e = e; |
|
} |
|
|
|
public boolean hasException() { |
|
return e != null; |
|
|
|
} |
|
|
|
public Throwable getException() { |
|
return e; |
|
} |
|
} |
|
}
|
|
|