1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
75
76
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
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
131
132
133 public List<Category> getComboCategories()
134 {
135 List<Category> listCategorys = (List<Category>) CategoryHome.getCategorysList( );
136
137 return listCategorys;
138 }
139
140
141
142
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
171
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 }