/*****************************************************************

//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( "<html>");
			articleLog.println( "<head>");
			articleLog.println( "<title>"+ articleDoc.getItemValueString( "Title") + "</title>");
			articleLog.println( "<body>");
			articleLog.println( "<h1>DEXT Backup ["+format.format(now)+"]</h1>");
			articleLog.println( "<p>Here's a snapshot of all the calendar entries in DEXT.</p>");
			articleLog.println( "<h3>All Articles</h3>");
			
			//loop through the articles
			while( articleDoc != null ){

				//add a link to this article in main index.html
				articleLog.println( "<h4><a href=\"articles/"+articleDoc.getUniversalID()+".html\">"+articleDoc.getItemValueString( "Title")+"</a></h4>");
				articleLog.println("<p>Created: "+datetime.format( articleDoc.getCreated().toJavaDate() )+"<br>");
				articleLog.println("<hr>");

				//Create a new file for the HTML.
			 	PrintWriter outLog = new PrintWriter( new FileOutputStream( backup.getPath()+slash+"articles"+slash+articleDoc.getUniversalID()+".html" ));
			 		
				outLog.println( "<html>");
				outLog.println( "<head>");
				outLog.println( "<title>"+ articleDoc.getItemValueString( "Title") + "</title>");
				outLog.println( "<body>");
				outLog.println( "<h1>DEXT Backup ["+format.format(now)+"]</h1>");
				outLog.println( "<p><a href=\"../index.html\">Back to Index of Articles</a></p>");
				
				outLog.println( "<h2>"+articleDoc.getItemValueString( "Title") + "</h2>" );
				outLog.println("<h3>Created: "+datetime.format( articleDoc.getCreated().toJavaDate() )+"</h3>");
				
				
				
				Vector content = articleDoc.getItemValue("Body");
				outLog.println( "<div>" );

				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( "</div>");
				
				//get comments/responses and output them here:
				responses = articleDoc.getResponses();
				if( responses.getCount() != 0 ){
					
					outLog.println( "<h2>Comments</h2>" );
	
 					responseDoc = responses.getFirstDocument();
					responseTemp = null;
			 				
					while( responseDoc != null ){
						
						created = responseDoc.getCreated().toJavaDate();
						outLog.println( "<hr>");
						outLog.println( "<h4>" + responseDoc.getItemValueString("AuthorsDisplayName") + "</h4>");
						outLog.println( "<h5>" + datetime.format(created) + "</h5>");
						outLog.println( "<div>" + responseDoc.getItemValueString("Body") + "</div>");
						outLog.println( "<hr>");
						
						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( "<h3>Attachments</h3>");					
				outLog.println( "<ul>");
				boolean attachments = false;
				//loops through all the items in the response document and gets the attachments
				for( int j=0; j<items.size(); j++ ){
					
					attachItem = ( Item )items.elementAt( j );
										
					if( attachItem.getType() == Item.ATTACHMENT && attachItem.getValueString().length() != 0 ) {
						String attachName = attachItem.getValueString();

						//creates an embedded object called attachment that holds the attachment
						EmbeddedObject attachment = ( EmbeddedObject ) articleDoc.getAttachment( attachName );
							
						outLog.println( "<li><a href=\""+articleDoc.getUniversalID()+"/$file/"+attachment.getName()+"\">"+attachment.getName()+"</a></li>");
						
						// 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( "<li>No attachments found</li>");
					
				outLog.println( "</ul>");
				
			 	outLog.println( "</body>");
			 	outLog.println( "</html>");
			 	
			 	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( "<hr>");
			articleLog.println( "<p>This backup was created by " + doc.getItemValueString("Remote_User") + " on " + datetime.format(now) + "</p>");
			
			//Close the index.html in "root" dir
			articleLog.println( "</body>");
			articleLog.println( "</html>");
			 	
			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();
		}
	}
}