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  import javax.xml.bind.JAXBContext;
20  import javax.xml.bind.JAXBException;
21  import javax.xml.bind.Unmarshaller;
22  
23  import org.kuali.common.impex.model.DataType;
24  import org.kuali.common.impex.model.Schema;
25  import org.kuali.common.impex.model.Table;
26  import org.kuali.common.util.LocationUtils;
27  
28  /**
29   * String utility methods for classes creating sql from schema model data
30   */
31  public class ProducerUtils {
32  
33  	// Constants in common between many producer classes
34  	public static final String NEWLINE = "\n";
35  	public static final String COMMA = ",";
36  	public static final String DOT = ".";
37  	public static final String SPACE = " ";
38  
39  	/**
40  	 * This value was retrieved from the Velocity templates that were generating the schema sqls in previous versions of Impex.
41  	 * 
42  	 * 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
43  	 */
44  	private static final int MAX_TABLE_NAME_SNIPPET_SIZE_FOR_PK = 27;
45  
46  	private static final String PRIMARY_KEY_SUFFIX = "P1";
47  	public static final char SINGLE_QUOTE = '\'';
48  	public static final String TYPE_SIZE_PREFIX = "(";
49  	public static final String TYPE_SIZE_SUFFIX = ")";
50  	public static final String NOT_NULL = "NOT NULL";
51  	public static final String NEWLINE_TAB = "\n\t";
52  
53  	public static String generatePrimaryKeyName(Table t) {
54  		StringBuilder sb = new StringBuilder();
55  
56  		// truncate table name to MAX_TABLE_NAME_SNIPPET_SIZE_FOR_PK
57  		String truncated = t.getName();
58  		if (truncated.length() > MAX_TABLE_NAME_SNIPPET_SIZE_FOR_PK) {
59  			truncated = truncated.substring(0, MAX_TABLE_NAME_SNIPPET_SIZE_FOR_PK);
60  		}
61  
62  		sb.append(truncated).append(PRIMARY_KEY_SUFFIX);
63  
64  		return sb.toString();
65  	}
66  
67  	public static boolean isDateType(DataType dataType) {
68  		if (dataType == null) {
69  			return false;
70  		}
71  
72  		return dataType == DataType.DATE || dataType == DataType.TIMESTAMP;
73  	}
74  
75  	/**
76  	 * Standard code for initializing a schema model from an xml resource
77  	 * 
78  	 * @param xmlLocation
79  	 *            resource location of the xml
80  	 * @return a Schema populated from the xml
81  	 * @throws JAXBException
82  	 * @throws IOException
83  	 */
84  	public static Schema unmarshalSchema(String xmlLocation) throws JAXBException, IOException {
85  		JAXBContext context = JAXBContext.newInstance(Schema.class);
86  		Unmarshaller unmarshaller = context.createUnmarshaller();
87  
88  		return (Schema) unmarshaller.unmarshal(LocationUtils.getBufferedReader(xmlLocation));
89  	}
90  }