Beispiel #1
1
  /**
   * List the entities in JSON format
   *
   * @param entities entities to return as JSON strings
   */
  public static String writeJSON(Iterable<Entity> entities) {
    logger.log(Level.INFO, "creating JSON format object");
    StringBuilder sb = new StringBuilder();

    int i = 0;
    sb.append("{\"data\": [");
    for (Entity result : entities) {
      Map<String, Object> properties = result.getProperties();
      sb.append("{");
      if (result.getKey().getName() == null)
        sb.append("\"name\" : \"" + result.getKey().getId() + "\",");
      else sb.append("\"name\" : \"" + result.getKey().getName() + "\",");

      for (String key : properties.keySet()) {
        Object object = properties.get(key);
        if (object instanceof Text) {
          Text value = (Text) properties.get(key);
          sb.append("\"" + key + "\" : \"" + value.getValue() + "\",");
          // logger.info(value.getValue());//debug;
        } else {
          sb.append("\"" + key + "\" : \"" + properties.get(key) + "\",");
        }
      }
      sb.deleteCharAt(sb.lastIndexOf(","));
      sb.append("},");
      i++;
    }
    if (i > 0) {
      sb.deleteCharAt(sb.lastIndexOf(","));
    }
    sb.append("]}");
    return sb.toString();
  }
  /**
   * This method gets the entity having primary key id. It uses HTTP GET method.
   *
   * @param id the primary key of the java bean.
   * @return The entity with primary key id.
   * @throws UnauthorizedException
   */
  @ApiMethod(
      name = "findById",
      scopes = {Config.EMAIL_SCOPE},
      clientIds = {Config.CHROME_CLIENT_ID, Config.WEB_CLIENT_ID, Config.API_EXPLORER_CLIENT_ID})
  public ServiceResponse findById(@Named("_name") String _name, @Named("_id") Long _id, User user)
      throws UnauthorizedException {
    if (user == null) {
      throw new UnauthorizedException("UnauthorizedException # User is Null.");
    }
    Key key = KeyFactory.createKey(_name, _id);

    DatastoreService dsService = DatastoreServiceFactory.getDatastoreService();
    ServiceResponse res = new ServiceResponse();
    try {
      Entity entity = dsService.get(key);
      res.set_id(entity.getKey().getId());

      res.set_createdAt((Date) entity.getProperty(_createdAt));
      res.set_createdBy((String) entity.getProperty(_createdBy));

      res.set_upatedAt((Date) entity.getProperty(_updatedAt));
      res.set_updatedBy((String) entity.getProperty(_updatedBy));

      res.set_status((String) entity.getProperty(_status));

      Text dataText = (Text) entity.getProperty(data);
      res.setData(dataText.getValue());

    } catch (com.google.appengine.api.datastore.EntityNotFoundException e) {
      throw new EntityNotFoundException("Object does not exist.");
    }

    return res;
  }
  /**
   * This method lists all the entities inserted in datastore. It uses HTTP GET method and paging
   * support.
   *
   * @return A CollectionResponse class containing the list of all entities persisted and a cursor
   *     to the next page.
   * @throws UnauthorizedException
   */
  @ApiMethod(
      name = "findAll",
      scopes = {Config.EMAIL_SCOPE},
      clientIds = {Config.CHROME_CLIENT_ID, Config.WEB_CLIENT_ID, Config.API_EXPLORER_CLIENT_ID})
  public CollectionResponse<ServiceResponse> findAll(
      @Named("_name") String _name,
      @Nullable @Named("cursor") String cursorString,
      @Nullable @Named("limit") Integer limit,
      User user)
      throws UnauthorizedException {
    if (user == null) {
      throw new UnauthorizedException("UnauthorizedException # User is Null.");
    }
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    if (limit == null) {
      limit = 10;
    }
    FetchOptions fetchOptions = FetchOptions.Builder.withLimit(limit);
    if (cursorString != null) {
      fetchOptions.startCursor(Cursor.fromWebSafeString(cursorString));
    }
    Query q = new Query(_name);
    q.addSort("_updatedAt", SortDirection.DESCENDING);
    PreparedQuery pq = datastore.prepare(q);
    QueryResultList<Entity> results = pq.asQueryResultList(fetchOptions);

    List<ServiceResponse> responses = new ArrayList<ServiceResponse>();
    ServiceResponse res = null;
    for (Entity entity : results) {
      res = new ServiceResponse();
      // TODO add properties from entity to service response
      res.set_id(entity.getKey().getId());

      res.set_createdAt((Date) entity.getProperty(_createdAt));
      res.set_createdBy((String) entity.getProperty(_createdBy));

      res.set_upatedAt((Date) entity.getProperty(_updatedAt));
      res.set_updatedBy((String) entity.getProperty(_updatedBy));

      res.set_status((String) entity.getProperty(_status));

      Text dataText = (Text) entity.getProperty(data);
      res.setData(dataText.getValue());

      responses.add(res);
    }

    cursorString = results.getCursor().toWebSafeString();

    return CollectionResponse.<ServiceResponse>builder()
        .setItems(responses)
        .setNextPageToken(cursorString)
        .build();
  }
  /**
   * Returns an instance of {@code TestReport} representing the given test report entity.
   *
   * @param entity the test report entity.
   * @return the {@code TestReport} instance.
   * @throws java.lang.NullPointerException if {@code entity} is {@code null}.
   * @throws java.lang.IllegalArgumentException if {@code entity} king is not {@code TestReport}.
   */
  public static TestReport from(Entity entity) {
    checkNotNull(entity, "'entity' parameter cannot be null");
    checkArgument(TEST_REPORT.equals(entity.getKind()), "'entity' parameter wrong kind");

    final List<Test> failedTests = new ArrayList<>();
    try {
      final JSONArray jsonArray =
          new JSONArray(new JSONTokener(((Text) entity.getProperty("failedTests")).getValue()));
      for (int i = 0; i < jsonArray.length(); i++) {
        final Test test = Test.valueOf(jsonArray.getJSONObject(i));
        failedTests.add(test);
      }
    } catch (JSONException e) {
      throw new RuntimeException(e);
    }

    final List<Test> ignoredTests = new ArrayList<>();
    try {
      final Text ignoredTestsEntity = (Text) entity.getProperty("ignoredTests");
      if (ignoredTestsEntity != null) {
        final JSONArray jsonArray = new JSONArray(new JSONTokener(ignoredTestsEntity.getValue()));
        for (int i = 0; i < jsonArray.length(); i++) {
          final Test test = Test.valueOf(jsonArray.getJSONObject(i));
          ignoredTests.add(test);
        }
      } else {
        log.info(
            String.format(
                "Ignored test do not have detailed information : %s [%s]",
                entity.getProperty("buildTypeId"), entity.getProperty("buildId")));
      }
    } catch (JSONException e) {
      throw new RuntimeException(e);
    }

    return new TestReport(
        (String) entity.getProperty("buildTypeId"),
        ((Long) entity.getProperty("buildId")).intValue(),
        (Date) entity.getProperty("buildDate"),
        ((Long) entity.getProperty("buildDuration")),
        ((Long) entity.getProperty("numberOfPassedTests")).intValue(),
        ((Long) entity.getProperty("numberOfIgnoredTests")).intValue(),
        ((Long) entity.getProperty("numberOfFailedTests")).intValue(),
        failedTests,
        ignoredTests);
  }
Beispiel #5
0
 public String getPayLoad() {
   return payLoad.getValue();
 }
Beispiel #6
0
 public void setMapJsonAsText(Text mapJsonAsText) {
   this.mapJsonAsText = new Text(mapJsonAsText.getValue());
 }
 private static ObjectNode getTextNode(Text text) {
   ObjectNode dateNode = JsonUtils.getObjectMapper().createObjectNode();
   dateNode.put("type", "text");
   dateNode.put("value", text.getValue());
   return dateNode;
 }
 public String getMessage() {
   return message == null ? null : message.getValue();
 }
Beispiel #9
0
 public String getBody() {
   return (body == null) ? null : body.getValue();
 }