package org.projectapollo.demo.General.GZIP;
import apollo.*;
import java.util.zip.*;
import java.io.*;
import apollo.Log.*;
import apollo.Template.*;
import apollo.Storable.*;
import apollo.Session.*;
import org.projectapollo.demo.Storable.*;
import org.projectapollo.demo.General.GZIP.Download.*;
import java.util.*;
 
 
public class GZIPBroker extends PageBroker {
    private Vector AS;
 
    AcceptClause displayEntry;
    AcceptClause uploadFile;
    public GZIPBroker(ManagerTracker MT, String fquid, PageBroker PB) throws TemplatePageException {
        super(MT,fquid,PB);
        this.registerPageHandler("Download",new DownloadBroker(MT,fquid+".Download", this));
        MT.getPM().registerTemplatePage(FQUID,
new TemplatePage(MT,this));
        AS =
new Vector();
        displayEntry=
new AcceptClause("DisplayEntry");
        uploadFile=
new AcceptClause("UploadFile");
        uploadFile.addAcceptClause(AcceptClause.STRING,
"Upload", AcceptClause.REQUIRED);
        AS.addElement(uploadFile);
        AS.addElement(displayEntry);
    }
 
    public HTTPResponse render(TransactionTracker TT, HTTPRequest req, WebSession thisSession) throws ApolloException {
        EntryAssertionManager EAM =
new EntryAssertionManager(MT, AS);
        EAM.evaluate(req);
        User user = (User)thisSession.getValue(
"User");
        Hashtable RT =
new Hashtable();
        if (EAM.isAccept(uploadFile)) {
            HTTPFile file = req.getFile(
"GZIPFile");
        if (file.getSize()!=0) {
            thisSession.setValue(
"GZIPFileSize", new Integer(file.getFileData().length()));
             //GZIP the data
            try {
                ByteArrayOutputStream GZIPOutputDataStream =
new ByteArrayOutputStream();
                GZIPOutputStream GZIPOS =
new GZIPOutputStream(GZIPOutputDataStream);
                GZIPOS.write(file.getFileData().getBytes());
                GZIPOS.finish();
                thisSession.setValue(
"GZIPedFileSize",new Integer(GZIPOutputDataStream.toString().length()));
            }
catch (Exception e){
                thisSession.remove(
"GZIPFileSize");
                thisSession.remove(
"GZIPedFileSize");
            }
        }
    }
    if (thisSession.hasValue("GZIPFileSize")) {
        int originalFileSize = ((Integer)thisSession.getValue("GZIPFileSize")).intValue();
        int newFileSize = ((Integer)thisSession.getValue("GZIPedFileSize")).intValue();
        double percentage = 100-((double)newFileSize/(double)originalFileSize)*100;
        RT.put(
"BeforeSize", new Integer(originalFileSize));
        RT.put(
"AfterSize", new Integer(newFileSize));
        RT.put(
"Percentage", new Integer((int) percentage));
        RT.put(
"PercentageDecimal", new Integer((int) percentage));
        RT.put(
"UncompressedSeconds",new Integer((int)((double)originalFileSize/(double)2500)));
        RT.put(
"CompressedSeconds",new Integer((int)((double)newFileSize/(double)2500)));
 
    }
    RT.put(
"FileStatsAvailable", new Boolean(thisSession.hasValue("GZIPFileSize")));
    return MT.getPM().getPage(FQUID).render(RT,thisSession);
    }
// end render
 
}