/*
 *	EasyScout Boy Scouts of America tracking software.
 *	http://www.easyscout.org/
 *
 *	Copyright (C) 2000 Conan C. Albrecht
 *	conan@warp.cx
 *
 *	This program is released under the GNU license so tech-savvy
 *	Boy Scouts and others can enhance the program for the use
 *	of all.  Please e-mail changes to my address above so I can
 *	reintegrate into the main program at http://www.easyscout.org/.
 *	This way, all users mutually benefit one another in the spirit of
 *	scouting.
 *
 *	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 2
 *	of the License, or (at your option) any later version.
 *
 * 	Any and all changes to the program code must be documented with
 *  date, author name and e-mail, and a short description.
 *
 *	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, write to the Free Software
 *	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package cx.warp.scout.file;

import cx.warp.scout.lookup.*;
import java.util.*;
import java.util.zip.*;
import java.io.*;

/** Checks the current ranks and verifies that they are all correct with the XML.
		This procedure requires exclusive access to troop data (i.e. stop the web server)
 */
public class checkRanks {
	
	static FileDataGate datagate = null;

	/** Checks ranks */
	public static void main(String args[]) {
		try {
			// user message
			System.out.println("This procedure verifies all troop rank records against the XML files.");
			System.out.println("Please ensure exclusive access to the data files before continuing,");
			System.out.println("including stopping the servlet engine.");
			System.out.println(" ");
			System.out.println("Press [Enter] to start or [Ctrl-C] to cancel.");
			System.in.read();

			// initialize the data gate
			datagate = new FileDataGate();
			datagate.init();
			
			// load the troops, one by one
			File dir = new File(datagate.DATA_DIR);
			File files[] = dir.listFiles();
			for (int i = 0; i < files.length; i++) {
				try {
					// load the troop
					ObjectInputStream in = null;
					try { // try first with a gzipinputstream
						in = new ObjectInputStream(new GZIPInputStream(new FileInputStream(files[i])));
					}catch (IOException ex) { // now try a non-zipped version (deprecated)
					  in = new ObjectInputStream(new FileInputStream(files[i]));
					}//try
					FileTroop troop = (FileTroop)in.readObject();
					System.out.println("Working on troop: " + troop.getName());
					in.close();
					
					// check all scouts
					for (Enumeration enum = troop.getScouts(); enum.hasMoreElements(); ) {
						FileScout scout = (FileScout)enum.nextElement();
						System.out.println("     " + scout.getName());
						check(scout);
					}//for
					
					// save the troop again
					ObjectOutputStream out = new ObjectOutputStream(new GZIPOutputStream(new FileOutputStream(files[i])));
					out.writeObject(troop);
					out.close();
					
				}catch (Exception e) {
					e.printStackTrace();
				}//try
			}//for i
	
			System.out.println("Done.");
	
		}catch (Exception e) {
			e.printStackTrace();
		}//try
		System.exit(0);
	}//main
	
	/** Checks ranks for a scout */
	protected static void check(FileScout scout) {
		try {
			Vector xml = XMLLookup.getRanks(scout.getRequirementsVersion() + ".xml");
			Vector ranks = scout.getRanksVector();
			
			// check for current or added items
			for (int i = 0; i < xml.size(); i++) {
				FileRankObject xmlrank = (FileRankObject)xml.elementAt(i);
				
				if (i >= ranks.size()) { // addition?
					ranks.addElement(xmlrank);
				}else{ // have this one, so check one by one
					FileRankObject rank = (FileRankObject)ranks.elementAt(i);
					checkRank(xmlrank, rank);
				}//if
			}//for
			
			// check for removed items
			while (ranks.size() != xml.size()) {
				ranks.setSize(xml.size());
			}//whiel
			
		}catch (Exception e) {
			e.printStackTrace();
		}//try

	}//check
	
	/** Checks a single rank for changes */
	protected static void checkRank(FileRankObject xmlrank, FileRankObject rank) throws Exception {
		rank.setMeritBadgeCount(xmlrank.getMeritBadgeCount());
		rank.setRequiredCount(xmlrank.getRequiredCount());
		
		for (int i = 0; i < xmlrank.size(); i++) {
			FileRequirement xmlreq = (FileRequirement)xmlrank.elementAt(i);
			
			if (i >= rank.size()) { // addition?
				rank.addElement(xmlreq);
			}else{ // check the name and id
				FileRequirement req = (FileRequirement)rank.elementAt(i);
				if (req == null) {
					rank.setElementAt(xmlreq, i);
				}else{
					req.setIndex(xmlreq.getIndex());
					req.setName(xmlreq.getName());
				}//if
			}//if
			
			// check for removed items
			if (rank.size() != xmlrank.size()) {
				rank.setSize(xmlrank.size());
			}//if
		}//for i
	}//checkRank

}//CheckRanks
