/*   
   Copyright 2011-2013 The Cassandra Consortium (cassandra-fp7.eu)


   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 eu.cassandra.server.mongo;


import javax.ws.rs.core.HttpHeaders;

import eu.cassandra.server.api.exceptions.RestQueryParamMissingException;
import eu.cassandra.server.mongo.util.JSONValidator;
import eu.cassandra.server.mongo.util.JSONtoReturn;
import eu.cassandra.server.mongo.util.MongoDBQueries;

public class MongoPricingPolicy {

	public final static String COL_PRICING = "pricing";
	public final static String REF_PROJECT = "prj_id";
	public final static String REF_ENTITY = "entity_id";
	
	/**
	 * curl -i http://localhost:8080/cassandra/api/prc/4ff1d9d4e4b0ddb832a310bc
	 * 
	 * @param id
	 * @return
	 */
	public String getPricingPolicy(HttpHeaders httpHeaders,String id) {
		return new MongoDBQueries().getEntity(httpHeaders,COL_PRICING,"_id", 
				id, "Pricing retrieved successfully").toString();
	}

	/**
	 * curl -i http://localhost:8080/cassandra/api/demog?scn_id=4ff1d9d4e4b0ddb832a310bc
	 * 
	 * @param scn_id
	 * @return
	 */
	public String getPricingPolicies(HttpHeaders httpHeaders,String scn_id, boolean count) {
		if(scn_id == null) {
			return new JSONtoReturn().createJSONError(
					"Only the pricing policies of a particular Scenario can be retrieved", 
					new RestQueryParamMissingException("scn_id QueryParam is missing")).toString();
		}
		else {
			return new MongoDBQueries().getEntity(httpHeaders, COL_PRICING, REF_PROJECT, 
					scn_id, "Pricing policies retrieved successfully",count).toString();
		}
	}

	/**
	 * curl -i --data  pricing.json    --header Content-type:application/json http://localhost:8080/cassandra/api/prc
	 * 
	 * @param dataToInsert
	 * @return
	 */
	public String createPricingPolicy(String dataToInsert) {
		return new MongoDBQueries().insertData(COL_PRICING, dataToInsert,
				"Pricing policy created successfully", MongoScenarios.COL_SCENARIOS ,
				REF_PROJECT, JSONValidator.PRICING_SCHEMA).toString();
	}

	/**
	 * curl -i -X DELETE http://localhost:8080/cassandra/api/prc/4ff153dbe4b0c855ac36d9a7
	 * 
	 * @param id
	 * @return
	 */
	public String deletePricingPolicy(String id) {
		return new MongoDBQueries().deleteDocument(COL_PRICING, id).toString();
	}

	/**
	 * curl -X PUT -d @pricing.json   --header Content-type:application/json   http://localhost:8080/cassandra/api/demog/4fec8b53df4ffdb8d1d1ce57
	 * 
	 * @param id
	 * @param jsonToUpdate
	 * @return
	 */
	public String updatePricingPolicy(String id,String jsonToUpdate) {
		return new MongoDBQueries().updateDocument("_id", id,jsonToUpdate,
				COL_PRICING, "Pricing policy updated successfully",
				MongoScenarios.COL_SCENARIOS , REF_PROJECT,JSONValidator.PRICING_SCHEMA).toString();
	}
}


@@ -1453,94 +1453,8 @@
 /**%0A
-%09 * curl -i http://localhost:8080/cassandra/api/demog?scn_id=4ff1d9d4e4b0ddb832a310bc%0A
 %09 * 
@@ -1554,19 +1554,19 @@
 ,String 
-scn
+prj
 _id, boo
@@ -1584,19 +1584,19 @@
  %7B%0A%09%09if(
-scn
+prj
 _id == n
@@ -1696,24 +1696,23 @@
 ticular 
-Scenario
+Project
  can be 
@@ -1908,19 +1908,19 @@
 , %0A%09%09%09%09%09
-scn
+prj
 _id, %22Pr
@@ -2329,32 +2329,29 @@
 ongo
-Scenarios.COL_SCENARIOS 
+Projects.COL_PROJECTS
 ,%0A%09%09
@@ -3079,32 +3079,29 @@
 ongo
-Scenarios.COL_SCENARIOS 
+Projects.COL_PROJECTS
 , RE
@@ -3155,11 +3155,13 @@
 g();%0A%09%7D%0A
+%09%0A
 %7D%0A%0A

Fix #276