Search in sources :

Example 1 with EnumDeclaration

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

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

the class EnumDeclarationPart method getModelSpecificChildren.

protected List<IEntity> getModelSpecificChildren() {
    EnumDeclaration enumDeclaration = getModelEntity();
    List<IEntity> list = new ArrayList<IEntity>(3);
    list.add(enumDeclaration.getAnnotations());
    list.add(enumDeclaration.getName());
    list.add(enumDeclaration.getTemplate());
    list.add(enumDeclaration.getValues());
    return list;
}
Also used : IEntity(org.whole.lang.model.IEntity) ArrayList(java.util.ArrayList) EnumDeclaration(org.whole.lang.pojo.model.EnumDeclaration)

Aggregations

IEntity (org.whole.lang.model.IEntity)2 EnumDeclaration (org.whole.lang.pojo.model.EnumDeclaration)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ArrayType (org.whole.lang.pojo.model.ArrayType)1 CollectionType (org.whole.lang.pojo.model.CollectionType)1 DataTypeDeclaration (org.whole.lang.pojo.model.DataTypeDeclaration)1 MapType (org.whole.lang.pojo.model.MapType)1 PojoDeclaration (org.whole.lang.pojo.model.PojoDeclaration)1 PrimitiveType (org.whole.lang.pojo.model.PrimitiveType)1 ProductDeclaration (org.whole.lang.pojo.model.ProductDeclaration)1 ReferenceType (org.whole.lang.pojo.model.ReferenceType)1 Type (org.whole.lang.pojo.model.Type)1 PojoDeclaration (org.whole.lang.pojo.reflect.PojoEntityDescriptorEnum.PojoDeclaration)1 PrimitiveType (org.whole.lang.pojo.reflect.PojoEntityDescriptorEnum.PrimitiveType)1 ReferenceType (org.whole.lang.pojo.reflect.PojoEntityDescriptorEnum.ReferenceType)1