/*
 *  Copyright (C) 2014 Stichting Akvo (Akvo Foundation)
 *
 *  This file is part of Akvo FLOW.
 *
 *  Akvo FLOW is free software: you can redistribute it and modify it under the terms of
 *  the GNU Affero General Public License (AGPL) as published by the Free Software Foundation,
 *  either version 3 of the License or any later version.
 *
 *  Akvo FLOW is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *  See the GNU Affero General Public License included below for more details.
 *
 *  The full license text can also be seen at <http://www.gnu.org/licenses/agpl.html>.
 */

package org.waterforpeople.mapping.app.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONObject;
import org.waterforpeople.mapping.app.web.dto.DeviceNotificationRequest;
import org.waterforpeople.mapping.app.web.dto.DeviceNotificationResponse;

import com.gallatinsystems.device.dao.DeviceDAO;
import com.gallatinsystems.device.dao.DeviceFileJobQueueDAO;
import com.gallatinsystems.device.domain.Device;
import com.gallatinsystems.device.domain.DeviceFileJobQueue;
import com.gallatinsystems.framework.rest.AbstractRestApiServlet;
import com.gallatinsystems.framework.rest.RestRequest;
import com.gallatinsystems.framework.rest.RestResponse;

public class DeviceNotificationRestServlet extends AbstractRestApiServlet {

	private static final long serialVersionUID = -2243167279214074216L;

	public DeviceNotificationRestServlet() {
		super();
		setMode(JSON_MODE);
	}

	@Override
	protected RestRequest convertRequest() throws Exception {
		HttpServletRequest req = getRequest();
		RestRequest restRequest = new DeviceNotificationRequest();
		restRequest.populateFromHttpRequest(req);
		return restRequest;
	}

	@Override
	protected RestResponse handleRequest(RestRequest req) throws Exception {
		DeviceNotificationRequest dnReq = (DeviceNotificationRequest) req;
		DeviceNotificationResponse resp = new DeviceNotificationResponse();
		Device d = getDevice(dnReq);

		if (d == null) {
			return resp;
		}


		DeviceFileJobQueueDAO jobDao = new DeviceFileJobQueueDAO();

		List<DeviceFileJobQueue> missingByDevice = jobDao.listByDeviceId(d
				.getKey().getId());
		List<DeviceFileJobQueue> missingUnknown = jobDao.listByUnknownDevice();

		resp.setMissingFiles(missingByDevice);
		resp.setMissingUnknown(missingUnknown);

		return resp;
	}

	@Override
	protected void writeOkResponse(RestResponse resp) throws Exception {
		getResponse().setStatus(200);
		JSONObject obj = new JSONObject(resp);
		getResponse().getWriter().println(obj.toString());
	}

	private Device getDevice(DeviceNotificationRequest req) {
		DeviceDAO deviceDao = new DeviceDAO();

		if (req.getImei() != null) {
			return deviceDao.getByImei(req.getImei().trim());

		}

		if (req.getPhoneNumber() != null) {
			return deviceDao.get(req.getPhoneNumber().trim());
		}

		return null;
	}

}

@@ -810,24 +810,51 @@
 etRequest;%0A%0A
+import org.json.JSONArray;%0A
 import org.j
@@ -2179,17 +2179,16 @@
 p;%0A%09%09%7D%0A%0A
-%0A
 %09%09Device
@@ -2632,46 +2632,559 @@
 ;%0A%09%09
-JSONObject obj = new JSONObject(resp);
+DeviceNotificationResponse r = (DeviceNotificationResponse) resp;%0A%0A%09%09// manually building the JSON response as the current version of the%0A%09%09// JSON library can't handle the resp object%0A%0A%09%09JSONObject json = new JSONObject();%0A%09%09JSONArray missingFiles = new JSONArray();%0A%09%09JSONArray missingUnknown = new JSONArray();%0A%0A%09%09for (String mf : r.getMissingFiles()) %7B%0A%09%09%09missingFiles.put(mf);%0A%09%09%7D%0A%0A%09%09for (String mu : r.getMissingUnknown()) %7B%0A%09%09%09missingUnknown.put(mu);%0A%09%09%7D%0A%0A%09%09json.put(%22missingFiles%22, missingFiles);%0A%09%09json.put(%22missingUnknown%22, missingUnknown);%0A
 %0A%09%09g
@@ -3216,19 +3216,20 @@
 println(
-obj
+json
 .toStrin

Build a response proper response for the device notification