View Javadoc

1   package org.apache.torque.templates.transformer.om;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import org.apache.torque.generator.control.ControllerState;
26  import org.apache.torque.generator.source.SourceElement;
27  import org.apache.torque.generator.source.transform.SourceTransformer;
28  import org.apache.torque.generator.source.transform.SourceTransformerException;
29  import org.apache.torque.templates.TemplateOptionName;
30  import org.apache.torque.templates.TorqueSchemaAttributeName;
31  import org.apache.torque.templates.TorqueSchemaElementName;
32  import org.apache.torque.templates.transformer.om.mapInit.DatabaseMapInitTransformer;
33  
34  /**
35   * Performs the transformations which are necessary to apply the OM templates
36   * to the source tree.
37   * This transformer performs the following actions:
38   * <ul>
39   *   <li>adds attributes to source elements</li>
40   *   <li>links elements to other source elements</li>
41   * </ul>
42   * No elements or attributes are deleted.
43   *
44   * This transformer calls the following other transformers on the source tree:
45   * <ul>
46   *   <li>IncludeSchemaTransformer on the root node</li>
47   *   <li>LoadExternalSchemaTransformer on the root node</li>
48   *   <li>OMTableAndViewTransformer on all tables and views</li>
49   *   <li>OMForeignKeyColumnTransformer on all columns</li>
50   *   <li>OMForeignKeyTransformer on all foreign keys (two passes)</li>
51   * <ul>
52   *
53   * $Id: OMTransformer.java 1345952 2012-06-04 13:18:12Z tfischer $
54   */
55  public class OMTransformer implements SourceTransformer
56  {
57      /**
58       * The transformer which adds references to the foreign keys referenced by
59       * this column or referencing this column.
60       *
61       * @see OMForeignKeyColumnTransformer
62       */
63      private static final OMForeignKeyColumnTransformer foreignKeyColumnTransformer
64          = new OMForeignKeyColumnTransformer();
65  
66      /**
67       * The transformer which adds references to the referenced and referencing
68       * columns, tables and fields to the foreign key.
69       *
70       * @see OMForeignKeyTransformer
71       */
72      private static final OMForeignKeyTransformer foreignKeyTransformer
73          = new OMForeignKeyTransformer();
74  
75      /**
76       * The transformer which adds transforms the table/view and column elements
77       * of the schema.
78       *
79       * @see OMTableAndViewTransformer
80       */
81      private static final OMTableAndViewTransformer tableOrViewTransformer;
82  
83      /**
84       * The transformer which loads the external schemata.
85       *
86       * @see LoadExternalSchemaTransformer
87       */
88      private static final SourceTransformer loadExternalSchemaTransformer
89              = new LoadExternalSchemaTransformer();
90  
91      /**
92       * The transformer which includes the included schemata.
93       *
94       * @see LoadExternalSchemaTransformer
95       */
96      private static final SourceTransformer includeSchemaTransformer
97              = new IncludeSchemaTransformer();
98  
99      static
100     {
101         try
102         {
103             tableOrViewTransformer = new OMTableAndViewTransformer();
104         }
105         catch (SourceTransformerException e)
106         {
107             throw new RuntimeException(e);
108         }
109     }
110 
111     /**
112      * Transforms the source tree so it can be used by the om templates.
113      *
114      * @param root the database root element of the source tree, not null.
115      * @param controllerState the controller state, not null.
116      *
117      * @throws SourceTransformerException if the transformation fails.
118      */
119     public SourceElement transform(
120             SourceElement root,
121             ControllerState controllerState)
122         throws SourceTransformerException
123     {
124         TemplateOptionName.checkRequiredOptions(
125                 controllerState,
126                 TemplateOptionName.OM_PACKAGE);
127 
128         setRootDatabaseNameAttribute(root);
129         DatabaseMapInitTransformer.setDatabaseMapInitClassNameAttributes(
130                 root,
131                 (String) root.getAttribute(TorqueSchemaAttributeName.NAME),
132                 controllerState);
133         // include included schemata
134         includeSchemaTransformer.transform(root, controllerState);
135         // load referenced external schemata
136         loadExternalSchemaTransformer.transform(root, controllerState);
137 
138         List<SourceElement> allTablesAndViews = new ArrayList<SourceElement>();
139         allTablesAndViews.addAll(
140                 root.getChild(DatabaseChildElementName.ALL_TABLES)
141                     .getChildren(TorqueSchemaElementName.TABLE));
142         allTablesAndViews.addAll(
143                 root.getChild(DatabaseChildElementName.ALL_VIEWS)
144                     .getChildren(TorqueSchemaElementName.VIEW));
145 
146         for (SourceElement tableOrViewElement : allTablesAndViews)
147         {
148             tableOrViewTransformer.transform(
149                     tableOrViewElement,
150                     controllerState);
151         }
152 
153         for (SourceElement tableOrViewElement : allTablesAndViews)
154         {
155             for (SourceElement columnElement : tableOrViewElement.getChildren(
156                     TorqueSchemaElementName.COLUMN))
157             {
158                 foreignKeyColumnTransformer.transform(
159                         columnElement,
160                         controllerState);
161             }
162             for (SourceElement foreignKeyElement
163                     : tableOrViewElement.getChildren(
164                         TorqueSchemaElementName.FOREIGN_KEY))
165             {
166                 foreignKeyTransformer.transform(
167                         foreignKeyElement,
168                         controllerState);
169             }
170         }
171 
172         for (SourceElement tableOrViewElement : allTablesAndViews)
173         {
174             for (SourceElement foreignKeyElement
175                     : tableOrViewElement.getChildren(
176                         TorqueSchemaElementName.FOREIGN_KEY))
177             {
178                 foreignKeyTransformer.transformSecondPass(
179                         foreignKeyElement,
180                         controllerState);
181             }
182         }
183 
184         return root;
185     }
186 
187     /**
188      * Sets the rootDatabaseName attribute of the database element
189      * to the database's name.
190      *
191      * @param databaseElement the database element, not null.
192      */
193     public static void setRootDatabaseNameAttribute(SourceElement databaseElement)
194     {
195         String databaseName = (String) databaseElement.getAttribute(
196                 TorqueSchemaAttributeName.NAME);
197         databaseElement.setAttribute(
198                 DatabaseAttributeName.ROOT_DATABASE_NAME,
199                 databaseName);
200     }
201 }