/*
   Licensed to the Apache Software Foundation (ASF) under one
   or more contributor license agreements.  See the NOTICE file
   distributed with this work for additional information
   regarding copyright ownership.  The ASF licenses this file
   to you under the Apache License, Version 2.0 (the
   "License"); you may not use this file except in compliance
   with the License.  You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing,
   software distributed under the License is distributed on an
   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   KIND, either express or implied.  See the License for the
   specific language governing permissions and limitations
   under the License.    
 */
package org.opentides.persistence.impl;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.opentides.bean.user.SessionUser;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl;

/**
 * This class is responsible in retrieving the user information.
 * TODO: Rewrite this to use JPA EntityManager... if at all, possible.
 * @author allantan
 *
 */
public class AuthenticationDAOJdbcImpl extends JdbcDaoImpl {

	private static Log _log = LogFactory.getLog(AuthenticationDAOJdbcImpl.class);	
	private static String loadUserByUsernameQuery = 
		"select U.USERID ID, FIRSTNAME, LASTNAME, EMAIL, P.OFFICE OFFICE, P.LASTLOGIN LASTLOGIN, '' POSITION,'' COMPANY,'' PICTUREURL" +
		" from USER_PROFILE P, USERS U where P.ID=U.USERID and U.USERNAME=?";
	
	@Override 
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
		try {
            UserDetails user = super.loadUserByUsername(username);
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SessionUser sessUser = new SessionUser(user.getUsername(), user.getPassword(), user.isEnabled(), user.getAuthorities());
            Map<String,Object> result = getJdbcTemplate().queryForMap(loadUserByUsernameQuery.replace("?", "'"+username+"'"));
            sessUser.setFirstName(result.get("FIRSTNAME").toString());
            sessUser.setLastName(result.get("LASTNAME").toString());
            sessUser.setEmailAddress(result.get("EMAIL").toString());
			if (result.get("OFFICE") != null)
				sessUser.setOffice(result.get("OFFICE").toString());
            sessUser.setPosition(result.get("POSITION").toString());
            sessUser.setCompany(result.get("COMPANY").toString());
            sessUser.setPictureUrl(result.get("PICTUREURL").toString());
            if (result.get("LASTLOGIN") != null) {
				try {
					sessUser.setLastLogin(format.parse(result.get("LASTLOGIN").toString()));
				} catch (ParseException e) {
					sessUser.setLastLogin(Calendar.getInstance().getTime());
				}
			}            sessUser.setId((Long) result.get("ID"));
            return sessUser;
		} catch (UsernameNotFoundException ex1) {
			_log.error(ex1);
		    throw ex1;
		} catch (DataAccessException ex2) {
			_log.error(ex2);
		    throw ex2;
		}
	}
}

@@ -2572,24 +2572,27 @@
 etFirstName(
+%22%22+
 result.get(%22
@@ -2598,35 +2598,24 @@
 %22FIRSTNAME%22)
-.toString()
 );%0D%0A        
@@ -2639,16 +2639,19 @@
 astName(
+%22%22+
 result.g
@@ -2664,27 +2664,16 @@
 STNAME%22)
-.toString()
 );%0D%0A    
@@ -2705,16 +2705,19 @@
 Address(
+%22%22+
 result.g
@@ -2731,62 +2731,12 @@
 IL%22)
-.toString());%0D%0A%09%09%09if (result.get(%22OFFICE%22) != null)%0D%0A%09
+);%0D%0A
 %09%09%09s
@@ -2753,16 +2753,19 @@
 tOffice(
+%22%22+
 result.g
@@ -2776,27 +2776,16 @@
 OFFICE%22)
-.toString()
 );%0D%0A    
@@ -2813,16 +2813,19 @@
 osition(
+%22%22+
 result.g
@@ -2838,27 +2838,16 @@
 SITION%22)
-.toString()
 );%0D%0A    
@@ -2874,16 +2874,19 @@
 Company(
+%22%22+
 result.g
@@ -2898,27 +2898,16 @@
 OMPANY%22)
-.toString()
 );%0D%0A    
@@ -2937,16 +2937,19 @@
 tureUrl(
+%22%22+
 result.g
@@ -2964,26 +2964,69 @@
 UREURL%22)
-.toString(
+);%0D%0A            sessUser.setId((Long) result.get(%22ID%22
 ));%0D%0A   
@@ -3276,60 +3276,8 @@
 %09%09%09%7D
-            sessUser.setId((Long) result.get(%22ID%22));
 %0D%0A  
@@ -3471,15 +3471,316 @@
 ;%0D%0A%09%09%7D%0D%0A
-
 %09%7D%0D
+%0A%0D%0A%09/**%0D%0A%09 * Setter method for loadUserByUsernameQuery.%0D%0A%09 *%0D%0A%09 * @param loadUserByUsernameQuery the loadUserByUsernameQuery to set%0D%0A%09 */%0D%0A%09public void setLoadUserByUsernameQuery(String loadUserByUsernameQuery) %7B%0D%0A%09%09AuthenticationDAOJdbcImpl.loadUserByUsernameQuery = loadUserByUsernameQuery;%0D%0A%09%7D%09%0D%0A%09%0D
 %0A%7D%0D%0A

fix to avoid null pointer