/*
 * Copyright (C) 2011 Benoit GUEROUT <bguerout at gmail dot com> and Yves AMSELLEM <amsellem dot yves at gmail dot com>
 *
 * 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 org.jongo;

import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.WriteConcern;
import com.mongodb.WriteResult;
import org.jongo.query.Query;
import org.jongo.query.QueryFactory;

public class Update {

    private final DBCollection collection;
    private final Query query;
    private final QueryFactory queryFactory;

    private WriteConcern writeConcern;
    private boolean upsert = false;
    private boolean multi = false;

    Update(DBCollection collection, WriteConcern writeConcern, QueryFactory queryFactory, String query, Object... parameters) {
        this.collection = collection;
        this.writeConcern = writeConcern;
        this.queryFactory = queryFactory;
        this.query = createQuery(query, parameters);
    }

    private WriteConcern determineWriteConcern() {
        return writeConcern == null ? collection.getWriteConcern() : writeConcern;
    }

    public WriteResult with(String modifier) {
        return with(modifier, new Object[0]);
    }

    public WriteResult with(String modifier, Object... parameters) {
        Query updateQuery = queryFactory.createQuery(modifier, parameters);
        return collection.update(this.query.toDBObject(), updateQuery.toDBObject(), upsert, multi, determineWriteConcern());
    }

    public WriteResult with(Object pojo) {
        DBObject updateDbo = queryFactory.createQuery("{$set:#}", pojo).toDBObject();
        DBObject pojoAsDbo = (DBObject) updateDbo.get("$set");
        pojoAsDbo.removeField("_id");
        return collection.update(this.query.toDBObject(), updateDbo, upsert, multi, determineWriteConcern());
    }

    public Update upsert() {
        this.upsert = true;
        return this;
    }

    public Update multi() {
        this.multi = true;
        return this;
    }

    private Query createQuery(String query, Object[] parameters) {
        try {
            return this.queryFactory.createQuery(query, parameters);
        } catch (Exception e) {
            String message = String.format("Unable execute update operation using query %s", query);
            throw new IllegalArgumentException(message, e);
        }
    }
}

@@ -1462,149 +1462,8 @@
  %7D%0A%0A
-    private WriteConcern determineWriteConcern() %7B%0A        return writeConcern == null ? collection.getWriteConcern() : writeConcern;%0A    %7D%0A%0A
     
@@ -1501,24 +1501,24 @@
 modifier) %7B%0A
+
         retu
@@ -1798,34 +1798,25 @@
 ert, multi, 
-determineW
+w
 riteConcern(
@@ -1806,34 +1806,32 @@
 ti, writeConcern
-()
 );%0A    %7D%0A%0A    pu
@@ -2142,18 +2142,9 @@
 ti, 
-determineW
+w
 rite
@@ -2150,18 +2150,16 @@
 eConcern
-()
 );%0A    %7D

Remove merged determineWriteConcern method