Search in sources :

Example 6 with DiscriminatorColumn

use of javax.persistence.DiscriminatorColumn in project CloudStack-archive by CloudStack-extras.

the class SqlGenerator method buildDiscriminatorClause.

/**
     * buildDiscriminatorClause builds the join clause when there are multiple tables.
     * 
     * @return
     */
public Pair<StringBuilder, Map<String, Object>> buildDiscriminatorClause() {
    StringBuilder sql = new StringBuilder();
    Map<String, Object> values = new HashMap<String, Object>();
    for (Class<?> table : _tables) {
        DiscriminatorValue dv = table.getAnnotation(DiscriminatorValue.class);
        if (dv != null) {
            Class<?> parent = table.getSuperclass();
            String tableName = DbUtil.getTableName(parent);
            DiscriminatorColumn dc = parent.getAnnotation(DiscriminatorColumn.class);
            assert (dc != null) : "Parent does not have discrminator column: " + parent.getName();
            sql.append(tableName);
            sql.append(".");
            sql.append(dc.name()).append("=");
            Object value = null;
            if (dc.discriminatorType() == DiscriminatorType.INTEGER) {
                sql.append(dv.value());
                value = Integer.parseInt(dv.value());
            } else if (dc.discriminatorType() == DiscriminatorType.CHAR) {
                sql.append(dv.value());
                value = dv.value().charAt(0);
            } else if (dc.discriminatorType() == DiscriminatorType.STRING) {
                String v = dv.value();
                v = v.substring(0, v.length() < dc.length() ? v.length() : dc.length());
                sql.append("'").append(v).append("'");
                value = v;
            }
            values.put(dc.name(), value);
            sql.append(" AND ");
        }
    }
    return new Pair<StringBuilder, Map<String, Object>>(sql, values);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DiscriminatorColumn(javax.persistence.DiscriminatorColumn) DiscriminatorValue(javax.persistence.DiscriminatorValue) Pair(com.cloud.utils.Pair)

Example 7 with DiscriminatorColumn

use of javax.persistence.DiscriminatorColumn in project CloudStack-archive by CloudStack-extras.

the class SqlGenerator method handleDaoAttributes.

protected void handleDaoAttributes(Class<?> clazz) {
    Attribute attr;
    Class<?> current = clazz;
    while (current != null && current.getAnnotation(Entity.class) != null) {
        DiscriminatorColumn column = current.getAnnotation(DiscriminatorColumn.class);
        if (column != null) {
            String columnName = column.name();
            attr = findAttribute(columnName);
            if (attr != null) {
                attr.setTrue(Attribute.Flag.DaoGenerated);
                attr.setTrue(Attribute.Flag.Insertable);
                attr.setTrue(Attribute.Flag.Updatable);
                attr.setFalse(Attribute.Flag.Nullable);
                attr.setTrue(Attribute.Flag.DC);
            } else {
                attr = new Attribute(DbUtil.getTableName(current), column.name());
                attr.setFalse(Flag.Selectable);
                attr.setTrue(Flag.Insertable);
                attr.setTrue(Flag.DaoGenerated);
                attr.setTrue(Flag.DC);
                _attributes.add(attr);
            }
            if (column.discriminatorType() == DiscriminatorType.CHAR) {
                attr.setTrue(Attribute.Flag.CharDT);
            } else if (column.discriminatorType() == DiscriminatorType.STRING) {
                attr.setTrue(Attribute.Flag.StringDT);
            } else if (column.discriminatorType() == DiscriminatorType.INTEGER) {
                attr.setTrue(Attribute.Flag.IntegerDT);
            }
        }
        PrimaryKeyJoinColumn[] pkjcs = DbUtil.getPrimaryKeyJoinColumns(current);
        if (pkjcs != null) {
            for (PrimaryKeyJoinColumn pkjc : pkjcs) {
                String tableName = DbUtil.getTableName(current);
                attr = findAttribute(pkjc.name());
                if (attr == null || !tableName.equals(attr.table)) {
                    Attribute id = new Attribute(DbUtil.getTableName(current), pkjc.name());
                    if (pkjc.referencedColumnName().length() > 0) {
                        attr = findAttribute(pkjc.referencedColumnName());
                        assert (attr != null) : "Couldn't find referenced column name " + pkjc.referencedColumnName();
                    }
                    id.field = attr.field;
                    id.setTrue(Flag.Id);
                    id.setTrue(Flag.Insertable);
                    id.setFalse(Flag.Updatable);
                    id.setFalse(Flag.Nullable);
                    id.setFalse(Flag.Selectable);
                    _attributes.add(id);
                    List<Attribute> attrs = _ids.get(id.table);
                    attrs.add(id);
                }
            }
        }
        current = current.getSuperclass();
    }
    attr = findAttribute(GenericDao.CREATED_COLUMN);
    if (attr != null && attr.field.getType() == Date.class) {
        attr.setTrue(Attribute.Flag.DaoGenerated);
        attr.setTrue(Attribute.Flag.Insertable);
        attr.setFalse(Attribute.Flag.Updatable);
        attr.setFalse(Attribute.Flag.Date);
        attr.setFalse(Attribute.Flag.Time);
        attr.setTrue(Attribute.Flag.TimeStamp);
        attr.setFalse(Attribute.Flag.Nullable);
        attr.setTrue(Attribute.Flag.Created);
    }
    attr = findAttribute(GenericDao.XID_COLUMN);
    if (attr != null && attr.field.getType() == String.class) {
        attr.setTrue(Attribute.Flag.DaoGenerated);
        attr.setTrue(Attribute.Flag.Insertable);
        attr.setFalse(Attribute.Flag.Updatable);
        attr.setFalse(Attribute.Flag.TimeStamp);
        attr.setFalse(Attribute.Flag.Time);
        attr.setFalse(Attribute.Flag.Date);
        attr.setFalse(Attribute.Flag.Nullable);
        attr.setFalse(Attribute.Flag.Removed);
    }
}
Also used : PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) DiscriminatorColumn(javax.persistence.DiscriminatorColumn) Date(java.util.Date)

Aggregations

DiscriminatorColumn (javax.persistence.DiscriminatorColumn)7 DiscriminatorValue (javax.persistence.DiscriminatorValue)4 DiscriminatorType (javax.persistence.DiscriminatorType)3 Pair (com.cloud.utils.Pair)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 PrimaryKeyJoinColumn (javax.persistence.PrimaryKeyJoinColumn)2 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 Element (org.dom4j.Element)1 AnnotationException (org.hibernate.AnnotationException)1 MappingException (org.hibernate.MappingException)1 DiscriminatorFormula (org.hibernate.annotations.DiscriminatorFormula)1 DiscriminatorOptions (org.hibernate.annotations.DiscriminatorOptions)1 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)1