/*
* The MIT License (MIT)
*
* Copyright (c) 2014 México Abierto
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* For more information visit https://github.com/mxabierto/avisos.
*/
package mx.org.cedn.avisosconagua.mongo;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* Utility class to generate a zip compressed file with the HTML representation of the advice.
* @author serch
*/
public class HtmlZipGenerator {
/** HTML generator object instance **/
private final HtmlGenerator html;
/** Web link to the generated zip **/
private final String link;
/** Name of the generated HTML file **/
private final String name;
/** Required prefix for the file name **/
private final String prefix;
/** Name of the generated ZIP file **/
private final String nameZip;
/** ID of the current advice in MongoDB **/
private final String adviceID;
/** Flag to indicate wether the generation was successful **/
private boolean isOK = false;
//Test local charset
// {
// System.out.println("Default Charset=" + Charset.defaultCharset());
// System.out.println("file.encoding=" + System.getProperty("file.encoding"));
// System.out.println("Default Charset=" + Charset.defaultCharset());
// System.out.println("Default Charset in Use=" + getDefaultCharSet());
// }
/**
* Constructor. Creates a new instance of an HtmlZipGenerator.
* @param adviceID ID of the current advice
*/
public HtmlZipGenerator(String adviceID){
html = new HtmlGenerator(adviceID);
this.adviceID = adviceID;
prefix = MongoInterface.getInstance().getHTMLFileNamePrefix(adviceID);
name = prefix+".html";
nameZip = adviceID + ".zip";
link = "/getFile/" + nameZip;
}
/**
* Gets previous advice.
* @return previous advice ID
*/
public String getPrevious(){
return html.getPrevious();
}
/**
* Gets the title of the HTML advice.
* @return title of the advice
*/
public String getTitle(){
return html.getTitle();
}
/**
* Gets the default system charset.
* @return name of the default character encoding
*/
private static String getDefaultCharSet() {
OutputStreamWriter writer = new OutputStreamWriter(new ByteArrayOutputStream());
String enc = writer.getEncoding();
return enc;
}
/**
* Generates the ZIP file of the HTMl advice.
*/
public void generate() {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
String localFolder = "./"+adviceID+"/";
ZipOutputStream zout = new ZipOutputStream(baos);
zout.setLevel(9);
zout.putNextEntry(new ZipEntry(name));
zout.write(html.generate(true).getBytes("ISO8859-1"));
//zout.putNextEntry(new ZipEntry(localFolder));
if (html.getPrincipalFile()!=null){
GridFS gridfs = MongoInterface.getInstance().getImagesFS();
GridFSDBFile imageForOutput = gridfs.findOne(html.getPrincipalFile());
zout.putNextEntry(new ZipEntry(prefix+"_1"+html.getPrincipalFile().substring(html.getPrincipalFile().lastIndexOf(".")))); //localFolder +
imageForOutput.writeTo(zout);
}
if (html.getPronosticoFile()!=null){
GridFS gridfs = MongoInterface.getInstance().getImagesFS();
GridFSDBFile imageForOutput = gridfs.findOne(html.getPronosticoFile());
zout.putNextEntry(new ZipEntry(prefix+"_2"+html.getPrincipalFile().substring(html.getPrincipalFile().lastIndexOf(".")))); //localFolder +
imageForOutput.writeTo(zout);
}
zout.putNextEntry(new ZipEntry(prefix+"_f.gif"));
InputStream fin = HtmlZipGenerator.class.getResourceAsStream("/fondo.gif");
byte[] buff = new byte[8192];
int lenght;
while ((lenght = fin.read(buff))>-1){
zout.write(buff, 0, lenght);
}
// ArrayList<String> lista = MongoInterface.getInstance().listFilesFromAdvice(adviceID);
// for (String filename : lista) {
// GridFS gridfs = MongoInterface.getInstance().getImagesFS();
// GridFSDBFile imageForOutput = gridfs.findOne(filename);
// String fnpart[] = filename.split(":");
// zout.putNextEntry(new ZipEntry(localFolder + fnpart[1]));
// imageForOutput.writeTo(zout);
// }
zout.close();
GridFS fs = MongoInterface.getInstance().getGeneratedFS();
fs.remove(nameZip);
GridFSInputFile infile = fs.createFile(baos.toByteArray());
infile.setContentType("application/zip");
infile.setFilename(nameZip);
infile.save();
isOK = true;
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
/**
* Gets the isOk field.
* @return value of the isOk flag.
*/
public boolean isOK() {
return isOK;
}
/**
* Gets the web path to the ZIP file.
* @return path of the ZIP file
*/
public String getLink() {
return link;
}
/**
* Gets the name of the ZIP file
* @return name of the zip file
*/
public String getName() {
return nameZip;
}
}