package controllers;

import models.*;

import play.cache.Cache;

public abstract class CachedPage {
    public static final String ATTENDANCE_INDEX = "Attendance-index-";
    public static final String JC_INDEX = "Application-index-";
    public static final String MANUAL_INDEX = "Application-viewManual-";
    public static final String RECENT_COMMENTS = "CRM-recentComments-";

    public String title;
    public String menu;
    public String selected_button;
    public String cache_key;

    static String getKey(String key_base) {
        return key_base + "-" + OrgConfig.get().org.id;
    }

    public static void remove(String key_base) {
        Cache.remove(getKey(key_base));
    }

    public CachedPage(String key_base, String title, String menu,
        String selected_button) {
        this.title = title;
        this.cache_key = getKey(key_base);
        this.menu = menu;
        this.selected_button = selected_button;
    }

    public String getPage() {
        String result = (String)Cache.get(cache_key);
        if (result != null) {
            return result;
        }

        result = render();
        // cache it for 12 hours
        Cache.set(cache_key, result, 60 * 60 * 12);
        return result;
    }

    abstract String render();

    public static void clearAll() {
        remove(ATTENDANCE_INDEX);
        remove(JC_INDEX);
        remove(MANUAL_INDEX);
        remove(RECENT_COMMENTS);
        Utils.updateCustodia();
    }

    public static void onPeopleChanged() {
        remove(ATTENDANCE_INDEX);
        remove(JC_INDEX);
        remove(RECENT_COMMENTS);
        Utils.updateCustodia();
    }
}

@@ -59,16 +59,263 @@
 Cache;%0A%0A
+import java.io.BufferedReader;%0Aimport java.io.ByteArrayInputStream;%0Aimport java.io.ByteArrayOutputStream;%0Aimport java.io.InputStreamReader;%0Aimport java.io.IOException;%0Aimport java.util.zip.GZIPInputStream;%0Aimport java.util.zip.GZIPOutputStream;%0A%0A%0A
 public a
@@ -1233,31 +1233,37 @@
     
-String result = (String
+byte%5B%5D cached_bytes = (byte%5B%5D
 )Cac
@@ -1297,52 +1297,774 @@
 if (
-result != null) %7B%0A            return result;
+cached_bytes != null) %7B%0A            try %7B%0A                ByteArrayInputStream bais = new ByteArrayInputStream(cached_bytes);%0A                GZIPInputStream gzis = new GZIPInputStream(bais);%0A                BufferedReader reader = new BufferedReader(new InputStreamReader(gzis));%0A%0A                StringBuilder sb = new StringBuilder();%0A                while (true) %7B%0A                    String line = reader.readLine();%0A                    if (line == null) %7B%0A                        break;%0A                    %7D%0A                    sb.append(line);%0A                %7D%0A                return sb.toString();%0A            %7D catch (IOException e) %7B%0A                e.printStackTrace();%0A                System.out.println(%22Exception reading cached bytes%22);%0A            %7D
 %0A   
@@ -2079,16 +2079,23 @@
         
+String 
 result =
@@ -2101,24 +2101,547 @@
 = render();%0A
+%0A
+        try %7B%0A            byte%5B%5D bytes_to_compress = result.getBytes(%22UTF-8%22);%0A            ByteArrayOutputStream baos = new ByteArrayOutputStream(result.length());%0A            GZIPOutputStream gzos = new GZIPOutputStream(baos);%0A            gzos.write(bytes_to_compress);%0A            gzos.finish();%0A%0A            byte%5B%5D compressed_bytes = baos.toByteArray();%0A%0A            System.out.println(%22Cache %22 + cache_key + %22: orig size %22 + bytes_to_compress.length%0A                + %22; compressed: %22 + compressed_bytes.length);%0A%0A    
         // c
@@ -2661,16 +2661,20 @@
 2 hours%0A
+    
         
@@ -2698,30 +2698,184 @@
 ey, 
-result, 60 * 60 * 12);
+compressed_bytes, 60 * 60 * 12);%0A        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A            System.out.println(%22Exception writing cached bytes%22);%0A        %7D%0A
 %0A   

Compress cache entries