/*******************************************************************************
* Copyright (c) 2013 -- WPI Suite
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Chris Casola
* JPage
* Andrew Hurle
******************************************************************************/
package edu.wpi.cs.wpisuitetng.modules.defecttracker.search.observers;
import edu.wpi.cs.wpisuitetng.modules.defecttracker.models.Defect;
import edu.wpi.cs.wpisuitetng.modules.defecttracker.search.controllers.RetrieveAllDefectsController;
import edu.wpi.cs.wpisuitetng.network.Request;
import edu.wpi.cs.wpisuitetng.network.RequestObserver;
import edu.wpi.cs.wpisuitetng.network.models.IRequest;
import edu.wpi.cs.wpisuitetng.network.models.ResponseModel;
/**
* An observer for a request to retrieve all defects
*/
public class RetrieveAllDefectsRequestObserver implements RequestObserver {
/** The controller managing the request */
protected RetrieveAllDefectsController controller;
/**
* Construct the observer
* @param controller
*/
public RetrieveAllDefectsRequestObserver(RetrieveAllDefectsController controller) {
this.controller = controller;
}
@Override
public void responseSuccess(IRequest iReq) {
// cast observable to request
Request request = (Request) iReq;
// get the response from the request
ResponseModel response = request.getResponse();
if (response.getStatusCode() == 200) {
// parse the response
Defect[] defects = Defect.fromJSONArray(response.getBody());
// notify the controller
controller.receivedData(defects);
}
else {
controller.errorReceivingData("Received " + iReq.getResponse().getStatusCode() + " error from server: " + iReq.getResponse().getStatusMessage());
}
}
@Override
public void responseError(IRequest iReq) {
// an error occurred
controller.errorReceivingData("Received " + iReq.getResponse().getStatusCode() + " error from server: " + iReq.getResponse().getStatusMessage());
}
@Override
public void fail(IRequest iReq, Exception exception) {
// an error occurred
controller.errorReceivingData("Unable to complete request: " + exception.getMessage());
}
}