1 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 ; 28 29 35 public final class JDBCAuditMetaData 36 { 37 39 41 42 final private JDBCCMPFieldMetaData createdPrincipalField; 43 44 45 final private JDBCCMPFieldMetaData createdTimeField; 46 47 48 final private JDBCCMPFieldMetaData updatedPrincipalField; 49 50 51 final private JDBCCMPFieldMetaData updatedTimeField; 52 53 54 final private Logger log; 55 56 58 62 public JDBCAuditMetaData(JDBCEntityMetaData entityMetaData, Element element) 63 throws DeploymentException 64 { 65 log = Logger.getLogger(entityMetaData.getName()); 66 67 Element 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 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 130 134 private static JDBCCMPFieldMetaData constructAuditField( 135 JDBCEntityMetaData entity, 136 Element element, 137 String defaultName) 138 throws DeploymentException 139 { 140 String fieldName = MetaData.getOptionalChildContent(element, "field-name"); 142 if (fieldName == null || fieldName.trim().length() < 1) 143 fieldName = defaultName; 144 145 String columnName = MetaData.getOptionalChildContent(element, "column-name"); 147 if (columnName == null || columnName.trim().length() < 1) 148 columnName = defaultName; 149 150 Class fieldType; 152 String fieldTypeStr = MetaData.getOptionalChildContent(element, "field-type"); 153 if (fieldTypeStr != null) 154 { 155 try 156 { 157 fieldType = GetTCLAction.getContextClassLoader().loadClass(fieldTypeStr); 158 } 159 catch(ClassNotFoundException 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 .class; 170 else 171 fieldType = java.util.Date .class; 172 } 173 174 int jdbcType; 176 String sqlType; 177 String 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 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 |