KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > export > Dumper


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.export;
31
32 import java.io.BufferedInputStream JavaDoc;
33 import java.io.BufferedOutputStream JavaDoc;
34 import java.io.File JavaDoc;
35 import java.io.FileOutputStream JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.InputStream JavaDoc;
38 import java.io.ObjectInputStream JavaDoc;
39 import java.io.ObjectOutputStream JavaDoc;
40 import java.io.OutputStream JavaDoc;
41 import java.io.StreamCorruptedException JavaDoc;
42 import java.io.UnsupportedEncodingException JavaDoc;
43 import java.math.BigDecimal JavaDoc;
44 import java.sql.ResultSet JavaDoc;
45 import java.sql.SQLException JavaDoc;
46 import java.sql.Timestamp JavaDoc;
47 import java.sql.Types JavaDoc;
48 import java.util.HashMap JavaDoc;
49
50 import com.genimen.djeneric.language.Messages;
51 import com.genimen.djeneric.repository.DjDomain;
52 import com.genimen.djeneric.repository.DjExtent;
53 import com.genimen.djeneric.repository.DjProperty;
54 import com.genimen.djeneric.repository.exceptions.DjenericException;
55 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException;
56 import com.genimen.djeneric.repository.rdbms.RdbmsExtent;
57 import com.genimen.djeneric.repository.rdbms.RdbmsPersistenceManager;
58 import com.genimen.djeneric.repository.rdbms.RdbmsSession;
59 import com.genimen.djeneric.repository.rdbms.SqlStatement;
60 import com.genimen.djeneric.util.DjLogger;
61 import com.genimen.djeneric.util.DjProgressDisplayer;
62
63 public class Dumper
64 {
65   private HashMap JavaDoc _ignoreList = new HashMap JavaDoc();
66
67   private static final String JavaDoc INVALID_FILE_MSG = Messages.getString("Dumper.ImportFileInvalid");
68   private static final String JavaDoc CP1252 = "Cp1252";
69   private static final String JavaDoc UTF8 = "UTF8";
70
71   public final static String JavaDoc HEADER = "DJENERIC_EXPORT_DUMP";
72
73   RdbmsPersistenceManager _manager;
74   DjProgressDisplayer _progressMonitor;
75   RdbmsSession _session = null;
76
77   public Dumper(DjProgressDisplayer progressMonitor, RdbmsPersistenceManager manager)
78   {
79     _progressMonitor = progressMonitor;
80     _manager = manager;
81   }
82
83   public void ignoreExtent(String JavaDoc extentName)
84   {
85     _ignoreList.put(extentName, extentName);
86   }
87
88   public void setContext(String JavaDoc context) throws ObjectNotDefinedException, DjenericException
89   {
90     _manager.setCurrentContext(context);
91   }
92
93   public RdbmsPersistenceManager getManager()
94   {
95     return _manager;
96   }
97
98   public RdbmsSession getSession() throws DjenericException
99   {
100     if (_session == null) _session = (RdbmsSession) _manager.createSession();
101     return _session;
102   }
103
104   public void close()
105   {
106     if (_manager != null) _manager.close();
107   }
108
109   public void println(String JavaDoc msg)
110   {
111     _progressMonitor.displayProgress(msg);
112   }
113
114   public long exportDump(File JavaDoc outFile) throws IOException JavaDoc, DjenericException, SQLException JavaDoc
115   {
116     OutputStream JavaDoc outputStream = new FileOutputStream JavaDoc(outFile);
117     ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(new BufferedOutputStream JavaDoc(outputStream));
118
119     oos.write(HEADER.getBytes(UTF8));
120     byte[] modelBytes = _manager.getModel().getBytes(UTF8);
121     oos.writeInt(modelBytes.length);
122     oos.write(modelBytes);
123
124     DjExtent extent[] = _manager.getExtents();
125     long counter = 0;
126     for (int i = 0; i < extent.length; i++)
127     {
128       if (!_ignoreList.containsKey(extent[i].getName())) counter += exportExtent(oos, (RdbmsExtent) extent[i]);
129     }
130
131     oos.close();
132     _progressMonitor.displayProgress(Messages.getString("Dumper.ExportDone", String.valueOf(counter)));
133     close();
134
135     return counter;
136   }
137
138   protected long exportExtent(ObjectOutputStream JavaDoc oos, RdbmsExtent extent) throws DjenericException, SQLException JavaDoc,
139       IOException JavaDoc
140   {
141     _progressMonitor.displayProgress(Messages.getString("Dumper.ExportingExtent", extent.getName()));
142     byte[] extentAlias = extent.getInternalCode().getBytes(UTF8);
143
144     StringBuffer JavaDoc sqlStmt = new StringBuffer JavaDoc(100);
145     sqlStmt.append("select ");
146     DjProperty props[] = extent.getProperties();
147
148     for (int i = 0; i < props.length; i++)
149     {
150       if (i != 0) sqlStmt.append(", ");
151       sqlStmt.append(props[i].getName());
152     }
153     sqlStmt.append(" from ");
154     sqlStmt.append(extent.getName());
155
156     RdbmsSession session = (RdbmsSession) _manager.createSession();
157     SqlStatement stmt = session.getSqlStatement(sqlStmt.toString());
158     ResultSet JavaDoc rs = stmt.executeQuery();
159     long counter = 0;
160
161     while (rs.next())
162     {
163       counter++;
164       oos.writeInt(extentAlias.length);
165       oos.write(extentAlias);
166       for (int i = 0; i < props.length; i++)
167       {
168         int typeCode = props[i].getTypeCode();
169
170         switch (typeCode)
171         {
172           case DjDomain.BIGDECIMAL_TYPE :
173             writeBigDecimal(oos, rs, i);
174             break;
175           case DjDomain.BYTE_TYPE :
176             writeBytes(oos, props, rs, i);
177             break;
178           case DjDomain.DATE_TYPE :
179             writeDate(oos, rs, i);
180             break;
181           case DjDomain.INT_TYPE :
182             writeInt(oos, rs, i);
183             break;
184           case DjDomain.LONG_TYPE :
185             writeLong(oos, rs, i);
186             break;
187           case DjDomain.STRING_TYPE :
188             writeString(oos, props, rs, i);
189             break;
190           default :
191             throw new DjenericException(Messages.getString("global.PropUnsupported", String.valueOf(typeCode)));
192         }
193       }
194     }
195     rs.close();
196     stmt.close();
197     String JavaDoc name = (counter == 1 ? extent.getNameSingular() : extent.getNamePlural());
198     _progressMonitor
199         .displayProgress(" " + Messages.getString("Dumper.exported", String.valueOf(counter), name) + "\n");
200     return counter;
201
202   }
203
204   protected void writeString(ObjectOutputStream JavaDoc oos, DjProperty[] props, ResultSet JavaDoc rs, int i) throws SQLException JavaDoc,
205       IOException JavaDoc, UnsupportedEncodingException JavaDoc
206   {
207     if (props[i].isMappedToLong())
208     {
209       byte[] b = SqlStatement.getBlob(rs, props[i].getName());
210       oos.writeBoolean(b == null);
211       if (b != null)
212       {
213         oos.writeInt(b.length);
214         oos.write(b);
215       }
216     }
217     else
218     {
219       String JavaDoc s = rs.getString(i + 1);
220       boolean wasNull = rs.wasNull();
221       oos.writeBoolean(wasNull);
222       if (!wasNull)
223       {
224         byte[] b = s.getBytes(CP1252);
225         oos.writeInt(b.length);
226         oos.write(b);
227       }
228     }
229   }
230
231   protected void writeLong(ObjectOutputStream JavaDoc oos, ResultSet JavaDoc rs, int i) throws SQLException JavaDoc, IOException JavaDoc
232   {
233     long l = rs.getLong(i + 1);
234     boolean wasNull = rs.wasNull();
235     oos.writeBoolean(wasNull);
236     if (!wasNull) oos.writeLong(l);
237   }
238
239   protected void writeInt(ObjectOutputStream JavaDoc oos, ResultSet JavaDoc rs, int i) throws SQLException JavaDoc, IOException JavaDoc
240   {
241     int integer = rs.getInt(i + 1);
242     boolean wasNull = rs.wasNull();
243     oos.writeBoolean(wasNull);
244     if (!wasNull) oos.writeInt(integer);
245   }
246
247   protected void writeDate(ObjectOutputStream JavaDoc oos, ResultSet JavaDoc rs, int i) throws SQLException JavaDoc, IOException JavaDoc
248   {
249     Timestamp JavaDoc date = rs.getTimestamp(i + 1);
250     boolean wasNull = rs.wasNull();
251     oos.writeBoolean(wasNull);
252     if (!wasNull) oos.writeLong(date.getTime());
253   }
254
255   protected void writeBytes(ObjectOutputStream JavaDoc oos, DjProperty[] props, ResultSet JavaDoc rs, int i) throws SQLException JavaDoc,
256       IOException JavaDoc
257   {
258     if (props[i].isMappedToLong())
259     {
260       byte[] b = SqlStatement.getBlob(rs, props[i].getName());
261       boolean wasNull = rs.wasNull();
262       oos.writeBoolean(wasNull);
263       if (!wasNull)
264       {
265         oos.writeInt(b.length);
266         oos.write(b);
267       }
268     }
269     else
270     {
271       String JavaDoc s = rs.getString(i + 1);
272       boolean wasNull = rs.wasNull();
273       oos.writeBoolean(wasNull);
274       if (!wasNull)
275       {
276         byte[] b = s.getBytes(CP1252);
277         oos.writeInt(b.length);
278         oos.write(b);
279       }
280     }
281   }
282
283   protected void writeBigDecimal(ObjectOutputStream JavaDoc oos, ResultSet JavaDoc rs, int i) throws SQLException JavaDoc, IOException JavaDoc
284   {
285     BigDecimal JavaDoc bd = rs.getBigDecimal(i + 1);
286     boolean wasNull = rs.wasNull();
287     oos.writeBoolean(wasNull);
288     if (!wasNull) oos.writeFloat(bd.floatValue());
289   }
290
291   public HashMap JavaDoc determineNewIds(InputStream JavaDoc inputStream) throws IOException JavaDoc, DjenericException
292   {
293     _progressMonitor.displayProgress(Messages.getString("Dumper.DetermineNewIds"));
294     HashMap JavaDoc newids = new HashMap JavaDoc();
295
296     ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(new BufferedInputStream JavaDoc(inputStream));
297
298     byte[] header = new byte[HEADER.length()];
299
300     ois.readFully(header);
301     if (!new String JavaDoc(header, UTF8).equals(HEADER)) throw new DjenericException(INVALID_FILE_MSG);
302     int modelSize = ois.readInt();
303
304     byte[] modelBytes = new byte[modelSize];
305
306     ois.readFully(modelBytes);
307     String JavaDoc sourceModel = new String JavaDoc(modelBytes, UTF8);
308
309     ImportPersistenceManager srcMgr = new ImportPersistenceManager();
310     srcMgr.setModelManually(sourceModel);
311     srcMgr.loadModel();
312
313     while (ois.available() > 0)
314     {
315       determineNewId(ois, newids, srcMgr);
316     }
317
318     ois.close();
319     return newids;
320   }
321
322   protected void determineNewId(ObjectInputStream JavaDoc ois, HashMap JavaDoc newIds, ImportPersistenceManager srcMgr)
323       throws IOException JavaDoc, DjenericException
324   {
325     int length = ois.readInt();
326     byte[] extentAliasBuffer = new byte[length];
327     ois.readFully(extentAliasBuffer);
328     String JavaDoc extentAlias = new String JavaDoc(extentAliasBuffer, UTF8);
329
330     DjExtent extent = srcMgr.getExtentByInternalCode(extentAlias);
331     DjProperty idProperty = extent.getIdProperty();
332     DjProperty props[] = extent.getProperties();
333
334     for (int i = 0; i < props.length; i++)
335     {
336       int typeCode = props[i].getTypeCode();
337
338       switch (typeCode)
339       {
340         case DjDomain.BIGDECIMAL_TYPE :
341           readBigDecimal(ois);
342           break;
343         case DjDomain.BYTE_TYPE :
344           readBytes(ois);
345           break;
346         case DjDomain.DATE_TYPE :
347           readDate(ois);
348           break;
349         case DjDomain.INT_TYPE :
350           readInt(ois);
351           break;
352         case DjDomain.LONG_TYPE :
353           Long JavaDoc someLong = readLong(ois);
354           if (props[i] == idProperty)
355           {
356             newIds.put(someLong, new Long JavaDoc(_manager.getNextObjectId()));
357           }
358           break;
359         case DjDomain.STRING_TYPE :
360           readString(ois);
361           break;
362         default :
363           throw new DjenericException(Messages.getString("global.PropUnsupported", String.valueOf(typeCode)));
364       }
365     }
366   }
367
368   protected void checkModel(InputStream JavaDoc inputStream) throws StreamCorruptedException JavaDoc, IOException JavaDoc, DjenericException
369   {
370     ObjectInputStream JavaDoc oos = new ObjectInputStream JavaDoc(new BufferedInputStream JavaDoc(inputStream));
371
372     byte[] header = new byte[HEADER.length()];
373
374     oos.readFully(header);
375     if (!new String JavaDoc(header, UTF8).equals(HEADER)) throw new DjenericException(INVALID_FILE_MSG);
376     int modelSize = oos.readInt();
377
378     byte[] modelBytes = new byte[modelSize];
379
380     oos.readFully(modelBytes);
381     String JavaDoc sourceModel = new String JavaDoc(modelBytes, UTF8);
382
383     // If the repository is empty (no model defined)
384
// then import the model also
385
if (_manager.getExtents().length == 0)
386     {
387       _manager.setModel(sourceModel);
388       _manager.saveModel();
389       println(Messages.getString("Dumper.EmptyReposModelImported"));
390     }
391     oos.close();
392   }
393
394   protected long importDump(InputStream JavaDoc inputStream, HashMap JavaDoc newids) throws IOException JavaDoc, SQLException JavaDoc,
395       DjenericException
396   {
397     _progressMonitor.displayProgress(Messages.getString("Dumper.ImportFirstPass"));
398     ObjectInputStream JavaDoc oos = new ObjectInputStream JavaDoc(new BufferedInputStream JavaDoc(inputStream));
399
400     byte[] header = new byte[HEADER.length()];
401
402     oos.readFully(header);
403     if (!new String JavaDoc(header, UTF8).equals(HEADER)) throw new DjenericException(INVALID_FILE_MSG);
404     int modelSize = oos.readInt();
405
406     byte[] modelBytes = new byte[modelSize];
407
408     oos.readFully(modelBytes);
409     String JavaDoc sourceModel = new String JavaDoc(modelBytes, UTF8);
410
411     // If the repository is empty (no model defined)
412
// then import the model also
413
if (_manager.getExtents().length == 0)
414     {
415       _manager.setModel(sourceModel);
416       _manager.saveModel();
417       println(Messages.getString("Dumper.EmptyReposModelImported"));
418     }
419
420     ImportPersistenceManager srcMgr = new ImportPersistenceManager();
421     srcMgr.setModelManually(sourceModel);
422     srcMgr.loadModel();
423
424     long counter = 0;
425     while (oos.available() > 0)
426     {
427       importRecordPass1(oos, srcMgr, newids);
428       counter++;
429       if (counter % 125 == 0) _progressMonitor.displayProgress(Messages.getString("Dumper.ImportProgress", String
430           .valueOf(counter)));
431     }
432     _progressMonitor.displayProgress(Messages.getString("Dumper.FirstPassDone", String.valueOf(counter)));
433
434     srcMgr.close();
435
436     oos.close();
437     return counter;
438   }
439
440   protected long importRelations(InputStream JavaDoc inputStream, HashMap JavaDoc newids) throws IOException JavaDoc, SQLException JavaDoc,
441       DjenericException
442   {
443     _progressMonitor.displayProgress(Messages.getString("Dumper.ImportRelations"));
444
445     ObjectInputStream JavaDoc oos = new ObjectInputStream JavaDoc(new BufferedInputStream JavaDoc(inputStream));
446
447     byte[] header = new byte[HEADER.length()];
448
449     oos.readFully(header);
450     if (!new String JavaDoc(header, UTF8).equals(HEADER)) throw new DjenericException(INVALID_FILE_MSG);
451     int modelSize = oos.readInt();
452
453     byte[] modelBytes = new byte[modelSize];
454
455     oos.readFully(modelBytes);
456     String JavaDoc sourceModel = new String JavaDoc(modelBytes, UTF8);
457
458     ImportPersistenceManager srcMgr = new ImportPersistenceManager();
459     srcMgr.setModelManually(sourceModel);
460     srcMgr.loadModel();
461
462     long counter = 0;
463     while (oos.available() > 0)
464     {
465       if (importRecordPass2(oos, srcMgr, newids)) counter++;
466       if (counter % 125 == 0 && counter > 0) _progressMonitor.displayProgress(Messages
467           .getString("Dumper.ImportAssocProgress", String.valueOf(counter)));
468     }
469     _progressMonitor.displayProgress(Messages.getString("Dumper.ImportAssocDone", String.valueOf(counter)));
470     srcMgr.close();
471
472     oos.close();
473     return counter;
474   }
475
476   HashMap JavaDoc _stmts = new HashMap JavaDoc();
477
478   protected void importRecordPass1(ObjectInputStream JavaDoc oos, ImportPersistenceManager srcMgr, HashMap JavaDoc newids)
479       throws IOException JavaDoc, SQLException JavaDoc, DjenericException
480   {
481     int length = oos.readInt();
482     byte[] extentAliasBuffer = new byte[length];
483     oos.readFully(extentAliasBuffer);
484     String JavaDoc extentAlias = new String JavaDoc(extentAliasBuffer, UTF8);
485
486     DjExtent extent = srcMgr.getExtentByInternalCode(extentAlias);
487     DjProperty props[] = extent.getProperties();
488
489     String JavaDoc theStmt = (String JavaDoc) _stmts.get(extent.getInternalCode());
490     if (theStmt == null)
491     {
492       theStmt = buildInsertStmt(extent, props);
493       _stmts.put(extent.getInternalCode(), theStmt);
494     }
495
496     DjProperty idproperty = extent.getIdProperty();
497
498     SqlStatement stmt = getSession().getSqlStatement(theStmt);
499     for (int i = 0; i < props.length; i++)
500     {
501       String JavaDoc colName = props[i].getName();
502
503       int typeCode = props[i].getTypeCode();
504
505       switch (typeCode)
506       {
507         case DjDomain.BIGDECIMAL_TYPE :
508           BigDecimal JavaDoc bd = readBigDecimal(oos);
509           if (bd != null) stmt.setBigDecimal(colName, bd);
510           else stmt.setNull(colName, Types.DECIMAL);
511           break;
512         case DjDomain.BYTE_TYPE :
513           byte[] bytes = readBytes(oos);
514           if (bytes != null) stmt.setBytes(colName, bytes);
515           else stmt.setNull(colName, Types.BINARY);
516           break;
517         case DjDomain.DATE_TYPE :
518           Timestamp JavaDoc date = readDate(oos);
519           if (date != null) stmt.setTimestamp(colName, date);
520           else stmt.setNull(colName, Types.TIMESTAMP);
521           break;
522         case DjDomain.INT_TYPE :
523           Integer JavaDoc ii = readInt(oos);
524           if (ii != null) stmt.setInt(colName, ii.intValue());
525           else stmt.setNull(colName, Types.INTEGER);
526           break;
527         case DjDomain.LONG_TYPE :
528
529           Long JavaDoc ll = readLong(oos);
530           // translate into new ID
531
if (props[i] == idproperty)
532           {
533             stmt.setLong(colName, ((Long JavaDoc) newids.get(ll)).longValue());
534           }
535           else
536           // Leave foreign keys null for now:
537
if (props[i].isMappedToRelation())
538           {
539             stmt.setNull(colName, Types.BIGINT);
540           }
541           else
542           { // ok; just a property: set it then
543
if (ll != null) stmt.setLong(colName, ll.longValue());
544             else stmt.setNull(colName, Types.BIGINT);
545           }
546           break;
547         case DjDomain.STRING_TYPE :
548           String JavaDoc s = readString(oos);
549           if (s != null) stmt.setString(colName, s);
550           else stmt.setNull(colName, Types.VARCHAR);
551           break;
552         default :
553           throw new DjenericException(Messages.getString("global.PropUnsupported", String.valueOf(typeCode)));
554       }
555     }
556     if (!_ignoreList.containsKey(extent.getName())) stmt.executeUpdate();
557   }
558
559   HashMap JavaDoc _updStmts = new HashMap JavaDoc();
560   HashMap JavaDoc _skipExtents = new HashMap JavaDoc();
561
562   protected boolean importRecordPass2(ObjectInputStream JavaDoc oos, ImportPersistenceManager srcMgr, HashMap JavaDoc newids)
563       throws IOException JavaDoc, SQLException JavaDoc, DjenericException
564   {
565     int length = oos.readInt();
566     byte[] extentAliasBuffer = new byte[length];
567     oos.readFully(extentAliasBuffer);
568     String JavaDoc extentAlias = new String JavaDoc(extentAliasBuffer, UTF8);
569
570     DjExtent extent = srcMgr.getExtentByInternalCode(extentAlias);
571     DjProperty props[] = extent.getProperties();
572     DjProperty idproperty = extent.getIdProperty();
573
574     Boolean JavaDoc shouldSkip = (Boolean JavaDoc) _skipExtents.get(extent.getInternalCode());
575     if (shouldSkip == null)
576     {
577       shouldSkip = containsNoRels(props);
578       _skipExtents.put(extent.getInternalCode(), shouldSkip);
579     }
580
581     boolean result = false;
582     if (shouldSkip.booleanValue())
583     {
584       skipRecord(oos, props);
585     }
586     else
587     {
588       String JavaDoc theStmt = (String JavaDoc) _updStmts.get(extent.getInternalCode());
589       if (theStmt == null)
590       {
591         theStmt = buildUpdateStmt(extent, props, idproperty);
592         _updStmts.put(extent.getInternalCode(), theStmt);
593       }
594
595       SqlStatement stmt = getSession().getSqlStatement(theStmt);
596
597       for (int i = 0; i < props.length; i++)
598       {
599         String JavaDoc colName = props[i].getName();
600
601         int typeCode = props[i].getTypeCode();
602
603         switch (typeCode)
604         {
605           case DjDomain.BIGDECIMAL_TYPE :
606             readBigDecimal(oos);
607             break;
608           case DjDomain.BYTE_TYPE :
609             readBytes(oos);
610             break;
611           case DjDomain.DATE_TYPE :
612             readDate(oos);
613             break;
614           case DjDomain.INT_TYPE :
615             readInt(oos);
616             break;
617           case DjDomain.LONG_TYPE :
618
619             Long JavaDoc ll = readLong(oos);
620             // translate into new ID?
621
if (props[i] == idproperty || props[i].isMappedToRelation())
622             {
623               if (ll != null)
624               {
625                 Long JavaDoc lo = (Long JavaDoc) newids.get(ll);
626                 if (lo != null) stmt.setLong(colName, lo.longValue());
627                 else
628                 {
629                   DjLogger.log(props[i].getName() + " set to null");
630                   stmt.setNull(colName, Types.BIGINT);
631                 }
632               }
633               else stmt.setNull(colName, Types.BIGINT);
634             }
635             break;
636           case DjDomain.STRING_TYPE :
637             readString(oos);
638             break;
639           default :
640             throw new DjenericException(Messages.getString("global.PropUnsupported", String.valueOf(typeCode)));
641         }
642       }
643       if (!_ignoreList.containsKey(extent.getName()))
644       {
645         stmt.executeUpdate();
646         result = true;
647       }
648     }
649     return result;
650   }
651
652   protected Boolean JavaDoc containsNoRels(DjProperty[] props)
653   {
654     Boolean JavaDoc shouldSkip;
655     boolean ss = true;
656     for (int i = 0; i < props.length; i++)
657     {
658       if (props[i].isMappedToRelation())
659       {
660         ss = false;
661         break;
662       }
663     }
664     shouldSkip = new Boolean JavaDoc(ss);
665     return shouldSkip;
666   }
667
668   protected DjExtent resolveDestinationExtent(DjExtent srcExtent) throws ObjectNotDefinedException
669   {
670     DjExtent extent = null;
671
672     try
673     {
674       extent = _manager.getExtentByInternalCode(srcExtent.getInternalCode());
675     }
676     catch (ObjectNotDefinedException onde)
677     {
678     }
679
680     try
681     {
682       if (extent == null) extent = _manager.getExtentByObjectType(srcExtent.getObjectType());
683     }
684     catch (ObjectNotDefinedException onde)
685     {
686     }
687
688     if (extent == null) extent = _manager.getExtent(srcExtent.getName());
689
690     return extent;
691   }
692
693   protected String JavaDoc buildInsertStmt(DjExtent srcExtent, DjProperty[] props) throws ObjectNotDefinedException
694   {
695     DjExtent extent = resolveDestinationExtent(srcExtent);
696
697     String JavaDoc theStmt;
698     StringBuffer JavaDoc sqlStmt = new StringBuffer JavaDoc(100);
699     sqlStmt.append("insert into ");
700     sqlStmt.append(extent.getName());
701     sqlStmt.append("(");
702
703     for (int i = 0; i < props.length; i++)
704     {
705       if (i != 0) sqlStmt.append(", ");
706       sqlStmt.append(props[i].getName());
707     }
708     sqlStmt.append(") values (");
709     for (int i = 0; i < props.length; i++)
710     {
711       if (i != 0) sqlStmt.append(", ");
712       sqlStmt.append(":");
713       sqlStmt.append(props[i].getName());
714     }
715     sqlStmt.append(")");
716
717     theStmt = sqlStmt.toString();
718     return theStmt;
719   }
720
721   protected String JavaDoc buildUpdateStmt(DjExtent srcExtent, DjProperty[] props, DjProperty idproperty)
722       throws ObjectNotDefinedException
723   {
724     DjExtent extent = resolveDestinationExtent(srcExtent);
725
726     String JavaDoc theStmt;
727     StringBuffer JavaDoc sqlStmt = new StringBuffer JavaDoc(100);
728     sqlStmt.append("update ");
729     sqlStmt.append(extent.getName());
730     sqlStmt.append(" set ");
731
732     boolean first = true;
733     for (int i = 0; i < props.length; i++)
734     {
735       if (!props[i].isMappedToRelation()) continue;
736
737       if (!first) sqlStmt.append(", ");
738       first = false;
739       sqlStmt.append(props[i].getName() + " = :" + props[i].getName());
740     }
741     sqlStmt.append(" where " + idproperty.getName() + " = :" + idproperty.getName());
742
743     theStmt = sqlStmt.toString();
744     return theStmt;
745   }
746
747   protected void skipRecord(ObjectInputStream JavaDoc oos, DjProperty[] props) throws DjenericException, IOException JavaDoc
748   {
749     for (int i = 0; i < props.length; i++)
750     {
751       int typeCode = props[i].getTypeCode();
752
753       switch (typeCode)
754       {
755         case DjDomain.BIGDECIMAL_TYPE :
756           readBigDecimal(oos);
757           break;
758         case DjDomain.BYTE_TYPE :
759           readBytes(oos);
760           break;
761         case DjDomain.DATE_TYPE :
762           readDate(oos);
763           break;
764         case DjDomain.INT_TYPE :
765           readInt(oos);
766           break;
767         case DjDomain.LONG_TYPE :
768           readLong(oos);
769           break;
770         case DjDomain.STRING_TYPE :
771           readString(oos);
772           break;
773         default :
774           throw new DjenericException(Messages.getString("global.PropUnsupported", String.valueOf(typeCode)));
775       }
776     }
777   }
778
779   protected String JavaDoc readString(ObjectInputStream JavaDoc oos) throws IOException JavaDoc, UnsupportedEncodingException JavaDoc
780   {
781     String JavaDoc result = null;
782
783     boolean wasNull = oos.readBoolean();
784     if (!wasNull)
785     {
786       int xx = oos.readInt();
787       byte[] buffer = new byte[xx];
788       oos.readFully(buffer);
789       result = new String JavaDoc(buffer, CP1252);
790     }
791     return result;
792   }
793
794   protected Long JavaDoc readLong(ObjectInputStream JavaDoc oos) throws IOException JavaDoc
795   {
796     Long JavaDoc result = null;
797
798     boolean wasNull = oos.readBoolean();
799     if (!wasNull)
800     {
801       result = new Long JavaDoc(oos.readLong());
802     }
803     return result;
804   }
805
806   protected Integer JavaDoc readInt(ObjectInputStream JavaDoc oos) throws IOException JavaDoc
807   {
808     Integer JavaDoc result = null;
809
810     boolean wasNull = oos.readBoolean();
811     if (!wasNull)
812     {
813       result = new Integer JavaDoc(oos.readInt());
814     }
815     return result;
816   }
817
818   protected Timestamp JavaDoc readDate(ObjectInputStream JavaDoc oos) throws IOException JavaDoc
819   {
820     Timestamp JavaDoc result = null;
821     boolean wasNull = oos.readBoolean();
822     if (!wasNull)
823     {
824       result = new Timestamp JavaDoc(oos.readLong());
825     }
826     return result;
827   }
828
829   protected byte[] readBytes(ObjectInputStream JavaDoc oos) throws IOException JavaDoc
830   {
831     byte[] result = null;
832     boolean wasNull = oos.readBoolean();
833     if (!wasNull)
834     {
835       int xx = oos.readInt();
836       byte[] buffer = new byte[xx];
837       oos.readFully(buffer);
838       result = buffer;
839     }
840     return result;
841   }
842
843   protected BigDecimal JavaDoc readBigDecimal(ObjectInputStream JavaDoc oos) throws IOException JavaDoc
844   {
845     BigDecimal JavaDoc result = null;
846     boolean wasNull = oos.readBoolean();
847     if (!wasNull)
848     {
849       result = new BigDecimal JavaDoc(oos.readFloat());
850     }
851     return result;
852   }
853 }
Popular Tags