KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc > metadata > JDBCAuditMetaData


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb.plugins.cmp.jdbc.metadata;
23
24 import org.jboss.deployment.DeploymentException;
25 import org.jboss.metadata.MetaData;
26 import org.jboss.logging.Logger;
27 import org.w3c.dom.Element JavaDoc;
28
29 /**
30  * Audit field meta data
31  *
32  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>
33  * @version $Revision: 37459 $
34  */

35 public final class JDBCAuditMetaData
36 {
37    // Constants ---------------------------------------
38

39    // Attributes --------------------------------------
40

41    /** The created by principal field */
42    final private JDBCCMPFieldMetaData createdPrincipalField;
43
44    /** The created by time field */
45    final private JDBCCMPFieldMetaData createdTimeField;
46
47    /** The last update by principal field */
48    final private JDBCCMPFieldMetaData updatedPrincipalField;
49
50    /** The last update time time field */
51    final private JDBCCMPFieldMetaData updatedTimeField;
52
53    /** logger */
54    final private Logger log;
55
56    // Constructors ------------------------------------
57

58    /**
59     * Constructs audit metadata reading
60     * audit XML element
61     */

62    public JDBCAuditMetaData(JDBCEntityMetaData entityMetaData, Element JavaDoc element)
63       throws DeploymentException
64    {
65       log = Logger.getLogger(entityMetaData.getName());
66
67       Element JavaDoc workElement;
68
69       if ((workElement = MetaData.getOptionalChild(element, "created-by")) != null)
70       {
71          createdPrincipalField = constructAuditField(entityMetaData, workElement, "audit_created_by");
72
73          log.debug("created-by: " + createdPrincipalField);
74       }
75       else
76          createdPrincipalField = null;
77
78       if ((workElement = MetaData.getOptionalChild(element, "created-time")) != null)
79       {
80          createdTimeField = constructAuditField(entityMetaData, workElement, "audit_created_time");
81
82          log.debug("created-time: " + createdTimeField);
83       }
84       else
85          createdTimeField = null;
86
87       if ((workElement = MetaData.getOptionalChild(element, "updated-by")) != null)
88       {
89          updatedPrincipalField = constructAuditField(entityMetaData, workElement, "audit_updated_by");
90
91          log.debug("updated-by: " + updatedPrincipalField);
92       }
93       else
94          updatedPrincipalField = null;
95
96       if ((workElement = MetaData.getOptionalChild(element, "updated-time")) != null)
97       {
98          updatedTimeField = constructAuditField(entityMetaData, workElement, "audit_updated_time");
99
100          log.debug("updated-time: " + updatedTimeField);
101       }
102       else
103          updatedTimeField = null;
104    }
105
106    // Public ------------------------------------------
107

108    public JDBCCMPFieldMetaData getCreatedPrincipalField()
109    {
110       return createdPrincipalField;
111    }
112
113    public JDBCCMPFieldMetaData getCreatedTimeField()
114    {
115       return createdTimeField;
116    }
117
118    public JDBCCMPFieldMetaData getUpdatedPrincipalField()
119    {
120       return updatedPrincipalField;
121    }
122
123    public JDBCCMPFieldMetaData getUpdatedTimeField()
124    {
125       return updatedTimeField;
126    }
127
128    // Private -----------------------------------------
129

130    /**
131     * Constructs an audit locking field metadata from
132     * XML element
133     */

134    private static JDBCCMPFieldMetaData constructAuditField(
135       JDBCEntityMetaData entity,
136       Element JavaDoc element,
137       String JavaDoc defaultName)
138       throws DeploymentException
139    {
140       // field name
141
String JavaDoc fieldName = MetaData.getOptionalChildContent(element, "field-name");
142       if (fieldName == null || fieldName.trim().length() < 1)
143          fieldName = defaultName;
144
145       // column name
146
String JavaDoc columnName = MetaData.getOptionalChildContent(element, "column-name");
147       if (columnName == null || columnName.trim().length() < 1)
148          columnName = defaultName;
149
150       // field type
151
Class JavaDoc fieldType;
152       String JavaDoc fieldTypeStr = MetaData.getOptionalChildContent(element, "field-type");
153       if (fieldTypeStr != null)
154       {
155          try
156          {
157             fieldType = GetTCLAction.getContextClassLoader().loadClass(fieldTypeStr);
158          }
159          catch(ClassNotFoundException JavaDoc e)
160          {
161             throw new DeploymentException(
162                "Could not load field type for audit field "
163                + fieldName + ": " + fieldTypeStr);
164          }
165       }
166       else
167       {
168          if (defaultName.endsWith("by"))
169             fieldType = String JavaDoc.class;
170          else
171             fieldType = java.util.Date JavaDoc.class;
172       }
173
174       // JDBC/SQL Type
175
int jdbcType;
176       String JavaDoc sqlType;
177       String JavaDoc jdbcTypeName = MetaData.getOptionalChildContent(element, "jdbc-type");
178       if (jdbcTypeName != null)
179       {
180          jdbcType = JDBCMappingMetaData.getJdbcTypeFromName(jdbcTypeName);
181          sqlType = MetaData.getUniqueChildContent(element, "sql-type");
182       }
183       else
184       {
185          jdbcType = Integer.MIN_VALUE;
186          sqlType = null;
187       }
188
189       // Is the field exposed?
190
JDBCCMPFieldMetaData result = entity.getCMPFieldByName(fieldName);
191
192       if (result == null)
193          result = new JDBCCMPFieldMetaData(entity, fieldName, fieldType, columnName, jdbcType, sqlType);
194
195       return result;
196    }
197 }
198
Popular Tags