/*
 * Copyright 2008-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.broadleafcommerce.openadmin.web.processor;

import org.broadleafcommerce.common.web.dialect.AbstractModelVariableModifierProcessor;
import org.broadleafcommerce.openadmin.server.security.domain.AdminSection;
import org.broadleafcommerce.openadmin.server.security.service.AdminNavigationService;
import org.springframework.context.ApplicationContext;
import org.thymeleaf.Arguments;
import org.thymeleaf.dom.Element;
import org.thymeleaf.spring3.context.SpringWebContext;
import org.thymeleaf.standard.expression.StandardExpressionProcessor;

import javax.servlet.http.HttpServletRequest;

/**
 * A Thymeleaf processor that will generate the HREF of a given Admin Section.
 * This is useful in constructing the left navigation menu for the admin console.
 *
 * @author elbertbautista
 */
public class AdminSectionHrefProcessor extends AbstractModelVariableModifierProcessor {

    /**
     * Sets the name of this processor to be used in Thymeleaf template
     */
    public AdminSectionHrefProcessor() {
        super("admin_section_href");
    }

    @Override
    public int getPrecedence() {
        return 10002;
    }

    @Override
    protected void modifyModelAttributes(Arguments arguments, Element element) {
        String href = "#";
        String resultVar = element.getAttributeValue("resultVar");
        String sectionStr = element.getAttributeValue("section");

        AdminSection section = (AdminSection) StandardExpressionProcessor.processExpression(arguments, sectionStr);
        if (section == null) {
            addToModel(arguments, resultVar, href);
            return;
        }

        HttpServletRequest request = ((SpringWebContext) arguments.getContext()).getHttpServletRequest();
        String context = request.getContextPath();
        String gwtDebug = "";

        if (request.getParameter("gwt.codesvr") != null ) {
            gwtDebug = "?gwt.codesvr=" + request.getParameter("gwt.codesvr");
        }

        href = context + section.getUrl() + gwtDebug + "#moduleKey=" + section.getModule().getModuleKey() + "&pageKey=" + section.getSectionKey();
        addToModel(arguments, resultVar, href);
    }

}

@@ -705,283 +705,85 @@
 rce.
-common.web.dialect.AbstractModelVariableModifierProcessor;%0Aimport org.broadleafcommerce.openadmin.server.security.domain.AdminSection;%0Aimport org.broadleafcommerce.openadmin.server.security.service.AdminNavigationService;%0Aimport org.springframework.context.ApplicationContext
+openadmin.server.security.domain.AdminSection;%0Aimport org.thymeleaf.Arguments
 ;%0Aim
@@ -797,33 +797,35 @@
 g.thymeleaf.
-Argu
+dom.Ele
 ment
-s
 ;%0Aimport org
@@ -831,35 +831,77 @@
 g.thymeleaf.
-dom.Element
+processor.attr.AbstractAttributeModifierAttrProcessor
 ;%0Aimport org
@@ -1063,16 +1063,65 @@
 quest;%0A%0A
+import java.util.HashMap;%0Aimport java.util.Map;%0A%0A
 /**%0A * A
@@ -1369,20 +1369,16 @@
 ract
-ModelVariabl
+Attribut
 eMod
@@ -1382,16 +1382,20 @@
 Modifier
+Attr
 Processo
@@ -1680,24 +1680,39 @@
 ted 
-void modifyModel
+Map%3CString, String%3E getModified
 Attr
@@ -1716,16 +1716,21 @@
 ttribute
+Value
 s(Argume
@@ -1763,181 +1763,66 @@
 ment
-) %7B%0A        String href = %22#%22;%0A        String resultVar = element.getAttributeValue(%22resultVar%22);%0A        String sectionStr = element.getAttributeValue(%22section%22);%0A%0A        
+, String attributeName) %7B%0A        String href = %22#%22;%0A%09%09%0A%09%09
 Admi
@@ -1916,18 +1916,48 @@
 ts, 
-sectionStr
+element.getAttributeValue(attributeName)
 );%0A 
@@ -1975,17 +1975,17 @@
 section 
-=
+!
 = null) 
@@ -1994,87 +1994,8 @@
     
-        addToModel(arguments, resultVar, href);%0A            return;%0A        %7D%0A%0A
     
@@ -2108,82 +2108,402 @@
     
-String context = request.getContextPath();%0A        String gwtDebug = %22%22;%0A%0A
+    %0A            // The url is based on the current context%0A            String context = request.getContextPath();%0A            href = context + section.getUrl();%0A            %0A            // If we're using the default handler, we're loading a GWT module%0A            if (section.getUseDefaultHandler()) %7B%0A                %0A                // Append the GWT dev mode parameter if it exists%0A        
     
@@ -2570,25 +2570,30 @@
         
-gwtDebug 
+        href +
 = %22?gwt.
@@ -2653,65 +2653,106 @@
     
-%7D%0A%0A        href = context + section.getUrl() + gwtDebug
+        %7D%0A        %0A                // Append the necessary GWT module keys%0A                href
  +
+=
  %22#m
@@ -2852,46 +2852,669 @@
     
-addToModel(arguments, resultVar, href)
+    %7D%0A        %7D%0A%09%09%0A%09%09Map%3CString, String%3E attrs = new HashMap%3CString, String%3E();%0A%09%09attrs.put(%22href%22, href);%0A%09%09return attrs;%0A    %7D%0A%0A    @Override%0A    protected ModificationType getModificationType(Arguments arguments, Element element, String attributeName, String newAttributeName) %7B%0A        return ModificationType.SUBSTITUTION;%0A    %7D%0A%0A    @Override%0A    protected boolean removeAttributeIfEmpty(Arguments arguments, Element element, String attributeName, String newAttributeName) %7B%0A        return true;%0A    %7D%0A%0A    @Override%0A    protected boolean recomputeProcessorsAfterExecution(Arguments arguments, Element element, String attributeName) %7B%0A        return false
 ;%0A  

Remove the module and section keys from non-GWT admin pages