[chronojump/optimizeRFD] New products update/download if version of a news changed or image changed



commit 88c2cc76367999f6e637a0ce50fe33e5549acb44
Author: Xavier de Blas <xaviblas gmail com>
Date:   Fri Jan 1 19:11:36 2021 +0100

    New products update/download if version of a news changed or image changed

 src/gui/app1/chronojump.cs |  4 ++--
 src/json/json.cs           | 25 +++++++++++++++++++------
 src/news.cs                | 41 +++++++++++++++++++++++++++++++++++++----
 src/sqlite/news.cs         | 12 ++++++++++++
 4 files changed, 70 insertions(+), 12 deletions(-)
---
diff --git a/src/gui/app1/chronojump.cs b/src/gui/app1/chronojump.cs
index f65d772ac..2bded4d24 100644
--- a/src/gui/app1/chronojump.cs
+++ b/src/gui/app1/chronojump.cs
@@ -6788,7 +6788,7 @@ LogB.Debug("mc finished 5");
                if(! pingThread.IsAlive)
                {
                        // 1) Insert if needed
-                       if(newsAtServer_l != null && News.InsertIfNeeded (newsAtDB_l, newsAtServer_l))
+                       if(newsAtServer_l != null && News.InsertOrUpdateIfNeeded (newsAtDB_l, newsAtServer_l))
                        {
                                Pixbuf pixbuf = new Pixbuf (null, Util.GetImagePath(false) + 
"image_store_has_new_products.png");
                                image_menu_news.Pixbuf = pixbuf;
@@ -6842,7 +6842,7 @@ LogB.Debug("mc finished 5");
        private void getNews()
        {
                LogB.Information("getNews()");
-               newsAtServer_l = jsPing.GetNews();
+               newsAtServer_l = jsPing.GetNews(newsAtDB_l); //send the local list to know if images have to 
be re-downloaded on a version update
        }
 
        private void on_preferences_debug_mode_start (object o, EventArgs args) {
diff --git a/src/json/json.cs b/src/json/json.cs
index 87e1ac55e..ee1acd4be 100644
--- a/src/json/json.cs
+++ b/src/json/json.cs
@@ -186,7 +186,7 @@ public class Json
 
        //get all the news, news class will decide if something have to be inserted or selected
        //called by pingThread at start
-       public List<News> GetNews()
+       public List<News> GetNews(List<News> newsAtDB_l)
        {
                // Create a request using a URL that can receive a post.
                if (! createWebRequest(requestType.GENERIC, "/getNews"))
@@ -210,9 +210,9 @@ public class Json
 
                this.ResultMessage = responseFromServer;
 
-               return newsDeserialize(responseFromServer);
+               return newsDeserialize(responseFromServer, newsAtDB_l);
        }
-       private List<News> newsDeserialize(string str)
+       private List<News> newsDeserialize(string str, List<News> newsAtDB_l)
        {
                LogB.Information("newsDeserialize:|" + str + "|");
 
@@ -233,8 +233,9 @@ public class Json
                        string descriptionEs = jsonNews ["descriptionEs"];
                        string linkServerImage = jsonNews ["linkServerImage"];
 
-                       news_l.Add(new News(code, category, version, false,
-                                               titleEn, titleEs, linkEn, linkEs, descriptionEn, 
descriptionEs, linkServerImage));
+                       News newsAtJson = new News(code, category, version, false,
+                                               titleEn, titleEs, linkEn, linkEs, descriptionEn, 
descriptionEs, linkServerImage);
+                       news_l.Add(newsAtJson);
 
                        // 2) download image
                        //if image does not exist, download here (in pingThread)
@@ -246,7 +247,19 @@ public class Json
                                extension = ".png";
 
                        string copyTo = Path.Combine(News.GetNewsDir(), code.ToString() + extension);
-                       if(! File.Exists(copyTo))
+
+                       //download the image if (1 version has changed OR 2 linkServerImage has changed OR 3 
image does not exists locally)
+                       bool needToDownloadImage = false;
+                       foreach(News newsAtDB in newsAtDB_l)
+                               if( newsAtJson.Code == newsAtDB.Code &&
+                                               (newsAtJson.Version > newsAtDB.Version ||                     
  // 1
+                                                newsAtJson.LinkServerImage != newsAtDB.LinkServerImage)      
  // 2
+                                               )
+                                       needToDownloadImage = true;
+
+                       if(needToDownloadImage ||
+                                       ! File.Exists(copyTo)                                                 
  // 3
+                                       )
                                downloadNewsImage(linkServerImage, copyTo);
                }
 
diff --git a/src/news.cs b/src/news.cs
index 6aff19026..79b147449 100644
--- a/src/news.cs
+++ b/src/news.cs
@@ -77,6 +77,11 @@ public class News
                return SqliteNews.Insert(dbconOpened, toSQLInsertString());
        }
 
+       public void UpdateSQL(bool dbconOpened)
+       {
+               SqliteNews.Update(dbconOpened, toSQLUpdateString());
+       }
+
        public string GetTitle(bool es)
        {
                if(es)
@@ -105,19 +110,25 @@ public class News
        /* public static methods */
 
        //this method uses SQL, called by main thread
-       public static bool InsertIfNeeded(List<News> newsAtDB_l, List<News> newsAtServer_l)
+       public static bool InsertOrUpdateIfNeeded(List<News> newsAtDB_l, List<News> newsAtServer_l)
        {
                bool newStuff = false;
                foreach(News nAtServer in newsAtServer_l)
                {
-                       bool found = false;
+                       bool existsLocally = false;
                        foreach(News nAtDB in newsAtDB_l)
                                if(nAtServer.Code == nAtDB.Code)
                                {
-                                       found = true;
+                                       existsLocally = true;
+                                       if(nAtServer.Version > nAtDB.Version)
+                                       {
+                                               nAtServer.UpdateSQL(false);
+                                               newStuff = true;
+                                       }
+
                                        break;
                                }
-                       if(! found)
+                       if(! existsLocally)
                        {
                                nAtServer.InsertSQL(false);
                                newStuff = true;
@@ -157,11 +168,33 @@ public class News
                        linkServerImage + "\"";
        }
 
+       private string toSQLUpdateString()
+       {
+               return
+                       " code = " + code +
+                       ", category = " + category +
+                       ", version = " + version +
+                       ", viewed = \"False\"" +
+                       ", titleEn = \"" + titleEn +
+                       "\", titleEs = \"" + titleEs +
+                       "\", linkEn = \"" + linkEn +
+                       "\", linkEs = \"" + linkEs +
+                       "\", descriptionEn = \"" + descriptionEn +
+                       "\", descriptionEs = \"" + descriptionEs +
+                       "\", linkServerImage = \"" + linkServerImage +
+                       "\" WHERE code = " + code;
+       }
+
        public int Code
        {
                get { return code; }
        }
 
+       public int Version
+       {
+               get { return version; }
+       }
+
        public string LinkServerImage
        {
                get { return linkServerImage; }
diff --git a/src/sqlite/news.cs b/src/sqlite/news.cs
index c8587a531..980d637c3 100644
--- a/src/sqlite/news.cs
+++ b/src/sqlite/news.cs
@@ -82,6 +82,18 @@ class SqliteNews : Sqlite
                return myLast;
        }
 
+       public static void Update (bool dbconOpened, string updateString)
+       {
+               openIfNeeded(dbconOpened);
+
+               dbcmd.CommandText = "UPDATE " + table + " SET " + updateString;
+
+               LogB.SQL(dbcmd.CommandText.ToString());
+               dbcmd.ExecuteNonQuery();
+
+               closeIfNeeded(dbconOpened);
+       }
+
        //code -1 (select all)
        //limit is 10 by default on show news
        public static List<News> Select (bool dbconOpened, int code, int limit)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]