SubQueryExecutionLeaf.java :  » Database-DBMS » jalisto » org » objectweb » jalisto » se » query » execution » Java Open Source

Java Open Source » Database DBMS » jalisto 
jalisto » org » objectweb » jalisto » se » query » execution » SubQueryExecutionLeaf.java
/*
 * Jalisto - JAva LIght STOrage
 * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 * 
 * Xcalia
 * 71, rue Desnouettes
 * 75014 Paris - France
 * http://www.xcalia.com
 */
package org.objectweb.jalisto.se.query.execution;

import org.objectweb.jalisto.se.api.ClassDescription;
import org.objectweb.jalisto.se.impl.LogicalOid;
import org.objectweb.jalisto.se.api.Session;
import org.objectweb.jalisto.se.api.MetaRepository;
import org.objectweb.jalisto.se.api.query.IndexManager;
import org.objectweb.jalisto.se.api.internal.SessionInternal;
import org.objectweb.jalisto.se.query.NavigationInfo;
import org.objectweb.jalisto.se.query.constraint.SubQueryConstraintImpl;
import org.objectweb.jalisto.se.query.result.QueryResultWrapper;
import org.objectweb.jalisto.se.query.result.WrapperSet;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class SubQueryExecutionLeaf extends ExecutionElement {

    public SubQueryExecutionLeaf(ExecutionTree tree, ExecutionElement father,
                                 SubQueryConstraintImpl subQueryConstraint, int depth) {
        super(tree, father, depth);
        this.subQueryConstraint = subQueryConstraint;
        setToLeaf(0);
    }

    // not an index => "Or branch" i.e. not resolvable branch
    public boolean isInOrBranch(IndexManager indexManager) {
        return false;
    }

    public boolean isResolved() {
        return false;
    }

    public WrapperSet getResolvedValues() {
        return new WrapperSet();
    }

    public void defineMeta(MetaRepository repository) {
        ClassDescription meta = repository.getMetaData(subQueryConstraint.getClassName());
        navigationInfos = new NavigationInfo(meta, subQueryConstraint.getFieldNames());
    }

    /**
     * *****************************   RESOLVE   ******************************************
     */

    public boolean resolveOnElement(Session session, QueryResultWrapper wrapper) {
        Object value = wrapper.getValue()[navigationInfos.getIndex()];
        ArrayList candidates = new ArrayList();
        if (Collection.class.isAssignableFrom(value.getClass())) {
            Iterator it = ((Collection) value).iterator();
            while (it.hasNext()) {
                LogicalOid floid = (LogicalOid) it.next();
                Object[] array = session.readObjectByOid(floid);
                candidates.add(new QueryResultWrapper(floid, array));
            }
        } else {
            Object[] array = session.readObjectByOid(value);
            candidates.add(new QueryResultWrapper((LogicalOid) value, array));
        }
        for (int i = 0; i < candidates.size(); i++) {
            boolean result = subQueryConstraint.getTree().resolveOnOneElement(
                    (SessionInternal) session,
                    (QueryResultWrapper) candidates.get(i));
            if (result) {
                return true;
            }
        }
        return false;
    }

    public WrapperSet resolveWithCandidates(Session session, WrapperSet candidates) {
        return new WrapperSet();
    }

    public String toString() {
        return "Eleaf:" + subQueryConstraint.toString();
    }

    private SubQueryConstraintImpl subQueryConstraint;
    private NavigationInfo navigationInfos;
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.