Search in sources :

Example 6 with IEntityFactory

use of org.whole.lang.factories.IEntityFactory in project whole by wholeplatform.

the class JSONLDEntityDecoder method decode.

public IEntity decode(org.whole.lang.json.model.Object jsonEntity) {
    String edUri = null;
    Value body = jsonEntity;
    for (int i = 0; i < jsonEntity.size(); i++) {
        Pair pair = jsonEntity.get(i);
        String name = pair.getName().getValue();
        if (name.equals("@type"))
            edUri = pair.getValue().wStringValue();
        else if (name.equals("@list") || name.equals("@set") || name.equals("@value"))
            body = pair.getValue();
    }
    if (edUri == null)
        return super.applyRules(jsonEntity);
    EntityDescriptor<?> ed = CommonsDataTypePersistenceParser.getEntityDescriptor(edUri, true, null);
    IEntityFactory ef = GenericEntityFactory.instance(RegistryConfigurations.RESOLVER);
    IEntity entity = ef.create(ed);
    switch(ed.getEntityKind()) {
        case SIMPLE:
            FeatureDescriptorEnum fdEnum = ed.getFeatureDescriptorEnum();
            for (int i = 0; i < body.wSize(); i++) {
                Pair pair = (Pair) body.wGet(i);
                String name = pair.getName().getValue();
                if (!name.startsWith("@")) {
                    FeatureDescriptor fd = fdEnum.valueOf(name);
                    entity.wSet(fd, applyRules(pair.getValue()));
                }
            }
            break;
        case COMPOSITE:
            for (int i = 0; i < body.wSize(); i++) entity.wAdd(applyRules(body.wGet(i)));
            break;
        case DATA:
            DataTypeUtils.convert(body, entity);
    }
    return entity;
}
Also used : FeatureDescriptorEnum(org.whole.lang.reflect.FeatureDescriptorEnum) IEntity(org.whole.lang.model.IEntity) FeatureDescriptor(org.whole.lang.reflect.FeatureDescriptor) IEntityFactory(org.whole.lang.factories.IEntityFactory) Value(org.whole.lang.json.model.Value) Pair(org.whole.lang.json.model.Pair)

Example 7 with IEntityFactory

use of org.whole.lang.factories.IEntityFactory in project whole by wholeplatform.

the class WorkflowsInterpreterVisitor method visit.

@Override
public void visit(CreateEntity entity) {
    try {
        entity.getEntityName().accept(this);
        String typeName = getResultString();
        EntityDescriptor<?> ed = CommonsDataTypePersistenceParser.parseEntityDescriptor(typeName);
        if (ed == null)
            throw new WholeIllegalArgumentException("The requested entity does not exist: " + typeName).withSourceEntity(entity).withBindings(getBindings());
        IEntityRegistryProvider provider = null;
        switch(entity.getRegistry().getValue().getOrdinal()) {
            case RegistryEnum.DEFAULT_ord:
                provider = RegistryConfigurations.DEFAULT;
                break;
            case RegistryEnum.RESOLVER_ord:
                provider = RegistryConfigurations.RESOLVER;
                break;
            case RegistryEnum.ADAPTER_ord:
                provider = RegistryConfigurations.ADAPTER;
                break;
            case RegistryEnum.STRICT_ord:
                provider = RegistryConfigurations.STRICT;
                break;
            case RegistryEnum.CUSTOM_ord:
                provider = RegistryConfigurations.CUSTOM;
                break;
        }
        IEntityFactory ef = GenericEntityFactory.instance(provider);
        IEntity model;
        Arguments arguments = entity.getArguments();
        if (Matcher.matchImpl(WorkflowsEntityDescriptorEnum.Assignments, arguments)) {
            ITransactionScope resettableScope = BindingManagerFactory.instance.createTransactionScope();
            getBindings().wEnterScope(resettableScope);
            arguments.accept(this);
            for (int i = 0; i < arguments.wSize(); i++) {
                String name = ((Assignments) arguments).get(i).getName().getValue();
                FeatureDescriptor fd = ed.getFeatureDescriptorEnum().valueOf(name);
                if (fd != null)
                    getBindings().wDef(name, EntityUtils.convertCloneIfReparenting(getBindings().wGet(name), ed.getEntityFeatureDescriptor(fd)));
            }
            model = ef.create(ed, getBindings());
            resettableScope.rollback();
            getBindings().wExitScope();
        } else if (Matcher.matchImpl(WorkflowsEntityDescriptorEnum.Expressions, arguments)) {
            IEntity selfEntity = getBindings().wGet("self");
            if (ed.getEntityKind().isData()) {
                ((Expressions) arguments).get(0).accept(this);
                model = DataTypeUtils.convertCloneIfParented(getResult(), ed);
                resetSelfEntity(selfEntity);
            } else {
                IEntity[] values = new IEntity[arguments.wSize()];
                for (int i = 0; i < values.length; i++) {
                    ((Expressions) arguments).get(i).accept(this);
                    values[i] = EntityUtils.convertCloneIfReparenting(getResult(), ed.getEntityFeatureDescriptor(i));
                    resetSelfEntity(selfEntity);
                }
                model = ef.create(ed, values);
            }
        } else
            model = ef.create(ed);
        setResult(entity.getModel(), model);
    } catch (Exception e) {
        throw IWholeRuntimeException.asWholeException(e, entity, getBindings());
    }
}
Also used : ITransactionScope(org.whole.lang.bindings.ITransactionScope) IEntity(org.whole.lang.model.IEntity) IEntityFactory(org.whole.lang.factories.IEntityFactory) Arguments(org.whole.lang.workflows.model.Arguments) Assignments(org.whole.lang.workflows.model.Assignments) WholeIllegalArgumentException(org.whole.lang.exceptions.WholeIllegalArgumentException) MissingVariableException(org.whole.lang.visitors.MissingVariableException) IOException(java.io.IOException) IWholeRuntimeException(org.whole.lang.exceptions.IWholeRuntimeException) VisitException(org.whole.lang.visitors.VisitException) ParseException(org.whole.lang.parsers.ParseException) WholeIllegalArgumentException(org.whole.lang.exceptions.WholeIllegalArgumentException) FeatureDescriptor(org.whole.lang.reflect.FeatureDescriptor) IEntityRegistryProvider(org.whole.lang.factories.IEntityRegistryProvider) Expressions(org.whole.lang.workflows.model.Expressions)

Example 8 with IEntityFactory

use of org.whole.lang.factories.IEntityFactory in project whole by wholeplatform.

the class ReflectionServer method createEntity.

public <E extends IEntity> E createEntity(EntityDescriptor<E> ed) {
    IEntityFactory ef = GenericEntityFactory.instance;
    E entity = ef.create(ed);
    // TODO add reflectionServer to entity compound model
    // TODO ? set pid and put in pidInstanceMap or put in rootInstances and lazy set pid in AbstractEntiy
    getPid(entity);
    return entity;
}
Also used : IEntityFactory(org.whole.lang.factories.IEntityFactory)

Example 9 with IEntityFactory

use of org.whole.lang.factories.IEntityFactory in project whole by wholeplatform.

the class BindingManagerFactory method createNames.

public IEntity createNames(Set<String> includeNames) {
    IEntityFactory ef = GenericEntityFactory.instance;
    IEntity names = ef.create(getEdEnum().valueOf("Names"));
    for (String name : includeNames) names.wAdd(ef.create(getEdEnum().valueOf("Name"), name));
    return names;
}
Also used : IEntity(org.whole.lang.model.IEntity) IEntityFactory(org.whole.lang.factories.IEntityFactory)

Example 10 with IEntityFactory

use of org.whole.lang.factories.IEntityFactory in project whole by wholeplatform.

the class BindingManagerFactory method createFlatBindingsModel.

public IEntity createFlatBindingsModel(IBindingManager bm, Set<String> includeNames) {
    IEntityFactory ef = GenericEntityFactory.instance;
    IEntity bindings = ef.create(getEdEnum().valueOf("Bindings"));
    Set<String> names = bm.wNames();
    for (String name : new TreeSet<String>(includeNames)) bindings.wAdd(createBinding(name, names.contains(name) ? bm.wGet(name) : CommonsEntityFactory.instance.createResolver()));
    return bindings;
}
Also used : IEntity(org.whole.lang.model.IEntity) IEntityFactory(org.whole.lang.factories.IEntityFactory) TreeSet(java.util.TreeSet)

Aggregations

IEntityFactory (org.whole.lang.factories.IEntityFactory)11 IEntity (org.whole.lang.model.IEntity)10 FeatureDescriptor (org.whole.lang.reflect.FeatureDescriptor)3 TreeSet (java.util.TreeSet)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 ITransactionScope (org.whole.lang.bindings.ITransactionScope)1 CommonsFeatureDescriptorEnum (org.whole.lang.commons.reflect.CommonsFeatureDescriptorEnum)1 IWholeRuntimeException (org.whole.lang.exceptions.IWholeRuntimeException)1 WholeIllegalArgumentException (org.whole.lang.exceptions.WholeIllegalArgumentException)1 IEntityRegistryProvider (org.whole.lang.factories.IEntityRegistryProvider)1 Pair (org.whole.lang.json.model.Pair)1 Value (org.whole.lang.json.model.Value)1 ParseException (org.whole.lang.parsers.ParseException)1 FeatureDescriptorEnum (org.whole.lang.reflect.FeatureDescriptorEnum)1 MissingVariableException (org.whole.lang.visitors.MissingVariableException)1 VisitException (org.whole.lang.visitors.VisitException)1 Arguments (org.whole.lang.workflows.model.Arguments)1