Optionally check javax.validation annotations, Remove some Ldap code

This commit is contained in:
Robin Bygrave
2013-04-22 22:49:11 +12:00
parent 2567022d95
commit f4b02b0121
9 changed files with 36 additions and 371 deletions
@@ -92,7 +92,7 @@ public class BeanDescriptor<T> {
private final ConcurrentHashMap<String, BeanFkeyProperty> fkeyMap = new ConcurrentHashMap<String, BeanFkeyProperty>();
public enum EntityType {
ORM, EMBEDDED, SQL, META, LDAP, XMLELEMENT
ORM, EMBEDDED, SQL, META, XMLELEMENT
}
/**
@@ -1950,13 +1950,6 @@ public class BeanDescriptor<T> {
return EntityType.SQL.equals(entityType);
}
/**
* Return true if this an LDAP object.
*/
public boolean isLdapEntityType() {
return EntityType.LDAP.equals(entityType);
}
/**
* Return the base table. Only properties mapped to the base table are by
* default persisted.
@@ -29,12 +29,5 @@ public class BeanManager<T> {
public BeanDescriptor<T> getBeanDescriptor() {
return descriptor;
}
/**
* Return true if this bean type is an LDAP entity type.
*/
public boolean isLdapEntityType() {
return descriptor.isLdapEntityType();
}
}
@@ -603,8 +603,6 @@ public class DeployBeanProperty {
public String getElPlaceHolder(EntityType et) {
if (sqlFormulaSelect != null) {
return sqlFormulaSelect;
} else if (EntityType.LDAP.equals(et)){
return getDbColumn();
} else {
if (secondaryTableJoinPrefix != null){
return "${"+secondaryTableJoinPrefix+"}"+getDbColumn();
@@ -97,11 +97,13 @@ public class AnnotationAssocOnes extends AnnotationParser {
prop.setExtraWhere(where.clause());
}
NotNull notNull = get(prop, NotNull.class);
if (notNull != null) {
prop.setNullable(false);
// overrides optional attribute of ManyToOne etc
prop.getTableJoin().setType(TableJoin.JOIN);
if (validationAnnotations) {
NotNull notNull = get(prop, NotNull.class);
if (notNull != null) {
prop.setNullable(false);
// overrides optional attribute of ManyToOne etc
prop.getTableJoin().setType(TableJoin.JOIN);
}
}
// check for manually defined joins
@@ -35,7 +35,6 @@ import com.avaje.ebean.config.GlobalProperties;
import com.avaje.ebean.config.dbplatform.DbEncrypt;
import com.avaje.ebean.config.dbplatform.DbEncryptFunction;
import com.avaje.ebean.config.dbplatform.IdType;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor.EntityType;
import com.avaje.ebeaninternal.server.deploy.generatedproperty.GeneratedPropertyFactory;
import com.avaje.ebeaninternal.server.deploy.meta.DeployBeanProperty;
import com.avaje.ebeaninternal.server.deploy.meta.DeployBeanPropertyAssoc;
@@ -48,9 +47,6 @@ import com.avaje.ebeaninternal.server.type.ScalarType;
import com.avaje.ebeaninternal.server.type.ScalarTypeBytesBase;
import com.avaje.ebeaninternal.server.type.ScalarTypeBytesEncrypted;
import com.avaje.ebeaninternal.server.type.ScalarTypeEncryptedWrapper;
import com.avaje.ebeaninternal.server.type.ScalarTypeLdapBoolean;
import com.avaje.ebeaninternal.server.type.ScalarTypeLdapDate;
import com.avaje.ebeaninternal.server.type.ScalarTypeLdapTimestamp;
/**
* Read the field level deployment annotations.
@@ -69,7 +65,7 @@ public class AnnotationFields extends AnnotationParser {
if (GlobalProperties.getBoolean("ebean.lobEagerFetch", false)) {
defaultLobFetchType = FetchType.EAGER;
}
}
}
/**
@@ -135,16 +131,10 @@ public class AnnotationFields extends AnnotationParser {
}
if (prop.getDbColumn() == null) {
if (EntityType.LDAP.equals(descriptor.getEntityType())) {
// just use matching for now. Could consider an LdapNamingConvention
// later.
prop.setDbColumn(prop.getName());
} else {
// No @Column annotation or @Column.name() not set
// Use the NamingConvention to set the DB column name
String dbColumn = namingConvention.getColumnFromProperty(beanType, prop.getName());
prop.setDbColumn(dbColumn);
}
// No @Column annotation or @Column.name() not set
// Use the NamingConvention to set the DB column name
String dbColumn = namingConvention.getColumnFromProperty(beanType, prop.getName());
prop.setDbColumn(dbColumn);
}
GeneratedValue gen = get(prop, GeneratedValue.class);
@@ -201,17 +191,19 @@ public class AnnotationFields extends AnnotationParser {
generatedPropFactory.setUpdateTimestamp(prop);
}
NotNull notNull = get(prop, NotNull.class);
if (notNull != null) {
// explicitly specify a version column
prop.setNullable(false);
}
Size size = get(prop, Size.class);
if (size != null) {
if (size.max() < Integer.MAX_VALUE) {
if (validationAnnotations) {
NotNull notNull = get(prop, NotNull.class);
if (notNull != null) {
// explicitly specify a version column
prop.setDbLength(size.max());
prop.setNullable(false);
}
Size size = get(prop, Size.class);
if (size != null) {
if (size.max() < Integer.MAX_VALUE) {
// explicitly specify a version column
prop.setDbLength(size.max());
}
}
}
@@ -261,35 +253,6 @@ public class AnnotationFields extends AnnotationParser {
}
}
if (EntityType.LDAP.equals(descriptor.getEntityType())) {
adjustTypesForLdap(prop);
}
}
private static final ScalarTypeLdapBoolean LDAP_BOOLEAN_SCALARTYPE = new ScalarTypeLdapBoolean();
@SuppressWarnings({ "unchecked", "rawtypes" })
private void adjustTypesForLdap(DeployBeanProperty prop) {
Class<?> pt = prop.getPropertyType();
if (boolean.class.equals(pt) || Boolean.class.equals(pt)) {
prop.setScalarType(LDAP_BOOLEAN_SCALARTYPE);
} else {
ScalarType<?> sqlScalarType = prop.getScalarType();
int sqlType = sqlScalarType.getJdbcType();
if (sqlType == Types.TIMESTAMP) {
// Use LDAP Timestamp String format
prop.setScalarType(new ScalarTypeLdapTimestamp(sqlScalarType));
} else if (sqlType == Types.DATE) {
// Use LDAP Timestamp String format
prop.setScalarType(new ScalarTypeLdapDate(sqlScalarType));
} else {
// Just using string parsing for all other types
}
}
}
private void setEncryption(DeployBeanProperty prop, boolean dbEncString, int dbLen) {
@@ -16,11 +16,22 @@ public abstract class AnnotationParser extends AnnotationBase {
protected final Class<?> beanType;
protected boolean validationAnnotations;
public AnnotationParser(DeployBeanInfo<?> info) {
super(info.getUtil());
this.info = info;
this.beanType = info.getDescriptor().getBeanType();
this.descriptor = info.getDescriptor();
try {
Class.forName("javax.validation.constraints.NotNull");
validationAnnotations = true;
} catch (ClassNotFoundException e) {
// javax.validation not in the classpath so don't
// check for NotNull and Size
validationAnnotations = false;
}
}
/**
@@ -1,13 +0,0 @@
package com.avaje.ebeaninternal.server.type;
/**
* ScalarType for LDAP Boolean.
*/
public class ScalarTypeLdapBoolean extends ScalarTypeBoolean.StringBoolean {
public ScalarTypeLdapBoolean() {
super("TRUE", "FALSE");
}
}
@@ -1,141 +0,0 @@
package com.avaje.ebeaninternal.server.type;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Types;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.persistence.PersistenceException;
import com.avaje.ebean.text.json.JsonValueAdapter;
import com.avaje.ebeaninternal.server.text.json.WriteJsonBuffer;
/**
* Wrapper type that wraps all java.sql.Date types for LDAP.
*
* @author rbygrave
*/
public class ScalarTypeLdapDate<T> implements ScalarType<T> {
private static final String timestampLDAPFormat = "yyyyMMddHHmmss'Z'";
private final ScalarType<T> baseType;
public ScalarTypeLdapDate(ScalarType<T> baseType) {
this.baseType = baseType;
}
public T toBeanType(Object value) {
if (value == null){
return null;
}
if (value instanceof String == false){
String msg = "Expecting a String type but got "+value.getClass()+" value["+value+"]";
throw new PersistenceException(msg);
}
try {
SimpleDateFormat sdf = new SimpleDateFormat(timestampLDAPFormat);
Date date = sdf.parse((String)value);
return baseType.parseDateTime(date.getTime());
} catch (Exception e) {
String msg = "Error parsing LDAP timestamp "+value;
throw new PersistenceException(msg, e);
}
}
public Object toJdbcType(Object value) {
if (value == null){
return null;
}
Object ts = baseType.toJdbcType(value);
if (ts instanceof java.sql.Date == false){
String msg = "Expecting a java.sql.Date type but got "+value.getClass()+" value["+value+"]";
throw new PersistenceException(msg);
}
java.sql.Date t = (java.sql.Date)ts;
SimpleDateFormat sdf = new SimpleDateFormat(timestampLDAPFormat);
return sdf.format(t);
}
public void bind(DataBind b, T value) throws SQLException {
baseType.bind(b, value);
}
public int getJdbcType() {
return Types.VARCHAR;
}
public int getLength() {
return baseType.getLength();
}
public Class<T> getType() {
return baseType.getType();
}
public boolean isDateTimeCapable() {
return baseType.isDateTimeCapable();
}
public boolean isJdbcNative() {
return false;
}
public void loadIgnore(DataReader dataReader) {
baseType.loadIgnore(dataReader);
}
public String format(Object v) {
return baseType.format(v);
}
public String formatValue(T t) {
return baseType.formatValue(t);
}
public T parse(String value) {
return baseType.parse(value);
}
public T parseDateTime(long systemTimeMillis) {
return baseType.parseDateTime(systemTimeMillis);
}
public T read(DataReader dataReader) throws SQLException {
return baseType.read(dataReader);
}
public void accumulateScalarTypes(String propName, CtCompoundTypeScalarList list) {
baseType.accumulateScalarTypes(propName, list);
}
public String jsonToString(T value, JsonValueAdapter ctx) {
return baseType.jsonToString(value, ctx);
}
public void jsonWrite(WriteJsonBuffer buffer, T value, JsonValueAdapter ctx) {
baseType.jsonWrite(buffer, value, ctx);
}
public T jsonFromString(String value, JsonValueAdapter ctx) {
return baseType.jsonFromString(value, ctx);
}
public Object readData(DataInput dataInput) throws IOException {
return baseType.readData(dataInput);
}
public void writeData(DataOutput dataOutput, Object v) throws IOException {
baseType.writeData(dataOutput, v);
}
}
@@ -1,141 +0,0 @@
package com.avaje.ebeaninternal.server.type;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.persistence.PersistenceException;
import com.avaje.ebean.text.json.JsonValueAdapter;
import com.avaje.ebeaninternal.server.text.json.WriteJsonBuffer;
/**
* Wrapper type that wraps all java.sql.Timestamp types for LDAP.
*
* @author rbygrave
*/
public class ScalarTypeLdapTimestamp<T> implements ScalarType<T> {
private static final String timestampLDAPFormat = "yyyyMMddHHmmss'Z'";
private final ScalarType<T> baseType;
public ScalarTypeLdapTimestamp(ScalarType<T> baseType) {
this.baseType = baseType;
}
public T toBeanType(Object value) {
if (value == null){
return null;
}
if (value instanceof String == false){
String msg = "Expecting a String type but got "+value.getClass()+" value["+value+"]";
throw new PersistenceException(msg);
}
try {
SimpleDateFormat sdf = new SimpleDateFormat(timestampLDAPFormat);
Date date = sdf.parse((String)value);
return baseType.parseDateTime(date.getTime());
} catch (Exception e) {
String msg = "Error parsing LDAP timestamp "+value;
throw new PersistenceException(msg, e);
}
}
public Object toJdbcType(Object value) {
if (value == null){
return null;
}
Object ts = baseType.toJdbcType(value);
if (ts instanceof java.sql.Timestamp == false){
String msg = "Expecting a Timestamp type but got "+value.getClass()+" value["+value+"]";
throw new PersistenceException(msg);
}
Timestamp t = (Timestamp)ts;
SimpleDateFormat sdf = new SimpleDateFormat(timestampLDAPFormat);
return sdf.format(t);
}
public void bind(DataBind b, T value) throws SQLException {
baseType.bind(b, value);
}
public int getJdbcType() {
return Types.VARCHAR;
}
public int getLength() {
return baseType.getLength();
}
public Class<T> getType() {
return baseType.getType();
}
public boolean isDateTimeCapable() {
return baseType.isDateTimeCapable();
}
public boolean isJdbcNative() {
return false;
}
public void loadIgnore(DataReader dataReader) {
baseType.loadIgnore(dataReader);
}
public String format(Object v) {
return baseType.format(v);
}
public String formatValue(T t) {
return baseType.formatValue(t);
}
public T parse(String value) {
return baseType.parse(value);
}
public T parseDateTime(long systemTimeMillis) {
return baseType.parseDateTime(systemTimeMillis);
}
public T read(DataReader dataReader) throws SQLException {
return baseType.read(dataReader);
}
public void accumulateScalarTypes(String propName, CtCompoundTypeScalarList list) {
baseType.accumulateScalarTypes(propName, list);
}
public String jsonToString(T value, JsonValueAdapter ctx) {
return baseType.jsonToString(value, ctx);
}
public void jsonWrite(WriteJsonBuffer buffer, T value, JsonValueAdapter ctx) {
baseType.jsonWrite(buffer, value, ctx);
}
public T jsonFromString(String value, JsonValueAdapter ctx) {
return baseType.jsonFromString(value, ctx);
}
public Object readData(DataInput dataInput) throws IOException {
return baseType.readData(dataInput);
}
public void writeData(DataOutput dataOutput, Object v) throws IOException {
baseType.writeData(dataOutput, v);
}
}