Android Open Source - jpdroid Jpdroid Dto Converter






From Project

Back to project page jpdroid.

License

The source code is released under:

GNU General Public License

If you think the Android project jpdroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package br.com.rafael.jpdroid.converters;
/* www . j a  v a 2s . com*/
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;

import br.com.rafael.jpdroid.annotations.Dto;
import br.com.rafael.jpdroid.annotations.DtoField;

/**
 * JpdroidDtoConverter - Esta classe permite a convers?o de um objeto do tipo entity para um DTO e vice-versa. <br/>
 * Ex: <br/>
 * Pessoa pessoaOrigem = jpdroid.getObjects(Pessoa.class,true).get(0); <br/>
 * PessoaDTO pessoaDTO = JpdroidDtoConverter.Convert(pessoaOrigem, PessoaDTO.class); <br/>
 * Pessoa pessoa = JpdroidDtoConverter.Convert(pessoaDTO, Pessoa.class);
 * 
 * @author Rafael
 */
public class JpdroidDtoConverter {

  public static <T, E> List<T> convert(List<E> orig, Class<T> dest) {

    List<T> retorno = new ArrayList<T>();

    for (E item : orig) {

      retorno.add(convert(item, dest));

    }

    return retorno;

  }

  public static <T> T convert(Object orig, Class<T> dest) {

    T classConvert = null;

    try {

      if (orig.getClass().getSuperclass().getAnnotation(Dto.class) != null) {

        classConvert = convert(orig.getClass().getSuperclass(), dest);

      } else {

        classConvert = dest.newInstance();

      }

      Class<?> dto = null;

      Class<?> origem = orig.getClass();

      if (orig.getClass().getAnnotation(Dto.class) != null) {

        dto = orig.getClass();

      } else {

        dto = dest;

      }

      Field[] fields = dto.getDeclaredFields();

      for (Field field : fields) {

        DtoField dtoField = field.getAnnotation(DtoField.class);

        if (dtoField != null) {

          Field fieldDestino = null;

          Field fieldOrigem = null;

          if (dto.equals(dest)) {

            fieldDestino = field;

          } else {

            fieldDestino = dest.getDeclaredField(field.getName());

          }

          fieldDestino.setAccessible(true);

          fieldOrigem = origem.getDeclaredField(field.getName());

          fieldOrigem.setAccessible(true);

          Class<? extends Object> ob = fieldOrigem.getType();

          if (ob.isAssignableFrom(List.class)) {

            ParameterizedType fieldGenericType = (ParameterizedType) fieldDestino.getGenericType();

            Class<?> fieldType = (Class<?>) fieldGenericType.getActualTypeArguments()[0];

            Object value = fieldOrigem.get(orig);
            if (value != null) {

              List<Object> valores = new ArrayList<Object>();

              for (Object item : ((List<?>) value)) {

                valores.add(convert(item, fieldType));

              }

              fieldDestino.set(classConvert, valores);
            }

          } else {

            Object value = fieldOrigem.get(orig);
            if (value != null) {
              Dto destino = value.getClass().getAnnotation(Dto.class);

              if (destino != null) {

                fieldDestino.set(classConvert, convert(value, value.getClass()));

              } else {

                fieldDestino.set(classConvert, fieldOrigem.get(orig));

              }
            }

          }

        }

      }

      convertAncestor(orig, dto, classConvert);

    } catch (Exception ex) {
      ex.printStackTrace();
    }

    return classConvert;

  }

  private static void convertAncestor(Object orig, Class<?> dto, Object dest) throws Exception {

    Class<?> dtoAnc = dto.getSuperclass();

    if (dtoAnc.equals(Object.class)) {

      return;

    }

    Class<?> classOrig = null;

    Class<?> classDest = null;

    if (!orig.getClass().getSuperclass().equals(Object.class)) {

      classOrig = orig.getClass().getSuperclass();

    } else {

      classOrig = orig.getClass();

    }

    if (!dest.getClass().getSuperclass().equals(Object.class)) {

      classDest = dest.getClass().getSuperclass();

    } else {

      classDest = dest.getClass();

    }

    Field[] fields = dtoAnc.getDeclaredFields();

    for (Field field : fields) {

      DtoField dtoField = field.getAnnotation(DtoField.class);

      if (dtoField != null) {

        Field fieldDestino = null;

        Field fieldOrigem = null;

        if (dto.equals(dest)) {

          fieldDestino = field;

        } else {

          fieldDestino = classDest.getDeclaredField(field.getName());

        }

        fieldDestino.setAccessible(true);

        fieldOrigem = classOrig.getDeclaredField(field.getName());

        fieldOrigem.setAccessible(true);

        Class<? extends Object> ob = fieldOrigem.getType();

        if (ob.isAssignableFrom(List.class)) {

          ParameterizedType fieldGenericType = (ParameterizedType) fieldDestino.getGenericType();

          Class<?> fieldType = (Class<?>) fieldGenericType.getActualTypeArguments()[0];

          Object value = fieldOrigem.get(orig);
          if (value != null) {
            List<Object> valores = new ArrayList<Object>();

            for (Object item : ((List<?>) value)) {

              valores.add(convert(item, fieldType));

            }

            fieldDestino.set(dest, valores);
          }

        } else {

          Object value = fieldOrigem.get(orig);
          if (value != null) {
            Dto destino = value.getClass().getAnnotation(Dto.class);

            if (destino != null) {

              fieldDestino.set(dest, convert(value, value.getClass()));

            } else {

              fieldDestino.set(dest, fieldOrigem.get(orig));

            }
          }

        }

      }

    }

  }

}




Java Source Code List

br.com.rafael.jpdroid.annotations.Column.java
br.com.rafael.jpdroid.annotations.DefaultOrder.java
br.com.rafael.jpdroid.annotations.DtoField.java
br.com.rafael.jpdroid.annotations.Dto.java
br.com.rafael.jpdroid.annotations.Entity.java
br.com.rafael.jpdroid.annotations.ForeignKey.java
br.com.rafael.jpdroid.annotations.Ignorable.java
br.com.rafael.jpdroid.annotations.PrimaryKey.java
br.com.rafael.jpdroid.annotations.RelationClass.java
br.com.rafael.jpdroid.annotations.ViewColumn.java
br.com.rafael.jpdroid.converters.JpdroidCsvConverter.java
br.com.rafael.jpdroid.converters.JpdroidDateUtil.java
br.com.rafael.jpdroid.converters.JpdroidDtoConverter.java
br.com.rafael.jpdroid.converters.JpdroidJsonConverter.java
br.com.rafael.jpdroid.converters.JpdroidMatrixCursorConverter.java
br.com.rafael.jpdroid.converters.JpdroidXmlConverter.java
br.com.rafael.jpdroid.core.JpdroidAbout.java
br.com.rafael.jpdroid.core.JpdroidDbHelper.java
br.com.rafael.jpdroid.core.JpdroidEntityValidation.java
br.com.rafael.jpdroid.core.JpdroidObjectMap.java
br.com.rafael.jpdroid.core.JpdroidTransaction.java
br.com.rafael.jpdroid.core.JpdroidWriteFile.java
br.com.rafael.jpdroid.core.Jpdroid.java
br.com.rafael.jpdroid.enums.Order.java
br.com.rafael.jpdroid.enums.RelationType.java
br.com.rafael.jpdroid.enums.ScriptPath.java
br.com.rafael.jpdroid.exceptions.JpdroidException.java
br.com.rafael.jpdroid.interfaces.ITransaction.java
br.com.rafael.jpdroid.util.JpdroidConverter.java
br.com.rafael.jpdroid.util.JpdroidCsvFile.java
br.com.rafael.jpdroid.util.JpdroidJsonFile.java
br.com.rafael.jpdroid.util.JpdroidXmlFile.java
br.com.rafael.pedidojpdroid.ContatoActivity.java
br.com.rafael.pedidojpdroid.EnderecoActivity.java
br.com.rafael.pedidojpdroid.ExportacoesActivity.java
br.com.rafael.pedidojpdroid.ImportacoesActivity.java
br.com.rafael.pedidojpdroid.ItensPedidoActivity.java
br.com.rafael.pedidojpdroid.ListaCidadesActivity.java
br.com.rafael.pedidojpdroid.ListaContatoActivity.java
br.com.rafael.pedidojpdroid.ListaEnderecoActivity.java
br.com.rafael.pedidojpdroid.ListaItensPedidoActivity.java
br.com.rafael.pedidojpdroid.ListaPedidoActivity.java
br.com.rafael.pedidojpdroid.ListaPessoaActivity.java
br.com.rafael.pedidojpdroid.ListaProdutoActivity.java
br.com.rafael.pedidojpdroid.ListagemPedidoActivity.java
br.com.rafael.pedidojpdroid.ListagemPessoaActivity.java
br.com.rafael.pedidojpdroid.ListagemProdutoActivity.java
br.com.rafael.pedidojpdroid.PedidoActivity.java
br.com.rafael.pedidojpdroid.PesquisaDialogActivity.java
br.com.rafael.pedidojpdroid.PessoaActivity.java
br.com.rafael.pedidojpdroid.PrincipalActivity.java
br.com.rafael.pedidojpdroid.ProdutoActivity.java
br.com.rafael.pedidojpdroid.SobreActivity.java
br.com.rafael.pedidojpdroid.dto.ContatoDTO.java
br.com.rafael.pedidojpdroid.dto.PessoaDTO.java
br.com.rafael.pedidojpdroid.entity.Cidade.java
br.com.rafael.pedidojpdroid.entity.Contato.java
br.com.rafael.pedidojpdroid.entity.Endereco.java
br.com.rafael.pedidojpdroid.entity.Estado.java
br.com.rafael.pedidojpdroid.entity.ItensPedido.java
br.com.rafael.pedidojpdroid.entity.Pedido.java
br.com.rafael.pedidojpdroid.entity.Pessoa.java
br.com.rafael.pedidojpdroid.entity.Produto.java
com.rafael.androidcomjpdroid.ContatoActivity.java
com.rafael.androidcomjpdroid.MainActivity.java
com.rafael.androidcomjpdroid.PessoaActivity.java
com.rafael.androidcomjpdroid.entity.Contato.java
com.rafael.androidcomjpdroid.entity.Pessoa.java
com.rafael.androidcomjpdroid.entity.TipoContato.java