/*
* 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.google.publicalerts.cap.Alert;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSInputFile;
import java.io.UnsupportedEncodingException;
/**
* Utility class to generate a CAP file from a MongoDB object containing advice information.
* @author serch
*/
public class CAPFileGenerator {
/** {@link mx.org.cedn.avisosconagua.mongo.CAPGenerator} object instance **/
private CAPGenerator generator;
/** Name for the generated CAP file **/
private String name;
/** Flag to determine if the generation succeeded **/
private boolean isOK = false;
/** Web path to the generated CAP file **/
private String link;
/**
* Constructor. Creates a new instance of the CAPFileGenerator with the provided advice ID.
* @param adviceID ID of the advice to get information from.
*/
public CAPFileGenerator(String adviceID) {
generator = new CAPGenerator(adviceID);
name = adviceID + "_cap.xml";
link = "/getFile/" + name;
}
/**
* Generates and stores the CAP file.
*/
public void generate() {
try {
GridFS fs = MongoInterface.getInstance().getGeneratedFS();
fs.remove(name);
GridFSInputFile infile = fs.createFile(generator.generate().getBytes("UTF-8"));
infile.setContentType("text/xml");
infile.setFilename(name);
infile.save();
isOK = true;
} catch (UnsupportedEncodingException uex) {
uex.printStackTrace();
}
}
/** Gets the OK field
* @return true if the CAP file was generated succesfully, false otherwise
**/
public boolean isOK() {
return isOK;
}
/** Gets the CAP file Web path
* @return link path to the CAP file
**/
public String getLink() {
return link;
}
/** Gets the CAP file name
* @return name of the CAP file
**/
public String getName() {
return name;
}
/**
* Gets the {@link com.google.publicalerts.cap.Alert} object related to the generated CAP file
* @return {@link com.google.publicalerts.cap.Alert} object.
*/
public Alert getAlert() {
return generator.generateAlert();
}
/**
* Gets the issue date string for the advice related to the CAP file.
* @return string date of issue
*/
public String getDate() {
return generator.getDate();
}
}