KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ManyManyBi


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contact: jorm-team@objectweb.org
21  *
22  */

23
24 import org.objectweb.util.monolog.api.BasicLevel;
25 import org.objectweb.jorm.api.PException;
26 import org.objectweb.jorm.metainfo.api.Class;
27 import org.objectweb.jorm.metainfo.api.PrimitiveElement;
28 import org.objectweb.jorm.metainfo.api.NameDef;
29 import org.objectweb.jorm.metainfo.api.GenClassRef;
30 import org.objectweb.jorm.metainfo.api.ScalarField;
31 import org.objectweb.jorm.metainfo.api.ClassRef;
32 import org.objectweb.jorm.metainfo.api.ClassProject;
33 import org.objectweb.jorm.metainfo.rdb.api.RdbMapping;
34 import org.objectweb.jorm.metainfo.rdb.api.RdbClassMapping;
35 import org.objectweb.jorm.metainfo.rdb.api.RdbTable;
36 import org.objectweb.jorm.metainfo.rdb.api.RdbGenClassMapping;
37 import org.objectweb.jorm.metainfo.rdb.api.RdbClassMultiMapping;
38 import org.objectweb.jorm.type.api.PTypeSpace;
39
40 /**
41  *
42  * @author S.Chassande-Barrioz
43  */

44 public class ManyManyBi extends JormcRunner {
45
46     public final static String JavaDoc PROJECT_NAME = "jormExamples";
47     public final static String JavaDoc A_CLASS_NAME = "org.objectweb.jorm.examples.mmb.A";
48     public final static String JavaDoc B_CLASS_NAME = "org.objectweb.jorm.examples.mmb.B";
49
50
51     public static void main(String JavaDoc[] args) {
52         try {
53             ManyManyBi mmb = new ManyManyBi(args.length == 0 ? "./" : args[0]);
54             logger.log(BasicLevel.INFO, "Example ManyManyBi");
55             mmb.createMIofA();
56             mmb.createMIofB();
57             mmb.generate();
58         } catch (Exception JavaDoc e) {
59             logger.log(BasicLevel.ERROR, "", getInner(e));
60         }
61     }
62     public ManyManyBi(String JavaDoc output) throws PException {
63         super(PROJECT_NAME, output);
64     }
65
66     public void createMIofA() throws PException {
67         logger.log(BasicLevel.INFO, "Create the meta information of the A class");
68         Class JavaDoc aclass = compiler.getMIManager().createClass(A_CLASS_NAME);
69
70         PrimitiveElement ida =
71                 aclass.createPrimitiveElement("ida", PTypeSpace.LONG);
72         PrimitiveElement f1 =
73                         aclass.createPrimitiveElement("f1", PTypeSpace.STRING);
74
75         // class identifier
76
NameDef aNameDef = aclass.createNameDef();
77         aNameDef.setFieldName(ida.getName());
78
79         // mulitvalued reference to B
80
GenClassRef bgcr = aclass.createGenClassRef("myB", "Set");
81         //How the gen class is referenced
82
NameDef myBNameDef = bgcr.createRefNameDef();
83         myBNameDef.setFieldName(ida.getName());
84
85         //how the gen clas is identified
86
ScalarField fka = bgcr.createHiddenField("fka", PTypeSpace.LONG);
87         NameDef gcrIdNd = bgcr.createIdNameDef();
88         gcrIdNd.setFieldName(fka.getName());
89
90         //element of the gen class
91
Class JavaDoc bclass = compiler.getMIManager().createClass(B_CLASS_NAME);
92         ClassRef elemCr = bgcr.createClassRef(bclass);
93         ScalarField fkb = bgcr.createHiddenField("fkb", PTypeSpace.LONG);
94         NameDef gcrElemNd = elemCr.createRefNameDef();
95         gcrElemNd.setFieldName(fkb.getName());
96
97
98         // ----------------- Mapping for A -----------------
99
ClassProject acp = aclass.createClassProject(PROJECT_NAME);
100         RdbMapping am = (RdbMapping) acp.createMapping("rdb");
101
102         RdbClassMapping acm = am.createClassMapping("to-table");
103         RdbTable tableA = acm.createRdbTable("table_A");
104         //Identifier of the A class
105
acm.createIdentifierMapping(aNameDef);
106
107         // Mapping rule of the reference myB
108
acm.createReferenceMapping("embedded-target-reference", myBNameDef);
109
110         // Mapping of the ida field over the pka_col
111
tableA.createPrimitiveElementMapping(ida, "pka_col", null, true);
112         // Mapping of the f1 field over the f1_col
113
tableA.createPrimitiveElementMapping(f1, "f1_col", null, true);
114
115
116         RdbGenClassMapping gccm =
117                 am.createGenClassMapping("embedded-target-references", bgcr);
118         RdbTable tableGC = gccm.createRdbTable("table_JOIN");
119
120         tableGC.createPrimitiveElementMapping(fka, "fka_col", null, true);
121         tableGC.createPrimitiveElementMapping(fkb, "fkb_col", null, true);
122         gccm.createIdentifierMapping(gcrIdNd);
123         gccm.createReferenceMapping("embedded-target-reference", gcrElemNd);
124     }
125
126     public void createMIofB() throws PException {
127         logger.log(BasicLevel.INFO, "Create the meta information of the B class");
128         Class JavaDoc bclass = compiler.getMIManager().createClass(B_CLASS_NAME);
129         PrimitiveElement idb =
130                 bclass.createPrimitiveElement("idb", PTypeSpace.LONG);
131         PrimitiveElement f2 =
132                 bclass.createPrimitiveElement("f2", PTypeSpace.STRING);
133
134         // pk simple
135
NameDef bNameDef = bclass.createNameDef();
136         bNameDef.setFieldName(idb.getName());
137
138         // mulitvalued reference to A
139
GenClassRef agcr = bclass.createGenClassRef("myA", "Set");
140         //How the gen class is referenced
141
NameDef myANameDef = agcr.createRefNameDef();
142         myANameDef.setFieldName(idb.getName());
143
144         //how the gen clas is identified
145
ScalarField fkb = agcr.createHiddenField("fkb", PTypeSpace.LONG);
146         NameDef gcrIdNd = agcr.createIdNameDef();
147         gcrIdNd.setFieldName(fkb.getName());
148
149         //element of the gen class
150
Class JavaDoc aclass = compiler.getMIManager().createClass(A_CLASS_NAME);
151         ClassRef elemCr = agcr.createClassRef(aclass);
152         ScalarField fka = agcr.createHiddenField("fka", PTypeSpace.LONG);
153         NameDef gcrElemNd = elemCr.createRefNameDef();
154         gcrElemNd.setFieldName("fka");
155
156         // ----------------- Mapping for B -----------------
157

158         ClassProject bcp = bclass.createClassProject(PROJECT_NAME);
159         RdbMapping bm = (RdbMapping) bcp.createMapping("rdb");
160
161         RdbClassMultiMapping bcm = bm.createClassMultiMapping("to-table");
162         RdbTable tableB = bcm.createRdbTable("table_B");
163         bcm.createIdentifierMapping(bNameDef);
164
165         bcm.createReferenceMapping("embedded-target-reference", myANameDef);
166
167         tableB.createPrimitiveElementMapping(idb, "pkb_col", null, true);
168         tableB.createPrimitiveElementMapping(f2, "f2_col", null, true);
169
170         RdbGenClassMapping gccm =
171                 bm.createGenClassMapping("embedded-target-references", agcr);
172         RdbTable tableGC = gccm.createRdbTable("table_JOIN");
173
174         tableGC.createPrimitiveElementMapping(fkb, "fkb_col", null, true);
175         tableGC.createPrimitiveElementMapping(fka, "fka_col", null, true);
176         gccm.createIdentifierMapping(gcrIdNd);
177         gccm.createReferenceMapping("embedded-target-reference", gcrElemNd);
178     }
179
180 }
181
Popular Tags