Search in sources :

Example 1 with ProductDeclaration

use of org.whole.lang.pojo.model.ProductDeclaration in project whole by wholeplatform.

the class PojoUtils method create.

@SuppressWarnings("unchecked")
public static <E extends IEntity> E create(Object fromObject, Type type, Library library) {
    // map product type
    ProductDeclaration productDeclaration = findProductDeclaration(type, library);
    E toIEntity = PojoUtils.<E>createEntity(productDeclaration, library);
    // translate contents as needed
    switch(productDeclaration.wGetEntityDescriptor().getOrdinal()) {
        case PojoDeclaration_ord:
            translate(fromObject, toIEntity, (PojoDeclaration) productDeclaration, library);
            break;
        case EnumDeclaration_ord:
            EnumValue enumValue = DefaultDataTypePersistenceParser.instance.parseEnumValue(toIEntity.wGetEntityDescriptor(), fromObject.toString());
            toIEntity.wSetValue(enumValue);
            break;
        case // TODO
        AnnotationDeclaration_ord:
            break;
        case DataTypeDeclaration_ord:
            Type elementType;
            switch(type.wGetEntityDescriptor().getOrdinal()) {
                case PrimitiveType_ord:
                case ReferenceType_ord:
                    toIEntity.wSetValue(fromObject);
                    break;
                case ArrayType_ord:
                    ArrayType arrayType = (ArrayType) type;
                    elementType = arrayType.getElementType();
                    for (int i = 0; i < Array.getLength(fromObject); i++) toIEntity.wAdd(create(Array.get(fromObject, i), elementType, library));
                    break;
                case CollectionType_ord:
                    CollectionType collectionType = (CollectionType) type;
                    Collection<Object> fromCollection = (Collection<Object>) fromObject;
                    elementType = collectionType.getElementType();
                    for (Object element : fromCollection) toIEntity.wAdd(create(element, elementType, library));
                    break;
                case MapType_ord:
                    MapType mapType = (MapType) type;
                    Map<Object, Object> fromMap = (Map<Object, Object>) fromObject;
                    Type keyType = mapType.getKeyType();
                    elementType = mapType.getValueType();
                    for (Entry<Object, Object> element : fromMap.entrySet()) // FIXME workaround for Java 8 compiler
                    toIEntity.wSet(// FIXME workaround for Java 8 compiler
                    (IEntity) create(element.getKey(), keyType, library), create(element.getValue(), elementType, library));
                    break;
            }
            break;
    }
    return toIEntity;
}
Also used : IEntity(org.whole.lang.model.IEntity) EnumValue(org.whole.lang.model.EnumValue) MapType(org.whole.lang.pojo.model.MapType) ArrayType(org.whole.lang.pojo.model.ArrayType) PrimitiveType(org.whole.lang.pojo.model.PrimitiveType) PrimitiveType(org.whole.lang.pojo.reflect.PojoEntityDescriptorEnum.PrimitiveType) Type(org.whole.lang.pojo.model.Type) MapType(org.whole.lang.pojo.model.MapType) ArrayType(org.whole.lang.pojo.model.ArrayType) ReferenceType(org.whole.lang.pojo.reflect.PojoEntityDescriptorEnum.ReferenceType) CollectionType(org.whole.lang.pojo.model.CollectionType) ReferenceType(org.whole.lang.pojo.model.ReferenceType) ProductDeclaration(org.whole.lang.pojo.model.ProductDeclaration) CollectionType(org.whole.lang.pojo.model.CollectionType) Collection(java.util.Collection) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with ProductDeclaration

use of org.whole.lang.pojo.model.ProductDeclaration in project whole by wholeplatform.

the class PojoUtils method create.

/*
	 * expects a normalized library
	 */
public static Object create(IEntity fromEntity, Library library) {
    // find product type
    EntityDescriptor<?> ed = fromEntity.wGetEntityDescriptor();
    ProductDeclaration productDeclaration = findProductDeclarationByTemplateName(ed, library);
    if (productDeclaration == null)
        throw new IllegalStateException("Cannot find type mapping for entity: " + fromEntity);
    // translate contents as needed
    Object toObject = null;
    switch(productDeclaration.wGetEntityDescriptor().getOrdinal()) {
        case PojoDeclaration_ord:
            toObject = createInstance(fromEntity, (PojoDeclaration) productDeclaration, library);
            translate(fromEntity, toObject, (PojoDeclaration) productDeclaration, library);
            break;
        case EnumDeclaration_ord:
            String enumValue = DefaultDataTypePersistenceParser.instance.unparseEnumValue(fromEntity.wGetEntityDescriptor(), fromEntity.wEnumValue());
            toObject = createEnumValue((EnumDeclaration) productDeclaration, enumValue);
            break;
        case // TODO
        AnnotationDeclaration_ord:
            break;
        case DataTypeDeclaration_ord:
            Type type = ((DataTypeDeclaration) productDeclaration).getName();
            switch(type.wGetEntityDescriptor().getOrdinal()) {
                case PrimitiveType_ord:
                case ReferenceType_ord:
                    toObject = fromEntity.wGetValue();
                    break;
                case ArrayType_ord:
                    toObject = Array.newInstance(getClass(((ArrayType) type).getElementType()), fromEntity.wSize());
                    for (int i = 0; i < fromEntity.wSize(); i++) {
                        IEntity element = fromEntity.wGet(i);
                        Array.set(toObject, i, create(element, library));
                    }
                    break;
                case CollectionType_ord:
                    Collection<Object> toCollection = ((CollectionType) type).getCollectionInterface().getValue().equals(CollectionInterfaceEnum.Set) ? new HashSet<Object>() : new ArrayList<Object>();
                    IEntityIterator<IEntity> ci = IteratorFactory.childIterator();
                    ci.reset(fromEntity);
                    for (IEntity feature : ci) toCollection.add(create(feature, library));
                    toObject = toCollection;
                    break;
                case MapType_ord:
                    Map<Object, Object> toMap = new HashMap<Object, Object>();
                    for (int i = 0; i < fromEntity.wSize(); i++) {
                        IEntity key = fromEntity.wGet(i);
                        IEntity value = fromEntity.wGet(key);
                        toMap.put(create(key, library), create(value, library));
                    }
                    toObject = toMap;
                    break;
            }
            break;
    }
    return toObject;
}
Also used : IEntity(org.whole.lang.model.IEntity) HashMap(java.util.HashMap) EnumDeclaration(org.whole.lang.pojo.model.EnumDeclaration) PojoDeclaration(org.whole.lang.pojo.reflect.PojoEntityDescriptorEnum.PojoDeclaration) PojoDeclaration(org.whole.lang.pojo.model.PojoDeclaration) PrimitiveType(org.whole.lang.pojo.model.PrimitiveType) PrimitiveType(org.whole.lang.pojo.reflect.PojoEntityDescriptorEnum.PrimitiveType) Type(org.whole.lang.pojo.model.Type) MapType(org.whole.lang.pojo.model.MapType) ArrayType(org.whole.lang.pojo.model.ArrayType) ReferenceType(org.whole.lang.pojo.reflect.PojoEntityDescriptorEnum.ReferenceType) CollectionType(org.whole.lang.pojo.model.CollectionType) ReferenceType(org.whole.lang.pojo.model.ReferenceType) ProductDeclaration(org.whole.lang.pojo.model.ProductDeclaration) DataTypeDeclaration(org.whole.lang.pojo.model.DataTypeDeclaration)

Example 3 with ProductDeclaration

use of org.whole.lang.pojo.model.ProductDeclaration in project whole by wholeplatform.

the class PojoNormalizerVisitor method resolveTemplateNameCollisionsInDeclarations.

public static void resolveTemplateNameCollisionsInDeclarations(Library entity, IBindingManager bindings, FreshNameGenerator entityNameGenerator) {
    Path findAllProductDeclarationsWithTemplateName = (Path) PojoTemplateManager.instance().create("findAllProductDeclarationsWithTemplateName");
    bindings.wEnterScope();
    for (ProductDeclaration declaration : BehaviorUtils.<ProductDeclaration>compileAndLazyEvaluate(findAllProductDeclarationsWithTemplateName, entity, bindings)) {
        Template template = declaration.getTemplate();
        template.wSetValue(entityNameGenerator.nextFreshName(StringUtils.toSimpleName(template.wStringValue())));
    }
    bindings.wExitScope();
}
Also used : Path(org.whole.lang.queries.model.Path) ProductDeclaration(org.whole.lang.pojo.model.ProductDeclaration) Template(org.whole.lang.pojo.model.Template)

Example 4 with ProductDeclaration

use of org.whole.lang.pojo.model.ProductDeclaration in project whole by wholeplatform.

the class PojoNormalizerVisitor method createDefaultTemplateInDeclarations.

public static void createDefaultTemplateInDeclarations(Library entity, IBindingManager bindings, FreshNameGenerator entityNameGenerator) {
    PojoEntityFactory pef = PojoEntityFactory.instance;
    Path findAllProductDeclarationsWithEmptyTemplate = (Path) PojoTemplateManager.instance().create("findAllProductDeclarationsWithEmptyTemplate");
    bindings.wEnterScope();
    for (ProductDeclaration declaration : BehaviorUtils.<ProductDeclaration>compileAndLazyEvaluate(findAllProductDeclarationsWithEmptyTemplate, entity, bindings)) {
        String name = StringUtils.toSimpleName(bindings.wStringValue("name"));
        declaration.setTemplate(pef.createName(entityNameGenerator.nextFreshName(name)));
    }
    bindings.wExitScope();
}
Also used : PojoEntityFactory(org.whole.lang.pojo.factories.PojoEntityFactory) Path(org.whole.lang.queries.model.Path) ProductDeclaration(org.whole.lang.pojo.model.ProductDeclaration)

Example 5 with ProductDeclaration

use of org.whole.lang.pojo.model.ProductDeclaration in project whole by wholeplatform.

the class PojoUtils method findProductDeclaration.

public static ProductDeclaration findProductDeclaration(Type type, Library entity) {
    IBindingManager bindings = BindingManagerFactory.instance.createArguments();
    bindings.wDef("name", type);
    return BehaviorUtils.<ProductDeclaration>evaluateFirstResult((Expression) PojoTemplateManager.instance().share("findProductDeclarationByName"), entity, bindings);
}
Also used : ProductDeclaration(org.whole.lang.pojo.model.ProductDeclaration) IBindingManager(org.whole.lang.bindings.IBindingManager)

Aggregations

ProductDeclaration (org.whole.lang.pojo.model.ProductDeclaration)6 HashMap (java.util.HashMap)2 IEntity (org.whole.lang.model.IEntity)2 ArrayType (org.whole.lang.pojo.model.ArrayType)2 CollectionType (org.whole.lang.pojo.model.CollectionType)2 MapType (org.whole.lang.pojo.model.MapType)2 PrimitiveType (org.whole.lang.pojo.model.PrimitiveType)2 ReferenceType (org.whole.lang.pojo.model.ReferenceType)2 Type (org.whole.lang.pojo.model.Type)2 PrimitiveType (org.whole.lang.pojo.reflect.PojoEntityDescriptorEnum.PrimitiveType)2 ReferenceType (org.whole.lang.pojo.reflect.PojoEntityDescriptorEnum.ReferenceType)2 Path (org.whole.lang.queries.model.Path)2 Collection (java.util.Collection)1 Map (java.util.Map)1 IBindingManager (org.whole.lang.bindings.IBindingManager)1 EnumValue (org.whole.lang.model.EnumValue)1 PojoEntityFactory (org.whole.lang.pojo.factories.PojoEntityFactory)1 DataTypeDeclaration (org.whole.lang.pojo.model.DataTypeDeclaration)1 EnumDeclaration (org.whole.lang.pojo.model.EnumDeclaration)1 PojoDeclaration (org.whole.lang.pojo.model.PojoDeclaration)1