View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  /*
17   * Created on 2005/08/09
18   *
19   * TODO To change the template for this generated file go to
20   * Window - Preferences - Java - Code Style - Code Templates
21   */
22  package org.seasar.tuigwaa.database;
23  
24  import java.util.Properties;
25  
26  import org.hibernate.FetchMode;
27  import org.hibernate.MappingException;
28  import org.hibernate.id.PersistentIdentifierGenerator;
29  import org.hibernate.mapping.Column;
30  import org.hibernate.mapping.ManyToOne;
31  import org.hibernate.mapping.Property;
32  import org.hibernate.mapping.RootClass;
33  import org.hibernate.mapping.SimpleValue;
34  import org.hibernate.mapping.Table;
35  import org.hibernate.type.TypeFactory;
36  import org.seasar.tuigwaa.model.core.TgwAttribute;
37  import org.seasar.tuigwaa.model.core.impl.FileAttribute;
38  import org.seasar.tuigwaa.model.core.impl.FkAttribute;
39  import org.seasar.tuigwaa.system.Constants;
40  import org.seasar.tuigwaa.util.TgwUtils;
41  
42  
43  /***
44   * おそらく使われていない。様子見のため残している。
45   * @author nishioka
46   * @deprecated
47   */
48  class DynaBinder {
49  
50  	/***
51  	 * @deprecated
52  	 */
53  	public static void bindColumn(RootClass root, TgwAttribute field)
54  			throws MappingException {
55  		String columnName = field.getName();
56  		String hibernateType = TgwUtils.toHibernateType(field);
57  
58  		// Type type = Don't delete
59  		TypeFactory.heuristicType(hibernateType);
60  
61  		Table table = root.getTable();
62  
63  		SimpleValue value = new SimpleValue(table);
64  		value.setTypeName(hibernateType);
65  
66  		bindColumn(table, value, columnName, hibernateType, true);
67  
68  		// String propName = CharUtil.toUpperCaseAtFisrtChar(columnName);
69  		String propName = columnName;
70  		Property prop = createProperty(propName, value, field);
71  		if (propName.equals(Constants.ENTITY_BUILTIN_CREATIONDATE)
72  				|| field instanceof FileAttribute) {
73  			prop.setUpdateable(false);
74  		}
75  		root.addProperty(prop);
76  	}
77  
78  	/***
79  	 * @deprecated
80  	 */
81  	public static void bindManyToOne(RootClass root, TgwAttribute field)
82  			throws MappingException, ClassNotFoundException {
83  		FkAttribute fk = (FkAttribute) field;
84  		Table table = root.getTable();
85  
86  		ManyToOne value = new ManyToOne(root.getTable());
87  
88  		value.setReferencedEntityName(fk.getRefEntity().getJavaClassName());
89  
90  		// Type type =
91  		// TypeFactory.manyToOne(Class.forName(fk.getRefClassName()));
92  
93  		bindColumn(table, value, field.getName(), null, true);
94  
95  		// value.setType(type);
96  		// value.setOuterJoinFetchSetting(OuterJoinLoader.AUTO);
97  		value.setFetchMode(FetchMode.DEFAULT);
98  		// value.setForeignKeyName();
99  
100 		value.createForeignKey();
101 		// String propName = CharUtil.toUpperCaseAtFisrtChar(field.getName());
102 		String propName = field.getName();
103 		Property prop = createProperty(propName, value, field);
104 		root.addProperty(prop);
105 
106 	}
107 
108 	/***
109 	 * @deprecated
110 	 */
111 	public static void bindId(RootClass root, String schema)
112 			throws MappingException {
113 
114 		Table table = root.getTable();
115 
116 		SimpleValue id = new SimpleValue(table);
117 		root.setIdentifier(id);
118 
119 		id.setTypeName("java.lang.Long");
120 		id.setIdentifierGeneratorStrategy("increment");
121 		id.getTable().setIdentifierValue(id);
122 
123 		// bindColumn(table, id, "id", id.getType(), false);
124 		bindColumn(table, id, Constants.ENTITY_BUILTIN_ID, null, true);
125 
126 		Properties param = new Properties();
127 
128 		if (schema != null) {
129 			param.setProperty(PersistentIdentifierGenerator.SCHEMA, schema);
130 		}
131 
132 		param.setProperty(PersistentIdentifierGenerator.TABLE, root.getTable()
133 				.getName());
134 		param.setProperty(PersistentIdentifierGenerator.PK, "id");
135 		// param.setProperty("sequence", table.getName() + "_id_seq");
136 
137 		id.setIdentifierGeneratorProperties(param);
138 
139 		Property idProp = createProperty("Id", id, null);
140 		root.setIdentifierProperty(idProp);
141 
142 		root.createPrimaryKey();
143 	}
144 
145 	/***
146 	 * @deprecated
147 	 */
148 	public static void bindColumn(Table table, SimpleValue value, String name,
149 			String hibernateType, boolean nullable) {
150 		Column contentCol = new Column();
151 		// contentCol.setType(type);
152 		// contentCol.setT(hibernateType);
153 		contentCol.setNullable(nullable);
154 		if(!Constants.ENTITY_BUILTIN_ID.equals(name)){
155 			contentCol.setName(name + "_");	
156 		}else{
157 			contentCol.setName(name);	
158 		}
159 		table.addColumn(contentCol);
160 		value.addColumn(contentCol);
161 	}
162 
163 	/***
164 	 * @deprecated
165 	 */
166 	public static Property createProperty(String propertyName,
167 			SimpleValue value, TgwAttribute field) {
168 		Property prop = new Property();
169 		prop.setName(propertyName);
170 		prop.setValue(value);
171 		return prop;
172 	}
173 }