/*
Copyright (c) 2000-2013 "independIT Integrative Technologies GmbH",
Authors: Ronald Jeninga, Dieter Stubler

schedulix Enterprise Job Scheduling System

independIT Integrative Technologies GmbH [http://www.independit.de]
mailto:contact@independit.de

This file is part of schedulix

schedulix is free software: 
you can redistribute it and/or modify it under the terms of the 
GNU Affero General Public License as published by the 
Free Software Foundation, either version 3 of the License, 
or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


package de.independit.scheduler.server.util;

import java.lang.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;
import de.independit.scheduler.server.*;
import de.independit.scheduler.server.exception.*;
import de.independit.scheduler.server.locking.*;
import de.independit.scheduler.server.output.*;
import de.independit.scheduler.server.repository.*;

public class AuditWriter
{

	public final static String __version = "@(#) $Id: AuditWriter.java,v 2.1.8.1 2013/03/14 10:25:28 ronald Exp $";

	private static final SimpleDateFormat sysDateFmt = (SimpleDateFormat) SystemEnvironment.staticSystemDateFormat.clone();

	private static PrintStream audit = null;

	private static void openAuditFile(SystemEnvironment sysEnv)
		throws IOException
	{
		int fileno = 0;
		File base = new File(sysEnv.auditFile);
		String dir = base.getParent();
		String child = base.getName() + ".";
		String files[] = new File(dir).list();
		for (int i = 0; i < files.length; ++i) {
			if (files[i].startsWith(child)) {
				String nr = files[i].substring(child.length());
				try {
					int tmp;
					tmp = Integer.parseInt(nr);
					if (tmp > fileno) fileno = tmp;
				} catch (NumberFormatException nfe) {

				}
			}
		}
		fileno++;
		child = child + fileno;
		File auditFile = new File(dir, child);
		audit = new PrintStream(auditFile);
	}

	private static final String getHeader (SystemEnvironment sysEnv, Long versionId)
		throws SDMSException
	{
		String name = null;
		if (sysEnv.cEnv != null) {
			final Long uid = sysEnv.cEnv.uid();
			if (uid != null) {
				try {
					SDMSUser u = SDMSUserTable.getObject(sysEnv, uid);
					name = u.getName(sysEnv);
				} catch (NotFoundException nfe) {
					name = sysEnv.cEnv.name() + "(" + uid + ")";
				}
			} else
				name = sysEnv.cEnv.name();
		} else
			name = "Unknown";

		String header = "" + versionId + "\t" + name;

		synchronized (sysDateFmt) {
			return header + "\t[" + sysDateFmt.format (new java.util.Date (System.currentTimeMillis())) + "]\t";
		}
	}

	public static void write (SystemEnvironment sysEnv, Long versionId, String stmt)
		throws SDMSException
	{
		try {
			if (audit == null) {
				openAuditFile(sysEnv);
			}
			audit.println(getHeader(sysEnv, versionId) + stmt);
		} catch (IOException ioe) {

			if (audit != null) {
				audit.close();
				audit = null;
			}
		} catch (SerializationException e) {
			throw e;
		} catch (SDMSException e) {

		}
	}

}


@@ -915,18 +915,16 @@
 s/%3E.%0A*/%0A
-%0A%0A
 package 
@@ -1331,123 +1331,8 @@
 r%0A%7B%0A
-%0A%09public final static String __version = %22@(#) $Id: AuditWriter.java,v 2.1.8.1 2013/03/14 10:25:28 ronald Exp $%22;%0A%0A
 %09pri
@@ -1447,16 +1447,141 @@
 clone();
+%0A%09private static final SimpleDateFormat fileExtFmt = new SimpleDateFormat (%22yyyyMMddHHmmss%22, SystemEnvironment.systemLocale);
 %0A%0A%09priva
@@ -1615,16 +1615,56 @@
  = null;
+%0A%09private static int nrLinesWritten = 0;
 %0A%0A%09priva
@@ -1887,334 +1887,80 @@
 ing 
-files%5B%5D = new File(dir).list();%0A%09%09for (int i = 0; i %3C files.length; ++i) %7B%0A%09%09%09if (files%5Bi%5D.startsWith(child)) %7B%0A%09%09%09%09String nr = files%5Bi%5D.substring(child.length());%0A%09%09%09%09try %7B%0A%09%09%09%09%09int tmp;%0A%09%09%09%09%09tmp = Integer.parseInt(nr);%0A%09%09%09%09%09if (tmp %3E fileno) fileno = tmp;%0A%09%09%09%09%7D catch (NumberFormatException nfe) %7B%0A%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%09%09fileno++
+dt = fileExtFmt.format (new java.util.Date (System.currentTimeMillis()))
 ;%0A%09%09
@@ -1975,22 +1975,18 @@
 child + 
-fileno
+dt
 ;%0A%09%09File
@@ -2058,16 +2058,38 @@
 tFile);%0A
+%09%09nrLinesWritten = 0;%0A
 %09%7D%0A%0A%09pri
@@ -2769,24 +2769,37 @@
 blic static 
+synchronized 
 void write (
@@ -2998,16 +2998,138 @@
  stmt);%0A
+%09%09%09nrLinesWritten++;%0A%09%09%09if (nrLinesWritten %3E= SystemEnvironment.auditEntries) %7B%0A%09%09%09%09audit.close();%0A%09%09%09%09audit = null;%0A%09%09%09%7D%0A
 %09%09%7D catc
@@ -3150,17 +3150,16 @@
  ioe) %7B%0A
-%0A
 %09%09%09if (a
@@ -3267,16 +3267,16 @@
 hrow e;%0A
+
 %09%09%7D catc
@@ -3297,17 +3297,16 @@
 on e) %7B%0A
-%0A
 %09%09%7D%0A%09%7D%0A%0A

write audit files with datetime extensions