/**
 * DescriptionStore.java
 * 
 * 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/>.
 * 
 * Copyright (C) Wouter Lueks, Radboud University Nijmegen, Februari 2013.
 */

package org.irmacard.credentials.info;

import java.net.URI;
import java.util.HashMap;

/**
 * TODO: Change print statements to proper Logging statements
 */
public class DescriptionStore {
	static URI CORE_LOCATION;
	static TreeWalkerI treeWalker;
	
	static DescriptionStore ds;
	
	HashMap<Integer,CredentialDescription> credentialDescriptions = new HashMap<Integer, CredentialDescription>();
	HashMap<String,IssuerDescription> issuerDescriptions = new HashMap<String, IssuerDescription>();
	HashMap<Integer,VerificationDescription> verificationDescriptions = new HashMap<Integer, VerificationDescription>();

	/**
	 * Define the CoreLocation. This has to be set before using the 
	 * DescriptionStore or define a TreeWalker instead.
	 * @param coreLocation Location of configuration files.
	 */
	public static void setCoreLocation(URI coreLocation) {
		CORE_LOCATION = coreLocation;
	}
	
	/**
	 * Define the TreeWalker. This allows crawling more difficult storage systems,
	 * like Android's. This has to be set before using the DescriptionStore or define
	 * a coreLocation instead.
	 * @param treeWalker
	 */
	public static void setTreeWalker(TreeWalkerI treeWalker) {
		DescriptionStore.treeWalker = treeWalker;
	}

	/**
	 * Get DescriptionStore instance
	 * 
	 * @return The DescriptionStore instance
	 * @throws Exception if CoreLocation has not been set
	 */
	public static DescriptionStore getInstance() throws InfoException {		
		if(CORE_LOCATION == null && treeWalker == null) {
			// TODO: Improve exception type
			throw new InfoException(
					"Please set CoreLocation before using the DescriptionStore");
		}

		if(ds == null) {
			ds = new DescriptionStore();
		}

		return ds;
	}

	private DescriptionStore() throws InfoException {
		if(CORE_LOCATION != null) {
			treeWalker = new TreeWalker(CORE_LOCATION);
		}

		treeWalker.parseConfiguration(this);
	}
	
	public CredentialDescription getCredentialDescription(short id) {
		return credentialDescriptions.get(new Integer(id));
	}

	public CredentialDescription getCredentialDescriptionByName(String issuer,
			String credID) {
		for (CredentialDescription cd : credentialDescriptions.values()) {
			if (cd.getIssuerID().equals(issuer)
					&& cd.getCredentialID().equals(credID)) {
				return cd;
			}
		}

		return null;
	}

	public VerificationDescription getVerificationDescriptionByName(
			String verifier, String verificationID) {
		for (VerificationDescription vd : verificationDescriptions.values()) {
			if (vd.getVerifierID().equals(verifier)
					&& vd.getVerificationID().equals(verificationID)) {
				return vd;
			}
		}

		return null;
	}

	public void addCredentialDescription(CredentialDescription cd) {
		credentialDescriptions.put(new Integer(cd.getId()), cd);
	}
	
	public IssuerDescription getIssuerDescription(String name) {
		return issuerDescriptions.get(name);
	}
	
	public void addIssuerDescription(IssuerDescription id) {
		issuerDescriptions.put(id.getID(), id);
	}

	public void addVerificationDescription(VerificationDescription vd) {
		verificationDescriptions.put(new Integer(vd.getID()), vd);
	}
}

@@ -835,23 +835,107 @@
 va.util.
-HashMap
+ArrayList;%0Aimport java.util.Collection;%0Aimport java.util.HashMap;%0Aimport java.util.Iterator
 ;%0A%0A/**%0A 
@@ -3938,10 +3938,860 @@
 vd);%0A%09%7D%0A
+%09%0A%09public Collection%3CIssuerDescription%3E getIssuerDescriptions() %7B%0A%09%09return issuerDescriptions.values();%0A%09%7D%0A%09%0A%09public Collection%3CVerificationDescription%3E getVerificationDescriptionsForVerifier(String verifierID) %7B%0A%09%09ArrayList%3CVerificationDescription%3E result = new ArrayList%3CVerificationDescription%3E();%0A%09%09for (VerificationDescription vd : verificationDescriptions.values()) %7B%0A%09%09%09if (vd.getVerifierID().equals(verifierID)) %7B%0A%09%09%09%09result.add(vd);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return result;%0A%09%7D%0A%09%0A%09public Collection%3CVerificationDescription%3E getVerificationDescriptionsForVerifier(IssuerDescription verifier) %7B%0A%09%09ArrayList%3CVerificationDescription%3E result = new ArrayList%3CVerificationDescription%3E();%0A%09%09for (VerificationDescription vd : verificationDescriptions.values()) %7B%0A%09%09%09if (vd.getVerifierID().equals(verifier.getID())) %7B%0A%09%09%09%09result.add(vd);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return result;%0A%09%7D%0A
 %7D%0A

Add code to obtain lists of Issuers and VerificationDescriptions.