use of org.whole.lang.reflect.EntityDescriptor in project whole by wholeplatform.
the class QueriesDynamicCompilerVisitor method visit.
@Override
public void visit(Choose entity) {
int size = entity.wSize();
if (size == 1)
entity.get(0).accept(this);
else {
boolean canOptimize = true;
ILanguageKit languageKit = null;
Map<EntityDescriptor<?>, Expression> typeMap = new HashMap<EntityDescriptor<?>, Expression>();
If ifWithTemplate = new IfWithTemplate().create();
If ifWithTypeTest = new IfWithTypeTest().create();
ITransactionScope ts = BindingManagerFactory.instance.createTransactionScope();
getBindings().wEnterScope(ts);
for (int i = 0; i < size; i++) {
Expression child = entity.get(i);
try {
if (!Matcher.match(ifWithTemplate, child, getBindings()) && !Matcher.match(ifWithTypeTest, child, getBindings())) {
canOptimize = false;
break;
}
EntityDescriptor<?> ed = getBindings().wIsSet("typeTest") && getBindings().wGet("typeTest").wGetEntityDescriptor().getDataKind().isString() ? CommonsDataTypePersistenceParser.getEntityDescriptor(getBindings().wStringValue("typeTest")) : getBindings().wIsSet("pattern") ? getBindings().wGet("pattern").wGetEntityDescriptor() : null;
if (ed == null) {
canOptimize = false;
break;
}
if (typeMap.containsKey(ed)) {
Expression behavior = typeMap.get(ed);
boolean isPattern = behavior.wGetParent() == entity;
if (isPattern) {
canOptimize = false;
break;
}
} else {
if (languageKit == null)
languageKit = ed.getLanguageKit();
else if (!languageKit.equals(ed.getLanguageKit())) {
// ||
// !languageKit.getURI().equals("whole:org.whole.lang.javascript:JavaScript"))
// {
canOptimize = false;
break;
}
typeMap.put(ed, getBindings().wIsSet("pattern") ? child : getBindings().wGet("expression").wGetAdapter(QueriesEntityDescriptorEnum.Expression));
}
} finally {
ts.rollback();
}
}
getBindings().wExitScope();
if (canOptimize && languageKit != null) {
ChooseByTypeIterator<IEntity> chooseIterator = IteratorFactory.chooseIterator(languageKit);
for (Entry<EntityDescriptor<?>, Expression> entry : typeMap.entrySet()) {
Set<String> oldDeclaredNames = declaredNames;
entry.getValue().accept(this);
chooseIterator.setCase(entry.getKey(), getResultIterator());
declaredNames = oldDeclaredNames;
}
setResultIterator(chooseIterator.withSourceEntity(entity));
} else {
IEntityIterator<? extends IEntity>[] iteratorChain = new IEntityIterator<?>[size];
for (int i = 0; i < size; i++) {
entity.get(i).accept(this);
iteratorChain[i] = getResultIterator();
}
setResultIterator(IteratorFactory.chooseIterator(iteratorChain).withSourceEntity(entity));
}
}
}
use of org.whole.lang.reflect.EntityDescriptor in project whole by wholeplatform.
the class DBSchemaTemplateFactoryTest method testRDBSchemaTemplateFactory.
@Test
public void testRDBSchemaTemplateFactory() throws Exception {
IBindingManager bm = BindingManagerFactory.instance.createArguments();
bm.wDefValue("connection", connection);
bm.wDefValue("generateDropStatements", false);
Database template = new CycleModel().create();
Database database = EntityUtils.clone(template);
Matcher.removeVars(database, true);
InterpreterOperation.interpret(database, bm);
DBSchemaTemplateFactory dbSchemaTemplateFactory = new DBSchemaTemplateFactory(connection, null, "PUBLIC");
Database generatedDatabase = dbSchemaTemplateFactory.create();
HashMap<EntityDescriptor<?>, Comparator<IEntity>> comparatorsMap = new HashMap<EntityDescriptor<?>, Comparator<IEntity>>();
comparatorsMap.put(RDBEntityDescriptorEnum.Tables, new OrderedMatcher.SimpleFeatureComparator(RDBFeatureDescriptorEnum.name));
Assert.assertTrue(OrderedMatcher.match(template, generatedDatabase, comparatorsMap));
}
use of org.whole.lang.reflect.EntityDescriptor in project whole by wholeplatform.
the class CompiledMappingStrategy method isMixedType.
public boolean isMixedType(EntityDescriptor<?> ed) {
if (mixedTypes == null) {
mixedTypes = new HashSet<EntityDescriptor<?>>();
Types uris = strategy.getMixedTypes();
for (int i = 0, size = uris.wSize(); i < size; i++) mixedTypes.add(parseEntityDescriptor(uris.wGet(i).wStringValue()));
}
return mixedTypes.contains(ed);
}
use of org.whole.lang.reflect.EntityDescriptor in project whole by wholeplatform.
the class CommonsContentAssistVisitor method visit.
public void visit(VarType entity) {
try {
IEntity variableEntity = entity.wGetParent();
IEntity variableEntityParent = variableEntity.wGetParent();
Set<EntityDescriptor<?>> assignableSet = getAllAssignableTo(variableEntityParent.wGetEntityDescriptor(variableEntity));
assignableSet.remove(entity.getValue());
List<VarType> assignableEntities = new ArrayList<VarType>(assignableSet.size());
for (EntityDescriptor<?> ed : assignableSet) assignableEntities.add(CommonsEntityFactory.instance.createVarType(ed));
Collections.sort(assignableEntities, new Comparator<VarType>() {
public int compare(VarType e1, VarType e2) {
return CommonsDataTypePresentationParser.unparseEntityDescriptor(e1.getValue()).compareToIgnoreCase(CommonsDataTypePresentationParser.unparseEntityDescriptor(e2.getValue()));
}
});
getOperation().setResult(assignableEntities.toArray(new IEntity[0]));
} catch (Exception e) {
// do nothing on error
}
}
use of org.whole.lang.reflect.EntityDescriptor in project whole by wholeplatform.
the class DefaultValueFactory method setUniqueDefault.
public static void setUniqueDefault(IEntity entity) {
final String defaultPrefix = entity.wStringValue();
entity.wAddRequestEventHandler(new IdentityRequestEventHandler() {
int max = 0;
void updateMax(int value) {
if (value > max)
max = value;
}
public String notifyRequested(final IEntity source, FeatureDescriptor feature, String value) {
if (value.equals(defaultPrefix)) {
final EntityDescriptor<?> sourceDescriptor = source.wGetEntityDescriptor();
final EntityDescriptor<?> parentDescriptor = source.wGetParent().wGetEntityDescriptor();
GenericTraversalFactory.instance.topDown(new AbstractVisitor() {
public void visit(IEntity entity) {
if (entity == source)
return;
if (sourceDescriptor.equals(entity.wGetEntityDescriptor()) && parentDescriptor.equals(entity.wGetParent().wGetEntityDescriptor())) {
String strValue = entity.wStringValue();
if (strValue.startsWith(defaultPrefix)) {
try {
int value = Integer.parseInt(strValue.substring(defaultPrefix.length()));
updateMax(value);
} catch (NumberFormatException e) {
} catch (IndexOutOfBoundsException e) {
}
}
}
}
}, false).visit(EntityUtils.getCompoundRoot(source));
source.wSetValue(value = defaultPrefix + ++max);
source.wRemoveRequestEventHandler(this);
}
return value;
}
});
}
Aggregations