/*
 * Copyright 2016, Frederik Boster
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.syquel.bushytail.serializer;

import de.syquel.bushytail.serializer.exception.OlingoSerializerException;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.olingo.commons.api.data.Entity;
import org.apache.olingo.commons.api.data.Property;
import org.apache.olingo.commons.api.data.ValueType;
import org.apache.olingo.commons.api.edm.EdmEntityType;

import java.lang.reflect.Method;

/**
 * Converts Java objects to Olingo entities.
 *
 * @author Clemens Bartz
 * @author Frederik Boster
 * @since 1.0
 */
public class OlingoSerializer {

    public static <T> Entity serialize(EdmEntityType entityType, T entityObject) throws OlingoSerializerException {
        Entity olingoEntity = new Entity();
        olingoEntity.setType(entityType.getFullQualifiedName().getFullQualifiedNameAsString());

        for (String propertyName : entityType.getPropertyNames()) {
            try {
                Method propertyAccessor = PropertyUtils.getPropertyDescriptor(entityObject, propertyName).getReadMethod();
                Object value = propertyAccessor.invoke(entityObject);

                olingoEntity.addProperty(new Property(null, propertyName, ValueType.PRIMITIVE, value));
            } catch (Exception e) {
                throw new OlingoSerializerException("Cannot access property '" + propertyName + "' of class '" + entityObject.getClass() + "'", e);
            }
        }

        return olingoEntity;
    }

}

@@ -971,16 +971,73 @@
 ityType;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aimport
@@ -1193,16 +1193,22 @@
 %0Apublic 
+final 
 class Ol
@@ -1225,16 +1225,551 @@
 izer %7B%0A%0A
+    /** The logger. */%0A    private static final Logger logger = LoggerFactory.getLogger(OlingoSerializer.class);%0A%0A    /**%0A     * Hidden constructor.%0A     */%0A    private OlingoSerializer() %7B%0A%0A    %7D%0A%0A    /**%0A     * Convert an %7B@link T object%7D to an %7B@link Entity Olingo entity%7D.%0A     * @param entityType the type of the entity%0A     * @param entityObject the object to convert%0A     * @param %3CT%3E the type of the entity%0A     * @return an Olingo entity%0A     * @throws OlingoSerializerException if the method cannot access a property%0A     */%0A
     publ
@@ -1884,16 +1884,22 @@
         
+final 
 Entity o
@@ -2125,16 +2125,22 @@
         
+final 
 Method p
@@ -2254,16 +2254,22 @@
         
+final 
 Object v
@@ -2451,24 +2451,149 @@
 eption e) %7B%0A
+                logger.error(%22Cannot access property '%22 + propertyName + %22' of class '%22 + entityObject.getClass() + %22'%22, e);%0A
             

Document seralizer.