public Serializable getCacheableObject(String feedUri) throws Exception {
    JSONObject json = new JSONObject();

    PreparedQuery pq = datastoreService.prepare(new Query("feeds"));
    for (Entity entity : pq.asIterable()) {
      JSONObject item = new JSONObject();
      item.put("uri", "/addama/feeds/" + entity.getKey().getName());
      item.put("creator", entity.getProperty("creator").toString());
      json.append("items", item);
    }
    return json.toString();
  }
 @Test
 public void testPrepareStatement() throws Exception {
   QueryBuilder<Foo, Integer> qb =
       new QueryBuilder<Foo, Integer>(databaseType, baseFooTableInfo, null);
   PreparedQuery<Foo> stmt = qb.prepare();
   stmt.getStatement();
   StringBuilder sb = new StringBuilder();
   sb.append("SELECT * FROM ");
   databaseType.appendEscapedEntityName(sb, baseFooTableInfo.getTableName());
   sb.append(' ');
   assertEquals(sb.toString(), qb.prepareStatementString());
 }
Esempio n. 3
0
  public ArrayList<Training> trainingSearchByDomain(String domain) {
    ArrayList<Training> trainings = new ArrayList<>();

    Query.Filter domainFilter =
        new Query.FilterPredicate(DatabaseInfo.TRAINING_DOMAIN, Query.FilterOperator.EQUAL, domain);

    Query q = new Query(DatabaseInfo.TRAINING_DATABASE).setFilter(domainFilter);
    PreparedQuery pq = dataStore.prepare(q);

    for (Entity e : pq.asIterable()) {
      trainings.add(new Training(e));
    }
    return trainings;
  }
Esempio n. 4
0
  public static List<StorageNode> getStorageNodesInFolder(String path, Owner owner) {

    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();

    ArrayList<StorageNode> nodes = new ArrayList<StorageNode>();

    String folderPath = PathHelper.getFolderpath(path);

    // Read folders
    Query q = new Query("Folder");
    q.addFilter("path", FilterOperator.GREATER_THAN_OR_EQUAL, path);
    q.addFilter("path", FilterOperator.LESS_THAN, path + "\ufffd");
    q.addFilter("ownerId", FilterOperator.EQUAL, owner.getId());

    PreparedQuery pq = ds.prepare(q);
    List<Entity> results = pq.asList(FetchOptions.Builder.withLimit(99999));
    for (Entity e : results) {
      Folder f = new Folder();
      ReflectionHelper.setPropertiesFromEntity(Folder.class, f, e);

      log.debug("Folder compare: {} =?= {}", PathHelper.getFolderpath(f.getPath()), folderPath);
      if (PathHelper.getFolderpath(f.getPath()).equals(folderPath)) {
        nodes.add(f);
      }
    }

    // Read files
    Query q2 = new Query("File");
    q2.addFilter("path", FilterOperator.GREATER_THAN_OR_EQUAL, path);
    q2.addFilter("path", FilterOperator.LESS_THAN, path + "\ufffd");
    q2.addFilter("ownerId", FilterOperator.EQUAL, owner.getId());

    PreparedQuery pq2 = ds.prepare(q);
    List<Entity> results2 = pq2.asList(FetchOptions.Builder.withLimit(99999));
    for (Entity e : results2) {
      File f = new File();
      ReflectionHelper.setPropertiesFromEntity(File.class, f, e);

      log.debug("File compare: {} =?= {}", PathHelper.getFolderpath(f.getPath()), folderPath);
      if (PathHelper.getFolderpath(f.getPath()).equals(folderPath)) {

        nodes.add(f);
      }
    }

    return nodes;
  }
 @Test
 public void testOffsetAndLimit() throws Exception {
   QueryBuilder<Foo, Integer> qb =
       new QueryBuilder<Foo, Integer>(new LimitInline(), baseFooTableInfo, null);
   long offset = 200;
   long limit = 213;
   qb.offset(offset);
   qb.limit(limit);
   PreparedQuery<Foo> stmt = qb.prepare();
   stmt.getStatement();
   StringBuilder sb = new StringBuilder();
   sb.append("SELECT * FROM ");
   databaseType.appendEscapedEntityName(sb, baseFooTableInfo.getTableName());
   sb.append(" LIMIT ").append(limit);
   sb.append(" OFFSET ").append(offset).append(' ');
   assertEquals(sb.toString(), qb.prepareStatementString());
 }
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {
    //	Result result = null;

    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

    String name = request.getParameter("name");
    String pass = request.getParameter("pass");
    String lat = request.getParameter("lat");
    String lon = request.getParameter("lon");

    if (name.isEmpty() || pass.isEmpty() || lat.isEmpty() || lon.isEmpty()) {
      response.setContentType("text/plain");
      response.getWriter().println("ERROR1:Field Empty");
    }

    Filter driverdata = new FilterPredicate("email", FilterOperator.EQUAL, name);
    Query q1 = new Query("driver").setFilter(driverdata);

    PreparedQuery pq1 = datastore.prepare(q1);
    @SuppressWarnings("deprecation")
    int p = pq1.countEntities();
    if (p == 0) {
      response.setContentType("text/plain");
      response.getWriter().println("ERROR2:USER DOES NOT EXIST");
    } else {
      Entity res = pq1.asSingleEntity();
      if (res.getProperty("password").toString().equals(pass)) {
        // passwords match. update gcm id send ack

        res.setProperty("lat", lat);
        res.setProperty("lon", lon);
        datastore.put(res);

        // give auth as encrypted username
        // TODO Encrypted username

        response.setContentType("text/plain");
        response.getWriter().println("SUCCESS");

      } else {
        response.setContentType("text/plain");
        response.getWriter().println("ERROR3:Incorrect Password");
      }
    }
  }
Esempio n. 7
0
  public ArrayList<Training> trainingSearch(String search) {
    ArrayList<Training> trainings = new ArrayList<>();

    Query.Filter titleFilter =
        new Query.FilterPredicate(DatabaseInfo.TRAINING_TITLE, Query.FilterOperator.EQUAL, search);

    Query.Filter descriptionFilter =
        new Query.FilterPredicate(
            DatabaseInfo.TRAINING_DESCRIPTION, Query.FilterOperator.EQUAL, search);

    // Use CompositeFilter to combine multiple filters
    Query.Filter allFilter = Query.CompositeFilterOperator.or(titleFilter, descriptionFilter);

    Query q = new Query(DatabaseInfo.TRAINING_DATABASE).setFilter(allFilter);
    PreparedQuery pq = dataStore.prepare(q);

    for (Entity e : pq.asIterable()) {
      trainings.add(new Training(e));
    }
    return trainings;
  }
 /** Returns the number of student placements for the specified school and school year. */
 protected int getPrivateSchoolOCCPlacementCount(int schoolId, String schoolYearName)
     throws RemoteException {
   PreparedQuery query = null;
   if (!schoolYearName.equals("0")) {
     ReportBusiness rb = getReportBusiness();
     query = getQuery(QUERY_PRIVATE);
     if (query == null) {
       query = new PreparedQuery(getConnection());
       query.setSelectCountDistinctUsers();
       query.setPlacements(rb.getSchoolSeasonId());
       query.setNotNackaCitizens();
       query.setSchoolType(); // parameter 1
       query.setSchoolYearName(); // parameter 2
       query.setSchool(); // parameter 3
       query.prepare();
       setQuery(QUERY_PRIVATE, query);
     }
     if (schoolYearName.equals("F")) {
       query.setInt(1, rb.getPreSchoolClassTypeId());
     } else {
       query.setInt(1, rb.getElementarySchoolTypeId());
     }
     query.setString(2, schoolYearName);
     query.setInt(3, schoolId);
   } else { // 6 years old students
     query = getQuery(QUERY_PRIVATE_6_YEAR_STUDENTS);
     if (query == null) {
       query = new PreparedQuery(getConnection());
       query.setSelectCountDistinctUsers();
       query.setPlacements(getReportBusiness().getSchoolSeasonId());
       query.setNotNackaCitizens();
       query.setOnlyStudentsBorn(getReportBusiness().getSchoolSeasonStartYear() - 6);
       query.setSchoolYearName("1");
       query.setSchoolTypeElementarySchool();
       query.setSchool(); // parameter 1
       query.prepare();
       setQuery(QUERY_PRIVATE_6_YEAR_STUDENTS, query);
     }
     query.setInt(1, schoolId);
   }
   return query.execute();
 }
  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    resp.setContentType("text/html");
    resp.getWriter().println("<html><body>");

    String keyname = req.getParameter("keyname");
    String value = req.getParameter("value");

    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    // Using the synchronous cache.
    MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
    syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO));

    // display every element of kind TaskData for /datastore
    if (req.getParameterMap().isEmpty()) {
      // querying from datastore
      resp.getWriter().println("<h3>Datastore results:</h3>");
      List<String> listOfKeys = new ArrayList<String>();
      Query q = new Query("TaskData");
      PreparedQuery pq = datastore.prepare(q);
      for (Entity result : pq.asIterable()) {
        String datastore_key = result.getKey().getName();
        String taskData_value = (String) result.getProperty("value");
        Date taskData_date = (Date) result.getProperty("date");
        resp.getWriter()
            .println(
                "<p>keyname = "
                    + datastore_key
                    + "  value = "
                    + taskData_value
                    + " date = "
                    + taskData_date.toString()
                    + "</p>");
        listOfKeys.add(datastore_key);
      }
      // check which of the keys exist in memcache
      String memcache_value;
      resp.getWriter().println("<h3>Memcache results:</h3>");
      for (String datastore_key : listOfKeys) {
        memcache_value = (String) syncCache.get(datastore_key);
        if (memcache_value != null) {
          // String decoded = new String(memcache_value, "UTF-8");
          resp.getWriter()
              .println("<p>keyname = " + datastore_key + " value = " + memcache_value + "</p>");
        }
      }
    }

    // display element of kind TaskData with key=keyname
    else if (keyname != null && value == null) {

      // first check in the cache
      String memcache_value = (String) syncCache.get(keyname); // Read from cache.
      // Get value from datastore
      Key task_key = KeyFactory.createKey("TaskData", keyname);
      try {
        Entity tne = datastore.get(task_key);
        if (memcache_value == null) {
          resp.getWriter().println("<h2>Datastore</h2>");
        } else {
          resp.getWriter().println("<h2>Both</h2>");
        }

      } catch (EntityNotFoundException e) {
        resp.getWriter().println("<h2>Neither</h2>");
      }
    }

    // store element of kind TaskData with key=keyname and value=value
    else if (keyname != null && value != null) {
      Entity tne = new Entity("TaskData", keyname);
      tne.setProperty("value", value);
      tne.setProperty("date", new Date());
      datastore.put(tne);
      syncCache.put(keyname, value); // Populate cache.
      resp.getWriter()
          .println("<h2>Stored " + keyname + " and " + value + " in Datastore and Memcache</h2>");
    } else {

      resp.getWriter().println("<h2>You entered wrong query parameters</h2>");
    }

    /*
       Entity tne = new Entity("TaskData", "Person");
    alice.setProperty("gender", "female");
    alice.setProperty("age", 20);
    */

    resp.getWriter().println("</body></html>");
  }