001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb.plugins.cmp.jdbc.metadata;
023:
024: import org.jboss.deployment.DeploymentException;
025: import org.jboss.metadata.MetaData;
026: import org.jboss.logging.Logger;
027: import org.w3c.dom.Element;
028:
029: /**
030: * Audit field meta data
031: *
032: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>
033: * @version $Revision: 57209 $
034: */
035: public final class JDBCAuditMetaData {
036: // Constants ---------------------------------------
037:
038: // Attributes --------------------------------------
039:
040: /** The created by principal field */
041: final private JDBCCMPFieldMetaData createdPrincipalField;
042:
043: /** The created by time field */
044: final private JDBCCMPFieldMetaData createdTimeField;
045:
046: /** The last update by principal field */
047: final private JDBCCMPFieldMetaData updatedPrincipalField;
048:
049: /** The last update time time field */
050: final private JDBCCMPFieldMetaData updatedTimeField;
051:
052: /** logger */
053: final private Logger log;
054:
055: // Constructors ------------------------------------
056:
057: /**
058: * Constructs audit metadata reading
059: * audit XML element
060: */
061: public JDBCAuditMetaData(JDBCEntityMetaData entityMetaData,
062: Element element) throws DeploymentException {
063: log = Logger.getLogger(entityMetaData.getName());
064:
065: Element workElement;
066:
067: if ((workElement = MetaData.getOptionalChild(element,
068: "created-by")) != null) {
069: createdPrincipalField = constructAuditField(entityMetaData,
070: workElement, "audit_created_by");
071:
072: log.debug("created-by: " + createdPrincipalField);
073: } else
074: createdPrincipalField = null;
075:
076: if ((workElement = MetaData.getOptionalChild(element,
077: "created-time")) != null) {
078: createdTimeField = constructAuditField(entityMetaData,
079: workElement, "audit_created_time");
080:
081: log.debug("created-time: " + createdTimeField);
082: } else
083: createdTimeField = null;
084:
085: if ((workElement = MetaData.getOptionalChild(element,
086: "updated-by")) != null) {
087: updatedPrincipalField = constructAuditField(entityMetaData,
088: workElement, "audit_updated_by");
089:
090: log.debug("updated-by: " + updatedPrincipalField);
091: } else
092: updatedPrincipalField = null;
093:
094: if ((workElement = MetaData.getOptionalChild(element,
095: "updated-time")) != null) {
096: updatedTimeField = constructAuditField(entityMetaData,
097: workElement, "audit_updated_time");
098:
099: log.debug("updated-time: " + updatedTimeField);
100: } else
101: updatedTimeField = null;
102: }
103:
104: // Public ------------------------------------------
105:
106: public JDBCCMPFieldMetaData getCreatedPrincipalField() {
107: return createdPrincipalField;
108: }
109:
110: public JDBCCMPFieldMetaData getCreatedTimeField() {
111: return createdTimeField;
112: }
113:
114: public JDBCCMPFieldMetaData getUpdatedPrincipalField() {
115: return updatedPrincipalField;
116: }
117:
118: public JDBCCMPFieldMetaData getUpdatedTimeField() {
119: return updatedTimeField;
120: }
121:
122: // Private -----------------------------------------
123:
124: /**
125: * Constructs an audit locking field metadata from
126: * XML element
127: */
128: private static JDBCCMPFieldMetaData constructAuditField(
129: JDBCEntityMetaData entity, Element element,
130: String defaultName) throws DeploymentException {
131: // field name
132: String fieldName = MetaData.getOptionalChildContent(element,
133: "field-name");
134: if (fieldName == null || fieldName.trim().length() < 1)
135: fieldName = defaultName;
136:
137: // column name
138: String columnName = MetaData.getOptionalChildContent(element,
139: "column-name");
140: if (columnName == null || columnName.trim().length() < 1)
141: columnName = defaultName;
142:
143: // field type
144: Class fieldType;
145: String fieldTypeStr = MetaData.getOptionalChildContent(element,
146: "field-type");
147: if (fieldTypeStr != null) {
148: try {
149: fieldType = GetTCLAction.getContextClassLoader()
150: .loadClass(fieldTypeStr);
151: } catch (ClassNotFoundException e) {
152: throw new DeploymentException(
153: "Could not load field type for audit field "
154: + fieldName + ": " + fieldTypeStr);
155: }
156: } else {
157: if (defaultName.endsWith("by"))
158: fieldType = String.class;
159: else
160: fieldType = java.util.Date.class;
161: }
162:
163: // JDBC/SQL Type
164: int jdbcType;
165: String sqlType;
166: String jdbcTypeName = MetaData.getOptionalChildContent(element,
167: "jdbc-type");
168: if (jdbcTypeName != null) {
169: jdbcType = JDBCMappingMetaData
170: .getJdbcTypeFromName(jdbcTypeName);
171: sqlType = MetaData.getUniqueChildContent(element,
172: "sql-type");
173: } else {
174: jdbcType = Integer.MIN_VALUE;
175: sqlType = null;
176: }
177:
178: // Is the field exposed?
179: JDBCCMPFieldMetaData result = entity
180: .getCMPFieldByName(fieldName);
181:
182: if (result == null)
183: result = new JDBCCMPFieldMetaData(entity, fieldName,
184: fieldType, columnName, jdbcType, sqlType);
185:
186: return result;
187: }
188: }
|