1 package org.apache.torque.betwixt.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
76
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 }