use of org.jboss.tools.hibernate.runtime.spi.IType in project jbosstools-hibernate by jbosstools.
the class QueryHelper method generateSQL.
public static String generateSQL(ExecutionContext executionContext, final ISessionFactory sessionFactory, final String query, final IService service) {
// $NON-NLS-1$
if (StringHelper.isEmpty(query))
return "";
String result = (String) executionContext.execute(new ExecutionContext.Command() {
public Object execute() {
try {
StringBuffer str = new StringBuffer(256);
IHQLQueryPlan plan = service.newHQLQueryPlan(query, false, sessionFactory);
IQueryTranslator[] translators = plan.getTranslators();
for (int i = 0; i < translators.length; i++) {
IQueryTranslator translator = translators[i];
if (translator.isManipulationStatement()) {
// $NON-NLS-1$
str.append(ConsoleMessages.DynamicSQLPreviewView_manipulation_of + i + ":");
Iterator<?> iterator = translator.getQuerySpaces().iterator();
while (iterator.hasNext()) {
Object qspace = iterator.next();
str.append(qspace);
// $NON-NLS-1$
if (iterator.hasNext()) {
str.append(", ");
}
}
} else {
IType[] returnTypes = translator.getReturnTypes();
// $NON-NLS-1$
str.append(i + ": ");
for (int j = 0; j < returnTypes.length; j++) {
IType returnType = returnTypes[j];
str.append(returnType.getName());
// $NON-NLS-1$
if (j < returnTypes.length - 1) {
str.append(", ");
}
}
}
// $NON-NLS-1$
str.append("\n-----------------\n");
Iterator<?> sqls = translator.collectSqlStrings().iterator();
while (sqls.hasNext()) {
String sql = (String) sqls.next();
str.append(QLFormatHelper.formatForScreen(sql));
// $NON-NLS-1$
str.append("\n\n");
}
}
return str.toString();
} catch (Throwable t) {
StringBuffer msgs = new StringBuffer();
Throwable cause = t;
while (cause != null) {
msgs.append(t);
if (cause.getCause() == cause) {
cause = null;
} else {
cause = cause.getCause();
if (cause != null)
msgs.append(ConsoleMessages.DynamicSQLPreviewView_caused_by);
}
}
return msgs.toString();
}
}
});
return result;
}
use of org.jboss.tools.hibernate.runtime.spi.IType in project jbosstools-hibernate by jbosstools.
the class ElementsFactory method processExpand.
protected void processExpand(ExpandableShape shape) {
Object element = shape.getOrmElement();
if (!(element instanceof IProperty)) {
return;
}
OrmShape s = null;
IProperty property = (IProperty) element;
if (!property.isComposite()) {
final IConfiguration config = getConfig();
//
IValue v = property.getValue();
IType type = UtilTypeExtract.getTypeUsingExecContext(v, getConsoleConfig());
if (type != null && type.isEntityType()) {
Object clazz = config != null ? config.getClassMapping(type.getAssociatedEntityName()) : null;
if (clazz instanceof IPersistentClass && ((IPersistentClass) clazz).isInstanceOfRootClass()) {
IPersistentClass rootClass = (IPersistentClass) clazz;
s = getOrCreatePersistentClass(rootClass, null);
if (shouldCreateConnection(shape, s)) {
connections.add(new Connection(shape, s));
}
} else if (clazz instanceof IPersistentClass && ((IPersistentClass) clazz).isInstanceOfSubclass()) {
s = getOrCreatePersistentClass(((IPersistentClass) clazz).getRootClass(), null);
}
}
} else {
s = getOrCreatePersistentClass(getService().newSpecialRootClass(property), null);
if (shouldCreateConnection(shape, s)) {
connections.add(new Connection(shape, s));
}
createConnections(s, getOrCreateDatabaseTable(property.getValue().getTable()));
}
}
use of org.jboss.tools.hibernate.runtime.spi.IType in project jbosstools-hibernate by jbosstools.
the class OrmShape method initModel.
/**
* creates children of the shape,
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
protected void initModel() {
Object ormElement = getOrmElement();
if (ormElement instanceof IPersistentClass && ((IPersistentClass) ormElement).isInstanceOfRootClass()) {
IPersistentClass rootClass = (IPersistentClass) ormElement;
IProperty identifierProperty = rootClass.getIdentifierProperty();
if (identifierProperty != null) {
addChild(new Shape(identifierProperty, getConsoleConfigName()));
}
IValue identifier = rootClass.getIdentifier();
if (identifier != null && identifier.isComponent()) {
IValue component = identifier;
if (component.isEmbedded()) {
Iterator<IProperty> iterator = identifier.getPropertyIterator();
while (iterator.hasNext()) {
IProperty property = iterator.next();
addChild(new Shape(property, getConsoleConfigName()));
}
}
}
Iterator<IProperty> iterator = rootClass.getPropertyIterator();
while (iterator.hasNext()) {
IProperty field = iterator.next();
if (!field.isBackRef()) {
if (!field.isComposite()) {
final IValue val = field.getValue();
Shape bodyOrmShape = null;
if (val.isSimpleValue() && val.isTypeSpecified()) {
bodyOrmShape = new Shape(field, getConsoleConfigName());
} else {
if (val.isCollection()) {
bodyOrmShape = new ComponentShape(field, getConsoleConfigName());
} else {
IType type = getTypeUsingExecContext(val);
if (type != null && type.isEntityType()) {
bodyOrmShape = new ExpandableShape(field, getConsoleConfigName());
} else {
bodyOrmShape = new Shape(field, getConsoleConfigName());
}
}
}
addChild(bodyOrmShape);
} else {
Shape bodyOrmShape = new ExpandableShape(field, getConsoleConfigName());
addChild(bodyOrmShape);
}
}
}
} else if (ormElement instanceof IPersistentClass && ((IPersistentClass) ormElement).isInstanceOfSubclass()) {
IPersistentClass rootClass = ((IPersistentClass) ormElement).getRootClass();
IProperty identifierProperty = rootClass.getIdentifierProperty();
if (identifierProperty != null) {
addChild(new Shape(identifierProperty, getConsoleConfigName()));
}
IValue identifier = rootClass.getIdentifier();
if (identifier.isComponent()) {
Iterator<IProperty> iterator = identifier.getPropertyIterator();
while (iterator.hasNext()) {
IProperty property = iterator.next();
addChild(new Shape(property, getConsoleConfigName()));
}
}
Iterator<IProperty> iterator = rootClass.getPropertyIterator();
while (iterator.hasNext()) {
IProperty field = iterator.next();
if (!field.isBackRef()) {
if (!field.isComposite()) {
IValue fieldValue = field.getValue();
boolean typeIsAccessible = true;
if (fieldValue.isSimpleValue() && fieldValue.isTypeSpecified()) {
try {
field.getValue().getType();
} catch (Exception e) {
typeIsAccessible = false;
}
}
Shape bodyOrmShape = null;
if (typeIsAccessible && field.getValue().isSimpleValue()) {
bodyOrmShape = new Shape(field, getConsoleConfigName());
} else if (typeIsAccessible && field.getValue().getType().isEntityType()) {
bodyOrmShape = new ExpandableShape(field, getConsoleConfigName());
} else if (typeIsAccessible && field.getValue().getType().isCollectionType()) {
bodyOrmShape = new ComponentShape(field, getConsoleConfigName());
} else {
bodyOrmShape = new Shape(field, getConsoleConfigName());
}
addChild(bodyOrmShape);
} else {
Shape bodyOrmShape = new ExpandableShape(field, getConsoleConfigName());
addChild(bodyOrmShape);
}
}
}
Iterator<IProperty> iter = ((IPersistentClass) ormElement).getPropertyIterator();
while (iter.hasNext()) {
IProperty property = iter.next();
if (!property.isBackRef()) {
if (!property.isComposite()) {
boolean typeIsAccessible = true;
IValue propertyValue = property.getValue();
if (propertyValue.isSimpleValue() && propertyValue.isTypeSpecified()) {
try {
property.getValue().getType();
} catch (Exception e) {
typeIsAccessible = false;
}
}
Shape bodyOrmShape = null;
if (typeIsAccessible && property.getValue().getType().isEntityType()) {
bodyOrmShape = new ExpandableShape(property, getConsoleConfigName());
} else if (typeIsAccessible && property.getValue().getType().isCollectionType()) {
bodyOrmShape = new ComponentShape(property, getConsoleConfigName());
} else {
bodyOrmShape = new Shape(property, getConsoleConfigName());
}
addChild(bodyOrmShape);
} else {
Shape bodyOrmShape = new ExpandableShape(property, getConsoleConfigName());
addChild(bodyOrmShape);
}
}
}
} else if (ormElement instanceof ITable) {
Iterator iterator = ((ITable) getOrmElement()).getColumnIterator();
while (iterator.hasNext()) {
IColumn column = (IColumn) iterator.next();
Shape bodyOrmShape = new Shape(column, getConsoleConfigName());
addChild(bodyOrmShape);
}
}
}
use of org.jboss.tools.hibernate.runtime.spi.IType in project jbosstools-hibernate by jbosstools.
the class OrmImageMap method getImageName.
/**
* the image name for hierarchy:
* Property
* @param field
* @return
*/
public static String getImageName(IProperty field, final ConsoleConfiguration cfg) {
// $NON-NLS-1$
String str = "Image_PersistentFieldSimple";
if (field == null) {
return str;
}
final IPersistentClass persistentClass = field.getPersistentClass();
if (persistentClass != null && persistentClass.getVersion() == field) {
// $NON-NLS-1$
str = "Image_PersistentFieldSimple_version";
} else if (persistentClass != null && persistentClass.getIdentifierProperty() == field) {
// $NON-NLS-1$
str = "Image_PersistentFieldSimple_id";
} else if (field.getValue() != null) {
final IValue value = field.getValue();
if (value.isOneToMany()) {
// $NON-NLS-1$
str = "Image_PersistentFieldOne-to-many";
} else if (value.isOneToOne()) {
// $NON-NLS-1$
str = "Image_PersistentFieldOne-to-one";
} else if (value.isManyToOne()) {
// $NON-NLS-1$
str = "Image_PersistentFieldMany-to-one";
} else if (value.isAny()) {
// $NON-NLS-1$
str = "Image_PersistentFieldAny";
} else {
IType type = UtilTypeExtract.getTypeUsingExecContext(value, cfg);
if (type != null && type.isCollectionType()) {
if (value.isPrimitiveArray()) {
// $NON-NLS-1$
str = "Image_Collection_primitive_array";
} else if (value.isArray()) {
// $NON-NLS-1$
str = "Image_Collection_array";
} else if (value.isList()) {
// $NON-NLS-1$
str = "Image_Collection_list";
} else if (value.isSet()) {
// $NON-NLS-1$
str = "Image_Collection_set";
} else if (value.isMap()) {
// $NON-NLS-1$
str = "Image_Collection_map";
} else if (value.isBag()) {
// $NON-NLS-1$
str = "Image_Collection_bag";
} else if (value.isIdentifierBag()) {
// $NON-NLS-1$
str = "Image_Collection_idbag";
} else {
// $NON-NLS-1$
str = "Image_Collection";
}
}
}
} else if ("parent".equals(field.getName())) {
// $NON-NLS-1$
// $NON-NLS-1$
str = "Image_PersistentFieldParent";
}
return str;
}
use of org.jboss.tools.hibernate.runtime.spi.IType in project jbosstools-hibernate by jbosstools.
the class QueryFacadeTest method testSetParameter.
@Test
public void testSetParameter() {
Type typeProxy = (Type) Proxy.newProxyInstance(FACADE_FACTORY.getClassLoader(), new Class[] { Type.class }, new TypeInvocationHandler());
IType typeFacade = FACADE_FACTORY.createType(typeProxy);
Object object = new Object();
query.setParameter(Integer.MAX_VALUE, object, typeFacade);
Assert.assertEquals("setParameter", methodName);
Assert.assertArrayEquals(new Object[] { Integer.MAX_VALUE, object, typeProxy }, arguments);
methodName = null;
arguments = null;
query.setParameter("foobar", object, typeFacade);
Assert.assertEquals("setParameter", methodName);
Assert.assertArrayEquals(new Object[] { "foobar", object, typeProxy }, arguments);
}
Aggregations