package org.orienteer.pages;

import org.apache.wicket.core.request.handler.IPageClassRequestHandler;
import org.apache.wicket.request.Url;
import org.apache.wicket.request.mapper.parameter.IPageParametersEncoder;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.request.mapper.parameter.PageParametersEncoder;
import org.apache.wicket.util.string.StringValue;

import com.orientechnologies.orient.core.id.ORID;

/**
 * {@link IPageParametersEncoder} for transparent adding page Id
 */
public class OPageParametersEncoder extends PageParametersEncoder {

	public static final String PAGE_IDENTITY = "__orienteerPageId__";
	
	private final ORID pageIdentity;
	
	public OPageParametersEncoder(ORID pageIdentity) {
		this.pageIdentity = pageIdentity;
	}
	@Override
	public PageParameters decodePageParameters(Url url) {
		PageParameters ret =  super.decodePageParameters(url);
		if(ret==null) ret = new PageParameters();
		ret.add(PAGE_IDENTITY, pageIdentity);
		return ret;
	}

	@Override
	public Url encodePageParameters(PageParameters pageParameters) {
		StringValue sv = pageParameters.get(PAGE_IDENTITY);
		if(!sv.isEmpty()) {
			pageParameters.remove(PAGE_IDENTITY);
		}
		Url ret = super.encodePageParameters(pageParameters);
		if(!sv.isEmpty()) {
			pageParameters.add(PAGE_IDENTITY, sv.toString());
		}
		return ret;
	}
	
	public static boolean matchHandler(ORID pageIdentity, IPageClassRequestHandler requestHandler) {
		StringValue sv = requestHandler.getPageParameters().get(PAGE_IDENTITY);
		return pageIdentity!=null?pageIdentity.toString().equals(sv.toOptionalString()):false;
	}
	
}
@@ -1463,30 +1463,37 @@
 er) %7B%0A%09%09
-StringValue sv
+PageParameters params
  = reque
@@ -1521,16 +1521,56 @@
 meters()
+;%0A%09%09StringValue sv = params!=null?params
 .get(PAG
@@ -1576,24 +1576,29 @@
 GE_IDENTITY)
+:null
 ;%0A%09%09return p
@@ -1595,16 +1595,28 @@
 %09%09return
+ sv!=null &&
  pageIde

Fix NPE in OPages