
import java.io.*;
import java.security.*;
import java.util.*;

/**
 *	Displays all credit cards in the database,
 *	after decrypting them with a private key.
 */
public class ViewTest {

	// Properties file for the database and public key information
	private static final String PROPERTIES_FILE = "config.properties";

	// Keystore that holds the private key
	private static final String KEYSTORE = "tarjetas.ks";

	// Password for the keystore
	private static final char[] PASSWORD = {'d','e','m','o'};

	/**
	 *	Attempts to display all credit cards in the database.
	 */
	public static void main(String[] args) throws Exception {

		// Load the keystore to retrieve the private key.
		String ksType = KeyStore.getDefaultType();
		KeyStore ks = KeyStore.getInstance(ksType);
		FileInputStream fis = new FileInputStream(KEYSTORE);
		ks.load(fis,PASSWORD);
		fis.close();
		PrivateKey privateKey = (PrivateKey)ks.getKey("mykey",PASSWORD);

		// Load the database properties file.
		Properties properties = new Properties();
		fis = new FileInputStream(PROPERTIES_FILE);
		properties.load(fis);
		fis.close();

		// Create a credit card factory with the given properties.
		CreditCardFactory factory = new CreditCardFactory(properties);

		// Get all the credit cards.
		Iterator iterator = factory.findAllCreditCards(privateKey);

		// Display all credit cards.
		while(iterator.hasNext()) {
			CreditCard creditCard = (CreditCard)iterator.next();
			System.out.println("\nAccount ID: "+creditCard.getAccountID());
			System.out.println("CC Number: "+creditCard.getCreditCardNumber());
		}
	}
}
