View Javadoc

1   package org.apache.torque.betwixt.util;
2   
3   /*
4    * Copyright 2001-2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.Hashtable;
20  
21  import org.apache.commons.betwixt.AttributeDescriptor;
22  import org.apache.commons.betwixt.ElementDescriptor;
23  import org.apache.commons.betwixt.strategy.ValueSuppressionStrategy;
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.torque.TorqueException;
27  import org.apache.torque.map.ColumnMap;
28  import org.apache.torque.map.TableMap;
29  import org.apache.torque.om.BaseObject;
30  
31  /***
32   * A Betwixt BeanWriter ValueSuppressionStategy that will filter out
33   * all primary key columns when the XML is written.  This can be useful
34   * if your primary keys are autogenerated ids and you want to create
35   * new primary ids on import.  It can also make for more "user readable"
36   * exports in some cases.  (Plus it's a good example of using this
37   * Betwixt filter).<P>
38   * 
39   * To use it, you need to add it to the BeanWriter's bindingConfiguration
40   * before writing the XML out.  E.g.: 
41   * <p>
42   * <code>
43   * beanWriter.getBindingConfiguration().setValueSuppressionStrategy( 
44   *                                           new SuppressPrimaryKeys() );
45   * </code>
46   * <p>
47   *
48   * @author <a href="mailto:greg.monroe@dukece.com">Greg Monroe</a>
49   */
50  public class SuppressPrimaryKeys extends ValueSuppressionStrategy 
51  {
52      static Log logger = LogFactory.getLog(SuppressPrimaryKeys.class);
53      
54      private Hashtable tableFieldLookup = new Hashtable();
55  
56      /***
57       * 
58       */
59      public SuppressPrimaryKeys() 
60      {
61          super();
62      }
63  
64      /***
65       * Do nothing but call DEFAULT implimentation because superclass defines
66       * this as abstract.
67       */
68      public boolean suppressAttribute(AttributeDescriptor attributeDescriptor, 
69                                       String value) 
70      {
71          return ValueSuppressionStrategy.DEFAULT.suppressAttribute(
72                              attributeDescriptor, value);
73      }
74  
75      /* (non-Javadoc)
76       * @see org.apache.commons.betwixt.strategy.ValueSuppressionStrategy#suppressElement(org.apache.commons.betwixt.ElementDescriptor, java.lang.String, java.lang.String, java.lang.String, java.lang.Object)
77       */
78      public boolean suppressElement(ElementDescriptor element, 
79                                     String namespaceUri, 
80                                     String localName, 
81                                     String qualifiedName, 
82                                     Object value) 
83      {
84          if ( value instanceof BaseObject ) 
85          {
86              try 
87              {
88                  TableMap tMap = ((BaseObject)value).getTableMap();
89                  String tableName = tMap.getName();
90                  Hashtable fieldLookup = 
91                              (Hashtable) tableFieldLookup.get(tableName);
92                  if ( fieldLookup == null ) 
93                  {
94                      ColumnMap[] cMaps = tMap.getColumns();
95                      fieldLookup = new Hashtable(cMaps.length);
96                      for ( int i = 0; i < cMaps.length; i++ ) 
97                      {
98                          ColumnMap cMap = cMaps[i];
99                          String cName = cMap.getColumnName().toLowerCase();
100                         fieldLookup.put(cName, 
101                                         new Boolean(cMap.isPrimaryKey()));
102                     }
103                     tableFieldLookup.put(tableName, fieldLookup );
104                 }
105                 Boolean primaryKey = 
106                             (Boolean) fieldLookup.get(localName.toLowerCase());
107                 if ( primaryKey != null ) 
108                 { 
109                     return primaryKey.booleanValue();
110                 }
111             }
112             catch ( TorqueException e ) 
113             {
114                 return super.suppressElement(element, namespaceUri, 
115                                              localName, qualifiedName,
116                                              value);
117             }
118         }
119         return super.suppressElement(element, namespaceUri, localName, qualifiedName,
120                 value);
121     }
122 }