package com.almende.dialog;

import java.io.Serializable;

import com.almende.dialog.accounts.AdapterConfig;
import com.almende.util.twigmongo.annotations.Id;
import com.almende.util.uuid.UUID;

public class Log implements Serializable {
	private static final long	serialVersionUID	= -8797389516750753990L;
	
	@Id
	private String				logId				= null;
	private LogLevel			level				= null;
	private String				adapterID			= null;
	private String				adapterType			= null;
	private String				message				= null;
	private long				timestamp			= 0;
	
	public Log() {
	}
	
	public Log(LogLevel level, String adapterID, String adapterType,
			String message) {
		this.logId = new UUID().toString();
		this.level = level;
		this.adapterID = adapterID;
		//fetch the adapter type if empty
		if((adapterType == null || adapterType.isEmpty()) && adapterID != null )
		{
		    AdapterConfig adapterConfig = AdapterConfig.getAdapterConfig( adapterID );
		    adapterType = adapterConfig.getAdapterType();
		}
		this.adapterType = adapterType;
		this.message = message;
		this.timestamp = System.currentTimeMillis();
	}
	
    public Log( LogLevel level, AdapterConfig adapter, String message )
    {
        this( level, adapter.getConfigId(), adapter.getAdapterType(), message );
    }
	
	public LogLevel getLevel() {
		return level;
	}
	
	public void setLevel(LogLevel level) {
		this.level = level;
	}
	
	public String getAdapterID() {
		return adapterID;
	}
	
	public void setAdapterID(String adapterID) {
		this.adapterID = adapterID;
	}
	
	public String getAdapterType() {
		return adapterType;
	}
	
	public void setAdapterType(String adapterType) {
		this.adapterType = adapterType;
	}
	
	public String getMessage() {
		return message;
	}
	
	public void setMessage(String message) {
		this.message = message;
	}
	
	public long getTimestamp() {
		return timestamp;
	}
	
	public void setTimestamp(long timestamp) {
		this.timestamp = timestamp;
	}
	
	public String getLogId() {
		return logId;
	}
	
	public void setLogId(String logId) {
		this.logId = logId;
	}
	
}

@@ -102,16 +102,80 @@
 Config;%0A
+import com.almende.util.twigmongo.TwigCompatibleMongoDatastore;%0A
 import c
@@ -216,16 +216,16 @@
 ons.Id;%0A
-
 import c
@@ -1003,32 +1003,56 @@
    adapterType =
+ adapterConfig != null ?
  adapterConfig.g
@@ -1066,16 +1066,23 @@
 erType()
+ : null
 ;%0A%09%09%7D%0A%09%09
@@ -1610,25 +1610,28 @@
 terID;%0A%09%7D%0A%09%0A
-%09
+    
 public Strin
@@ -1648,21 +1648,382 @@
 erType()
- %7B%0A%09%09
+%0A    %7B%0A        if ( ( adapterType == null %7C%7C adapterType.isEmpty() ) && adapterID != null )%0A        %7B%0A            AdapterConfig adapterConfig = AdapterConfig.getAdapterConfig( adapterID );%0A            adapterType = adapterConfig != null ? adapterConfig.getAdapterType() : null;%0A            new TwigCompatibleMongoDatastore().storeOrUpdate( this );%0A        %7D%0A        
 return a
@@ -2026,33 +2026,36 @@
 rn adapterType;%0A
-%09
+    
 %7D%0A%09%0A%09public void

add adapterType if its missing