KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > repository > util > DjObjectExporter


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

29 package com.genimen.djeneric.repository.util;
30
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 import com.genimen.djeneric.language.Messages;
35 import com.genimen.djeneric.repository.DjAssociation;
36 import com.genimen.djeneric.repository.DjDomain;
37 import com.genimen.djeneric.repository.DjExtent;
38 import com.genimen.djeneric.repository.DjList;
39 import com.genimen.djeneric.repository.DjObject;
40 import com.genimen.djeneric.repository.DjProperty;
41 import com.genimen.djeneric.repository.DjRelation;
42 import com.genimen.djeneric.repository.DjUid;
43 import com.genimen.djeneric.repository.exceptions.CatalogException;
44 import com.genimen.djeneric.repository.exceptions.DjenericException;
45 import com.genimen.djeneric.structure.ExtentUsage;
46 import com.genimen.djeneric.structure.RelationUsage;
47 import com.genimen.djeneric.util.DjBase64;
48 import com.genimen.djeneric.util.DjStatusDisplayer;
49
50 public class DjObjectExporter
51 {
52   DjList _exportList = new DjList();
53   DjList _masters = new DjList();
54   HashMap JavaDoc _object2structure = new HashMap JavaDoc();
55   HashMap JavaDoc _structureKeys = new HashMap JavaDoc();
56   DjStatusDisplayer _statusDisplayer = null;
57
58   int _currentKey = 1;
59
60   public DjObjectExporter()
61   {
62   }
63
64   public DjObjectExporter(DjObject source)
65   {
66     markForExport(source);
67   }
68
69   public String JavaDoc export() throws DjenericException
70   {
71     StringBuffer JavaDoc result = new StringBuffer JavaDoc(2000);
72
73     result.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<djenericexport>\n");
74     HashMap JavaDoc uids = new HashMap JavaDoc();
75     HashMap JavaDoc hitlist = new HashMap JavaDoc();
76
77     Iterator JavaDoc it = _exportList.iterator();
78     while (it.hasNext())
79       result.append(doExportSingle((DjObject) it.next(), uids, hitlist));
80
81     result.append(writeUids(uids));
82     result.append(writeStructures(_structureKeys));
83     result.append("</djenericexport>");
84     setStatus(Messages.getString("ObjectExporter.ExportDone", String.valueOf(_exportList.size())));
85     return result.toString();
86   }
87
88   public void markForExport(DjObject source)
89   {
90     if (!_exportList.contains(source))
91     {
92       _exportList.add(source);
93       _masters.add(source);
94     }
95   }
96
97   public void markForExport(DjObject source, ExtentUsage usage) throws DjenericException
98   {
99     _masters.add(source);
100     _object2structure.put(source, usage);
101     if (!_structureKeys.containsKey(usage)) _structureKeys.put(usage, new Integer JavaDoc(_currentKey++));
102     doMarkForExport(source, usage);
103   }
104
105   protected void doMarkForExport(DjObject source, ExtentUsage usage) throws DjenericException
106   {
107     if (!_exportList.contains(source))
108     {
109       _exportList.add(source);
110
111       RelationUsage[] rels = usage.getDetailRelations();
112       for (int i = 0; i < rels.length; i++)
113       {
114         DjRelation rel = rels[i].getRelation();
115         DjExtent detailExtent = rels[i].getDetail().getExtent();
116         DjAssociation srcAssoc = source.getDetailAssociationByName(rel.getName());
117
118         DjList details = srcAssoc.getObjects();
119         for (int d = 0; d < details.size(); d++)
120         {
121           DjObject detailObject = details.getDjenericObjectAt(d);
122           // Accept only types that are of the detail type in the relation usage
123
if (!detailObject.isInstanceOf(detailExtent)) continue;
124
125           doMarkForExport(detailObject, rels[i].getDetail());
126         }
127       }
128     }
129   }
130
131   public void clear()
132   {
133     _exportList = new DjList();
134     _masters = new DjList();
135     _object2structure = new HashMap JavaDoc();
136     _structureKeys = new HashMap JavaDoc();
137     _currentKey = 1;
138   }
139
140   protected String JavaDoc doExportSingle(DjObject source, HashMap JavaDoc uids, HashMap JavaDoc hitlist) throws DjenericException
141   {
142     if (hitlist.containsKey(source)) return "";
143     hitlist.put(source, source);
144
145     StringBuffer JavaDoc result = new StringBuffer JavaDoc(2000);
146
147     result.append(writeObject(source, uids));
148     return result.toString();
149   }
150
151   protected String JavaDoc writeUids(HashMap JavaDoc uids) throws CatalogException
152   {
153     StringBuffer JavaDoc result = new StringBuffer JavaDoc(1000);
154     Iterator JavaDoc it = uids.keySet().iterator();
155     while (it.hasNext())
156     {
157       Object JavaDoc object = it.next();
158       DjUid uid = (DjUid) uids.get(object);
159       if (uid.getPropertyCount() == 0) throw new CatalogException(Messages.getString("DjPersistenceManager.NoUid", uid
160           .getExtent().getName()));
161       result.append(uid.toString(2));
162       result.append("\n");
163     }
164     return result.toString();
165   }
166
167   protected String JavaDoc writeStructures(HashMap JavaDoc structures)
168   {
169     StringBuffer JavaDoc result = new StringBuffer JavaDoc(1000);
170     Iterator JavaDoc it = structures.keySet().iterator();
171     while (it.hasNext())
172     {
173       ExtentUsage usg = (ExtentUsage) it.next();
174       result.append(" <structure extent=\"" + usg.getExtent().getName() + "\" id=\"" + _structureKeys.get(usg)
175                     + "\">\n");
176       result.append(usg.getRelationsAsXml(4));
177       result.append(" </structure>\n");
178     }
179     return result.toString();
180   }
181
182   protected String JavaDoc writeObject(DjObject source, HashMap JavaDoc uids) throws DjenericException
183   {
184     setStatus(Messages
185         .getString("ObjectExporter.ExportExtent", source.getExtent().getNameSingular(), source.toString()));
186     if (source.getExtent().hasUidDefined())
187     {
188       uids.put(source, source.getUID());
189     }
190
191     StringBuffer JavaDoc result = new StringBuffer JavaDoc(1000);
192     result.append(" <object extent=\"" + source.getExtent().getName() + "\" id=\"" + source.getObjectId() + "\"");
193     if (_masters.contains(source))
194     {
195       if (!source.getExtent().hasUidDefined()) throw new CatalogException(Messages
196           .getString("DjPersistenceManager.NoUid", source.getExtent().getName()));
197       result.append(" rootobject=\"true\"");
198     }
199     ExtentUsage usg = (ExtentUsage) _object2structure.get(source);
200     if (usg != null) result.append(" structure=\"" + _structureKeys.get(usg) + "\"");
201
202     result.append(">\n");
203     result.append(writeSimpleProperties(source));
204     result.append(writeFKProperties(source, uids));
205     result.append(writeTextProperties(source));
206     result.append(" </object>\n");
207     return result.toString();
208   }
209
210   protected String JavaDoc writeSimpleProperties(DjObject source) throws DjenericException
211   {
212     StringBuffer JavaDoc result = new StringBuffer JavaDoc(1000);
213     DjExtent extent = source.getExtent();
214
215     for (int i = 0; i < extent.getPropertyCount(); i++)
216     {
217       DjProperty prop = extent.getProperty(i);
218       if (isSimpleProperty(prop))
219       {
220         if (prop == extent.getIdProperty()) continue;
221
222         String JavaDoc value = source.getString(prop.getName());
223         if (value != null) result.append(" <property name=\"" + prop.getName() + "\" type=\"simple\">" + value
224                                          + "</property>\n");
225       }
226     }
227     return result.toString();
228   }
229
230   protected String JavaDoc writeFKProperties(DjObject source, HashMap JavaDoc uids) throws DjenericException
231   {
232     StringBuffer JavaDoc result = new StringBuffer JavaDoc(1000);
233     DjExtent extent = source.getExtent();
234
235     for (int i = 0; i < extent.getPropertyCount(); i++)
236     {
237       DjProperty prop = extent.getProperty(i);
238       if (isFKProperty(prop))
239       {
240         DjObject master = (DjObject) source.get(prop.getName());
241         if (master != null)
242         {
243           DjUid uid = master.getUID();
244           result.append(" <property name=\"" + prop.getName() + "\" type=\"uidptr\">" + uid.getAssociatedObjectid()
245                         + "</property>\n");
246           uids.put(master, uid);
247         }
248       }
249     }
250     return result.toString();
251   }
252
253   protected String JavaDoc writeTextProperties(DjObject source) throws DjenericException
254   {
255     StringBuffer JavaDoc result = new StringBuffer JavaDoc(1000);
256     DjExtent extent = source.getExtent();
257
258     for (int i = 0; i < extent.getPropertyCount(); i++)
259     {
260       DjProperty prop = extent.getProperty(i);
261       if (isTextProperty(prop))
262       {
263         String JavaDoc value = source.getString(prop.getName());
264         if (value != null) result.append(" <property name=\"" + prop.getName() + "\" type=\"text\"><![CDATA["
265                                          + value + "]]></property>\n");
266       }
267     }
268     return result.toString();
269   }
270
271   protected String JavaDoc writeByteProperties(DjObject source, DjExtent extent) throws DjenericException
272   {
273     StringBuffer JavaDoc result = new StringBuffer JavaDoc(1000);
274
275     for (int i = 0; i < extent.getPropertyCount(); i++)
276     {
277       DjProperty prop = extent.getProperty(i);
278       if (isByteProperty(prop))
279       {
280         byte[] bytes = source.getBytes(prop.getName());
281         if (bytes != null)
282         {
283           String JavaDoc value = DjBase64.encode(source.getBytes(prop.getName()));
284           if (value != null) result.append(" <property name=\"" + prop.getName() + "\" type=\"byte\"><![CDATA["
285                                            + value + "]]></property>\n");
286         }
287       }
288     }
289     return result.toString();
290   }
291
292   protected boolean isSimpleProperty(DjProperty prop)
293   {
294     if (prop.getType() instanceof DjDomain)
295     {
296       return prop.getTypeCode() == DjDomain.BIGDECIMAL_TYPE || prop.getTypeCode() == DjDomain.DATE_TYPE
297              || prop.getTypeCode() == DjDomain.INT_TYPE || prop.getTypeCode() == DjDomain.LONG_TYPE;
298     }
299     return false;
300   }
301
302   protected boolean isByteProperty(DjProperty prop)
303   {
304     if (prop.getType() instanceof DjDomain)
305     {
306       return prop.getTypeCode() == DjDomain.BYTE_TYPE;
307     }
308     return false;
309   }
310
311   protected boolean isFKProperty(DjProperty prop)
312   {
313     return prop.getType() instanceof DjExtent;
314   }
315
316   protected boolean isTextProperty(DjProperty prop)
317   {
318     return !isSimpleProperty(prop) && !isFKProperty(prop) && !isByteProperty(prop);
319   }
320
321   protected void setStatus(String JavaDoc msg)
322   {
323     if (_statusDisplayer != null) _statusDisplayer.setStatusMessageNow(msg, true);
324   }
325
326   public DjStatusDisplayer getStatusDisplayer()
327   {
328     return _statusDisplayer;
329   }
330
331   public void setStatusDisplayer(DjStatusDisplayer statusDisplayer)
332   {
333     _statusDisplayer = statusDisplayer;
334   }
335
336 }
Popular Tags