com.khubla.cbean.serializer.impl.json.JSONListFieldSerializer.java Source code

Java tutorial

Introduction

Here is the source code for com.khubla.cbean.serializer.impl.json.JSONListFieldSerializer.java

Source

/*
 * cBean Copyright 2016, Tom Everett <tom@khubla.com>
 *
 *   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.khubla.cbean.serializer.impl.json;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;
import org.json.JSONArray;

import com.khubla.cbean.CBean;
import com.khubla.cbean.CBeanKey;
import com.khubla.cbean.CBeanServer;
import com.khubla.cbean.annotation.Property;
import com.khubla.cbean.serializer.FieldSerializer;
import com.khubla.cbean.serializer.SerializerException;

public class JSONListFieldSerializer implements FieldSerializer {
    public JSONListFieldSerializer() {
    }

    @Override
    public boolean applies(Field field) {
        final Class<?> type = field.getType();
        return type == List.class;
    }

    @Override
    public void delete(Object o, Field field) throws SerializerException {
        final Property property = field.getAnnotation(Property.class);
        if ((null != property) && (property.cascadeDelete() == true)) {
            try {
                /*
                 * get the parameterized type
                 */
                final ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
                final Class<?> containedType = (Class<?>) parameterizedType.getActualTypeArguments()[0];
                /*
                 * get cBean
                 */
                final CBean<Object> cBean = CBeanServer.getInstance().getCBean(containedType);
                /*
                 * get the array object
                 */
                @SuppressWarnings("unchecked")
                final List<Object> list = (List<Object>) PropertyUtils.getProperty(o, field.getName());
                /*
                 * iterate
                 */
                for (final Object obj : list) {
                    final String key = cBean.getId(obj);
                    cBean.delete(new CBeanKey(key));
                }
            } catch (final Exception e) {
                throw new SerializerException(e);
            }
        }
    }

    @Override
    public void deserialize(Object o, Field field, String value) throws SerializerException {
        try {
            /*
             * get the parameterized type
             */
            final ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
            final Class<?> containedType = (Class<?>) parameterizedType.getActualTypeArguments()[0];
            /*
             * get cBean
             */
            final CBean<?> cBean = CBeanServer.getInstance().getCBean(containedType);
            /*
             * get the array object
             */
            @SuppressWarnings("unchecked")
            final List<Object> list = (List<Object>) PropertyUtils.getProperty(o, field.getName());
            /*
             * iterate
             */
            final JSONArray jsonArray = new JSONArray(value);
            for (int i = 0; i < jsonArray.length(); i++) {
                final String key = jsonArray.getString(i);
                final Object oo = cBean.load(new CBeanKey(key));
                /*
                 * oo could be null. if that happens, it can be because the underlying object has been deleted. this is ok. We add the null into the the list so that we preserve the list LENGTH
                 */
                list.add(oo);
            }
        } catch (final Exception e) {
            throw new SerializerException(e);
        }
    }

    @Override
    public String serialize(Object o, Field field) throws SerializerException {
        try {
            /*
             * get the parameterized type
             */
            final ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
            final Class<?> containedType = (Class<?>) parameterizedType.getActualTypeArguments()[0];
            /*
             * get cBean
             */
            final CBean<Object> cBean = CBeanServer.getInstance().getCBean(containedType);
            /*
             * get the list
             */
            @SuppressWarnings("unchecked")
            final List<Object> list = (List<Object>) PropertyUtils.getProperty(o, field.getName());
            /*
             * iterate and save contained objects
             */
            final Property property = field.getAnnotation(Property.class);
            if (property.cascadeSave() == true) {
                for (int i = 0; i < list.size(); i++) {
                    cBean.save(list.get(i));
                }
            }
            /*
             * iterate and grab the keys
             */
            final List<String> keys = new ArrayList<String>();
            for (int i = 0; i < list.size(); i++) {
                final String key = cBean.getId(list.get(i));
                keys.add(key);
            }
            final JSONArray jsonArray = new JSONArray(keys);
            return jsonArray.toString();
        } catch (final Exception e) {
            throw new SerializerException(e);
        }
    }
}