Example #1
0
  /**
   * Get a User {@link Key} for a facebook id
   *
   * @param facebookId
   * @return the user {@link Key} or null
   */
  @SuppressWarnings("unchecked")
  public static Key getUserForFacebookId(String facebookId) {

    // check if APIKey -> TDUser Key found in cache
    if (MemcacheServiceFactory.getMemcacheService().contains(facebookId)) {
      // if so, return from cache
      return (Key) MemcacheServiceFactory.getMemcacheService().get(facebookId);
    } else {
      // query for matching user
      final String queryString = "SELECT key FROM " + TDUser.class.getName();
      final Query q = PMF.get().getPersistenceManager().newQuery(queryString);
      q.setFilter("facebookId == :p");
      q.setRange("0,1");

      final List<Key> keys = (List<Key>) q.execute(facebookId);

      final Key result = (keys.size() > 0 ? keys.get(0) : null);

      if (null != result) {
        // put ApiKey -> TDUser Key map in cache
        MemcacheServiceFactory.getMemcacheService().put(facebookId, result);
      }

      // return the found key
      return result;
    }
  }
Example #2
0
 public EntityMemcache(String namespace, CacheControl cacheControl, MemcacheStats stats) {
   this.memcache = new KeyMemcacheService(MemcacheServiceFactory.getMemcacheService(namespace));
   this.memcache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.SEVERE));
   this.memcacheWithRetry =
       new KeyMemcacheService(
           MemcacheServiceRetryProxy.createProxy(
               MemcacheServiceFactory.getMemcacheService(namespace)));
   this.stats = stats;
   this.cacheControl = cacheControl;
 }
Example #3
0
  public static PracticeProp create(String client, String displayName, String login) {

    Client.ensureValid(client);
    User.ensureClientLevelPrivilege(client, login, ClientLevelPrivilege.UPDATE_PRACTICE);

    ensureNotNullNotEmpty(displayName, "displayName is null or empty");
    String name = Utils.removeSpaceUnderscoreBracketAndHyphen(displayName.toLowerCase());

    List<Key<PracticeEntity>> keys =
        ofy(client).load().type(PracticeEntity.class).filter("name", name).keys().list();

    if (keys.size() != 0)
      throw new APIException()
          .status(Status.ERROR_RESOURCE_ALREADY_EXISTS)
          .message("There is already a practice with name [" + displayName + "]");

    String key = getUniqueKey(client, name);
    long val = MemcacheServiceFactory.getMemcacheService().increment(key, 1, (long) 0);

    if (val != 1)
      throw new APIException()
          .status(Status.ERROR_RESOURCE_ALREADY_EXISTS)
          .message("There is already a group with name [" + displayName + "]");

    PracticeEntity entity = new PracticeEntity();
    entity.practiceId = Sequence.getNext(client, SequenceType.PRACTICE);
    entity.name = name;
    entity.displayName = displayName;
    ofy(client).save().entity(entity).now();

    return entity.toProp();
  }
Example #4
0
  public void doPost(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException {
    String fileName = null;
    long startTime, endTime;
    String fileContentToDisplay = req.getParameter("FileToDisplay");
    res.getWriter().println("File conetent to be displayed : " + fileContentToDisplay);
    startTime = System.currentTimeMillis();
    // Find if the file is in memcache. If present there is no need to go to blobstore to get the
    // data
    MemcacheService mService = MemcacheServiceFactory.getMemcacheService();
    AppEngineFile readableFile;
    readableFile = (AppEngineFile) mService.get(fileName);

    if (readableFile != null) {
      FileService fService = FileServiceFactory.getFileService();
      res.getWriter().println("File " + fileName + " is present in cache");
      FileReadChannel readChannel = fService.openReadChannel(readableFile, false);
      BufferedReader reader = new BufferedReader(Channels.newReader(readChannel, "UTF8"));
      String line = null;
      res.getWriter().println("Contents of the file: ");

      while ((line = reader.readLine()) != null) {
        res.getWriter().println(line + "<br>");
      }
      readChannel.close();
    } else // Search for the file in blob
    {
      List<BlobInfo> blobToRead = new LinkedList<BlobInfo>();
      Iterator<BlobInfo> blobIterator = new BlobInfoFactory().queryBlobInfos();
      while (blobIterator.hasNext()) blobToRead.add(blobIterator.next());
      res.setContentType("text/plain");
      Boolean filePresent = false;
      int i;
      for (i = 0; i < blobToRead.size(); i++) {
        fileName = blobToRead.get(i).getFilename();
        if (fileName.equals(fileContentToDisplay)) {
          FileService fService = FileServiceFactory.getFileService();
          res.getWriter().println("File " + fileName + " is present in the blob store");

          readableFile = fService.getBlobFile(blobToRead.get(i).getBlobKey());

          FileReadChannel readChannel = fService.openReadChannel(readableFile, false);
          BufferedReader reader = new BufferedReader(Channels.newReader(readChannel, "UTF8"));
          String line = null;
          res.getWriter().println("printing the contents : ");

          while ((line = reader.readLine()) != null) {

            res.getWriter().println(line);
          }

          readChannel.close();
        } else {
          res.getWriter().println("File " + fileName + " is not present in the blob store");
        }
      }
    }
    endTime = System.currentTimeMillis();
    res.getWriter().println("Time taken for the operation is " + (endTime - startTime) + " ms");
  }
 @PUT
 @Consumes(MediaType.APPLICATION_XML)
 public Response putTaskData(String value) {
   Response res = null;
   // add your code here
   // first check if the Entity exists in the datastore
   DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
   MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
   syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO));
   Key entKey = KeyFactory.createKey("TaskData", keyname);
   Date date = new Date();
   try {
     // if it is, signal that we updated the entity
     Entity ts = datastore.get(entKey);
     ts.setProperty("value", value);
     ts.setProperty("date", date);
     datastore.put(ts);
     res = Response.noContent().build();
   } catch (EntityNotFoundException e) {
     // if it is not, create it and
     // signal that we created the entity in the datastore
     Entity taskdata = new Entity("TaskData", keyname);
     taskdata.setProperty("value", value);
     taskdata.setProperty("date", date);
     datastore.put(taskdata);
     res = Response.created(uriInfo.getAbsolutePath()).build();
   }
   TaskData td = new TaskData(keyname, value, date);
   syncCache.put(keyname, td);
   return res;
 }
 /** Performs lazy initialization on the cache */
 @Override
 public void onCacheLoad(List<CastViewObject> listObjects) {
   this.listCastedObjects = listObjects;
   MemcacheService cache =
       MemcacheServiceFactory.getMemcacheService(CACHE_KEY + getStrategyName());
   cache.clearAll();
 }
Example #7
0
 /**
  * Creates a counter that is not completely reliable (it might loose counts if memcache is
  * resetted)
  *
  * @param key a key that is used to persist the counter shards in the datastore and memcache. must
  *     not contain any slashes
  * @param chanceToWrite the relative probability that changes to memcache get persisted. Must be
  *     between 0 and 1. The higher the value, the more reliable the counter is.
  * @return a counter object
  */
 public static Counter createPageCounter(String key, double chanceToWrite) {
   return new Counter(
       new Random(),
       new DatastorePersistence(PARTITION),
       MemcacheServiceFactory.getMemcacheService(),
       chanceToWrite,
       key,
       50);
 }
  @Test
  public void entityGroupCount_printsCount() throws Exception {
    StringWriter responseWriter = new StringWriter();
    MemcacheService cache = MemcacheServiceFactory.getMemcacheService();
    Entity entity1 = new Entity("Simple");
    Key key1 = datastore.put(entity1);
    Key entityGroupKey = Entities.createEntityGroupKey(key1);

    EntityGroupCount groupCount = new EntityGroupCount(0, 0);
    groupCount.showEntityGroupCount(
        datastore, cache, new PrintWriter(responseWriter), entityGroupKey);

    assertThat(responseWriter.toString()).contains(" entities");
  }
Example #9
0
  /**
   * Method to get the "default" user to use as an object's creator.
   *
   * @return {@link Key} of the "default" user
   */
  @SuppressWarnings("unchecked")
  public static Key getDefaultUser() {
    if (MemcacheServiceFactory.getMemcacheService().contains(UserConstants.DEFAULT_USER_EMAIL)) {
      return (Key)
          MemcacheServiceFactory.getMemcacheService().get(UserConstants.DEFAULT_USER_EMAIL);
    } else {
      final String queryString = "SELECT key FROM " + TDUser.class.getName();
      final Query q = PMF.get().getPersistenceManager().newQuery(queryString);
      q.setFilter("email == :p");
      q.setRange("0,1");

      final List<Key> keys = (List<Key>) q.execute(UserConstants.DEFAULT_USER_EMAIL);

      if (null != keys && !keys.isEmpty()) {

        final Key result = (Key) keys.get(0);
        MemcacheServiceFactory.getMemcacheService().put(UserConstants.DEFAULT_USER_EMAIL, result);

        return result;
      }
      return null;
    }
  }
  /**
   * Handles message requests and determines the appropriate response.
   *
   * @param req The HttpServletRequest object from doGet/doPost
   * @param resp The HttpServletResponse object from doGet/doPost
   * @throws IOException
   */
  private void messageReceived(HttpServletRequest req, HttpServletResponse resp)
      throws IOException {
    String username = req.getParameter("usr");
    String querytitle = req.getParameter("querytitle");

    // add the chat information to memcache
    MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
    Entity entity;
    String cnt = "";

    // Check if the user is in room
    if (username == null) {
      resp.sendRedirect("/");
      return;
    }

    Message message;
    // Get the message type from the last part of the requested url
    String type = req.getPathInfo();
    Calendar now = Calendar.getInstance();
    int hours = now.get(Calendar.HOUR_OF_DAY);
    int minutes = now.get(Calendar.MINUTE);
    String time = hours + ":" + minutes;

    if (isMessage(type)) {
      message = new Message(CHAT_MESSAGE, username, req.getParameter("message"), time);
      if (syncCache.get(querytitle) == null) {
        entity = new Entity("Message", querytitle);
        entity.setProperty(
            "content", "[" + time + "] [" + username + "] " + req.getParameter("message") + "<br>");
        syncCache.put(querytitle, entity);
      } else {
        entity = (Entity) syncCache.get(querytitle);
        cnt = (String) entity.getProperty("content");
        cnt = cnt + "[" + time + "] [" + username + "] " + req.getParameter("message") + "<br>";
        entity.setProperty("content", cnt);
        syncCache.put(querytitle, entity);
        // System.out.println(cnt);
      }
      sendMessage(querytitle, message);
    } else if (isJoin(type)) {
      message = new Message(JOIN_MESSAGE, username, "", time);
      sendMessage(querytitle, message);
    } else if (isLeave(type)) {
      message = new Message(LEAVE_MESSAGE, username, "", time);
      req.getSession().removeAttribute(querytitle);
      sendMessage(querytitle, message);
    }
  }
 @Override
 public GetAccessTokenResult getAccessToken(Iterable<String> scopes) {
   MemcacheService memcache = MemcacheServiceFactory.getMemcacheService(MEMCACHE_NAMESPACE);
   String memcacheKey = memcacheKeyForScopes(scopes);
   GetAccessTokenResult result;
   Object memcacheResult = memcache.get(memcacheKey);
   if (memcacheResult != null) {
     result = (GetAccessTokenResult) memcacheResult;
   } else {
     result = getAccessTokenUncached(scopes);
     Date memcacheExpiration = new Date(result.getExpirationTime().getTime() - 300000);
     memcache.put(memcacheKey, result, Expiration.onDate(memcacheExpiration));
   }
   return result;
 }
Example #12
0
  public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    MemcacheService ms;

    Gson gson;
    String linkID;
    List<String> linkList;

    ms = MemcacheServiceFactory.getMemcacheService();

    gson = new Gson();
    linkID = req.getParameter("linkid");
    linkList = gson.fromJson(req.getParameter("linklist"), List.class);

    ms.put("LinkList_" + linkID, linkList);
  }
  /** Cast objects: return the list of objects that must be shown in the castable device */
  @SuppressWarnings("unchecked")
  public List<CastViewObject> castObjects(CastView castView) {
    MemcacheService cache =
        MemcacheServiceFactory.getMemcacheService(CACHE_KEY + getStrategyName());
    List<CastViewObject> castViewObjects = (List<CastViewObject>) cache.get(castView.getMnemonic());

    if (castViewObjects != null) {
      return castViewObjects;
    } else {
      castViewObjects = loadObjects(castView);
      cache.put(
          castView.getMnemonic(), castViewObjects, Expiration.byDeltaSeconds(EXPIRATION_TIME));
    }

    return castViewObjects;
  }
 @ApiMethod(
     name = "helloWithCachedCounter",
     path = "greetingCached",
     httpMethod = ApiMethod.HttpMethod.GET)
 public GreetingImpl helloWithCachedCounter() {
   GreetingImpl greeter = new GreetingImpl();
   MemcacheService memcacheService = MemcacheServiceFactory.getMemcacheService();
   Integer cnt = (Integer) memcacheService.get(Constants.MEMCACHE_COUNTER_KEY);
   if (cnt == null) {
     cnt = new Integer(0);
   }
   cnt++;
   memcacheService.put(Constants.MEMCACHE_COUNTER_KEY, cnt);
   greeter.setMessage("Hello! You have got this message " + cnt + " times.");
   return greeter;
 }
  @Override
  public void doGet(final HttpServletRequest req, final HttpServletResponse resp)
      throws IOException {

    final MemcacheService cacheShared = MemcacheServiceFactory.getMemcacheService();
    if (cacheShared.contains(MemCacheKey.activePoints)) {
      try {
        final Map<String, Point> points =
            (Map<String, Point>) cacheShared.get(MemCacheKey.activePoints);
        cacheShared.delete(
            MemCacheKey.activePoints); // TODO possible race condition with record value service
        for (final Point point : points.values()) {
          TaskFactory.getInstance().startMoveCachedValuesToStoreTask(point);
        }
      } catch (InvalidValueException e) {
        cacheShared.clearAll();
      }
    }
  }
  @DELETE
  public void deleteIt() {

    // delete an entity from the datastore
    // just print a message upon exception (don't throw)
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
    if (syncCache.get(keyname) != null) {
      syncCache.delete(keyname);
    }
    Key entKey = KeyFactory.createKey("TaskData", keyname);
    try {
      Entity ent = datastore.get(entKey);
      datastore.delete(entKey);
      System.err.println("TaskData deleted in Datastore");
    } catch (EntityNotFoundException e) {
      System.err.println("TaskData not found in Datastore");
    }
  }
 // for the application
 @GET
 @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
 public TaskData getTaskData() {
   // same code as above method
   DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
   MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
   if (syncCache.get(keyname) != null) {
     TaskData ts = (TaskData) syncCache.get(keyname);
     return ts;
   }
   Key entKey = KeyFactory.createKey("TaskData", keyname);
   try {
     Entity ent = datastore.get(entKey);
     TaskData ts =
         new TaskData(keyname, (String) ent.getProperty("value"), (Date) ent.getProperty("date"));
     return ts;
   } catch (EntityNotFoundException e) {
     throw new RuntimeException("Get: TaskData with " + keyname + " not found");
   }
 }
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    MemcacheService cache = MemcacheServiceFactory.getMemcacheService();

    List<String> authors;
    if (cache.get("authors") == null) {
      authors = new LinkedList();
    } else {
      authors = (List<String>) cache.get("authors");
    }
    String author = req.getParameter("author");
    String message = req.getParameter("message");

    authors.add(author);
    cache.put("authors", authors);
    cache.put(author, message);

    req.getRequestDispatcher("/cache.jsp").forward(req, resp);
  }
Example #19
0
  public static PracticeProp rename(
      String client, long practiceId, String newDisplayName, String login) {
    Client.ensureValid(client);
    User.ensureClientLevelPrivilege(client, login, ClientLevelPrivilege.UPDATE_PRACTICE);

    PracticeEntity entity = safeGet(client, practiceId);

    ensureNotNullNotEmpty(newDisplayName, "newDisplayName is null or empty");
    String newName = Utils.removeSpaceUnderscoreBracketAndHyphen(newDisplayName.toLowerCase());

    if (entity.name.equals(newName)) {
      // ideally should be inside a transaction
      entity.displayName = newDisplayName;
      ofy(client).save().entity(entity).now();
      return entity.toProp();
    }

    List<Key<PracticeEntity>> keys =
        ofy(client).load().type(PracticeEntity.class).filter("name", newName).keys().list();
    if (keys.size() != 0)
      throw new APIException()
          .status(Status.ERROR_RESOURCE_ALREADY_EXISTS)
          .message("There is already a practice with name [" + newDisplayName + "]");

    String key = getUniqueKey(client, newName);
    long val = MemcacheServiceFactory.getMemcacheService().increment(key, 1, (long) 0);

    if (val != 1)
      throw new APIException()
          .status(Status.ERROR_RESOURCE_ALREADY_EXISTS)
          .message("There is already a practice with name [" + newDisplayName + "]");

    // ideally should be inside a transaction
    entity.name = newName;
    entity.displayName = newDisplayName;
    ofy(client).save().entity(entity).now();

    return entity.toProp();
  }
 // for the browser
 @GET
 @Produces(MediaType.TEXT_XML)
 public TaskData getTaskDataHTML() {
   // add your code here (get Entity from datastore using this.keyname)
   // throw new RuntimeException("Get: TaskData with " + keyname +  " not found");
   // if not found
   DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
   MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
   if (syncCache.get(keyname) != null) {
     TaskData ts = (TaskData) syncCache.get(keyname);
     return ts;
   }
   Key entKey = KeyFactory.createKey("TaskData", keyname);
   try {
     Entity ent = datastore.get(entKey);
     TaskData ts =
         new TaskData(keyname, (String) ent.getProperty("value"), (Date) ent.getProperty("date"));
     return ts;
   } catch (EntityNotFoundException e) {
     throw new RuntimeException("Get: TaskData with " + keyname + " not found");
   }
 }
  public RootResource() {
    cache = MemcacheServiceFactory.getMemcacheService();
    velocityEngine = new VelocityEngine();

    // Required for Velocity to run on GAE
    velocityEngine.setProperty(
        "runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogChute");

    // Load Velocity templates from the classpath (WEB-INF/classes)
    velocityEngine.setProperty("resource.loader", "class");
    velocityEngine.setProperty(
        "class.resource.loader.class",
        "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
    velocityEngine.init();

    // Load service properties
    Properties props = loadProperties();

    provider =
        new CachingProvider(
            new RottenTomatoesProvider(props.getProperty("rottenTomatoesApiKey")), cache);
    openingMovies = new OpeningMoviesResource(provider, velocityEngine);
  }
 @Before
 public void setUp() {
   service = MemcacheServiceFactory.getAsyncMemcacheService();
 }
Example #23
0
  public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    int index;

    DatastoreService ds;
    MemcacheService ms;
    BlobstoreService bs;
    URLFetchService us;
    FileService fs;

    Map<String, List<BlobKey>> blobMap;
    BlobKey blobKey;
    BlobInfoFactory blobInfoFactory;
    BlobInfo blobInfo;

    int postCount;
    int postIndex;
    String postType;
    String postText;
    String postDelpw;
    String postShowgallery;
    DataObj dataObj;
    String filelink;

    String picUrl;
    HTTPResponse picRes;
    List<HTTPHeader> headerList;
    String picMime;
    String[] fileNamePart;
    AppEngineFile picFile;
    FileWriteChannel writeChannel;

    PostObj postObj;
    List<PostObj> postObjList;
    String linkID;

    resp.setCharacterEncoding("UTF-8");
    resp.setContentType("text/plain");

    try {
      ds = DatastoreServiceFactory.getDatastoreService();
      ms = MemcacheServiceFactory.getMemcacheService();
      bs = BlobstoreServiceFactory.getBlobstoreService();
      us = URLFetchServiceFactory.getURLFetchService();
      fs = FileServiceFactory.getFileService();
      blobInfoFactory = new BlobInfoFactory(ds);

      blobMap = bs.getUploads(req);
      postCount = Integer.valueOf(req.getParameter("input_post_count"));
      postObjList = new ArrayList<PostObj>();

      for (postIndex = 0; postIndex < postCount; postIndex++) {
        try {
          postType = req.getParameter("input_post_type_" + postIndex);
          if (postType == null) {
            continue;
          }

          postText = req.getParameter("input_post_text_" + postIndex);
          postDelpw = req.getParameter("input_post_delpw_" + postIndex);
          postShowgallery = req.getParameter("input_post_showgallery_" + postIndex);
          if (postShowgallery == null) {
            postShowgallery = "";
          }

          if (postType.equals("file") == true) {
            blobKey = blobMap.get("input_post_file_" + postIndex).get(0);
            if (blobKey == null) {
              throw new Exception();
            }

            dataObj = new DataObj();
            dataObj.fileid = createUID();
            dataObj.posttime = new Date().getTime();
            dataObj.delpw = postDelpw;
            dataObj.blobkey = blobKey;
            dataObj.putDB(ds);

            blobInfo = blobInfoFactory.loadBlobInfo(dataObj.blobkey);
            filelink =
                "http://"
                    + req.getServerName()
                    + "/down/"
                    + dataObj.fileid
                    + "/"
                    + blobInfo.getFilename();
            postObj =
                new PostObj(
                    dataObj.fileid,
                    filelink,
                    blobInfo.getSize(),
                    dataObj.posttime,
                    dataObj.delpw,
                    postShowgallery);
            postObjList.add(postObj);
          } else if (postType.equals("url") == true) {
            picUrl = postText;

            picRes = us.fetch(new URL(picUrl));
            headerList = picRes.getHeaders();
            picMime = "application/octet-stream";
            for (index = 0; index < headerList.size(); index++) {
              if (headerList.get(index).getName().compareToIgnoreCase("Content-Type") == 0) {
                picMime = headerList.get(index).getValue();
                break;
              }
            }

            fileNamePart = picUrl.split("/");

            picFile = fs.createNewBlobFile(picMime, fileNamePart[fileNamePart.length - 1]);
            writeChannel = fs.openWriteChannel(picFile, true);
            writeChannel.write(ByteBuffer.wrap(picRes.getContent()));
            writeChannel.closeFinally();

            dataObj = new DataObj();
            dataObj.fileid = createUID();
            dataObj.posttime = new Date().getTime();
            dataObj.delpw = postDelpw;
            dataObj.blobkey = fs.getBlobKey(picFile);
            dataObj.putDB(ds);

            blobInfo = blobInfoFactory.loadBlobInfo(dataObj.blobkey);
            filelink =
                "http://"
                    + req.getServerName()
                    + "/down/"
                    + dataObj.fileid
                    + "/"
                    + blobInfo.getFilename();
            postObj =
                new PostObj(
                    dataObj.fileid,
                    filelink,
                    blobInfo.getSize(),
                    dataObj.posttime,
                    dataObj.delpw,
                    postShowgallery);
            postObjList.add(postObj);
          }
        } catch (Exception e) {
        }
      }

      linkID = postFile(us, postObjList);

      if (req.getParameter("specflag") != null) {
        resp.getWriter().print("http://tnfshmoe.appspot.com/link.jsp?linkid=" + linkID);
      } else {
        resp.sendRedirect("http://tnfshmoe.appspot.com/link.jsp?linkid=" + linkID);
      }
    } catch (Exception e) {
    }
  }
  private static MemcacheService cacheInit(String nameSpace) {
    MemcacheService memcache = MemcacheServiceFactory.getMemcacheService(nameSpace);

    return memcache;
  }
  @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>");
  }
public class AudioClipService {
  DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
  BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
  MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
  UserService userService = new UserService();

  public List<AudioClipInstance> getOtherUsersAudioClips(String userId) throws BadRequestException {
    List<AudioClipInstance> audioClipInstanceList;

    // no need to check for user existance here! if UserId is null, then all audioClips are returned
    Filter userFilter =
        new FilterPredicate(TuneInConstants.AUDIO_CLIP_OWNER_ID, FilterOperator.NOT_EQUAL, userId);
    Query q = new Query(TuneInConstants.AUDIO_CLIP_TYPE).setFilter(userFilter);
    PreparedQuery pq = datastore.prepare(q);

    // check mem-cache for the results first
    String CACHE_KEY = TuneInConstants.OTHERS_WORK_KEY + userId;
    audioClipInstanceList = (ArrayList<AudioClipInstance>) syncCache.get(CACHE_KEY);

    if (audioClipInstanceList == null) {
      audioClipInstanceList = new ArrayList<AudioClipInstance>();
      User user;
      AudioClipInstance audioClipInstance;

      for (Entity result : pq.asIterable()) {
        user =
            userService.getUserById(
                (String) result.getProperty(TuneInConstants.AUDIO_CLIP_OWNER_ID));
        audioClipInstance =
            new AudioClipInstance(
                KeyFactory.keyToString(result.getKey()),
                (String) result.getProperty(TuneInConstants.AUDIO_CLIP_TITLE),
                user,
                (String) result.getProperty(TuneInConstants.AUDIO_CLIP_AUDIO_ID),
                (String) result.getProperty(TuneInConstants.AUDIO_CLIP_IMAGE_ID),
                (Date) result.getProperty(TuneInConstants.AUDIO_CLIP_DATE));
        audioClipInstanceList.add(audioClipInstance);
      }
      syncCache.put(CACHE_KEY, audioClipInstanceList, Expiration.byDeltaSeconds(300));
    }

    return audioClipInstanceList;
  }

  public AudioClip getAudioClipById(String id) throws BadRequestException {
    AudioClip audioClip;
    try {
      Key audio_key = KeyFactory.stringToKey(id);
      Entity result = datastore.get(audio_key);
      audioClip =
          new AudioClip(
              id,
              (String) result.getProperty(TuneInConstants.AUDIO_CLIP_TITLE),
              (String) result.getProperty(TuneInConstants.AUDIO_CLIP_OWNER_ID),
              (String) result.getProperty(TuneInConstants.AUDIO_CLIP_AUDIO_ID),
              (String) result.getProperty(TuneInConstants.AUDIO_CLIP_IMAGE_ID),
              (Date) result.getProperty(TuneInConstants.AUDIO_CLIP_DATE));
      return audioClip;
    } catch (Exception e) {
      throw new BadRequestException();
    }
  }

  public List<AudioClip> getAudioClipsByUser(String userId) throws BadRequestException {
    List<AudioClip> audioClipList = new ArrayList<AudioClip>();

    // verify userId
    validateUserExistance(userId); // throws bad request exception of user doesn't exist
    Filter userFilter =
        new FilterPredicate(TuneInConstants.AUDIO_CLIP_OWNER_ID, FilterOperator.EQUAL, userId);
    Query q =
        new Query(TuneInConstants.AUDIO_CLIP_TYPE)
            .setFilter(userFilter)
            .addSort(TuneInConstants.AUDIO_CLIP_DATE, SortDirection.ASCENDING);
    PreparedQuery pq = datastore.prepare(q);
    AudioClip audioClip;
    for (Entity result : pq.asIterable()) {
      audioClip =
          new AudioClip(
              KeyFactory.keyToString(result.getKey()),
              (String) result.getProperty(TuneInConstants.AUDIO_CLIP_TITLE),
              (String) result.getProperty(TuneInConstants.AUDIO_CLIP_OWNER_ID),
              (String) result.getProperty(TuneInConstants.AUDIO_CLIP_AUDIO_ID),
              (String) result.getProperty(TuneInConstants.AUDIO_CLIP_IMAGE_ID),
              (Date) result.getProperty(TuneInConstants.AUDIO_CLIP_DATE));
      audioClipList.add(audioClip);
    }
    return audioClipList;
  }

  public String newAudioClip(String userId, String title, String audio, String image)
      throws BadRequestException {
    // validate existence of the user
    validateUserExistance(userId); // throws bad request exception of user doesn't exist

    Entity audioClip = new Entity(TuneInConstants.AUDIO_CLIP_TYPE);
    audioClip.setProperty(TuneInConstants.AUDIO_CLIP_TITLE, title);
    audioClip.setProperty(TuneInConstants.AUDIO_CLIP_AUDIO_ID, audio);
    audioClip.setProperty(TuneInConstants.AUDIO_CLIP_IMAGE_ID, image);
    audioClip.setProperty(TuneInConstants.AUDIO_CLIP_OWNER_ID, userId);
    audioClip.setProperty(TuneInConstants.AUDIO_CLIP_DATE, new Date());
    datastore.put(audioClip);
    return KeyFactory.keyToString(audioClip.getKey());
  }

  public void deleteAudioClip(String audioClipId) throws BadRequestException {
    AudioClip audioClip = getAudioClipById(audioClipId);
    datastore.delete(KeyFactory.stringToKey(audioClipId));

    // task queue to delete the associated audio and image blobs
    Queue queue = QueueFactory.getDefaultQueue();
    queue.add(
        TaskOptions.Builder.withUrl("/rest/users/" + audioClip.getOwnerId() + "/audioClips/audio")
            .method(TaskOptions.Method.DELETE)
            .param("blobkey", audioClip.getAudioId()));
    queue.add(
        TaskOptions.Builder.withUrl("/rest/users/" + audioClip.getOwnerId() + "/audioClips/image")
            .method(TaskOptions.Method.DELETE)
            .param("blobkey", audioClip.getImageId()));
  }

  public void deleteBlob(String blobkey) {
    BlobKey blob_key = new BlobKey(blobkey);
    blobstoreService.delete(blob_key);
  }

  // memcache only endpoints
  public List<AudioClip> getAudioClipInMemcache() throws BadRequestException {
    ArrayList<AudioClip> audioClipList =
        (ArrayList<AudioClip>) syncCache.get(TuneInConstants.ALL_AUDIO_CLIPS);
    return audioClipList;
  }

  public String newAudioClipInMemcache(String ownerId, AudioClip audioClip)
      throws BadRequestException {
    validateUserExistance(ownerId);
    ArrayList<AudioClip> audioCLipList =
        (ArrayList<AudioClip>) syncCache.get(TuneInConstants.ALL_AUDIO_CLIPS);
    if (audioCLipList == null) {
      audioCLipList = new ArrayList<AudioClip>();
    }
    audioCLipList.add(audioClip);
    syncCache.put(TuneInConstants.ALL_AUDIO_CLIPS, audioCLipList, Expiration.byDeltaSeconds(60));
    return audioClip.getKeyname();
  }

  private void validateUserExistance(String userId) throws BadRequestException {
    // validate existence of the user
    userService.getUserById(userId); // throws bad request exception of user doesn't exist
  }
}
Example #27
0
public class StringStore {
  // private static final Logger log =
  // Logger.getLogger(com.almende.dialog.state.StringStore.class.getName());

  static DatastoreService datastore = ParallelInit.getDatastore();
  static MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();

  public static boolean dropEntity(String id) {
    Entity entity = getEntity(id);
    if (entity != null) {
      // log.warning("drop :"+id+ " : "+ entity.getKey());
      datastore.delete(entity.getKey());
      syncCache.delete(id);
      return true;
    }
    return false;
  }

  public static void dropString(String id) {
    while (dropEntity(id)) ;
  }

  public static void storeString(String id, String text) {
    Entity entity = getEntity(id);

    if (entity == null) entity = new Entity("storedString");
    // log.warning("store :"+id+" : "+entity.getKey());

    entity.setProperty("id", id);
    entity.setUnindexedProperty("string", new Text(text));
    datastore.put(entity);
    syncCache.put(id, entity);
  }

  @SuppressWarnings("deprecation")
  public static Entity getEntity(String id) {
    Entity entity = null;
    if (syncCache.contains(id)) {
      entity = (Entity) syncCache.get(id);
    }
    if (entity == null) {
      Query q = new Query("storedString");
      //			q.setFilter(new Query.FilterPredicate("id", Query.FilterOperator.EQUAL, id));
      q.addFilter("id", Query.FilterOperator.EQUAL, id);
      PreparedQuery pq = datastore.prepare(q);
      try {
        entity = pq.asSingleEntity();
      } catch (PreparedQuery.TooManyResultsException e) {
        Iterator<Entity> iter = pq.asIterator();
        while (iter.hasNext()) {
          Entity ent = iter.next();
          if (entity == null) {
            entity = ent;
          } else {
            datastore.delete(ent.getKey());
          }
        }
      }
      if (entity != null) {
        // log.warning("store (because found in datastore) :"+id+" : "+entity.getKey());
        syncCache.put(id, entity);
      }
    }
    // log.warning("return :"+id+" : "+(entity!=null?entity.getKey():""));

    return entity;
  }

  public static String getString(String id) {
    Entity entity = getEntity(id);
    if (entity == null) return null;
    return ((Text) entity.getProperty("string")).getValue();
  }
}
/**
 * Mapped to /routesmemcache. GET, POST and DELETE are supported to manipulate Route objects in the
 * datastore.
 *
 * @author Aviral Takkar
 *     <p>Curl examples:
 *     <p>GET - curl "http://go-places-ucsb.appspot.com/rest/routesmemcache/{route_id}"
 *     <p>POST - curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X POST
 *     --data "{\"origin\":{\"address\":\"santa barbara\"},\"destination\":{\"address\":\"los
 *     angeles\"}, \"mapJsonAsText\":\"xyz\"}" http://go-places-ucsb.appspot.com/rest/routesmemcache
 */
@Path("/routesmemcache")
public class RoutesMemcacheAPI {
  @Context UriInfo uriInfo;
  @Context Request request;
  Random key = new java.util.Random();
  MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();

  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public String addRoute(Route route, @Context HttpServletResponse servletResponse)
      throws IOException {
    try {
      Entity routeEntity = new Entity("Route");
      routeEntity.setProperty("originPlaceID", route.getOrigin().getGooglePlaceId());
      routeEntity.setProperty("destinationPlaceID", route.getDestination().getGooglePlaceId());
      routeEntity.setProperty("duration", route.getDuration());
      routeEntity.setProperty("distance", route.getDistance());
      routeEntity.setProperty("origin", route.getOrigin().getAddress());
      routeEntity.setProperty("destination", route.getDestination().getAddress());

      Text mapJsonAsText = route.getMapJsonAsText();
      routeEntity.setProperty("routeJSON", mapJsonAsText);

      syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO));
      String cacheKey = "route-" + key.nextLong();
      syncCache.put(cacheKey, routeEntity);
      return new JSONObject().append("status", "OK").append("key", cacheKey).toString();
    } catch (Exception e) {
      return new JSONObject()
          .append("status", "fail")
          .append("message", "Could not cache object.")
          .toString();
    }
  }

  @GET
  @Path("{route_id}")
  @Produces(MediaType.APPLICATION_JSON)
  public String getPlace(@PathParam("route_id") String routeID) {
    JSONObject reply = new JSONObject();
    try {

      syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO));

      Entity result = (Entity) syncCache.get(routeID);
      if (result != null)
        return reply
            .append("status", "OK")
            .append("message", "Found in memcache.")
            .append("Route Object", result)
            .toString();

      return reply.append("status", "fail").append("message", "Not found in memcache.").toString();
    } catch (Exception e) {
      return new JSONObject()
          .append("status", "fail")
          .append("message", "Route object with ID " + routeID + " not found.")
          .toString();
    }
  }

  @DELETE
  @Path("{route_id}")
  @Produces(MediaType.APPLICATION_JSON)
  public String deletePlace(@PathParam("route_id") String routeID) {
    syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO));
    JSONObject reply = new JSONObject();
    try {
      syncCache.delete(routeID);
      return reply
          .append("status", "OK")
          .append("message", "successfully deleted route " + routeID + " from " + "memcache")
          .toString();
    } catch (Exception e) {
      return new JSONObject().append("status", "fail").append("message", e.getMessage()).toString();
    }
  }
}
Example #29
0
public class ChatServlet extends HttpServlet {

  private static final String _tokenKey = "token_list";
  private static final String _messageKey = "message_list";

  public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {

    UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();

    String message_type = req.getParameter("type");
    String user_name = user.getNickname();

    String userId = user.getUserId();

    if (message_type.compareTo("message") == 0) {
      String message = req.getParameter("text");
      String chat_message = chatMessage(user_name, message);
      sendChannelMessageToAll(user_name, chat_message);
      addMessageToCache(chat_message);
    } else if (message_type.compareTo("get_token") == 0) {
      // generate and give token to user
      ChannelService channelService = ChannelServiceFactory.getChannelService();
      String token = channelService.createChannel(userId);
      addToCacheList(_tokenKey, userId);

      resp.setCharacterEncoding("UTF-8");
      resp.setContentType("text/html");
      PrintWriter out = resp.getWriter();
      out.print(tokenMessage(user_name, token));
    } else if (message_type.compareTo("leave") == 0) {
      removeFromCacheList(_tokenKey, userId);
    }
  }

  private String chatMessage(String user_name, String message) {
    Map pack = new HashMap<String, String>();
    pack.put("type", "'message'");
    pack.put("user", "'" + user_name + "'");
    pack.put("text", "'" + message + "'");
    return responseToJson(pack);
  }

  private String tokenMessage(String user_name, String token) {
    Map pack = new HashMap<String, String>();
    pack.put("type", "'token'");
    pack.put("user", "'" + user_name + "'");
    pack.put("token", "'" + token + "'");

    String messages = messageArrayJson();

    if (messages.length() > 0) {
      pack.put("messages", messages);
    }

    return responseToJson(pack);
  }

  private void sendChannelMessageToAll(String author, String message) {
    ChannelService channelService = ChannelServiceFactory.getChannelService();
    List<String> keys = getListFromCacahe(_tokenKey);

    for (String k : keys) {
      channelService.sendMessage(new ChannelMessage(k, message));
    }
  }

  public static String responseToJson(Map pack) {
    String response = "{";
    if (pack != null) {
      for (Object entry : pack.entrySet()) {
        response += ((Map.Entry) entry).getKey() + ":" + ((Map.Entry) entry).getValue() + ",";
      }

      // Remove trail comma
      response = response.substring(0, response.length() - 1);
      response += "}";
    }

    return response;
  }

  private String messageArrayJson() {
    List<String> messages = getListFromCacahe(_messageKey);

    if (messages == null) return "";

    String ret = "[";

    for (String mess : messages) {
      ret += mess + ",";
    }

    // Remove trail comma
    ret = ret.substring(0, ret.length() - 1);
    ret += "]";

    return ret;
  }

  private void addMessageToCache(String message) {
    List messages = (List) keycache.get(_messageKey);

    if (messages == null) messages = new LinkedList<String>();

    if (messages.size() > 50) messages.remove(0);
    messages.add(message);
    keycache.put(_messageKey, messages);
  }

  private static MemcacheService keycache = MemcacheServiceFactory.getMemcacheService();

  public static void addToCacheList(String key, String str) {
    // Add to string list in cache
    //
    if (!keycache.contains(key)) {
      List names = new LinkedList<String>();
      names.add(str);
      keycache.put(key, names);
    } else {
      List names = (List) keycache.get(key);
      if (!names.contains(str)) {
        names.add(str);
        keycache.put(key, names);
      }
    }
  }

  private List<String> getListFromCacahe(String key) {
    return (List) keycache.get(key);
  }

  private static void removeFromCacheList(String key, String str) {
    List names = (List) keycache.get(key);
    names.remove(str);
    keycache.put(key, names);
  }
}
Example #30
0
/** The server-side RPC endpoint for {@link Service}. */
@SuppressWarnings("serial")
public class ServiceImpl extends RemoteServiceServlet implements Service {
  private static final Logger log = Logger.getLogger(ServiceImpl.class.getName());
  /** A reference to a cache service. */
  private final CacheApplication cache =
      new CacheApplication(MemcacheServiceFactory.getMemcacheService());

  private final CacheApplication cacheApp =
      new CacheApplication(MemcacheServiceFactory.getMemcacheService());
  /** A reference to the data store. */
  private final StoreDB store = new StoreDB();

  private static ArrayList<ApplicationTemplate> toClientApplicationTemplates(
      ArrayList<StoreDB.ApplicationTemplate> applicationTemplates) {
    final ArrayList<ApplicationTemplate> clients = new ArrayList<ApplicationTemplate>();
    for (StoreDB.ApplicationTemplate n : applicationTemplates) {
      clients.add(
          new ApplicationTemplate(
              n.getAppId(),
              Utils.toClientApplication(n.getApp()),
              n.getTId(),
              Utils.toClientTemplate(n.getT()),
              n.getFlags(),
              n.getParentMenuId(),
              n.getRank(),
              n.getUserId(),
              n.getLastUpdatedAt()));
    }
    return clients;
  }

  private static ArrayList<TemplateTree> toClientTemplateTrees(
      ArrayList<StoreDB.TemplateTree> templateTrees) {
    final ArrayList<TemplateTree> clients = new ArrayList<TemplateTree>();
    for (StoreDB.TemplateTree tt : templateTrees) {
      clients.add(
          new TemplateTree(
              tt.getId(),
              tt.getCode(),
              tt.getName(),
              tt.getDesc(),
              tt.getTId(),
              tt.getFlags(),
              tt.getRank(),
              tt.getUserId(),
              tt.getLastUpdatedAt()));
    }
    return clients;
  }

  private static ArrayList<TemplateTreeItem> toClientTemplateTreeItems(
      ArrayList<StoreDB.TemplateTreeItem> templateTreeItems) {
    final ArrayList<TemplateTreeItem> clients = new ArrayList<TemplateTreeItem>();
    for (StoreDB.TemplateTreeItem tti : templateTreeItems) {
      clients.add(
          new TemplateTreeItem(
              tti.getId(),
              tti.getTtId(),
              Utils.toClientTemplateTree(tti.getTt()),
              tti.getTaId(),
              Utils.toClientTemplateAttribute(tti.getTa()),
              tti.getRank(),
              tti.getSubsetOf()));
    }
    return clients;
  }

  private static ArrayList<TemplateList> toClientTemplateLists(
      ArrayList<StoreDB.TemplateList> templateTrees) {
    final ArrayList<TemplateList> clients = new ArrayList<TemplateList>();
    for (StoreDB.TemplateList tl : templateTrees) {
      clients.add(
          new TemplateList(
              tl.getId(),
              tl.getCode(),
              tl.getName(),
              tl.getDesc(),
              tl.getTId(),
              tl.getFlags(),
              tl.getRank(),
              tl.getUserId(),
              tl.getLastUpdatedAt()));
    }
    return clients;
  }

  private static ArrayList<TemplateListItem> toClientTemplateListItems(
      ArrayList<StoreDB.TemplateListItem> templateTreeItems) {
    final ArrayList<TemplateListItem> clients = new ArrayList<TemplateListItem>();
    for (StoreDB.TemplateListItem tli : templateTreeItems) {
      clients.add(
          new TemplateListItem(
              tli.getId(),
              tli.getTlId(),
              Utils.toClientTemplateList(tli.getTl()),
              tli.getTaId(),
              Utils.toClientTemplateAttribute(tli.getTa()),
              tli.getRank()));
    }
    return clients;
  }

  private static ArrayList<TemplateAttribute> toClientTemplateAttributes(
      ArrayList<StoreDB.TemplateAttribute> templateAttributes) {
    final ArrayList<TemplateAttribute> clients = new ArrayList<TemplateAttribute>();
    for (StoreDB.TemplateAttribute ta : templateAttributes) {
      clients.add(Utils.toClientTemplateAttribute(ta));
    }
    return clients;
  }

  private static ArrayList<TemplateRelation> toClientTemplateRelations(
      ArrayList<StoreDB.TemplateRelation> templateRelations) {
    final ArrayList<TemplateRelation> clients = new ArrayList<TemplateRelation>();
    for (StoreDB.TemplateRelation tr : templateRelations) {
      clients.add(Utils.toClientTemplateRelation(tr));
    }
    return clients;
  }

  @Override
  public ArrayList<ApplicationTemplate> getApplicationTemplates(Application app) {
    ArrayList<ApplicationTemplate> result = new ArrayList<ApplicationTemplate>();
    final StoreDB.Api api = store.getApi();
    try {
      final ArrayList<ApplicationTemplate> fromCache =
          cacheApp.getApplicationTemplates(app.getId());
      if (fromCache != null) {
        return fromCache;
      }

      return cacheApp.putApplicationTemplates(
          app.getId(), toClientApplicationTemplates(api.getAppTemplatesByApp(app.getId())));
    } catch (SQLException ex) {
      log.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
    } finally {
      api.close();
    }

    return result;
  }

  @Override
  public List<TemplateTree> getTemplateTrees(int tId) {
    final StoreDB.Api api = store.getApi();
    try {
      final List<TemplateTree> fromCache = cache.getTemplateTrees(tId);
      if (fromCache != null) {
        return fromCache;
      }

      return cache.putTemplateTrees(tId, toClientTemplateTrees(api.getTemplateTrees(tId)));
    } catch (SQLException ex) {
      log.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
    } finally {
      api.close();
    }

    return new ArrayList<TemplateTree>();
  }

  @Override
  public List<TemplateTreeItem> getTemplateTreeItems(TemplateTree tt) {
    ArrayList<TemplateTreeItem> result = new ArrayList<TemplateTreeItem>();
    final StoreDB.Api api = store.getApi();
    try {
      final List<TemplateTreeItem> fromCache = cache.getTemplateTreeItems(tt);
      if (fromCache != null) {
        return fromCache;
      }

      return cache.putTemplateTreeItems(
          tt, toClientTemplateTreeItems(api.getTemplateTreeItems(tt.getId())));
    } catch (SQLException ex) {
      log.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
    } finally {
      api.close();
    }

    return result;
  }

  @Override
  public List<TemplateList> getTemplateLists(int tId) {
    List<TemplateList> result = new ArrayList<TemplateList>();
    final StoreDB.Api api = store.getApi();
    try {
      final List<TemplateList> fromCache = cache.getTemplateLists(tId);
      if (fromCache != null) {
        return fromCache;
      }

      return cache.putTemplateLists(tId, toClientTemplateLists(api.getTemplateLists(tId)));
    } catch (SQLException ex) {
      log.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
    } finally {
      api.close();
    }

    return result;
  }

  @Override
  public List<TemplateListItem> getTemplateListItems(TemplateList tl) {
    ArrayList<TemplateListItem> result = new ArrayList<TemplateListItem>();
    final StoreDB.Api api = store.getApi();
    try {
      final List<TemplateListItem> fromCache = cache.getTemplateListItems(tl);
      if (fromCache != null) {
        return fromCache;
      }

      return cache.putTemplateListItems(
          tl, toClientTemplateListItems(api.getTemplateListItems(tl.getId())));
    } catch (SQLException ex) {
      log.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
    } finally {
      api.close();
    }

    return result;
  }

  @Override
  public List<TemplateAttribute> getTemplateAttributes(Template t) {
    ArrayList<TemplateAttribute> result = new ArrayList<TemplateAttribute>();
    final StoreDB.Api api = store.getApi();
    try {
      final List<TemplateAttribute> fromCache = cache.getTemplateAttributes(t);
      if (fromCache != null) {
        return fromCache;
      }

      return cache.putTemplateAttributes(
          t, toClientTemplateAttributes(api.getTemplateAttributes(t.getId())));
    } catch (SQLException ex) {
      log.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
    } finally {
      api.close();
    }

    return result;
  }

  @Override
  public List<TemplateRelation> getTemplateRelations(Template t) {
    ArrayList<TemplateRelation> result = new ArrayList<TemplateRelation>();
    final StoreDB.Api api = store.getApi();
    try {
      final List<TemplateRelation> fromCache = cache.getTemplateRelations(t);
      if (fromCache != null) {
        return fromCache;
      }

      return cache.putTemplateRelations(
          t, toClientTemplateRelations(api.getTemplateRelations(t.getId())));
    } catch (SQLException ex) {
      log.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
    } finally {
      api.close();
    }

    return result;
  }
}