/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.usergrid.persistence;


import com.fasterxml.jackson.annotation.JsonIgnore;
import org.apache.usergrid.persistence.index.query.Query;
import java.util.Iterator;
import java.util.UUID;

import org.apache.usergrid.persistence.index.query.Query.Level;


public class PathQuery<E> {

    private  EntityRef head;
    private PathQuery source;
    private Query query;
    private UUID uuid;
    private String type;


    public PathQuery() {
    }


    /**
     * top level
     *
     * @param head the top-level entity
     */
    public PathQuery( EntityRef head ) {
        this.uuid = head.getUuid();
        this.type = head.getType();
        this.head = head;
        this.query = null;
    }


    /**
     * top level
     *
     * @param head the top-level entity
     * @param query the query - must have a collection or connectType value set
     */
    public PathQuery( EntityRef head, Query query ) {
        if ( query.getCollection() == null && query.getConnectionType() == null ) {
            throw new IllegalArgumentException( "Query must have a collection or connectionType value" );
        }
        this.uuid = head.getUuid();
        this.type = head.getType();
        this.head = head;
        this.query = query;
    }


    /**
     * chained
     *
     * @param source the source query we're chaining from
     * @param query the query - must have a collection or connectType value set
     */
    public PathQuery( PathQuery source, Query query ) {
        if ( query.getCollection() == null && query.getConnectionType() == null ) {
            throw new IllegalArgumentException( "Query must have a collection or connectionType value" );
        }
        this.source = source;
        this.query = query;
    }


    public PathQuery chain( Query query ) {
        return new PathQuery( this, query );
    }


    public Iterator<E> iterator( EntityManager em ) {
        try {
            if ( uuid != null && type != null ) {
                return new PagingResultsIterator( getHeadResults( em ), query.getResultsLevel() );
            }
            else {
                return new MultiQueryIterator( em, source.refIterator( em ), query );
            }
        }
        catch ( Exception e ) {
            throw new RuntimeException( e );
        }
    }


    protected Results getHeadResults( EntityManager em ) throws Exception {
        EntityRef ref = head != null ? head : new SimpleEntityRef(type,uuid);
        return ( query.getCollection() != null ) ? 
               em.searchCollection( ref, query.getCollection(), query ) :
               em.searchConnectedEntities( ref, query );
    }


    protected Iterator refIterator( EntityManager em ) throws Exception {
        if ( type != null  && uuid != null) {
            return new PagingResultsIterator( getHeadResults( em ), Level.REFS );
        }
        else {
            Query q = query;
            if ( query.getResultsLevel() != Level.REFS ) { // ensure REFS level
                q = new Query( q );
                q.setResultsLevel( Level.REFS );
            }
            return new MultiQueryIterator( em, source.refIterator( em ), q );
        }
    }


    public PathQuery getSource() {
        return source;
    }




    public String getType(){return type;}

    public UUID getUuid(){return uuid;}

    public Query getQuery() {
        return query;
    }
}

@@ -843,60 +843,8 @@
 ;%0A%0A%0A
-import com.fasterxml.jackson.annotation.JsonIgnore;%0A
 impo
@@ -897,16 +897,16 @@
 .Query;%0A
+
 import j
@@ -1047,37 +1047,8 @@
  %7B%0A%0A
-    private  EntityRef head;%0A
     
@@ -1378,34 +1378,8 @@
 ();%0A
-        this.head = head;%0A
     
@@ -1899,34 +1899,8 @@
 ();%0A
-        this.head = head;%0A
     
@@ -3084,30 +3084,8 @@
 ef =
- head != null ? head :
  new
@@ -3893,24 +3893,24 @@
 source;%0A
+
     %7D%0A%0A%0A
 %0A%0A    pu
@@ -3901,18 +3901,16 @@
     %7D%0A%0A%0A
-%0A%0A
     publ

Remove unserializable interface EntityRef from PathQuery.