Search in sources :

Example 46 with IColumn

use of org.jboss.tools.hibernate.runtime.spi.IColumn in project jbosstools-hibernate by jbosstools.

the class OpenMappingAction method run.

public void run() {
    DiagramEditorInput objectEditorInput = (DiagramEditorInput) ((DiagramViewer) getWorkbenchPart()).getEditorInput();
    ConsoleConfiguration consoleConfig = objectEditorInput.getConsoleConfig();
    DiagramViewer part = (DiagramViewer) getWorkbenchPart();
    Set<Shape> selectedElements = part.getSelectedElements();
    IEditorPart editorPart = null;
    Iterator<Shape> iterator = selectedElements.iterator();
    // open only first editor - no sense to open all of them
    while (iterator.hasNext() && editorPart == null) {
        Shape shape = iterator.next();
        Object selection = shape.getOrmElement();
        if (selection instanceof IProperty && ((IProperty) selection).getPersistentClass().isInstanceOfSpecialRootClass()) {
            IProperty compositSel = ((IProperty) selection);
            IProperty parentProperty = ((IPersistentClass) compositSel.getPersistentClass()).getProperty();
            try {
                editorPart = org.hibernate.eclipse.console.actions.OpenMappingAction.run(consoleConfig, compositSel, parentProperty);
            } catch (CoreException e) {
                HibernateConsolePlugin.getDefault().logErrorMessage(DiagramViewerMessages.OpenMappingAction_canot_find_or_open_mapping_file, e);
            } catch (FileNotFoundException e) {
                HibernateConsolePlugin.getDefault().logErrorMessage(DiagramViewerMessages.OpenMappingAction_canot_find_or_open_mapping_file, e);
            }
            continue;
        }
        if (selection instanceof IPersistentClass && ((IPersistentClass) selection).isInstanceOfSpecialRootClass()) {
            selection = ((IPersistentClass) selection).getProperty();
        }
        Shape shapeParent = null;
        Object selectionParent = null;
        if (selection instanceof IColumn) {
            shapeParent = (Shape) shape.getParent();
            selectionParent = shapeParent.getOrmElement();
        }
        try {
            editorPart = org.hibernate.eclipse.console.actions.OpenMappingAction.run(consoleConfig, selection, selectionParent);
        } catch (CoreException e) {
            HibernateConsolePlugin.getDefault().logErrorMessage(DiagramViewerMessages.OpenMappingAction_open_mapping_file, e);
        } catch (FileNotFoundException e) {
            HibernateConsolePlugin.getDefault().logErrorMessage(DiagramViewerMessages.OpenMappingAction_open_mapping_file, e);
        }
    }
}
Also used : DiagramEditorInput(org.jboss.tools.hibernate.ui.view.DiagramEditorInput) ConsoleConfiguration(org.hibernate.console.ConsoleConfiguration) Shape(org.jboss.tools.hibernate.ui.diagram.editors.model.Shape) FileNotFoundException(java.io.FileNotFoundException) IEditorPart(org.eclipse.ui.IEditorPart) DiagramViewer(org.jboss.tools.hibernate.ui.diagram.editors.DiagramViewer) IPersistentClass(org.jboss.tools.hibernate.runtime.spi.IPersistentClass) CoreException(org.eclipse.core.runtime.CoreException) IProperty(org.jboss.tools.hibernate.runtime.spi.IProperty) IColumn(org.jboss.tools.hibernate.runtime.spi.IColumn)

Example 47 with IColumn

use of org.jboss.tools.hibernate.runtime.spi.IColumn in project jbosstools-hibernate by jbosstools.

the class OpenSourceAction method run.

public void run() {
    DiagramEditorInput objectEditorInput = (DiagramEditorInput) ((DiagramViewer) getWorkbenchPart()).getEditorInput();
    ConsoleConfiguration consoleConfig = objectEditorInput.getConsoleConfig();
    DiagramViewer part = (DiagramViewer) getWorkbenchPart();
    Set<Shape> selectedElements = part.getSelectedElements();
    IEditorPart editorPart = null;
    Iterator<Shape> iterator = selectedElements.iterator();
    // open only first editor - no sense to open all of them
    while (iterator.hasNext() && editorPart == null) {
        Shape shape = iterator.next();
        Object selection = shape.getOrmElement();
        if (selection instanceof IColumn || selection instanceof ITable) {
            Iterator<Connection> targetConnections = shape.getTargetConnections().iterator();
            while (targetConnections.hasNext()) {
                Connection connection = targetConnections.next();
                Shape sh1 = connection.getSource();
                Shape sh2 = connection.getTarget();
                if (shape == sh1 && sh2 != null) {
                    shape = sh2;
                    break;
                } else if (shape == sh2 && sh1 != null) {
                    shape = sh1;
                    break;
                }
            }
            selection = shape.getOrmElement();
        }
        IPersistentClass rootClass = null;
        if (selection instanceof IPersistentClass) {
            rootClass = (IPersistentClass) selection;
        } else if (selection instanceof IProperty) {
            rootClass = ((IProperty) selection).getPersistentClass();
        } else {
            continue;
        }
        // HibernateUtils.getPersistentClassName(rootClass);
        String fullyQualifiedName = rootClass.getClassName();
        /*if (fullyQualifiedName.indexOf("$") > 0) {
				fullyQualifiedName = fullyQualifiedName.substring(0, fullyQualifiedName.indexOf("$"));
			}*/
        try {
            editorPart = org.hibernate.eclipse.console.actions.OpenSourceAction.run(consoleConfig, selection, fullyQualifiedName);
        } catch (CoreException e) {
            HibernateConsolePlugin.getDefault().logErrorMessage(DiagramViewerMessages.OpenSourceAction_canot_open_source_file, e);
        } catch (FileNotFoundException e) {
            HibernateConsolePlugin.getDefault().logErrorMessage(DiagramViewerMessages.OpenSourceAction_canot_find_source_file, e);
        }
    }
}
Also used : DiagramEditorInput(org.jboss.tools.hibernate.ui.view.DiagramEditorInput) ConsoleConfiguration(org.hibernate.console.ConsoleConfiguration) Shape(org.jboss.tools.hibernate.ui.diagram.editors.model.Shape) Connection(org.jboss.tools.hibernate.ui.diagram.editors.model.Connection) FileNotFoundException(java.io.FileNotFoundException) IEditorPart(org.eclipse.ui.IEditorPart) DiagramViewer(org.jboss.tools.hibernate.ui.diagram.editors.DiagramViewer) IPersistentClass(org.jboss.tools.hibernate.runtime.spi.IPersistentClass) CoreException(org.eclipse.core.runtime.CoreException) IColumn(org.jboss.tools.hibernate.runtime.spi.IColumn) IProperty(org.jboss.tools.hibernate.runtime.spi.IProperty) ITable(org.jboss.tools.hibernate.runtime.spi.ITable)

Example 48 with IColumn

use of org.jboss.tools.hibernate.runtime.spi.IColumn in project jbosstools-hibernate by jbosstools.

the class ElementsFactory method createConnections.

@SuppressWarnings("rawtypes")
private boolean createConnections(ExpandableShape persistentClass, ExpandableShape dbTable) {
    boolean res = false;
    if (persistentClass == null || dbTable == null) {
        return res;
    }
    IProperty parentProperty = null;
    if (persistentClass.getOrmElement() instanceof IPersistentClass && ((IPersistentClass) persistentClass.getOrmElement()).isInstanceOfSpecialRootClass()) {
        parentProperty = ((IPersistentClass) persistentClass.getOrmElement()).getParentProperty();
    }
    Iterator<Shape> itFields = persistentClass.getChildrenIterator();
    Set<Shape> processed = new HashSet<Shape>();
    while (itFields.hasNext()) {
        final Shape shape = itFields.next();
        Object element = shape.getOrmElement();
        if (!(element instanceof IProperty && parentProperty != element)) {
            continue;
        }
        IValue value = ((IProperty) element).getValue();
        Iterator iterator = value.getColumnIterator();
        while (iterator.hasNext()) {
            Object o = iterator.next();
            if (!(o instanceof IColumn)) {
                continue;
            }
            IColumn dbColumn = (IColumn) o;
            Iterator<Shape> itColumns = dbTable.getChildrenIterator();
            while (itColumns.hasNext()) {
                final Shape shapeCol = itColumns.next();
                if (processed.contains(shapeCol)) {
                    continue;
                }
                if (shape.equals(shapeCol)) {
                    continue;
                }
                Object ormElement = shapeCol.getOrmElement();
                // $NON-NLS-1$
                String name2 = "";
                if (ormElement instanceof IColumn) {
                    IColumn dbColumn2 = (IColumn) ormElement;
                    name2 = dbColumn2.getName();
                } else if (ormElement instanceof IProperty) {
                    IProperty property2 = (IProperty) ormElement;
                    name2 = property2.getName();
                }
                if (dbColumn.getName().equals(name2)) {
                    if (shouldCreateConnection(shape, shapeCol)) {
                        connections.add(new Connection(shape, shapeCol));
                        res = true;
                    }
                    processed.add(shapeCol);
                }
            }
        }
    }
    return res;
}
Also used : IPersistentClass(org.jboss.tools.hibernate.runtime.spi.IPersistentClass) IValue(org.jboss.tools.hibernate.runtime.spi.IValue) IProperty(org.jboss.tools.hibernate.runtime.spi.IProperty) IColumn(org.jboss.tools.hibernate.runtime.spi.IColumn) Iterator(java.util.Iterator) HashSet(java.util.HashSet)

Example 49 with IColumn

use of org.jboss.tools.hibernate.runtime.spi.IColumn in project jbosstools-hibernate by jbosstools.

the class ElementsFactory method createForeingKeyConnections.

@SuppressWarnings("unchecked")
public void createForeingKeyConnections() {
    // do clone cause elements could be changed during iteration!
    HashMap<String, OrmShape> elementsTmp = (HashMap<String, OrmShape>) elements.clone();
    Iterator<OrmShape> it = elementsTmp.values().iterator();
    while (it.hasNext()) {
        final OrmShape shape = it.next();
        Object ormElement = shape.getOrmElement();
        if (ormElement instanceof ITable) {
            ITable databaseTable = (ITable) ormElement;
            Iterator<IForeignKey> itFK = databaseTable.getForeignKeyIterator();
            while (itFK.hasNext()) {
                final IForeignKey fk = itFK.next();
                ITable referencedTable = fk.getReferencedTable();
                final OrmShape referencedShape = getOrCreateDatabaseTable(referencedTable);
                // 
                Iterator<IColumn> itColumns = fk.columnIterator();
                while (itColumns.hasNext()) {
                    IColumn col = itColumns.next();
                    Shape shapeColumn = shape.getChild(col);
                    Iterator<IColumn> itReferencedColumns = null;
                    if (fk.isReferenceToPrimaryKey()) {
                        itReferencedColumns = (Iterator<IColumn>) referencedTable.getPrimaryKey().columnIterator();
                    } else {
                        itReferencedColumns = (Iterator<IColumn>) fk.getReferencedColumns().iterator();
                    }
                    while (itReferencedColumns != null && itReferencedColumns.hasNext()) {
                        IColumn colReferenced = itReferencedColumns.next();
                        Shape shapeReferencedColumn = referencedShape.getChild(colReferenced);
                        if (shouldCreateConnection(shapeColumn, shapeReferencedColumn)) {
                            connections.add(new Connection(shapeColumn, shapeReferencedColumn));
                        }
                    }
                }
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) IForeignKey(org.jboss.tools.hibernate.runtime.spi.IForeignKey) IColumn(org.jboss.tools.hibernate.runtime.spi.IColumn) ITable(org.jboss.tools.hibernate.runtime.spi.ITable)

Example 50 with IColumn

use of org.jboss.tools.hibernate.runtime.spi.IColumn in project jbosstools-hibernate by jbosstools.

the class ElementsFactory method refreshComponentReferences.

protected void refreshComponentReferences(ComponentShape componentShape) {
    IProperty property = (IProperty) componentShape.getOrmElement();
    IValue v = property.getValue();
    if (!v.isCollection()) {
        return;
    }
    IValue collection = v;
    IValue component = collection.getElement();
    Shape csChild0 = null, csChild1 = null;
    Iterator<Shape> tmp = componentShape.getChildrenIterator();
    if (tmp.hasNext()) {
        csChild0 = tmp.next();
    }
    if (tmp.hasNext()) {
        csChild1 = tmp.next();
    }
    OrmShape childShape = null;
    if (component.isComponent()) {
        childShape = elements.get(component.getComponentClassName());
        if (childShape == null) {
            childShape = getOrCreateComponentClass(property);
        }
        IValue value = (IValue) csChild0.getOrmElement();
        OrmShape tableShape = getOrCreateDatabaseTable(value.getTable());
        if (tableShape != null) {
            Iterator<IColumn> it = value.getColumnIterator();
            while (it.hasNext()) {
                IColumn el = it.next();
                Shape shape = tableShape.getChild(el);
                if (shouldCreateConnection(csChild0, shape)) {
                    connections.add(new Connection(csChild0, shape));
                }
            }
        }
        if (shouldCreateConnection(csChild1, childShape)) {
            connections.add(new Connection(csChild1, childShape));
        }
    } else if (collection.isOneToMany()) {
        childShape = getOrCreateAssociationClass(property);
        if (childShape != null) {
            if (shouldCreateConnection(csChild1, childShape)) {
                connections.add(new Connection(csChild1, childShape));
            }
            OrmShape keyTableShape = getOrCreateDatabaseTable(collection.getKey().getTable());
            Iterator<IColumn> it = collection.getKey().getColumnIterator();
            while (it.hasNext()) {
                Object el = it.next();
                if (el instanceof IColumn) {
                    IColumn col = (IColumn) el;
                    Shape shape = keyTableShape.getChild(col);
                    if (shouldCreateConnection(csChild0, shape)) {
                        connections.add(new Connection(csChild0, shape));
                    }
                }
            }
        }
    } else {
        // this is case: if (collection.isMap() || collection.isSet())
        childShape = getOrCreateDatabaseTable(collection.getCollectionTable());
        if (childShape != null) {
            Iterator<IColumn> it = ((IValue) csChild0.getOrmElement()).getColumnIterator();
            while (it.hasNext()) {
                Object el = it.next();
                if (el instanceof IColumn) {
                    IColumn col = (IColumn) el;
                    Shape shape = childShape.getChild(col);
                    if (shouldCreateConnection(csChild0, shape)) {
                        connections.add(new Connection(csChild0, shape));
                    }
                }
            }
            it = ((IValue) csChild1.getOrmElement()).getColumnIterator();
            while (it.hasNext()) {
                Object el = it.next();
                if (el instanceof IColumn) {
                    IColumn col = (IColumn) el;
                    Shape shape = childShape.getChild(col);
                    if (shouldCreateConnection(csChild1, shape)) {
                        connections.add(new Connection(csChild1, shape));
                    }
                }
            }
        }
    }
}
Also used : IValue(org.jboss.tools.hibernate.runtime.spi.IValue) IProperty(org.jboss.tools.hibernate.runtime.spi.IProperty) IColumn(org.jboss.tools.hibernate.runtime.spi.IColumn) Iterator(java.util.Iterator)

Aggregations

IColumn (org.jboss.tools.hibernate.runtime.spi.IColumn)86 Test (org.junit.Test)69 Column (org.hibernate.mapping.Column)68 ITable (org.jboss.tools.hibernate.runtime.spi.ITable)26 SimpleValue (org.hibernate.mapping.SimpleValue)16 Table (org.hibernate.mapping.Table)16 ArrayList (java.util.ArrayList)12 IProperty (org.jboss.tools.hibernate.runtime.spi.IProperty)9 AbstractForeignKeyFacade (org.jboss.tools.hibernate.runtime.common.AbstractForeignKeyFacade)7 IPersistentClass (org.jboss.tools.hibernate.runtime.spi.IPersistentClass)7 IValue (org.jboss.tools.hibernate.runtime.spi.IValue)7 Iterator (java.util.Iterator)5 IFacade (org.jboss.tools.hibernate.runtime.common.IFacade)4 IPrimaryKey (org.jboss.tools.hibernate.runtime.spi.IPrimaryKey)4 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)3 ConsoleConfiguration (org.hibernate.console.ConsoleConfiguration)3 Shape (org.jboss.tools.hibernate.ui.diagram.editors.model.Shape)3 FileNotFoundException (java.io.FileNotFoundException)2 HashMap (java.util.HashMap)2 List (java.util.List)2