com.angstoverseer.persistence.converter.GoogleAppEngineEntityConverter.java Source code

Java tutorial

Introduction

Here is the source code for com.angstoverseer.persistence.converter.GoogleAppEngineEntityConverter.java

Source

/* 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.angstoverseer.persistence.converter;

import com.google.appengine.api.datastore.Entity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by pedro on 3/3/14.
 */
@Component
public class GoogleAppEngineEntityConverter implements EntityConverter {
    private static final Logger log = LoggerFactory.getLogger(GoogleAppEngineEntityConverter.class.getName());

    @Override
    public Object convert(Entity entity, Class klass) {
        log.debug("convert: {}, {}", entity, klass);

        final Object convertedInstance;
        try {
            convertedInstance = klass.newInstance();
        } catch (Exception e) {
            throw new RuntimeException("Unable to instantiate " + klass.getName(), e);
        }

        Method[] methods = klass.getDeclaredMethods();
        Map<String, Object> propertiesToBeSet = getPropertiesToBeSet(entity, methods);
        for (Method method : methods) {
            if (method.getName().startsWith("set")
                    && propertiesToBeSet.containsKey(extractPropertyNameFromSetterMethod(method))) {
                log.debug("method.getName(): {}", method.getName());

                try {
                    final Object newValue = propertiesToBeSet.get(extractPropertyNameFromSetterMethod(method));
                    log.debug("newValue: {}", newValue);
                    method.invoke(convertedInstance, newValue);
                } catch (Exception e) {
                    throw new RuntimeException(
                            "Unable to invoke method " + klass.getName() + "." + method.getName(), e);
                }
            }
        }

        return convertedInstance;
    }

    private String extractPropertyNameFromSetterMethod(Method method) {
        return method.getName().substring("set".length());
    }

    private Map<String, Object> getPropertiesToBeSet(Entity entity, Method[] methods) {
        Map<String, Object> propertiesToBeSet = new HashMap<>();
        for (Method method : methods) {
            final String propertyName;
            if (method.getName().startsWith("get") || method.getName().startsWith("is")) {
                if (method.getName().startsWith("get")) {
                    propertyName = method.getName().substring("get".length());
                } else {
                    propertyName = method.getName().substring("is".length());
                }
            } else {
                continue;
            }

            String lowerCasePropertyName = propertyName.toLowerCase();
            if (entity.hasProperty(lowerCasePropertyName)) {
                Object propertyValue = entity.getProperty(lowerCasePropertyName);
                propertiesToBeSet.put(propertyName, propertyValue);
            }
        }

        return propertiesToBeSet;
    }
}