package org.lucee.mongodb.cache;

import java.io.IOException;
import java.io.Serializable;

import org.lucee.mongodb.util.SerializerUtil;
import org.lucee.mongodb.util.print;

import com.mongodb.BasicDBObject;

public class MongoDBCacheDocument implements Serializable {

	private BasicDBObject dbObject;
	
	public MongoDBCacheDocument(BasicDBObject dbObject){
		this.dbObject = dbObject;
	}

	public String getId(){
		return dbObject.getString("_id");
	}

	public void setValue(Object value) throws IOException {
		dbObject.put("data",SerializerUtil.serialize(value));
	}
	
	public Object getValue() throws ClassNotFoundException, IOException{
		return getValue(dbObject);
	}

	public static Object getValue(BasicDBObject dbObject) throws ClassNotFoundException, IOException{
		return SerializerUtil.evaluate(dbObject.getString("data"));
	}

	public void setKey(String value) {
		dbObject.put("key",value);
	}
	
	public String getKey(){
		return dbObject.getString("key");
	}
	
	public String getCraetedOn(){
		return dbObject.getString("createdOn");
	}
	
	public void setCraetedOn(long created){
		dbObject.put("createdOn",created);
	}
	

	
	
	public void setLastAccessed(long value) {
		dbObject.put("lastAccessed",value);
	}
	
	public long getLastAccessed(){
		return dbObject.getLong("lastAccessed");
	}

	public void setLastUpdated(long value) {
		dbObject.put("lastUpdated",value);
	}
	
	public long getLastUpdated(){
		return dbObject.getLong("lastUpdated");
	}
	
	/*public void setLifeSpan(long value) {
		dbObject.put("lifeSpan",value);
	}
	
	public long getLifeSpan(){
		return dbObject.getLong("lifeSpan");
	}*/
	
	public void setTimeIdle(long value) {
		dbObject.put("timeIdle",value);
	}
	
	public long getTimeIdle(){
		return dbObject.getLong("timeIdle");
	}

	public void setHits(int value) {
		dbObject.put("hits",value);
	}
	
	public int getHits(){
		int hits = dbObject.getInt("hits",0);
		return hits;
	}

	public void setExpires(long value) {
		print.ds("set-expires:\n"+value+"\n"+((int)value));
		dbObject.put("expires",value);
	}
	
	public long getExpires(){
		return dbObject.getLong("expires"); 		
	}

	public BasicDBObject getDbObject(){
		return dbObject;
	}
	
	public void addHit(){
		int hits = getHits();
		hits++;
		setHits(hits);		
	}
}

@@ -134,45 +134,8 @@
 til;
-%0Aimport org.lucee.mongodb.util.print;
 %0A%0Aim
@@ -228,16 +228,87 @@
 able %7B%0A%0A
+%09private static final long serialVersionUID = -7033308305053665899L;%0A%09%0A
 %09private
@@ -1995,62 +1995,8 @@
 ) %7B%0A
-%09%09print.ds(%22set-expires:%5Cn%22+value+%22%5Cn%22+((int)value));%0A
 %09%09db

clean up