Example usage for javax.persistence DiscriminatorType CHAR

List of usage examples for javax.persistence DiscriminatorType CHAR

Introduction

In this page you can find the example usage for javax.persistence DiscriminatorType CHAR.

Prototype

DiscriminatorType CHAR

To view the source code for javax.persistence DiscriminatorType CHAR.

Click Source Link

Document

Single character as the discriminator type.

Usage

From source file:com.cloud.utils.db.SqlGenerator.java

protected void handleDaoAttributes(Class<?> clazz) {
    Attribute attr;/* ww w.ja v  a 2s.co  m*/
    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);
    }
}

From source file:com.cloud.utils.db.SqlGenerator.java

/**
 * buildDiscriminatorClause builds the join clause when there are multiple tables.
 *
 * @return//from w w w  .ja v  a 2  s  . co  m
 */
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);
}

From source file:org.projectforge.continuousdb.Table.java

/**
 * Adds all attributes which are annotated with Column (getter or fields).
 * /*from w w w. j  a va  2 s.c om*/
 * @return this for chaining.
 */
public Table autoAddAttributes() {
    if (entityClass == null) {
        throw new IllegalStateException(
                "Entity class isn't set. Can't add attributes from property names. Please set entity class first.");
    }
    final Field[] fields;
    final List<Method> getter;
    if (this.superTable != null) {
        // Get only fields of the current entity, all fields of the super class are handled by the super table.
        fields = entityClass.getDeclaredFields();
        getter = BeanHelper.getAllGetterMethods(entityClass, false);
    } else {
        fields = BeanHelper.getAllDeclaredFields(entityClass);
        getter = BeanHelper.getAllGetterMethods(entityClass);
    }
    for (final Field field : fields) {
        final List<Annotation> annotations = handlePersistencyAnnotations(field);
        if (annotations == null) {
            continue;
        }
        final String fieldName = field.getName();
        if (log.isDebugEnabled() == true) {
            log.debug(name + "." + fieldName);
        }
        addTableAttribute(fieldName, annotations);
    }
    for (final Method method : getter) {
        final List<Annotation> annotations = handlePersistencyAnnotations(method);
        if (annotations == null) {
            continue;
        }
        if (log.isDebugEnabled() == true) {
            log.debug(name + "." + method.getName());
        }
        final String property = BeanHelper.getProperty(method);
        if (property != null) {
            addTableAttribute(property, annotations);
        } else {
            log.error("Can't determine property of getter method: '" + method.getName());
        }
    }
    if (this.discriminatorColumn != null) {
        TableAttributeType type;
        if (this.discriminatorColumn.discriminatorType() == DiscriminatorType.CHAR) {
            type = TableAttributeType.CHAR;
        } else if (this.discriminatorColumn.discriminatorType() == DiscriminatorType.INTEGER) {
            type = TableAttributeType.INT;

        } else {
            type = TableAttributeType.VARCHAR;
        }
        final TableAttribute attr = new TableAttribute(this.discriminatorColumn.name(), type);
        if (type == TableAttributeType.VARCHAR) {
            attr.setLength(31);
        }
        addAttribute(attr);
    }
    return this;
}