Package de.linwave.gtm

Source Code of de.linwave.gtm.POQByExample

package de.linwave.gtm;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.odbms.OP;
import org.odbms.Predicate;

public class POQByExample extends QueryImpl
{
  /**
   *
   * @param clasz
   */
  public POQByExample(Class<?> clasz) {
    super(clasz);
  }

  public <E> POQByExample(Predicate<E> predicate) {
    super(predicate);
  }

  public POQByExample(Object obj) {
    this(obj, GTM.MAX_RECURSION_LEVEL);
  }

  public POQByExample(Object obj, int queryDept) {
    super(obj.getClass());

    final Set<FieldInfo> queryNodes = new HashSet<FieldInfo>();

    for (String fieldName : ObjectTraverser.getFields(obj).keySet()) {
      FieldInfo fi = ObjectTraverser.getFieldInfo(obj, fieldName);
      Field field = fi.field;
      field.setAccessible(true);

      Object value = null;
      try {
        value = field.get(obj);
        if (value == null)
          continue;
      } catch (Exception ex) {
        throw new RuntimeException("Could to access field " + fi.field.getName(), ex);
      }

      if (fi.isArray && fi.isPrimitive) {
        Object[] arr = (Object[]) value;
        if (arr.length > 0)
          throw new RuntimeException("Unsupported type. Array not supported at this time");
      } else if (fi.isCollection && fi.isPrimitive) {
        Collection coll = (Collection) value;
        if (coll.size() > 0)
          throw new RuntimeException("Unsupported type. Collection not supported at this time");

      } else if (fi.isPrimitive) {
        if (!fi.typeHandler.isNullValue(value)) {
          if (field.getType() == String.class) {
            fi.value = (String) value;
            queryNodes.add(fi);
          } else if (field.getType() == int.class) {
            int i = (Integer) value;
            if (i != 0) {
              fi.value = (Integer) value;
              queryNodes.add(fi);
            }
          }
        }

      } else if (fi.isCollection) {
        Collection coll = (Collection) value;
        if (coll.size() > 0)
          throw new RuntimeException("Unsupported type. Collection not supported at this time");

      } else if (fi.isArray) {
        Object[] arr = (Object[]) value;
        if (arr.length > 0)
          throw new RuntimeException("Unsupported type. Array not supported at this time");

      } else {
        throw new RuntimeException("Unsupported type. (Primitive,Array,Collection are supported");
      }
    }

    for (Iterator<FieldInfo> i = queryNodes.iterator(); i.hasNext();) {
      FieldInfo fi = i.next();
      OP op = null;
      String val = fi.value.toString();
      if ((val.endsWith("*") || val.endsWith("%")) && (val.startsWith("*") || val.startsWith("%"))) {
        op = OP.CONTAINS;
        val = val.substring(1, val.length() - 2);
      } else if (val.endsWith("*") || val.endsWith("%")) {
        op = OP.STARTS_WITH;
        val = val.substring(0, val.length() - 1);
      } else if (val.startsWith("*") || val.startsWith("%")) {
        op = OP.ENDS_WITH;
        val = val.substring(1);
      } else {
        op = OP.EQUALS;
      }

      constrain(new Constraint(getOUTQueue(), clasz, fi.field.getName(), op, null, val));
    }
  }
}
TOP

Related Classes of de.linwave.gtm.POQByExample

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.