View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.surf.mvc;
20  
21  import java.util.Locale;
22  
23  import org.springframework.extensions.surf.FrameworkUtil;
24  import org.springframework.extensions.surf.RequestContext;
25  import org.springframework.extensions.surf.WebFrameworkConstants;
26  import org.springframework.extensions.surf.types.Page;
27  import org.springframework.extensions.surf.types.PageType;
28  import org.springframework.extensions.surf.types.Theme;
29  import org.springframework.web.servlet.view.AbstractUrlBasedView;
30  
31  /**
32   * Resolver for web framework Page Type views
33   * 
34   * @author muzquiano
35   */
36  public class PageTypeViewResolver extends AbstractWebFrameworkViewResolver 
37  {
38      private static final String URI_PREFIX_PAGE_TYPE = "type/";
39      
40      public PageTypeViewResolver() 
41      {
42          super();
43      }
44      
45      /* (non-Javadoc)
46       * @see org.springframework.web.servlet.view.UrlBasedViewResolver#canHandle(java.lang.String, java.util.Locale)
47       */
48      protected boolean canHandle(String viewName, Locale locale) 
49      {
50          boolean canHandle = false;
51          
52          if (!canHandle && viewName.startsWith(URI_PREFIX_PAGE_TYPE))
53          {
54              String pageTypeId = viewName.substring(URI_PREFIX_PAGE_TYPE.length());
55              
56              // check whether the page type exists
57              PageType pageType = FrameworkUtil.getCurrentRequestContext().getModel().getPageType(pageTypeId);
58              canHandle = (pageType != null);
59          }
60          
61          return canHandle;
62      }
63      
64  
65      /* (non-Javadoc)
66       * @see org.springframework.web.servlet.view.UrlBasedViewResolver#buildView(java.lang.String)
67       */
68      protected AbstractUrlBasedView buildView(String viewName) throws Exception 
69      {
70          AbstractUrlBasedView view = null;
71          
72          // request context
73          RequestContext context = FrameworkUtil.getCurrentRequestContext();        
74  
75          if (viewName.startsWith(URI_PREFIX_PAGE_TYPE))
76          {
77              String pageTypeId = viewName.substring(URI_PREFIX_PAGE_TYPE.length());
78              
79              // get the page type
80              PageType pageType = FrameworkUtil.getCurrentRequestContext().getModel().getPageType(pageTypeId);
81   
82              // determine which page to use based on requested type
83              String pageId = null;
84              
85              // theme binding
86              String themeId = (String) context.getThemeId();
87              Theme theme = context.getModel().getTheme(themeId);
88              if (theme != null)
89              {
90                  pageId = theme.getPageId(pageTypeId);
91              }
92              
93              // system default page            
94              if (pageId == null)
95              {
96                  pageId = FrameworkUtil.getWebFrameworkConfiguration().getDefaultPageTypeInstanceId(pageTypeId);
97              }
98              
99              // use a generic page
100             if (pageId == null)
101             {
102                 pageId = FrameworkUtil.getWebFrameworkConfiguration().getDefaultPageTypeInstanceId(WebFrameworkConstants.GENERIC_PAGE_TYPE_DEFAULT_PAGE_ID);
103             }   
104             
105             // build a page view
106             Page page = FrameworkUtil.getCurrentRequestContext().getModel().getPage(pageId);
107             if (page != null)
108             {
109                 view = new PageView(getServiceRegistry());            
110                 view.setUrl(pageId);
111             }            
112         }
113         
114         return view;
115     }
116 }