package nl.sense_os.commonsense.server;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import javax.jdo.PersistenceManager;

import nl.sense_os.commonsense.server.data.BooleanValue;
import nl.sense_os.commonsense.server.data.FloatValue;
import nl.sense_os.commonsense.server.data.JsonValue;
import nl.sense_os.commonsense.server.data.StringValue;
import nl.sense_os.commonsense.server.data.PMF;
import nl.sense_os.commonsense.server.data.SensorType;
import nl.sense_os.commonsense.server.data.SensorValue;
import nl.sense_os.commonsense.server.utility.SensorValueConverter;

import com.google.appengine.repackaged.org.json.JSONException;
import com.google.appengine.repackaged.org.json.JSONObject;


@SuppressWarnings("serial")
public class IVOSensorValueServlet extends IVOServlet {
	
	private HashMap<Integer, SensorType> sensorTypes;
	private Collection<BooleanValue> booleanValues;
	private Collection<FloatValue> floatValues;
	private Collection<JsonValue> jsonValues;
	private Collection<StringValue> stringValues;
	
	public IVOSensorValueServlet() {
		super();
		booleanValues = new ArrayList<BooleanValue>();
		floatValues = new ArrayList<FloatValue>();
		jsonValues = new ArrayList<JsonValue>();
		stringValues = new ArrayList<StringValue>();
	}
	
	@SuppressWarnings("unchecked")
	protected void initialize() {
		sensorTypes = new HashMap<Integer, SensorType>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			String query = "select from " + SensorType.class.getName() + " order by id";
			List<SensorType> rawList = (List<SensorType>) pm.newQuery(query).execute();
			SensorType s;
			for (int i = 0; i < rawList.size(); i++) {
				s = rawList.get(i);
				sensorTypes.put(s.getId(), s);
			}
		} finally {
			pm.close();
		}
		
	}
	
	protected String create(JSONObject change) throws JSONException {
			if (sensorTypes.size() > 0) {
				SensorType sensorType = sensorTypes.get(change.getInt("sensor_type"));
				SensorValue sensorValue = SensorValueConverter.jsonToEntity(change, sensorType.getType());
				// for storage, we need to explicitly differentiate between sensor value types... 
				switch (sensorType.getType()) {
					case SensorType.BOOL: 
						booleanValues.add((BooleanValue) sensorValue);
						break;
					case SensorType.FLOAT: 
						floatValues.add((FloatValue) sensorValue);
						break;
					case SensorType.JSON: 
						jsonValues.add((JsonValue) sensorValue);
						break;
					case SensorType.STRING: 
						stringValues.add((StringValue) sensorValue);
						break;
				}
			} else
				log.warning("Could not find corresponding sensor type in datastore.");
        return change.getString("id");
	}
		
	protected String update(JSONObject change) throws JSONException {
		return Integer.toString(((JSONObject) change.get("old")).getInt("id"));
	}

	protected String delete(JSONObject change) throws JSONException {
        return Integer.toString(change.getInt("id"));
	}
	
	protected void finalize() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistentAll(booleanValues);
			pm.makePersistentAll(floatValues);
			pm.makePersistentAll(jsonValues);
			pm.makePersistentAll(stringValues);
			booleanValues.clear();
			floatValues.clear();
			jsonValues.clear();
			stringValues.clear();
		} finally {
			pm.close();	
		}
	}

}

@@ -40,67 +40,8 @@
 %0D%0A%0D%0A
-import java.util.ArrayList;%0D%0Aimport java.util.Collection;%0D%0A
 impo
@@ -871,427 +871,30 @@
 ate 
-Collection%3CBooleanValue%3E booleanValues;%0D%0A%09private Collection%3CFloatValue%3E floatValues;%0D%0A%09private Collection%3CJsonValue%3E jsonValues;%0D%0A%09private Collection%3CStringValue%3E stringValues;%0D%0A%09%0D%0A%09public IVOSensorValueServlet() %7B%0D%0A%09%09super();%0D%0A%09%09booleanValues = new ArrayList%3CBooleanValue%3E();%0D%0A%09%09floatValues = new ArrayList%3CFloatValue%3E();%0D%0A%09%09jsonValues = new ArrayList%3CJsonValue%3E();%0D%0A%09%09stringValues = new ArrayList%3CStringValue%3E();%0D%0A%09%7D
+PersistenceManager pm;
 %0D%0A%09%0D
@@ -1014,35 +1014,16 @@
 %3E();%0D%0A%09%09
-PersistenceManager 
 pm = PMF
@@ -1055,25 +1055,16 @@
 ger();%0D%0A
-%09%09try %7B%0D%0A
 %09%09%09Strin
@@ -1352,48 +1352,8 @@
 %09%7D%0D%0A
-%09%09%7D finally %7B%0D%0A%09%09%09pm.close();%0D%0A%09%09%7D%0D%0A%09%09%0D%0A
 %09%7D%0D%0A
@@ -1793,25 +1793,25 @@
 %09%09%09%09
-booleanValues.add
+pm.makePersistent
 ((Bo
@@ -1891,23 +1891,25 @@
 %09%09%09%09
-floatValues.add
+pm.makePersistent
 ((Fl
@@ -1986,22 +1986,25 @@
 %09%09%09%09
-jsonValues.add
+pm.makePersistent
 ((Js
@@ -2082,24 +2082,25 @@
 %09%09%09%09
-stringValues.add
+pm.makePersistent
 ((St
@@ -2594,355 +2594,8 @@
  %7B%0D%0A
-%09%09PersistenceManager pm = PMF.get().getPersistenceManager();%0D%0A%09%09try %7B%0D%0A%09%09%09pm.makePersistentAll(booleanValues);%0D%0A%09%09%09pm.makePersistentAll(floatValues);%0D%0A%09%09%09pm.makePersistentAll(jsonValues);%0D%0A%09%09%09pm.makePersistentAll(stringValues);%0D%0A%09%09%09booleanValues.clear();%0D%0A%09%09%09floatValues.clear();%0D%0A%09%09%09jsonValues.clear();%0D%0A%09%09%09stringValues.clear();%0D%0A%09%09%7D finally %7B%0D%0A%09
 %09%09pm
@@ -2607,14 +2607,8 @@
 e();
-%09%0D%0A%09%09%7D
 %0D%0A%09%7D

update ivoservlet