Return-Path: Delivered-To: apmail-db-torque-dev-archive@www.apache.org Received: (qmail 39052 invoked from network); 16 Feb 2010 17:17:33 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 16 Feb 2010 17:17:33 -0000 Received: (qmail 99390 invoked by uid 500); 16 Feb 2010 17:17:33 -0000 Delivered-To: apmail-db-torque-dev-archive@db.apache.org Received: (qmail 99369 invoked by uid 500); 16 Feb 2010 17:17:33 -0000 Mailing-List: contact torque-dev-help@db.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Apache Torque Developers List" Reply-To: "Apache Torque Developers List" Delivered-To: mailing list torque-dev@db.apache.org Received: (qmail 99355 invoked by uid 500); 16 Feb 2010 17:17:33 -0000 Received: (qmail 99352 invoked by uid 99); 16 Feb 2010 17:17:33 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 16 Feb 2010 17:17:33 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 16 Feb 2010 17:17:25 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id E18462388C0F; Tue, 16 Feb 2010 17:16:13 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r910600 [19/29] - in /db/torque/torque4/trunk: maven-torque-gf-plugin/ maven-torque-gf-plugin/src/ maven-torque-gf-plugin/src/main/ maven-torque-gf-plugin/src/main/java/ maven-torque-gf-plugin/src/main/java/org/ maven-torque-gf-plugin/src/m... Date: Tue, 16 Feb 2010 17:16:02 -0000 To: torque-commits@db.apache.org From: tfischer@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100216171613.E18462388C0F@eris.apache.org> Added: db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/PlatformSapdbImpl.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/PlatformSapdbImpl.java?rev=910600&view=auto ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/PlatformSapdbImpl.java (added) +++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/PlatformSapdbImpl.java Tue Feb 16 17:15:43 2010 @@ -0,0 +1,107 @@ +package org.apache.torque.templates.platform; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.torque.templates.typemapping.SqlType; +import org.apache.torque.templates.typemapping.SchemaType; + +/** + * SAP DB Platform implementation. + * + * @author Martin Poeschl + * @version $Id: PlatformSapdbImpl.java 473814 2006-11-11 22:30:30 +0000 (Sat, 11 Nov 2006) tv $ + */ +public class PlatformSapdbImpl extends PlatformDefaultImpl +{ + /** + * Default constructor. + */ + public PlatformSapdbImpl() + { + super(); + initialize(); + } + + /** + * Initializes db specific domain mapping. + */ + private void initialize() + { + setSchemaTypeToSqlTypeMapping( + SchemaType.BIT, + new SqlType("FIXED", "1", "0")); + setSchemaTypeToSqlTypeMapping( + SchemaType.TINYINT, + new SqlType("CHAR BYTE")); + setSchemaTypeToSqlTypeMapping( + SchemaType.BIGINT, + new SqlType("FIXED", "38", "0")); + setSchemaTypeToSqlTypeMapping( + SchemaType.DOUBLE, + new SqlType("DOUBLE PRECISION")); + setSchemaTypeToSqlTypeMapping( + SchemaType.NUMERIC, + new SqlType("DECIMAL")); + setSchemaTypeToSqlTypeMapping( + SchemaType.LONGVARCHAR, + new SqlType("LONG VARCHAR")); + setSchemaTypeToSqlTypeMapping( + SchemaType.BINARY, + new SqlType("CHAR(254) BYTE")); + setSchemaTypeToSqlTypeMapping( + SchemaType.VARBINARY, + new SqlType("LONG BYTE")); + setSchemaTypeToSqlTypeMapping( + SchemaType.LONGVARBINARY, + new SqlType("LONG BYTE")); + setSchemaTypeToSqlTypeMapping( + SchemaType.JAVA_OBJECT, + new SqlType("LONG BYTE")); + setSchemaTypeToSqlTypeMapping( + SchemaType.BLOB, + new SqlType("LONG BYTE")); + setSchemaTypeToSqlTypeMapping( + SchemaType.CLOB, + new SqlType("LONG UNICODE")); + setSchemaTypeToSqlTypeMapping( + SchemaType.BOOLEANINT, + new SqlType("SMALLINT")); + setSchemaTypeToSqlTypeMapping( + SchemaType.BOOLEANCHAR, + new SqlType("CHAR")); + } + + /** + * @see Platform#getNativeIdMethod() + */ + public String getNativeIdMethod() + { + return Platform.SEQUENCE; + } + + /** + * @see Platform#getAutoIncrement() + */ + public String getAutoIncrement() + { + return "DEFAULT SERIAL"; + } + +} Added: db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/PlatformSybaseImpl.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/PlatformSybaseImpl.java?rev=910600&view=auto ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/PlatformSybaseImpl.java (added) +++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/PlatformSybaseImpl.java Tue Feb 16 17:15:43 2010 @@ -0,0 +1,94 @@ +package org.apache.torque.templates.platform; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.torque.templates.typemapping.SqlType; +import org.apache.torque.templates.typemapping.SchemaType; + +/** + * Sybase Platform implementation. + * + * @author Martin Poeschl + * @version $Id: PlatformSybaseImpl.java 591651 2007-11-03 16:48:23Z tfischer $ + */ +public class PlatformSybaseImpl extends PlatformDefaultImpl +{ + /** + * Default constructor. + */ + public PlatformSybaseImpl() + { + super(); + initialize(); + } + + /** + * Initializes db specific domain mapping. + */ + private void initialize() + { + setSchemaTypeToSqlTypeMapping( + SchemaType.INTEGER, + new SqlType("INT")); + setSchemaTypeToSqlTypeMapping( + SchemaType.BOOLEANINT, + new SqlType("INT")); + setSchemaTypeToSqlTypeMapping( + SchemaType.DOUBLE, + new SqlType("FLOAT")); + setSchemaTypeToSqlTypeMapping( + SchemaType.LONGVARCHAR, + new SqlType("TEXT")); + setSchemaTypeToSqlTypeMapping( + SchemaType.DATE, + new SqlType("DATETIME")); + setSchemaTypeToSqlTypeMapping( + SchemaType.TIMESTAMP, + new SqlType("DATETIME")); + setSchemaTypeToSqlTypeMapping( + SchemaType.VARBINARY, + new SqlType("IMAGE")); + setSchemaTypeToSqlTypeMapping( + SchemaType.LONGVARBINARY, + new SqlType("IMAGE")); + setSchemaTypeToSqlTypeMapping( + SchemaType.BLOB, + new SqlType("IMAGE")); + setSchemaTypeToSqlTypeMapping( + SchemaType.CLOB, + new SqlType("TEXT")); + } + + /** + * @see Platform#getMaxColumnNameLength() + */ + public int getMaxColumnNameLength() + { + return 30; + } + + /** + * @see Platform#getNullString(boolean) + */ + public String getNullString(boolean notNull) + { + return (notNull ? "NOT NULL" : "NULL"); + } +} Added: db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/package.html URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/package.html?rev=910600&view=auto ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/package.html (added) +++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/package.html Tue Feb 16 17:15:43 2010 @@ -0,0 +1,45 @@ + + + +Platform package + + + + +This package contains classes that provide compatibility to a range of RDBM platforms. +

+The Platform interface provides callbacks that allow to get +RDBMS Platform specific information whereever neccessary. + +

+The class PlatformDefaultImpl implements Platform and +provides default implementations of all methods. + +

+For each supported Platform Xxx there is a class PlatformXxxImpl +which extends PlatformDefaultImpl and may override the methods of the +baseclass. + +

+The factory class PlatformFactory can be used to create Platform +instances. + + + Added: db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/sourcefilter/BeanSourceFilter.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/sourcefilter/BeanSourceFilter.java?rev=910600&view=auto ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/sourcefilter/BeanSourceFilter.java (added) +++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/sourcefilter/BeanSourceFilter.java Tue Feb 16 17:15:43 2010 @@ -0,0 +1,39 @@ +package org.apache.torque.templates.sourcefilter; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.torque.gf.control.ControllerState; +import org.apache.torque.gf.source.filter.SourceFilter; +import org.apache.torque.templates.TemplateOptionName; + +/** + * A source filter which returns true if the option + * "torque.om.generateBeans" is true. + * + * @version $Id: $ + */ +public class BeanSourceFilter implements SourceFilter +{ + public boolean proceed(ControllerState controllerState) + { + return controllerState.getBooleanOption( + TemplateOptionName.GENERATE_BEANS); + } +} Added: db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/sourcefilter/InterfaceSourceFilter.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/sourcefilter/InterfaceSourceFilter.java?rev=910600&view=auto ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/sourcefilter/InterfaceSourceFilter.java (added) +++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/sourcefilter/InterfaceSourceFilter.java Tue Feb 16 17:15:43 2010 @@ -0,0 +1,49 @@ +package org.apache.torque.templates.sourcefilter; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.torque.gf.control.ControllerState; +import org.apache.torque.gf.source.SourceElement; +import org.apache.torque.gf.source.filter.SourceFilter; +import org.apache.torque.templates.TorqueSchemaAttributeName; + +/** + * A source filter which returns true if the attribute "interface" + * is set on the current source element and if the name does not contain + * a dot. + * + * @version $Id: $ + */ +public class InterfaceSourceFilter implements SourceFilter +{ + public boolean proceed(ControllerState controllerState) + { + SourceElement sourceElement = controllerState.getSourceElement(); + String interfaceName + = (String) sourceElement.getAttribute( + TorqueSchemaAttributeName.INTERFACE.getName()); + if (interfaceName == null) + { + return false; + } + boolean containsDot = interfaceName.indexOf('.') != -1; + return !containsDot; + } +} Added: db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/sourcefilter/ManagerSourceFilter.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/sourcefilter/ManagerSourceFilter.java?rev=910600&view=auto ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/sourcefilter/ManagerSourceFilter.java (added) +++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/sourcefilter/ManagerSourceFilter.java Tue Feb 16 17:15:43 2010 @@ -0,0 +1,46 @@ +package org.apache.torque.templates.sourcefilter; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.util.List; + +import org.apache.torque.gf.control.ControllerState; +import org.apache.torque.gf.source.SourceElement; +import org.apache.torque.gf.source.filter.SourceFilter; +import org.apache.torque.templates.TemplateOptionName; +import org.apache.torque.templates.TorqueSchemaElementName; +import org.apache.torque.templates.transformer.om.TableChildElementName; + +public class ManagerSourceFilter implements SourceFilter +{ + public boolean proceed(ControllerState controllerState) + { + if (!controllerState.getBooleanOption( + TemplateOptionName.OM_USE_MANAGERS)) + { + return false; + } + SourceElement sourceElement = controllerState.getSourceElement(); + List primaryKeyColumns + = sourceElement.getChild(TableChildElementName.PRIMARY_KEYS) + .getChildren(TorqueSchemaElementName.COLUMN); + return !primaryKeyColumns.isEmpty(); + } +} Added: db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/sourcefilter/MapInitSourceFilter.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/sourcefilter/MapInitSourceFilter.java?rev=910600&view=auto ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/sourcefilter/MapInitSourceFilter.java (added) +++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/sourcefilter/MapInitSourceFilter.java Tue Feb 16 17:15:43 2010 @@ -0,0 +1,39 @@ +package org.apache.torque.templates.sourcefilter; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.torque.gf.control.ControllerState; +import org.apache.torque.gf.source.filter.SourceFilter; +import org.apache.torque.templates.TemplateOptionName; + +/** + * A source filter which returns true if the option + * "torque.om.generateMapInit" is true. + * + * @version $Id: $ + */ +public class MapInitSourceFilter implements SourceFilter +{ + public boolean proceed(ControllerState controllerState) + { + return controllerState.getBooleanOption( + TemplateOptionName.OM_GENERATE_MAP_INIT); + } +} Added: db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/CollectAttributeSetTrueTransformer.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/CollectAttributeSetTrueTransformer.java?rev=910600&view=auto ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/CollectAttributeSetTrueTransformer.java (added) +++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/CollectAttributeSetTrueTransformer.java Tue Feb 16 17:15:43 2010 @@ -0,0 +1,98 @@ +package org.apache.torque.templates.transformer; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.util.ArrayList; +import java.util.List; + +import org.apache.torque.gf.control.ControllerState; +import org.apache.torque.gf.source.RichSourceElementImpl; +import org.apache.torque.gf.source.SourceAttributeName; +import org.apache.torque.gf.source.SourceElement; +import org.apache.torque.gf.source.SourceElementName; + +/** + * Collects the children which have a certain attribute set to true + * in another child element. + * + * So the source elements are + * rootElement + * ${childrenToProcessName} + * ... + * ${childrenToProcessName} + * ... + * ... + * + * and the outcome is + * + * rootElement + * ${childrenToProcessName} + * ... + * ${childrenToProcessName} + * ... + * ... + * ${targetElementName} + * ${childrenToProcessName} + * ... + * ... + */ +public class CollectAttributeSetTrueTransformer +{ + /** + * Processes the transformation. + * + * @param rootElement the root of the transformation. + * @param controllerState the controller state, not null. + * @param childrenToProcessName the name of the child elements + * to check. + * @param attributeToCheckName the attribute in the child which is checked + * for the value "true" + * @param targetElementName the child element which collects the hits. + */ + public void transform( + SourceElement rootElement, + ControllerState controllerState, + SourceElementName childrenToProcessName, + SourceAttributeName attributeToCheckName, + SourceElementName targetElementName) + { + List hits + = new ArrayList(); + for (SourceElement column : rootElement.getChildren( + childrenToProcessName)) + { + String attributeToCheck + = (String) column.getAttribute(attributeToCheckName); + if (Boolean.parseBoolean(attributeToCheck)) + { + hits.add(column); + } + } + + SourceElement targetElement + = new RichSourceElementImpl(targetElementName); + rootElement.addChild(targetElement); + + for (SourceElement hit : hits) + { + targetElement.addChild(hit); + } + } +} Added: db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/SchemaTypeHelper.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/SchemaTypeHelper.java?rev=910600&view=auto ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/SchemaTypeHelper.java (added) +++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/SchemaTypeHelper.java Tue Feb 16 17:15:43 2010 @@ -0,0 +1,111 @@ +package org.apache.torque.templates.transformer; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.torque.gf.control.ControllerState; +import org.apache.torque.gf.source.SourceElement; +import org.apache.torque.gf.source.transform.SourceTransformerException; +import org.apache.torque.templates.TemplateOptionName; +import org.apache.torque.templates.TorqueSchemaAttributeName; +import org.apache.torque.templates.platform.Platform; +import org.apache.torque.templates.platform.PlatformFactory; +import org.apache.torque.templates.typemapping.SchemaType; +import org.apache.torque.templates.typemapping.SqlType; + +/** + * Helper class for retrieving the schema type of a column. + * + * $Id: $ + */ +public class SchemaTypeHelper +{ + /** + * Determines the schema type of a column. + * + * @param columnElement the source element which defines the column + * for which the schema type should be determined; not null. + * @param controllerState the controller state, not null. + * + * @return the schema type of the column, not null. + * + * @throws SourceTransformerException if the name attribute is not set + * in the column or if the type refers to an unknown type. + */ + public static SchemaType getSchemaType( + SourceElement columnElement, + ControllerState controllerState) + throws SourceTransformerException + { + String columnNameFromSchema + = (String) columnElement.getAttribute( + TorqueSchemaAttributeName.NAME); + if (columnNameFromSchema == null) + { + throw new SourceTransformerException("The attribute " + + TorqueSchemaAttributeName.NAME.getName() + + " on element " + + columnElement.getName() + + " is null"); + } + SchemaType schemaType = null; + String schemaTypeString = (String) columnElement.getAttribute( + TorqueSchemaAttributeName.TYPE.getName()); + // TODO: should there be a default type ? If not, throw an exception. + if (schemaTypeString == null) + { + schemaTypeString = controllerState.getStringOption( + TemplateOptionName.DEFAULT_TYPE); + } + schemaType = SchemaType.valueOf(schemaTypeString); + if (schemaType == null) + { + throw new SourceTransformerException("Unknown type " + + schemaTypeString + + " in Column " + + columnNameFromSchema); + } + return schemaType; + } + + /** + * Returns the SQL type for a schema type and the specified target database. + * + * @param schemaType the schema type for which the domain should be + * determined, not null. + * @param controllerState the controller state, not null. + * + * @return the domain for the schema type, or null if no domain + * exists for the schema type. + */ + public static SqlType getSqlType( + SchemaType schemaType, + ControllerState controllerState, + String size, + String scale, + String defaultValue) + { + Platform platform = PlatformFactory.getPlatformFor( + controllerState.getStringOption(TemplateOptionName.DATABASE)); + SqlType platformSqlType = platform.getSqlTypeForSchemaType(schemaType); + SqlType result + = new SqlType(platformSqlType, size, scale, defaultValue); + return result; + } +} Added: db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/ColumnAttributeName.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/ColumnAttributeName.java?rev=910600&view=auto ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/ColumnAttributeName.java (added) +++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/ColumnAttributeName.java Tue Feb 16 17:15:43 2010 @@ -0,0 +1,73 @@ +package org.apache.torque.templates.transformer.om; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.torque.gf.source.SourceAttributeName; + +/** + * Contains the attributes for a column source element which are not + * defined in the Torque schema. + */ +public enum ColumnAttributeName implements SourceAttributeName +{ + /** + * Whether the column is a primitive column. + */ + PRIMITIVE_TYPE("primitive"), + + /** + * Whether the column is a number column. + */ + NUMBER_TYPE("number"), + PEER_COLUMN_NAME("peerColumnName"), + QUALIFIED_COLUMN_NAME("qualifiedColumnName"), + POSITION("position"), + VILLAGE_METHOD("villageMethod"), + SAMPLE_OBJECT("sampleObject"); + + /** The name of the source element attribute, not null. */ + private String name; + + /** + * Constructor. + * + * @param name the name of the source element attribute, not null. + */ + private ColumnAttributeName(String name) + { + this.name = name; + } + + /** + * Returns the name of the referenced source element attribute. + * + * @return the name of the referenced source element attribute. + */ + public String getName() + { + return name; + } + + @Override + public String toString() + { + return name; + } +} Added: db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/ColumnChildElementName.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/ColumnChildElementName.java?rev=910600&view=auto ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/ColumnChildElementName.java (added) +++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/ColumnChildElementName.java Tue Feb 16 17:15:43 2010 @@ -0,0 +1,71 @@ +package org.apache.torque.templates.transformer.om; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.torque.gf.source.SourceElementName; + +/** + * Contains the child elements for a column source element which are not + * defined in the Torque schema. These elements might be created + * in the transformation + */ +public enum ColumnChildElementName implements SourceElementName +{ + /** + * The element containing the column which is referenced by the parent + * column element in a foreign-key relation. + */ + REFERENCED_COLUMN("referenced-column"), + + /** + * The element containing the column which references the parent + * column element in a foreign-key relation. + */ + REFERENCING_COLUMN("referencing-column"); + + /** The name of the source element, not null. */ + private String name; + + /** + * Constructor. + * + * @param name the name of the source element, not null. + */ + private ColumnChildElementName(String name) + { + this.name = name; + } + + /** + * Returns the name of the referenced source element. + * + * @return the name of the referenced source element. + */ + public String getName() + { + return name; + } + + @Override + public String toString() + { + return name; + } +} Added: db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/DatabaseAttributeName.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/DatabaseAttributeName.java?rev=910600&view=auto ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/DatabaseAttributeName.java (added) +++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/DatabaseAttributeName.java Tue Feb 16 17:15:43 2010 @@ -0,0 +1,61 @@ +package org.apache.torque.templates.transformer.om; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.torque.gf.source.SourceAttributeName; + +/** + * Contains the attributes for a database source element which are not + * defined in the Torque schema. + */ +public enum DatabaseAttributeName implements SourceAttributeName +{ + /** The name of the mapInit class to generate. */ + MAP_INIT_CLASS_NAME("mapInitClassName"); + + /** The name of the source element attribute, not null. */ + private String name; + + /** + * Constructor. + * + * @param name the name of the source element attribute, not null. + */ + private DatabaseAttributeName(String name) + { + this.name = name; + } + + /** + * Returns the name of the referenced source element attribute. + * + * @return the name of the referenced source element attribute. + */ + public String getName() + { + return name; + } + + @Override + public String toString() + { + return name; + } +} Added: db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/FieldHelper.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/FieldHelper.java?rev=910600&view=auto ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/FieldHelper.java (added) +++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/FieldHelper.java Tue Feb 16 17:15:43 2010 @@ -0,0 +1,113 @@ +package org.apache.torque.templates.transformer.om; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.commons.lang.StringUtils; +import org.apache.torque.gf.control.ControllerState; +import org.apache.torque.templates.TemplateOptionName; +import org.apache.torque.templates.typemapping.JavaType; + +/** + * Generates method names from field names. + * + * $Id: $ + */ +final class FieldHelper +{ + /** + * Private constructor for utility class. + */ + private FieldHelper() + { + } + + /** + * Returns the name of the getter method for a field. + * "get" is used as prefix, except if the option + * USE_IS_FOR_GETTERS_OPTION_NAME is set to true and + * the field type is "boolean", in which case "is" is used as prefix. + * + * @param fieldName the name of the field, not null. + * @param fieldType the type of the field, not null. + * @param controllerState the current controller state, not null. + * + * @return the getter method name, not null. + */ + public static String getGetterName( + String fieldName, + String fieldType, + ControllerState controllerState) + { + String getterName; + if (controllerState.getBooleanOption( + TemplateOptionName.OM_USE_IS_FOR_BOOLEAN_GETTERS) + && JavaType.BOOLEAN_PRIMITIVE.getClassName().equals(fieldType)) + { + getterName = "is" + StringUtils.capitalize(fieldName); + } + else + { + getterName = "get" + StringUtils.capitalize(fieldName); + } + return getterName; + } + + /** + * Returns the name of the setter method for a field. + * Uses "set" as prefix. + * + * @param fieldName the name of the field, not null. + * + * @return the setter method name, not null. + */ + public static String getSetterName(String fieldName) + { + String setterName = "set" + StringUtils.capitalize(fieldName); + return setterName; + } + + /** + * Returns the name of the adder method for a field. + * Uses "add" as prefix. + * + * @param fieldName the name of the field, not null. + * + * @return the adder method name, not null. + */ + public static String getAdderName(String fieldName) + { + String setterName = "add" + StringUtils.capitalize(fieldName); + return setterName; + } + + /** + * Returns the name of the initializer method for a field. + * Uses "init" as prefix and "s" as suffix. + * + * @param fieldName the name of the field, not null. + * + * @return the initializer method name, not null. + */ + public static String getInitializerName(String fieldName) + { + String setterName = "init" + StringUtils.capitalize(fieldName) + "s"; + return setterName; + } +} Added: db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/FindHelper.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/FindHelper.java?rev=910600&view=auto ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/FindHelper.java (added) +++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/FindHelper.java Tue Feb 16 17:15:43 2010 @@ -0,0 +1,137 @@ +package org.apache.torque.templates.transformer.om; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.util.ArrayList; +import java.util.List; + +import org.apache.torque.gf.source.SourceElement; +import org.apache.torque.templates.TorqueSchemaAttributeName; +import org.apache.torque.templates.TorqueSchemaElementName; + +/** + * Utility class containing methods to find certain elements + * in the source tree. + * + * $Id: $ + */ +final class FindHelper +{ + /** + * Private constructor for utility class. + */ + private FindHelper() + { + } + + /** + * Finds a table in the database by name. + * + * @param database the database Source element, not null. + * @param tableName the table name, not null. + * + * @return the column's source element, or null if no matching table + * exists. + */ + public static SourceElement findTable( + SourceElement database, + String tableName) + { + if (!TorqueSchemaElementName.DATABASE.getName().equals( + database.getName())) + { + throw new IllegalArgumentException("Wrong source element " + + database.getName()); + } + for (SourceElement table : database.getChildren( + TorqueSchemaElementName.TABLE)) + { + if (tableName.equals(table.getAttribute( + TorqueSchemaAttributeName.NAME))) + { + return table; + } + } + return null; + } + + /** + * Finds a column in a table by name. + * + * @param table the table Source element, not null. + * @param columnName the column name, not null. + * + * @return the column's source element, or null if no matching column + * exists. + */ + public static SourceElement findColumn( + SourceElement table, + String columnName) + { + if (!TorqueSchemaElementName.TABLE.getName().equals( + table.getName())) + { + throw new IllegalArgumentException("Wrong source element " + + table.getName()); + } + for (SourceElement column : table.getChildren( + TorqueSchemaElementName.COLUMN)) + { + if (column.getAttribute(TorqueSchemaAttributeName.NAME) + .equals(columnName)) + { + return column; + } + } + return null; + } + + /** + * Finds all foreign key in one table that reference a certain other table. + * + * @param table the table containing the foreign keys to search, not null. + * @param tableName the name of the foreign table in the foreign Key. + * + * @return the foreign keys referencing the given table name. + */ + public static List findForeignKeyByReferencedTable( + SourceElement table, + String tableName) + { + List result = new ArrayList(); + if (!TorqueSchemaElementName.TABLE.getName().equals( + table.getName())) + { + throw new IllegalArgumentException("Wrong source element " + + table.getName()); + } + for (SourceElement foreignKey : table.getChildren( + TorqueSchemaElementName.FOREIGN_KEY)) + { + if (foreignKey.getAttribute( + TorqueSchemaAttributeName.FOREIGN_TABLE) + .equals(tableName)) + { + result.add(foreignKey); + } + } + return result; + } +} Added: db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/ForeignKeyChildAttributeName.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/ForeignKeyChildAttributeName.java?rev=910600&view=auto ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/ForeignKeyChildAttributeName.java (added) +++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/ForeignKeyChildAttributeName.java Tue Feb 16 17:15:43 2010 @@ -0,0 +1,92 @@ +package org.apache.torque.templates.transformer.om; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.torque.gf.source.SourceAttributeName; + +/** + * Contains the attribute names of the child elements for a foreign-key + * source element which are not defined in the Torque schema. + * These elements might be created in the transformation. + */ +public enum ForeignKeyChildAttributeName implements SourceAttributeName +{ + /** + * The name of the getter method in the database object + * which joins the other table. + */ + FOREIGN_FIELD_JOIN_GETTER("joinGetter"), + + /** + * The name of the getter method in the database object + * which joins all the other tables except the referenced table. + * TODO: is this used at all ? + */ + FOREIGN_FIELD_JOIN_ALL_EXCEPT_GETTER("joinAllExceptGetter"), + + /** + * The name of the peer class method which joins the other table. + */ + PEER_JOIN_SELECT_METHOD("peerJoinSelectMethod"), + + /** + * The name of the peer class method which joins all the other tables + * except the referenced table. + */ + PEER_JOIN_ALL_EXCEPT_SELECT_METHOD("peerJoinAllExceptSelectMethod"), + + /** + * The name of the cache field for the criteria with which the last + * collection was accessed. + */ + FOREIGN_COLUMN_CRITERIA_CACHE_FIELD("criteriaCacheField"), + + BEAN_GETTER("beanGetter"), + BEAN_SETTER("beanSetter"); + + /** The name of the source element, not null. */ + private String name; + + /** + * Constructor. + * + * @param name the name of the source element, not null. + */ + private ForeignKeyChildAttributeName(String name) + { + this.name = name; + } + + /** + * Returns the name of the referenced source element. + * + * @return the name of the referenced source element. + */ + public String getName() + { + return name; + } + + @Override + public String toString() + { + return name; + } +} Added: db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/ForeignKeyChildElementName.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/ForeignKeyChildElementName.java?rev=910600&view=auto ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/ForeignKeyChildElementName.java (added) +++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/ForeignKeyChildElementName.java Tue Feb 16 17:15:43 2010 @@ -0,0 +1,83 @@ +package org.apache.torque.templates.transformer.om; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.torque.gf.source.SourceElementName; + +/** + * Contains the child elements for a foreign-key source element which are not + * defined in the Torque schema. These elements might be created + * in the transformation. + */ +public enum ForeignKeyChildElementName implements SourceElementName +{ + /** + * The child element containing information about the field in the local + * database object containing the referenced foreign database object. + */ + LOCAL_FIELD("local-field"), + + /** + * The child element containing information about the field in the foreign + * database object containing the referenced local database object. + */ + FOREIGN_FIELD("foreign-field"), + + /** + * The child element containing information about the field in the local + * bean containing the referenced foreign bean. + */ + LOCAL_FIELD_IN_BEAN("local-field-in-bean"), + + /** + * The child element containing information about the field in the foreign + * bean containing the referenced local beans. + */ + FOREIGN_FIELD_IN_BEAN("foreign-field-in-bean"); + + /** The name of the source element, not null. */ + private String name; + + /** + * Constructor. + * + * @param name the name of the source element, not null. + */ + private ForeignKeyChildElementName(String name) + { + this.name = name; + } + + /** + * Returns the name of the referenced source element. + * + * @return the name of the referenced source element. + */ + public String getName() + { + return name; + } + + @Override + public String toString() + { + return name; + } +} Added: db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/JavaFieldAttributeName.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/JavaFieldAttributeName.java?rev=910600&view=auto ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/JavaFieldAttributeName.java (added) +++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/JavaFieldAttributeName.java Tue Feb 16 17:15:43 2010 @@ -0,0 +1,149 @@ +package org.apache.torque.templates.transformer.om; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.torque.gf.source.SourceAttributeName; + +/** + * Defines the standard attributes for a source element representing + * a java field (i.e instance variable of a class). + */ +public enum JavaFieldAttributeName implements SourceAttributeName +{ + /** + * The name of the field + **/ + FIELD_NAME("field"), + + /** + * The type of the field + */ + FIELD_TYPE("fieldType"), + + /** + * The access modifier (e.g. "public" ...) for the field. + * Default is "private" + **/ + FIELD_ACCESS_MODIFIER("fieldAccessModifier"), + + /** + * In case the field is a collection: + * The type of the objects contained in the collection. + */ + FIELD_CONTAINED_TYPE("fieldContainedType"), + + /** + * The method name of the getter for the field. + */ + SETTER_NAME("setter"), + + /** + * The method name of the setter for the field. + */ + GETTER_NAME("getter"), + + /** + * In case the field must be initialized on first access, + * this contains the name of the init method. + */ + INITIALIZER_NAME("initializer"), + + /** + * In case the type cannot be instantiated (e.g. is an interface like List), + * this provides the type of the object the field is initialized with. + */ + INITIALIZER_TYPE("initializerType"), + + /** + * In case the field is a collection: + * The name of the method used to add one object to the collection. + */ + ADDER_NAME("adder"), + + /** + * The access modifier (e.g. "public" ...) for the getter method + **/ + GETTER_ACCESS_MODIFIER("getterAccessModifer"), + + /** + * The access modifier (e.g. "public" ...) for the setter method + **/ + SETTER_ACCESS_MODIFIER("setterAccessModifer"), + + /** + * The throws clause (excluding "throws " itself) for the getter method. + **/ + GETTER_THROWS("getterThrows"), + + /** + * The throws clause (excluding "throws " itself) for the setter method. + **/ + SETTER_THROWS("setterThrows"), + + /** + * The throws clause (excluding "throws " itself) for the initializer method. + **/ + INITIALIZER_THROWS("initializerThrows"), + + /** + * The throws clause (excluding "throws " itself) for the adder method. + **/ + ADDER_THROWS("adderThrows"), + + /** + * The default value for the field. + * This is the value the field is initialized with. + */ + DEFAULT_VALUE("defaultValue"), + + /** + * The description of the field. + */ + DESCRIPTION("description"); + + /** The name of the source element attribute, not null. */ + private String name; + + /** + * Constructor. + * + * @param name the name of the source element attribute, not null. + */ + private JavaFieldAttributeName(String name) + { + this.name = name; + } + + /** + * Returns the name of the referenced source element attribute. + * + * @return the name of the referenced source element attribute. + */ + public String getName() + { + return name; + } + + @Override + public String toString() + { + return name; + } +} Added: db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMColumnTransformer.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMColumnTransformer.java?rev=910600&view=auto ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMColumnTransformer.java (added) +++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMColumnTransformer.java Tue Feb 16 17:15:43 2010 @@ -0,0 +1,637 @@ +package org.apache.torque.templates.transformer.om; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.commons.lang.StringUtils; +import org.apache.torque.gf.control.ControllerState; +import org.apache.torque.gf.processor.string.Camelbacker; +import org.apache.torque.gf.source.SourceElement; +import org.apache.torque.gf.source.transform.SourceTransformerException; +import org.apache.torque.templates.TorqueSchemaAttributeName; +import org.apache.torque.templates.TorqueSchemaElementName; +import org.apache.torque.templates.TorqueSchemaJavaType; +import org.apache.torque.templates.transformer.SchemaTypeHelper; +import org.apache.torque.templates.typemapping.JavaType; +import org.apache.torque.templates.typemapping.SchemaType; +import org.apache.torque.templates.typemapping.TypeMap; +import org.apache.torque.templates.typemapping.VillageMethod; + +/** + * Sets the class names and packages for the OM model. + */ +public class OMColumnTransformer +{ + private static Camelbacker camelbacker = new Camelbacker(); + + private static OMInheritanceTransformer inheritanceTransformer + = new OMInheritanceTransformer(); + + public void transform( + SourceElement columnElement, + ControllerState controllerState, + int columnPosition) + throws SourceTransformerException + { + checkElementName(columnElement); + checkColumnNameExists(columnElement); + + columnElement.setAttribute( + ColumnAttributeName.POSITION, + columnPosition); + setAttributeDefaultValues(columnElement); + + SchemaType schemaType = SchemaTypeHelper.getSchemaType( + columnElement, + controllerState); + + JavaType fieldJavaType = getFieldJavaType(columnElement, schemaType); + columnElement.setAttribute( + JavaFieldAttributeName.FIELD_TYPE, + fieldJavaType.getFullClassName()); + + + setPrimitiveTypeAttribute(columnElement, fieldJavaType); + setNumberTypeAttribute(columnElement, fieldJavaType); + setJavaNameAttribute(columnElement); + setFieldNameAttribute(columnElement); + setPeerColumnNameAttribute(columnElement); + setQualifiedColumnNameAttribute(columnElement); + setGetterNameAttribute(columnElement, fieldJavaType, controllerState); + setSetterNameAttribute(columnElement); + setAccessModifierAttributes(columnElement); + setDefaultValueAttribute(columnElement, fieldJavaType); + setVillageMethodAttribute(columnElement, fieldJavaType, schemaType); + setSampleObjectAttribute(columnElement, schemaType); + + for (SourceElement inheritanceElement : columnElement.getChildren( + TorqueSchemaElementName.INHERITANCE.getName())) + { + inheritanceTransformer.transform( + inheritanceElement, + controllerState); + } + } + + /** + * Sets default values for attributes of the column element + * if the attribute is not set. + * The following attributes are checked: + * autoIncrement, protected, inheritance, required, primaryKey + * + * @param columnElement the column element, not null. + */ + private void setAttributeDefaultValues(SourceElement columnElement) + { + // set autoincrement attribute + // TODO: remove once the autoincrement attribute is removed + // from the Torque schema. + if (columnElement.getAttribute( + TorqueSchemaAttributeName.AUTO_INCREMENT) + == null) + { + columnElement.setAttribute( + TorqueSchemaAttributeName.AUTO_INCREMENT, + Boolean.FALSE.toString()); + } + + // set autoincrement attribute + if (columnElement.getAttribute( + TorqueSchemaAttributeName.PROTECTED) + == null) + { + columnElement.setAttribute( + TorqueSchemaAttributeName.PROTECTED, + Boolean.FALSE.toString()); + } + + // set inheritance attribute + if (columnElement.getAttribute( + TorqueSchemaAttributeName.INHERITANCE) + == null) + { + columnElement.setAttribute( + TorqueSchemaAttributeName.INHERITANCE, + Boolean.FALSE.toString()); + } + + Object required = columnElement.getAttribute( + TorqueSchemaAttributeName.REQUIRED); + if (required == null) + { + columnElement.setAttribute( + TorqueSchemaAttributeName.REQUIRED, + Boolean.FALSE.toString()); + } + + Object primaryKey = columnElement.getAttribute( + TorqueSchemaAttributeName.PRIMARY_KEY); + if (primaryKey == null) + { + columnElement.setAttribute( + TorqueSchemaAttributeName.PRIMARY_KEY, + Boolean.FALSE.toString()); + } + } + + /** + * Sets the attributes getterAccessModifer and setterAccessModifer + * on the column element. + * + * @param columnElement the column element, not null. + */ + private void setAccessModifierAttributes(SourceElement columnElement) + { + boolean isProtected = "true".equals( + columnElement.getAttribute( + TorqueSchemaAttributeName.PROTECTED)); + + String accessModifier; + if (isProtected) + { + accessModifier = "protected"; + } + else + { + accessModifier = "public"; + } + columnElement.setAttribute( + JavaFieldAttributeName.GETTER_ACCESS_MODIFIER, + accessModifier); + columnElement.setAttribute( + JavaFieldAttributeName.SETTER_ACCESS_MODIFIER, + accessModifier); + } + + + /** + * Returns the java type of the field representing a database column. + * + * @param columnElement the column element, not null. + * @param schemaType the schema type, not null. + * + * @return the java type of the column + */ + protected JavaType getFieldJavaType( + SourceElement columnElement, + SchemaType schemaType) + throws SourceTransformerException + { + JavaType result; + String javaType = (String) columnElement.getAttribute( + TorqueSchemaAttributeName.JAVA_TYPE); + if (TorqueSchemaJavaType.OBJECT.getValue().equals(javaType)) + { + result = TypeMap.getJavaObjectType(schemaType); + } + else if (TorqueSchemaJavaType.PRIMITIVE.getValue().equals(javaType) + || javaType == null) + { + result = TypeMap.getJavaPrimitiveType(schemaType); + } + else + { + String columnName = (String) columnElement.getAttribute( + TorqueSchemaAttributeName.NAME); + throw new SourceTransformerException("Unknown javaType " + + javaType + + " in column " + + columnName); + } + return result; + } + + /** + * Checks that the name of the column element is correct. + * + * @param columnElement the column element, not null. + * + * @throws IllegalArgumentException if the element name is wrong. + */ + protected void checkElementName(SourceElement columnElement) + { + if (!TorqueSchemaElementName.COLUMN.getName().equals( + columnElement.getName())) + { + throw new IllegalArgumentException("Illegal element Name " + + columnElement.getName()); + } + } + + /** + * Checks that the name attribute exists on the column element. + * + * @param columnElement the column element, not null. + * + * @throws SourceTransformerException if the name attribute does not exist. + */ + protected void checkColumnNameExists(SourceElement columnElement) + throws SourceTransformerException + { + String columnName + = (String) columnElement.getAttribute( + TorqueSchemaAttributeName.NAME); + if (columnName == null) + { + throw new SourceTransformerException("The attribute " + + TorqueSchemaAttributeName.NAME.getName() + + " on element " + + columnElement.getName() + + " is null"); + } + } + + /** + * Sets the javaName attribute of the column element if it is not + * already set. + * + * @param columnElement the column element, not null. + */ + protected void setJavaNameAttribute(SourceElement columnElement) + { + if (columnElement.getAttribute(TorqueSchemaAttributeName.JAVA_NAME) + != null) + { + return; + } + String columnName = (String) columnElement.getAttribute( + TorqueSchemaAttributeName.NAME); + String javaName = camelbacker.process(columnName); + columnElement.setAttribute( + TorqueSchemaAttributeName.JAVA_NAME, + javaName); + } + + /** + * Sets the fieldName attribute of the column element if it is not + * already set. + * The javaName attribute of the column must be set. + * + * @param columnElement the column element, not null. + */ + protected void setFieldNameAttribute(SourceElement columnElement) + { + if (columnElement.getAttribute(JavaFieldAttributeName.FIELD_NAME) + != null) + { + return; + } + String javaName = (String) columnElement.getAttribute( + TorqueSchemaAttributeName.JAVA_NAME); + String fieldName = StringUtils.uncapitalize(javaName); + columnElement.setAttribute( + JavaFieldAttributeName.FIELD_NAME, + fieldName); + } + + /** + * Sets the peerColumnName attribute of the column element if it is not + * already set. + * + * @param columnElement the column element, not null. + */ + protected void setPeerColumnNameAttribute(SourceElement columnElement) + { + if (columnElement.getAttribute(ColumnAttributeName.PEER_COLUMN_NAME) + != null) + { + return; + } + String columnName = (String) columnElement.getAttribute( + TorqueSchemaAttributeName.NAME); + String peerColumnName = columnName.toUpperCase(); + columnElement.setAttribute( + ColumnAttributeName.PEER_COLUMN_NAME, + peerColumnName); + } + + /** + * Sets the qualifiedColumnName attribute of the column element + * if it is not already set. + * + * @param columnElement the column element, not null. + */ + protected void setQualifiedColumnNameAttribute(SourceElement columnElement) + { + if (columnElement.getAttribute(ColumnAttributeName.QUALIFIED_COLUMN_NAME) + != null) + { + return; + } + String tableName = (String) columnElement.getParent().getAttribute( + TorqueSchemaAttributeName.NAME); + String columnName = (String) columnElement.getAttribute( + TorqueSchemaAttributeName.NAME); + String qualifiedColumnName + = tableName + "." + columnName; + columnElement.setAttribute( + ColumnAttributeName.QUALIFIED_COLUMN_NAME, + qualifiedColumnName); + } + + /** + * Sets the getterName attribute of the column element + * if it is not already set. + * The fieldName attribute of the column element must already be set. + * + * @param columnElement the column element, not null. + * @param javaType the java type of the column, not null. + * @param controllerState the controller state, not null. + */ + protected void setGetterNameAttribute( + SourceElement columnElement, + JavaType javaType, + ControllerState controllerState) + { + if (columnElement.getAttribute(JavaFieldAttributeName.GETTER_NAME) + != null) + { + return; + } + String fieldName = (String) columnElement.getAttribute( + JavaFieldAttributeName.FIELD_NAME); + String getterName = FieldHelper.getGetterName( + fieldName, + javaType.getFullClassName(), + controllerState); + columnElement.setAttribute( + JavaFieldAttributeName.GETTER_NAME, + getterName); + } + + /** + * Sets the setterName attribute of the column element + * if it is not already set. + * The fieldName attribute of the column element must already be set. + * + * @param columnElement the column element, not null. + */ + protected void setSetterNameAttribute(SourceElement columnElement) + { + if (columnElement.getAttribute(JavaFieldAttributeName.SETTER_NAME) + != null) + { + return; + } + String fieldName = (String) columnElement.getAttribute( + JavaFieldAttributeName.FIELD_NAME); + String setterName = FieldHelper.getSetterName(fieldName); + columnElement.setAttribute( + JavaFieldAttributeName.SETTER_NAME, + setterName); + } + + /** + * Sets the primitiveType attribute of the column element + * if it is not already set. + * + * @param columnElement the column element, not null. + * @param javaType the type of the java field corresponding to the + * column, not null. + */ + protected void setPrimitiveTypeAttribute( + SourceElement columnElement, + JavaType javaType) + { + if (columnElement.getAttribute(ColumnAttributeName.PRIMITIVE_TYPE) + != null) + { + return; + } + boolean primitiveFieldType = javaType.isPrimitive(); + columnElement.setAttribute( + ColumnAttributeName.PRIMITIVE_TYPE, + Boolean.toString(primitiveFieldType)); + } + + /** + * Sets the numberType attribute of the column element + * if it is not already set. + * + * @param columnElement the column element, not null. + * @param javaType the type of the java field corresponding to the + * column, not null. + */ + protected void setNumberTypeAttribute( + SourceElement columnElement, + JavaType javaType) + { + if (columnElement.getAttribute(ColumnAttributeName.NUMBER_TYPE) + != null) + { + return; + } + boolean numberFieldType = javaType.isNumber(); + columnElement.setAttribute( + ColumnAttributeName.NUMBER_TYPE, + Boolean.toString(numberFieldType)); + } + + + /** + * Sets the defaultValue attribute of the column element + * if it is not already set. + * + * @param columnElement the column element, not null. + * @param javaType the type of the java field corresponding to the + * column, not null. + * + * @throws SourceTransformerException if an unknown primitive type + * is encountered + */ + protected void setDefaultValueAttribute( + SourceElement columnElement, + JavaType javaType) + throws SourceTransformerException + { + if (columnElement.getAttribute(JavaFieldAttributeName.DEFAULT_VALUE) + != null) + { + return; + } + String defaultValue = (String) columnElement.getAttribute( + TorqueSchemaAttributeName.DEFAULT.getName()); + boolean primitiveFieldType = javaType.isPrimitive(); + + String fieldDefaultValue; + if (defaultValue != null) + { + if (JavaType.BOOLEAN_PRIMITIVE == javaType) + { + if ("Y".equals(defaultValue) + || "1".equals(defaultValue) + || "true".equalsIgnoreCase(defaultValue)) + { + fieldDefaultValue = "true"; + } + else + { + fieldDefaultValue = "false"; + } + } + else if (JavaType.BOOLEAN_OBJECT == javaType) + { + if ("Y".equals(defaultValue) + || "1".equals(defaultValue) + || "true".equalsIgnoreCase(defaultValue)) + { + fieldDefaultValue = "Boolean.TRUE"; + } + else + { + fieldDefaultValue = "Boolean.FALSE"; + } + } + else if (JavaType.STRING == javaType) + { + fieldDefaultValue = "\"" + defaultValue + "\""; + } + else if (JavaType.SHORT_OBJECT == javaType) + { + // The following is better than casting with (short) + // because a range check is performed, + fieldDefaultValue = "new Short(\"" + defaultValue + "\")"; + } + else if (JavaType.BYTE_OBJECT == javaType) + { + // The following is better than casting with (byte) + // because a range check is performed, + fieldDefaultValue = "new Byte(\"" + defaultValue + "\")"; + } + else if (primitiveFieldType) + { + fieldDefaultValue = defaultValue; + } + else + { + fieldDefaultValue + = "new " + javaType.getFullClassName() + + "(" + defaultValue + ")"; + } + } + else + { + if (primitiveFieldType) + { + if (JavaType.BOOLEAN_PRIMITIVE == javaType) + { + fieldDefaultValue = "false"; + } + else if (JavaType.BYTE_PRIMITIVE == javaType) + { + fieldDefaultValue = "(byte) 0"; + } + else if (JavaType.SHORT_PRIMITIVE == javaType) + { + fieldDefaultValue = "(short) 0"; + } + else if (JavaType.INTEGER_PRIMITIVE == javaType) + { + fieldDefaultValue = "0"; + } + else if (JavaType.LONG_PRIMITIVE == javaType) + { + fieldDefaultValue = "0L"; + } + else if (JavaType.FLOAT_PRIMITIVE == javaType) + { + fieldDefaultValue = "0"; + } + else if (JavaType.DOUBLE_PRIMITIVE == javaType) + { + fieldDefaultValue = "0"; + } + else if (JavaType.CHAR_PRIMITIVE == javaType) + { + fieldDefaultValue = "'\0'"; + } + else + { + throw new SourceTransformerException( + "unknown primitive type" + javaType); + } + } + else + { + fieldDefaultValue = "null"; + } + } + columnElement.setAttribute( + JavaFieldAttributeName.DEFAULT_VALUE, + fieldDefaultValue); + } + + /** + * Sets the villageMethod attribute of the column element + * if it is not already set. + * + * @param columnElement the column element, not null. + * @param javaType the type of the java field corresponding to the + * column, not null. + * @param schemaType the schema type of the column, not null. + */ + protected void setVillageMethodAttribute( + SourceElement columnElement, + JavaType javaType, + SchemaType schemaType) + { + if (columnElement.getAttribute(ColumnAttributeName.VILLAGE_METHOD) + != null) + { + return; + } + VillageMethod villageMethod; + if (javaType.isPrimitive()) + { + villageMethod = TypeMap.getVillageMethod(schemaType); + } + else + { + villageMethod = TypeMap.getVillageObjectMethod(schemaType); + } + columnElement.setAttribute( + ColumnAttributeName.VILLAGE_METHOD, + villageMethod); + } + + + /** + * Sets the sampleObject attribute of the column element + * if it is not already set. + * + * @param columnElement the column element, not null. + * @param schemaType the schema type of the column, not null. + */ + protected void setSampleObjectAttribute( + SourceElement columnElement, + SchemaType schemaType) + { + if (columnElement.getAttribute(ColumnAttributeName.SAMPLE_OBJECT) + != null) + { + return; + } + + String sampleObject = TypeMap.getJavaObject(schemaType); + columnElement.setAttribute( + ColumnAttributeName.SAMPLE_OBJECT, + sampleObject); + } + +} --------------------------------------------------------------------- To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org For additional commands, e-mail: torque-dev-help@db.apache.org