public class Repository {
	private static Logger logger = Logger.getLogger(DataMiner.class.getName());
	private ArrayList<Recording> recordings;
	private ArrayList<Source> sources;
	private String baseSourcePath;
	private ArrayList<DateFormat> dates;
	private boolean recursiveMode;
	private boolean onlyMatches;
	private boolean stats; // unused
	private boolean timings; // unused
	private HashMap<String, String> preferences;

	public Repository() {
		preferences = new HashMap<String, String>();
		preferences.put("timings", "false");
		preferences.put("stats", "true");
		preferences.put("chartSize", "350");
		preferences.put("ThreadPool_Group", "4");
		preferences.put("ThreadPool_File", "8");
		preferences.put("editorCommand", "gvim -R +$line $file");
		preferences.put("gatherstats", "true");
		preferences.put("gatherevents", "true");
		preferences.put("gatherreports", "true");
		recordings = new ArrayList<Recording>();
		dates = new ArrayList<DateFormat>();
		sources = new ArrayList<Source>();
		recursiveMode = false;
		// logger.info("repository ArrayList initialized");
	}

	public boolean isRecursiveMode() {
		return recursiveMode;
	}

	public void setPreference(String key, String value) {
		preferences.put(key, value);
	}

	public String getPreference(String key) {
		return getPreferences().get(key);
	}

	public void setRecursiveMode(boolean recursiveMode) {
		// logger.info("recursive mode is :"+recursiveMode);
		this.recursiveMode = recursiveMode;
	}

	public ArrayList<Source> getSources() {
		return sources;
	}

	public Source getSource(String name) {
		Iterator sourceIterator = sources.iterator();
		int cnt = 0;
		while (sourceIterator.hasNext()) {
			Source src = (Source) sourceIterator.next();
			if (src.getSourceName() == name) {
				return src;
			}
		}
		return (Source) null;
	}

	public void setSources(ArrayList<Source> sources) {
		this.sources = sources;
	}

	public void addSource(Source s) {
		if (sources == null) {
			sources = new ArrayList<Source>();
		}
		sources.add(s);
	}

	public void deleteSource(int id) {
		sources.remove(id);
	}

	public Source getSource(int id) {
		return sources.get(id);
	}

	public void updateSource(int id, String txtName, String txtRegularExp, Boolean active) {
		sources.get(id).setSourceName(txtName);
		sources.get(id).setSourcePattern(txtRegularExp);
		sources.get(id).setActive(active);
	}

	public ArrayList<Recording> getRecordings() {
		// logger.info(xstream.toXML(recordings));
		return recordings;
	}

	public ArrayList<Recording> getRecordings(Class _class, boolean onlyActive) {

		ArrayList<Recording> statRecordingArrayList = new ArrayList<Recording>();
		Iterator recordingIterator = recordings.iterator();
		int cnt = 0;
		while (recordingIterator.hasNext()) {
			Recording r = (Recording) recordingIterator.next();
			if (r.getClass().equals(_class)) { 
				if (onlyActive){
					if (r.getIsActive())
					statRecordingArrayList.add(r);	
				} else {
				statRecordingArrayList.add(r);
				}
			}
		}

		return (ArrayList<Recording>) statRecordingArrayList;
	}


	public ArrayList getReportRecordings(MineResultSet mineResultSet1, boolean b) {
		ArrayList<Recording> temp = new ArrayList<Recording>();
		ArrayList<Recording> returned= new ArrayList<Recording>();		
		Iterator<Map<Recording, Map<List<Object>, Long>>> it1 = mineResultSet1.getOccurenceReport().values().iterator();
		while (it1.hasNext()){
			Iterator test = (Iterator) it1.next().keySet().iterator();
			while (test.hasNext())
			{
			temp.add((Recording) test.next());
			}
		}
		
		Iterator<Map<Recording, Map<List<Object>, Double>>> it2 =mineResultSet1.getSumReport().values().iterator();
		while (it2.hasNext()){
			Iterator test = (Iterator) it2.next().keySet().iterator();
			while (test.hasNext())
			{
			temp.add((Recording) test.next());
			}
		}
		
		Iterator<Map<Recording, SortedMap<Double, List<Object>>>> it3 =mineResultSet1.getTop100Report().values().iterator();
		while (it3.hasNext()){
			Iterator test = (Iterator) it3.next().keySet().iterator();
			while (test.hasNext())
			{
			temp.add((Recording) test.next());
			}
		}
		
		ArrayList<Recording> aL = getRecordings(ReportRecording.class, b);
		Iterator ite = aL.iterator();
		while (ite.hasNext()){
			Recording rec= (Recording) ite.next();
			if (temp.contains(rec))	{
			returned.add(rec);	
			}
		}
		return returned;
	}
	
	
	public Recording getRecording(Class _class, int id, boolean onlyActive) {
		return getRecordings(_class,onlyActive).get(id);
	}

	public HashMap<String, String> getPreferences() {
		if (preferences == null) {
			preferences = new HashMap<String, String>();
			logger.info("new preferences");
		}
		if (!preferences.containsKey("timings")) {
			preferences.put("timings", "false");
		}

		if (!preferences.containsKey("stats")) {
			logger.info("stats set to true");
			preferences.put("stats", "true");
		}

		if (!preferences.containsKey("chartSize")) {
			preferences.put("chartSize", "350");
		}

		if (!preferences.containsKey("ThreadPool_Group")) {
			preferences.put("ThreadPool_Group", "4");
		}
		if (!preferences.containsKey("ThreadPool_File")) {
			preferences.put("ThreadPool_File", "8");
		}
		if (!preferences.containsKey("editorCommand")) {
			preferences.put("editorCommand", "gvim -R +$line $file");
		}
		
		return preferences;
	}

	public void addRecording(Recording r) {
		recordings.add(r);
		// logger.info(xstream.toXML(recordings));<
	}

	public int getRecordingCount() {
		return recordings.size();
	}

	public void deleteRecording(int id) {
		recordings.remove(id);
		ArrayList<Source> sources= this.getSources();
		Iterator<Source> ite=sources.iterator();
		while (ite.hasNext()){
			Source src=(Source) ite.next();
			src.removeActiveRecording(recordings.get(id));
		}
	}

	public Recording getRecording(int id) {
		return recordings.get(id);
	}

	public Recording getRecording(String _id) {
		Iterator recordingIterator = recordings.iterator();
		Recording recReturn = null; 
		int cnt = 0;
		while (recordingIterator.hasNext()) {
			Recording r = (Recording) recordingIterator.next();
			if (r.getId() == _id) {
				recReturn = r;
			}
		}
		return recReturn;
	}

	public void duplicateRecording(int id) {
		Recording newRecording = recordings.get(id).duplicate();
		this.addRecording(newRecording);
		ArrayList<Source> sources= this.getSources();
		Iterator<Source> ite=sources.iterator();
		if (!MetadataRecording.class.isInstance(recordings.get(id))){
		while (ite.hasNext()){
			Source src=(Source) ite.next();
			if (src.isActiveRecordingOnSource(recordings.get(id))){
				src.toggleActiveRecording(newRecording);
			}
		}}
	}

	public void update(Repository repo) {
		recordings = repo.getRecordings();
	}

	public void save(File file) {

		FileOutputStream fos = null;
		try {
			String xml = new XStream(new StaxDriver()).toXML(recordings);
			fos = new FileOutputStream(file);
			// fos.write("<?xml version=\"1.0\"?>".getBytes("UTF-8"));
			byte[] bytes = xml.getBytes("UTF-8");
			fos.write(bytes);

		} catch (Exception e) {
			System.err.println("Error in XML Write: " + e.getMessage());
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public void open(File file) {
		// XStream xstream = new XStream(new StaxDriver());
		FileOutputStream fos = null;
		try {
			recordings = (ArrayList<Recording>) new XStream(new StaxDriver()).fromXML(file);
			/*
			 * fos = new FileOutputStream(file);
			 * 
			 * byte[] bytes = xml.getBytes("UTF-8"); fos.write(bytes);
			 */
			logger.info(new XStream(new StaxDriver()).toXML(recordings));
		} catch (Exception e) {
			System.err.println("Error in XML Write: " + e.getMessage());
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public void setRecordings(ArrayList recordings) {
		this.recordings = recordings;
	}

	public void save() {

	}

	public String getBaseSourcePath() {
		return baseSourcePath;
	}

	public void setBaseSourcePath(String baseSourcePath) {
		this.baseSourcePath = baseSourcePath;
	}

	public ArrayList<DateFormat> getDates() {
		return dates;
	}

	public void setDates(ArrayList<DateFormat> hm) {
		dates = hm;
	}

	public void addDateFormat(DateFormat df) {
		if (dates == null) {
			dates = new ArrayList<DateFormat>();
		}
		dates.add(df);
		// logger.info(xstream.toXML(recordings));
	}

	public void deleteDateFormat(int id) {
		dates.remove(id);
	}

	public DateFormat getDateFormat(int id) {
		return dates.get(id);
	}

	public DateFormat getDateFormat(String id) {
		Iterator dateFormatIterator = dates.iterator();
		int cnt = 0;
		while (dateFormatIterator.hasNext()) {
			DateFormat df = (DateFormat) dateFormatIterator.next();
			if (df.getId() == id) {
				return df;
			}
		}
		return (DateFormat) null;
	}

	public void setOnlyMatches(boolean selected) {
		onlyMatches = selected;

	}

	public boolean isOnlyMatches() {
		return onlyMatches;
	}

	public boolean isStats() {
		return Boolean.parseBoolean(getPreference("stats"));
	}

	public void setStats(boolean stats) {
		setPreference("stats", Boolean.toString(stats));
	}

	public boolean isTimings() {
		return Boolean.parseBoolean(getPreference("timings"));
	}

	public void setTimings(boolean timings) {
		setPreference("timings", Boolean.toString(timings));
	}

} public class Source implements Comparable{
	private static Logger logger = Logger.getLogger(Source.class.getName());
	private String sourceName;
	private String sourcePattern;
	private DateFormat dateFormat;
	private Boolean active;
	private MetadataRecording activeMetadataRecording;
	private ArrayList<String> selectedRecordingIds;

	public Source(String name, String pattern, DateFormat _dateFormat, Boolean _active) {
		sourceName = name;
		sourcePattern = pattern;
		dateFormat = _dateFormat;
		setActive(_active);
		selectedRecordingIds = new ArrayList<String>();
		logger.info("Source loaded");

	}

	public String getSourcePattern() {
		return sourcePattern;
	}

	public void setSourcePattern(String sourcePattern) {
		this.sourcePattern = sourcePattern;
	}

	public void setActiveMetadata(MetadataRecording _activeMetadataRecording){
		activeMetadataRecording=_activeMetadataRecording;
	}

	public MetadataRecording getActiveMetadata(){
		return activeMetadataRecording;
	}
	
	public void toggleActiveRecording(Recording recording) {
		// logger.info("toggleActiveRecording of "+recording.getName());
		if (selectedRecordingIds.contains(recording.getId())) {
			selectedRecordingIds.remove(recording.getId());
			if (logger.isEnabledFor(Level.TRACE))
				logger.trace("toggleActiveRecording removed " + recording.getName() + " now size " + selectedRecordingIds.size());
		} else {
			selectedRecordingIds.add(recording.getId());
			if (logger.isEnabledFor(Level.TRACE))
				logger.trace("toggleActiveRecording added " + recording.getName() + " now size " + selectedRecordingIds.size());
		}

	}

	public void removeActiveRecording(Recording recording) {
		// logger.info("toggleActiveRecording of "+recording.getName());
		if (selectedRecordingIds.contains(recording.getId())) {
			selectedRecordingIds.remove(recording.getId());
			if (logger.isEnabledFor(Level.TRACE))
				logger.trace("toggleActiveRecording removed " + recording.getName() + " now size " + selectedRecordingIds.size());
		} 
	}
	
	
	
	public Boolean isActiveRecordingOnSource(Recording recording) {
		if (MetadataRecording.class.isInstance(recording)){
				if (getActiveMetadata()!=null && getActiveMetadata()==recording){
					return true;
				}
				else return false;
		}
		// logger.info("***isActiveRecordingOnSource of "+recording.getName());
		Boolean b = false;
		Iterator it = selectedRecordingIds.iterator();
		String recordingId = recording.getId();
		while (it.hasNext()) {
			// String temp=(String)it.next();
			if ((String) it.next() == recordingId) {
				// logger.info("***isActiveRecordingOnSource of "+recording.getName()
				// + " with id: "+ temp);
				b = true;
			}
		}
		return b;
	}

	public String getSourceName() {
		return sourceName;
	}

	public void setSourceName(String sourceName) {
		this.sourceName = sourceName;
	}

	public Boolean getActive() {
		return active;
	}

	public void setActive(Boolean active) {
		this.active = active;
	}

	@Override
	public int compareTo(Object o) {
		String local = getSourceName();
		String remote = ((Source)o).getSourceName();
		//return remote.compareTo(local);
		return local.compareTo(remote);

	}

	public DateFormat getDateFormat() {
		return dateFormat;
	}

	public void setDateFormat(DateFormat dateFormat) {
		this.dateFormat = dateFormat;
	}
} public class DataVault {
	static public MineResultSet mineResultSet;
	private static Logger logger = Logger.getLogger(DataVault.class.getName());
	static private  Map<Recording,ArrayList<String>> recordingMatchedLines = new HashMap<Recording,ArrayList<String>>();
	static private  Map<Recording,ArrayList<String>> recordingUnmatchedLines = new HashMap<Recording,ArrayList<String>>();
	public synchronized static MineResultSet getMineResultSet() {
		return mineResultSet;
	}

	public synchronized static void setMineResultSet(MineResultSet mineResultSet) {
		DataVault.mineResultSet = mineResultSet;
	}

	public DataVault() {
		// TODO Auto-generated constructor stub
	}

	public static String getMatchedLines(Recording rec){
		ArrayList aL=recordingMatchedLines.get(rec);
		String returned="";
		if (aL!=null){
		Iterator ite= aL.iterator();
		
		while (ite.hasNext()){
			returned+=ite.next()+System.getProperty("line.separator");
		}}
		return returned;
	}
	
	public static void addMatchedLines(Recording rec,String line){
		ArrayList<String> al=null;
		if (recordingMatchedLines.get(rec)==null)
				{
			al=new ArrayList<String>();
			recordingMatchedLines.put(rec,al);
				}
		if (recordingMatchedLines.get(rec)!=null ){
			if (recordingMatchedLines.get(rec).size()<5){
				recordingMatchedLines.get(rec).add(line);	
			}}
	//	recordingMatchedLines.put(rec,al);
	}
	
	public static String getUnmatchedLines(Recording rec){
		ArrayList aL=recordingUnmatchedLines.get(rec);

		String returned="";
		if (aL!=null){
			Iterator ite= aL.iterator();
			while (ite.hasNext()){
			returned+=ite.next()+System.getProperty("line.separator");;
		}}
		return returned;
	}
	public static void addUnmatchedLines(Recording rec,String line){
		ArrayList<String> al=null;
		if (recordingUnmatchedLines.get(rec)==null)
				{
			al=new ArrayList<String>();
			recordingUnmatchedLines.put(rec,al);
				}
		if (recordingUnmatchedLines.get(rec)!=null ){
			if (recordingUnmatchedLines.get(rec).size()<5){
				recordingUnmatchedLines.get(rec).add(line);	
			}
			
	//	recordingMatchedLines.put(rec,al);
		}
	}
	
	public static long[] getRecordingStats(String recording) {
		if (mineResultSet == null)
			return null;
		Iterator it = mineResultSet.mineResults.values().iterator();
		long[] tmpArray;
		long[] stats = { 0, 0, 0, 0 };// == new HashMap<String, long[]> ();
		while (it.hasNext()) {
			Map<String, MineResult> map = (Map<String, MineResult>) it.next();
			// logger.info("keys: "+map.keySet());
			// logger.info("Values: "+map.values());
			Iterator it2 = map.values().iterator();
			while (it2.hasNext()) {
				MineResult mr = (MineResult) it2.next();
				Map<String, long[]> tempStatMap = mr.getMatchingStats();
				if (tempStatMap.containsKey(recording)) {
					tmpArray = tempStatMap.get(recording);
					// logger.info(file.getName() + " contains " + arrayBefore);
					// 0-> sum of time for success matching of given
					// recording ; 1-> sum of time for failed
					// matching ; 2-> count of match attempts,
					// 3->count of success attempts
					stats[0] += tmpArray[0];
					stats[1] += tmpArray[1];
					stats[2] += tmpArray[2];
					stats[3] += tmpArray[3];
					// logger.info(file.getName() + " add success to" +
					// rec.getName() + " 0: "+ array[0] + " 1: "+ array[1]+
					// " 2: "+ array[2] +" 3: "+ array[3]);
				}
			}
		}
		return stats;

	}
} public class ReportRecording extends Recording {
	private static Logger logger = Logger.getLogger(ReportRecording.class.getName());
	private String dateFormat;
	private String subType;
	private int top100Index = -1;

	public ReportRecording(String _name, String _regexp, String _exampleLine, String _dateFormat, Boolean _isActive,Boolean _useSourceDateFormat, ArrayList<RecordingItem> _recordingItem,
			String subType1, boolean _caseSensitive) {
		setName(_name);
		setRegexp(_regexp);
		setExampleLine(_exampleLine);
		setIsActive(_isActive);
		setCaseSensitive(_caseSensitive);
		setUseSourceDateFormat(_useSourceDateFormat);
		dateFormat = _dateFormat;
		recordingItem = _recordingItem;
		subType = subType1;
		super.id = generate();
		computeTop100RecordID();
		logger.info("New ReportRecording name: " + _name + ", regexp: " + _regexp + ", id: " + super.id);
		if (recordingItem != null)
			logger.info("New ReportRecording with recordingItem ArrayList: " + recordingItem.toString());
	}

	public String getType() {
		return "Report";
	}

	public void setSubType(String subType1) {
		this.subType = subType1;
	}

	public String getSubType() {
		return subType;
	}

	private void computeTop100RecordID() {
		if (recordingItem != null) {
			Iterator<RecordingItem> rIIte = recordingItem.iterator();
			top100Index = -1;
			int count = 0;
			while (rIIte.hasNext() && top100Index == -1) {
				if (rIIte.next().getProcessingType().equals("top100")) {
					top100Index = count;
				}
				count++;
			}
		}
	}

	public int getTop100RecordID() {
		if (top100Index == -1) {
			computeTop100RecordID();
			;
			return top100Index;
		} else {
			return top100Index;
		}

	}

	public String getDateFormat() {
		return dateFormat;
	}

	public void setDateFormat(String dateFormat) {
		this.dateFormat = dateFormat;
	}

	public void setRecordingItem(ArrayList<RecordingItem> recordingItem) {
		this.recordingItem = recordingItem;
	}

	public void update(String txtName, String txtRegularExp, String exampleLine, String _dateFormat, Boolean _isActive, Boolean _useSourceDateFormat,
			ArrayList<RecordingItem> _recordingItem, String subType1, boolean _caseSensitive) {
		setName(txtName);
		setRegexp(txtRegularExp);
		setExampleLine(exampleLine);
		setIsActive(_isActive);
		setUseSourceDateFormat(_useSourceDateFormat);
		setCaseSensitive(_caseSensitive);
		dateFormat = _dateFormat;
		subType = subType1;
		recordingItem = _recordingItem;
		if (this.id == null) {
			super.id = generate();
		}
		computeTop100RecordID();

	}

	public Recording duplicate() {

		ArrayList<RecordingItem> _recordingItem = null;
		// might need put back .toString() to those?? *** TBR
		String _name = getName().toString();
		String _regexp = getRegexp().toString();
		String _exampleLine = getExampleLine().toString();
		String _dateFormat = dateFormat.toString();
		String _subType = subType.toString();

		Boolean _isActive = getIsActive().booleanValue();
		Boolean _caseSensitive = isCaseSensitive();
		Boolean _useSourceDateFormat=getUseSourceDateFormat();
		if (recordingItem != null) {
			_recordingItem = (ArrayList<RecordingItem>) recordingItem.clone();
		}
		ReportRecording eR = new ReportRecording(_name, _regexp, _exampleLine, _dateFormat, _isActive,_useSourceDateFormat, _recordingItem, _subType, _caseSensitive);
		eR.setDateFormatID(this.getDateFormatID());
		return eR;
	}
} public class MetadataRecording extends Recording {
	private static Logger logger = Logger.getLogger(MetadataRecording.class.getName());
	private ArrayList recordingItem;
	private String dateFormat;

	public String getType() {
		return "File Grouping";
	}

	public MetadataRecording(String _name, String _regexp, String _exampleLine, String _dateFormat, Boolean _isActive, Boolean _caseSensitive, ArrayList _recordingItem) {
		setName(_name);
		setRegexp(_regexp);
		setExampleLine(_exampleLine);
		setIsActive(_isActive);
		setCaseSensitive(_caseSensitive);
		dateFormat = _dateFormat;
		recordingItem = _recordingItem;
		super.id = generate();
		logger.info("New MetadataRecording name: " + _name + ", regexp: " + _regexp + ", id: " + super.id);
		if (recordingItem != null)
			logger.info("New MetadataRecording with recordingItem ArrayList: " + recordingItem.toString());
	}

	public String getDateFormat() {
		return dateFormat;
	}

	public void setDelimitator(String delimitator) {
	//	this.dateFormat = dateFormat;
	}

	public ArrayList getRecordingItem() {
		// logger.info("***********"+recordingItem.toString()+recordingItem.size());
		// Thread.currentThread().dumpStack();
		return (ArrayList) recordingItem;
	}

	public void setRecordingItem(ArrayList recordingItem) {
		this.recordingItem = recordingItem;
	}

	public void update(String txtName, String txtRegularExp, String exampleLine, String _dateFormat, Boolean _isActive, Boolean _caseSensitive, ArrayList _recordingItem) {
		setName(txtName);
		setRegexp(txtRegularExp);
		setExampleLine(exampleLine);
		setIsActive(_isActive);
		setCaseSensitive(_caseSensitive);
		dateFormat = _dateFormat;
		recordingItem = _recordingItem;
	}

	public Recording duplicate() {

		ArrayList _recordingItem = null;
		// might need put back .toString() to those?? *** TBR
		String _name = getName().toString();
		String _regexp = getRegexp().toString();
		String _exampleLine = getExampleLine().toString();
		String _dateFormat = getDateFormat().toString();

		Boolean _isActive = getIsActive().booleanValue();
		Boolean _caseSensitive = isCaseSensitive();
		if (getRecordingItem() != null) {
			_recordingItem = (ArrayList) getRecordingItem().clone();
		}
		
		MetadataRecording eR=new MetadataRecording(_name, _regexp, _exampleLine, _dateFormat, _isActive, _caseSensitive, _recordingItem);
		eR.setDateFormatID(this.getDateFormatID());
		return eR;
		
	}

} public abstract class Recording {
	private static Logger logger = Logger.getLogger(DataMiner.class.getName());
	private String name;
	private String regexp;
	protected ArrayList recordingItem;
	private String FastDateFormat;
	private String exampleLine;
	private Boolean isActive;
	protected String id;
	private Boolean caseSensitive;
	private Boolean useSourceDateFormat;
	private String dateFormatID;

	public String getId() {
		return id;
	}

	public String generate() {
		String generatedUniqueId = UUID.randomUUID().toString();
		logger.info("unique ID: " + generatedUniqueId);
		return generatedUniqueId;
	}

	public ArrayList getRecordingItem() {
		// logger.info("***********"+recordingItem.toString()+recordingItem.size());
		// Thread.currentThread().dumpStack();
		return recordingItem;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getRegexp() {
		return regexp;
	}

	public void setRegexp(String regexp) {
		this.regexp = regexp;
	}

	public String getExampleLine() {
		return exampleLine;
	}

	public void setExampleLine(String exampleLine) {
		this.exampleLine = exampleLine;
	}

	public abstract String getType();

	public Boolean getIsActive() {
		return isActive;
	}

	public boolean isCaseSensitive() {
		if (caseSensitive!=null){
			return caseSensitive;
		}else {
			return true;
		}
	}

	public void setCaseSensitive(boolean caseSensitive) {
		this.caseSensitive = caseSensitive;
	}

	public void setIsActive(Boolean isActive) {
		this.isActive = isActive;
	}

	abstract public Recording duplicate();

	public String getDateFormatID() {
		return dateFormatID;
	}

	public void setDateFormatID(String dateFormatID) {
		this.dateFormatID = dateFormatID;
	}

	public Boolean getUseSourceDateFormat() {
		if (useSourceDateFormat==null){
			return false;
		} else{
		return useSourceDateFormat;}
	}

	public void setUseSourceDateFormat(Boolean useSourceDateFormat) {
		this.useSourceDateFormat = useSourceDateFormat;
	}

} public class RecordingItem {
	private String processingType;
	protected String name;
	protected String before;
	protected String after;
	protected String type;
	protected String inside;
	protected String value;
	protected boolean selected;
	protected boolean show;

	public String getBefore() {
		return before;
	}

	public void setBefore(String before) {
		this.before = before;
	}

	public String getAfter() {
		return after;
	}

	public void setAfter(String after) {
		this.after = after;
	}

	public String getProcessingType() {
		return processingType;
	}

	public void setProcessingType(String processingType) {
		this.processingType = processingType;
	}

	public RecordingItem(String name, String before, String type, String processingType, String insideRegex, String after, Boolean isSelected, Boolean show, String value) {
		this.name = name;
		this.before = before;
		this.after = after;
		this.value = value;
		this.type = type;
		this.selected = isSelected;
		this.processingType = processingType;
		this.inside=insideRegex;
		this.show=show;
		// super(name, before, type, after, isSelected, value);
		// TODO Auto-generated constructor stub
	}

	public RecordingItem(String name, String before, String type, String insideRegex, String after, Boolean isSelected, Boolean show, String value) {
		this.name = name;
		this.before = before;
		this.after = after;
		this.value = value;
		this.type = type;
		this.selected = isSelected;
		this.inside=insideRegex;
		this.show=show;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public boolean isSelected() {
		return selected;
	}

	public void setSelected(boolean selected) {
		this.selected = selected;
	}

	public boolean isShow() {
		return show;
	}

	public void setShow(boolean show) {
		this.show = show;
	}

	public String getInside() {
		return inside;
	}

	public void setInside(String _inside) {
		this.inside = _inside;
	}
	
	

} public class StatRecording extends Recording {
	private static Logger logger = Logger.getLogger(StatRecording.class.getName());
//	private ArrayList recordingItem;
	private String dateFormat;

	public String getType() {
		return "Stat";
	}

	public StatRecording(String _name, String _regexp, String _exampleLine, String _dateFormat, Boolean _isActive, Boolean _useSourceDateFormat, Boolean _caseSensitive, ArrayList _recordingItem) {
		setName(_name);
		setRegexp(_regexp);
		setExampleLine(_exampleLine);
		setIsActive(_isActive);
		setUseSourceDateFormat(_useSourceDateFormat);
		setCaseSensitive(_caseSensitive);
		dateFormat = _dateFormat;
		recordingItem = _recordingItem;
		super.id = generate();
		// logger.info("New EventRecording name: "+_name + ", regexp: "+ _regexp
		// + ", id: "+ super.id +recordingItem.toString());
	}

	public String getDateFormat() {
		return dateFormat;
	}

	public void setDelimitator(String delimitator) {
	//	this.dateFormat = dateFormat;
	}

	public ArrayList getRecordingItem() {
		// logger.info("***********"+recordingItem.toString()+recordingItem.size());
		// Thread.currentThread().dumpStack();
		return recordingItem;
	}

	public void setRecordingItem(ArrayList recordingItem) {
		this.recordingItem = recordingItem;
	}

	public void update(String txtName, String txtRegularExp, String exampleLine, String _dateFormat, Boolean _isActive, Boolean _useSourceDateFormat, Boolean _caseSensitive, ArrayList _recordingItem) {
		setName(txtName);
		setRegexp(txtRegularExp);
		setExampleLine(exampleLine);
		setIsActive(_isActive);
		setCaseSensitive(_caseSensitive);
		setUseSourceDateFormat(_useSourceDateFormat);
		dateFormat = _dateFormat;
		recordingItem = _recordingItem;
		if (this.id == null) {
			super.id = generate();
		}
	}

	public Recording duplicate() {

		ArrayList _recordingItem = null;
		// might need put back .toString() to those?? *** TBR
		String _name = getName().toString();
		String _regexp = getRegexp().toString();
		String _exampleLine = getExampleLine().toString();
		String _dateFormat = dateFormat.toString();

		Boolean _isActive = getIsActive().booleanValue();
		Boolean _caseSensitive = isCaseSensitive();
		Boolean _useSourceDateFormat=getUseSourceDateFormat();
		if (recordingItem != null) {
			_recordingItem = (ArrayList) recordingItem.clone();
		}
		StatRecording eR=new StatRecording(_name, _regexp, _exampleLine, _dateFormat, _isActive,_useSourceDateFormat, _caseSensitive, _recordingItem);
		eR.setDateFormatID(this.getDateFormatID());
		return eR;
		
	}
} public class ReportRecordingEditor extends JPanel {
	private static Logger logger = Logger.getLogger(DataMiner.class.getName());
	private JPanel _this = this;
	private final JPanel contentPanel = new JPanel();
	private JTextField txtName;
	private JTextField txtRegularExp;
	private JTextField txtDate;
	JPanel panel2;
	private ReportRecordingEditorTable reportRecordingEditorTablePanel;
	private Repository repository;
	DefaultListModel listModel;
	JTextPane examplePane = new JTextPane();
	private ReportRecording recording;
	Document doc;
	JCheckBox chckbxActive;
	JCheckBox chckbxCaseSensitive;
	private JLabel nameLabel;
	private JLabel regularExpressionLabel;
	private JLabel dateFormatLabel;
	JTextPane textPane ;
	JComboBox comboBox ;
	JCheckBox chckbxDefaultDateFormat;
	JButton dateFormatButton;
	// private JList recordingList;

	/*
	 * public EventRecordingEditor(Repository repository2, String exampleLine,
	 * String regexp, ReportRecording eventRecording) { this(null, repository2,
	 * exampleLine, regexp, eventRecording); }
	 */

	/**
	 * Create the dialog.
	 */
	public ReportRecordingEditor(final JPanel newRecordingList, ReportRecording re, Repository repo) {
		this(newRecordingList, repo, re.getExampleLine(), re.getRegexp(), re, re.getSubType());
	}

	public ReportRecordingEditor(Repository repository2, String exampleLine, String regexp, ReportRecording reportRecording) {
		this(null, repository2, exampleLine, regexp, reportRecording, regexp);
	}
	/**
	 * Create the dialog.
	 * @param typeString 
	 * @param string 
	 * 
	 * @wbp.parser.constructor
	 */
	public ReportRecordingEditor(final JPanel newRecordingList, Repository repo, String theLine, String regex, final ReportRecording re, String typeString) {
		// setBounds(0, 0, 1015, 467);

		repository = repo;
		recording = re;
		// logger.debug("myTableModel2 null? "+(newRecordingList==null));
		BorderLayout borderLayout = new BorderLayout();
		this.setLayout(borderLayout);
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		this.add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));

		{
			JSplitPane splitPane = new JSplitPane();
			splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
			contentPanel.add(splitPane, BorderLayout.CENTER);
			{
				JPanel panelTop = new JPanel();
				splitPane.setLeftComponent(panelTop);
				{
					panelTop.setLayout(new BorderLayout(0, 0));
					{
						JPanel panel_1a = new JPanel();
						panelTop.add(panel_1a, BorderLayout.NORTH);
						panel_1a.setLayout(new GridLayout(0, 4, 5, 2));
						{
							JPanel namePanel = new JPanel();
							panel_1a.add(namePanel);
							namePanel.setLayout(new BorderLayout(0, 0));
							{
								nameLabel = new JLabel("name:");
								namePanel.add(nameLabel, BorderLayout.WEST);
							}
							nameLabel.setLabelFor(txtName);
							{
								txtName = new JTextField();
								namePanel.add(txtName);
								txtName.setText("name");
								txtName.setColumns(5);
							}
						}
						{
							JPanel regularExpressionPanel = new JPanel();
							panel_1a.add(regularExpressionPanel);
							regularExpressionPanel.setLayout(new BorderLayout(0, 0));
							{
								regularExpressionLabel = new JLabel("reg. exp.:");
								regularExpressionPanel.add(regularExpressionLabel, BorderLayout.WEST);
							}
							{
								txtRegularExp = new JTextField();
								regularExpressionLabel.setLabelFor(txtRegularExp);
								regularExpressionPanel.add(txtRegularExp);
								txtRegularExp.addCaretListener(new CaretListener() {
									public void caretUpdate(CaretEvent e) {
										doc = examplePane.getDocument();
										PatternCache patternCache = new PatternCache();
										Highlighter h = examplePane.getHighlighter();
										h.removeAllHighlights();
										Pattern pattern = patternCache.getPattern(txtRegularExp.getText(),re.isCaseSensitive());
										String[] lines = examplePane.getText().split(System.getProperty("line.separator"));
										int currIndex = 0;
										if (lines.length>=1){
										for (int i=0; i<lines.length ; i++)
										{
											logger.debug("line: "+lines[i]);
											
											Matcher matcher = pattern.matcher(lines[i]);
											if (matcher.find()) {
												// int currIndex=doc.getLength();
												// doc.insertString(doc.getLength(),line+"\n",
												// null);
												try {
													h.addHighlight(currIndex+matcher.start(), currIndex+matcher.end(), DefaultHighlighter.DefaultPainter);
												} catch (BadLocationException e1) {
													// TODO Auto-generated catch block
													e1.printStackTrace();
												}
											}
											logger.debug("currIndex: "+currIndex+",length: "+lines[i].length());
											currIndex +=lines[i].length()+1 ;
										}
										}
									}
								});
								txtRegularExp.setText(regex);
								txtRegularExp.setColumns(10);
							}
						}
						{
							JPanel panel_2 = new JPanel();
							panel_1a.add(panel_2);
							panel_2.setLayout(new BorderLayout(0, 0));
							{
								dateFormatLabel = new JLabel("Date Format:\n");
								panel_2.add(dateFormatLabel, BorderLayout.WEST);
							}
							{
								txtDate = new JTextField();
								dateFormatLabel.setLabelFor(txtDate);
								txtDate.setEditable(false);
								panel_2.add(txtDate, BorderLayout.CENTER);
								txtDate.setText("date format");
								txtDate.setColumns(10);
							}
							{
								dateFormatButton = new JButton("...");
								dateFormatButton.addActionListener(new ActionListener() {
									public void actionPerformed(ActionEvent e) {
										DateSelector dateDialog = new DateSelector(repository, txtDate, re);
										dateDialog.validate();
										dateDialog.setResizable(true);
										dateDialog.setModal(false);
										dateDialog.setVisible(true);
									}
								});
								dateFormatButton.setFont(new Font("Dialog", Font.BOLD, 6));
								panel_2.add(dateFormatButton, BorderLayout.EAST);
							}
						}
						{
							chckbxDefaultDateFormat = new JCheckBox("default date format");
							chckbxDefaultDateFormat.setSelected(re.getUseSourceDateFormat());
							dateFormatButton.setEnabled(!re.getUseSourceDateFormat());
							txtDate.setEnabled(!re.getUseSourceDateFormat());
							chckbxDefaultDateFormat.addActionListener(new ActionListener() {
								public void actionPerformed(ActionEvent e) {
									if (chckbxDefaultDateFormat.isSelected()){
										dateFormatButton.setEnabled(false);
										txtDate.setEnabled(false);
									} else {
										dateFormatButton.setEnabled(true);
										txtDate.setEnabled(true);
									}
									
								}
							});
							panel_1a.add(chckbxDefaultDateFormat);
						}
						{
							comboBox = new JComboBox();
							comboBox.setModel(new DefaultComboBoxModel(new String[] {"histogram", "top100","sum"}));
							panel_1a.add(comboBox);
						}
						{
							chckbxCaseSensitive = new JCheckBox("case sensitive");
							chckbxCaseSensitive.setSelected(re.isCaseSensitive());
							panel_1a.add(chckbxCaseSensitive);
						}
						{
							chckbxActive = new JCheckBox("active");
							chckbxActive.setSelected(true);
							panel_1a.add(chckbxActive);
						}
						{
							JCheckBox chckbxMultiLine = new JCheckBox("multi line");
							chckbxMultiLine.setEnabled(false);
							panel_1a.add(chckbxMultiLine);
						}
					}
					{
						JPanel panel_1 = new JPanel();
						panelTop.add(panel_1, BorderLayout.CENTER);
						panel_1.setLayout(new BorderLayout(5, 5));
						panel2 = new JPanel();
						panel2.setPreferredSize(new Dimension(0,180));
						panel_1.add(panel2, BorderLayout.CENTER);
						panel2.setLayout(new BorderLayout(0, 0));

						{
							JPanel panel_2 = new JPanel();
							panel_1.add(panel_2, BorderLayout.SOUTH);
							FlowLayout flowLayout = (FlowLayout) panel_2.getLayout();
							flowLayout.setAlignment(FlowLayout.LEFT);
							{
								JButton btnAddButton = new JButton("Add");
								btnAddButton.addActionListener(new ActionListener() {
									public void actionPerformed(ActionEvent e) {
										reportRecordingEditorTablePanel.Add();
									}
								});
								btnAddButton.setHorizontalAlignment(SwingConstants.LEFT);
								panel_2.add(btnAddButton);
							}
							{
								JButton btnRemoveButton = new JButton("Remove");
								btnRemoveButton.addActionListener(new ActionListener() {
									public void actionPerformed(ActionEvent e) {
										reportRecordingEditorTablePanel.Remove();
									}
								});
								{
									JButton btnInsert = new JButton("Insert");
									btnInsert.addActionListener(new ActionListener() {
										public void actionPerformed(ActionEvent e) {
											reportRecordingEditorTablePanel.Insert();
										}
									});
									panel_2.add(btnInsert);
								}
								btnRemoveButton.setHorizontalAlignment(SwingConstants.LEFT);
								panel_2.add(btnRemoveButton);
							}
							{
								JButton btnCheck = new JButton("Check");
								btnCheck.addActionListener(new ActionListener() {
									public void actionPerformed(ActionEvent e) {
										logger.debug("check");
										reportRecordingEditorTablePanel.FixValues();

									}
								});
								panel_2.add(btnCheck);
							}
							{
								JPanel buttonPane = new JPanel();
								panel_2.add(buttonPane);
								buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));
								{
									JButton okButton = new JButton("Save");
									okButton.setForeground(Color.BLUE);
									okButton.addActionListener(new ActionListener() {
										public void actionPerformed(ActionEvent arg0) {
											ArrayList rIs = reportRecordingEditorTablePanel.getRecordingItems();
											if (newRecordingList.getClass()==RecordingList.class) {
												if (recording == null){
												logger.debug("RecordingEditor - ok 1");
												Recording r = new ReportRecording(txtName.getText(), txtRegularExp.getText(), examplePane.getText(), txtDate.getText(), chckbxActive
														.isSelected(),chckbxDefaultDateFormat.isSelected(), rIs,comboBox.getSelectedItem().toString(),chckbxCaseSensitive.isSelected());
												repository.addRecording(r);
												logger.debug("RecordingEditor - ok 1");
												if (newRecordingList.getClass()==RecordingList.class){
													logger.debug("RecordingEditor - ok 1");
												if (((RecordingList) newRecordingList).model != null) {
													logger.debug("RecordingEditor - ok 1");
													((RecordingList) newRecordingList).model.addRow(new Object[] { txtName.getText(), txtRegularExp.getText(), chckbxActive.isSelected() });
													((RecordingList) newRecordingList).model.fireTableDataChanged();
												
											}}} else {
												int selectedRow = ((((RecordingList) newRecordingList).table.getSelectedRow() != -1) ? ((((RecordingList) newRecordingList).table.getSelectedRow())) : -1);
												//int selectedRow = ((((ReportPanel) newRecordingList).table.getSelectedRow() != -1) ? ((ReportPanel) newRecordingList).table.convertRowIndexToModel(((ReportPanel) newRecordingList).table.getSelectedRow()) : -1);
												((ReportRecording) recording).update(txtName.getText(), txtRegularExp.getText(), examplePane.getText(), txtDate.getText(),
														chckbxActive.isSelected(),chckbxDefaultDateFormat.isSelected(), rIs,comboBox.getSelectedItem().toString(),chckbxCaseSensitive.isSelected());
												((RecordingList) newRecordingList).model.fireTableDataChanged();
												logger.debug("RecordingEditor - row Updated");
												((RecordingList) newRecordingList).table.setRowSelectionInterval(selectedRow, selectedRow);
											}}
											else
											{
												int rowCount = ((ReportRecordingSelectorPanel) newRecordingList).table.getRowCount();
												if (recording == null){
													int selectedRow = ((((ReportRecordingSelectorPanel) newRecordingList).table.getSelectedRow() != -1) ? ((ReportRecordingSelectorPanel) newRecordingList).table.convertRowIndexToModel(((ReportRecordingSelectorPanel) newRecordingList).table.getSelectedRow()) : -1);
												if (((ReportRecordingSelectorPanel) newRecordingList).model != null) {
													logger.debug("RecordingEditor - ok 1");
													((ReportRecordingSelectorPanel) newRecordingList).model.addRow(new Object[] { txtName.getText(), txtRegularExp.getText(), chckbxActive.isSelected() });
													((ReportRecordingSelectorPanel) newRecordingList).model.fireTableDataChanged();
													((ReportRecordingSelectorPanel) newRecordingList).table.setRowSelectionInterval(selectedRow, selectedRow);
												}}
											 else {
												int selectedRow = ((((ReportRecordingSelectorPanel) newRecordingList).table.getSelectedRow() != -1) ? (((ReportRecordingSelectorPanel) newRecordingList).table.getSelectedRow()) : -1);
												((ReportRecording) recording).update(txtName.getText(), txtRegularExp.getText(), examplePane.getText(), txtDate.getText(),
														chckbxActive.isSelected(), chckbxDefaultDateFormat.isSelected(),rIs,comboBox.getSelectedItem().toString(),chckbxCaseSensitive.isSelected());
												logger.debug("RecordingEditor - NEVER HERE row Updated");
												((ReportRecordingSelectorPanel) newRecordingList).model.fireTableDataChanged();
												((ReportRecordingSelectorPanel) newRecordingList).table.setRowSelectionInterval(selectedRow, selectedRow);
											}
											}
							/*				if (contentPanel.getParent().getParent().getParent().getParent().getClass().equals(JDialog.class)) {
												((JDialog) contentPanel.getParent().getParent().getParent().getParent()).dispose();
											}*/
											//
										}
									});
									{
										JSeparator separator = new JSeparator();
										buttonPane.add(separator);
									}
									okButton.setActionCommand("OK");
									buttonPane.add(okButton);
									// getRootPane().setDefaultButton(okButton);
								}
							}
						}
					}
				}

			}
			{
				JPanel panel_1 = new JPanel();
				splitPane.setRightComponent(panel_1);
		
				{
					
					GridBagLayout gbl_panel_1 = new GridBagLayout();
					gbl_panel_1.columnWidths = new int[]{0, 0};
					gbl_panel_1.rowHeights = new int[]{0, 0, 0};
					gbl_panel_1.columnWeights = new double[]{1.0, Double.MIN_VALUE};
					gbl_panel_1.rowWeights = new double[]{1.0, 1.0, Double.MIN_VALUE};
					panel_1.setLayout(gbl_panel_1);
					{
						JPanel panela = new JPanel();
						GridBagConstraints gbc_panela = new GridBagConstraints();
						gbc_panela.fill = GridBagConstraints.BOTH;
						gbc_panela.insets = new Insets(0, 0, 5, 0);
						gbc_panela.gridx = 0;
						gbc_panela.gridy = 0;
						panel_1.add(panela, gbc_panela);
						panela.setLayout(new BorderLayout(0, 0));
						{
							JScrollPane scrollPane = new JScrollPane();
							panela.add(scrollPane);
							{
								examplePane = new JTextPane();
								examplePane.setText(theLine);
								scrollPane.setViewportView(examplePane);
							}
						}
					}
					{
						JPanel panelb = new JPanel();
						GridBagConstraints gbc_panelb = new GridBagConstraints();
						gbc_panelb.fill = GridBagConstraints.BOTH;
						gbc_panelb.gridx = 0;
						gbc_panelb.gridy = 1;
						panel_1.add(panelb, gbc_panelb);
							panelb.setLayout(new BorderLayout(0, 0));
						
						
							textPane = new JTextPane();
							JScrollPane scrollPane = new JScrollPane(textPane);
							panelb.add(scrollPane);
						
					}
				}
			}
		}
		if (re != null) {
			txtName.setText(re.getName());
			txtRegularExp.setText(re.getRegexp());
			txtDate.setText(((ReportRecording) re).getDateFormat());
			examplePane.setText(re.getExampleLine());
			comboBox.setSelectedItem(re.getSubType());
			if (DataVault.getMatchedLines(re)!=null && !DataVault.getMatchedLines(re).equals("") ){
				examplePane.setText(DataVault.getMatchedLines(re));
			}
			if (DataVault.getUnmatchedLines(re)!=null  && !DataVault.getUnmatchedLines(re).equals("") ){
				textPane.setText(DataVault.getUnmatchedLines(re));
			}
			  
		}
		
		{
			{
				if (re == null) {
					reportRecordingEditorTablePanel = new ReportRecordingEditorTable(examplePane);
					logger.debug("RecordingEditor - re=null");
				} else {
					reportRecordingEditorTablePanel = new ReportRecordingEditorTable(repo, re, examplePane);
					logger.debug("RecordingEditor - re!=null");
				}
				reportRecordingEditorTablePanel.setBackground(UIManager.getColor("Panel.background"));
				reportRecordingEditorTablePanel.setOpaque(true);
				reportRecordingEditorTablePanel.setVisible(true);
			}
		}
		JScrollPane scrollPaneEventRecordingEditorTablePanel = new JScrollPane(reportRecordingEditorTablePanel);
		panel2.add(scrollPaneEventRecordingEditorTablePanel);
		reportRecordingEditorTablePanel.FixValues();
	}
} public class MetadataRecordingEditor extends JPanel {
	private static Logger logger = Logger.getLogger(DataMiner.class.getName());
	private JPanel _this = this;
	private JPanel panel_1;
	JPanel panel_2;
	JPanel panel2;
	private final JPanel contentPanel = new JPanel();
	private JScrollPane scrollPane;
	private JTextField txtName;
	private JTextField txtRegularExp;
	private JTextField txtDate;
	private MetadataRecordingEditorTable recordingEditorTablePanel;
	private Repository repository;
	DefaultListModel listModel;
	JTextPane examplePane = new JTextPane();
	private MetadataRecording recording;
	Document doc;
	JCheckBox chckbxActive;
	JCheckBox chckbxCaseSensitive;
	Source selectedSource;

	// private JList recordingList;

	/**
	 * Create the dialog.
	 */
	public MetadataRecordingEditor(final JPanel newRecordingList, MetadataRecording re, Repository repo) {
		this(newRecordingList, repo, re.getExampleLine(), re.getRegexp(), re,null);
	}

	public MetadataRecordingEditor(Repository repo, String theLine, String regex, MetadataRecording re) {
		this(null, repo, theLine, regex, re,null);
	}

	/**
	 * Create the dialog.
	 * @param source 
	 * 
	 * @wbp.parser.constructor
	 */
	public MetadataRecordingEditor(final JPanel newRecordingList, Repository repo, String theLine, String regex,
			final MetadataRecording re, Source source) {
		// setBounds(0, 0, 1015, 467);

		repository = repo;
		recording = re;
		selectedSource = source;
		BorderLayout borderLayout = new BorderLayout();
		this.setLayout(borderLayout);
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		this.add(contentPanel, BorderLayout.CENTER);
		JPanel panel = new JPanel();
		contentPanel.setLayout(new BorderLayout(0, 0));

		JPanel panel_1_1 = new JPanel();
		{


			panel_1_1.setLayout(new BorderLayout(0, 0));
			{
				scrollPane = new JScrollPane();
				panel_1_1.add(scrollPane);
				{
					examplePane = new JTextPane();
				//	examplePane.setText(theLine);
					scrollPane.setViewportView(examplePane);
				}
			}
			
					{
						panel.setLayout(new BorderLayout(0, 0));
						panel.setMinimumSize(new Dimension(0,200));
						{
							panel_1 = new JPanel();
							panel.add(panel_1, BorderLayout.NORTH);
							panel_1.setLayout(new GridLayout(0, 3, 5, 2));
								txtName = new JTextField();
								panel_1.add(txtName);
								txtName.setText("name");
								txtName.setColumns(10);
							{
								txtRegularExp = new JTextField();
								txtRegularExp.addCaretListener(new CaretListener() {
									public void caretUpdate(CaretEvent e) {
										PatternCache patternCache= new PatternCache();
										doc = examplePane.getDocument();
			
										Pattern pattern = patternCache.getPattern(txtRegularExp.getText(),re.isCaseSensitive());
										Matcher matcher = pattern.matcher(examplePane.getText());
										Highlighter h = examplePane.getHighlighter();
										h.removeAllHighlights();
										if (matcher.find()) {
											// int currIndex=doc.getLength();
											// doc.insertString(doc.getLength(),line+"\n",
											// null);
			
											try {
												h.addHighlight(matcher.start(), matcher.end(), DefaultHighlighter.DefaultPainter);
											} catch (BadLocationException e1) {
												// TODO Auto-generated catch block
												e1.printStackTrace();
											}
			
										}
									}
								});
								panel_1.add(txtRegularExp);
								txtRegularExp.setText(regex);
								txtRegularExp.setColumns(10);
							}
							{
								JPanel panel_2_1 = new JPanel();
								panel_1.add(panel_2_1);
								panel_2_1.setLayout(new BorderLayout(0, 0));
								{
									txtDate = new JTextField();
									txtDate.setEditable(false);
									panel_2_1.add(txtDate, BorderLayout.CENTER);
									txtDate.setText("date format");
									txtDate.setColumns(10);
								}
								{
									JButton button = new JButton("...");
									button.addActionListener(new ActionListener() {
										public void actionPerformed(ActionEvent e) {
											DateSelector dateDialog = new DateSelector(repository, txtDate, re);
											dateDialog.validate();
											dateDialog.setResizable(true);
											dateDialog.setModal(false);
											dateDialog.setVisible(true);
										}
									});
									button.setFont(new Font("Dialog", Font.BOLD, 6));
									panel_2_1.add(button, BorderLayout.EAST);
								}
							}
							{
								chckbxActive = new JCheckBox("active");
								chckbxActive.setSelected(true);
								panel_1.add(chckbxActive);
							}
							{
								chckbxCaseSensitive = new JCheckBox("case sensitive");
								chckbxCaseSensitive.setSelected(re.isCaseSensitive());
								panel_1.add(chckbxCaseSensitive);
							}
						}
						{
							JPanel panel_3 = new JPanel();
							panel.add(panel_3, BorderLayout.CENTER);
							panel_3.setLayout(new BorderLayout(0, 0));
							panel2 = new JPanel();
							panel_3.add(panel2, BorderLayout.CENTER);
							panel2.setLayout(new BorderLayout(0, 0));

							{
								panel_2 = new JPanel();
								panel_3.add(panel_2, BorderLayout.SOUTH);
								FlowLayout flowLayout = (FlowLayout) panel_2.getLayout();
								flowLayout.setAlignment(FlowLayout.LEFT);
								{
									JButton btnAddButton = new JButton("Add");
									btnAddButton.addActionListener(new ActionListener() {
										public void actionPerformed(ActionEvent e) {
											recordingEditorTablePanel.Add();
										}
									});
									btnAddButton.setHorizontalAlignment(SwingConstants.LEFT);
									panel_2.add(btnAddButton);
								}
								{
									JButton btnRemoveButton = new JButton("Remove");
									btnRemoveButton.addActionListener(new ActionListener() {
										public void actionPerformed(ActionEvent e) {
											recordingEditorTablePanel.Remove();
										}
									});
									{
										JButton btnInsert = new JButton("Insert");
										btnInsert.addActionListener(new ActionListener() {
											public void actionPerformed(ActionEvent e) {
												recordingEditorTablePanel.Insert();
											}
										});
										panel_2.add(btnInsert);
									}
									btnRemoveButton.setHorizontalAlignment(SwingConstants.LEFT);
									panel_2.add(btnRemoveButton);
								}
								{
									JButton btnCheck = new JButton("Check");
									btnCheck.addActionListener(new ActionListener() {
										public void actionPerformed(ActionEvent e) {
											logger.debug("check");
											recordingEditorTablePanel.FixValues();
			
										}
									});
									panel_2.add(btnCheck);
								}
										{
											JSeparator separator = new JSeparator();
											panel_2.add(separator);
										}
								
										{
											JPanel buttonPane = new JPanel();
											buttonPane.setBorder(null);
											panel_2.add(buttonPane);
											FlowLayout fl_buttonPane = new FlowLayout(FlowLayout.CENTER);
											fl_buttonPane.setAlignOnBaseline(true);
											buttonPane.setLayout(fl_buttonPane);
											{
												JButton okButton = new JButton("Save");
												okButton.setForeground(Color.BLUE);
												okButton.addActionListener(new ActionListener() {
													public void actionPerformed(ActionEvent arg0) {
														ArrayList<RecordingItem> rIs = recordingEditorTablePanel.getRecordingItems();
														if (newRecordingList.getClass()==RecordingList.class) {
															if (recording == null){
															logger.debug("RecordingEditor - ok 1");
															Recording r = new MetadataRecording(txtName.getText(), txtRegularExp.getText(),"", txtDate.getText(), chckbxActive
																	.isSelected(), chckbxCaseSensitive.isSelected(), rIs);
															repository.addRecording(r);
															logger.debug("RecordingEditor - ok 1");
															if (newRecordingList.getClass()==RecordingList.class){
																logger.debug("RecordingEditor - ok 1");
															if (((RecordingList) newRecordingList).model != null) {
																logger.debug("RecordingEditor - ok 1");
																((RecordingList) newRecordingList).model.addRow(new Object[] { txtName.getText(), txtRegularExp.getText(), chckbxActive.isSelected() });
																((RecordingList) newRecordingList).model.fireTableDataChanged();
															
														}}} else {
															int selectedRow = ((((RecordingList) newRecordingList).table.getSelectedRow() != -1) ? ((((RecordingList) newRecordingList).table.getSelectedRow())) : -1);
															((MetadataRecording) recording).update(txtName.getText(), txtRegularExp.getText(),"", txtDate.getText(),
																	chckbxActive.isSelected(), chckbxCaseSensitive.isSelected(), rIs);
															((RecordingList) newRecordingList).model.fireTableDataChanged();
															logger.debug("RecordingEditor - row Updated");
															((RecordingList) newRecordingList).table.setRowSelectionInterval(selectedRow, selectedRow);
														}}
														else
														{
															if (recording == null){
																int selectedRow = ((((StatRecordingSelectorPanel) newRecordingList).table.getSelectedRow() != -1) ? ((StatRecordingSelectorPanel) newRecordingList).table.convertRowIndexToModel(((StatRecordingSelectorPanel) newRecordingList).table.getSelectedRow()) : -1);
															if (((MetadataRecordingSelectorPanel) newRecordingList).model != null) {
																logger.debug("RecordingEditor - ok 1");
																((MetadataRecordingSelectorPanel) newRecordingList).model.addRow(new Object[] { txtName.getText(), txtRegularExp.getText(), chckbxActive.isSelected() });
																((MetadataRecordingSelectorPanel) newRecordingList).model.fireTableDataChanged();
																((MetadataRecordingSelectorPanel) newRecordingList).table.setRowSelectionInterval(selectedRow, selectedRow);
															}}
														 else {
															int selectedRow = ((((MetadataRecordingSelectorPanel) newRecordingList).table.getSelectedRow() != -1) ? (((MetadataRecordingSelectorPanel) newRecordingList).table.getSelectedRow()) : -1);
															((MetadataRecording) recording).update(txtName.getText(), txtRegularExp.getText(), examplePane.getText(), txtDate.getText(),
																	chckbxActive.isSelected(),chckbxCaseSensitive.isSelected(), rIs);
															logger.debug("RecordingEditor - row Updated");
															((MetadataRecordingSelectorPanel) newRecordingList).model.fireTableDataChanged();
															((MetadataRecordingSelectorPanel) newRecordingList).table.setRowSelectionInterval(selectedRow, selectedRow);
														}
														}
												/*		if (contentPanel.getParent().getParent().getParent().getParent().getClass().equals(JDialog.class)) {
															((JDialog) contentPanel.getParent().getParent().getParent().getParent()).dispose();
														}*/
														//
													}
												});
												{
													JSeparator separator = new JSeparator();
													buttonPane.add(separator);
												}
												okButton.setActionCommand("OK");
												buttonPane.add(okButton);
												// getRootPane().setDefaultButton(okButton);
											}
										}
										{
											JSeparator separator = new JSeparator();
											panel_2.add(separator);
										}
							}
						}
			
					}
		}
		
			

		{
			JSplitPane splitPane = new JSplitPane();
			splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
			contentPanel.add(splitPane, BorderLayout.CENTER);
			{
				JPanel panela = new JPanel();
				splitPane.setTopComponent(panel);
			}
			{
				JPanel panelb = new JPanel();
				splitPane.setBottomComponent(panel_1_1);
			}
		}		
		
		if (re == null) {
			recordingEditorTablePanel = new MetadataRecordingEditorTable(examplePane,source);
			logger.debug("RecordingEditor - re=null");
		} else {
			txtName.setText(re.getName());
			txtRegularExp.setText(re.getRegexp());
			txtDate.setText(((MetadataRecording) re).getDateFormat());
		//	examplePane.setText(re.getExampleLine());
			scrollPane.repaint();
			panel_1.repaint();
			recordingEditorTablePanel = new MetadataRecordingEditorTable(repo, re, examplePane,source);
		//	logger.debug("RecordingEditor - re!=null examplePane: " + examplePane.getText());
		}
		recordingEditorTablePanel.setBackground(UIManager.getColor("Panel.background"));
		recordingEditorTablePanel.setOpaque(true);
		recordingEditorTablePanel.setVisible(true);
		JScrollPane scrollPaneRecordingEditorTablePanel = new JScrollPane(recordingEditorTablePanel);
		panel2.add(scrollPaneRecordingEditorTablePanel);
		recordingEditorTablePanel.FixValues();
	}
} public class StatRecordingEditor extends JPanel {
	private static Logger logger = Logger.getLogger(DataMiner.class.getName());
	private final JPanel contentPanel = new JPanel();
	private JTextField txtName;
	private JTextField txtRegularExp;
	private JTextField txtDate;
	JPanel panel2;
	private StatRecordingEditorTable recordingEditorTablePanel;
	private Repository repository;
	DefaultListModel listModel;
	JTextPane examplePane = new JTextPane();
	private StatRecording recording;
	Document doc;
	JCheckBox chckbxActive;
	JCheckBox chckbxCaseSensitive;
	JTextPane textPane;
	JCheckBox chckbxDefault;
	JButton dateFormatButton;
	// private JList recordingList;

	/**
	 * Create the dialog.
	 */
	public StatRecordingEditor(final JPanel newRecordingList, StatRecording re, Repository repo) {
		this(newRecordingList, repo, re.getExampleLine(), re.getRegexp(), re);
	}

	/**
	 * Create the dialog.
	 * 
	 * @wbp.parser.constructor
	 */
	public StatRecordingEditor(final JPanel newRecordingList, Repository repo, String theLine, String regex, final StatRecording re) {
		// setBounds(0, 0, 1015, 467);

		repository = repo;
		recording = re;

		BorderLayout borderLayout = new BorderLayout();
		this.setLayout(borderLayout);
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		this.add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));



		{
			JSplitPane splitPane = new JSplitPane();
			splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
			contentPanel.add(splitPane, BorderLayout.CENTER);
			
			JPanel topPanel = new JPanel();
			splitPane.setLeftComponent(topPanel);
			topPanel.setLayout(new BorderLayout(0, 0));
			{
				JPanel panel_1a = new JPanel();
				topPanel.add(panel_1a, BorderLayout.NORTH);
				panel_1a.setLayout(new GridLayout(0, 4, 5, 2));
					{
						JPanel panel = new JPanel();
						panel_1a.add(panel);
						panel.setLayout(new BorderLayout(0, 0));
						{
							JLabel lblName = new JLabel("Name:");
							panel.add(lblName, BorderLayout.WEST);
						}
						txtName = new JTextField();
						panel.add(txtName, BorderLayout.CENTER);
						txtName.setText("name");
						txtName.setColumns(10);
					}
				{
					{
						JPanel panel = new JPanel();
						panel_1a.add(panel);
						panel.setLayout(new BorderLayout(0, 0));
						{
							JLabel lblRegex = new JLabel("regex:");
							panel.add(lblRegex, BorderLayout.WEST);
						}
						txtRegularExp = new JTextField();
						panel.add(txtRegularExp);
						
											txtRegularExp.addCaretListener(new CaretListener() {
												public void caretUpdate(CaretEvent e) {
													PatternCache patternCache= new PatternCache();
													doc = examplePane.getDocument();
													Highlighter h = examplePane.getHighlighter();
													h.removeAllHighlights();
													
													Pattern pattern = patternCache.getPattern(txtRegularExp.getText(),re.isCaseSensitive());
													String[] lines = examplePane.getText().split(System.getProperty("line.separator"));
													int currIndex = 0;
													if (lines.length>=1){
													for (int i=0; i<lines.length ; i++)
													{
														logger.debug("line: "+lines[i]);
														
														Matcher matcher = pattern.matcher(lines[i]);
													if (matcher.find()) {
														// int currIndex=doc.getLength();
														// doc.insertString(doc.getLength(),line+"\n",
														// null);
						
														try {
															h.addHighlight(currIndex+matcher.start(), currIndex+matcher.end(), DefaultHighlighter.DefaultPainter);
														} catch (BadLocationException e1) {
															// TODO Auto-generated catch block
															e1.printStackTrace();
														}
													}
													currIndex +=lines[i].length()+1 ;
													}
													
													}
												}
											});
											txtRegularExp.setText(regex);
											txtRegularExp.setColumns(10);
					}
				}
				{
					JPanel panel = new JPanel();
					panel.setMinimumSize(new Dimension(200, 20));
					panel_1a.add(panel);
					panel.setLayout(new BorderLayout(0, 0));
					{
						JLabel lblDateFormat = new JLabel("Date:");
						panel.add(lblDateFormat, BorderLayout.WEST);
					}
					{
						JPanel panel_2 = new JPanel();
						panel_2.setMinimumSize(new Dimension(50, 20));
						panel.add(panel_2, BorderLayout.CENTER);
						panel_2.setLayout(new BorderLayout(0, 0));
						{
							txtDate = new JTextField();
							txtDate.setEnabled(false);
							txtDate.setColumns(10);
							txtDate.setEditable(false);
							txtDate.setMinimumSize(new Dimension(50, 20));
							panel_2.add(txtDate, BorderLayout.CENTER);
							txtDate.setText("date format");
						}
						{
							dateFormatButton = new JButton("...");
							dateFormatButton.setEnabled(false);
							dateFormatButton.addActionListener(new ActionListener() {
								public void actionPerformed(ActionEvent e) {
									DateSelector dateDialog = new DateSelector(repository, txtDate, (Recording) re);
									dateDialog.validate();
									dateDialog.setResizable(true);
									dateDialog.setModal(false);
									dateDialog.setVisible(true);
								}
							});
							dateFormatButton.setFont(new Font("Dialog", Font.BOLD, 6));
							panel_2.add(dateFormatButton, BorderLayout.EAST);
						}
					}
				}
				{
					chckbxDefault = new JCheckBox("default date format");
					chckbxDefault.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent e) {
							if (chckbxDefault.isSelected()){
								dateFormatButton.setEnabled(false);
								txtDate.setEnabled(false);
							}else {
								dateFormatButton.setEnabled(true);
								txtDate.setEnabled(true);
							}
						}
					});
					panel_1a.add(chckbxDefault);
					chckbxDefault.setSelected(re.getUseSourceDateFormat());
					dateFormatButton.setEnabled(!re.getUseSourceDateFormat());
					txtDate.setEnabled(!re.getUseSourceDateFormat());
				}
				{
					chckbxActive = new JCheckBox("active");
					chckbxActive.setSelected(re.getIsActive());
					panel_1a.add(chckbxActive);
				}
				{
					chckbxCaseSensitive = new JCheckBox("case sensitive");
					chckbxCaseSensitive.setSelected(re.isCaseSensitive());
					panel_1a.add(chckbxCaseSensitive);
				}
				{
					JCheckBox chckbxMultiLine = new JCheckBox("multi line");
					chckbxMultiLine.setEnabled(false);
					panel_1a.add(chckbxMultiLine);
				}
			}
			panel2 = new JPanel();
			panel2.setPreferredSize(new Dimension(0,180));
			topPanel.add(panel2, BorderLayout.CENTER);
			panel2.setLayout(new BorderLayout(0, 0));
			examplePane = new JTextPane();
			textPane = new JTextPane();
			if (re != null) {
				txtName.setText(re.getName());
				txtRegularExp.setText(re.getRegexp());
				txtDate.setText(((StatRecording) re).getDateFormat());
				examplePane.setText(re.getExampleLine());
				logger.debug("examplePane.setText with recording re.getExampleLine(): "+re.getExampleLine());
				if (DataVault.getMatchedLines(re)!=null && !DataVault.getMatchedLines(re).equals("")){
					examplePane.setText(DataVault.getMatchedLines(re));
					logger.debug("examplePane.setText with DataVault.getMatchedLines(re): "+DataVault.getMatchedLines(re));
				}
				if (DataVault.getUnmatchedLines(re)!=null && !DataVault.getUnmatchedLines(re).equals("")){
					textPane.setText(DataVault.getUnmatchedLines(re));
					logger.debug("examplePane.setText with DataVault.getUnmatchedLines(re): "+DataVault.getUnmatchedLines(re));
				}
			}
							{
								JPanel panel_2 = new JPanel();
								topPanel.add(panel_2, BorderLayout.SOUTH);
								FlowLayout flowLayout = (FlowLayout) panel_2.getLayout();
								flowLayout.setAlignment(FlowLayout.LEFT);
								{
									JButton btnAddButton = new JButton("Add");
									btnAddButton.addActionListener(new ActionListener() {
										public void actionPerformed(ActionEvent e) {
											recordingEditorTablePanel.Add();
										}
									});
									btnAddButton.setHorizontalAlignment(SwingConstants.LEFT);
									panel_2.add(btnAddButton);
								}
								{
									JButton btnRemoveButton = new JButton("Remove");
									btnRemoveButton.addActionListener(new ActionListener() {
										public void actionPerformed(ActionEvent e) {
											recordingEditorTablePanel.Remove();
										}
									});
									{
										JButton btnInsert = new JButton("Insert");
										btnInsert.addActionListener(new ActionListener() {
											public void actionPerformed(ActionEvent e) {
												recordingEditorTablePanel.Insert();
											}
										});
										panel_2.add(btnInsert);
									}
									btnRemoveButton.setHorizontalAlignment(SwingConstants.LEFT);
									panel_2.add(btnRemoveButton);
								}
								{
									JButton btnCheck = new JButton("Check");
									btnCheck.addActionListener(new ActionListener() {
										public void actionPerformed(ActionEvent e) {
											logger.debug("check");
											recordingEditorTablePanel.FixValues();
			
										}
									});
									panel_2.add(btnCheck);
								}
								{
									JPanel buttonPane = new JPanel();
									panel_2.add(buttonPane);
									buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));
									{
										JButton okButton = new JButton("Save");
										okButton.setForeground(Color.BLUE);
										okButton.addActionListener(new ActionListener() {
											public void actionPerformed(ActionEvent arg0) {
												ArrayList<RecordingItem> rIs = recordingEditorTablePanel.getRecordingItems();
												if (newRecordingList.getClass()==RecordingList.class) {
													if (recording == null){
													logger.debug("RecordingEditor - ok 1");
													Recording r = new StatRecording(txtName.getText(), txtRegularExp.getText(), examplePane.getText(), txtDate.getText(), chckbxActive
															.isSelected(),chckbxDefault.isSelected(),chckbxCaseSensitive.isSelected(), rIs);
													repository.addRecording(r);
													logger.debug("RecordingEditor - ok 1");
													if (newRecordingList.getClass()==RecordingList.class){
														logger.debug("RecordingEditor - ok 1");
													if (((RecordingList) newRecordingList).model != null) {
														logger.debug("RecordingEditor - ok 1");
														((RecordingList) newRecordingList).model.addRow(new Object[] { txtName.getText(), txtRegularExp.getText(), chckbxActive.isSelected() });
														((RecordingList) newRecordingList).model.fireTableDataChanged();
													
												}}} else {
													int selectedRow = ((((RecordingList) newRecordingList).table.getSelectedRow() != -1) ? ((((RecordingList) newRecordingList).table.getSelectedRow())) : -1);
													((StatRecording) recording).update(txtName.getText(), txtRegularExp.getText(), examplePane.getText(), txtDate.getText(),
															chckbxActive.isSelected(),chckbxDefault.isSelected(),chckbxCaseSensitive.isSelected(), rIs);
													((RecordingList) newRecordingList).model.fireTableDataChanged();
													logger.debug("RecordingEditor - row Updated");
													((RecordingList) newRecordingList).table.setRowSelectionInterval(selectedRow, selectedRow);
												}}
												else
												{
													if (recording == null){
														int selectedRow = ((((StatRecordingSelectorPanel) newRecordingList).table.getSelectedRow() != -1) ? ((StatRecordingSelectorPanel) newRecordingList).table.convertRowIndexToModel(((StatRecordingSelectorPanel) newRecordingList).table.getSelectedRow()) : -1);
													if (((StatRecordingSelectorPanel) newRecordingList).model != null) {
														logger.debug("RecordingEditor - ok 1");
														((StatRecordingSelectorPanel) newRecordingList).model.addRow(new Object[] { txtName.getText(), txtRegularExp.getText(), chckbxActive.isSelected() });
														((StatRecordingSelectorPanel) newRecordingList).model.fireTableDataChanged();
														((StatRecordingSelectorPanel) newRecordingList).table.setRowSelectionInterval(selectedRow, selectedRow);
													}}
												 else {
													int selectedRow = ((((StatRecordingSelectorPanel) newRecordingList).table.getSelectedRow() != -1) ? ((((StatRecordingSelectorPanel) newRecordingList).table.getSelectedRow())) : -1);
													((StatRecording) recording).update(txtName.getText(), txtRegularExp.getText(), examplePane.getText(), txtDate.getText(),
															chckbxActive.isSelected(),chckbxDefault.isSelected(), chckbxCaseSensitive.isSelected(), rIs);
													logger.debug("RecordingEditor - row Updated");
													((StatRecordingSelectorPanel) newRecordingList).model.fireTableDataChanged();
													((StatRecordingSelectorPanel) newRecordingList).table.setRowSelectionInterval(selectedRow, selectedRow);
												}
												}
			
			/*						if (contentPanel.getParent().getParent().getParent().getParent().getClass().equals(JDialog.class)) {
													((JDialog) contentPanel.getParent().getParent().getParent().getParent()).dispose();
												}*/
												//
											}
										});
										{
											JSeparator separator = new JSeparator();
											buttonPane.add(separator);
										}
										okButton.setActionCommand("OK");
										buttonPane.add(okButton);
								//		 getRootPane().setDefaultButton(okButton);
									}
								}
							}
			{
				JPanel bottomPanel = new JPanel();
				splitPane.setRightComponent(bottomPanel);
				bottomPanel.setLayout(new BorderLayout(0, 0));
				{
					JPanel panel_1 = new JPanel();
					bottomPanel.add(panel_1);
					GridBagLayout gbl_panel_1 = new GridBagLayout();
					gbl_panel_1.columnWidths = new int[]{0, 0};
					gbl_panel_1.rowHeights = new int[]{0, 0, 0};
					gbl_panel_1.columnWeights = new double[]{1.0, Double.MIN_VALUE};
					gbl_panel_1.rowWeights = new double[]{1.0, 1.0, Double.MIN_VALUE};
					panel_1.setLayout(gbl_panel_1);
					{
						JPanel panelB = new JPanel();
						GridBagConstraints gbc_panelB = new GridBagConstraints();
						gbc_panelB.fill = GridBagConstraints.BOTH;
						gbc_panelB.insets = new Insets(0, 0, 5, 0);
						gbc_panelB.gridx = 0;
						gbc_panelB.gridy = 0;
						panel_1.add(panelB, gbc_panelB);
						panelB.setLayout(new BorderLayout(0, 0));
						{
							JScrollPane scrollPane = new JScrollPane();
							panelB.add(scrollPane);
							{

								examplePane.setText(theLine);
								scrollPane.setViewportView(examplePane);
							}
						}
					}
					{
						JPanel panelA = new JPanel();
						GridBagConstraints gbc_panelA = new GridBagConstraints();
						gbc_panelA.fill = GridBagConstraints.BOTH;
						gbc_panelA.gridx = 0;
						gbc_panelA.gridy = 1;
						panel_1.add(panelA, gbc_panelA);
						panelA.setLayout(new BorderLayout(0, 0));

						JScrollPane scrollPane = new JScrollPane(textPane);
						panelA.add(scrollPane);
						
					}
				}
			}
		}
		{
			{
				if (re == null) {
					recordingEditorTablePanel = new StatRecordingEditorTable(examplePane);
					logger.debug("RecordingEditor - re=null");
				} else {
					recordingEditorTablePanel = new StatRecordingEditorTable(repo, re, examplePane);
					logger.debug("RecordingEditor - re!=null");
				}
				recordingEditorTablePanel.setBackground(UIManager.getColor("Panel.background"));
				recordingEditorTablePanel.setOpaque(true);
				recordingEditorTablePanel.setVisible(true);
			}
		}
		JScrollPane scrollPaneRecordingEditorTablePanel = new JScrollPane(recordingEditorTablePanel);
		panel2.add(scrollPaneRecordingEditorTablePanel);
		recordingEditorTablePanel.FixValues();
	}

	public StatRecordingEditor(Repository repository2, String exampleLine, String regexp, StatRecording statRecording) {
		this(null, repository2, exampleLine, regexp, statRecording);
	}
} public  class MyTableModel2 extends AbstractTableModel {
	private String[] header;
	 private Vector data = new Vector();
		 
	public MyTableModel2(Vector<Object[]> data, String[] header) {
		this.header = header;
		this.data = data;
	}

	@Override
	public int getColumnCount() {
		if (header == null) {
			return 0;
		} else
			return header.length;
	}

	@Override
	public String getColumnName(int column) {
		return header[column];
	}

	@Override
	public int getRowCount() {
		return repository.getRecordingCount();
	}
		
	public void addRow(Object[] obj) {
		data.add(obj);
	}

	public void updateRow(int rowId, Object[] obj) {
		data.set(rowId, obj);
	}

	@Override
	public Object getValueAt(int row, int column) {
		if (column == 0 ) {
			return repository.getRecording(row).getName();
		} else if (column == 1 ) {
			return repository.getRecording(row).getRegexp();
		} else if (column == 2 ) {
			return repository.getRecording(row).getType();
		} else if (column == 3 ) {
			logger.debug("setValueAt calls setActiveRecording");
			return repository.getRecording(row).getIsActive();
		} else if (column >3 && column<9) {
			stats = DataVault.getRecordingStats(repository.getRecording(row).getName());
			if (stats != null) {
				if (Preferences.getPreference("timings").equals("false") && Preferences.getPreference("matches").equals("true") ) {
					return stats[column - 2];
				} else {
					return stats[column - 4];
					} 
			} else return 0;
		}
		else return 0;

	}

	@Override
	public void setValueAt(Object value, int row, int column) {
		
		if (column == 3 ) {
			logger.debug("setValueAt calls setActiveRecording");
			repository.getRecording(row).setIsActive((Boolean)value);
			//.toggleActiveRecording(repository.getRecording(MetadataRecording.class, row));
			fireTableCellUpdated(row, column);
			// logger.debug("control of setValueAt: "+source.isActiveRecordingOnSource(repository.getRecording(MetadataRecording.class,
			// row)));
		} else {
			((Object[]) data.get(row))[column] = value;
			//	logger.debug("setValueAt"+row+","+column);
				fireTableCellUpdated(row, column);
		}
	}

	/*
	 * JTable uses this method to determine the default renderer/ editor for
	 * each cell. If we didn't implement this method, then the last column
	 * would contain text ("true"/"false"), rather than a check box.
	 */
	public Class getColumnClass(int c) {
		return getValueAt(0, c).getClass();
	}

	/*
	 * Don't need to implement this method unless your table's editable.
	 */
	public boolean isCellEditable(int row, int col) {
		// Note that the data/cell address is constant,
		// no matt&er where the cell appears onscreen.
		if (col != 3) {
			return false;
		} else {
			return true;
		}
	}

		
	/*
	 * Don't need to implement this method unless your table's data can
	 * change.
	 */
	/*
	 * public void setValueAt(Object value, int row, int col) { if (DEBUG) {
	 * logger.debug("Setting value at " + row + "," + col + " to " + value +
	 * " (an instance of " + value.getClass() + ")"); }
	 * 
	 * data[row][col] = value; fireTableCellUpdated(row, col);
	 * 
	 * if (DEBUG) { logger.debug("New value of data:"); printDebugData(); } }
	 * 
	 * private void printDebugData() { int numRows = getRowCount(); int
	 * numCols = getColumnCount();
	 * 
	 * for (int i=0; i < numRows; i++) { System.out.print("    row " + i +
	 * ":"); for (int j=0; j < numCols; j++) { System.out.print("  " +
	 * data[i][j]); } logger.debug(); }
	 * logger.debug("--------------------------"); }
	 */
}
 public class DataMiner {
	private static Logger logger = Logger.getLogger(DataMiner.class.getName());
	static List<File> listOfFiles = null;
	private static ExecutorService ThreadPool_FileWorkers = null;
	private static ExecutorService ThreadPool_GroupWorkers = null;
	static long estimatedTime = 0;
	static long startTime = 0;
	static final Map<Source, Map<Recording, Map<List<Object>, Long>>> occurenceReport = new ConcurrentHashMap<Source, Map<Recording, Map<List<Object>, Long>>>();
	static final Map<Source, Map<Recording, Map<List<Object>, Double>>> sumReport = new ConcurrentHashMap<Source, Map<Recording, Map<List<Object>, Double>>>();
	static final Map<Source, Map<Recording, SortedMap<Double,List<Object>>>> top100Report = new ConcurrentHashMap<Source, Map<Recording, SortedMap<Double,List<Object>>>>();;
	
	public static MineResultSet gatherMsineResultSet(ChartData cd, final Repository repo, final MainFrame mainFrame) {
		String test = Preferences.getPreference("ThreadPool_Group");
		int ini = Integer.parseInt(test);
		logger.info("gatherMineResultSet parallelism: " + ini);
		ThreadPool_GroupWorkers = Executors.newFixedThreadPool(ini);
	//	ChartData cd = new ChartData();
		Collection<Callable<MineResult>> tasks = new ArrayList<Callable<MineResult>>();
		MineResultSet mineResultSet = new MineResultSet();
		occurenceReport.clear();
		sumReport.clear();
		top100Report.clear();
		// tOP100Report = new ConcurrentHashMap<Recording,Map<String, Long>>();

		startTime = System.currentTimeMillis();
	/*	try {
			cd = gatherSourceData(repo);
		} catch (Exception e) {
			return null;
		}*/
		
		// if (logger.isEnabledFor(Level.INFO))
		// logger.info("ArrayList sourceFileGroup" + sourceFileGroup);
		Iterator<Source> sourceIterator2 = repo.getSources().iterator();
		int progressCount = 0;
		while (sourceIterator2.hasNext()) {
			final Source source = sourceIterator2.next();
			// sourceFiles contains all the matched files for a given source
			if (source.getActive() && source.getActiveMetadata()!=null) {
				Iterator<Entry<String, ArrayList<FileRecord>>> it = cd.getGroupFilesMap(source).entrySet().iterator();
				while (it.hasNext()) {
					final Map.Entry<String, ArrayList<FileRecord>> pairs = it.next();
					progressCount = progressCount + pairs.getValue().size();
					logger.debug("Source:" + source.getSourceName() + ", group: " + pairs.getKey() + " = " + pairs.getValue().toString());
					tasks.add(new Callable<MineResult>() {
						public MineResult call() throws Exception {
							return DataMiner.mine(pairs.getKey(), pairs.getValue(), repo, source, Preferences.isStats(),
									Preferences.isTimings(), Preferences.isMatches(), mainFrame);
						}

					});

				}
			}
		}
		mainFrame.setMaxProgress(progressCount);
		// logger.info("progressCount "+ progressCount);
		/*
		 * invokeAll blocks until all service requests complete, or a max of
		 * 1000 seconds.
		 */
		List<Future<MineResult>> results = null;
		try {
			results = ThreadPool_GroupWorkers.invokeAll(tasks, 100000, TimeUnit.SECONDS);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for (Future<MineResult> f : results) {
			MineResult mineRes = null;
			try {
				// if (mineRes!=null)
				mineRes = f.get();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (mineRes != null) {
				mineResultSet.updateStartDate(mineRes.getStartDate());
				mineResultSet.updateEndDate(mineRes.getEndDate());
				if (!mineResultSet.mineResults.keySet().contains(mineRes.getSource())) {
					mineResultSet.mineResults.put(mineRes.getSource(), new HashMap<String, MineResult>());
				}
				mineResultSet.mineResults.get(mineRes.getSource()).put(mineRes.getSource().getSourceName() + mineRes.getGroup(), mineRes);
			}
		}
		estimatedTime = System.currentTimeMillis() - startTime;
		logger.info("gathering time: " + estimatedTime);
		/*
		 * Iterator oRIte= occurenceReport.keySet().iterator(); while
		 * (oRIte.hasNext()) { String occString=(String) oRIte.next();
		 * logger.info("nb: "+ occurenceReport.get(occString)+" string: "
		 * +occString); }
		 */
		mineResultSet.setOccurenceReport(occurenceReport);
		mineResultSet.setTop100Report(top100Report);
		mineResultSet.setSumReport(sumReport);	

		return mineResultSet;

	}
	
	// handle gathering for ArrayList of file for one source-group
	public static MineResult mine(String group, ArrayList<FileRecord> arrayList, final Repository repo, final Source source, final boolean stats, final boolean timings,
			final boolean matches, final MainFrame mainFrame) {
		logger.debug("call to mine for source " + source.getSourceName() + " on group " + group);
		ThreadPool_FileWorkers = Executors.newFixedThreadPool(Integer.parseInt(Preferences.getPreference("ThreadPool_File")));
		Date startDate = null;
		Date endDate = null;
		// Map<String, ExtendedTimeSeries> statMap = HashObjObjMaps<String,
		// ExtendedTimeSeries>();
		Map<String, ExtendedTimeSeries> statMap = new HashMap<String, ExtendedTimeSeries>();
		Map<String, ExtendedTimeSeries> eventMap = new HashMap<String, ExtendedTimeSeries>();
		Map<String, long[]> timingStatsMap = new HashMap<String, long[]>();
		Map<String, Map<Date, FileLine>> fileLine = new HashMap<String, Map<Date, FileLine>>();
		Collection<Callable<FileMineResult>> tasks = new ArrayList<Callable<FileMineResult>>();
		
		final Map<Recording, String> recMatch1 = getAllRegexSingleMap(repo, source); 

		ArrayList<Object> mapArrayList;
		mapArrayList = new ArrayList<>();
		if (logger.isEnabledFor(Level.INFO))
			logger.info("mine called on " + source.getSourceName());
		Iterator<FileRecord> fileArrayListIterator = arrayList.iterator();
		while (fileArrayListIterator.hasNext()) {
			final FileRecord fileRec = fileArrayListIterator.next();
			tasks.add(new Callable<FileMineResult>() {
				public FileMineResult call() throws Exception {
					logger.debug("file mine on " + fileRec);
					return fileMine(fileRec, recMatch1, repo, source, stats, timings, matches, mainFrame);
				}

			});

		}
		List<Future<FileMineResult>> results = null;
		try {
			results = ThreadPool_FileWorkers.invokeAll(tasks, 100000, TimeUnit.SECONDS);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for (Future<FileMineResult> f : results) {
			FileMineResult fileMineRes = null;
			try {
				if (f != null) {
					fileMineRes = f.get();
				}
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NullPointerException e) {
				// TODO Auto-generated catch block
				// e.printStackTrace();
			}
			if (fileMineRes != null) {
				mapArrayList.add(fileMineRes);
			}

		}
		ArrayList<Object[]> fileDates = new ArrayList<Object[]>();
		Iterator<Object> mapArrayListIterator = mapArrayList.iterator();
		while (mapArrayListIterator.hasNext()) {
			FileMineResult fMR = (FileMineResult) mapArrayListIterator.next();

			if (startDate == null) {
				startDate = fMR.getStartDate();
			}
			if (endDate == null) {
				endDate = fMR.getEndDate();
			}
			if (fMR.getEndDate() != null && fMR.getStartDate() != null) {
				if (fMR.getEndDate().after(endDate)) {
					endDate = fMR.getEndDate();
				} else if (fMR.getStartDate().before(startDate)) {
					startDate = fMR.getStartDate();
				}
				if (logger.isDebugEnabled()) {
					logger.debug("1: " + fMR.getStartDate() + "2: " + fMR.getEndDate() + "3: " + fMR.getFile());
				}
				fileDates.add(new Object[] { fMR.getStartDate(), fMR.getEndDate(), fMR.getFile() });
			}

			Map<String, ExtendedTimeSeries> tempStatMap = fMR.statGroupTimeSeries;
			tempStatMap.entrySet();
			Iterator it = tempStatMap.entrySet().iterator();
			while (it.hasNext()) {
				Map.Entry<String, ExtendedTimeSeries> pairs = (Map.Entry<String, ExtendedTimeSeries>) it.next();
				if (!statMap.containsKey(pairs.getKey())) {
					statMap.put(pairs.getKey(), pairs.getValue());
				} else {
					ExtendedTimeSeries ts = statMap.get(pairs.getKey());
					if (stats) {
						int[] array = { pairs.getValue().getStat()[0] + ts.getStat()[0], pairs.getValue().getStat()[1] + ts.getStat()[1] };
						ts.setStat(array);
					}
					ts.getTimeSeries().addAndOrUpdate(pairs.getValue().getTimeSeries());
					statMap.put(pairs.getKey(), ts);
					// logger.info(pairs.getKey());
				}
			}

			Map tempEventMap = fMR.eventGroupTimeSeries;
			tempEventMap.entrySet();
			it = tempEventMap.entrySet().iterator();
			while (it.hasNext()) {
				Map.Entry<String, ExtendedTimeSeries> pairs = (Map.Entry<String, ExtendedTimeSeries>) it.next();
				if (!eventMap.containsKey(pairs.getKey())) {
					eventMap.put(pairs.getKey(), pairs.getValue());
				} else {
					ExtendedTimeSeries ts = eventMap.get(pairs.getKey());
					if (stats) {
						int[] array = { pairs.getValue().getStat()[0] + ts.getStat()[0], pairs.getValue().getStat()[1] + ts.getStat()[1] };
						ts.setStat(array);
					}
					ts.getTimeSeries().addAndOrUpdate(pairs.getValue().getTimeSeries());
					eventMap.put(pairs.getKey(), ts);
				}
			}

			it = fMR.matchingStats.entrySet().iterator();
			while (it.hasNext()) {
				Map.Entry<String, long[]> pairs = (Map.Entry<String, long[]>) it.next();
				if (!timingStatsMap.containsKey(pairs.getKey())) {
					timingStatsMap.put(pairs.getKey(), pairs.getValue());
				} else {
					long[] array = timingStatsMap.get(pairs.getKey());
					// 0-> sum of time for success matching of given
					// recording ; 1-> sum of time for failed
					// matching ; 2-> count of match attempts,
					// 3->count of success attempts
					long[] array2 = { pairs.getValue()[0] + array[0], pairs.getValue()[1] + array[1], pairs.getValue()[2] + array[2],
							pairs.getValue()[3] + array[3] };
					timingStatsMap.put(pairs.getKey(), array2);
				}
			}
			it = fMR.fileLineDateMap.entrySet().iterator();
			while (it.hasNext()) {
				Map.Entry<String, Map<Date, FileLine>> pairs = (Map.Entry<String, Map<Date, FileLine>>) it.next();
				if (logger.isDebugEnabled()) {
					logger.debug("Entry<String,Map<Date, FileLine>> : " + pairs);
				}
				if (!fileLine.containsKey(pairs.getKey())) {
					fileLine.put(pairs.getKey(), pairs.getValue());
					if (logger.isDebugEnabled()) {
						logger.debug("groupFileLineMap.put " + pairs.getKey() + " -> " + pairs.getValue());
					}
				} else {
					Map<Date, FileLine> ts = fileLine.get(pairs.getKey());
					Map<Date, FileLine> newDateFileLineEntries = pairs.getValue();
					Iterator it2 = newDateFileLineEntries.entrySet().iterator();
					while (it2.hasNext()) {
						Map.Entry<Date, FileLine> pairs2 = (Map.Entry<Date, FileLine>) it2.next();
						fileLine.get(pairs.getKey()).put(pairs2.getKey(), pairs2.getValue());
						if (logger.isDebugEnabled()) {
							logger.debug("groupFileLineMap.put " + pairs2.getKey() + " -> " + pairs2.getValue().getFileId() + ":" + pairs2.getValue().getLineNumber());
						}
					}
					// logger.info("cont2: "+groupFileLineMap.get(pairs.getKey()));
				}

			}

		}
		
		FileMineResultSet fMRS = new FileMineResultSet(fileDates, statMap, eventMap, timingStatsMap, fileLine, startDate, endDate);
		return new MineResult(group, fMRS, arrayList, repo, source);
	}

	public static String readFileLine(Source src, FileLine fileLine, ChartData cd) {
		FileReader flstr = null;
		String line = "";
		FileRecord fileRecord = cd.sourceFileArrayListMap.get(src).get(fileLine.getFileId());
		try {
			flstr = new FileReader(fileRecord.getCompletePath());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		BufferedReader r = new BufferedReader(flstr);
		for (int i = 0; i < fileLine.getLineNumber(); i++) {
			try {
				line = r.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return line;

	}

	// handle gathering for a single file
	public static FileMineResult fileMine(FileRecord fileRecord, Map<Recording, String> recMatch1, Repository repo, Source source, boolean stats, boolean timings, boolean matches,
			final MainFrame mainFrame) {
		ExtendedTimeSeries ts = null;
		PatternCache patternCache = new PatternCache();
		ClassCache classCache = new ClassCache();
		Date startDate = null;
		Date endDate = null;
		DecimalFormat decimalFormat = new DecimalFormat("#.#", new DecimalFormatSymbols(Locale.US));
		java.text.DateFormat fastDateFormat = null;
		FileReader flstr = null;
		BufferedReader buf1st;
		Matcher matcher;
		Matcher matcher2;
		FixedMillisecond fMS = null;
		DateFormat df = null;
		int statHit = 0;
		int statMatch = 0;
		int eventHit = 0;
		int eventMatch = 0;
		long[] arrayBefore;
		long match0 = 0;
		long match1 = 0;
		long timing0 = 0;
		long timing1 = 0;
		Map<Recording, String> recMatch = new HashMap<Recording, String>(recMatch1);
		Map<String, ExtendedTimeSeries> statMap = new HashMap<String, ExtendedTimeSeries>();
		Map<String, ExtendedTimeSeries> eventMap = new HashMap<String, ExtendedTimeSeries>();
		Map<String, Map<Date, FileLine>> RIFileLineDateMap = new HashMap<String, Map<Date, FileLine>>();
		Map<String, long[]> matchTimings = new HashMap<String, long[]>();
		boolean gatherStats = Preferences.getBooleanPreference("gatherstats");
		boolean gatherReports = Preferences.getBooleanPreference("gatherreports");
		boolean gatherEvents = Preferences.getBooleanPreference("gatherevents");
		long recordingMatchStart = 0;
		long recordingMatchEnd = 0;
		try {
			if (logger.isDebugEnabled()) {
				logger.debug("++file: " + repo.getBaseSourcePath() + " + " + (String) fileRecord.getCompletePath().toString());
			}
			flstr = new FileReader(fileRecord.getCompletePath());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

		if (!occurenceReport.containsKey(source)) {
			occurenceReport.put(source, new ConcurrentHashMap<Recording, Map<List<Object>, Long>>());
		}
		if (!top100Report.containsKey(source)) {
			top100Report.put(source, new ConcurrentHashMap<Recording, SortedMap<Double,List<Object>>>());
		}
		
		if (!sumReport.containsKey(source)) {
			sumReport.put(source, new ConcurrentHashMap<Recording, Map<List<Object>, Double>>());
		}
		
		buf1st = new BufferedReader(flstr);
		String line;
		try {
			//recMatch = getRegexp(repo, source);
			int lineCount = 1;
			while ((line = buf1st.readLine()) != null) {
				// check against one Recording pattern at a tim
				// if (logger.isDebugEnabled()) {
				// logger.debug("line " + line);
				// }
				Iterator<Entry<Recording, String>> recMatchIte = recMatch.entrySet().iterator();
				while (recMatchIte.hasNext()) {
					if (timings) {
						recordingMatchStart = ManagementFactory.getThreadMXBean().getCurrentThreadCpuTime();
					}
					Entry<Recording, String> me = recMatchIte.next();
					Recording rec = (Recording) me.getKey();
					matcher = patternCache.getMatcher((String) (rec.getRegexp()),rec.isCaseSensitive(),line);
					if (matcher.find()) {
						Boolean isStatRecording = classCache.getClass(rec).equals(StatRecording.class);
						if (stats) {
							if (isStatRecording) {
								statMatch++;
							} else {
								eventMatch++;
							}
						}
						// logger.info("1**** matched: " + line);
						ArrayList<RecordingItem> recordingItem = ((Recording) rec).getRecordingItem();
						matcher2 = patternCache.getMatcher((String) me.getValue(),rec.isCaseSensitive(),line);
						if (matcher2.find()) {
							if (stats) {
								if (isStatRecording) {
									statHit++;
								} else {
									eventHit++;
								}
							}
							if (!classCache.getClass(rec).equals(ReportRecording.class)) {
								int count = 1;
								Date date1 = null;
								// handling capture for each recording item
								Iterator<RecordingItem> recItemIte2 = recordingItem.iterator();
								while (recItemIte2.hasNext()) {
									RecordingItem recItem2 = recItemIte2.next();
									// logger.info("3A**** " +
									// recItem2.getType());
									if (recItem2.getType().equals("date")) {
										try {
											df = repo.getDateFormat(rec.getDateFormatID());
											if (logger.isDebugEnabled())
												logger.debug("4**** rec name" + rec.getName() + " df: " + df.getId());
											fastDateFormat =ThreadLocalDateFormatMap.getInstance().createSimpleDateFormat(df.getDateFormat());
										//	fastDateFormat = FastDateFormat.getInstance(df.getDateFormat());
											date1 = fastDateFormat.parse(matcher2.group(count));
											if (logger.isDebugEnabled())
												logger.debug("4b**** " + df.getDateFormat() + " date: " + date1.toString());
											// logger.info("4**** " +
											// date1.toString());
										} catch (ParseException e) {
											// TODO Auto-generated catch
											// block
											e.printStackTrace();
										}
									} else if (date1 != null) {
										if (recItem2.isSelected()) {
											if (logger.isDebugEnabled()) {
												logger.debug("FileRecord: " + fileRecord.getFile().getName() + ", Source: " + source.getSourceName() + ", "
														+ recItem2.getName() + ", " + fileRecord.getFile().getName() + ", " + lineCount);
											}
											// recording line of match in file
											// in map RIFileLineDateMap - note
											// the FileLine object use an int to
											// identify the files to save memory
											Map<Date, FileLine> dateFileLineMap = null;
											//change this to recItem2 to differentiate recording items with same name ?? TBD
											//if (RIFileLineDateMap.containsKey(recItem2.getName())) {
												dateFileLineMap = RIFileLineDateMap.get(recItem2.getName());
												if (dateFileLineMap==null){
													dateFileLineMap = new HashMap<Date, FileLine>();
												}
													dateFileLineMap.put(date1, new FileLine(fileRecord.getId(), lineCount));
											if (logger.isDebugEnabled()) {
												logger.debug(fileRecord.getFile().getName() + " dateFileLineMap put: " + date1 + "groupFileLineMap: "
														+ new FileLine(fileRecord.getId(), lineCount));
												logger.debug(fileRecord.getFile().getName() + " FileRecord: " + fileRecord.getFile().getName()
														+ ", RIFileLineDateMap.put: " + recItem2.getName() + ", line: " + lineCount
														+ " RIFileLineDateMap size: " + RIFileLineDateMap.size() + " dateFileLineMap size: "
														+ dateFileLineMap.size());
											}
											RIFileLineDateMap.put(recItem2.getName(), dateFileLineMap);

											if (startDate == null) {
												startDate = date1;
											}
											if (endDate == null) {
												endDate = date1;
											}
											if (date1.after(startDate)) {
												endDate = date1;
											} else if (date1.before(startDate)) {
												startDate = date1;
											}

											if (isStatRecording && (gatherStats)) {
													ts = statMap.get(recItem2.getName());
												 if (ts==null) {
													ts = new ExtendedTimeSeries(recItem2, FixedMillisecond.class);
													if (logger.isDebugEnabled())
														logger.debug("5**** Adding record to Map: " + recItem2.getName());
												}
												fMS = new FixedMillisecond(date1);
												if (matcher2.group(count) == null) {
													logger.info("null in match on " + recItem2.getName() + " at " + fileRecord.getFile().getName()
															+ " line cnt:" + lineCount);
													logger.info("line : " + line);
												}
												if (recItem2.getType().equals("long")) {
													ts.getTimeSeries().addOrUpdate((new TimeSeriesDataItem(fMS, Long.valueOf((String) matcher2.group(count)))));
												} else {
													try {
														ts.getTimeSeries().addOrUpdate(
																(new TimeSeriesDataItem(fMS, Double.parseDouble(String.valueOf(decimalFormat
																		.parse((String) matcher2.group(count).replace(',', '.')))))));
													} catch (Exception e) {
														e.printStackTrace();
													}

												}

												if (stats) {
													int[] array = ts.getStat();
													array[1] = array[1] + 1;
													array[0] = array[0] + 1;
													ts.setStat(array);
													if (logger.isDebugEnabled())
														logger.debug("stats " + array[0] + " " + array[1]);
												}
												statMap.put(recItem2.getName(), ts);
												// performance: add the
												// TmeSeriesDataItem to the
												// TimeSeries instead of
												// updating
												// the TimeSeries in the Map

											} else if (classCache.getClass(rec).equals(EventRecording.class) &&  (gatherEvents )) {
												ts = eventMap.get(recItem2.getName());
												if (ts==null) {
													ts = new ExtendedTimeSeries(recItem2, FixedMillisecond.class);
													if (logger.isDebugEnabled())
														logger.debug("5**** Adding record to Map: " + recItem2.getName());
												}
										//		SimpleTimePeriod stp = new SimpleTimePeriod(date1,DateUtils.addMilliseconds(date1,1));
												fMS = new FixedMillisecond(date1);

												if (((RecordingItem) recItem2).getProcessingType().equals("occurrences")) {
													TimeSeriesDataItem t = ts.getTimeSeries().getDataItem(fMS);
													if (t != null) {
														ts.getTimeSeries().addOrUpdate((new TimeSeriesDataItem(fMS, (double)t.getValue()+1))); // +
														// (double)t.getValue()
														// need some way to show
														// several occurrences
													} else {
														ts.getTimeSeries().add((new TimeSeriesDataItem(fMS, 1)));
													}

												} else if (((RecordingItem) recItem2).getProcessingType().equals("duration")) {
													try {
														ts.getTimeSeries().addOrUpdate(
																(new TimeSeriesDataItem(fMS, Double.parseDouble(String.valueOf(decimalFormat.parse(matcher2
																		.group(count)))))));
													} catch (ParseException e) {
														// TODO
														// Auto-generated
														// catch block
														e.printStackTrace();
													}
												
												// ts.addOrUpdate((new
												// TimeSeriesDataItem(fMS,
												// 100)));
											} else if (((RecordingItem) recItem2).getProcessingType().equals("sum")) {
													TimeSeriesDataItem t = ts.getTimeSeries().getDataItem(fMS);
													if (t != null) {
														if (!recItem2.getType().equals("date")) {
															try {
																ts.getTimeSeries().addOrUpdate(
																		(new TimeSeriesDataItem(fMS,
																				Double.parseDouble(String.valueOf(decimalFormat.parse(matcher2.group(count)))
																						+ ts.getTimeSeries().getDataItem(fMS).getValue()))));
																logger.info(ts.getTimeSeries().getDataItem(fMS).getValue());
															} catch (ParseException e) {
																// TODO
																// Auto-generated
																// catch block
																e.printStackTrace();
															}
														}
													} else {
														try {
															// to improve - should use the right type here
															ts.getTimeSeries().add(
																	(new TimeSeriesDataItem(fMS, Double.parseDouble(String.valueOf(decimalFormat.parse(matcher2
																			.group(count)))))));
														} catch (ParseException e) {
															// TODO
															// Auto-generated
															// catch block
															e.printStackTrace();
														}
													}

												} else if (((RecordingItem) recItem2).getProcessingType().equals("capture")) {
													
												} 
												// logger.debug(recItem2.getName()
												// +
												// " " +
												// Double.parseDouble((matcher2.group(count))));
												if (stats) {
													int[] array = ts.getStat();
													array[1] = array[1] + 1;
													array[0] = array[0] + 1;
													ts.setStat(array);
													if (logger.isDebugEnabled())
														logger.debug("stats " + array[0] + " " + array[1]);
												}
												eventMap.put(recItem2.getName(), ts);

											}

										}
									} // rec.getClass().equals(ReportRecording.class)

									count++;
									// logger.info("event statistics: "+eventMatch
									// +
									// " and " +eventHit +
									// " ; stat statistics: "+statMatch +
									// " and "
									// +statHit);
								}
							} else { if (gatherReports){
								int count = 0;
								if (((ReportRecording) rec).getSubType().equals("histogram") && rec.getIsActive()) {
									List<Object> temp = new ArrayList<Object>();
									Iterator<RecordingItem> recItemIte2 = recordingItem.iterator();
									while (recItemIte2.hasNext()) {
										RecordingItem recItem2 = recItemIte2.next();
										if (recItem2.isSelected()) {
											temp.add(matcher2.group(count + 1));
										}
										count++;
									}
									Map<List<Object>,Long>  occMap = occurenceReport.get(source).get(rec);
									if (occMap==null) {
										occurenceReport.get(source).put(rec, new ConcurrentHashMap<List<Object>, Long>());
										occMap = occurenceReport.get(source).get(rec);
									}
									synchronized (occMap) {
									Object occ = occMap.get(temp);
									
									if (occ==null) {
										occMap.put(temp, (long) 1);
									} else {
										occMap.put(temp, (long) occ + 1);
									}
									}
								} else if (((ReportRecording) rec).getSubType().equals("top100") && rec.getIsActive()) {
									double itemIndex = 0;

									SortedMap<Double,List<Object>> t100 = top100Report.get(source).get(rec);
									if (t100==null) {
										top100Report.get(source).put(rec, Collections.synchronizedSortedMap(new TreeMap<Double,List<Object>>()));
										t100 = top100Report.get(source).get(rec);
									}
									try {
									itemIndex = (double)Double.valueOf(matcher2.group(((ReportRecording) rec).getTop100RecordID()+1));
									} catch (NullPointerException npe){										
										//nothing
									}		
									catch (NumberFormatException nfe){
										//nothing
										logger.info(matcher2.group(0));
										logger.info(matcher2.group(((ReportRecording) rec).getTop100RecordID()+1));
									}
									synchronized (t100) {
									if (t100.size()<100){			
										List<Object> temp = new ArrayList<Object>();
										Iterator<RecordingItem> recItemIte2 = recordingItem.iterator();
										while (recItemIte2.hasNext()) {
											RecordingItem recItem2 = recItemIte2.next();
											if (recItem2.isSelected()) {
												if (recItem2.getProcessingType().equals("top100")){
												itemIndex=(double)Double.valueOf(matcher2.group(count + 1));
												} else {
												temp.add(matcher2.group(count + 1));
												}}
											count++;
										}	
										t100.put(itemIndex, temp);
									}
									else if (t100.size()==100){
											if (itemIndex>t100.firstKey()){
												List<Object> temp = new ArrayList<Object>();
												Iterator<RecordingItem> recItemIte2 = recordingItem.iterator();
												while (recItemIte2.hasNext()) {
													RecordingItem recItem2 = recItemIte2.next();
													if (recItem2.isSelected()) {
														if (recItem2.getProcessingType().equals("top100")){
														itemIndex=(double)Double.valueOf(matcher2.group(count + 1));
														} else {
														temp.add(matcher2.group(count + 1));
														}}
													count++;
												}												
												t100.remove(t100.firstKey());
												t100.put(itemIndex, temp);	
											}											
										}

									} 
								}
								
								else if (((ReportRecording) rec).getSubType().equals("sum") && rec.getIsActive()) {
									double itemIndex = 0;
									List<Object> temp = new ArrayList<Object>();
									Iterator<RecordingItem> recItemIte2 = recordingItem.iterator();
									while (recItemIte2.hasNext()) {
										RecordingItem recItem2 = recItemIte2.next();
										if (recItem2.isSelected()) {
											if (recItem2.getProcessingType().equals("sum")){
											itemIndex=(double)Double.valueOf(matcher2.group(count + 1));
											} else {
											temp.add(matcher2.group(count + 1));
											}}
										count++;
									}
									Map<List<Object>,Double>  sumMap = sumReport.get(source).get(rec);
									if (sumMap==null) {
										sumReport.get(source).put(rec, new ConcurrentHashMap<List<Object>, Double>());
										sumMap = sumReport.get(source).get(rec);
									}
									synchronized (sumMap) {
									Object sum = sumMap.get(temp);
									
									if (sum==null) {
										sumMap.put(temp, (double)itemIndex);
									} else {
										sumMap.put(temp, (double) sum + itemIndex);
									}
									}
								}
							}
							}
						}
						if (timings || matches) {
								arrayBefore = matchTimings.get(rec.getName());
								if (arrayBefore!=null) {
								// logger.info(file.getName() + " contains " +
								// arrayBefore);
								// 0-> sum of time for success matching of given
								// recording ; 1-> sum of time for failed
								// matching ; 2-> count of match attempts,
								// 3->count of success attempts
								if (timings) {
									recordingMatchEnd = ManagementFactory.getThreadMXBean().getCurrentThreadCpuTime();
									timing0 = arrayBefore[0] + recordingMatchEnd - recordingMatchStart;
									timing1 = arrayBefore[1];
								}
								if (matches) {
									match0 = arrayBefore[2] + 1;
									match1 = arrayBefore[3] + 1;
								}
								long[] array = { timing0, timing1, match0, match1 };
								matchTimings.put(rec.getName(), array);
							} else {
								if (timings) {
									recordingMatchEnd = ManagementFactory.getThreadMXBean().getCurrentThreadCpuTime();
									long[] array = { recordingMatchEnd - recordingMatchStart, 0, 1, 1 };
									matchTimings.put(rec.getName(), array);
								}else {
									long[] array = { 0, 0, 1, 1 };
									matchTimings.put(rec.getName(), array);
								}
							}
						}
					} else {
						if (timings || matches) {
								arrayBefore = matchTimings.get(rec.getName());
								if (arrayBefore!=null) {
								// logger.info(file.getName() + " contains " +
								// arrayBefore);
								// 0-> sum of time for success matching of given
								// recording ; 1-> sum of time for failed
								// matching ; 2-> count of match attempts,
								// 3->count of success attempts
								if (timings) {
									recordingMatchEnd = ManagementFactory.getThreadMXBean().getCurrentThreadCpuTime();
									timing0 = arrayBefore[0];
									timing1 = arrayBefore[1] + recordingMatchEnd - recordingMatchStart;
								}
								if (matches) {
									match0 = arrayBefore[2] + 1;
									match1 = arrayBefore[3];
								}
								long[] array = { timing0, timing1, match0, match1 };
								matchTimings.put(rec.getName(), array);
							} else {
								if (timings) {
									recordingMatchEnd = ManagementFactory.getThreadMXBean().getCurrentThreadCpuTime();
									long[] array = { 0, recordingMatchEnd - recordingMatchStart, 1, 0 };
									matchTimings.put(rec.getName(), array);
								} else{
									long[] array = { 0, 0, 1, 0 };
									matchTimings.put(rec.getName(), array);
								}
							}
						}
					}
				}
				lineCount++;

				// timing
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				buf1st.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		mainFrame.progress();
		return new FileMineResult(fileRecord, statMap, eventMap, matchTimings, RIFileLineDateMap, startDate, endDate);
	}

	/*
	 * public Map<String,ArrayList> getSourceFileGroup(ArrayList<String>
	 * sourceFiles,Source src) { String patternString = "";
	 * Map<String,ArrayList> Map=new
	 * Map<String,ArrayList>(); while (it.hasNext()){ it.next(); }}
	 * 
	 * returns Map with group id in key and a ArrayList of matching files in
	 * value.
	 * 
	 * @param repo
	 */
	public static Map<String, ArrayList<FileRecord>> getSourceFileGroup(Map<Integer, FileRecord> sourceFiles, Source src, Repository repo, boolean order) {
		PatternCache patternCache = new PatternCache();
		String patternString = "";
		Map<String, ArrayList<FileRecord>> sourceFileGroup = new HashMap<String, ArrayList<FileRecord>>();
		ArrayList<FileRecord> groupedFiles = new ArrayList<FileRecord>();
		Matcher matcher = null;
		Recording rec = src.getActiveMetadata();
		if (src!=null && rec!=null){
					ArrayList<RecordingItem> rIV = ((MetadataRecording) rec).getRecordingItem();
					Iterator<RecordingItem> itV = rIV.iterator();
					int nbRec = 0;
					while (itV.hasNext()) {
						RecordingItem rI = itV.next();
						String type = rI.getType();
						if (type == "date") {
							patternString += rI.getBefore() + "(" + repo.getDateFormat(rec.getDateFormatID()) + ")" + rI.getAfter();
						} else {
							patternString += rI.getBefore() + "(" + DataMiner.getTypeString(type) + ")" + rI.getAfter();
						}
						// logger.info("patternString: " + patternString
						// + " getType: " +
						// DataMiner.getTypeString(rI.getType()));
						nbRec++;
					}
					Iterator<FileRecord> sourceFileIterator = sourceFiles.values().iterator();
					String key = "";
					// tempV = new ArrayList<String>();
					while (sourceFileIterator.hasNext()) {
						groupedFiles.clear();
						FileRecord fileName = sourceFileIterator.next();
						// logger.info("file: "+fileName);
						try {

							if (logger.isTraceEnabled()){
								logger.trace("patternString: " + patternString);
								logger.trace("filename: " + fileName);
							}
							matcher = patternCache.getMatcher(patternString + ".*",rec.isCaseSensitive(),
									new File(repo.getBaseSourcePath()).toURI().relativize(new File(fileName.getFile().getCanonicalPath()).toURI()).getPath());
							if (matcher.find()) {
								if (logger.isTraceEnabled())
									logger.trace("found filename " + fileName + " with group");

								key = "";
								int i = 0;
								for (i = 0; i < matcher.groupCount(); i++) {
										if (logger.isTraceEnabled())
											logger.trace("group matched : " + matcher.group(i));
										key += matcher.group(i + 1) + " ";
								}
								if (logger.isTraceEnabled())
									logger.trace("i : " + i + " nbRec: " + nbRec);
								if (i == nbRec) {
									if (!sourceFileGroup.containsKey(key)) {
										ArrayList<FileRecord> v = new ArrayList<FileRecord>();
										v.add(fileName);
										sourceFileGroup.put(key, v);
										if (logger.isTraceEnabled())
											logger.trace(" to key: " + key + " added : " + fileName);
									} else {
										sourceFileGroup.get(key).add(fileName);
										if (logger.isTraceEnabled())
											logger.trace(" to key: " + key + " added : " + fileName);

									}

								}
								/*
								 * if (tempV != null) { sourceFileGroup.put(key,
								 * tempV); logger.info("Added file " + fileName
								 * + " to group " + key.toString());
								 * logger.info("files " + tempV);
								 * 
								 * }
								 */
							}

						} catch (Exception e1) {
							e1.printStackTrace();
							// System.exit(1);
						}

						// logger.info("found group " + key + "with " +
						// groupedFiles.size() + " files in source " +
						// src.getSourceName());
						
						//option for ordering files by date of lines - useless at this point
						if (order)
							{sourceFileGroup.put(key,Tools.orderFiles(sourceFileGroup.get(key),src));
							} else{
								sourceFileGroup.put(key,sourceFileGroup.get(key));								
							}
					}

		return sourceFileGroup;
		}
else {return null;}
	}
	
	private static Map<Class,Map<Recording, String>> getAllRegexp(Repository repo, Source source) {
		Map<Class,Map<Recording, String>> recMatch = new HashMap<Class,Map<Recording, String>>();
		recMatch.put(StatRecording.class,getRegexp(repo,source,StatRecording.class));
		recMatch.put(EventRecording.class,getRegexp(repo,source,StatRecording.class));
		recMatch.put(ReportRecording.class,getRegexp(repo,source,StatRecording.class));
		return recMatch;
	}

	public static Map<Recording, String> getAllRegexSingleMap(Repository repo, Source source) {
		long startTime = System.currentTimeMillis();
		Map<Recording, String> recMatch = new HashMap<Recording, String>();
		if (Preferences.getBooleanPreference("gatherstats")){
			Map<Recording, String> g1= getRegexp(repo,source,StatRecording.class);
			if (g1!=null){
			recMatch.putAll(g1);
			}
		}
		if (Preferences.getBooleanPreference("gatherevents")){
			Map<Recording, String> g2= getRegexp(repo,source,EventRecording.class);
			if (g2!=null){
				recMatch.putAll(g2);	
			}
		}
		if (Preferences.getBooleanPreference("gatherreports")){
			Map<Recording, String> g3= getRegexp(repo,source,ReportRecording.class);
			if (g3!=null){
			recMatch.putAll(g3);
			}
		}
		estimatedTime = System.currentTimeMillis() - startTime;
		logger.debug("getAllRegexSingleMap time: " + estimatedTime);
		return recMatch;
	}
	
	
	private static Map<Recording, String> getRegexp(Repository repo, Source source, Class recordingClass) {
		Map<Recording, String> recMatch = new HashMap<Recording, String>();
		Map<Recording, Boolean> activeRecordingOnSourceCache = new HashMap<Recording, Boolean>();
		ArrayList<Recording> recordings;
		StringBuffer sb = new StringBuffer(200);
		recordings = repo.getRecordings(recordingClass,true);
		Iterator<Recording> recordingIterator = recordings.iterator();
		boolean forceSourceDateFormat = Preferences.getBooleanPreference("ForceSourceDateFormat");
		
		while (recordingIterator.hasNext()) {
			Recording rec = recordingIterator.next();
			if (!activeRecordingOnSourceCache.containsKey(rec)) {
				activeRecordingOnSourceCache.put(rec, source.isActiveRecordingOnSource(rec));
			}
			if (activeRecordingOnSourceCache.get(rec)) {
				if (rec.getIsActive() == true) {
					ArrayList<RecordingItem> recordingItem = ((Recording) rec).getRecordingItem();
					Iterator<RecordingItem> recItemIte = recordingItem.iterator();
					if (logger.isTraceEnabled()) {
						logger.trace("Record: " + rec.getName());
					}
					sb.setLength(0);
					// processing each line of the table
					while (recItemIte.hasNext()) {
						RecordingItem recItem = recItemIte.next();
						String stBefore = (String) recItem.getBefore();
						String stType = (String) recItem.getType();
						String stAfter = (String) recItem.getAfter();
						String stInside = recItem.getInside();
						sb.append(stBefore);
						sb.append("(");
						if (forceSourceDateFormat){
							sb.append(getMainRegex(stType,stInside,source.getDateFormat()));
						}else{
							sb.append(getMainRegex(stType,stInside,repo.getDateFormat(rec.getDateFormatID())));
						}
						sb.append(")");
						sb.append(stAfter);				
					}
						recMatch.put(rec, sb.toString());
					if (logger.isTraceEnabled()) {
						logger.trace("regexp: " +rec.getRegexp());
						logger.trace("Pattern: " + sb.toString());
					}
				}
			}
		}
		return recMatch;
	}

	public static String getMainRegex(String stType, String stInside, DateFormat dateFormat) {
		if (stType.equals("date")) {
		return dateFormat.getPattern();
		} else{
			if (!stType.equals("manual")){
				return (getTypeString(stType));
			} else {
				return (stInside);
			}}
	}

	// public static get
	public static String getTypeString(String type) {
		String typeString = "";
		switch (type) {
		case "integer":
			typeString = "\\d+";
			break;
		case "percent":
			typeString = "\\d+";
			break;
		case "word":
			typeString = "\\w+";
			break;
		case "stringminimum":
			typeString = ".*?";
			break;
		case "string":
			typeString = ".*";
			break;
		case "double":
			typeString = "[-+]?[0-9]*.?[0-9]+(?:[eE][-+]?[0-9]+)?";
			break;
		case "long": // keeping for compatibility with older templates
			typeString = "\\d+";
			break;
		/*
		 * case "date": typeString = repo.getDateFormat(rec.getDateFormatID());
		 * break;
		 */
		default:
			typeString = ".*";
			break;
		}
		return typeString;
	}
	
	public static ChartData gatherSourceData(final Repository repo, boolean order) {

		PatternCache patternCache = new PatternCache();
		ChartData cd = new ChartData();
		List<File> listOfFiles = null;
		logger.debug("Base file path: " + repo.getBaseSourcePath());
		if (repo.getBaseSourcePath() == null)
			return null;
		File folder = new File(repo.getBaseSourcePath());
		try {
			if (repo.isRecursiveMode()) {
				listOfFiles = FileListing.getFileListing(folder);
			} else {
				listOfFiles = Arrays.asList(folder.listFiles());

			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (listOfFiles != null)
			logger.debug("number of files: " + listOfFiles.size());
		cd.sourceArrayList = repo.getSources();
		Iterator<Source> sourceIterator = cd.sourceArrayList.iterator();

		while (sourceIterator.hasNext()) {
			final Source source = sourceIterator.next();
			cd.selectedSourceFiles = new HashMap<Integer, FileRecord>();
			// sourceFiles contains all the matched files for a given source
			if (source.getActive()) {
				for (int i = 0; i < listOfFiles.size(); i++) {
					if (listOfFiles.get(i).isFile()) {
						String s1 = source.getSourcePattern();
						try {
							Matcher matcher = patternCache.getMatcher(s1,true,
									new File(repo.getBaseSourcePath()).toURI().relativize(new File(listOfFiles.get(i).getCanonicalPath()).toURI()).getPath());

							if (logger.isTraceEnabled()) {
								logger.trace(i
										+ " matching file: "
										+ new File(repo.getBaseSourcePath()).toURI().relativize(new File(listOfFiles.get(i).getCanonicalPath()).toURI())
												.getPath() + " with pattern: " + s1);
							}

							if (matcher.find()) {

								FileRecord tempFileRecord = new FileRecord(i, new File(listOfFiles.get(i).getCanonicalPath()));
								cd.selectedSourceFiles.put(i, tempFileRecord);
								if (logger.isTraceEnabled()) {

									logger.trace("Source: " + source.getSourceName() + " file: " + listOfFiles.get(i).getCanonicalPath());
									logger.trace(" Graphpanel file: "
											+ new File(repo.getBaseSourcePath()).toURI().relativize(new File(listOfFiles.get(i).getCanonicalPath()).toURI())
													.getPath());
									logger.trace(tempFileRecord.getCompletePath());
								}

							}
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
				if (logger.isEnabledFor(Level.DEBUG))
					logger.debug("matched file: " + cd.selectedSourceFiles.size() + " to source  " + source.getSourceName());
			}
			cd.sourceFileArrayListMap.put(source, cd.selectedSourceFiles);
		}
		Map<String, ArrayList<FileRecord>> sourceFileGroup = null;
		Iterator<Entry<Source, Map<Integer, FileRecord>>> ite = cd.sourceFileArrayListMap.entrySet().iterator();
		while (ite.hasNext()) {
			final Map.Entry sourcePairs = ite.next();
			final Source src = (Source) sourcePairs.getKey();
			Map<Integer, FileRecord> sourceFiles = (Map<Integer, FileRecord>) sourcePairs.getValue();
			sourceFileGroup = getSourceFileGroup(sourceFiles, src, repo,order);
			if (sourceFileGroup!=null && sourceFileGroup.keySet().size()>0)
				logger.info("matched groups: " + (sourceFileGroup!=null? sourceFileGroup.keySet().size():"") + " for source " + src.getSourceName());
		//	logger.debug(sourceFileGroup.toString());
			cd.setGroupFilesArrayListMap(src, sourceFileGroup);
		}
		return cd;
	}

	public static void populateRecordingSamples(Repository repo) {
		PatternCache patternCache = new PatternCache();
		FileReader flstr = null;
		BufferedReader buf1st;
		Map<Recording, String> recMatch = new HashMap<Recording, String>();
		Matcher matcher;
		Matcher matcher2;
		if (repo.getBaseSourcePath() == null)
			return;
		File folder = new File(repo.getBaseSourcePath());
		try {
			if (repo.isRecursiveMode()) {
				listOfFiles = FileListing.getFileListing(folder);
			} else {
				listOfFiles = Arrays.asList(folder.listFiles());
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
		}
		if (repo != null && repo.getBaseSourcePath() != null) {
			ChartData cd = DataMiner.gatherSourceData(repo,false);
			ArrayList sources = repo.getSources();
			Iterator sourceArrayListIte = sources.iterator();
			while (sourceArrayListIte.hasNext()) {

				// Map<Recording, String> regMap=getRegexp(repo, src);
				cd.sourceArrayList = repo.getSources();
				Iterator<Source> sourceIterator = cd.sourceArrayList.iterator();

				Source src = (Source) sourceArrayListIte.next();
				Map<String, ArrayList<FileRecord>> hm = cd.getGroupFilesMap(src);
				logger.info("population");
				if (hm != null && hm.entrySet() != null) {
					Iterator it = hm.entrySet().iterator();
					while (it.hasNext()) {
						final Map.Entry pairs = (Map.Entry) it.next();
						logger.info("populating: " + pairs.getKey());
						ArrayList<FileRecord> grouFile = (ArrayList<FileRecord>) pairs.getValue();
						// return DataMiner.mine((String) pairs.getKey(),
						// (ArrayList<String>) pairs.getValue(), repo, source,
						// repo.isStats(), repo.isTimings());
						Iterator<FileRecord> fileArrayListIterator = grouFile.iterator();
						while (fileArrayListIterator.hasNext()) {
							final FileRecord fileName = fileArrayListIterator.next();
							try {
								flstr = new FileReader(new File(fileName.getCompletePath()));
							} catch (FileNotFoundException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
							buf1st = new BufferedReader(flstr);
							String line;
							logger.info("matched file:" + fileName);
							recMatch = getAllRegexSingleMap(repo, src);
							try {
								while ((line = buf1st.readLine()) != null) {
									// check against one Recording pattern at a
									// tim
									// if (logger.isDebugEnabled()) {
									// logger.debug("line " + line);
									// }
									Iterator recMatchIte = recMatch.entrySet().iterator();
									while (recMatchIte.hasNext()) {
										Map.Entry me = (Map.Entry) recMatchIte.next();
										Recording rec = (Recording) me.getKey();
										matcher = patternCache.getMatcher((String) (rec.getRegexp()), rec.isCaseSensitive(),line);
										if (matcher.find()) {
											// logger.info("1**** matched: " +
											// line);
											ArrayList<RecordingItem> recordingItem = ((Recording) rec).getRecordingItem();
											int cnt = 0;
											matcher2 = patternCache.getMatcher((String) me.getValue(), rec.isCaseSensitive(),line);
											if (matcher2.find()) {

												DataVault.addMatchedLines(rec, line);
											} else {
												DataVault.addUnmatchedLines(rec, line);
											}
										}

									}
								}
							} catch (IOException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
						}
					}
				}
			}
		}
	}

/*	public static ArrayList<Map> exportData(Repository repo) {
		gatherMineResultSet(null,repo, null);
		return null;

	}*/
	/*
	 * public static ArrayList<Map> exportData(Repository repo) { PatternCache
	 * patternCache = new PatternCache(); Matcher matcher = null; ArrayList<Map>
	 * expVec = new ArrayList<Map>(); File folder = new
	 * File(repo.getBaseSourcePath()); try { if (repo.isRecursiveMode()) {
	 * listOfFiles = FileListing.getFileListing(folder); } else { listOfFiles =
	 * Arrays.asList(folder.listFiles());
	 * 
	 * } } catch (FileNotFoundException e) { // TODO Auto-generated catch block
	 * e.printStackTrace(); } if (logger.isEnabledFor(Level.INFO))
	 * logger.info("number of files: " + listOfFiles.size()); // int[][]
	 * fileListMatches = new int[listOfFiles.size()][3];
	 * 
	 * Iterator sourceIterator = repo.getSources().iterator();
	 * 
	 * while (sourceIterator.hasNext()) { Source r = (Source)
	 * sourceIterator.next(); ArrayList<String> sourceFiles = new
	 * ArrayList<String>(); // sourceFiles contains all the matched files for a
	 * given source
	 * 
	 * if (r.getActive()) {
	 * 
	 * for (int i = 0; i < listOfFiles.size(); i++) { if
	 * (listOfFiles.get(i).isFile()) { // logger.info("File " + //
	 * listOfFiles.get(i).getName()); String s1 = r.getSourcePattern(); matcher
	 * = patternCache.getPattern(s1).matcher(listOfFiles.get(i).getName()); if
	 * (matcher.find()) { try { sourceFiles.add(new
	 * File(repo.getBaseSourcePath()).toURI().relativize(new
	 * File(listOfFiles.get(i).getCanonicalPath()).toURI()) .getPath());
	 * 
	 * //
	 * logger.info(" Graphpanel file1: "+listOfFiles.get(i).getCanonicalPath());
	 * // logger.info(" Graphpanel file: "+new //
	 * File(repo.getBaseSourcePath()).toURI() // .relativize(new //
	 * File(listOfFiles.get(i).getCanonicalPath()).toURI()).getPath()); } catch
	 * (IOException e) { // TODO Auto-generated catch block e.printStackTrace();
	 * } // sourceFiles.add(listOfFiles.get(i).getAbsolutePath() // +
	 * listOfFiles.get(i).getName()); } } } logger.info("matched file: " +
	 * sourceFiles.size() + " to source group " + r.getSourceName()); }
	 * Map<String, ArrayList<String>> sourceFileGroup =
	 * getSourceFileGroup(sourceFiles, r, repo); expVec.add(sourceFileGroup);
	 * logger.info("matched groups: " + sourceFileGroup.keySet().size() +
	 * " for source " + r.getSourceName()); Iterator it =
	 * sourceFileGroup.entrySet().iterator(); while (it.hasNext()) { Map.Entry
	 * pairs = (Map.Entry) it.next(); logger.info(pairs.getKey().toString() +
	 * " = " + pairs.getValue()); // it.remove(); // avoids a
	 * ConcurrentModificationException
	 * 
	 * FileMineResultSet fMR = fastMine((ArrayList<String>) pairs.getValue(),
	 * repo, r, false, false);
	 * 
	 * expVec.add(fMR.eventGroupTimeSeries);
	 * expVec.add(fMR.statGroupTimeSeries); } } return expVec; }
	 */
}
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JTable;
import javax.swing.BoxLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.border.BevelBorder;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import java.text.ParseException;
import org.apache.commons.lang3.time.FastDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import logdruid.data.Repository;
import logdruid.data.Source;
import logdruid.data.mine.DataVault;
import logdruid.data.record.ReportRecording;
import logdruid.data.record.MetadataRecording;
import logdruid.data.record.Recording;
import logdruid.data.record.RecordingItem;
import logdruid.data.record.StatRecording;
import logdruid.ui.editor.ReportRecordingEditor;
import logdruid.ui.editor.MetadataRecordingEditor;
import logdruid.ui.editor.StatRecordingEditor;
import logdruid.ui.mainpanel.RecordingList.MyTableModel2;
import logdruid.util.DataMiner;
import javax.swing.SwingConstants;
import javax.swing.JSeparator;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import org.apache.log4j.Logger;
import javax.swing.JSplitPane;

/*******************************************************************************
 * LogDruid : Generate charts and reports using data gathered in log files
 * Copyright (C) 2016 Frederic Valente (frederic.valente@gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
 *******************************************************************************/
package logdruid.ui.mainpanel;















public class ReportRecordingSelectorPanel extends JPanel {
	private static Logger logger = Logger.getLogger(ReportRecordingSelectorPanel.class.getName());
	public static JTable table;
	JPanel jPanelDetail;
	ReportRecordingSelectorPanel thiis = this;
	boolean DEBUG = false;;
	static Matcher m;
	static ArrayList records = null;
	private String[] header = { "name", "regexp", "type","subtype", "active" };
	private Vector<Object[]> data = new Vector<Object[]>();
	private Repository repository = null;
	private JPanel recEditor = null;
	public logdruid.ui.mainpanel.ReportRecordingSelectorPanel.MyTableModel model;
	private Source source;

	/**
	 * Create the panel.
	 */
	public ReportRecordingSelectorPanel(final Repository rep, Source src) {
		repository = rep;
		source = src;
		records = rep.getRecordings(ReportRecording.class,true);
		// Collections.sort(records);
		Iterator it = records.iterator();
		while (it.hasNext()) {

			Recording record = (Recording) it.next();
