Example usage for org.apache.commons.beanutils BeanMap entrySet

List of usage examples for org.apache.commons.beanutils BeanMap entrySet

Introduction

In this page you can find the example usage for org.apache.commons.beanutils BeanMap entrySet.

Prototype

public Set entrySet() 

Source Link

Document

Gets a Set of MapEntry objects that are the mappings for this BeanMap.

Usage

From source file:org.sindice.rdfcommons.beanmapper.BeanDeserializer.java

@SuppressWarnings("unchecked")
public <T> T deserialize(DeserializationContext context, Class<T> clazz, Annotation[] annotations,
        Identifier identifier, QueryEndpoint endPoint) throws DeserializationException {

    final T bean;
    try {/*from ww w. jav  a2s  .  c o m*/
        bean = clazz.newInstance();
    } catch (Exception e) {
        throw new DeserializationException("Error while instantiating object.", e);
    }
    context.registerInstance(identifier, bean);

    final String classUrl = getClassURL(bean.getClass());
    final BeanMap beanMap = new BeanMap(bean);
    String propertyURL;
    for (Map.Entry<String, Object> entry : (Set<Map.Entry<String, Object>>) beanMap.entrySet()) {

        // Skipping self description.
        if ("class".equals(entry.getKey())) {
            continue;
        }

        final String propertyName = entry.getKey();
        final Method propertyWriteMethod = beanMap.getWriteMethod(propertyName);
        if (propertyWriteMethod == null) {
            throw new DeserializationException(
                    String.format("Cannot find setter for property '%s' in bean %s", propertyName, bean));
        }
        final Class propertyType = propertyWriteMethod.getParameterTypes()[0];

        // Skipping properties marked as ignored.
        if (isIgnored(propertyWriteMethod)) {
            continue;
        }

        final Object value = retrieveObject(classUrl, propertyName, propertyWriteMethod, identifier, endPoint,
                context);

        // Skipping missing values.
        if (value == null) {
            continue;
        }

        Object deserializedValue = context.deserialize(context, propertyType,
                propertyWriteMethod.getAnnotations(), new Identifier(value, Identifier.Type.resource),
                endPoint);

        try {
            propertyWriteMethod.invoke(bean, deserializedValue);
        } catch (Exception e) {
            throw new DeserializationException(
                    String.format("Error while setting value [%s] on bean [%s] using setter [%s].", value, bean,
                            propertyWriteMethod),
                    e);
        }
    }

    return bean;
}

From source file:org.sindice.rdfcommons.beanmapper.BeanSerializer.java

@SuppressWarnings("unchecked")
public Identifier serialize(SerializationContext context, Object bean, Annotation[] annotations,
        TripleSet buffer) throws SerializationException {

    // Handling adapted object.
    Object adapted = getAdapted(bean);
    if (adapted != null) {
        return context.serialize(context, adapted, annotations, buffer);
    }//  w  w w  .j av  a2 s  .  co m

    // Handling common bean.
    try {
        BeanMap beanMap = new BeanMap(bean);
        final String classUrl = getClassURL(bean.getClass());
        final String instanceUrl = getObjectURL(bean);

        buffer.addTriple(instanceUrl, RDFVocabulary.TYPE, classUrl);

        for (Map.Entry<String, Object> entry : (Set<Map.Entry<String, Object>>) beanMap.entrySet()) {
            final String propertyName = entry.getKey();
            final Method propertyReadMethod = beanMap.getReadMethod(propertyName);

            // Skipping self description.
            if ("class".equals(entry.getKey())) {
                continue;
            }
            // Skipping properties marked as ignored.
            if (isIgnored(propertyReadMethod)) {
                continue;
            }
            // Avoid serialization of null values.
            final Object propertyValue = entry.getValue();
            if (propertyValue == null) {
                continue;
            }

            Identifier identifierEntry = context.serialize(context, propertyValue,
                    getAnnotations(propertyReadMethod), buffer);

            buffer.addTriple(instanceUrl, getPropertyURL(classUrl, propertyName, propertyReadMethod),
                    identifierEntry.getId(), identifierEntry.isLiteral() ? ObjectType.literal : ObjectType.uri);

        }
        return null;
    } catch (Exception e) {
        throw new SerializationException(e);
    }
}

From source file:org.sindice.rdfcommons.beanmapper.StaticBeanDeserializer.java

public <T> T deserialize(DeserializationContext context, Class<T> clazz, Annotation[] annotations,
        Identifier identifier, QueryEndpoint endPoint) throws DeserializationException {

    if (identifier != null) {
        throw new IllegalArgumentException("for static deserialization the identifier is expected to be null.");
    }//  w  w w.  ja  va 2  s  .  c  om

    final Identifier staticIdentifier = getIdentifier(clazz, annotations);

    // Create the bean instance.
    final T instance;
    try {
        instance = clazz.newInstance();
    } catch (Exception e) {
        throw new DeserializationException(String
                .format("Error while creating instance of class %s: defualt constructor required.", clazz));
    }
    context.registerInstance(staticIdentifier, instance);

    // Define the bean map.
    final BeanMap beanMap = new BeanMap(instance);

    // Extracts the class property URLs.
    final Map<String, Method> propertyURLs = new HashMap<String, Method>();
    final String classURL = getClassURL(clazz);
    String propertyName;
    Method propertyWriteMethod;
    for (Map.Entry<String, Object> entry : (Set<Map.Entry<String, Object>>) beanMap.entrySet()) {
        // Skipping self description.
        if ("class".equals(entry.getKey())) {
            continue;
        }
        propertyName = entry.getKey();
        propertyWriteMethod = beanMap.getWriteMethod(propertyName);
        propertyURLs.put(getPropertyURL(classURL, propertyName, propertyWriteMethod), propertyWriteMethod);
    }

    // Retrieve the class triples.
    endPoint.addQuery((String) staticIdentifier.getId(), "?prop", "?value");
    ResultSet rs = endPoint.execute();

    // Coupling object properties with actual ones.
    Object property;
    Object value;
    Class propertyType;
    while (rs.hasNext()) {
        property = rs.getVariableValue("?prop");
        value = rs.getVariableValue("?value");
        rs.next();

        propertyWriteMethod = propertyURLs.get(property);
        if (propertyWriteMethod == null) {
            context.reportIssue(String.format("Cannot find write method for property URL %s", property));
            continue;
        }
        propertyType = propertyWriteMethod.getParameterTypes()[0];

        // Deserializing recursively.
        Object deserialized = context.deserialize(context, propertyType, propertyType.getAnnotations(),
                new Identifier(value, Identifier.Type.resource), endPoint);

        try {
            propertyWriteMethod.invoke(instance, deserialized);
        } catch (Exception e) {
            throw new DeserializationException(
                    String.format("Error while invoking write method %s of instance %s on value %s[%s]",
                            propertyWriteMethod, instance, deserialized, deserialized.getClass()));
        }
    }
    return instance;
}

From source file:org.sindice.rdfcommons.beanmapper.StaticBeanSerializer.java

public Identifier serialize(SerializationContext context, Object bean, Annotation[] annotations,
        TripleSet buffer) throws SerializationException {
    try {//from   www.j  a  v  a2 s. c o m
        BeanMap beanMap = new BeanMap(bean);
        final String classURL = getClassURL(bean.getClass());
        for (Map.Entry<String, Object> entry : (Set<Map.Entry<String, Object>>) beanMap.entrySet()) {

            final String propertyName = entry.getKey();
            final Method propertyReadMethod = beanMap.getReadMethod(propertyName);

            // Skipping self description.
            if ("class".equals(propertyName)) {
                continue;
            }
            // Skipping properties marked as ignored.
            if (isIgnored(propertyReadMethod)) {
                continue;
            }

            Object propertyValue = entry.getValue();

            Identifier identifierEntry = context.serialize(context, propertyValue,
                    getAnnotations(propertyReadMethod), buffer);

            buffer.addTriple(classURL, getPropertyURL(classURL, propertyName, propertyReadMethod),
                    identifierEntry.getId(), identifierEntry.isLiteral() ? ObjectType.literal : ObjectType.uri);

        }
        return null;
    } catch (Exception e) {
        e.printStackTrace();
        throw new SerializationException(e);
    }
}