Re: Generate Expression from DataObject

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Thu Mar 30 2006 - 09:19:48 EST

  • Next message: Cris Daniluk: "Re: CLA's from committers"

    On Mar 30, 2006, at 6:08 PM, Tobias SCHOESSLER wrote:

    > Maybe someone finds it usefull. See source attached.
    >
    > (See attached file: ExpressionUtil.java)

    Looks like Apache list is setup to strip attachments. Posting your
    sources inline. We may post it on Wiki as well.

    Andrus

    ----------------------

    package org.unodc.proj.ddb.util;

    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Set;

    import org.apache.log4j.Logger;
    import org.objectstyle.cayenne.DataObject;
    import org.objectstyle.cayenne.access.DataContext;
    import org.objectstyle.cayenne.exp.Expression;
    import org.objectstyle.cayenne.exp.ExpressionFactory;
    import org.objectstyle.cayenne.map.Entity;
    import org.objectstyle.cayenne.map.ObjAttribute;
    import org.objectstyle.cayenne.map.ObjEntity;
    import org.objectstyle.cayenne.map.Relationship;

    public class ExpressionUtil {

            Logger log = Logger.getLogger(ExpressionUtil.class);
            
            DataContext ctxt;
            
            public ExpressionUtil(DataContext ctxt ) {
                    this.ctxt = ctxt;
            }
            
            
            private void generateExpression(Expression[] searchExp, DataObject
    dataObject, Set avoid, String path, List excludeClasses) {
                    avoid.add(dataObject);
                    ObjEntity objEntity = ctxt.getEntityResolver().lookupObjEntity
    (dataObject);
                    Iterator relationshipsIt = objEntity.getRelationships().iterator();
                    log.debug(objEntity.getName() + ">>>");
                    // attributes
                    Iterator attributesIt = objEntity.getAttributes().iterator();
                    while (attributesIt.hasNext()) {
                            ObjAttribute objAttribute = (ObjAttribute) attributesIt.next();
                            String attr = objAttribute.getName();
                            Object value = dataObject.readProperty(objAttribute.getName());
                            log.debug(" " + objAttribute.getName() + ":" + value);

                            if (dataObject.readProperty(objAttribute.getName()) != null && !
    excludeClasses.contains(dataObject.getClass())) {
                                    log.debug(" adding expression for " + path + attr);
                                    Expression exp = null;
                                    if (objAttribute.getDbAttribute().getType() ==
    java.sql.Types.LONGVARCHAR) {
                                            exp = ExpressionFactory.likeExp(path + attr, value);
                                    } else {
                                            exp = ExpressionFactory.matchExp(path + attr, value);
                                    }
                                    if (searchExp[0] == null) {
                                            searchExp[0] = exp;
                                    } else {
                                            searchExp[0] = searchExp[0].andExp(exp);
                                    }
                            }
                    }

                    // dive into relationships
                    while (relationshipsIt.hasNext()) {
                            Relationship relationship = (Relationship) relationshipsIt.next();
                            Entity targetEntity = relationship.getTargetEntity();
                            String relName = relationship.getName();
                            log.debug(" <" + relName + ">");
                            if (relationship.isToMany()) {
                                    log.debug(" => " + relationship.getTargetEntityName());
                                    List list = (List) dataObject.readProperty(relName);
                                    Iterator depDataObjectsIt = list.iterator();
                                    while (depDataObjectsIt.hasNext()) {
                                            DataObject depDataObject = (DataObject) depDataObjectsIt.next();
                                            if (!avoid.contains(depDataObject)) { // prevent loops
                                                    generateExpression(searchExp, depDataObject, avoid, path +
    relName + ".", excludeClasses);
                                            }
                                    }
                            } else {
                                    log.debug(" -> " + relationship.getTargetEntityName());
                                    DataObject depDataObject = (DataObject) dataObject.readProperty
    (relName);
                                    if (depDataObject != null) {
                                            if (!avoid.contains(depDataObject)) { // prevent loops
                                                    generateExpression(searchExp, depDataObject, avoid, path +
    relName + ".", excludeClasses);
                                            }
                                    }
                            }
                            // addExpressions(searchExp, )
                    }
                    log.debug("<<<" + objEntity.getName());

            }

            /**
             * Generates an Expression matching all the non null attribute
    values of a given DataObject graph.
             *..aram dataObject root of the graph, expressions are generated
    relative to this node
             *..aram exclude a list of classes to exclude from the generation
             */
            public Expression expressionFromDataObject(DataObject dataObject,
    List exclude) {
                    Expression[] searchExp = new Expression[1];
                    generateExpression(searchExp, dataObject, new HashSet(), "", exclude);
                    return searchExp[0];
            }

    }



    This archive was generated by hypermail 2.0.0 : Thu Mar 30 2006 - 09:20:13 EST