/*
 * Copyright 2010 Lincoln Baxter, III
 * 
 * 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 com.ocpsoft.pretty.faces.beans;

import java.util.List;

import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ocpsoft.pretty.PrettyContext;
import com.ocpsoft.pretty.PrettyException;
import com.ocpsoft.pretty.faces.config.mapping.PathParameter;
import com.ocpsoft.pretty.faces.config.mapping.QueryParameter;
import com.ocpsoft.pretty.faces.config.mapping.UrlMapping;
import com.ocpsoft.pretty.faces.url.URL;
import com.ocpsoft.pretty.faces.util.FacesElUtils;
import com.ocpsoft.pretty.faces.util.FacesStateUtils;

/**
 * @author Lincoln Baxter, III <lincoln@ocpsoft.com>
 */
public class ParameterInjector
{
   private static final Log log = LogFactory.getLog(ParameterInjector.class);
   private static final FacesElUtils elUtils = new FacesElUtils();

   public void injectParameters(final FacesContext context)
   {
      log.trace("Injecting parameters");
      PrettyContext prettyContext = PrettyContext.getCurrentInstance();
      URL url = prettyContext.getRequestURL();
      UrlMapping mapping = prettyContext.getConfig().getMappingForUrl(url);

      if (mapping != null)
      {
         injectPathParams(context, url, mapping);
         injectQueryParams(context, mapping);
      }
   }

   private void injectPathParams(final FacesContext context, final URL url, final UrlMapping mapping)
   {

      // skip path parameter injection due to onPostback attribute?
      if (!mapping.isOnPostback() && FacesStateUtils.isPostback(context))
      {
         return;
      }

      List<PathParameter> params = mapping.getPatternParser().parse(url);
      for (PathParameter param : params)
      {
         String el = param.getExpression().getELExpression();
         if ((el != null) && !"".equals(el.trim()))
         {
            String value = param.getValue();
            try
            {
               elUtils.setValue(context, el, value);
            }
            catch (Exception e)
            {
               throw new PrettyException("PrettyFaces: Exception occurred while processing <" + mapping.getId() + ":"
                        + el + "> for URL <" + url + ">", e);
            }
         }
      }
   }

   private void injectQueryParams(final FacesContext context, final UrlMapping mapping)
   {
      boolean isPostback = FacesStateUtils.isPostback(context);
      List<QueryParameter> params = mapping.getQueryParams();
      for (QueryParameter param : params)
      {
         // check if to skip this QueryParameter due to onPostback attribute
         if (!param.isOnPostback() && isPostback)
         {
            continue;
         }

         String el = param.getExpression().getELExpression();
         if ((el != null) && !"".equals(el.trim()))
         {
            String name = param.getName();
            if (context.getExternalContext().getRequestParameterMap().containsKey(name))
            {
               try
               {
                  if (elUtils.getExpectedType(context, el).isArray())
                  {
                     String[] values = context.getExternalContext().getRequestParameterValuesMap().get(name);
                     elUtils.setValue(context, el, values);
                  }
                  else
                  {
                     String value = context.getExternalContext().getRequestParameterMap().get(name);
                     elUtils.setValue(context, el, value);
                  }
               }
               catch (Exception e)
               {
                  throw new PrettyException(
                           "PrettyFaces: Exception occurred while processing mapping<" + mapping.getId() + ":" + el
                                    + "> for query parameter named<" + name + "> " + e.getMessage(), e);
               }
            }
         }
      }
   }

}

@@ -1060,16 +1060,65 @@
 apping;%0A
+import com.ocpsoft.pretty.faces.url.QueryString;%0A
 import c
@@ -1150,16 +1150,16 @@
 rl.URL;%0A
-
 import c
@@ -1918,32 +1918,47 @@
 context, mapping
+, prettyContext
 );%0A      %7D%0A   %7D%0A
@@ -2979,24 +2979,59 @@
 ping mapping
+, final PrettyContext prettyContext
 )%0A   %7B%0A     
@@ -3147,24 +3147,95 @@
 ryParams();%0A
+      QueryString queryString = prettyContext.getRequestQueryString();%0A
       for (Q
@@ -3632,46 +3632,22 @@
 if (
-context.getExternalContext().getReques
+queryString.ge
 tPar
@@ -3860,46 +3860,22 @@
 s = 
-context.getExternalContext().getReques
+queryString.ge
 tPar
@@ -3886,25 +3886,16 @@
 erValues
-Map().get
 (name);%0A
@@ -4057,65 +4057,32 @@
 e = 
-context.getExternalContext().getRequestParameterMap().get
+queryString.getParameter
 (nam

Use QueryString class to parse query string for injection