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.portal.web.documentation;
35  
36  import fr.paris.lutece.portal.business.right.FeatureGroup;
37  import fr.paris.lutece.portal.business.right.FeatureGroupHome;
38  import fr.paris.lutece.portal.business.right.Right;
39  import fr.paris.lutece.portal.business.user.AdminUser;
40  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
41  import fr.paris.lutece.portal.service.admin.AdminUserService;
42  import fr.paris.lutece.portal.service.html.XmlTransformerService;
43  import fr.paris.lutece.portal.service.message.AdminMessage;
44  import fr.paris.lutece.portal.service.message.AdminMessageService;
45  import fr.paris.lutece.portal.service.template.AppTemplateService;
46  import fr.paris.lutece.portal.service.util.AppLogService;
47  import fr.paris.lutece.portal.service.util.AppPathService;
48  import fr.paris.lutece.util.html.HtmlTemplate;
49  
50  import java.io.File;
51  
52  import java.util.ArrayList;
53  import java.util.Collection;
54  import java.util.HashMap;
55  import java.util.List;
56  import java.util.Locale;
57  import java.util.Map;
58  
59  import javax.servlet.http.HttpServletRequest;
60  
61  import javax.xml.transform.stream.StreamSource;
62  
63  
64  /**
65   *
66   * Classe for display the admin features documentation
67   *
68   */
69  public class AdminDocumentationJspBean
70  {
71      //xsl
72      private static final String XSL_PATH = "admin_documentation.xsl";
73  
74      //xsl paramaters
75      private static final String PARAMS_LOCAL = "locale";
76      private static final String PARAMS_DEFAULT_LOCAL = "default_locale";
77  
78      //parameters
79      private static final String PARAMETER_FEATURE_DOC = "doc";
80  
81      //jsp
82      private static final String JSP_CLOSE = "javascript:window.close()";
83  
84      //templates
85      private static final String TEMPLATE_ADMIN_SUMMARY_DOCUMENTATION = "admin/documentation/admin_summary_documentation.html";
86  
87      //bookmark
88      private static final String BOOKMARK_FEATURE_GROUP_LIST = "feature_group_list";
89      private static final String BOOKMARK_HELP_ICON = "help_icon";
90  
91      //images
92      private static final String IMAGE_HELP_PATH = "images/admin/skin/features/admin_help.png";
93  
94      //properties
95      private static final String PROPERTY_XSL_BASE_PATH = "lutece.documentation.xsl.path";
96      private static final String ADMIN_DOCUMENTATION_XSL_UNIQUE_PREFIX = "adminDocumentation-";
97  
98      //messages
99      private static final String MESSAGE_ERROR = "portal.features.documentation.message.error";
100 
101     //utils
102     private static final String LOCAL_DEFAULT = "en";
103     private static final String XML_BASE_PATH = "/doc/xml/";
104     private static final String XML_USER_PATH = "/xdoc/user/";
105     private static final String FEATURES_GROUP_SYSTEM = "SYSTEM";
106 
107     /**
108     * Returns the view of features documentation
109     *
110     * @param request The request
111      * @return The HTML documentation
112      * @throws AccessDeniedException If the access is refused to the user
113     */
114     public String getDocumentation( HttpServletRequest request )
115         throws AccessDeniedException
116     {
117         String strFeature = request.getParameter( PARAMETER_FEATURE_DOC );
118 
119         AdminUser user = AdminUserService.getAdminUser( request );
120         Locale locale = user.getLocale(  );
121 
122         //get the xsl file
123         String strXslPath = AppPathService.getPath( PROPERTY_XSL_BASE_PATH, XSL_PATH );
124         File fileXsl = new File( strXslPath );
125         StreamSource sourceStyleSheet = new StreamSource( fileXsl );
126 
127         //get the xml documentation file
128         String strXmlPath;
129         StreamSource sourceXml;
130         String strLocal = locale.toString(  );
131 
132         if ( ( locale == null ) || strLocal.equals( LOCAL_DEFAULT ) )
133         {
134             strXmlPath = AppPathService.getWebAppPath(  ) + XML_BASE_PATH + XML_USER_PATH + strFeature + ".xml";
135         }
136         else
137         {
138             strXmlPath = AppPathService.getWebAppPath(  ) + XML_BASE_PATH + locale.toString(  ) + XML_USER_PATH +
139                 strFeature + ".xml";
140         }
141 
142         sourceXml = new StreamSource( new File( strXmlPath ) );
143 
144         String strHtmlDoc = null;
145 
146         Map<String, String> params = new HashMap<String, String>(  );
147         params.put( PARAMS_LOCAL, locale.toString(  ) );
148         params.put( PARAMS_DEFAULT_LOCAL, LOCAL_DEFAULT );
149 
150         XmlTransformerService xmlTransformerService = new XmlTransformerService(  );
151         String strUniqueId = ADMIN_DOCUMENTATION_XSL_UNIQUE_PREFIX + strXmlPath;
152 
153         try
154         {
155             strHtmlDoc = xmlTransformerService.transformBySourceWithXslCache( sourceXml, sourceStyleSheet, strUniqueId,
156                     params, null );
157         }
158         catch ( Exception e )
159         {
160             AppLogService.error( "Can't parse XML: " + e.getMessage(  ), e );
161 
162             return null;
163         }
164 
165         return strHtmlDoc;
166     }
167 
168     /**
169      * Returns an error message when an error occured
170      *
171      * @param request The request
172      * @return The URL of message
173      */
174     public String doAdminMessage( HttpServletRequest request )
175     {
176         return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR, JSP_CLOSE, AdminMessage.TYPE_ERROR );
177     }
178 
179     /**
180      * Returns the view of summary documentation
181      *
182      * @param request The request
183      * @return The HTML documentation
184      */
185     public String getSummaryDocumentation( HttpServletRequest request )
186     {
187         AdminUser user = AdminUserService.getAdminUser( request );
188 
189         List<FeatureGroup> listFeatureGroups = getFeatureGroupsList( user );
190         Map<String, Object> model = new HashMap<String, Object>(  );
191 
192         model.put( BOOKMARK_FEATURE_GROUP_LIST, listFeatureGroups );
193         model.put( BOOKMARK_HELP_ICON, IMAGE_HELP_PATH );
194 
195         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_ADMIN_SUMMARY_DOCUMENTATION,
196                 user.getLocale(  ), model );
197 
198         return template.getHtml(  );
199     }
200 
201     /**
202      * Returns an array that contains all feature groups corresponding to the user
203      *
204      * @param user The user
205      * @return A list of FeatureGroup objects
206      */
207     private List<FeatureGroup> getFeatureGroupsList( AdminUser user )
208     {
209         // structure that will be returned
210         ArrayList<FeatureGroup> aOutFeatureGroupList = new ArrayList<FeatureGroup>(  );
211 
212         // get the list of user's features
213         Map<String, Right> featuresMap = user.getRights(  );
214         Collection<Right> features = featuresMap.values(  );
215 
216         // for each group, load the features
217         for ( FeatureGroup featureGroup : FeatureGroupHome.getFeatureGroupsList(  ) )
218         {
219             ArrayList<Right> aLeftFeatures = new ArrayList<Right>(  );
220 
221             for ( Right right : features )
222             {
223                 right.setLocale( user.getLocale(  ) );
224 
225                 String strFeatureGroup = right.getFeatureGroup(  );
226                 String strUrlDocumentation = right.getDocumentationUrl(  );
227 
228                 if ( featureGroup.getId(  ).equalsIgnoreCase( strFeatureGroup ) && ( strUrlDocumentation != null ) &&
229                         !( strUrlDocumentation.equals( "" ) ) )
230                 {
231                     featureGroup.addFeature( right );
232                 }
233                 else if ( ( strUrlDocumentation != null ) && !( strUrlDocumentation.equals( "" ) ) )
234                 {
235                     aLeftFeatures.add( right );
236                 }
237             }
238 
239             if ( !featureGroup.isEmpty(  ) )
240             {
241                 featureGroup.setLocale( user.getLocale(  ) );
242                 aOutFeatureGroupList.add( featureGroup );
243             }
244 
245             features = aLeftFeatures;
246         }
247 
248         FeatureGroup featureGroupSystem = FeatureGroupHome.findByPrimaryKey( FEATURES_GROUP_SYSTEM );
249 
250         if ( ( featureGroupSystem != null ) && !features.isEmpty(  ) )
251         {
252             boolean bSystemFeaturesGroupExist = false;
253 
254             //Check if the features group system exist in list features group
255             for ( FeatureGroup featureGroup : aOutFeatureGroupList )
256             {
257                 //if exist
258                 if ( featureGroup.getId(  ).equalsIgnoreCase( featureGroupSystem.getId(  ) ) )
259                 {
260                     // add the features with no group to the list in features group SYSTEM
261                     for ( Right right : features )
262                     {
263                         featureGroup.addFeature( right );
264                     }
265 
266                     bSystemFeaturesGroupExist = true;
267 
268                     break;
269                 }
270             }
271 
272             // if not, add features group SYSTEM to the list with the feautres no group
273             if ( !bSystemFeaturesGroupExist )
274             {
275                 for ( Right right : features )
276                 {
277                     featureGroupSystem.addFeature( right );
278                 }
279 
280                 featureGroupSystem.setLocale( user.getLocale(  ) );
281                 aOutFeatureGroupList.add( featureGroupSystem );
282             }
283         }
284         else if ( ( aOutFeatureGroupList.size(  ) > 0 ) && !features.isEmpty(  ) )
285         {
286             FeatureGroup lastFeatureGroup = aOutFeatureGroupList.get( aOutFeatureGroupList.size(  ) - 1 );
287 
288             for ( Right right : features )
289             {
290                 lastFeatureGroup.addFeature( right );
291             }
292         }
293 
294         return aOutFeatureGroupList;
295     }
296 }