/**
 * Copyright Bielefeld University
 * Author: Marek Imialek <marek.imialek at uni-bielefeld.de>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package eu.dnetlib.data.udm.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

/**
 * @author <a href="mailto:marek.imialek at uni-bielefeld.de">Marek Imialek</a>
 *
 */

public class UsageDataSpliter {

	/** The Constant RECORD_HEADER_TAG. */
	private static final String RECORD_TAG = "<record ";
	
	/** The Constant RECORD_HEADER_CLOSE_TAG. */
	private static final String RECORD_CLOSE_TAG = "</record>";
	
	/** The Constant RECORD_HEADER_EXTENDED_TAG_PATTERN. */
	//private static final String RECORD_EXTENDED_TAG_PATTERN = "<record (.*)>";
	
	/** The Constant RECORD_HEADER_EXTENDED_TAG_PATTERN2. */
	//private static final String RECORD_EXTENDED_TAG_PATTERN2 = "^(.*)<record (.*)";
	
	private static final String COLLECTION_DATE_PATTERN = "<dri:dateOfCollection>(.*)T(.*)</dri:dateOfCollection>";
	
	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		
		processListRecordsFile("/var/lib/dnet/udm/servlet/2011/10/2011-10-05-all.xml");
	}
	
	private static List<String> processListRecordsFile (String filePath) 
		throws Exception {

	BufferedReader br = null;
	List<String> files = new ArrayList<String>();
	OutputStreamWriter oswriter  = null;
	BufferedWriter writer = null;
	
	Pattern collectionDatePattern = Pattern.compile(COLLECTION_DATE_PATTERN);

	try {
		br = new BufferedReader(
				new InputStreamReader(
						new FileInputStream(filePath), "UTF8"));
		String line;
		StringBuilder record = null;
		String recordCollectionDate = null;
		boolean collect = false;
		
		
		while ((line = br.readLine()) != null) {
			
			if (line.contains(RECORD_TAG)) {
				recordCollectionDate = new String();
				record = new StringBuilder();
				collect = true;
			}

			Matcher dateMatacher = collectionDatePattern.matcher(line);
			if(dateMatacher.find()){
				recordCollectionDate = dateMatacher.group(1);
			}
			
			if (collect){ 
				record.append(line);
			}	
				
			if (line.contains(RECORD_CLOSE_TAG)) {
				
				
				System.out.println(recordCollectionDate);
				if (recordCollectionDate != null) {
					String fileDir = "/tmp/"+recordCollectionDate+".xml";
					File file = new File(fileDir);
					boolean headerActive = false;
					if (!file.exists())
						headerActive = true;
					
					oswriter = new OutputStreamWriter(
			                  new FileOutputStream(fileDir, true), "UTF-8");
					writer= new BufferedWriter(oswriter);
					
					if (headerActive) {
						//writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
						writer.write("<daypackage created=\""+recordCollectionDate+"\">\n");
					}
						
					String formatted = xmlPrettyPrinter(record.toString(), true);
					formatted = formatted.replace(
							"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n", "");
					writer.write(formatted);
					writer.close();
					oswriter.close();
					
					if (!files.contains(fileDir))
						files.add(fileDir);
				} else
					throw new Exception("Collection date not found !!!");
				//records.add(record.toString());
				collect = false;
			}
		}
		
		Iterator<String> iter = files.iterator();
		while (iter.hasNext()){
			oswriter = new OutputStreamWriter(
	                  new FileOutputStream(iter.next(), true), "UTF-8");
			writer= new BufferedWriter(oswriter);
			writer.write("</daypackage>");
			writer.close();
			oswriter.close();
		}
		return files;
		
	} catch (Exception e) {
		String msg = "Problem by processing ListRecords file "+e;
		//System.out.println(msg);
		throw new Exception(msg);
	} finally {
		if (br != null) br.close();
		if (writer != null) writer.close();
		if (oswriter != null) oswriter.close();
	}
}
	
	public static String xmlPrettyPrinter (String xml, boolean indentActive) throws Exception {
		Document doc = DocumentHelper.parseText(xml);  
		StringWriter sw = new StringWriter();  
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setIndent(indentActive);
		XMLWriter xw = new XMLWriter(sw, format);  
		xw.write(doc); 
		return sw.toString();
	}

}
