Search in sources :

Example 1 with PojoDeclaration

use of org.whole.lang.pojo.model.PojoDeclaration 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 2 with PojoDeclaration

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

the class PojoNormalizerVisitor method createDefaultTypeInParameters.

public static void createDefaultTypeInParameters(Library entity, IBindingManager bindings) {
    Path findAllParametersWithoutType = (Path) PojoTemplateManager.instance().create("findAllParametersWithoutType");
    Path findParameterType = (Path) PojoTemplateManager.instance().create("findPropertyType");
    bindings.wEnterScope();
    for (Parameter parameter : BehaviorUtils.<Parameter>compileAndLazyEvaluate(findAllParametersWithoutType, entity, bindings)) {
        PojoDeclaration pojo = (PojoDeclaration) bindings.wGet("pojo");
        Type type = BehaviorUtils.<Type>evaluateFirstResult(findParameterType, pojo, bindings);
        if (type != null)
            parameter.setType(EntityUtils.clone(type));
        else
            throw new IllegalArgumentException("Missing " + bindings.wStringValue("name") + " property in " + pojo.getName().wStringValue() + " pojo declaration");
    }
    bindings.wExitScope();
}
Also used : Path(org.whole.lang.queries.model.Path) PojoDeclaration(org.whole.lang.pojo.model.PojoDeclaration) MapType(org.whole.lang.pojo.model.MapType) ArrayType(org.whole.lang.pojo.model.ArrayType) PrimitiveType(org.whole.lang.pojo.model.PrimitiveType) Type(org.whole.lang.pojo.model.Type) CollectionType(org.whole.lang.pojo.model.CollectionType) ReferenceType(org.whole.lang.pojo.model.ReferenceType) Parameter(org.whole.lang.pojo.model.Parameter)

Example 3 with PojoDeclaration

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

the class PojoDeclarationPart method getModelSpecificChildren.

protected List<IEntity> getModelSpecificChildren() {
    PojoDeclaration pojoDeclaration = getModelEntity();
    List<IEntity> list = new ArrayList<IEntity>(6);
    list.add(pojoDeclaration.getAnnotations());
    list.add(pojoDeclaration.getName());
    list.add(pojoDeclaration.getTemplate());
    list.add(pojoDeclaration.getTypes());
    list.add(pojoDeclaration.getProperties());
    list.add(pojoDeclaration.getConstructors());
    return list;
}
Also used : PojoDeclaration(org.whole.lang.pojo.model.PojoDeclaration) IEntity(org.whole.lang.model.IEntity) ArrayList(java.util.ArrayList)

Example 4 with PojoDeclaration

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

the class PojoUtils method translate.

public static void translate(IEntity fromEntity, Object toObject, PojoDeclaration pojoDeclaration, Library library) {
    // translate inherited properties
    IEntityIterator<ReferenceType> superPojosIterator = IteratorFactory.<ReferenceType>childIterator();
    superPojosIterator.reset(pojoDeclaration.getTypes());
    for (ReferenceType superType : superPojosIterator) {
        PojoDeclaration superDeclaration = (PojoDeclaration) findProductDeclaration(superType, library);
        translate(fromEntity, toObject, superDeclaration, library);
    }
    // translate declared properties
    IEntityIterator<Property> iterator = IteratorFactory.<Property>childIterator();
    iterator.reset(pojoDeclaration.getProperties());
    EntityDescriptor<?> ed = fromEntity.wGetEntityDescriptor();
    Property property = null;
    try {
        while (iterator.hasNext()) {
            property = iterator.next();
            if (isReadOnly(property))
                continue;
            Type type = property.getType();
            Name template = property.getTemplate();
            FeatureDescriptor fd = ed.getFeatureDescriptorEnum().valueOf(template.wStringValue());
            IEntity fieldEntity = fromEntity.wGet(fd);
            if (!EntityUtils.isNotResolver(fieldEntity))
                continue;
            if (Matcher.match(PrimitiveType, type))
                setPropertyValue(property, toObject, fieldEntity.wGetValue());
            else
                setPropertyValue(property, toObject, create(fieldEntity, library));
        }
    } catch (Exception e) {
        throw new IllegalStateException("Cannot translate property: " + property, e);
    }
}
Also used : 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) FeatureDescriptor(org.whole.lang.reflect.FeatureDescriptor) IEntity(org.whole.lang.model.IEntity) Property(org.whole.lang.pojo.model.Property) ReferenceType(org.whole.lang.pojo.reflect.PojoEntityDescriptorEnum.ReferenceType) ReferenceType(org.whole.lang.pojo.model.ReferenceType) InvocationTargetException(java.lang.reflect.InvocationTargetException) Name(org.whole.lang.pojo.model.Name)

Example 5 with PojoDeclaration

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

the class PojoNormalizerVisitor method createDefaultTemplateInParameters.

public static void createDefaultTemplateInParameters(Library entity, IBindingManager bindings) {
    Path findAllParametersWithoutTemlate = (Path) PojoTemplateManager.instance().create("findAllParametersWithoutTemlate");
    Path findParameterTemplate = (Path) PojoTemplateManager.instance().create("findPropertyTemplate");
    bindings.wEnterScope();
    for (Parameter parameter : BehaviorUtils.<Parameter>compileAndLazyEvaluate(findAllParametersWithoutTemlate, entity, bindings)) {
        PojoDeclaration pojo = (PojoDeclaration) bindings.wGet("pojo");
        Name name = BehaviorUtils.<Name>evaluateFirstResult(findParameterTemplate, pojo, bindings);
        if (name != null)
            parameter.setTemplate(EntityUtils.clone(name));
    }
    bindings.wExitScope();
}
Also used : Path(org.whole.lang.queries.model.Path) PojoDeclaration(org.whole.lang.pojo.model.PojoDeclaration) Parameter(org.whole.lang.pojo.model.Parameter) Name(org.whole.lang.pojo.model.Name)

Aggregations

PojoDeclaration (org.whole.lang.pojo.model.PojoDeclaration)5 IEntity (org.whole.lang.model.IEntity)3 ArrayType (org.whole.lang.pojo.model.ArrayType)3 CollectionType (org.whole.lang.pojo.model.CollectionType)3 MapType (org.whole.lang.pojo.model.MapType)3 PrimitiveType (org.whole.lang.pojo.model.PrimitiveType)3 ReferenceType (org.whole.lang.pojo.model.ReferenceType)3 Type (org.whole.lang.pojo.model.Type)3 Name (org.whole.lang.pojo.model.Name)2 Parameter (org.whole.lang.pojo.model.Parameter)2 PojoDeclaration (org.whole.lang.pojo.reflect.PojoEntityDescriptorEnum.PojoDeclaration)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 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 DataTypeDeclaration (org.whole.lang.pojo.model.DataTypeDeclaration)1 EnumDeclaration (org.whole.lang.pojo.model.EnumDeclaration)1 ProductDeclaration (org.whole.lang.pojo.model.ProductDeclaration)1