package gov.nih.nci.cabig.ctms.web.chrome;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.util.UrlPathHelper;
import org.springframework.util.AntPathMatcher;
import org.springframework.beans.factory.annotation.Required;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Interceptor which figures out which section a requested page is from
 * and associates that section with the current request.
 * <p>
 * Adds two attributes to the request:
 * <ul>
 *   <li><kbd>currentSection</kbd> - {@link Section} containing the requested page, if one is found.
 *   <li><kbd>sections</kbd> - the list of {@link Section}s provided to this interceptor.
 * </ul>
 *
 * @author Rhett Sutphin, Priyatam
 */
public class SectionInterceptor extends HandlerInterceptorAdapter {
    private List<Section> sections;
    private String attributePrefix;
    private UrlPathHelper urlPathHelper = new UrlPathHelper();
    private AntPathMatcher pathMatcher = new AntPathMatcher();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String controllerPath = urlPathHelper.getPathWithinServletMapping(request);
        Section currentSection = findSection(urlPathHelper.getPathWithinServletMapping(request));
        Task currentTask = findTask(currentSection, controllerPath);
        
        request.setAttribute(prefix("currentSection"), currentSection);
        request.setAttribute(prefix("currentTask"), currentTask);        
        request.setAttribute(prefix("sections"), getSections());
        return true;
    }

    private Section findSection(String controllerPath) {
        for (Section section : getSections()) {
            for (String pattern : section.getPathMappings()) {
                if (pathMatcher.match(pattern, controllerPath)) {
                    return section;
                }
            }
        }
        return null;
    }
    
    private Task findTask(Section section, String controllerPath){
    	for (Task task : section.getTasks()) {
    		if (task.getUrl().indexOf(controllerPath) > -1) {
    			return task;  	                	
    		}                		
    	}
    	 return null;
    }
    

    private String prefix(String attr) {
        if (getAttributePrefix() == null) {
            return attr;
        } else {
            return getAttributePrefix() + attr;
        }
    }

    public List<Section> getSections() {
        return sections;
    }

    @Required
    public void setSections(List<Section> sections) {
        this.sections = sections;
    }

    public String getAttributePrefix() {
        return attributePrefix;
    }

    public void setAttributePrefix(String attributePrefix) {
        this.attributePrefix = attributePrefix;
    }
}

@@ -271,16 +271,101 @@
 equired;
+%0Aimport org.apache.commons.logging.LogFactory;%0Aimport org.apache.commons.logging.Log;
 %0A%0Aimport
@@ -475,16 +475,18 @@
 .List;%0A%0A
+%0A%0A
 /**%0A * I
@@ -1171,16 +1171,69 @@
 tcher();
+%0A    private Log log = LogFactory.getLog(getClass());
 %0A%0A    @O
@@ -1362,16 +1362,89 @@
 ption %7B%0A
+        log.debug(%22preHandle invoked%22);%0A        Task currentTask = null;%0A
         
@@ -1617,24 +1617,25 @@
 uest));%0A
+%0A
         
 Task cur
@@ -1618,36 +1618,73 @@
 est));%0A%0A        
-Task
+if (currentSection != null) %7B%0A           
  currentTask = f
@@ -1724,32 +1724,34 @@
 rPath);%0A        
+%7D%0A
 %0A        request
@@ -2385,21 +2385,70 @@
 rPath)%7B%0A
+%0A        if(section.getTasks() != null) %7B%0A        
     
-%09
 for (Tas
@@ -2482,18 +2482,28 @@
 ) %7B%0A    
-%09%09
+            
 if (task
@@ -2552,11 +2552,24 @@
     
-%09%09%09
+                
 retu
@@ -2580,15 +2580,9 @@
 ask;
-  %09    
+%0A
     
@@ -2593,17 +2593,14 @@
     
-%09%0A
     
-%09%09
 %7D
+%0A
     
@@ -2611,28 +2611,25 @@
     
+%7D%0A
     
-%09%09%0A
     
-%09
 %7D%0A    %09
- 
 retu

add null check for if there is no current section specified and if there are no tasks specified in a section