package org.orienteer;

import org.apache.wicket.Session;
import org.apache.wicket.request.Request;
import org.orienteer.modules.PerspectivesModule;

import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.record.impl.ODocument;

import ru.ydn.wicket.wicketorientdb.OrientDbWebSession;

public class OrienteerWebSession extends OrientDbWebSession
{
	private OIdentifiable perspective;
	
	public OrienteerWebSession(Request request)
	{
		super(request);
	}
	
	public static OrienteerWebSession get()
	{
		return (OrienteerWebSession)Session.get();
	}
	
	@Override
	public boolean authenticate(String username, String password) {
		boolean ret = super.authenticate(username, password);
		if(ret)
		{
			perspective=null;
		}
		return ret;
	}

	@Override
	public void signOut() {
		perspective=null;
		super.signOut();
	}

	public OrienteerWebSession setPerspecive(ODocument perspective)
	{
		this.perspective = perspective;
		return this;
	}
	
	public ODocument getPerspective()
	{
		if(perspective instanceof ODocument)
		{
			return (ODocument) perspective;
		}
		else
		{
			if(perspective!=null)
			{
				perspective = perspective.getRecord();
				return (ODocument)perspective;
			}
			else
			{
				OrienteerWebApplication app = OrienteerWebApplication.get();
				PerspectivesModule perspectiveModule = app.getServiceInstance(PerspectivesModule.class);
				perspective = perspectiveModule.getDefaultPerspective(getDatabase(), getUser());
				return (ODocument)perspective;
			}
			
		}
	}

	@Override
	public void detach() {
		if(perspective!=null) perspective = perspective.getIdentity();
		super.detach();
	}
	
	
	

}

@@ -1141,26 +1141,17 @@
 e!=null)
-%0A%09%09%09%7B%0A%09%09%09%09
+ 
 perspect
@@ -1188,52 +1188,29 @@
 %0A%09%09%09
-%09return (ODocument)perspective;%0A%09%09%09%7D%0A%09%09%09else
+if(perspective==null)
 %0A%09%09%09
@@ -1455,16 +1455,20 @@
 er());%0A%09
+%09%09%7D%0A
 %09%09%09retur
@@ -1493,21 +1493,16 @@
 ective;%0A
-%09%09%09%7D%0A
 %09%09%09%0A%09%09%7D%0A

Allow delete default perspective in runtime. Issue #150