View Javadoc

1   package org.ocltf.mapping;
2   
3   import java.net.MalformedURLException;
4   import java.net.URL;
5   import java.util.Collection;
6   import java.util.HashMap;
7   import java.util.Iterator;
8   import java.util.Map;
9   
10  import org.apache.commons.lang.StringUtils;
11  import org.apache.commons.lang.builder.ToStringBuilder;
12  import org.ocltf.common.XmlObjectFactory;
13  import org.ocltf.templateobject.TemplateObjectException;
14  import org.ocltf.utils.ExceptionUtils;
15  
16  /***
17   * An object responsible for mapping types in the object model to other language identifiers/types.
18   * (For example, Java, SQL, Jdbc, etc).  The public constructor should NOT be used to 
19   * construct this instance.  An instance of this object should be retrieved
20   * through the method getInstance(java.net.URL). 
21   * 
22   * <p> The mappings will change based upon the language, database, etc being used. <p>
23   */
24  public class Mappings {
25  	
26  	/***
27  	 * Holds the name of this mapping. This corresponds usually to some language (i.e. Java,
28  	 * or a database such as Oracle, Sql Server, etc).
29  	 */
30  	private String name = null;
31  	
32  	/***
33  	 * Contains the set of JdbcTypeMapping objects keyed by the 'type'
34  	 * element defined within the type mapping XML file.
35  	 */
36  	private Map mappings = new HashMap();
37  	
38  	/***
39  	 * Holds the resource path from which this Mappings object
40  	 * was loaded.
41  	 */
42  	private URL resource;
43  	
44  	/***
45  	 * Returns a new configured instance of this TypeMappings
46  	 * configured from the mappings configuration URI string.
47  	 * 
48  	 * @param mappingsUri the URI to the XML type mappings configuration file.
49       * @return Mappings the configured Mappings instance.
50  	 * @throws MalformedURLException when the mappingsUri is invalid (not a valid URL).
51  	 */
52  	public static Mappings getInstance(String mappingsUri) throws MalformedURLException {
53  		String methodName = "getInstance";
54  		ExceptionUtils.checkNull(methodName, "mappingsUri", mappingsUri);
55  		Mappings mappings = getInstance(new URL(mappingsUri));
56  		return mappings;
57  	}
58  	
59  	/***
60  	 * Returns a new configured instance of this TypeMappings
61  	 * configured from the mappings configuration URI.
62  	 * 
63  	 * @param mappingsUri the URI to the XML type mappings configuration file.
64       * @return Mappings the configured Mappings instance.
65  	 */
66  	public static Mappings getInstance(URL mappingsUri) {
67  		String methodName = "getInstance";
68  		ExceptionUtils.checkNull(methodName, "mappingsUri", mappingsUri);
69  		Mappings mappings = (Mappings)XmlObjectFactory.getInstance(Mappings.class, mappingsUri);
70  		mappings.resource = mappingsUri;
71  		return mappings;
72  	}
73  
74  	/***
75  	 * Returns the name name (this is the
76  	 * name for which the type mappings are for).
77  	 * 
78  	 * @return String the name name
79  	 */
80  	public String getName() {
81  		String methodName = "getName";
82  		if (StringUtils.isEmpty(this.name)) {
83  			throw new TemplateObjectException(methodName
84  				 + " - name can not be null or empty");
85  		}
86  		return name;
87  	}
88  
89  	/***
90  	 * Sets the name name.
91  	 * @param name
92  	 */
93  	public void setName(String name) {
94  		this.name = name;
95  	}
96  	
97  	/***
98  	 * Adds a JdbcTypeMapping object to the set of current mappings.
99  	 * 
100 	 * @param mapping the JdbcTypeMapping instance.
101 	 */
102 	public void addMapping(Mapping mapping) {
103 		String methodName = "addMapping";
104 		ExceptionUtils.checkNull(methodName, "mapping", mapping);
105 		
106 		Collection fromTypes = mapping.getFroms();
107 		ExceptionUtils.checkNull(methodName, "mapping.fromTypes", fromTypes);
108 		
109 		Iterator typeIt = fromTypes.iterator();
110 		while (typeIt.hasNext()) {
111 			String type = (String)typeIt.next();
112 			this.mappings.put(type, mapping);	
113 		}
114 	}
115 	
116 	/***
117 	* Returns the JDBC type for the given model type.
118 	*
119 	* @param from the 'from' mapping, this is the type/identifier
120 	*        that is in the model.
121 	* @return String to the 'to' mapping (this is the mapping that
122 	*         can be retrieved if a corresponding 'from' is found.
123 	*/
124 	public String getTo(String from) {
125 		from = StringUtils.deleteWhitespace(from);
126 		
127 		String to = null;
128 		
129 		String arraySuffix = "[]";
130 		//if the type is an array suffix, then strip the array off
131 		//so we can find the mapping
132 		int suffixIndex = from.indexOf(arraySuffix);
133 		if (suffixIndex != -1) {
134 			from = StringUtils.replace(from, arraySuffix, "");
135 		}
136 		
137 		Mapping mapping = this.getMapping(from);
138 		
139 		if (mapping != null) {
140 			StringBuffer buf = new StringBuffer(mapping.getTo());
141 		
142 			if (suffixIndex != -1) {
143 				//append the suffix back to the return value;
144 				buf.append(arraySuffix);
145 			}
146 			to = buf.toString();	
147 		}
148 		
149 		return to;
150 	}
151 	
152 	/***
153 	 * Returns the resource URI from 
154 	 * which this Mappings object was loaded.
155 	 * @return URL
156 	 */
157 	public URL getResource() {
158 		return this.resource;
159 	}
160 	
161 	private Mapping getMapping(String from) {
162 		return (Mapping)mappings.get(from);
163 	}
164 
165 	/***
166 	 * @see java.lang.Object#toString()
167 	 */
168 	public String toString() {
169 		return ToStringBuilder.reflectionToString(this);
170 	}
171 }