View Javadoc
1   /*
2    * Copyright (c) 2002-2018, 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.appstore.web;
35  
36  import fr.paris.lutece.plugins.appstore.business.Application;
37  import fr.paris.lutece.plugins.appstore.business.ApplicationHome;
38  import fr.paris.lutece.plugins.appstore.business.ComponentHome;
39  import fr.paris.lutece.portal.service.message.SiteMessageException;
40  import fr.paris.lutece.portal.service.plugin.Plugin;
41  import fr.paris.lutece.portal.service.template.AppTemplateService;
42  import fr.paris.lutece.portal.service.util.AppPropertiesService;
43  import fr.paris.lutece.portal.web.xpages.XPage;
44  import fr.paris.lutece.portal.web.xpages.XPageApplication;
45  import fr.paris.lutece.util.ReferenceList;
46  import fr.paris.lutece.util.html.HtmlTemplate;
47  
48  import java.io.Serializable;
49  
50  import java.util.ArrayList;
51  import java.util.HashMap;
52  import java.util.List;
53  import java.util.Map;
54  
55  import javax.servlet.http.HttpServletRequest;
56  
57  /**
58   * This class provides a simple implementation of an XPage
59   */
60  public class AppStoreApp implements XPageApplication, Serializable
61  {
62      private static final String TEMPLATE_HOMEPAGE = "/skin/plugins/appstore/appstore.html";
63      private static final String TEMPLATE_APPLICATION = "/skin/plugins/appstore/application.html";
64      private static final String TEMPLATE_POM = "/admin/plugins/appstore/pom.xml";
65      private static final String MARK_APPLICATIONS_LIST = "applications_list";
66      private static final String MARK_APPLICATION = "application";
67      private static final String MARK_COMPONENTS_LIST = "components_list";
68      private static final String PARAMETER_ID_APPLICATION = "id_application";
69      private static final String PROPERTY_PAGE_PATH = "appstore.pagePathLabel";
70      private static final String PROPERTY_PAGE_TITLE = "appstore.pageTitle";
71      private static final String URL_HOME_APPSTORE = "page=appstore";
72  
73      /**
74       * Returns the content of the page appstore.
75       *
76       * @param request
77       *            The http request
78       * @param nMode
79       *            The current mode
80       * @param plugin
81       *            The plugin object
82       * @throws fr.paris.lutece.portal.service.message.SiteMessageException
83       *             Message displayed if an exception occurs
84       */
85      @Override
86      public XPage getPage( HttpServletRequest request, int nMode, Plugin plugin ) throws SiteMessageException
87      {
88          XPage page = new XPage( );
89  
90          String strApplicationId = request.getParameter( PARAMETER_ID_APPLICATION );
91  
92          if ( strApplicationId != null )
93          {
94              int nApplicationId = Integer.parseInt( strApplicationId );
95              getApplicationContent( request, page, nApplicationId );
96          }
97          else
98          {
99              getHomePageContent( request, page );
100         }
101 
102         return page;
103     }
104 
105     private void getHomePageContent( HttpServletRequest request, XPage page )
106     {
107         Map<String, Object> model = new HashMap<String, Object>( );
108 
109         model.put( MARK_APPLICATIONS_LIST, getActiveApplications( ) );
110 
111         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_HOMEPAGE, request.getLocale( ), model );
112 
113         page.setContent( template.getHtml( ) );
114         page.setTitle( AppPropertiesService.getProperty( PROPERTY_PAGE_TITLE ) );
115         page.setPathLabel( AppPropertiesService.getProperty( PROPERTY_PAGE_PATH ) );
116     }
117 
118     private List<Application> getActiveApplications( )
119     {
120         List<Application> list = new ArrayList<Application>( );
121 
122         for ( Application application : ApplicationHome.getApplicationsList( ) )
123         {
124             if ( application.getPublishStatus( ) > 0 )
125             {
126                 list.add( application );
127             }
128         }
129 
130         return list;
131     }
132 
133     private void getApplicationContent( HttpServletRequest request, XPage page, int nApplicationId )
134     {
135         Application application = ApplicationHome.findByPrimaryKey( nApplicationId );
136         Map<String, Object> model = new HashMap<String, Object>( );
137 
138         model.put( MARK_APPLICATION, application );
139         model.put( MARK_COMPONENTS_LIST, ComponentHome.findByApplication( application.getId( ) ) );
140 
141         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_APPLICATION, request.getLocale( ), model );
142 
143         page.setContent( template.getHtml( ) );
144         page.setTitle( AppPropertiesService.getProperty( PROPERTY_PAGE_TITLE ) + " - " + application.getTitle( ) );
145         page.setExtendedPathLabel( getXmlExtendedPath( application.getTitle( ) ) );
146     }
147 
148     public String generatePOM( HttpServletRequest request )
149     {
150         int nApplicationId = Integer.parseInt( request.getParameter( PARAMETER_ID_APPLICATION ) );
151         Application application = ApplicationHome.findByPrimaryKey( nApplicationId );
152         Map<String, Object> model = new HashMap<String, Object>( );
153 
154         model.put( MARK_APPLICATION, application );
155         model.put( MARK_COMPONENTS_LIST, ComponentHome.findByApplication( application.getId( ) ) );
156 
157         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_POM, request.getLocale( ), model );
158 
159         return template.getHtml( );
160     }
161 
162     /**
163      * Build XML path infos
164      * 
165      * @param strPageName
166      *            The page name
167      * @return The XML that content path info
168      */
169     private ReferenceList getXmlExtendedPath( String strPageName )
170     {
171         ReferenceList list = new ReferenceList( );
172         list.addItem( AppPropertiesService.getProperty( PROPERTY_PAGE_PATH ), URL_HOME_APPSTORE );
173         list.addItem( strPageName, "" );
174 
175         return list;
176     }
177 }