Search in sources :

Example 1 with Column

use of com.manydesigns.portofino.model.database.Column in project Portofino by ManyDesigns.

the class SessionFactoryBuilder method ensurePrimaryKey.

protected boolean ensurePrimaryKey(Table table) {
    List<Column> idColumns = new ArrayList<>();
    for (Column column : table.getColumns()) {
        column.getAnnotations().forEach(ann -> {
            Class<?> annotationClass = ann.getJavaAnnotationClass();
            if (Id.class.equals(annotationClass)) {
                idColumns.add(column);
            }
        });
    }
    if (idColumns.isEmpty()) {
        return false;
    } else {
        logger.info("Creating primary key on table {} according to @Id annotations", table.getQualifiedName());
        PrimaryKey pk = new PrimaryKey(table);
        pk.setPrimaryKeyName("synthetic_pk_" + table.getQualifiedName().replace('.', '_'));
        for (Column column : idColumns) {
            pk.add(column);
        }
        table.setPrimaryKey(pk);
        return true;
    }
}
Also used : Column(com.manydesigns.portofino.model.database.Column)

Example 2 with Column

use of com.manydesigns.portofino.model.database.Column in project Portofino by ManyDesigns.

the class SessionFactoryBuilder method setupColumns.

protected void setupColumns(Table table, CtClass cc, ConstPool constPool) throws CannotCompileException, NotFoundException {
    List<Column> columnPKList = table.getPrimaryKey().getColumns();
    Annotation annotation;
    for (Column column : table.getColumns()) {
        String propertyName = column.getActualPropertyName();
        CtField field = new CtField(classPool.get(column.getActualJavaType().getName()), propertyName, cc);
        AnnotationsAttribute fieldAnnotations = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
        annotation = new Annotation(javax.persistence.Column.class.getName(), constPool);
        annotation.addMemberValue("name", new StringMemberValue(jpaEscape(column.getColumnName()), constPool));
        annotation.addMemberValue("nullable", new BooleanMemberValue(column.isNullable(), constPool));
        if (column.getLength() != null) {
            annotation.addMemberValue("precision", new IntegerMemberValue(constPool, column.getLength()));
            annotation.addMemberValue("length", new IntegerMemberValue(constPool, column.getLength()));
        }
        if (column.getScale() != null) {
            annotation.addMemberValue("scale", new IntegerMemberValue(constPool, column.getScale()));
        }
        fieldAnnotations.addAnnotation(annotation);
        if (columnPKList.contains(column)) {
            annotation.addMemberValue("updatable", new BooleanMemberValue(false, constPool));
            annotation = new Annotation(Id.class.getName(), constPool);
            fieldAnnotations.addAnnotation(annotation);
            if (column.isAutoincrement()) {
                setupIdentityGenerator(fieldAnnotations, constPool);
            } else {
                PrimaryKeyColumn pkColumn = table.getPrimaryKey().findPrimaryKeyColumnByName(column.getColumnName());
                Generator generator = pkColumn.getGenerator();
                if (generator != null) {
                    setupNonIdentityGenerator(table, fieldAnnotations, generator, constPool);
                }
            }
        }
        setupColumnType(column, fieldAnnotations, constPool);
        column.getAnnotations().forEach(ann -> {
            Class<?> annotationClass = ann.getJavaAnnotationClass();
            if (javax.persistence.Column.class.equals(annotationClass) || Id.class.equals(annotationClass) || org.hibernate.annotations.Type.class.equals(annotationClass)) {
                logger.debug("@Column or @Id or @Type specified on column {}, ignoring annotation {}", column.getQualifiedName(), annotationClass);
                return;
            }
            Annotation fieldAnn = convertAnnotation(constPool, ann);
            if (fieldAnn != null) {
                fieldAnnotations.addAnnotation(fieldAnn);
            }
        });
        field.getFieldInfo().addAttribute(fieldAnnotations);
        field.setModifiers(javassist.Modifier.PROTECTED);
        cc.addField(field);
        String accessorName = propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
        cc.addMethod(CtNewMethod.getter("get" + accessorName, field));
        cc.addMethod(CtNewMethod.setter("set" + accessorName, field));
    }
}
Also used : AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute) Column(com.manydesigns.portofino.model.database.Column) javax.persistence(javax.persistence) SequenceGenerator(com.manydesigns.portofino.model.database.SequenceGenerator) TableGenerator(com.manydesigns.portofino.model.database.TableGenerator) GenericGenerator(org.hibernate.annotations.GenericGenerator)

Example 3 with Column

use of com.manydesigns.portofino.model.database.Column in project Portofino by ManyDesigns.

the class SessionFactoryBuilder method checkValidFk.

protected boolean checkValidFk(ForeignKey foreignKey) {
    Table toTable = foreignKey.getToTable();
    if (toTable == null) {
        logger.error("The foreign key " + foreignKey.getQualifiedName() + " does not refer to any table.");
        return false;
    }
    if (checkInvalidPrimaryKey(toTable, false)) {
        logger.error("The foreign key " + foreignKey.getQualifiedName() + " refers to a table with absent or invalid primary key.");
        return false;
    }
    // Check that referenced columns coincide with the primary key
    Set<Column> fkColumns = new HashSet<>();
    Set<Column> pkColumns = new HashSet<>(toTable.getPrimaryKey().getColumns());
    for (Reference reference : foreignKey.getReferences()) {
        fkColumns.add(reference.getActualToColumn());
    }
    if (!fkColumns.equals(pkColumns)) {
        logger.error("The foreign key " + foreignKey.getQualifiedName() + " does not refer to " + "the exact primary key of table " + toTable.getQualifiedName() + ", this is not supported.");
        return false;
    }
    try {
        getMappedClass(toTable);
    } catch (NotFoundException e) {
        logger.error("The foreign key " + foreignKey.getQualifiedName() + " refers to unmapped table " + toTable.getQualifiedName() + ", skipping.");
        return false;
    }
    return true;
}
Also used : Table(com.manydesigns.portofino.model.database.Table) Column(com.manydesigns.portofino.model.database.Column)

Example 4 with Column

use of com.manydesigns.portofino.model.database.Column in project Portofino by ManyDesigns.

the class SessionFactoryBuilder method defineEqualsAndHashCode.

protected void defineEqualsAndHashCode(Table table, CtClass cc) throws CannotCompileException {
    List<Column> columnPKList = table.getPrimaryKey().getColumns();
    String equalsMethod = "public boolean equals(Object other) {" + "    if(!(other instanceof " + cc.getName() + ")) {" + "        return false;" + "    }" + cc.getName() + " castOther = (" + cc.getName() + ") other;";
    String hashCodeMethod = "public int hashCode() {" + "    return java.util.Objects.hash(new java.lang.Object[] {";
    boolean first = true;
    for (Column c : columnPKList) {
        equalsMethod += "    if(!java.util.Objects.equals(this." + c.getActualPropertyName() + ", castOther." + c.getActualPropertyName() + ")) {" + "        return false;" + "    }";
        if (first) {
            first = false;
        } else {
            hashCodeMethod += ", ";
        }
        hashCodeMethod += c.getActualPropertyName();
    }
    equalsMethod += "return true; }";
    hashCodeMethod += "});}";
    cc.addMethod(CtNewMethod.make(equalsMethod, cc));
    cc.addMethod(CtNewMethod.make(hashCodeMethod, cc));
}
Also used : Column(com.manydesigns.portofino.model.database.Column)

Example 5 with Column

use of com.manydesigns.portofino.model.database.Column in project Portofino by ManyDesigns.

the class TableAccessor method setupKeyColumns.

private void setupKeyColumns(List<Column> columns, List<Column> pkColumns) {
    int i = 0;
    for (Column current : pkColumns) {
        int index = columns.indexOf(current);
        ColumnAccessor columnAccessor = columnAccessors[index];
        keyColumnAccessors[i] = columnAccessor;
        i++;
    }
}
Also used : Column(com.manydesigns.portofino.model.database.Column)

Aggregations

Column (com.manydesigns.portofino.model.database.Column)8 Table (com.manydesigns.portofino.model.database.Table)2 FileBlob (com.manydesigns.elements.annotations.FileBlob)1 Blob (com.manydesigns.elements.blobs.Blob)1 HierarchicalBlobManager (com.manydesigns.elements.blobs.HierarchicalBlobManager)1 AbstractBlobField (com.manydesigns.elements.fields.AbstractBlobField)1 Field (com.manydesigns.elements.fields.Field)1 FileBlobField (com.manydesigns.elements.fields.FileBlobField)1 ClassAccessor (com.manydesigns.elements.reflection.ClassAccessor)1 PropertyAccessor (com.manydesigns.elements.reflection.PropertyAccessor)1 MutableHttpServletRequest (com.manydesigns.elements.servlet.MutableHttpServletRequest)1 ActionDescriptor (com.manydesigns.portofino.actions.ActionDescriptor)1 Annotation (com.manydesigns.portofino.model.Annotation)1 Property (com.manydesigns.portofino.model.Property)1 SequenceGenerator (com.manydesigns.portofino.model.database.SequenceGenerator)1 TableGenerator (com.manydesigns.portofino.model.database.TableGenerator)1 ActionContext (com.manydesigns.portofino.resourceactions.ActionContext)1 ActionInstance (com.manydesigns.portofino.resourceactions.ActionInstance)1 CrudProperty (com.manydesigns.portofino.resourceactions.crud.configuration.CrudProperty)1 CrudConfiguration (com.manydesigns.portofino.resourceactions.crud.configuration.database.CrudConfiguration)1