Created
August 5, 2014 17:14
-
-
Save iperdomo/b9263a8a7c6d6f4680e9 to your computer and use it in GitHub Desktop.
Sample changes for akvo-flow issue #651
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/GAE/src/com/gallatinsystems/common/util/MemCacheUtils.java b/GAE/src/com/gallatinsystems/common/util/MemCacheUtils.java | |
index a2e6c80..f78408b 100644 | |
--- a/GAE/src/com/gallatinsystems/common/util/MemCacheUtils.java | |
+++ b/GAE/src/com/gallatinsystems/common/util/MemCacheUtils.java | |
@@ -27,7 +27,6 @@ import net.sf.jsr107cache.Cache; | |
import net.sf.jsr107cache.CacheFactory; | |
import net.sf.jsr107cache.CacheManager; | |
-import com.gallatinsystems.framework.dao.BaseDAO; | |
import com.google.appengine.api.memcache.MemcacheService; | |
import com.google.appengine.api.memcache.stdimpl.GCacheFactory; | |
@@ -37,7 +36,7 @@ public class MemCacheUtils { | |
/** | |
* Initialize a Cache object with a expiration delta defined in seconds | |
- * | |
+ * | |
* @param expirySeconds Expiration delta defined in seconds | |
* @return A Cache object or <b>null</b> when the runtime couldn't initialize the object | |
*/ | |
@@ -65,7 +64,7 @@ public class MemCacheUtils { | |
* The current implementation of Cache.put always return null, therefore this method doesn't | |
* return any value. <b>NOTE:</b> A failed put operation will get logged but also fail silently | |
* to the executing program | |
- * | |
+ * | |
* @param cache An initialized Cache object | |
* @param key The key (must implement java.io.Serializable) | |
* @param value The value (must implement java.io.Serializable) | |
@@ -84,7 +83,7 @@ public class MemCacheUtils { | |
/** | |
* Puts a set of objects in the cache, the expiration is already defined in the cache object.<br> | |
- * | |
+ * | |
* @param cache An initialized Cache object | |
* @param objects A map containing objects to be cached along with their corresponding cache | |
* keys. The keys and objects must implement java.io.Serializable. | |
@@ -104,7 +103,7 @@ public class MemCacheUtils { | |
/** | |
* Delegates testing to `Cache.containsKey`, handles runtime exceptions.<br> | |
* The runtime could raise an exception on "too long" requests, or partial service outage | |
- * | |
+ * | |
* @param cache Initialized Cache object | |
* @param key Key to search (must implement java.io.Serializable) | |
* @return true|false depending if the Cache contains that key.<br> | |
@@ -125,10 +124,11 @@ public class MemCacheUtils { | |
/** | |
* Takes in a list of keys and returns the objects which are found in the cache | |
- * | |
+ * | |
* @param keys | |
* @return | |
*/ | |
+ @SuppressWarnings("rawtypes") | |
public static Map getObjects(Cache cache, Collection keys) { | |
try { | |
if (cache == null) { | |
@@ -141,15 +141,4 @@ public class MemCacheUtils { | |
} | |
return Collections.emptyMap(); | |
} | |
- | |
- /** | |
- * Return consistent format for cache keys | |
- * | |
- * @param dao | |
- * @param objectId | |
- * @return | |
- */ | |
- public static String getCacheKey(BaseDAO dao, String objectId) { | |
- return dao.getClass().getSimpleName() + "-" + objectId; | |
- } | |
} | |
diff --git a/GAE/src/com/gallatinsystems/framework/domain/BaseDomain.java b/GAE/src/com/gallatinsystems/framework/domain/BaseDomain.java | |
index 29b0aab..c4ca31f 100644 | |
--- a/GAE/src/com/gallatinsystems/framework/domain/BaseDomain.java | |
+++ b/GAE/src/com/gallatinsystems/framework/domain/BaseDomain.java | |
@@ -86,4 +86,11 @@ public abstract class BaseDomain implements Serializable { | |
public void setCreateUserId(Long createUserId) { | |
this.createUserId = createUserId; | |
} | |
+ | |
+ public String getCacheKey() { | |
+ if (this.getKey() == null) { | |
+ throw new RuntimeException("Trying to get cache key from unsaved object"); | |
+ } | |
+ return this.getClass().getSimpleName() + "-" + this.getKey().getId(); | |
+ } | |
} | |
diff --git a/GAE/src/com/gallatinsystems/survey/dao/QuestionDao.java b/GAE/src/com/gallatinsystems/survey/dao/QuestionDao.java | |
index be7da79..340c2cb 100644 | |
--- a/GAE/src/com/gallatinsystems/survey/dao/QuestionDao.java | |
+++ b/GAE/src/com/gallatinsystems/survey/dao/QuestionDao.java | |
@@ -16,6 +16,11 @@ | |
package com.gallatinsystems.survey.dao; | |
+import static com.gallatinsystems.common.util.MemCacheUtils.containsKey; | |
+import static com.gallatinsystems.common.util.MemCacheUtils.initCache; | |
+import static com.gallatinsystems.common.util.MemCacheUtils.putObject; | |
+import static com.gallatinsystems.common.util.MemCacheUtils.putObjects; | |
+ | |
import java.lang.reflect.Field; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
@@ -49,8 +54,6 @@ import com.google.appengine.api.datastore.Entity; | |
import com.google.appengine.api.datastore.Key; | |
import com.google.appengine.api.datastore.Transaction; | |
-import static com.gallatinsystems.common.util.MemCacheUtils.*; | |
- | |
/** | |
* saves/finds question objects | |
*/ | |
@@ -73,7 +76,7 @@ public class QuestionDao extends BaseDAO<Question> { | |
/** | |
* lists all questions filtered by type optionally filtered by surveyId as well. | |
- * | |
+ * | |
* @param surveyId | |
* @param type | |
* @return | |
@@ -101,7 +104,7 @@ public class QuestionDao extends BaseDAO<Question> { | |
/** | |
* loads the Question object but NOT any associated options | |
- * | |
+ * | |
* @param id | |
* @return | |
*/ | |
@@ -112,12 +115,12 @@ public class QuestionDao extends BaseDAO<Question> { | |
/** | |
* lists minimal question information by surveyId | |
- * | |
+ * | |
* @param surveyId | |
* @return | |
*/ | |
public List<Question> listQuestionsBySurvey(Long surveyId) { | |
- List<Question> questionsList = (List<Question>) listByProperty("surveyId", surveyId, | |
+ List<Question> questionsList = listByProperty("surveyId", surveyId, | |
"Long", "order", "asc"); | |
if (questionsList == null) { | |
@@ -131,7 +134,7 @@ public class QuestionDao extends BaseDAO<Question> { | |
/** | |
* Delete a list of questions | |
- * | |
+ * | |
* @param qList | |
*/ | |
public void delete(List<Question> qList) { | |
@@ -141,7 +144,7 @@ public class QuestionDao extends BaseDAO<Question> { | |
/** | |
* Delete question from data store. | |
- * | |
+ * | |
* @param question | |
*/ | |
public void delete(Question question) throws IllegalDeletionException { | |
@@ -150,7 +153,7 @@ public class QuestionDao extends BaseDAO<Question> { | |
/** | |
* Delete a question and adjust the question order for the remaining questions if specified | |
- * | |
+ * | |
* @param question | |
* @param adjustQuestionOrder | |
* @throws IllegalDeletionException | |
@@ -201,7 +204,7 @@ public class QuestionDao extends BaseDAO<Question> { | |
/** | |
* Delete all the questions in a group | |
- * | |
+ * | |
* @param surveyId | |
* @throws IllegalDeletionException | |
*/ | |
@@ -213,7 +216,7 @@ public class QuestionDao extends BaseDAO<Question> { | |
/** | |
* lists all questions in a group and orders them by their sortOrder | |
- * | |
+ * | |
* @param groupId | |
* @return | |
*/ | |
@@ -225,7 +228,7 @@ public class QuestionDao extends BaseDAO<Question> { | |
/** | |
* lists all questions for a survey and orders them by sort order. THIS METHOD SHOULD NOT BE | |
* USED AS SORT ORDERS MAY BE DUPLICATED ACROSS QUESTIONGROUPS SO THE ORDERING IS UNDEFINED | |
- * | |
+ * | |
* @param surveyId | |
* @return | |
* @deprecated | |
@@ -251,7 +254,7 @@ public class QuestionDao extends BaseDAO<Question> { | |
/** | |
* list questions in order, by using question groups. Optionally filtered by type author: Mark | |
* Tiele Westra | |
- * | |
+ * | |
* @param surveyId | |
* @return | |
*/ | |
@@ -279,7 +282,7 @@ public class QuestionDao extends BaseDAO<Question> { | |
/** | |
* Lists questions by questionGroupId and type | |
- * | |
+ * | |
* @param questionGroupId | |
* @param type | |
* @return | |
@@ -301,7 +304,7 @@ public class QuestionDao extends BaseDAO<Question> { | |
/** | |
* saves a question object in a transaction | |
- * | |
+ * | |
* @param q | |
* @return | |
*/ | |
@@ -350,7 +353,7 @@ public class QuestionDao extends BaseDAO<Question> { | |
/** | |
* saves a question, including its question options, translations, and help media (if any). | |
- * | |
+ * | |
* @param question | |
* @param questionGroupId | |
* @return | |
@@ -424,7 +427,7 @@ public class QuestionDao extends BaseDAO<Question> { | |
/** | |
* Saves question and update cache | |
- * | |
+ * | |
* @param question | |
*/ | |
public Question save(Question question) { | |
@@ -436,7 +439,7 @@ public class QuestionDao extends BaseDAO<Question> { | |
/** | |
* Save a collection of questions and cache | |
- * | |
+ * | |
* @param qList | |
* @return | |
*/ | |
@@ -449,7 +452,7 @@ public class QuestionDao extends BaseDAO<Question> { | |
/** | |
* Add a collection of Question objects to the cache. If the object already exists in the cached | |
* questions list, they are replaced by the ones passed in through this list | |
- * | |
+ * | |
* @param qList | |
*/ | |
private void cache(List<Question> qList) { | |
@@ -459,7 +462,7 @@ public class QuestionDao extends BaseDAO<Question> { | |
Map<Object, Object> cacheMap = new HashMap<Object, Object>(); | |
for (Question qn : qList) { | |
- String cacheKey = getCacheKey(Long.toString(qn.getKey().getId())); | |
+ String cacheKey = qn.getCacheKey(); | |
cacheMap.put(cacheKey, qn); | |
} | |
@@ -468,7 +471,7 @@ public class QuestionDao extends BaseDAO<Question> { | |
/** | |
* Remove a collection of questions from the cache | |
- * | |
+ * | |
* @param qList | |
*/ | |
private void uncache(List<Question> qList) { | |
@@ -477,7 +480,7 @@ public class QuestionDao extends BaseDAO<Question> { | |
} | |
for (Question qn : qList) { | |
- String cacheKey = getCacheKey(Long.toString(qn.getKey().getId())); | |
+ String cacheKey = qn.getCacheKey(); | |
if (containsKey(cache, cacheKey)) { | |
cache.remove(cacheKey); | |
} | |
@@ -486,7 +489,7 @@ public class QuestionDao extends BaseDAO<Question> { | |
/** | |
* finds a question by its reference id | |
- * | |
+ * | |
* @param refid | |
* @return | |
* @deprecated | |
@@ -500,7 +503,7 @@ public class QuestionDao extends BaseDAO<Question> { | |
/** | |
* finds a question by its id. If needDetails is true, all child objects (options, help, | |
* translations) will also be loaded. | |
- * | |
+ * | |
* @param id | |
* @param needDetails | |
* @return | |
@@ -553,7 +556,7 @@ public class QuestionDao extends BaseDAO<Question> { | |
/** | |
* lists questions within a group ordered by creation date | |
- * | |
+ * | |
* @param questionGroupId | |
* @return | |
*/ | |
@@ -567,7 +570,7 @@ public class QuestionDao extends BaseDAO<Question> { | |
* lists questions within a group. If needDetails flag is true, the child objects will be loaded | |
* for each question. Due to processing constraints on GAE, needDetails should only be true when | |
* calling this method if being called from a backend or task. | |
- * | |
+ * | |
* @param questionGroupId | |
* @param needDetails | |
* @return | |
@@ -581,7 +584,7 @@ public class QuestionDao extends BaseDAO<Question> { | |
* lists all the questions in a group, optionally loading details. If allowSideEffects is true, | |
* it will attempt to reorder any duplicated question orderings on retrieval. New users of this | |
* method should ALWAY call this with allowSideEffects = false | |
- * | |
+ * | |
* @param questionGroupId | |
* @param needDetails | |
* @param allowSideEffects | |
@@ -633,7 +636,7 @@ public class QuestionDao extends BaseDAO<Question> { | |
/** | |
* finds q question by its path and order. Path is defined as the name of the | |
* "surveyGroupName/surveyName/QuestionGroupName" | |
- * | |
+ * | |
* @param order | |
* @param path | |
* @return | |
@@ -654,7 +657,7 @@ public class QuestionDao extends BaseDAO<Question> { | |
/** | |
* finds a question within a group by matching on the questionText passed in | |
- * | |
+ * | |
* @param questionGroupId | |
* @param questionText | |
* @return | |
@@ -678,7 +681,7 @@ public class QuestionDao extends BaseDAO<Question> { | |
/** | |
* finds a question by groupId and order. If there are questions with duplicated orders, the | |
* first is returned. | |
- * | |
+ * | |
* @param questionGroupId | |
* @param order | |
* @return | |
@@ -701,7 +704,7 @@ public class QuestionDao extends BaseDAO<Question> { | |
/** | |
* updates ONLY the order field within the question object for the questions passed in. All | |
* questions must exist in the datastore | |
- * | |
+ * | |
* @param questionList | |
*/ | |
public void updateQuestionOrder(List<Question> questionList) { | |
@@ -718,7 +721,7 @@ public class QuestionDao extends BaseDAO<Question> { | |
/** | |
* updates ONLY the order field within the question group object for the questions passed in. | |
* All question groups must exist in the datastore | |
- * | |
+ * | |
* @param questionList | |
*/ | |
public void updateQuestionGroupOrder(List<QuestionGroup> groupList) { | |
@@ -735,7 +738,7 @@ public class QuestionDao extends BaseDAO<Question> { | |
/** | |
* lists all questions that depend on the id passed in | |
- * | |
+ * | |
* @param questionId | |
* @return | |
*/ | |
diff --git a/GAE/src/org/waterforpeople/mapping/analytics/dao/SurveyQuestionSummaryDao.java b/GAE/src/org/waterforpeople/mapping/analytics/dao/SurveyQuestionSummaryDao.java | |
index 5c3bd9c..e859b2b 100644 | |
--- a/GAE/src/org/waterforpeople/mapping/analytics/dao/SurveyQuestionSummaryDao.java | |
+++ b/GAE/src/org/waterforpeople/mapping/analytics/dao/SurveyQuestionSummaryDao.java | |
@@ -38,7 +38,7 @@ import com.gallatinsystems.framework.servlet.PersistenceFilter; | |
/** | |
* updates survey question objects | |
- * | |
+ * | |
* @author Christopher Fagiani | |
*/ | |
public class SurveyQuestionSummaryDao extends BaseDAO<SurveyQuestionSummary> { | |
@@ -54,7 +54,7 @@ public class SurveyQuestionSummaryDao extends BaseDAO<SurveyQuestionSummary> { | |
* synchronized static method so that only 1 thread can be updating a summary at a time. This is | |
* inefficient but is the only way we can be sure we're keeping the count consistent since there | |
* is no "select for update" or sql dml-like construct | |
- * | |
+ * | |
* @param answer | |
*/ | |
@SuppressWarnings("rawtypes") | |
@@ -114,7 +114,7 @@ public class SurveyQuestionSummaryDao extends BaseDAO<SurveyQuestionSummary> { | |
/** | |
* Retrieve the survey question summary object by response. If possible retrieve from cache | |
- * | |
+ * | |
* @param questionId | |
* @param questionResponse | |
* @return | |
@@ -153,7 +153,7 @@ public class SurveyQuestionSummaryDao extends BaseDAO<SurveyQuestionSummary> { | |
* Add a collection of SurveyQuestionSummary objects to the cache. If the object already exists | |
* in the cached SurveyQuestionSummarys list, they are replaced by the ones passed in through | |
* this list | |
- * | |
+ * | |
* @param summaryList | |
*/ | |
private void cache(List<SurveyQuestionSummary> summaryList) { | |
@@ -163,7 +163,7 @@ public class SurveyQuestionSummaryDao extends BaseDAO<SurveyQuestionSummary> { | |
Map<Object, Object> cacheMap = new HashMap<Object, Object>(); | |
for (SurveyQuestionSummary summary : summaryList) { | |
- String cacheKey = getCacheKey(Long.toString(summary.getKey().getId()) + "-" | |
+ String cacheKey = getCacheKey(summary.getKey().getId() + "-" | |
+ summary.getResponse()); | |
cacheMap.put(cacheKey, summary); | |
} | |
@@ -173,7 +173,7 @@ public class SurveyQuestionSummaryDao extends BaseDAO<SurveyQuestionSummary> { | |
/** | |
* Remove a collection of SurveyQuestionSummarys from the cache | |
- * | |
+ * | |
* @param summaryList | |
*/ | |
private void uncache(List<SurveyQuestionSummary> summaryList) { | |
@@ -182,7 +182,7 @@ public class SurveyQuestionSummaryDao extends BaseDAO<SurveyQuestionSummary> { | |
} | |
for (SurveyQuestionSummary summary : summaryList) { | |
- String cacheKey = getCacheKey(Long.toString(summary.getKey().getId()) + "-" | |
+ String cacheKey = getCacheKey(summary.getKey().getId() + "-" | |
+ summary.getResponse()); | |
if (containsKey(cache, cacheKey)) { | |
cache.remove(cacheKey); | |
@@ -192,7 +192,7 @@ public class SurveyQuestionSummaryDao extends BaseDAO<SurveyQuestionSummary> { | |
/** | |
* Save and cache question summary | |
- * | |
+ * | |
* @param summary | |
*/ | |
public SurveyQuestionSummary save(SurveyQuestionSummary summary) { | |
@@ -203,7 +203,7 @@ public class SurveyQuestionSummaryDao extends BaseDAO<SurveyQuestionSummary> { | |
/** | |
* Save and cache question summary list | |
- * | |
+ * | |
* @param summary | |
*/ | |
public List<SurveyQuestionSummary> save(List<SurveyQuestionSummary> summary) { | |
@@ -215,7 +215,7 @@ public class SurveyQuestionSummaryDao extends BaseDAO<SurveyQuestionSummary> { | |
/** | |
* Delete from cache and datastore | |
- * | |
+ * | |
* @param summary | |
*/ | |
public void delete(SurveyQuestionSummary summary) { | |
@@ -225,7 +225,7 @@ public class SurveyQuestionSummaryDao extends BaseDAO<SurveyQuestionSummary> { | |
/** | |
* Delete summary list from cache and datastore | |
- * | |
+ * | |
* @param summaryList | |
*/ | |
public void delete(List<SurveyQuestionSummary> summaryList) { | |
diff --git a/GAE/src/org/waterforpeople/mapping/app/web/TestHarnessServlet.java b/GAE/src/org/waterforpeople/mapping/app/web/TestHarnessServlet.java | |
index 423fef4..46fb658 100644 | |
--- a/GAE/src/org/waterforpeople/mapping/app/web/TestHarnessServlet.java | |
+++ b/GAE/src/org/waterforpeople/mapping/app/web/TestHarnessServlet.java | |
@@ -20,6 +20,7 @@ import static com.gallatinsystems.common.util.MemCacheUtils.initCache; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
+import java.io.Writer; | |
import java.util.List; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
@@ -454,6 +455,20 @@ public class TestHarnessServlet extends HttpServlet { | |
e.printStackTrace(); | |
} | |
+ } else if ("testCacheKey".equals(action)) { | |
+ try { | |
+ Writer w = resp.getWriter(); | |
+ SurveyDAO dao = new SurveyDAO(); | |
+ for (Survey s : dao.list(null)) { | |
+ w.write(s.getCacheKey()); | |
+ w.write("\n"); | |
+ } | |
+ w.flush(); | |
+ w.close(); | |
+ } catch (IOException e) { | |
+ e.printStackTrace(); | |
+ } | |
+ | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment