/*****************************************************************
//Loop the view of all articles and create a new HTML file for each one in a folder
//called "/database.nsf/articles/"
//Files to be named DOC_UNID.html
//Subfolder called DOC_UNID/$file/ to be created to store any attachments
//All comments to be written to bottom of main index.html file
*****************************************************************/
import java.io.*;
import lotus.domino.*;
import java.util.Vector;
import java.text.*;
import java.util.Date;
public class ZipContent extends AgentBase {
Document articleTemp, articleDoc, responseDoc, responseTemp;
DocumentCollection responses;
View view;
PrintWriter outLog, articleLog;
InputStream in = null; //used to detach Notes attachments
Item item, attachItem;
Vector items;
int len;
boolean success = false;
Date now = new Date();
Date created;
SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy");
SimpleDateFormat datetime = new SimpleDateFormat("dd.MMM.yyyy 'at' HH:mm a");
String slash;
public void NotesMain() {
try {
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
//current database and current document
Database db = agentContext.getCurrentDatabase();
Document doc = agentContext.getDocumentContext();
view = db.getView( "demos.calendar" );
//Create a Zip file we can add our files to
ZipIt backup = new ZipIt();
slash = System.getProperty("file.separator");
//Get the articles to export from the notesview
articleDoc = view.getFirstDocument();
articleTemp = null;
//start an index.html to link to all articles
articleLog = new PrintWriter( new FileOutputStream( backup.getPath()+slash+"index.html" ));
articleLog.println( "");
articleLog.println( "
");
articleLog.println( ""+ articleDoc.getItemValueString( "Title") + "");
articleLog.println( "");
articleLog.println( "DEXT Backup ["+format.format(now)+"]
");
articleLog.println( "Here's a snapshot of all the calendar entries in DEXT.
");
articleLog.println( "All Articles
");
//loop through the articles
while( articleDoc != null ){
//add a link to this article in main index.html
articleLog.println( "");
articleLog.println("Created: "+datetime.format( articleDoc.getCreated().toJavaDate() )+"
");
articleLog.println("
");
//Create a new file for the HTML.
PrintWriter outLog = new PrintWriter( new FileOutputStream( backup.getPath()+slash+"articles"+slash+articleDoc.getUniversalID()+".html" ));
outLog.println( "");
outLog.println( "");
outLog.println( ""+ articleDoc.getItemValueString( "Title") + "");
outLog.println( "");
outLog.println( "DEXT Backup ["+format.format(now)+"]
");
outLog.println( "Back to Index of Articles
");
outLog.println( ""+articleDoc.getItemValueString( "Title") + "
" );
outLog.println("Created: "+datetime.format( articleDoc.getCreated().toJavaDate() )+"
");
Vector content = articleDoc.getItemValue("Body");
outLog.println( "" );
for (int i = 0; i < content.size(); i++)
{
//Strip hard-coded references to NSF file path
String tmp = content.get(i).toString();
outLog.println( tmp.replaceAll("\\/"+db.getFilePath().replaceAll("\\\\", "\\/")+"\\/", "..\\/"));
}
outLog.println( "
");
//get comments/responses and output them here:
responses = articleDoc.getResponses();
if( responses.getCount() != 0 ){
outLog.println( "Comments
" );
responseDoc = responses.getFirstDocument();
responseTemp = null;
while( responseDoc != null ){
created = responseDoc.getCreated().toJavaDate();
outLog.println( "
");
outLog.println( "" + responseDoc.getItemValueString("AuthorsDisplayName") + "
");
outLog.println( "" + datetime.format(created) + "
");
outLog.println( "" + responseDoc.getItemValueString("Body") + "
");
outLog.println( "
");
responseTemp = responses.getNextDocument( responseDoc );
responseDoc.recycle();
responseDoc = responseTemp;
}
}
//Any attachments? If so, add to Zip too!
//gets all items in the response document
items = articleDoc.getItems();
outLog.println( "Attachments
");
outLog.println( "");
boolean attachments = false;
//loops through all the items in the response document and gets the attachments
for( int j=0; j"+attachment.getName()+"");
// Add the attachment to the zip
in = attachment.getInputStream();
backup.addNotesAttachment( articleDoc.getUniversalID(), in, attachName );
in.close();
attachment.recycle();
attachments = true;
}
}
if (!attachments)
outLog.println( "- No attachments found
");
outLog.println( "
");
outLog.println( "");
outLog.println( "");
outLog.flush();
outLog.close();
//Add this new article's html file to the ZIP
backup.addFile("articles"+slash+articleDoc.getUniversalID()+".html" );
//Get teh next article to process
articleTemp = view.getNextSibling( articleDoc );
articleDoc.recycle();
articleDoc = articleTemp;
}
articleLog.println( "
");
articleLog.println( "This backup was created by " + doc.getItemValueString("Remote_User") + " on " + datetime.format(now) + "
");
//Close the index.html in "root" dir
articleLog.println( "");
articleLog.println( "");
articleLog.flush();
articleLog.close();
//Add the main index.html file to the ZIP
backup.addFile("index.html");
//Close the Zip before we attach it!
backup.close();
//Attach the newly created Zip to the current document and then delete from OS
RichTextItem Body = (RichTextItem)doc.getFirstItem( "Files" );
Body.embedObject( EmbeddedObject.EMBED_ATTACHMENT, null, backup.getFullZipName(), backup.getFullZipName() );
//Finalise the Zip (deletes the working directory).
backup.finalise();
} catch(Exception e) {
e.printStackTrace();
}
}
}