[from now] 2012/06/01 17:21:01

diff --git a/src/main/java/org/ryu22e/nico2cal/model/MyCalendarLog.java b/src/main/java/org/ryu22e/nico2cal/model/MyCalendarLog.java
index e2b2883..8f3a1ea 100644
--- a/src/main/java/org/ryu22e/nico2cal/model/MyCalendarLog.java
+++ b/src/main/java/org/ryu22e/nico2cal/model/MyCalendarLog.java
@@ -1,10 +1,10 @@
 package org.ryu22e.nico2cal.model;

 import java.io.Serializable;
+import java.util.List;

 import org.slim3.datastore.Attribute;
 import org.slim3.datastore.Model;
-import org.slim3.datastore.ModelRef;

 import com.google.appengine.api.datastore.Key;
 import com.google.appengine.api.users.User;
@@ -46,8 +46,7 @@ public final class MyCalendarLog implements Serializable {
     /**
      *
      */
-    private ModelRef<Nicolive> nicolive =
-            new ModelRef<Nicolive>(Nicolive.class);
+    private List<Key> nicoliveKeys;

     /**
      * Returns the key.
@@ -115,8 +114,18 @@ public final class MyCalendarLog implements Serializable {
         this.calendarId = calendarId;
     }

-    public ModelRef<Nicolive> getNicolive() {
-        return nicolive;
+    /**
+     * @return the nicoliveKeys
+     */
+    public List<Key> getNicoliveKeys() {
+        return nicoliveKeys;
+    }
+
+    /**
+     * @param nicoliveKeys the nicoliveKeys to set
+     */
+    public void setNicoliveKeys(List<Key> nicoliveKeys) {
+        this.nicoliveKeys = nicoliveKeys;
     }

     @Override
diff --git a/src/main/java/org/ryu22e/nico2cal/service/CalendarService.java b/src/main/java/org/ryu22e/nico2cal/service/CalendarService.java
index de68520..2d6a92c 100644
--- a/src/main/java/org/ryu22e/nico2cal/service/CalendarService.java
+++ b/src/main/java/org/ryu22e/nico2cal/service/CalendarService.java
@@ -395,7 +395,6 @@ public class CalendarService {
                 createGoogleCalendarClient(myCalendar.getUser());
         if (client != null) {
             NicoliveMeta n = NicoliveMeta.get();
-            MyCalendarLogMeta ml = MyCalendarLogMeta.get();
             org.joda.time.DateTime d = new org.joda.time.DateTime();
             d = d.minusWeeks(1);
             ModelQuery<Nicolive> query =
@@ -411,17 +410,26 @@ public class CalendarService {
                     return;
                 }
             }
+            MyCalendarLogMeta ml = MyCalendarLogMeta.get();
+            MyCalendarLog myCalendarLog =
+                    Datastore
+                        .query(ml)
+                        .filter(
+                            ml.user.equal(myCalendar.getUser()),
+                            ml.calendarId.equal(myCalendar.getCalendarId()))
+                        .asSingle();
+            if (myCalendarLog == null) {
+                myCalendarLog = new MyCalendarLog();
+                myCalendarLog.setUser(myCalendar.getUser());
+                myCalendarLog.setCalendarId(myCalendar.getCalendarId());
+                myCalendarLog.setNicoliveKeys(new ArrayList<Key>());
+            }
+            List<Key> importedNicoliveKeys = new LinkedList<Key>();
             List<Nicolive> nicolives = query.asList();
             for (Nicolive nicolive : nicolives) {
-                int count =
-                        Datastore
-                            .query(ml)
-                            .filter(
-                                ml.user.equal(myCalendar.getUser()),
-                                ml.calendarId.equal(myCalendar.getCalendarId()),
-                                ml.nicolive.equal(nicolive.getKey()))
-                            .count();
-                if (count <= 0) {
+                if (!myCalendarLog
+                    .getNicoliveKeys()
+                    .contains(nicolive.getKey())) {
                     Event event = new Event();
                     // カレンダーのSummaryに生放送のタイトルを設定する。
                     event.setSummary(nicolive.getTitle());
@@ -456,20 +464,34 @@ public class CalendarService {
                     event.setEnd(end);

                     if (AppEngineUtil.isProduction()) {
-                        client
-                            .events()
-                            .insert(myCalendar.getCalendarId(), event)
-                            .execute();
+                        try {
+                            client
+                                .events()
+                                .insert(myCalendar.getCalendarId(), event)
+                                .execute();
+                        } catch (IOException e) {
+                            putMyCalendarLog(myCalendarLog, importedNicoliveKeys);
+                            throw e;
+                        }
                     }

-                    // インポートしたNicoliveをmyCalendarLogに登録して、二度とインポートしないようにする。
-                    MyCalendarLog myCalendarLog = new MyCalendarLog();
-                    myCalendarLog.setUser(myCalendar.getUser());
-                    myCalendarLog.setCalendarId(myCalendar.getCalendarId());
-                    myCalendarLog.getNicolive().setModel(nicolive);
-                    Datastore.putAsync(myCalendarLog);
+                    importedNicoliveKeys.add(nicolive.getKey());
                 }
             }
+            putMyCalendarLog(myCalendarLog, importedNicoliveKeys);
+        }
+    }
+
+    /**
+     * @param myCalendarLog
+     * @param importedNicoliveKeys
+     */
+    private void putMyCalendarLog(MyCalendarLog myCalendarLog,
+            List<Key> importedNicoliveKeys) {
+        if (0 < importedNicoliveKeys.size()) {
+            // インポートしたNicoliveをmyCalendarLogに登録して、二度とインポートしないようにする。
+            myCalendarLog.getNicoliveKeys().addAll(importedNicoliveKeys);
+            Datastore.putAsync(myCalendarLog);
         }
     }
 }
diff --git a/src/test/java/org/ryu22e/nico2cal/service/CalendarServiceTest.java b/src/test/java/org/ryu22e/nico2cal/service/CalendarServiceTest.java
index c2c2a05..da1539f 100644
--- a/src/test/java/org/ryu22e/nico2cal/service/CalendarServiceTest.java
+++ b/src/test/java/org/ryu22e/nico2cal/service/CalendarServiceTest.java
@@ -645,19 +645,14 @@ public final class CalendarServiceTest extends AppEngineTestCase {
         mockService.importToMyCalendar(myCalendarKey);

         MyCalendarLogMeta ml = MyCalendarLogMeta.get();
-        List<MyCalendarLog> myCalendarLogs =
+        MyCalendarLog myCalendarLog =
                 Datastore
                     .query(ml)
                     .filter(
                         ml.user.equal(user),
                         ml.calendarId.equal("calendar1"))
-                    .asList();
-        assertThat(myCalendarLogs, is(notNullValue()));
-        assertThat(myCalendarLogs.size(), is(10));
-        List<Key> storedKeys = new ArrayList<Key>();
-        for (MyCalendarLog myCalendarLog : myCalendarLogs) {
-            storedKeys.add(myCalendarLog.getNicolive().getKey());
-        }
-        assertThat(storedKeys, is(testDataKeys));
+                    .asSingle();
+        assertThat(myCalendarLog, is(notNullValue()));
+        assertThat(myCalendarLog.getNicoliveKeys(), is(testDataKeys));
     }
 }