public virtual object GetIdValue(object value, EntityMetaData metaData) { try { if (metaData.IdMethod != null) { return(metaData.IdMethod.Invoke(value, null)); } else if (metaData.IdField != null) { return(metaData.IdField.GetValue(value)); } } catch (ArgumentException iae) { throw new ActivitiException("Illegal argument exception when getting value from id method/field on JPAEntity", iae); } //catch (Exception iae) //{ // throw new ActivitiException("Cannot access id method/field for JPA Entity", iae); //} catch (Exception ite) { throw new ActivitiException("Exception occurred while getting value from id field/method on JPAEntity: " + ite.InnerException.Message, ite.InnerException); } // Fall trough when no method and field is set throw new ActivitiException("Cannot get id from JPA Entity, no id method/field set"); }
public virtual object GetJPAEntity(string className, string idString) { Type entityClass = ReflectUtil.LoadClass(className); EntityMetaData metaData = GetEntityMetaData(entityClass); // Create primary key of right type object primaryKey = CreateId(metaData, idString); return(FindEntity(entityClass, primaryKey)); }
public virtual string GetJPAIdString(object value) { EntityMetaData metaData = GetEntityMetaData(value.GetType()); if (!metaData.JPAEntity) { throw new ActivitiIllegalArgumentException("Object is not a JPA Entity: class='" + value.GetType() + "', " + value); } object idValue = GetIdValue(value, metaData); return(GetIdString(idValue)); }
public virtual EntityMetaData GetEntityMetaData(Type clazz) { EntityMetaData metaData = classMetaDatamap[clazz.FullName]; if (metaData == null) { // Class not present in meta-data map, create metaData for it and // add metaData = ScanClass(clazz); classMetaDatamap[clazz.FullName] = metaData; } return(metaData); }
public virtual string GetJPAClassString(object value) { if (value == null) { throw new ActivitiIllegalArgumentException("null value cannot be saved"); } EntityMetaData metaData = GetEntityMetaData(value.GetType()); if (!metaData.JPAEntity) { throw new ActivitiIllegalArgumentException("Object is not a JPA Entity: class='" + value.GetType() + "', " + value); } // Extract the class from the Entity instance return(metaData.EntityClass.FullName); }
public virtual EntityMetaData ScanClass(Type clazz) { EntityMetaData metaData = new EntityMetaData(); // in case with JPA Enhancement // method should iterate over superclasses list // to find @Entity and @Id annotations while (clazz != null && !clazz.Equals(typeof(object))) { // Class should have @Entity annotation bool isEntity = IsEntityAnnotationPresent(clazz); if (isEntity) { metaData.EntityClass = clazz; metaData.IsEntity = true; // Try to find a field annotated with @Id FieldInfo idField = GetIdField(clazz); if (idField != null) { metaData.IdField = idField; } else { // Try to find a method annotated with @Id MethodInfo idMethod = GetIdMethod(clazz); if (idMethod != null) { metaData.IdMethod = idMethod; } else { throw new ActivitiException("Cannot find field or method with annotation @Id on class '" + clazz.FullName + "', only single-valued primary keys are supported on JPA-entities"); } } break; } clazz = clazz.BaseType; } return(metaData); }
public virtual object CreateId(EntityMetaData metaData, string @string) { Type type = metaData.IdType; // According to JPA-spec all primitive types (and wrappers) are // supported, String, util.Date, sql.Date, // BigDecimal and BigInteger if (type == typeof(long) || type == typeof(long)) { return(long.TryParse(@string, out var l) ? l : throw new ArgumentException()); } else if (type == typeof(string)) { return(@string); } else if (type == typeof(byte) || type == typeof(sbyte)) { return(sbyte.TryParse(@string, out var sb) ? sb : throw new ArgumentException()); } else if (type == typeof(short) || type == typeof(short)) { return(short.TryParse(@string, out var sh) ? sh : throw new ArgumentException()); } else if (type == typeof(int) || type == typeof(int)) { return(int.TryParse(@string, out var i) ? i : throw new ArgumentException()); } else if (type == typeof(float) || type == typeof(float)) { return(float.TryParse(@string, out var f) ? f : throw new ArgumentException()); } else if (type == typeof(double) || type == typeof(double)) { return(double.TryParse(@string, out var d) ? d : throw new ArgumentException()); } else if (type == typeof(char) || type == typeof(char)) { return(new char?(@string[0])); } else if (type == typeof(DateTime)) { long.TryParse(@string, out var ticks); return(new DateTime(ticks)); } else if (type == typeof(decimal)) { return(decimal.TryParse(@string, out var dec) ? dec : throw new ArgumentException()); } else if (type == typeof(BigInteger)) { return(long.TryParse(@string, out var bi) ? bi : throw new ArgumentException()); } else if (type == typeof(Guid)) { return(new Guid(@string)); } else { throw new ActivitiIllegalArgumentException("Unsupported Primary key type for JPA-Entity: " + type.FullName); } }