Search in sources :

Example 76 with IPersistentClass

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

the class OpenSourceAction method run.

public void run() {
    IStructuredSelection sel = getStructuredSelection();
    if (!(sel instanceof TreeSelection)) {
        return;
    }
    TreePath[] paths = ((TreeSelection) sel).getPaths();
    for (int i = 0; i < paths.length; i++) {
        TreePath path = paths[i];
        Object lastSegment = path.getLastSegment();
        IPersistentClass persClass = getPersistentClass(lastSegment);
        ConsoleConfiguration consoleConfig = (ConsoleConfiguration) (path.getSegment(0));
        String fullyQualifiedName = null;
        if (lastSegment instanceof IProperty) {
            Object prevSegment = path.getParentPath().getLastSegment();
            if (prevSegment instanceof IProperty && ((IProperty) prevSegment).isComposite()) {
                fullyQualifiedName = ((IValue) ((IProperty) prevSegment).getValue()).getComponentClassName();
            }
        }
        if (fullyQualifiedName == null && persClass != null) {
            fullyQualifiedName = persClass.getClassName();
        }
        try {
            run(consoleConfig, lastSegment, fullyQualifiedName);
        } catch (JavaModelException e) {
            HibernateConsolePlugin.getDefault().logErrorMessage(HibernateConsoleMessages.OpenSourceAction_cannot_find_source_file, e);
        } catch (PartInitException e) {
            HibernateConsolePlugin.getDefault().logErrorMessage(HibernateConsoleMessages.OpenSourceAction_cannot_open_source_file, e);
        } catch (FileNotFoundException e) {
            HibernateConsolePlugin.getDefault().logErrorMessage(HibernateConsoleMessages.OpenSourceAction_cannot_find_source_file, e);
        }
    }
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) ConsoleConfiguration(org.hibernate.console.ConsoleConfiguration) FileNotFoundException(java.io.FileNotFoundException) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IPersistentClass(org.jboss.tools.hibernate.runtime.spi.IPersistentClass) TreePath(org.eclipse.jface.viewers.TreePath) IProperty(org.jboss.tools.hibernate.runtime.spi.IProperty) TreeSelection(org.eclipse.jface.viewers.TreeSelection) PartInitException(org.eclipse.ui.PartInitException)

Example 77 with IPersistentClass

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

the class CriteriaEditor method getImports.

private String[] getImports() {
    final ConsoleConfiguration consoleConfiguration = getConsoleConfiguration();
    if (!consoleConfiguration.hasConfiguration()) {
        try {
            consoleConfiguration.build();
            consoleConfiguration.buildMappings();
        } catch (Exception e) {
            String mess = NLS.bind(HibernateConsoleMessages.CompletionHelper_error_could_not_build_cc, consoleConfiguration.getName());
            HibernateConsolePlugin.getDefault().logErrorMessage(mess, e);
        }
    }
    Set<String> imports = new HashSet<String>();
    IConfiguration configuration = consoleConfiguration.getConfiguration();
    if (configuration != null) {
        Iterator<IPersistentClass> classMappings = configuration.getClassMappings();
        while (classMappings.hasNext()) {
            IPersistentClass clazz = classMappings.next();
            String className = clazz.getClassName();
            if (className != null) {
                imports.add(className);
            }
        }
    }
    // $NON-NLS-1$
    imports.add("org.hibernate.*");
    // $NON-NLS-1$
    imports.add("org.hibernate.criterion.*");
    return imports.toArray(new String[imports.size()]);
}
Also used : ConsoleConfiguration(org.hibernate.console.ConsoleConfiguration) IConfiguration(org.jboss.tools.hibernate.runtime.spi.IConfiguration) JavaModelException(org.eclipse.jdt.core.JavaModelException) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) IPersistentClass(org.jboss.tools.hibernate.runtime.spi.IPersistentClass) HashSet(java.util.HashSet)

Example 78 with IPersistentClass

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

the class ConfigurationViewAdapter method getPersistentClasses.

public List<PersistentClassViewAdapter> getPersistentClasses() {
    if (persistentClasses == null) {
        Iterator<IPersistentClass> classMappings = cfg.getClassMappings();
        persistentClasses = new HashMap<String, PersistentClassViewAdapter>();
        while (classMappings.hasNext()) {
            IPersistentClass clazz = classMappings.next();
            persistentClasses.put(clazz.getEntityName(), new PersistentClassViewAdapter(this, clazz));
        }
        Iterator<PersistentClassViewAdapter> iterator = persistentClasses.values().iterator();
        while (iterator.hasNext()) {
            PersistentClassViewAdapter element = iterator.next();
            element.getSourceAssociations();
        }
    }
    return new ArrayList<PersistentClassViewAdapter>(persistentClasses.values());
}
Also used : ArrayList(java.util.ArrayList) IPersistentClass(org.jboss.tools.hibernate.runtime.spi.IPersistentClass)

Example 79 with IPersistentClass

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

the class TypeVisitor method visit.

@Override
public boolean visit(ParameterizedType type) {
    // $NON-NLS-1$
    Assert.isNotNull(type, "Type object cannot be null");
    // $NON-NLS-1$
    Assert.isNotNull(entityInfo, "EntityInfo object cannot be null");
    ITypeBinding tb = type.resolveBinding();
    // Unresolved binding. Omit the property.
    if (tb == null)
        return false;
    rootClass = rootClasses.get(entityInfo.getFullyQualifiedName());
    // $NON-NLS-1$
    Assert.isNotNull(rootClass, "RootClass not found.");
    ITypeBinding[] interfaces = Utils.getAllInterfaces(tb);
    IValue value = buildCollectionValue(interfaces);
    if (value != null) {
        if (ref != null && rootClasses.get(ref.fullyQualifiedName) != null) {
            IValue oValue = service.newOneToMany(rootClass);
            IPersistentClass associatedClass = rootClasses.get(ref.fullyQualifiedName);
            oValue.setAssociatedClass(associatedClass);
            oValue.setReferencedEntityName(associatedClass.getEntityName());
            // Set another table
            value.setCollectionTable(associatedClass.getTable());
            value.setElement(oValue);
        } else {
            IValue elementValue = buildSimpleValue(tb.getTypeArguments()[0].getQualifiedName());
            elementValue.setTable(rootClass.getTable());
            value.setElement(elementValue);
            // TODO what to set?
            value.setCollectionTable(rootClass.getTable());
        }
        if (value.isList()) {
            value.setIndex(service.newSimpleValue());
        } else if (value.isMap()) {
            IValue map_key = service.newSimpleValue();
            // FIXME: is it possible to map Map<SourceType, String>?
            // Or only Map<String, SourceType>
            map_key.setTypeName(tb.getTypeArguments()[0].getBinaryName());
            value.setIndex(map_key);
        }
    }
    if (value == null) {
        value = buildSimpleValue(tb.getBinaryName());
    }
    buildProperty(value);
    if (!(value.isSimpleValue())) {
        // $NON-NLS-1$
        prop.setCascade("none");
    }
    // do not visit children
    return false;
}
Also used : IValue(org.jboss.tools.hibernate.runtime.spi.IValue) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) IPersistentClass(org.jboss.tools.hibernate.runtime.spi.IPersistentClass)

Example 80 with IPersistentClass

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

the class TypeVisitor method createHierarhyStructure.

/**
 * Replace <class> element on <joined-subclass> or <subclass>.
 * @param project
 * @param rootClasses
 * @return
 */
private Collection<IPersistentClass> createHierarhyStructure(IJavaProject project, Map<String, IPersistentClass> rootClasses) {
    IService service = getService(project);
    Map<String, IPersistentClass> pcCopy = new HashMap<String, IPersistentClass>();
    for (Map.Entry<String, IPersistentClass> entry : rootClasses.entrySet()) {
        pcCopy.put(entry.getKey(), entry.getValue());
    }
    for (Map.Entry<String, IPersistentClass> entry : pcCopy.entrySet()) {
        IPersistentClass pc = null;
        try {
            pc = getMappedSuperclass(project, pcCopy, entry.getValue());
            IPersistentClass subclass = null;
            if (pc != null) {
                if (pc.isAbstract()) {
                    subclass = service.newSingleTableSubclass(pc);
                    if (pc.isInstanceOfRootClass() && pc.getDiscriminator() == null) {
                        IValue discr = service.newSimpleValue();
                        // $NON-NLS-1$
                        discr.setTypeName("string");
                        // $NON-NLS-1$
                        discr.addColumn(service.newColumn("DISCR_COL"));
                        pc.setDiscriminator(discr);
                    }
                } else {
                    subclass = service.newJoinedSubclass(pc);
                }
            } else {
                pc = getMappedImplementedInterface(project, pcCopy, entry.getValue());
                if (pc != null) {
                    subclass = service.newSingleTableSubclass(pc);
                }
            }
            if (subclass != null) {
                IPersistentClass pastClass = pcCopy.get(entry.getKey());
                subclass.setClassName(pastClass.getClassName());
                subclass.setEntityName(pastClass.getEntityName());
                subclass.setDiscriminatorValue(StringHelper.unqualify(pastClass.getClassName()));
                subclass.setAbstract(pastClass.isAbstract());
                if (subclass.isInstanceOfJoinedSubclass()) {
                    subclass.setTable(service.newTable(StringHelper.unqualify(pastClass.getClassName()).toUpperCase()));
                    subclass.setKey(pc.getIdentifierProperty().getValue());
                }
                if (pastClass.getIdentifierProperty() != null) {
                    subclass.addProperty(pastClass.getIdentifierProperty());
                }
                Iterator<IProperty> it = pastClass.getPropertyIterator();
                while (it.hasNext()) {
                    subclass.addProperty(it.next());
                }
                entry.setValue(subclass);
            }
        } catch (JavaModelException e) {
            HibernateConsolePlugin.getDefault().log(e);
        }
    }
    return pcCopy.values();
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IValue(org.jboss.tools.hibernate.runtime.spi.IValue) HashMap(java.util.HashMap) IProperty(org.jboss.tools.hibernate.runtime.spi.IProperty) HashMap(java.util.HashMap) Map(java.util.Map) IService(org.jboss.tools.hibernate.runtime.spi.IService) IPersistentClass(org.jboss.tools.hibernate.runtime.spi.IPersistentClass)

Aggregations

IPersistentClass (org.jboss.tools.hibernate.runtime.spi.IPersistentClass)175 Test (org.junit.Test)97 IFacade (org.jboss.tools.hibernate.runtime.common.IFacade)60 IValue (org.jboss.tools.hibernate.runtime.spi.IValue)51 RootClass (org.hibernate.mapping.RootClass)47 IProperty (org.jboss.tools.hibernate.runtime.spi.IProperty)46 PersistentClass (org.hibernate.mapping.PersistentClass)43 ConsoleConfiguration (org.hibernate.console.ConsoleConfiguration)19 AbstractPersistentClassFacade (org.jboss.tools.hibernate.runtime.common.AbstractPersistentClassFacade)16 IConfiguration (org.jboss.tools.hibernate.runtime.spi.IConfiguration)16 ITable (org.jboss.tools.hibernate.runtime.spi.ITable)14 OneToMany (org.hibernate.mapping.OneToMany)12 HashMap (java.util.HashMap)8 Iterator (java.util.Iterator)8 JoinedSubclass (org.hibernate.mapping.JoinedSubclass)8 PrimitiveArray (org.hibernate.mapping.PrimitiveArray)8 Property (org.hibernate.mapping.Property)8 SingleTableSubclass (org.hibernate.mapping.SingleTableSubclass)8 IEditorPart (org.eclipse.ui.IEditorPart)7 PartInitException (org.eclipse.ui.PartInitException)7