1 64 65 package com.jcorporate.expresso.ext.xml.dbobj; 66 67 import com.jcorporate.expresso.core.dataobjects.DataObject; 68 import com.jcorporate.expresso.core.dataobjects.Defineable; 69 import com.jcorporate.expresso.core.db.DBException; 70 import com.jcorporate.expresso.core.dbobj.DBObject; 71 import com.jcorporate.expresso.core.misc.URLUTF8Encoder; 72 import com.jcorporate.expresso.kernel.util.FastStringBuffer; 73 import org.apache.log4j.Logger; 74 import org.xml.sax.Attributes ; 75 import org.xml.sax.SAXException ; 76 import org.xml.sax.helpers.DefaultHandler ; 77 78 import java.util.Iterator ; 79 import java.util.Vector ; 80 81 82 88 public class XMLImportDocument 89 extends DefaultHandler { 90 private DataObject myDBObject = null; 91 private String currentItem = ("None"); 92 private FastStringBuffer theData = new FastStringBuffer(); 93 private static final String thisClass = XMLImportDocument.class.getName() + "."; 94 private String currentFieldName = ("None"); 95 private Vector errorList = new Vector (3); 96 int recordCount = 0; 97 private String dbName = "default"; 98 private static Logger log = Logger.getLogger(XMLImportDocument.class); 99 100 103 public XMLImportDocument() { 104 super(); 105 } 106 107 112 public XMLImportDocument(DataObject newDBObject) { 113 super(); 114 myDBObject = newDBObject; 115 } 116 117 public void setDBName(String newName) { 118 dbName = newName; 119 } 120 121 public String getDBName() { 122 return dbName; 123 } 124 125 132 public void characters(char[] ch, int start, int length) 133 throws SAXException { 134 for (int i = start; i < start + length; i++) { 135 theData.append(ch[i]); 136 } 137 138 if (log.isDebugEnabled()) { 139 log.debug("Data for " + currentItem + ":" + 140 theData.toString()); 141 } 142 } 143 144 145 149 public void endDocument() 150 throws SAXException { 151 log.debug("End document"); 152 153 if (myDBObject != null) { 154 saveDBObject(); 155 } else { 156 throw new SAXException ("Database object is null - Unable to save object"); 157 } 158 } 159 160 161 168 public void endElement(String uri, String localpart, String rawname) 169 throws SAXException { 170 String myName = (thisClass + "endElement(String, String, String)"); 171 log.debug("End:" + rawname); 172 173 if (rawname.equals("ObjectName")) { 174 log.debug("Record count was " + recordCount); 175 176 177 recordCount = 0; 178 179 try { 180 Class c = Class.forName(theData.toString()); 181 myDBObject = (DataObject) c.newInstance(); 182 myDBObject.setDataContext(getDBName()); 183 } catch (ClassNotFoundException cn) { 184 throw new SAXException (myName + ":Database object not found:" + 185 cn.getMessage()); 186 } catch (InstantiationException ie) { 187 throw new SAXException (myName + 188 ":Database object cannot be instantiated:" + 189 ie.getMessage()); 190 } catch (IllegalAccessException iae) { 191 throw new SAXException (myName + 192 ":Illegal access loading Database object:" + 193 iae.getMessage()); 194 } catch (Exception eeother) { 195 throw new SAXException (eeother.getMessage()); 196 } 197 } else if (rawname.equals("FieldName")) { 198 currentFieldName = theData.toString(); 199 } else if (rawname.equals("FieldValue")) { 200 if (myDBObject != null) { 201 try { 202 if (myDBObject.getFieldMetaData(currentFieldName) == null) { 203 throw new SAXException (myName + ":Error setting field " + 204 currentFieldName + ":" + 205 "Field does not exist"); 206 } else if (myDBObject.getFieldMetaData(currentFieldName).isVirtual()) { 207 log.debug("Skipping virtual field " + currentFieldName); 208 } else { 209 myDBObject.set(currentFieldName, 210 URLUTF8Encoder.decode(theData.toString())); 211 } 212 } catch (DBException de) { 213 throw new SAXException (myName + ":Error setting field " + 214 currentFieldName + ":" + 215 de.getMessage()); 216 } 217 } 218 } 219 220 if (rawname.equals("DataRecord")) { 221 log.debug("--> Saving datarecord"); 222 223 if (myDBObject != null) { 224 saveDBObject(); 225 } else { 226 throw new SAXException ("DataObject was null - Error saving object"); 227 } 228 } 229 230 theData.clear(); 231 } 232 233 234 240 public Vector getErrorList() { 241 return errorList; 242 } 243 244 248 public void saveDBObject() { 249 try { 250 log.debug("Saving database object"); 251 252 if (myDBObject == null) { 253 throw new DBException("DataObject is null, cannot save"); 254 } 255 256 257 DataObject checkObject = null; 258 259 if (myDBObject instanceof DBObject) { 260 checkObject = ((DBObject) myDBObject).newInstance(); 261 } else { 262 try { 263 checkObject = (DataObject) myDBObject.getClass().newInstance(); 264 if (checkObject instanceof Defineable) { 265 ((Defineable) checkObject).setDefinitionName(((Defineable) myDBObject).getDefinitionName()); 266 } 267 java.util.Map attrMap = myDBObject.getAllAttributes(); 268 java.util.Iterator keys = attrMap.keySet().iterator(); 269 while (keys.hasNext()) { 270 String key = keys.next().toString(); 271 checkObject.setAttribute(key, attrMap.get(key)); 272 } 273 } catch (Exception e) { 274 throw new DBException("Error creating checkObject: " + e.getMessage(), e); 275 } 276 } 277 checkObject.setDataContext(getDBName()); 278 279 String oneKey = null; 280 281 for (Iterator e = myDBObject.getMetaData().getAllKeysMap().keySet().iterator(); 282 e.hasNext();) { 283 oneKey = (String ) e.next(); 284 checkObject.set(oneKey, myDBObject.getField(oneKey)); 285 } 286 if (checkObject.find()) { 287 log.debug("Record is there, updating"); 288 289 290 myDBObject.getMetaData().setCheckZeroUpdate(false); 291 myDBObject.setDataContext(getDBName()); 292 myDBObject.update(); 293 } else { 294 log.debug("Record is not there, adding"); 295 myDBObject.setDataContext(getDBName()); 296 myDBObject.add(); 297 log.debug("Added"); 298 } 299 300 recordCount++; 301 } catch (DBException de) { 302 de.printStackTrace(); 303 errorList.addElement(de); 304 log.error(de); 305 } 306 } 307 308 312 337 public void startElement(String uri, String localpart, String rawname, 339 Attributes attributes) 340 throws SAXException { 341 log.debug("Start:" + rawname); 342 currentItem = rawname; 343 } 344 348 public DataObject getDBObject() 349 throws DBException { 350 return myDBObject; 351 } 352 353 354 } 355 | Popular Tags |