package info.bukova.isspst;

import info.bukova.isspst.data.DataModel;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

public class CommonUrlResolver implements EntityUrlResolver {

	@Autowired
	private HttpServletRequest request;
	protected String defaultUrl;
	
	public CommonUrlResolver() {
		 
	}
	
	@Override
	public String entityUrl(Object entity) {
		if (defaultUrl == null) {
			this.defaultUrl = request.getRequestURL().toString();
			this.defaultUrl = defaultUrl.substring(0, defaultUrl.indexOf(request.getServletPath()));
		}
		
		if (!DataModel.class.isAssignableFrom(entity.getClass())) {
			return defaultUrl + "/app";
		}
		
		String url = Constants.URL_MAP.get(entity.getClass());
		
		if (url == null) {
			return defaultUrl + "/app";
		}
		
		return defaultUrl + url + "?select=" + String.valueOf(((DataModel)entity).getId());
	}

}
