View Javadoc

1   /**
2    * Copyright 2011 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.common.impex;
17  
18  import java.io.IOException;
19  
20  import javax.xml.bind.JAXBContext;
21  import javax.xml.bind.JAXBException;
22  import javax.xml.bind.Unmarshaller;
23  
24  import org.kuali.common.impex.model.DataType;
25  import org.kuali.common.impex.model.Schema;
26  import org.kuali.common.impex.model.Table;
27  import org.kuali.common.util.LocationUtils;
28  
29  /**
30   * String utility methods for classes creating sql from schema model data
31   */
32  public class ProducerUtils {
33  
34  	// Constants in common between many producer classes
35  	public static final String NEWLINE = "\n";
36  	public static final String COMMA = ",";
37  	public static final String DOT = ".";
38  	public static final String SPACE = " ";
39  	public static final String STATEMENT_SEPARATOR = "/";
40  
41  	/**
42  	 * This value was retrieved from the Velocity templates that were generating the schema sqls in previous versions of Impex.
43  	 * 
44  	 * No idea on why this value was chosen, though it's safe to guess it ensures a primary key name that is of acceptable length to many DB vendors
45  	 */
46  	private static final int MAX_TABLE_NAME_SNIPPET_SIZE_FOR_PK = 27;
47  
48  	private static final String PRIMARY_KEY_SUFFIX = "P1";
49  	public static final char SINGLE_QUOTE = '\'';
50  	public static final String TYPE_SIZE_PREFIX = "(";
51  	public static final String TYPE_SIZE_SUFFIX = ")";
52  	public static final String NOT_NULL = "NOT NULL";
53  	public static final String NEWLINE_TAB = "\n\t";
54  
55  	public static String generatePrimaryKeyName(Table t) {
56  		StringBuilder sb = new StringBuilder();
57  
58  		// truncate table name to MAX_TABLE_NAME_SNIPPET_SIZE_FOR_PK
59  		String truncated = t.getName();
60  		if (truncated.length() > MAX_TABLE_NAME_SNIPPET_SIZE_FOR_PK) {
61  			truncated = truncated.substring(0, MAX_TABLE_NAME_SNIPPET_SIZE_FOR_PK);
62  		}
63  
64  		sb.append(truncated).append(PRIMARY_KEY_SUFFIX);
65  
66  		return sb.toString();
67  	}
68  
69  	public static boolean isDateType(DataType dataType) {
70  		if (dataType == null) {
71  			return false;
72  		}
73  
74  		return dataType == DataType.DATE || dataType == DataType.TIMESTAMP;
75  	}
76  
77  	public static boolean isTextType(DataType dataType) {
78  		if (dataType == null) {
79  			return false;
80  		}
81  
82  		return dataType == DataType.STRING || dataType == DataType.CLOB;
83  	}
84  
85  	/**
86  	 * Standard code for initializing a schema model from an xml resource
87  	 * 
88  	 * @param xmlLocation
89  	 *            resource location of the xml
90  	 * @return a Schema populated from the xml
91  	 * @throws JAXBException
92  	 * @throws IOException
93  	 */
94  	public static Schema unmarshalSchema(String xmlLocation) throws JAXBException, IOException {
95  		JAXBContext context = JAXBContext.newInstance(Schema.class);
96  		Unmarshaller unmarshaller = context.createUnmarshaller();
97  
98  		return (Schema) unmarshaller.unmarshal(LocationUtils.getBufferedReader(xmlLocation));
99  	}
100 }