package esg.security.attr.service.impl;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.springframework.core.io.ClassPathResource;

import esg.security.attr.service.api.SAMLAttributeFactory;
import esg.security.attr.service.api.SAMLAttributes;
import esg.security.common.SAMLUnknownPrincipalException;
import esg.security.utils.xml.Parser;

import esg.node.components.idp.SAMLDAO;
import esg.node.components.idp.SAMLUserInfo;

/**
 * Implementation of {@link SAMLAttributeFactory} backed up by node's "esgcet" RDBMS database.
 * 
 * @author gavin.bell
 */
public class SAMLAttributeFactoryDAOImpl implements SAMLAttributeFactory {
	
	final private String issuer;

    private SAMLAttributes attributes = null;
    private SAMLDAO samlDAO = null;
	
	public SAMLAttributeFactoryDAOImpl(final String issuer, Properties props) throws Exception {
		
		this.issuer = issuer;
        this.samlDAO = new SAMLDAO(props);
		
	}

	@Override
	public SAMLAttributes newInstance(String identifier) throws SAMLUnknownPrincipalException {        
        samlDAO.setIdentifier(identifier);
        SAMLMUserInfo samlUserInfo = samlDAO.getAttributesForId();

        //Note: as an optimization could put an LRU cache mapping
        //identifier to resultant attributes object so don't have to
        //hit the database as much.
		if (samlUserInfo != null) {
            attributes = new SAMLAttributes();
            attributes.setFirstName(samlUserInfo.getFirstName());
            attributes.setLastName(samlUserInfo.getLastName());
            attributes.setOpenid(samlUserInfo.getOpenid());
            attributes.setEmail(samlUserInfo.getEmail());
            attributes.setIssuer(this.issuer);
            //TODO: what do I do about attribute_type and attribute_value??
			return attributes.get(identifier);
		} else {
			throw new SAMLUnknownPrincipalException("Unknown identifier: "+identifier);
		}
	}
    
	@Override
	public String getIssuer() {
		return issuer;
	}
	
	/**
	 * Debug method.
	 */
	public void print() {
		
		for (final String identity : attributes.keySet()) {
			System.out.println("User="+identity);
			System.out.println(attributes.get(identity));
		}
		
	}

}

@@ -548,19 +548,25 @@
 nts.
-idp.SAMLDAO
+security.UserInfo
 ;%0Aim
@@ -594,24 +594,25 @@
 nts.
-idp.SAML
+security.
 UserInfo
 ;%0A%0A/
@@ -607,16 +607,19 @@
 UserInfo
+DAO
 ;%0A%0A/**%0A 
@@ -912,20 +912,28 @@
 ate 
-SAMLDAO saml
+userInfoDAO userInfo
 DAO 
@@ -1076,20 +1076,24 @@
    this.
-saml
+userInfo
 DAO = ne
@@ -1094,20 +1094,24 @@
 O = new 
-SAML
+UserInfo
 DAO(prop
@@ -1245,65 +1245,26 @@
     
-samlDAO.setIdentifier(identifier);%0A        SAMLMU
+UserInfo u
 serInfo 
 saml
@@ -1263,27 +1263,18 @@
 nfo 
-samlU
+= u
 serInfo
- = saml
 DAO.
@@ -1292,16 +1292,26 @@
 esForId(
+identifier
 );%0A%0A    
@@ -1483,21 +1483,17 @@
 .%0A%09%09if (
-samlU
+u
 serInfo 
@@ -1546,17 +1546,39 @@
 tributes
-(
+Impl(identifier, issuer
 );%0A     
@@ -1604,29 +1604,25 @@
 etFirstName(
-samlU
+u
 serInfo.getF
@@ -1669,21 +1669,17 @@
 astName(
-samlU
+u
 serInfo.
@@ -1727,21 +1727,17 @@
 tOpenid(
-samlU
+u
 serInfo.
@@ -1786,13 +1786,9 @@
 ail(
-samlU
+u
 serI
@@ -1834,104 +1834,45 @@
 .set
-Issuer(this.issuer);%0A            //TODO: what do I do about attribute_type and attribute_value??
+Attributes(userInfo.getAttributes());
 %0A%09%09%09

rename of objects - using UserInfo and UserInfoDAO objects instead of the SAML names...