KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > optim > jorm > JormRule


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2003 France Telecom R&D
5  * Contact: alexandre.lefebvre@rd.francetelecom.com
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * Initial developers: M. Alia, S. Chassande-Barrioz, A. Lefebvre
22  */

23
24 package org.objectweb.medor.optim.jorm;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 import org.objectweb.jorm.api.PClassMapping;
30 import org.objectweb.jorm.api.PException;
31 import org.objectweb.jorm.api.PMapper;
32 import org.objectweb.jorm.lib.JormPathHelper;
33 import org.objectweb.jorm.metainfo.api.Class;
34 import org.objectweb.jorm.metainfo.api.ClassMapping;
35 import org.objectweb.jorm.metainfo.api.ClassProject;
36 import org.objectweb.jorm.metainfo.api.GenClassMapping;
37 import org.objectweb.jorm.metainfo.api.GenClassRef;
38 import org.objectweb.jorm.metainfo.api.Mapping;
39 import org.objectweb.jorm.metainfo.api.MetaObject;
40 import org.objectweb.jorm.metainfo.api.ParentClassMapping;
41 import org.objectweb.jorm.metainfo.api.PrimitiveElement;
42 import org.objectweb.jorm.metainfo.api.PrimitiveElementMapping;
43 import org.objectweb.jorm.naming.api.PNameCoder;
44 import org.objectweb.medor.api.MedorException;
45 import org.objectweb.medor.expression.api.Operand;
46 import org.objectweb.medor.expression.lib.BasicOperand;
47 import org.objectweb.medor.optim.lib.BasicRule;
48 import org.objectweb.medor.query.jorm.api.JormExtent;
49 import org.objectweb.medor.query.jorm.lib.ClassExtent;
50 import org.objectweb.medor.query.jorm.lib.GenClassExtent;
51 import org.objectweb.medor.query.jorm.lib.PNameField;
52 import org.objectweb.medor.type.lib.PTypeSpaceMedor;
53 import org.objectweb.util.monolog.api.BasicLevel;
54
55 /**
56  * This class is a tool for the management of the JormExtent.
57  * <p>It is used for the
58  * replacement of JormExtent instances into specific QueryLeaf instances.
59  *
60  * @author S.Chassande-Barrioz
61  */

62 public abstract class JormRule extends BasicRule {
63
64     public JormRule(String JavaDoc suffix) {
65         super(suffix);
66     }
67
68
69     /**
70      * Retrieves the primitive element corresponding to a field name of
71      * a jorm object (class or generic class).
72      * @param extent represents the Jorm persistent class and references the
73      * Jorm meta object containing the expected jorm meta object.
74      * @param fieldname is the name of the
75      * @throws MedorException if the field does not exist.
76      */

77     protected PrimitiveElement getPrimitiveElement(JormExtent extent,
78                                                    String JavaDoc fieldname)
79         throws MedorException {
80         PrimitiveElement te = null;
81         if (extent.getMetaObject() instanceof Class JavaDoc) {
82             Class JavaDoc clazz = (Class JavaDoc) extent.getMetaObject();
83             te = (PrimitiveElement) clazz.getTypedElement(fieldname);
84             if (te == null) {
85                 te = clazz.getHiddenField(fieldname);
86             }
87             if (te == null) {
88                 String JavaDoc msg = "No field " + fieldname
89                     + " available on the class " + clazz.getFQName();
90                 log.log(BasicLevel.ERROR, msg);
91                 throw new MedorException(msg);
92             }
93         }
94         else if (extent.getMetaObject() instanceof GenClassRef) {
95             GenClassRef gcr = (GenClassRef) extent.getMetaObject();
96             te = gcr.getHiddenField(fieldname);
97             if (te == null) {
98                 String JavaDoc msg = "No field " + fieldname
99                     + " available on the generic class "
100                     + gcr.getGenClassId();
101                 log.log(BasicLevel.ERROR, msg);
102                 throw new MedorException(msg);
103             }
104         }
105         else {
106             throw new MedorException("unmanaged jorm extent: " + extent);
107         }
108         return te;
109     }
110
111     /**
112      * Retrieves the RdbPrimitiveElementMapping of a field
113      * @param extent the extent of a persistent class (ClassExtent
114      * or GenClassExtent)
115      * @param fieldName is the name of the field which the mapping is expected
116      * @throws MedorException if the extent is not managed or if the field does
117      * not have a mapping.
118      */

119     protected PrimitiveElementMapping getPEM(JormExtent extent,
120                                               String JavaDoc fieldName)
121         throws MedorException {
122         PrimitiveElementMapping res = null;
123         String JavaDoc pn = extent.getProjectName();
124         String JavaDoc mn = extent.getPMapper().getMapperName();
125         if (extent instanceof ClassExtent) {
126             Class JavaDoc clazz = (Class JavaDoc) extent.getMetaObject();
127             ClassMapping cm = getClassMapping(extent);
128             String JavaDoc jormFieldName = getJORMFieldName(fieldName, extent);
129             res = cm.getPrimitiveElementMapping(jormFieldName);
130             if (res == null) {
131                 
132                 //parents
133
ArrayList JavaDoc pcms = new ArrayList JavaDoc(cm.getParentClassMappings());
134                 while (!pcms.isEmpty() && res == null) {
135                     ParentClassMapping pcm = (ParentClassMapping) pcms.remove(0);
136                     ClassMapping _cm = pcm.getMOClass()
137                         .getClassProject(pcm.getProjectName())
138                         .getMapping(pcm.getMapperName()).getClassMapping();
139                     res = _cm.getPrimitiveElementMapping(jormFieldName);
140                     if (res == null) {
141                         pcms.addAll(_cm.getParentClassMappings());
142                     }
143                 }
144                 //if null for parents, try children
145
if (res == null) {
146                     //children
147
//get the pcm of the super class
148
PClassMapping pcm = extent.getPMapper().lookup(clazz.getFQName());
149                     try {
150                         //get the sub PClassMappings
151
PClassMapping[] childrenPCMs = pcm.getSubPCMs();
152                         if (childrenPCMs != null) {
153                             //for each child
154
for(int j = 0; j < childrenPCMs.length; j++){
155                                 // get the class mapping
156
Class JavaDoc subclass = extent.getPMapper().getMetaInfoManager().getClass(
157                                         childrenPCMs[j].getClassName());
158                                 if (subclass == null) {
159                                     throw new MedorException("Impossible to retrieve the jorm class " + childrenPCMs[j].getClassName());
160                                 }
161                                 ClassMapping subCM = subclass.getClassMapping(
162                                         extent.getProjectName(),getMapperName(extent.getPMapper().getMapperName()));
163                                 if (subCM != null) {
164                                     res = subCM.getPrimitiveElementMapping(jormFieldName);
165                                     if (res != null) {
166                                         return res;
167                                     }
168                                 }
169                             }
170                         }
171                     } catch(PException e) {
172                         throw new MedorException("Problem occurred while computing list of pems for sub classes.", e);
173                     }
174                 }
175             }
176             if (res == null) {
177                 String JavaDoc msg = "No mapping for field '" + getJORMFieldName(fieldName, extent)
178                     + "' of the class '" + clazz.getFQName()
179                     + "' in the project '" + pn
180                     + "' and the mapping '" + mn;
181                 log.log(BasicLevel.ERROR, msg);
182                 log.log(BasicLevel.ERROR, "Existing field mappings:");
183                 for(Iterator JavaDoc it= cm.getAllPrimitiveElementMappings().iterator(); it.hasNext();) {
184                     res = (PrimitiveElementMapping) it.next();
185                     log.log(BasicLevel.ERROR, "Field: "
186                         + ((PrimitiveElement) res.getLinkedMO()).getName());
187                 }
188                 throw new MedorException(msg);
189             }
190             return res;
191         }
192         else if (extent instanceof GenClassExtent) {
193             GenClassRef gcr = (GenClassRef) extent.getMetaObject();
194             GenClassMapping gcm = getGenClassMapping(extent);
195             res = gcm.getPrimitiveElementMapping(
196                     getJORMFieldName(fieldName, extent));
197             if (res == null) {
198                 String JavaDoc msg = "No mapping for field '" + getJORMFieldName(fieldName, extent)
199                     + "' of the generic class '" + gcr.getGenClassId()
200                     + "' in the project '" + pn
201                     + " and the mapping '" + mn;
202                 log.log(BasicLevel.ERROR, msg);
203                 log.log(BasicLevel.ERROR, "Existing field mappings:");
204                 for(Iterator JavaDoc it= gcm.getPrimitiveElementMappings().iterator(); it.hasNext();) {
205                     res = (PrimitiveElementMapping) it.next();
206                     log.log(BasicLevel.ERROR, "Field: "
207                         + ((PrimitiveElement) res.getLinkedMO()).getName());
208                 }
209                 throw new MedorException(msg);
210             }
211             return res;
212         }
213         else {
214             throw new MedorException("Unmanaged JormExtent: " + extent);
215         }
216     }
217
218     /**
219      * Returns the field name without the extent name
220      * @param fullName the full field name containing the extent name
221      * @param ext the extent
222       * @return the abridged field name
223      */

224     protected String JavaDoc getJORMFieldName(String JavaDoc fullName, JormExtent ext) {
225         int idx = fullName.indexOf(ext.getName() + '.');
226         if (idx == -1)
227             return fullName;
228         else
229             return fullName.substring(ext.getName().length() + 1);
230     }
231
232     /**
233      * Retrieves the jorm meta object describing the mapping of the persistent
234      * object representing by its extent. This method must be call with a
235      * ClassExtent instance. It retrieves a RdbClassMultiMapping instance.
236      * The JormExtent instance must contain the project name and the mapper
237      * instance.
238      * @param extent must be a ClassExtent instance.
239      * @return a RdbClassMultiMapping instance
240      * @throws org.objectweb.medor.api.MedorException if no class mapping has been found for the project
241      * name and the mapper specified by the JormExtent.
242      */

243     protected ClassMapping getClassMapping(JormExtent extent)
244         throws MedorException {
245         String JavaDoc pn = extent.getProjectName();
246         PMapper mapper = extent.getPMapper();
247         if (mapper == null) {
248             String JavaDoc msg = "No mapper has not been assigned to the JormExtent (jorm name: "
249                 + extent.getJormName()
250                 + ", node name: " + extent.getName() + ")";
251             log.log(BasicLevel.ERROR, msg);
252             throw new MedorException(msg);
253         }
254         String JavaDoc mn = mapper.getMapperName();
255         Class JavaDoc clazz = (Class JavaDoc) extent.getMetaObject();
256         ClassProject cp = clazz.getClassProject(pn);
257         if (cp == null) {
258             String JavaDoc msg = "No project " + pn + " for the class "
259                 + clazz.getFQName();
260             log.log(BasicLevel.ERROR, msg);
261             throw new MedorException(msg);
262         }
263         int idx = mn.indexOf('.');
264         if (idx != -1) {
265             mn = mn.substring(0, idx);
266         }
267         Mapping m = cp.getMapping(mn);
268         if (m == null) {
269             String JavaDoc msg = "No mapping '" + mn
270                 + "' in the project '" + pn
271                 + "' of the class " + clazz.getFQName();
272             log.log(BasicLevel.ERROR, msg);
273             throw new MedorException(msg);
274         }
275         return m.getClassMapping();
276     }
277
278     /**
279      * Retrieves the jorm meta object describing the mapping of the persistent
280      * object representing by its extent. This method must be call with a
281      * GenClassExtent instance. It retrieves a RdbGenClassMapping instance.
282      * The JormExtent instance must contain the project name and the mapper
283      * instance.
284      * @param extent must be a ClassExtent instance.
285      * @return a RdbGenClassMapping instance
286      * @throws org.objectweb.medor.api.MedorException if no class mapping has been found for the project
287      * name and the mapper specified by the JormExtent.
288      */

289     protected GenClassMapping getGenClassMapping(JormExtent extent)
290         throws MedorException {
291         String JavaDoc pn = extent.getProjectName();
292         String JavaDoc mn = extent.getPMapper().getMapperName();
293         GenClassRef gcr = (GenClassRef) extent.getMetaObject();
294         MetaObject parentclazz = gcr.getParent();
295         while (!(parentclazz instanceof Class JavaDoc)) {
296             parentclazz = parentclazz.getParent();
297         }
298         ClassProject cp = ((Class JavaDoc) parentclazz).getClassProject(pn);
299         if (cp == null) {
300             String JavaDoc msg = "No project " + pn + " for the generic class "
301                 + gcr.getGenClassId();
302             log.log(BasicLevel.ERROR, msg);
303             throw new MedorException(msg);
304         }
305         int idx = mn.indexOf('.');
306         if (idx != -1) {
307             mn = mn.substring(0, idx);
308         }
309         Mapping m = cp.getMapping(mn);
310         if (m == null) {
311             String JavaDoc msg = "No mapping '" + mn
312                 + "' in the project '" + pn
313                 + "' of the generic class " + gcr.getGenClassId();
314             log.log(BasicLevel.ERROR, msg);
315             throw new MedorException(msg);
316         }
317         return m.getGenClassMapping(gcr.getGenClassId());
318     }
319
320     /**
321      * Retrieves the Operand representing the PNameCoder needed to decode a
322      * pname. If the mapper is assigned to the extent and the persistent class
323      * is initialized then a constant operand is returned. Otherwise a
324      * ParameterOperand is returned.
325      * @param extent can contain the mapper
326      * @param pnf is the field representing the PName to be decoded
327      * @return a Operand instance.
328      */

329     protected Operand getPNCOperand(JormExtent extent, PNameField pnf) {
330         PNameCoder pnc = JormPathHelper.getPNameCoder(
331             pnf.getPNamingContextParameter(), extent.getPMapper());
332         log.log(BasicLevel.DEBUG, "getPNCOperand for pnf " + pnf);
333
334         if (pnc == null) {
335             return new org.objectweb.medor.expression.lib.BasicParameterOperand(
336                 PTypeSpaceMedor.NAMING_CONTEXT,
337                 pnf.getPNamingContextParameter());
338         } else {
339             return new BasicOperand(pnc, PTypeSpaceMedor.NAMING_CONTEXT);
340         }
341     }
342     
343     private static String JavaDoc getMapperName(String JavaDoc mn) {
344         if (mn == null) {
345             return null;
346         }
347         int idx = mn.indexOf('.');
348         if (idx == -1) {
349             return mn;
350         } else {
351             return mn.substring(0, idx);
352         }
353     }
354 }
355
Popular Tags