View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.graphite.web;
35  
36  import fr.paris.lutece.plugins.graphite.business.Category;
37  import fr.paris.lutece.plugins.graphite.business.CategoryHome;
38  import fr.paris.lutece.plugins.graphite.business.GraphHome;
39  import fr.paris.lutece.portal.service.security.SecurityService;
40  import fr.paris.lutece.portal.service.util.AppPropertiesService;
41  import fr.paris.lutece.portal.web.xpages.XPage;
42  import fr.paris.lutece.portal.util.mvc.xpage.MVCApplication;
43  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
44  import fr.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
45  import java.util.ArrayList;
46  import java.util.Collection;
47  import java.util.List;
48  import java.util.Map;
49  
50  import javax.servlet.http.HttpServletRequest;
51  import org.apache.commons.collections.CollectionUtils;
52  import org.apache.commons.lang.StringUtils;
53  
54  /**
55   * This class provides a simple implementation of an XPage
56   */
57   
58  @Controller( xpageName = "graphite" , pageTitleProperty = "graphite.pageTitle" , pagePathProperty = "graphite.pagePathLabel" )
59  public class Graphite extends MVCApplication
60  {
61      private static final String TEMPLATE_XPAGE = "/skin/plugins/graphite/graphite.html";
62      private static final String VIEW_HOME = "home";
63      
64      private static final String PROPERTY_DEFAULT_ROLE_CODE = "defaultRole.code";
65      
66      private static final String MARK_CATEGORIES_COMBO = "combo_categories";
67      private static final String MARK_CATEGORY = "categorie";
68      
69      private static final String MARKER_GRAPHS_LIST = "graphs_list";
70      private static final String MARKER_ROLE = "role";
71      
72      private Category _category;
73      /**
74       * Returns the content of the page Graphite.
75       * @param request The HTTP request
76       * @return The view
77       */
78      @View( value = VIEW_HOME, defaultView = true )
79      public XPage viewHome( HttpServletRequest request )
80      {
81          
82          String strIdCategory = request.getParameter( "category" );
83          int nId =-1;
84          
85          if(!StringUtils.isEmpty(strIdCategory))
86          {
87              nId = Integer.parseInt( strIdCategory );
88          }
89          else
90          {
91              //default category
92              List<Category> listCategorys = getAuthorizedCategory(request);
93              if(!CollectionUtils.isEmpty(listCategorys))
94              {
95                  boolean find = false;
96                  for(Category c : listCategorys)
97                  {
98                      if(find == false)
99                      {
100                         if(c.getDisplayFront()== 1)
101                         {
102                             nId=c.getIdCategory();
103                             find = true;
104                         }
105                     }
106                 }
107                 if(find == false)
108                 {
109                     nId=listCategorys.get(0).getIdCategory();
110                 }
111             }
112         }
113         
114         Map<String, Object> model = getModel(  );
115         
116         if (nId != -1)
117         {
118             _category = CategoryHome.findByPrimaryKey( nId );
119             
120             model.put( MARK_CATEGORY, _category);
121             model.put( MARKER_GRAPHS_LIST, GraphHome.getGraphsList(  ) );
122             model.put( MARK_CATEGORIES_COMBO, getAuthorizedCategory(request));
123             model.put( MARKER_ROLE, isAuthorized(request, _category));
124         }
125         
126         return getXPage( TEMPLATE_XPAGE, request.getLocale(  ), model );
127     }
128     
129     /**
130     * Returns the categories
131     * @return the categories
132     */
133     public List<Category> getComboCategories()
134     {
135         List<Category> listCategorys = (List<Category>) CategoryHome.getCategorysList(  );
136         
137         return listCategorys;
138     }
139     
140     /**
141     * Returns the Authorized Collection
142     * @return the Authorized Collection
143     */
144     public List<Category> getAuthorizedCategory(HttpServletRequest request)
145     {
146         List<Category> listAllCategorys = getComboCategories();
147         Collection<Category> listCategorys = new ArrayList<Category>(  );
148         
149         if(SecurityService.isAuthenticationEnable())
150         {
151             for ( Category c : listAllCategorys)
152             {
153                 if(SecurityService.getInstance().isUserInRole(request, c.getCategoryRole()) || c.getCategoryRole().equals(AppPropertiesService.getProperty( PROPERTY_DEFAULT_ROLE_CODE )))
154                 {
155                     listCategorys.add( c );
156                 }
157             }
158         }
159         else
160         {
161             for ( Category c : listAllCategorys)
162             {
163                 listCategorys.add( c );
164             }
165         }
166         return (List<Category>) listCategorys;
167     }
168     
169     /**
170     * Returns a boolean for the authorisation
171     * @return the boolean
172     */
173     public boolean isAuthorized(HttpServletRequest request, Category category)
174     {
175         boolean isRole = true;
176         
177         if(SecurityService.isAuthenticationEnable())
178         {
179             isRole = SecurityService.getInstance().isUserInRole(request, category.getCategoryRole());
180             if(category.getCategoryRole().equals(AppPropertiesService.getProperty( PROPERTY_DEFAULT_ROLE_CODE )))
181             {
182                 isRole = true;
183             }
184         }
185         return isRole;
186     }
187 }