package org.d214.whs.wcc.app;

import java.io.*;
import java.util.Calendar;

public class Cache
{
    private Cache() {}
    
    private static String upcomingEvents, topNews, dailyAnnouncements;
    private static Calendar lastUpdated;
    
    //Used for accessing WheelingApp's non-static getJson() methods.
    private final static WheelingApp wa = new WheelingApp();
    
    //Tests whether the current get request time is already the same date as
    //the last time the cache was updated
    //Tests whether the DAY is equal, not hours/minutes/seconds
    public static boolean sameDate()
    {
        if (lastUpdated == null)
            return false;
        Calendar c = Calendar.getInstance();
        if (c.get(Calendar.YEAR) == lastUpdated.get(Calendar.YEAR))
            return (c.get(Calendar.DAY_OF_YEAR) ==
                lastUpdated.get(Calendar.DAY_OF_YEAR));
        else
            return false;
    }
    
    //Updates the String values.
    private static void update()
    {
        upcomingEvents = wa.getUpcomingEventsJson();
        topNews = wa.getTopNewsJson();
        dailyAnnouncements = wa.getDailyAnnouncementsJson();
        lastUpdated = Calendar.getInstance();
    }
    
    public static String getUpcomingEvents()
    {
        if (sameDate())
        {
            if (upcomingEvents != null)
                return upcomingEvents;
            else
                return wa.getUpcomingEventsJson();
        }
        else
        {
            update();
            return upcomingEvents;
        }
    }
    
    public static String getTopNews()
    {
        if (sameDate())
        {
            if (topNews != null)
                return topNews;
            else
                return wa.getTopNewsJson();
        }
        else
        {
            update();
            return topNews;
        }
    }
    
    public static String getDailyAnnouncements()
    {
        if (sameDate())
        {
            if (dailyAnnouncements != null)
                return dailyAnnouncements;
            else
                return wa.getDailyAnnouncementsJson();
        }
        else
        {
            update();
            return dailyAnnouncements;
        }
    }
    
    
    
    /*      Persistence section     */
    
    //Write cache to file.
    public static void writeCache()
    {
        try
        {
            File out = new File("whs_app_cache");
            FileOutputStream fos = new FileOutputStream(out);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            
            //write values
            oos.writeObject(upcomingEvents);
            oos.writeObject(topNews);
            oos.writeObject(dailyAnnouncements);
            oos.writeObject(lastUpdated); //yes, Calendar is serializable
            
            oos.close();
            fos.close();
        }
        catch (IOException exc)
        {
            System.err.println(exc);
        }
    }
    
    //returns boolean because true if there was a cache file to read from,
    //false if there wasn't a cache file
    public static boolean readCache()
    {
        try
        {
            File in = new File("whs_app_cache");
            if (!in.exists())
                return false;
            
            FileInputStream fis = new FileInputStream(in);
            ObjectInputStream ois = new ObjectInputStream(fis);
            
            upcomingEvents = (String) ois.readObject();
            topNews = (String) ois.readObject();
            dailyAnnouncements = (String) ois.readObject();
            lastUpdated = (Calendar) ois.readObject();
            
            ois.close();
            fis.close();
            
            return true;
        }
        catch (Exception exc)
        {
            System.err.println(exc);
            return false;
        }
    }
}
@@ -28,26 +28,8 @@
 p;%0A%0A
-import java.io.*;%0A
 impo
@@ -1237,36 +1237,33 @@
 nts()%0A    %7B%0A    
-    
+%09
 if (sameDate())%0A
@@ -1264,34 +1264,11 @@
 te()
-)%0A        %7B%0A            if
+ &&
  (up
@@ -1288,33 +1288,24 @@
 != null)
+)
 %0A    
-            
+%09%09
 return u
@@ -1327,90 +1327,9 @@
     
-        else%0A                return wa.getUpcomingEventsJson();%0A        %7D%0A        
+%09
 else
@@ -1333,34 +1333,25 @@
 lse%0A    
-  
-  
+%09
 %7B%0A    
-        
+%09%09
 update()
@@ -1352,32 +1352,26 @@
 date();%0A    
-        
+%09%09
 return upcom
@@ -1377,36 +1377,33 @@
 mingEvents;%0A    
-    
+%09
 %7D%0A    %7D%0A    %0A   
@@ -1439,36 +1439,33 @@
 ews()%0A    %7B%0A    
-    
+%09
 if (sameDate())%0A
@@ -1466,34 +1466,11 @@
 te()
-)%0A        %7B%0A            if
+ &&
  (to
@@ -1483,33 +1483,24 @@
 != null)
+)
 %0A    
-            
+%09%09
 return t
@@ -1515,83 +1515,9 @@
     
-        else%0A                return wa.getTopNewsJson();%0A        %7D%0A        
+%09
 else
@@ -1521,34 +1521,25 @@
 lse%0A    
-    
+%09
 %7B%0A    
- 
-       
+%09%09
 update()
@@ -1540,32 +1540,26 @@
 date();%0A    
-        
+%09%09
 return topNe
@@ -1562,28 +1562,25 @@
 opNews;%0A    
-    
+%09
 %7D%0A    %7D%0A    
@@ -1635,28 +1635,25 @@
 )%0A    %7B%0A    
-    
+%09
 if (sameDate
@@ -1658,34 +1658,11 @@
 te()
-)%0A        %7B%0A            if
+ &&
  (da
@@ -1690,713 +1690,23 @@
 ull)
-%0A                return dailyAnnouncements;%0A            else%0A                return wa.getDailyAnnouncementsJson();%0A        %7D%0A        else%0A        %7B%0A            update();%0A            return dailyAnnouncements;%0A        %7D%0A    %7D%0A    %0A    %0A    %0A    /*      Persistence section     */%0A    %0A    //Write cache to file.%0A    public static void writeCache()%0A    %7B%0A        try%0A        %7B%0A            File out = new File(%22whs_app_cache%22);%0A            FileOutputStream fos = new FileOutputStream(out);%0A            ObjectOutputStream oos = new ObjectOutputStream(fos);%0A            %0A            //write values%0A            oos.writeObject(upcomingEvents);%0A            oos.writeObject(topNews);%0A            oos.writeObject(
+)%0A    %09%09return 
 dail
@@ -1723,1140 +1723,81 @@
 ents
-)
 ;%0A    
-        oos.writeObject(lastUpdated); //yes, Calendar is serializable%0A            %0A            oos.close();%0A            fos.close();%0A        %7D%0A        catch (IOException exc)%0A        %7B%0A            System.err.println(exc);%0A        %7D%0A    %7D%0A    %0A    //returns boolean because true if there was a cache file to read from,%0A    //false if there wasn't a cache file%0A    public static boolean readCache()%0A    %7B%0A        try%0A        %7B%0A            File in = new File(%22whs_app_cache%22);%0A            if (!in.exists())%0A                return false;%0A            %0A            FileInputStream fis = new FileInputStream(in);%0A            ObjectInputStream ois = new ObjectInputStream(fis);%0A            %0A            upcomingEvents = (String) ois.readObject();%0A            topNews = (String) ois.readObject();%0A            dailyAnnouncements = (String) ois.readObject();%0A            lastUpdated = (Calendar) ois.readObject();%0A            %0A            ois.close();%0A            fis.close();%0A            %0A            return true;%0A        %7D%0A        catch (Exception exc)%0A        %7B%0A            System.err.println(exc);%0A            return false;%0A        
+%09else%0A    %09%7B%0A    %09%09update();%0A    %09%09return dailyAnnouncements;%0A    %09
 %7D%0A  

Update on caching code