KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > api > persistence > mapping > ejb > SunCmpMappingsUtils


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.jdo.api.persistence.mapping.ejb;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.ResourceBundle JavaDoc;
29
30 import org.netbeans.modules.schema2beans.BaseBean;
31 import org.netbeans.modules.schema2beans.GraphManager;
32 import org.netbeans.modules.schema2beans.BaseProperty;
33
34 import com.sun.jdo.api.persistence.mapping.ejb.beans.*;
35
36
37 /** Finder utilities for elements of the SunCmpMappings
38  * object graph.
39  * @author vkraemer
40  */

41 public class SunCmpMappingsUtils {
42     private static ResourceBundle JavaDoc bundle =
43         ResourceBundle.getBundle("com.sun.jdo.api.persistence.mapping.ejb.Bundle"); //NOI18N
44

45     
46     /** Creates a new instance of SunCmpMappingsUtils */
47     private SunCmpMappingsUtils() {
48     }
49     
50     /** Return the first SunCmpMapping element from the graph.
51      * @param scms The root of a SunCmpMappings object graph
52      * @param addEmpty flag to add an empty version of the SunCmpMapping element to the graph if one is
53      * not found.
54      * @return The first SunCmpMapping element in the graph or null if it doesn't exists and
55      * addEmpty is false.
56      */

57     public static SunCmpMapping getFirstSunCmpMapping(SunCmpMappings scms, boolean addEmpty) {
58         SunCmpMapping retVal = null;
59         if (0 < scms.sizeSunCmpMapping())
60             retVal = scms.getSunCmpMapping(0);
61         if ((null == retVal) && addEmpty) {
62             retVal = new SunCmpMapping();
63             scms.addSunCmpMapping(retVal);
64         }
65         return retVal;
66     }
67         
68     
69     /** Find the EntityMapping element that correspond to a bean.
70      * @return the EntityMapping element, or null if addEmpty is false and the EntityMapping is not
71      * found.
72      * @param scms the root of the SunCmpMappings graph
73      * @param bname The value of the ejb-name element for a bean in the graph
74      * @param addEmpty flag to add an empty version of the EntityMapping element to the graph if one is
75      * not found.
76      * @throws IllegalArgumentException if more than one EntityMapping element has an ejb-name element with the matching
77      * value
78      */

79     public static EntityMapping findEntityMapping(SunCmpMappings scms, String JavaDoc bname, boolean addEmpty) throws IllegalArgumentException JavaDoc {
80         EntityMapping retVal = (EntityMapping) findSingleCompatibleBean(
81                 "scms", scms, "bname", bname, "ejb-name", EntityMapping.class); // NOI18N
82
if ((null == retVal) && addEmpty) {
83             retVal = new EntityMapping();
84             retVal.setEjbName(bname);
85             SunCmpMapping scm = getFirstSunCmpMapping(scms, addEmpty);
86             scm.addEntityMapping(retVal);
87         }
88         return retVal;
89     }
90     
91     /** Find the cmr-field-mapping element for a field in an EntityMapping object
92      * @return the CmrFieldMapping element or null if addEmpty is false and the field is not
93      * found.
94      * @param em The root of the search
95      * @param fname the name of the field
96      * @param addEmpty flag to add an empty version of the CmrFieldMapping element to the graph if one is
97      * not found.
98      * @throws IllegalArgumentException If there is more than one cmr-field with a matching cmr-field-name element in
99      * the EntityMapping graph.
100      */

101     public static CmrFieldMapping findCmrFieldMapping(EntityMapping em, String JavaDoc fname, boolean addEmpty) throws IllegalArgumentException JavaDoc {
102         CmrFieldMapping retVal = (CmrFieldMapping) findSingleCompatibleBean(
103                 "em", em, "fname", fname, "cmr-field-name", CmrFieldMapping.class); // NOI18N
104
if ((null == retVal) && addEmpty) {
105             retVal = new CmrFieldMapping();
106             retVal.setCmrFieldName(fname);
107             em.addCmrFieldMapping(retVal);
108         }
109         return retVal;
110     }
111     
112     /** Find the CmpFieldMapping element with a matching field-name element value
113      * @return The CmpFieldMapping element or null if addEmpty is false and the field is not
114      * found.
115      * @param em the root of the search
116      * @param fname the value of the field-name element
117      * @param addEmpty flag to add an empty version of the CmpFieldMapping element to the graph if one is
118      * not found.
119      * @throws IllegalArgumentException If there is more than one cmp-field with a matching field-name element in
120      * the EntityMapping graph.
121      */

122     public static CmpFieldMapping findCmpFieldMapping(EntityMapping em, String JavaDoc fname, boolean addEmpty) throws IllegalArgumentException JavaDoc {
123         CmpFieldMapping retVal = (CmpFieldMapping) findSingleCompatibleBean(
124                 "em", em, "fname", fname, "field-name", CmpFieldMapping.class); // NOI18N
125
if ((null == retVal) && addEmpty) {
126             retVal = new CmpFieldMapping();
127             retVal.setFieldName(fname);
128             em.addCmpFieldMapping(retVal);
129         }
130         return retVal;
131     }
132     
133     /** helper method */
134     private static BaseBean findSingleCompatibleBean(String JavaDoc argOneName, BaseBean argOne, String JavaDoc argTwoName,
135             String JavaDoc argTwo, String JavaDoc propName, Class JavaDoc type) {
136         BaseBean retVal = null;
137         if (null == argTwo || argTwo.length() < 1)
138             throw new IllegalArgumentException JavaDoc(argTwoName);
139         
140         List JavaDoc l = findCompatibleBeansWithValue(argOne, propName, argTwo,
141             type);
142         if (null != l) {
143             if (l.size() == 1)
144                 retVal = (BaseBean) l.get(0);
145             else if (l.size() > 1)
146                 throw new IllegalArgumentException JavaDoc(argOneName);
147         }
148         return retVal;
149     }
150
151     /** A utility for finding beans in a graph of BaseBean objects.
152      *
153      * Search the bean graph, starting at a given root, for beans where the named
154      * property has the given value. The returned list is filtered by assignment
155      * compatibility.
156      * @return The assignment compatible BaseBeans that were found.
157      * @param root The root of a search
158      * @param propName The name of the element
159      * @param propVal the value of the element
160      * @param type The expected type of the value to be returned.
161      * @throws IllegalArgumentException If the bean is not part of a complete bean graph.
162      */

163     protected static List JavaDoc findCompatibleBeansWithValue(BaseBean root, String JavaDoc propName, String JavaDoc propVal, Class JavaDoc type) throws IllegalArgumentException JavaDoc {
164         List JavaDoc retVal = null;
165         GraphManager gm = root.graphManager();
166         if (null == gm)
167             throw new IllegalArgumentException JavaDoc(
168                     bundle.getString("ERR_DISCONNECTED_NOT_SUPPORTED"));
169         String JavaDoc[] props = root.findPropertyValue(propName, propVal);
170         int len = 0;
171         if (null != props)
172             len = props.length;
173         if (len > 0)
174             retVal = new ArrayList JavaDoc();
175         for (int i = 0; i < len; i++) {
176             // get the bean that is the property's parent.
177
BaseBean candidate = gm.getPropertyParent(props[i]);
178             if (type.isInstance(candidate))
179                 retVal.add(candidate);
180         }
181         return retVal;
182     }
183 }
184
Popular Tags