KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Iterator JavaDoc;
34
35 import com.genimen.djeneric.repository.DjAssociation;
36 import com.genimen.djeneric.repository.DjExtent;
37 import com.genimen.djeneric.repository.DjList;
38 import com.genimen.djeneric.repository.DjObject;
39 import com.genimen.djeneric.repository.DjProperty;
40 import com.genimen.djeneric.repository.DjRelation;
41 import com.genimen.djeneric.repository.exceptions.DjenericException;
42 import com.genimen.djeneric.structure.ExtentUsage;
43 import com.genimen.djeneric.structure.RelationUsage;
44
45 public class DjObjectCloner
46 {
47
48   public DjObject clone(DjObject source, ExtentUsage usage) throws DjenericException
49   {
50     HashMap JavaDoc source2dest = new HashMap JavaDoc();
51     ArrayList JavaDoc createdList = new ArrayList JavaDoc();
52     DjObject result = doClone(source, usage, source2dest, createdList);
53
54     // Now the whole structure is copied; details in place.
55
// Al we have to do now is to update references from objects within the source set
56
// to other objects within the source set
57
// (ignoring references FROm within the source set TO outside the set)
58

59     Iterator JavaDoc it = createdList.iterator();
60     while (it.hasNext())
61     {
62       DjObject created = (DjObject) it.next();
63       DjProperty props[] = created.getExtent().getProperties();
64       for (int i = 0; i < props.length; i++)
65       {
66         if (props[i].getType() instanceof DjExtent)
67         {
68           Long JavaDoc newId = (Long JavaDoc) source2dest.get(new Long JavaDoc(created.getLong(props[i].getName())));
69           if (newId != null)
70           {
71             created.setLong(props[i].getName(), newId);
72           }
73         }
74       }
75     }
76
77     return result;
78   }
79
80   protected DjObject doClone(DjObject source, ExtentUsage usage, HashMap JavaDoc idMap, ArrayList JavaDoc created)
81       throws DjenericException
82   {
83     DjObject result = source.shallowCopy();
84     created.add(result);
85
86     idMap.put(new Long JavaDoc(source.getObjectId()), new Long JavaDoc(result.getObjectId()));
87
88     RelationUsage[] rels = usage.getDetailRelations();
89     for (int i = 0; i < rels.length; i++)
90     {
91       DjRelation rel = rels[i].getRelation();
92       DjExtent detailExtent = rels[i].getDetail().getExtent();
93       DjAssociation srcAssoc = source.getDetailAssociationByName(rel.getName());
94       DjAssociation destAssoc = result.getDetailAssociationByName(rel.getName());
95
96       DjList details = srcAssoc.getObjects();
97       for (int d = 0; d < details.size(); d++)
98       {
99         DjObject detailObject = details.getDjenericObjectAt(d);
100         // Accept only types that are of the detail type in the relation usage
101
if (!detailObject.isInstanceOf(detailExtent)) continue;
102         destAssoc.add(doClone(detailObject, rels[i].getDetail(), idMap, created));
103       }
104     }
105
106     return result;
107   }
108
109 }
Popular Tags