1 23 24 import org.objectweb.jorm.api.PException; 25 import org.objectweb.jorm.metainfo.api.Class; 26 import org.objectweb.jorm.metainfo.api.ClassProject; 27 import org.objectweb.jorm.metainfo.api.ClassRef; 28 import org.objectweb.jorm.metainfo.api.CompositeName; 29 import org.objectweb.jorm.metainfo.api.NameDef; 30 import org.objectweb.jorm.metainfo.api.NameRef; 31 import org.objectweb.jorm.metainfo.api.PrimitiveElement; 32 import org.objectweb.jorm.metainfo.api.ScalarField; 33 import org.objectweb.jorm.metainfo.rdb.api.RdbClassMapping; 34 import org.objectweb.jorm.metainfo.rdb.api.RdbClassMultiMapping; 35 import org.objectweb.jorm.metainfo.rdb.api.RdbExternalTable; 36 import org.objectweb.jorm.metainfo.rdb.api.RdbJoin; 37 import org.objectweb.jorm.metainfo.rdb.api.RdbMapping; 38 import org.objectweb.jorm.metainfo.rdb.api.RdbTable; 39 import org.objectweb.jorm.type.api.PTypeSpace; 40 import org.objectweb.util.monolog.api.BasicLevel; 41 42 54 public class OneOneBiCPK extends JormcRunner { 55 56 public final static String PROJECT_NAME = "jormExamples"; 57 public final static String A_CLASS_NAME = "org.objectweb.jorm.examples.oobcpk.A"; 58 public final static String A_PK_COMPOSITE_NAME = "org.objectweb.jorm.examples.oobcpk.APk"; 59 public final static String B_CLASS_NAME = "org.objectweb.jorm.examples.oobcpk.B"; 60 public final static String B_PK_COMPOSITE_NAME = "org.objectweb.jorm.examples.oobcpk.BPk"; 61 62 63 public static void main(String [] args) { 64 try { 65 OneOneBiCPK oobcpk = new OneOneBiCPK(args.length == 0 ? "./" : args[0]); 66 logger.log(BasicLevel.INFO, "Example OneOneBiCPK"); 67 oobcpk.createMIofA(); 68 oobcpk.createMIofB(); 69 oobcpk.generate(); 70 } catch (Exception e) { 71 logger.log(BasicLevel.ERROR, "", getInner(e)); 72 } 73 } 74 public OneOneBiCPK(String output) throws PException { 75 super(PROJECT_NAME, output); 76 } 77 78 public void createMIofA() throws PException { 79 logger.log(BasicLevel.INFO, "Create the meta information of the A class"); 80 Class aclass = compiler.getMIManager().createClass(A_CLASS_NAME); 81 82 PrimitiveElement ida1 = 83 aclass.createPrimitiveElement("ida1", PTypeSpace.STRING); 84 PrimitiveElement ida2 = 85 aclass.createPrimitiveElement("ida2", PTypeSpace.LONG); 86 PrimitiveElement f1 = 87 aclass.createPrimitiveElement("f1", PTypeSpace.STRING); 88 89 91 NameDef aNameDef = aclass.createNameDef(); 92 NameRef nr = aNameDef.createNameRef(getACompositeName()); 93 nr.addProjection("foo1", ida1.getName()); 94 nr.addProjection("foo2", ida2.getName()); 95 96 ScalarField idb1 = aclass.createHiddenField("idb1", PTypeSpace.BYTE); 98 ScalarField idb2 = aclass.createHiddenField("idb2", PTypeSpace.INT); 99 Class bclass = compiler.getMIManager().createClass(B_CLASS_NAME); 100 ClassRef bcr = aclass.createClassRef("myB", bclass); 101 NameDef myBNameDef = bcr.createRefNameDef(); 102 NameRef myBnr = myBNameDef.createNameRef(getBCompositeName()); 103 myBnr.addProjection("bar1", idb1.getName()); 104 myBnr.addProjection("bar2", idb2.getName()); 105 106 ClassProject acp = aclass.createClassProject(PROJECT_NAME); 108 RdbMapping am = (RdbMapping) acp.createMapping("rdb"); 109 110 RdbClassMapping acm = am.createClassMapping("to-table"); 111 RdbTable tableA = acm.createRdbTable("table_A"); 112 acm.createIdentifierMapping(aNameDef); 114 115 acm.createReferenceMapping("embedded-target-reference", myBNameDef); 117 118 tableA.createPrimitiveElementMapping(ida1, "pka1_col", null, true); 120 tableA.createPrimitiveElementMapping(ida2, "pka2_col", null, true); 121 tableA.createPrimitiveElementMapping(f1, "f1_col", null, true); 123 tableA.createPrimitiveElementMapping(idb1, "fkb1_col", null, false); 125 tableA.createPrimitiveElementMapping(idb2, "fkb2_col", null, false); 126 } 127 128 public void createMIofB() throws PException { 129 logger.log(BasicLevel.INFO, "Create the meta information of the B class"); 130 Class bclass = compiler.getMIManager().createClass(B_CLASS_NAME); 131 PrimitiveElement idb1 = 132 bclass.createPrimitiveElement("idb1", PTypeSpace.BYTE); 133 PrimitiveElement idb2 = 134 bclass.createPrimitiveElement("idb2", PTypeSpace.INT); 135 PrimitiveElement f2 = 136 bclass.createPrimitiveElement("f2", PTypeSpace.STRING); 137 138 NameDef bNameDef = bclass.createNameDef(); 140 NameRef nr = bNameDef.createNameRef(getBCompositeName()); 141 nr.addProjection("bar1", idb1.getName()); 142 nr.addProjection("bar2", idb2.getName()); 143 144 ScalarField ida1 = bclass.createHiddenField("ida1", PTypeSpace.STRING); 146 ScalarField ida2 = bclass.createHiddenField("ida2", PTypeSpace.LONG); 147 Class aclass = compiler.getMIManager().createClass(A_CLASS_NAME); 148 ClassRef acr = bclass.createClassRef("myA", aclass); 149 NameDef myANameDef = acr.createRefNameDef(); 150 NameRef myAnr = myANameDef.createNameRef(getACompositeName()); 151 myAnr.addProjection("foo1", ida1.getName()); 152 myAnr.addProjection("foo2", ida2.getName()); 153 154 155 157 ClassProject bcp = bclass.createClassProject(PROJECT_NAME); 158 RdbMapping bm = (RdbMapping) bcp.createMapping("rdb"); 159 160 RdbClassMultiMapping bcm = bm.createClassMultiMapping("to-table"); 161 RdbTable tableB = bcm.createRdbTable("table_B"); 162 bcm.createIdentifierMapping(bNameDef); 163 164 bcm.createReferenceMapping("multi-table", myANameDef); 165 166 tableB.createPrimitiveElementMapping(idb1, "pkb1_col", null, true); 167 tableB.createPrimitiveElementMapping(idb2, "pkb2_col", null, true); 168 tableB.createPrimitiveElementMapping(f2, "f2_col", null, true); 169 170 RdbExternalTable extTableA = bcm.createRdbExternalTable("table_A"); 172 extTableA.setReadOnly(true); extTableA.createPrimitiveElementMapping(ida1, "pka1_col"); 174 extTableA.createPrimitiveElementMapping(ida2, "pka2_col"); 175 RdbJoin joinToA = extTableA.createRdbJoin("myA_Join"); 176 joinToA.addJoinColumnNames("pkb1_col", "fkb1_col"); 177 joinToA.addJoinColumnNames("pkb2_col", "fkb2_col"); 178 } 179 180 public CompositeName getACompositeName() { 181 CompositeName acn = compiler.getMIManager() 182 .getCompositeName(A_PK_COMPOSITE_NAME); 183 if (acn == null) { 184 acn = compiler.getMIManager() 185 .createCompositeName(A_PK_COMPOSITE_NAME); 186 acn.createCompositeNameField("foo1", PTypeSpace.STRING); 187 acn.createCompositeNameField("foo2", PTypeSpace.LONG); 188 } 189 return acn; 190 } 191 public CompositeName getBCompositeName() { 192 CompositeName bcn = compiler.getMIManager() 193 .getCompositeName(B_PK_COMPOSITE_NAME); 194 if (bcn == null) { 195 bcn = compiler.getMIManager() 196 .createCompositeName(B_PK_COMPOSITE_NAME); 197 bcn.createCompositeNameField("bar1", PTypeSpace.BYTE); 198 bcn.createCompositeNameField("bar2", PTypeSpace.INT); 199 } 200 return bcn; 201 } 202 203 } 204 | Popular Tags |