KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > mapper > rdb > generator > RdbMappingMOP


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 package org.objectweb.jorm.mapper.rdb.generator;
25
26 import org.apache.velocity.context.Context;
27 import org.objectweb.jorm.api.PException;
28 import org.objectweb.jorm.generator.api.MOP;
29 import org.objectweb.jorm.generator.lib.CommonGenerator;
30 import org.objectweb.jorm.generator.lib.NamingFilterExpressionHelper;
31 import org.objectweb.jorm.mapper.rdb.lib.RdbExtentGenInfos;
32 import org.objectweb.jorm.metainfo.api.Class;
33 import org.objectweb.jorm.metainfo.api.NameDef;
34 import org.objectweb.jorm.metainfo.api.PrimitiveElementMapping;
35 import org.objectweb.jorm.type.api.PType;
36 import org.objectweb.medor.expression.api.Expression;
37 import org.objectweb.medor.expression.api.ExpressionException;
38 import org.objectweb.medor.expression.api.ParameterOperand;
39 import org.objectweb.util.monolog.api.BasicLevel;
40 import org.objectweb.util.monolog.api.Logger;
41
42 public class RdbMappingMOP extends RdbCommonHelper implements MOP {
43
44     public RdbMappingMOP(Logger logger) {
45         super(logger);
46     }
47
48     public boolean isInPK(PrimitiveElementMapping pem) {
49         return false;
50     }
51
52     // IMPLEMENTATION OF METHODS FROM THE MOP INTERFACE
53

54     public void initContext(Context ctx, Class JavaDoc clazz,
55                             String JavaDoc projectName,
56                             String JavaDoc mapperName) throws PException {
57         ctx.put("rdbTools", this);
58         String JavaDoc mn = (String JavaDoc) ctx.get("mappername");
59         ctx.put("adapter", getRdbAdapter(mn));
60         ctx.put("G", "\"");
61         RdbGenInfos rdbGenInfos = getRdbGenInfos(clazz, projectName, mapperName);
62         ctx.put("genInfos", rdbGenInfos);
63         ctx.put("extentGenInfos", new RdbExtentGenInfos(logger));
64         
65         if (clazz.isPolymorphic()) {
66             NamingFilterExpressionHelper mi = null;
67             try {
68                 //get the namedef : it can belong to a super class
69
NameDef nd = clazz.getClassMapping(projectName, mapperName).getIdentifierMapping().getNameDef();
70                 //get the class in which looking for the filter
71
Class JavaDoc classToLookForExpression = (Class JavaDoc) nd.getParent();
72                 //get the medor expression of the filter
73
Expression exp = classToLookForExpression.getInheritanceFilter(nd);
74                 //compile it to compute the type
75
exp.compileExpression();
76                 //generate the filter evaluate method
77
mi = new PolymorphicFilterExpressionHelper(exp, logger);
78             } catch (ExpressionException e) {
79                 throw new PException(e);
80             }
81             ctx.put("mi", mi);
82         }
83     }
84
85     public String JavaDoc[] getTemplateLibraries() {
86         String JavaDoc[] libs = {
87                 CommonGenerator.MAPPER_TEMPLATE_DIR + "rdb/generator/RdbMapping.vm",
88                 CommonGenerator.MAPPER_TEMPLATE_DIR + "rdb/generator/RdbExtentDefinition.vm"};
89         return libs;
90     }
91     
92     private String JavaDoc getStrAsObject(String JavaDoc str, PType type) {
93         switch (type.getTypeCode()) {
94         case PType.TYPECODE_BYTE:
95             return "new Byte(" + str +")";
96         case PType.TYPECODE_CHAR:
97             return "new Character(" + str +")";
98         case PType.TYPECODE_SHORT:
99             return "new Short(" + str +")";
100         case PType.TYPECODE_INT:
101             return "new Integer(" + str +")";
102         case PType.TYPECODE_LONG:
103             return "new Long(" + str +")";
104         case PType.TYPECODE_OBJBYTE:
105         case PType.TYPECODE_OBJCHAR:
106         case PType.TYPECODE_OBJSHORT:
107         case PType.TYPECODE_OBJINT:
108         case PType.TYPECODE_OBJLONG:
109         case PType.TYPECODE_DATE:
110         case PType.TYPECODE_CHARARRAY:
111         case PType.TYPECODE_BYTEARRAY:
112         case PType.TYPECODE_STRING:
113         case PType.TYPECODE_BIGDECIMAL:
114         case PType.TYPECODE_BIGINTEGER:
115         default:
116             return str;
117         }
118     }
119     
120     private String JavaDoc getTupleType(PType type) {
121         switch (type.getTypeCode()) {
122         case PType.TYPECODE_DATE:
123             return "Date";
124         case PType.TYPECODE_STRING:
125             return "String";
126         case PType.TYPECODE_CHARARRAY:
127             return "CharArray";
128         case PType.TYPECODE_BYTEARRAY:
129             return "ByteArray";
130         case PType.TYPECODE_BIGDECIMAL:
131             return "BigDecimal";
132         case PType.TYPECODE_BIGINTEGER:
133             return "BigInteger";
134         case PType.TYPECODE_BYTE:
135             return "Byte";
136         case PType.TYPECODE_CHAR:
137             return "Char";
138         case PType.TYPECODE_SHORT:
139             return "Short";
140         case PType.TYPECODE_INT:
141             return "Int";
142         case PType.TYPECODE_LONG:
143             return "Long";
144         case PType.TYPECODE_OBJBYTE:
145         case PType.TYPECODE_OBJBOOLEAN:
146         case PType.TYPECODE_OBJDOUBLE:
147         case PType.TYPECODE_OBJFLOAT:
148         case PType.TYPECODE_OBJCHAR:
149         case PType.TYPECODE_OBJSHORT:
150         case PType.TYPECODE_OBJINT:
151         case PType.TYPECODE_OBJLONG:
152         default:
153             return "Object";
154         }
155     }
156     /**
157      * This class is used to compute the filter for classes being part of
158      * an inheritance object graph and not having the filter in the primary key.
159      * @author Y.Bersihand
160      */

161     public class PolymorphicFilterExpressionHelper extends NamingFilterExpressionHelper {
162
163         private PType filterType;
164         public PolymorphicFilterExpressionHelper(Expression e,
165                                                Logger logger)
166             throws PException, ExpressionException {
167             super(logger);
168             this.filterType = e.getType();
169             fillMatchInfo(e);
170         }
171
172         public String JavaDoc getFilter() {
173             return getStrAsObject(super.getFilter(), filterType);
174         }
175
176         /**
177          * Genrates the java code to get the value of a field into a Tuple:
178          * tuple.getXXX(fieldRank);
179          */

180         protected void fillMatchInfo(ParameterOperand po) throws PException, ExpressionException {
181             logger.log(BasicLevel.DEBUG, "ParameterOperand " + po.getName());
182             if (field2declarations.get(po.getName()) == null) {
183                 StringBuffer JavaDoc declaration = new StringBuffer JavaDoc();
184                 declaration.append(po.getType().getJavaName());
185                 declaration.append(' ');
186                 declaration.append(po.getName());
187                 declaration.append(" = ");
188                 declaration.append("tuple.get");
189                 declaration.append(getTupleType(po.getType()));
190                 declaration.append("(fieldRank");
191                 declaration.append(")");
192                 declaration.append(";");
193                 field2declarations.put(po.getName(), declaration);
194                 if (logger.isLoggable(BasicLevel.DEBUG)) {
195                     logger.log(BasicLevel.DEBUG, "Add declaration: " + declaration);
196                 }
197             }
198             filter.append(po.getName());
199         }
200         
201     }
202 }
203
Popular Tags