Package edu.wpi.cs.wpisuitetng.modules.defecttracker.search.observers

Source Code of edu.wpi.cs.wpisuitetng.modules.defecttracker.search.observers.RetrieveDefectRequestObserver

/*******************************************************************************
* 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:
*    JPage
*    Chris Casola
*    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.RetrieveDefectController;
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 a defect with the provided id
*/
public class RetrieveDefectRequestObserver implements RequestObserver {

  /** The retrieve defect controller using this observer */
  protected RetrieveDefectController controller;

  /**
   * Construct a new observer
   * @param controller the controller managing the request
   */
  public RetrieveDefectRequestObserver(RetrieveDefectController controller) {
    this.controller = controller;
  }

  @Override
  public void responseSuccess(IRequest iReq) {
    // cast observable to a Request
    Request request = (Request) iReq;

    // get the response from the request
    ResponseModel response = request.getResponse();

    // check the response code of the request
    if (response.getStatusCode() != 200) {
      controller.errorRetrievingDefect("Received " + iReq.getResponse().getStatusCode() + " error from server: " + iReq.getResponse().getStatusMessage());
      return;
    }

    // parse the defect received from the core
    Defect[] defects = Defect.fromJSONArray(response.getBody());
    if (defects.length > 0 && defects[0] != null) {
      controller.showDefect(defects[0]);
    }
    else {
      controller.errorRetrievingDefect("No defects received.");
    }
  }

  @Override
  public void responseError(IRequest iReq) {
    controller.errorRetrievingDefect("Received " + iReq.getResponse().getStatusCode() + " error from server: " + iReq.getResponse().getStatusMessage());
  }

  @Override
  public void fail(IRequest iReq, Exception exception) {
    // TODO deal with exception
    controller.errorRetrievingDefect("Unable to complete request: " + exception.getMessage());
  }
}
TOP

Related Classes of edu.wpi.cs.wpisuitetng.modules.defecttracker.search.observers.RetrieveDefectRequestObserver

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.