OSDN Git Service

Add the ability to add & remove notebook stacks.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / sql / NotebookTable.java
index 41379db..e528364 100644 (file)
@@ -70,7 +70,41 @@ public class NotebookTable {
         newnote.setName("My Notebook");\r
         newnote.setPublished(false);\r
         newnote.setGuid("1");\r
-        addNotebook(newnote, true, false);\r
+        \r
+        // Setup an initial notebook\r
+               SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");\r
+        query = new NSqlQuery(db.getConnection());\r
+               query.prepare("Insert Into Notebook (guid, sequence, name, defaultNotebook, "\r
+                               +"serviceCreated, serviceUpdated, published, "   \r
+                               + "isDirty, autoEncrypt, " \r
+                               + "local, archived) Values("\r
+                               +":guid, :sequence, :name, :defaultNotebook,  "\r
+                               +":serviceCreated, :serviceUpdated, :published, "\r
+                               +":isDirty, :autoEncrypt, "\r
+                               +":local, false)");\r
+               query.bindValue(":guid", newnote.getGuid());\r
+               query.bindValue(":sequence", newnote.getUpdateSequenceNum());\r
+               query.bindValue(":name", newnote.getName());\r
+               query.bindValue(":defaultNotebook", newnote.isDefaultNotebook());\r
+               \r
+               StringBuilder serviceCreated = new StringBuilder(simple.format(newnote.getServiceCreated()));                   \r
+               StringBuilder serviceUpdated = new StringBuilder(simple.format(newnote.getServiceUpdated()));\r
+               if (serviceUpdated.toString() == null)\r
+                       serviceUpdated = serviceCreated;\r
+               query.bindValue(":serviceCreated", serviceCreated.toString());\r
+               query.bindValue(":serviceUpdated", serviceCreated.toString());\r
+               query.bindValue(":published",newnote.isPublished());\r
+               \r
+               query.bindValue(":isDirty", true);\r
+               query.bindValue(":autoEncrypt", false);\r
+               query.bindValue(":local", false);\r
+\r
+               boolean check = query.exec();\r
+               if (!check) {\r
+                       logger.log(logger.MEDIUM, "Initial Notebook Table insert failed.");\r
+                       logger.log(logger.MEDIUM, query.lastError().toString());\r
+               }\r
+\r
                \r
        }\r
        // Drop the table\r
@@ -86,12 +120,12 @@ public class NotebookTable {
         NSqlQuery query = new NSqlQuery(db.getConnection());\r
                check = query.prepare("Insert Into Notebook (guid, sequence, name, defaultNotebook, "\r
                                +"serviceCreated, serviceUpdated, published, "   \r
-                               + "isDirty, autoEncrypt, stack" \r
+                               + "isDirty, autoEncrypt, stack\r
                                + "local, archived) Values("\r
                                +":guid, :sequence, :name, :defaultNotebook,  "\r
                                +":serviceCreated, :serviceUpdated, :published, "\r
                                +":isDirty, :autoEncrypt, "\r
-                               +":local, false, :stack)");\r
+                               +":stack, :local, false)");\r
                query.bindValue(":guid", tempNotebook.getGuid());\r
                query.bindValue(":sequence", tempNotebook.getUpdateSequenceNum());\r
                query.bindValue(":name", tempNotebook.getName());\r
@@ -349,7 +383,7 @@ public class NotebookTable {
         NSqlQuery query = new NSqlQuery(db.getConnection());\r
                                        \r
                check = query.exec("Select guid, sequence, name, defaultNotebook, " +\r
-                               "serviceCreated, serviceUpdated, published from Notebook where isDirty = true and local=false");\r
+                               "serviceCreated, serviceUpdated, published, stack from Notebook where isDirty = true and local=false");\r
                if (!check) \r
                        logger.log(logger.EXTREME, "Notebook SQL retrieve has failed.");\r
                while (query.next()) {\r
@@ -368,7 +402,8 @@ public class NotebookTable {
                                e.printStackTrace();\r
                        }\r
                        tempNotebook.setPublished(new Boolean(query.valueString(6)));\r
-                       index.add(tempNotebook); \r
+                       tempNotebook.setStack(query.valueString(7));\r
+                       index.add(tempNotebook);\r
                }       \r
                return index;   \r
        }\r
@@ -509,5 +544,68 @@ public class NotebookTable {
                return counts;\r
        }\r
 \r
+       // Get/Set stacks\r
+       public void clearStack(String guid) {\r
+               NSqlQuery query = new NSqlQuery(db.getConnection());\r
+               \r
+               query.prepare("Update notebook set stack='' where guid=:guid");\r
+               query.bindValue(":guid", guid);\r
+               if (!query.exec())\r
+                       logger.log(logger.EXTREME, "Error clearing notebook stack.");\r
+       }\r
+       // Get/Set stacks\r
+       public void setStack(String guid, String stack) {\r
+               NSqlQuery query = new NSqlQuery(db.getConnection());\r
+               \r
+               query.prepare("Update notebook set stack=:stack, isDirty=true where guid=:guid");\r
+               query.bindValue(":guid", guid);\r
+               query.bindValue(":stack", stack);\r
+               if (!query.exec())\r
+                       logger.log(logger.EXTREME, "Error setting notebook stack.");\r
+       }\r
+       // Get all stack names\r
+       public List<String> getAllStackNames() {\r
+               List<String> stacks = new ArrayList<String>();\r
+               NSqlQuery query = new NSqlQuery(db.getConnection());\r
+               \r
+               if (!query.exec("Select distinct stack from notebook")) {\r
+                       logger.log(logger.EXTREME, "Error getting all stack names.");\r
+                       return null;\r
+               }\r
+               \r
+               while (query.next()) {\r
+                       if (query.valueString(0) != null && !query.valueString(0).trim().equals(""))\r
+                               stacks.add(query.valueString(0));\r
+               }\r
+               return stacks;\r
+       }\r
+       // Rename a stack\r
+       public void renameStacks(String oldName, String newName) {\r
+               NSqlQuery query = new NSqlQuery(db.getConnection());\r
+               \r
+               if (!query.prepare("update notebook set stack=:newName where stack=:oldName")) {\r
+                       logger.log(logger.EXTREME, "Error preparing in renameStacks.");\r
+                       return;\r
+               }\r
+               query.bindValue(":oldName", oldName);\r
+               query.bindValue(":newName", newName);\r
+               if (!query.exec()) {\r
+                       logger.log(logger.EXTREME, "Error updating stack names");\r
+                       return;\r
+               }\r
+       }\r
+       // Get/Set stacks\r
+       public boolean stackExists(String stack) {\r
+               NSqlQuery query = new NSqlQuery(db.getConnection());\r
+               \r
+               query.prepare("Select guid from notebook where stack=:stack limit 1");\r
+               query.bindValue(":stack", stack);\r
+               if (!query.exec())\r
+                       logger.log(logger.EXTREME, "Error setting notebook stack.");\r
+               if (query.next())\r
+                       return true;\r
+               else\r
+                       return false;\r
+       }\r
 }\r
 \r