/*
 * Copyright (C) 2010 BloatIt.
 * 
 * This file is part of BloatIt.
 * 
 * BloatIt is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * BloatIt 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 Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with BloatIt. If not, see <http://www.gnu.org/licenses/>.
 */

package com.bloatit.framework;

import java.util.UUID;

import javassist.NotFoundException;

import com.bloatit.common.Log;
import com.bloatit.framework.managers.MemberManager;

public class AuthToken {
	private final Member member;
	private final UUID key;

	public AuthToken(String login, String password) throws NotFoundException {
		final Member tmp = MemberManager.getByLoginAndPassword(login, password);
		if (tmp == null) {
			Log.framework().warn("Authentication error " + login + " " + password);
			throw new NotFoundException("Identifiaction or authentication failed");
		}
		this.member = tmp;
		this.key = UUID.randomUUID();
	}

	public UUID getKey() {
		return key;
	}

	public Member getMember() {
		return member;
	}

}

@@ -901,16 +901,67 @@
 Manager;
+%0Aimport com.bloatit.model.data.util.SessionManager;
 %0A%0Apublic
@@ -979,17 +979,20 @@
 Token %7B%0A
-%09
+    
 private 
@@ -1012,17 +1012,20 @@
 member;%0A
-%09
+    
 private 
@@ -1041,17 +1041,20 @@
 D key;%0A%0A
-%09
+    
 public A
@@ -1120,18 +1120,24 @@
 ption %7B%0A
-%09%09
+        
 final Me
@@ -1201,18 +1201,24 @@
 sword);%0A
-%09%09
+        
 if (tmp 
@@ -1228,19 +1228,28 @@
 null) %7B%0A
-%09%09%09
+            
 Log.fram
@@ -1316,11 +1316,20 @@
 d);%0A
-%09%09%09
+            
 thro
@@ -1400,14 +1400,26 @@
 %22);%0A
-%09%09%7D%0A%09%09
+        %7D%0A        
 this
@@ -1433,18 +1433,24 @@
  = tmp;%0A
-%09%09
+        
 this.key
@@ -1471,21 +1471,27 @@
 UUID();%0A
-%09%7D%0A%0A%09
+    %7D%0A%0A    
 public U
@@ -1501,26 +1501,32 @@
  getKey() %7B%0A
-%09%09
+        
 return key;%0A
@@ -1529,13 +1529,19 @@
 ey;%0A
-%09%7D%0A%0A%09
+    %7D%0A%0A    
 publ
@@ -1568,26 +1568,205 @@
 ) %7B%0A
-%09%09return member;%0A%09
+        if (SessionManager.getSessionFactory().getCurrentSession().contains(member.getDao())) %7B%0A            return member;%0A        %7D%0A        return MemberManager.getMemberById(member.getId());%0A    
 %7D%0A%0A%7D

correct the persistence problem with AuthToken