use of com.querydsl.codegen.Property in project querydsl by querydsl.
the class HibernateDomainExporter method handleProperty.
private void handleProperty(EntityType entityType, Class<?> cl, org.hibernate.mapping.Property p) throws NoSuchMethodException, ClassNotFoundException {
if (p.isBackRef()) {
return;
}
Class<?> clazz = Object.class;
try {
clazz = p.getType().getReturnedClass();
} catch (MappingException e) {
// ignore
}
Type propertyType = getType(cl, clazz, p.getName());
try {
propertyType = getPropertyType(p, propertyType);
} catch (MappingException e) {
// ignore
}
AnnotatedElement annotated = getAnnotatedElement(cl, p.getName());
propertyType = getTypeOverride(propertyType, annotated);
if (propertyType == null) {
return;
}
if (p.isComposite()) {
EntityType embeddedType = createEmbeddableType(propertyType);
Iterator<?> properties = ((Component) p.getValue()).getPropertyIterator();
while (properties.hasNext()) {
handleProperty(embeddedType, embeddedType.getJavaClass(), (org.hibernate.mapping.Property) properties.next());
}
propertyType = embeddedType;
} else if (propertyType.getCategory() == TypeCategory.ENTITY || p.getValue() instanceof ManyToOne) {
propertyType = createEntityType(propertyType);
} else if (propertyType.getCategory() == TypeCategory.CUSTOM) {
propertyType = createEmbeddableType(propertyType);
} else if (p.getValue() instanceof org.hibernate.mapping.Collection) {
org.hibernate.mapping.Collection collection = (org.hibernate.mapping.Collection) p.getValue();
if (collection.getElement() instanceof OneToMany) {
String entityName = ((OneToMany) collection.getElement()).getReferencedEntityName();
if (entityName != null) {
if (collection.isMap()) {
Type keyType = typeFactory.get(Class.forName(propertyType.getParameters().get(0).getFullName()));
Type valueType = typeFactory.get(Class.forName(entityName));
propertyType = new SimpleType(propertyType, normalize(propertyType.getParameters().get(0), keyType), normalize(propertyType.getParameters().get(1), valueType));
} else {
Type componentType = typeFactory.get(Class.forName(entityName));
propertyType = new SimpleType(propertyType, normalize(propertyType.getParameters().get(0), componentType));
}
}
} else if (collection.getElement() instanceof Component) {
Component component = (Component) collection.getElement();
Class<?> embeddedClass = Class.forName(component.getComponentClassName());
EntityType embeddedType = createEmbeddableType(embeddedClass);
Iterator<?> properties = component.getPropertyIterator();
while (properties.hasNext()) {
handleProperty(embeddedType, embeddedClass, (org.hibernate.mapping.Property) properties.next());
}
}
}
Property property = createProperty(entityType, p.getName(), propertyType, annotated);
entityType.addProperty(property);
}
use of com.querydsl.codegen.Property in project querydsl by querydsl.
the class TypeElementHandler method toProperty.
private Property toProperty(EntityType entityType, String name, TypeMirror type, Annotations annotations) {
// type
Type propertyType = typeFactory.getType(type, true);
if (annotations.isAnnotationPresent(QueryType.class)) {
PropertyType propertyTypeAnn = annotations.getAnnotation(QueryType.class).value();
if (propertyTypeAnn != PropertyType.NONE) {
TypeCategory typeCategory = TypeCategory.valueOf(annotations.getAnnotation(QueryType.class).value().name());
if (typeCategory == null) {
return null;
}
propertyType = propertyType.as(typeCategory);
} else {
return null;
}
}
// inits
List<String> inits = Collections.emptyList();
if (annotations.isAnnotationPresent(QueryInit.class)) {
inits = Arrays.asList(annotations.getAnnotation(QueryInit.class).value());
}
return new Property(entityType, name, propertyType, inits);
}
use of com.querydsl.codegen.Property in project querydsl by querydsl.
the class ExtendedBeanSerializer method addToString.
@Override
protected void addToString(EntityType model, CodeWriter writer) throws IOException {
Collection<PrimaryKeyData> primaryKeys = (Collection<PrimaryKeyData>) model.getData().get(PrimaryKeyData.class);
if (primaryKeys == null || primaryKeys.isEmpty()) {
super.addToString(model, writer);
return;
}
StringBuilder toString = new StringBuilder();
Map<String, Property> columnToProperty = new HashMap<String, Property>();
for (Property property : model.getProperties()) {
columnToProperty.put(property.getAnnotation(Column.class).value(), property);
}
for (PrimaryKeyData pk : primaryKeys) {
for (String column : pk.getColumns()) {
Property property = columnToProperty.get(column);
String propName = property.getEscapedName();
if (toString.length() > 0) {
toString.append("+ \";\" + ");
} else {
toString.append("\"" + model.getSimpleName() + "#\" + ");
}
toString.append(propName);
}
}
// toString
writer.annotation(Override.class);
writer.beginPublicMethod(Types.STRING, "toString");
// writer.line("if (", anyColumnIsNull + ") {");
// writer.line(" return super.toString();");
// writer.line("}");
writer.line("return ", toString + ";");
writer.end();
}
use of com.querydsl.codegen.Property in project querydsl by querydsl.
the class ExtendedBeanSerializer method bodyEnd.
@SuppressWarnings("unchecked")
@Override
protected void bodyEnd(EntityType model, CodeWriter writer) throws IOException {
Collection<PrimaryKeyData> primaryKeys = (Collection<PrimaryKeyData>) model.getData().get(PrimaryKeyData.class);
if (primaryKeys == null || primaryKeys.isEmpty()) {
return;
}
Map<String, Property> columnToProperty = new HashMap<String, Property>();
for (Property property : model.getProperties()) {
columnToProperty.put(property.getAnnotation(Column.class).value(), property);
}
StringBuilder anyColumnIsNull = new StringBuilder();
StringBuilder columnEquals = new StringBuilder();
List<String> properties = new ArrayList<String>();
for (PrimaryKeyData pk : primaryKeys) {
for (String column : pk.getColumns()) {
Property property = columnToProperty.get(column);
String propName = property.getEscapedName();
if (anyColumnIsNull.length() > 0) {
anyColumnIsNull.append(" || ");
columnEquals.append(" && ");
}
anyColumnIsNull.append(propName).append(" == null");
columnEquals.append(propName).append(".equals(obj.").append(propName).append(")");
properties.add(propName);
}
}
// equals
writer.annotation(Override.class);
writer.beginPublicMethod(Types.BOOLEAN_P, "equals", o);
writer.line("if (", anyColumnIsNull + ") {");
writer.line(" return super.equals(o);");
writer.line("}");
writer.line("if (!(o instanceof ", model.getSimpleName(), ")) {");
writer.line(" return false;");
writer.line("}");
writer.line(model.getSimpleName(), " obj = (", model.getSimpleName(), ") o;");
writer.line("return ", columnEquals + ";");
writer.end();
// hashCode
writer.annotation(Override.class);
writer.beginPublicMethod(Types.INT, "hashCode");
writer.line("if (", anyColumnIsNull + ") {");
writer.line(" return super.hashCode();");
writer.line("}");
writer.line("final int prime = 31;");
writer.line("int result = 1;");
for (String property : properties) {
writer.line("result = prime * result + ", property, ".hashCode();");
}
writer.line("return result;");
writer.end();
}
use of com.querydsl.codegen.Property in project querydsl by querydsl.
the class OrdinalPositionComparator method compare.
@Override
public int compare(Property property1, Property property2) {
Integer comparison = null;
for (Property property : Arrays.asList(property1, property2)) {
Map<Object, Object> data = property.getData();
ColumnMetadata columnMetadata = (ColumnMetadata) data.get("COLUMN");
int index = columnMetadata.getIndex();
comparison = comparison == null ? index : comparison - index;
}
return comparison;
}
Aggregations