コード例 #1
0
  public void connect(MessageContext messageContext) throws ConnectException {
    try {

      String spreadsheetName =
          GoogleSpreadsheetUtils.lookupFunctionParam(messageContext, SPREADSHEET_NAME);
      if (spreadsheetName == null || "".equals(spreadsheetName.trim())) {
        log.error("Please make sure you have given a name for the spreadsheet");
        ConnectException connectException =
            new ConnectException("Please make sure you have given a name for the spreadsheet");
        GoogleSpreadsheetUtils.storeErrorResponseStatus(messageContext, connectException);
        return;
      }

      SpreadsheetService ssService =
          new GoogleSpreadsheetClientLoader(messageContext).loadSpreadsheetService();

      GoogleSpreadsheet gss = new GoogleSpreadsheet(ssService);

      SpreadsheetEntry ssEntry = gss.getSpreadSheetsByTitle(spreadsheetName);

      GoogleSpreadsheetWorksheet gssWorksheet =
          new GoogleSpreadsheetWorksheet(ssService, ssEntry.getWorksheetFeedUrl());

      List<String> resultData = gssWorksheet.getAllWorksheets();

      int resultSize = resultData.size();

      if (messageContext.getEnvelope().getBody().getFirstElement() != null) {
        messageContext.getEnvelope().getBody().getFirstElement().detach();
      }

      OMFactory factory = OMAbstractFactory.getOMFactory();
      OMNamespace ns =
          factory.createOMNamespace("http://org.wso2.esbconnectors.googlespreadsheet", "ns");
      OMElement searchResult = factory.createOMElement("getAllWorksheetsResult", ns);

      OMElement result = factory.createOMElement("result", ns);
      searchResult.addChild(result);
      result.setText("true");

      OMElement data = factory.createOMElement("data", ns);
      searchResult.addChild(data);

      for (int iterateCount = 0; iterateCount < resultSize; iterateCount++) {
        if (resultData.get(iterateCount) != null) {
          OMElement title = factory.createOMElement("title", ns);
          data.addChild(title);
          title.setText(resultData.get(iterateCount));
        }
      }

      messageContext.getEnvelope().getBody().addChild(searchResult);

    } catch (IOException te) {
      log.error("Failed to show status: " + te.getMessage(), te);
      GoogleSpreadsheetUtils.storeErrorResponseStatus(messageContext, te);
    } catch (ServiceException te) {
      log.error("Failed to show status: " + te.getMessage(), te);
      GoogleSpreadsheetUtils.storeErrorResponseStatus(messageContext, te);
    } catch (Exception te) {
      log.error("Failed to show status: " + te.getMessage(), te);
      GoogleSpreadsheetUtils.storeErrorResponseStatus(messageContext, te);
    }
  }
コード例 #2
0
  public void connect(MessageContext messageContext) throws ConnectException {
    try {
      String worksheetName =
          GoogleSpreadsheetUtils.lookupFunctionParam(messageContext, WORKSHEET_NAME);
      String spreadsheetName =
          GoogleSpreadsheetUtils.lookupFunctionParam(messageContext, SPREADSHEET_NAME);

      if ((worksheetName == null || "".equals(worksheetName.trim()))
          && (spreadsheetName == null || "".equals(spreadsheetName.trim()))) {
        log.error(
            "Please make sure you have given a valid input for the worksheet or spreadsheet name");
        ConnectException connectException =
            new ConnectException(
                "Please make sure you have given a valid input for the worksheet or spreadsheet name");
        GoogleSpreadsheetUtils.storeErrorResponseStatus(messageContext, connectException);
        return;
      }

      SpreadsheetService ssService =
          new GoogleSpreadsheetClientLoader(messageContext).loadSpreadsheetService();

      GoogleSpreadsheet gss = new GoogleSpreadsheet(ssService);

      SpreadsheetEntry ssEntry = gss.getSpreadSheetsByTitle(spreadsheetName);

      if (ssEntry != null) {

        List<Person> resultData = null;

        resultData = gss.getAuthors(ssEntry);

        if (worksheetName != null) {

          GoogleSpreadsheetWorksheet gssWorksheet =
              new GoogleSpreadsheetWorksheet(ssService, ssEntry.getWorksheetFeedUrl());

          WorksheetEntry wsEntry = gssWorksheet.getWorksheetByTitle(worksheetName);
          if (wsEntry != null) {
            if (gssWorksheet.getAuthors(wsEntry).size() > 0) {
              resultData.clear();
              resultData = gssWorksheet.getAuthors(wsEntry);
            }
          } else {
            ConnectException connectException =
                new ConnectException(
                    "Cannot retrieve the authors. Worksheet with the given name is not available.");
            log.error("Error occured: " + connectException.getMessage(), connectException);
            GoogleSpreadsheetUtils.storeErrorResponseStatus(messageContext, connectException);
            return;
          }
        }

        int resultSize = resultData.size();

        GoogleSpreadsheetUtils.removeTransportHeaders(messageContext);

        if (messageContext.getEnvelope().getBody().getFirstElement() != null) {
          messageContext.getEnvelope().getBody().getFirstElement().detach();
        }

        OMFactory factory = OMAbstractFactory.getOMFactory();
        OMNamespace ns =
            factory.createOMNamespace("http://org.wso2.esbconnectors.googlespreadsheet", "ns");
        OMElement searchResult = factory.createOMElement("getAuthorsResult", ns);

        OMElement result = factory.createOMElement("result", ns);
        searchResult.addChild(result);
        result.setText("true");

        OMElement data = factory.createOMElement("data", ns);
        searchResult.addChild(data);

        for (int iterateCount = 0; iterateCount < resultSize; iterateCount++) {
          if (resultData.get(iterateCount) != null) {
            OMElement author = factory.createOMElement("author", ns);
            data.addChild(author);
            author.setText(resultData.get(iterateCount).getName());
          }
        }

        messageContext.getEnvelope().getBody().addChild(searchResult);
      } else {
        ConnectException connectException =
            new ConnectException(
                "Cannot retrieve the authors. Spreadsheet with the given name is not available.");
        log.error("Error occured: " + connectException.getMessage(), connectException);
        GoogleSpreadsheetUtils.storeErrorResponseStatus(messageContext, connectException);
      }

    } catch (IOException te) {
      log.error("Error occured " + te.getMessage(), te);
      GoogleSpreadsheetUtils.storeErrorResponseStatus(messageContext, te);
    } catch (ServiceException te) {
      log.error("Error occured " + te.getMessage(), te);
      GoogleSpreadsheetUtils.storeErrorResponseStatus(messageContext, te);
    } catch (Exception te) {
      log.error("Error occured " + te.getMessage(), te);
      GoogleSpreadsheetUtils.storeErrorResponseStatus(messageContext, te);
    }
  }