/*
   Copyright 2009 Janne Hietamäki

   Licensed 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 mungbean.protocol.message;

import java.util.LinkedHashMap;
import java.util.Map;

import mungbean.protocol.LittleEndianDataReader;
import mungbean.protocol.LittleEndianDataWriter;
import mungbean.protocol.bson.AbstractBSONCoders;
import mungbean.protocol.bson.BSON;
import mungbean.protocol.bson.BSONCoder;
import mungbean.protocol.bson.BSONMap;
import mungbean.query.QueryBuilder;

public class QueryRequest<ResponseType> extends CollectionRequest<QueryResponse<ResponseType>> {

    private final QueryOptionsBuilder builder;
    private final int numberToSkip;
    private final int numberToReturn;
    private final BSON query;
    private final boolean closeCursor;
    private final BSONCoder<ResponseType> coder;

    public QueryRequest(String collectionName, QueryOptionsBuilder builder, QueryBuilder query, boolean closeCursor, AbstractBSONCoders coders, BSONCoder<ResponseType> coder) {
        super(collectionName);
        this.builder = builder;
        this.numberToSkip = query.skip();
        this.numberToReturn = query.limit();
        this.closeCursor = closeCursor;
        this.query = buildQuery(coders, query.build(), query.order());
        this.coder = coder;
    }

    public QueryRequest(String collectionName, QueryOptionsBuilder builder, Map<String, Object> params, Map<String, Object> orders, AbstractBSONCoders coders, BSONCoder<ResponseType> coder) {
        super(collectionName);
        this.builder = builder;
        this.numberToSkip = 0;
        this.numberToReturn = 1;
        this.closeCursor = true;
        this.query = buildQuery(coders, params, orders);
        this.coder = coder;
    }

    private BSON buildQuery(AbstractBSONCoders coders, final Map<String, Object> query, final Map<String, Object> order) {
        if (order == null || order.isEmpty()) {
            return coders.forValue(query).write(coders, query);
        }
        return new BSONMap().write(coders, new LinkedHashMap<String, Object>() {
            {
                put("query", query);
                put("orderby", order);
            }
        });
    }

    @Override
    public int length() {
        return 4 + collectionNameLength() + 4 + 4 + query.length();
    }

    @Override
    public QueryResponse<ResponseType> readResponse(LittleEndianDataReader reader) {
        return new QueryResponse<ResponseType>(reader, coder);
    }

    @Override
    public void send(LittleEndianDataWriter writer) {
        writer.writeInt(builder.build());
        writeCollectionName(writer);
        writer.writeInt(numberToSkip);
        if (closeCursor) {
            if (numberToReturn > 0) {
                writer.writeInt(-numberToReturn);
            } else {
                writer.writeInt(Integer.MIN_VALUE);
            }
        } else {
            writer.writeInt(numberToReturn);
        }
        writer.write(query.bytes());
    }

    @Override
    public RequestOpCode type() {
        return RequestOpCode.OP_QUERY;
    }
}

@@ -677,16 +677,40 @@
 l.Map;%0A%0A
+import mungbean.Assert;%0A
 import m
@@ -1517,37 +1517,36 @@
 oder) %7B%0A        
-super
+this
 (collectionName)
@@ -1548,114 +1548,90 @@
 Name
-);%0A        this.builder = builder;%0A        this.numberToSkip = query.skip();%0A        this.numberToReturn =
+, builder, query.build(), query.order(), coders, coder, closeCursor, query.skip(),
  que
@@ -1644,120 +1644,209 @@
 it()
+)
 ;%0A    
+%7D%0A%0A
     
-this.closeCursor = closeCursor;%0A        this.query = buildQuery(coders, query.build(), query.order());
+public QueryRequest(String collectionName, QueryOptionsBuilder builder, Map%3CString, Object%3E params, Map%3CString, Object%3E orders, AbstractBSONCoders coders, BSONCoder%3CResponseType%3E coder) %7B
 %0A   
@@ -1850,38 +1850,92 @@
         this
-.coder = coder
+(collectionName, builder, params, orders, coders, coder, true, 0, 1)
 ;%0A    %7D%0A%0A   
@@ -1928,37 +1928,38 @@
 1);%0A    %7D%0A%0A    p
-ublic
+rivate
  QueryRequest(St
@@ -2112,32 +2112,91 @@
 ponseType%3E coder
+, boolean closeCursor, int numberToSkip, int numberToReturn
 ) %7B%0A        supe
@@ -2274,17 +2274,28 @@
 oSkip = 
-0
+numberToSkip
 ;%0A      
@@ -2318,17 +2318,30 @@
 eturn = 
-1
+numberToReturn
 ;%0A      
@@ -2361,20 +2361,27 @@
 ursor = 
-true
+closeCursor
 ;%0A      
@@ -2585,24 +2585,80 @@
 t%3E order) %7B%0A
+        Assert.notNull(query, %22Query can not be null%22);%0A
         if (

clean up + improve error reporting when the given query is null