[from now] 2012/05/30 17:36:32

diff --git a/src/main/java/org/ryu22e/nico2cal/service/CalendarService.java b/src/main/java/org/ryu22e/nico2cal/service/CalendarService.java
index 6ab134a..a442760 100644
--- a/src/main/java/org/ryu22e/nico2cal/service/CalendarService.java
+++ b/src/main/java/org/ryu22e/nico2cal/service/CalendarService.java
@@ -114,6 +114,29 @@ public class CalendarService {
     }

     /**
+     * キーワードに該当する{@link Nicolive}のキーを取得する。
+     * @param keywords キーワード
+     * @return キーワードに該当する{@link Nicolive}のキー
+     */
+    private List<Key> getKeywordKeys(List<String> keywords) {
+        NicoliveIndexMeta ni = NicoliveIndexMeta.get();
+        List<Key> keywordKeys = new LinkedList<Key>();
+        for (String keyword : keywords) {
+            List<NicoliveIndex> indexes =
+                    Datastore
+                        .query(ni)
+                        .filter(ni.keyword.equal(keyword))
+                        .asList();
+            List<Key> keys = new LinkedList<Key>();
+            for (NicoliveIndex nicoliveIndex : indexes) {
+                keys.add(nicoliveIndex.getNicoliveKey());
+            }
+            keywordKeys = merge(keys, keywordKeys);
+        }
+        return keywordKeys;
+    }
+
+    /**
      * @param userId
      * @return
      */
@@ -152,8 +175,6 @@ public class CalendarService {
         calendar.getProperties().add(new XProperty("X-WR-CALNAME", CALNAME));

         NicoliveMeta n = NicoliveMeta.get();
-        NicoliveIndexMeta ni = NicoliveIndexMeta.get();
-
         ModelQuery<Nicolive> query =
                 Datastore
                     .query(n)
@@ -163,19 +184,7 @@ public class CalendarService {

         if (condition.getKeywords() != null
                 && 0 < condition.getKeywords().size()) {
-            List<Key> keywordKeys = new LinkedList<Key>();
-            for (String keyword : condition.getKeywords()) {
-                List<NicoliveIndex> indexes =
-                        Datastore
-                            .query(ni)
-                            .filter(ni.keyword.equal(keyword))
-                            .asList();
-                List<Key> keys = new LinkedList<Key>();
-                for (NicoliveIndex nicoliveIndex : indexes) {
-                    keys.add(nicoliveIndex.getNicoliveKey());
-                }
-                keywordKeys = merge(keys, keywordKeys);
-            }
+            List<Key> keywordKeys = getKeywordKeys(condition.getKeywords());
             if (0 < keywordKeys.size()) {
                 query = query.filterInMemory(n.key.in(keywordKeys));
             } else {
@@ -370,11 +379,20 @@ public class CalendarService {
             MyCalendarLogMeta ml = MyCalendarLogMeta.get();
             org.joda.time.DateTime d = new org.joda.time.DateTime();
             d = d.minusWeeks(1);
-            List<Nicolive> nicolives =
-                    Datastore
-                        .query(n)
-                        .filter(n.openTime.greaterThanOrEqual(d.toDate()))
-                        .asList();
+            ModelQuery<Nicolive> query =
+                    Datastore.query(n).filter(
+                        n.openTime.greaterThanOrEqual(d.toDate()));
+            if (0 < myCalendar.getKeywords().size()) {
+                List<Key> keywordKeys =
+                        getKeywordKeys(myCalendar.getKeywords());
+                if (0 < keywordKeys.size()) {
+                    query = query.filterInMemory(n.key.in(keywordKeys));
+                } else {
+                    // キーワード検索で該当するエンティティがなければ、この後のクエリを発行する必要がないので、ここで検索を終了とする。
+                    return;
+                }
+            }
+            List<Nicolive> nicolives = query.asList();
             for (Nicolive nicolive : nicolives) {
                 int count =
                         Datastore