Example #1
0
  @GET
  @Path("feature-non-compliant-devices-with-details")
  public Response getFeatureNonCompliantDevicesWithDetails(
      @QueryParam("non-compliant-feature") String nonCompliantFeature,
      @QueryParam("platform") String platform,
      @QueryParam("ownership") String ownership,
      @QueryParam("pagination-enabled") String paginationEnabled,
      @QueryParam("start-index") int startIndex,
      @QueryParam("result-count") int resultCount) {

    Message message = new Message();
    if (nonCompliantFeature == null || paginationEnabled == null) {

      message.setErrorMessage("Missing required query parameters.");
      message.setDescription(
          "non-compliant-feature with some text value and "
              + "pagination-enabled with value true or false are required.");
      return Response.status(HttpStatus.SC_BAD_REQUEST).entity(message).build();

    } else if ("true".equals(paginationEnabled)) {

      if (startIndex < 0) {
        message.setErrorMessage("Invalid start index.");
        message.setDescription("Start index cannot be less than 0.");
        return Response.status(HttpStatus.SC_BAD_REQUEST).entity(message).build();
      }

      if (resultCount < 10) {
        message.setErrorMessage("Invalid request count.");
        message.setDescription("Requested result count should be equal to 10 or more than that.");
        return Response.status(HttpStatus.SC_BAD_REQUEST).entity(message).build();
      }

      Map<String, Object> filters = new LinkedHashMap<>();
      if (platform != null) {
        if ("android".equals(platform) || "ios".equals(platform) || "windows".equals(platform)) {
          filters.put("PLATFORM", platform);
        } else {
          message.setErrorMessage("Invalid value for platform query parameter.");
          message.setDescription(
              "platform query parameter value could only be either android, ios or windows.");
          return Response.status(HttpStatus.SC_BAD_REQUEST).entity(message).build();
        }
      }

      if (ownership != null) {
        if ("BYOD".equals(ownership) || "COPE".equals(ownership)) {
          filters.put("OWNERSHIP", ownership);
        } else {
          message.setErrorMessage("Invalid value for ownership query parameter.");
          message.setDescription(
              "ownership query parameter value could only be either BYOD or COPE.");
          return Response.status(HttpStatus.SC_BAD_REQUEST).entity(message).build();
        }
      }

      GadgetDataService gadgetDataService = MDMAPIUtils.getGadgetDataService();
      DashboardPaginationGadgetDataWrapper dashboardPaginationGadgetDataWrapper =
          new DashboardPaginationGadgetDataWrapper();

      PaginationResult paginationResult =
          gadgetDataService.getFeatureNonCompliantDevicesWithDetails(
              nonCompliantFeature, filters, new PaginationRequest(startIndex, resultCount));

      if (paginationResult == null) {
        return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).build();
      }

      Map<String, Object> featureNonCompliantDeviceDetailEntryDataWrapper;
      List<Map<String, Object>> featureNonCompliantDeviceDetailEntriesDataWrapper =
          new ArrayList<>();
      for (Object listElement : paginationResult.getData()) {
        Map entry = (Map<?, ?>) listElement;
        featureNonCompliantDeviceDetailEntryDataWrapper = new LinkedHashMap<>();
        featureNonCompliantDeviceDetailEntryDataWrapper.put("device-id", entry.get("device-id"));
        featureNonCompliantDeviceDetailEntryDataWrapper.put("platform", entry.get("platform"));
        featureNonCompliantDeviceDetailEntryDataWrapper.put("ownership", entry.get("ownership"));
        featureNonCompliantDeviceDetailEntryDataWrapper.put(
            "connectivity-details", entry.get("connectivity-details"));
        featureNonCompliantDeviceDetailEntriesDataWrapper.add(
            featureNonCompliantDeviceDetailEntryDataWrapper);
      }

      dashboardPaginationGadgetDataWrapper.setContext("feature-non-compliant-device-details");
      dashboardPaginationGadgetDataWrapper.setData(
          featureNonCompliantDeviceDetailEntriesDataWrapper);
      dashboardPaginationGadgetDataWrapper.setTotalRecordCount(paginationResult.getRecordsTotal());

      List<DashboardPaginationGadgetDataWrapper> responsePayload = new ArrayList<>();
      responsePayload.add(dashboardPaginationGadgetDataWrapper);

      return Response.status(HttpStatus.SC_OK).entity(responsePayload).build();

    } else if ("false".equals(paginationEnabled)) {

      Map<String, Object> filters = new LinkedHashMap<>();
      if (platform != null) {
        if ("android".equals(platform) || "ios".equals(platform) || "windows".equals(platform)) {
          filters.put("PLATFORM", platform);
        } else {
          message.setErrorMessage("Invalid value for platform query parameter.");
          message.setDescription(
              "platform query parameter value could only be either android, ios or windows.");
          return Response.status(HttpStatus.SC_BAD_REQUEST).entity(message).build();
        }
      }

      if (ownership != null) {
        if ("BYOD".equals(ownership) || "COPE".equals(ownership)) {
          filters.put("OWNERSHIP", ownership);
        } else {
          message.setErrorMessage("Invalid value for ownership query parameter.");
          message.setDescription(
              "ownership query parameter value could only be either BYOD or COPE.");
          return Response.status(HttpStatus.SC_BAD_REQUEST).entity(message).build();
        }
      }

      GadgetDataService gadgetDataService = MDMAPIUtils.getGadgetDataService();
      DashboardGadgetDataWrapper dashboardGadgetDataWrapper = new DashboardGadgetDataWrapper();

      List<Map<String, Object>> featureNonCompliantDevicesWithDetails =
          gadgetDataService.getFeatureNonCompliantDevicesWithDetails(nonCompliantFeature, filters);

      if (featureNonCompliantDevicesWithDetails == null) {
        return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).build();
      }

      dashboardGadgetDataWrapper.setContext("feature-non-compliant-device-details");
      dashboardGadgetDataWrapper.setData(featureNonCompliantDevicesWithDetails);

      List<DashboardGadgetDataWrapper> responsePayload = new ArrayList<>();
      responsePayload.add(dashboardGadgetDataWrapper);

      return Response.status(HttpStatus.SC_OK).entity(responsePayload).build();

    } else {

      message.setErrorMessage("Invalid query parameter value.");
      message.setDescription(
          "Invalid value for "
              + "query parameter pagination-enabled. Should be either true or false.");
      return Response.status(HttpStatus.SC_BAD_REQUEST).entity(message).build();
    }
  }