Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* Copyright 2008-2009 Graeme Rocher
   *
   * 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.codehaus.groovy.grails.scaffolding.view;
 
 import java.util.List;
 import java.util.Map;
 
Overrides the default Grails view resolver and resolves scaffolded views at runtime.

Author(s):
Graeme Rocher
Since:
1.1
 
 public class ScaffoldingViewResolver extends GrailsViewResolver implements ApplicationContextAware {
 
     Map<StringList<String>> scaffoldedActionMap = Collections.emptyMap();
     Map<StringGrailsDomainClassscaffoldedDomains = Collections.emptyMap();
 
     static final Map<ViewKeyViewscaffoldedViews = new ConcurrentHashMap<ViewKeyView>();
     static final Log LOG = LogFactory.getLog(ScaffoldingViewResolver.class);

    
Clears any cached scaffolded views.
 
     public static void clearViewCache() {
         .clear();
     }
 
     @Override
     protected View loadView(String viewNameLocale localethrows Exception {
         final View resolvedView = super.loadView(viewNamelocale);
         if ( == null || resolvedView instanceof GroovyPageView) {
             return resolvedView;
         }
 
         GrailsWebRequest webRequest = WebUtils.retrieveGrailsWebRequest();
         List<StringcontrollerActions = .get(webRequest.getControllerName());
         if (controllerActions != null && controllerActions.contains(webRequest.getActionName())) {
             GrailsDomainClass domainClass = .get(webRequest.getControllerName());
             if (domainClass != null) {
                 String viewFileName;
                 final int i = viewName.lastIndexOf('/');
                 if (i > -1) {
                     viewFileName = viewName.substring(iviewName.length());
                 }
                 else {
                     viewFileName = viewName;
                 }
                 final ViewKey viewKey = new ViewKey(webRequest.getControllerName(), viewFileName);
                 View v = .get(viewKey);
                 if (v == null) {
                     String viewCode = null;
                     try {
                         viewCode = generateViewSource(viewFileNamedomainClass);
                     }
                     catch (Exception e) {
                         .error("Error generating scaffolded view [" + viewName + "]: " + e.getMessage(),e);
                         return resolvedView;
                     }
                     if(StringUtils.hasLengthviewCode )) {
                         v = createScaffoldedView(viewNameviewCode);
                         .put(viewKeyv);
                     }
                 }
                 if (v != null) {
                     return v;
                 }
             }
         }
         return resolvedView;
    }
    protected View createScaffoldedView(String viewNameString viewCodethrows Exception {
        final ScaffoldedGroovyPageView view = new ScaffoldedGroovyPageView(viewNameviewCode);
        view.setServletContext(getServletContext());
        view.setTemplateEngine();
        view.afterPropertiesSet();
        return view;
    }
    protected String generateViewSource(String viewNameGrailsDomainClass domainClass) {
        StringWriter sw = new StringWriter();
        .generateView(domainClassviewName,sw);
        return sw.toString();
    }
    private class ViewKey {
        private String controller;
        private String action;
        ViewKey(String controllerString action) {
            this. = controller;
            this. = action;
        }
        @Override
        public boolean equals(Object o) {
            if (this == oreturn true;
            if (o == null || getClass() != o.getClass()) return false;
            ViewKey viewKey = (ViewKeyo;
            if ( != null ? !.equals(viewKey.action) : viewKey.action != nullreturn false;
            if (!.equals(viewKey.controller)) return false;
            return true;
        }
        @Override
        public int hashCode() {
            int result = .hashCode();
            result = 31 * result + ( != null ? .hashCode() : 0);
            return result;
        }
    }
    public void setTemplateGenerator(GrailsTemplateGenerator templateGenerator) {
        this. = templateGenerator;
    }
    @SuppressWarnings({ "unchecked""rawtypes" })
    public void setScaffoldedActionMap(Map scaffoldedActionMap) {
        this. = scaffoldedActionMap;
    }
    @SuppressWarnings({ "unchecked""rawtypes" })
    public void setScaffoldedDomains(Map scaffoldedDomains) {
        this. = scaffoldedDomains;
    }
New to GrepCode? Check out our FAQ X