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.insertalbum.web;
35  
36  import fr.paris.lutece.plugins.document.business.Document;
37  import fr.paris.lutece.plugins.document.business.DocumentHome;
38  import fr.paris.lutece.plugins.document.service.spaces.DocumentSpacesService;
39  import fr.paris.lutece.plugins.insertalbum.service.NoImageInWorkspaceException;
40  import fr.paris.lutece.portal.business.user.AdminUser;
41  import fr.paris.lutece.portal.service.admin.AdminUserService;
42  import fr.paris.lutece.portal.service.template.AppTemplateService;
43  import fr.paris.lutece.portal.service.util.AppPathService;
44  import fr.paris.lutece.portal.service.util.AppPropertiesService;
45  import fr.paris.lutece.portal.web.insert.InsertServiceJspBean;
46  import fr.paris.lutece.portal.web.insert.InsertServiceSelectionBean;
47  import fr.paris.lutece.util.html.HtmlTemplate;
48  
49  import org.apache.commons.lang.StringEscapeUtils;
50  
51  import java.util.Arrays;
52  import java.util.HashMap;
53  import java.util.List;
54  import java.util.Locale;
55  import java.util.Map;
56  
57  import javax.servlet.http.HttpServletRequest;
58  
59  
60  /**
61   * InsertService main class
62   */
63  public class InsertAlbumJspBean extends InsertServiceJspBean implements InsertServiceSelectionBean
64  {
65      private static final String SLIDESHOW = "slideshow";
66      private static final long serialVersionUID = -3189672631047661765L;
67      private static final String TEMPLATE_SELECTOR_PAGE = "admin/plugins/insertalbum/selector.html";
68      private static final String TEMPLATE_PRESENTATION = "admin/plugins/insertalbum/presentation.html";
69  
70      // template for all admin pages
71      private static final String TEMPLATE_CHOOSE_DOCUMENT = "admin/plugins/insertalbum/choose_workspace.html";
72      private static final String MARK_SPACES_BROWSER = "spaces_browser";
73      private static final String PARAMETER_INPUT = "input";
74      private static final String IMAGES_LIST = "images_list";
75      private static final String BASE_URL = "baseUrl";
76      private static final String SLIDESHOWLIST = "slideshow_list";
77      private static final String TEMPLATE_PREVIEW_SLIDESHOW = "admin/plugins/insertalbum/choose_slideshow.html";
78      private AdminUser _user;
79      private String _input;
80      private String PROPERTY_SLIDESHOW_LIST = "insertalbum.slideshows";
81  
82      private void init( HttpServletRequest request )
83      {
84          _user = AdminUserService.getAdminUser( request );
85          _input = request.getParameter( PARAMETER_INPUT );
86      }
87  
88      public String getInsertServiceSelectorUI( HttpServletRequest request )
89      {
90          init( request );
91  
92          Locale locale = AdminUserService.getLocale( request );
93          Map<String, Object> model = new HashMap<String, Object>(  );
94          model.put( PARAMETER_INPUT, _input );
95  
96          HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_PRESENTATION, locale, model );
97  
98          return template.getHtml(  );
99      }
100 
101     public String getSpaceBrowser( HttpServletRequest request )
102     {
103         init( request );
104 
105         Map<String, Object> model = new HashMap<String, Object>(  );
106         String strSpaceId = request.getParameter( DocumentSpacesService.PARAMETER_BROWSER_SELECTED_SPACE_ID );
107 
108         if ( ( strSpaceId != null ) && !strSpaceId.equals( "" ) )
109         {
110             return getSelectSlideshow( request );
111         }
112 
113         Locale locale = AdminUserService.getLocale( request );
114         // Spaces browser
115         model.put( MARK_SPACES_BROWSER,
116             DocumentSpacesService.getInstance(  ).getSpacesBrowser( request, _user, locale, true, true ) );
117 
118         String strBaseUrl = AppPathService.getBaseUrl( request );
119         model.put( BASE_URL, strBaseUrl );
120         model.put( PARAMETER_INPUT, _input );
121 
122         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CHOOSE_DOCUMENT, locale, model );
123 
124         return template.getHtml(  );
125     }
126 
127     /**
128      * Give the user the choose of the workspace
129      *
130      * @param request The Http Request
131      * @return The html form.
132      */
133     public String getSelectSlideshow( HttpServletRequest request )
134     {
135         String slideshowListInProperties = AppPropertiesService.getProperty( PROPERTY_SLIDESHOW_LIST );
136 
137         List<String> slideshowList = Arrays.asList( slideshowListInProperties.split( "," ) );
138 
139         String strIdWorksapce = request.getParameter( DocumentSpacesService.PARAMETER_BROWSER_SELECTED_SPACE_ID );
140 
141         Map<String, Object> model = new HashMap<String, Object>(  );
142         model.put( PARAMETER_INPUT, _input );
143         model.put( SLIDESHOWLIST, slideshowList );
144 
145         String strBaseUrl = AppPathService.getBaseUrl( request );
146         model.put( BASE_URL, strBaseUrl );
147         model.put( DocumentSpacesService.PARAMETER_BROWSER_SELECTED_SPACE_ID, strIdWorksapce );
148 
149         Locale locale = AdminUserService.getLocale( request );
150         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_SELECTOR_PAGE, locale, model );
151 
152         return template.getHtml(  );
153     }
154 
155     public String doInsertLink( HttpServletRequest request )
156     {
157         //get the name of the html tag to return to the WYNGZIG editor
158         String strInput = request.getParameter( PARAMETER_INPUT );
159 
160         try
161         {
162             HtmlTemplate template = getSlideshowTemplate( request );
163 
164             return insertUrlWithoutEscape( request, strInput, template.getHtml(  ) );
165         }
166         catch ( NoImageInWorkspaceException e )
167         {
168             return insertUrlWithoutEscape( request, strInput, "" );
169         }
170     }
171 
172     public String doGetSlideshow( HttpServletRequest request )
173     {
174         try
175         {
176             HtmlTemplate templateSlideshow = getSlideshowTemplate( request );
177 
178             Map<String, Object> model = new HashMap<String, Object>(  );
179             Locale locale = AdminUserService.getLocale( request );
180             model.put( SLIDESHOW, templateSlideshow.getHtml(  ) );
181 
182             String strBaseUrl = AppPathService.getBaseUrl( request );
183             model.put( BASE_URL, strBaseUrl );
184             model.put( PARAMETER_INPUT, _input );
185 
186             HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_PREVIEW_SLIDESHOW, locale, model );
187 
188             return template.getHtml(  );
189         }
190         catch ( NoImageInWorkspaceException e )
191         {
192             return "no image in workspace";
193         }
194     }
195 
196     private HtmlTemplate getSlideshowTemplate( HttpServletRequest request )
197         throws NoImageInWorkspaceException
198     {
199         //get the workspace of witch we want the images
200         String strIdWorksapce = request.getParameter( DocumentSpacesService.PARAMETER_BROWSER_SELECTED_SPACE_ID );
201         int nIdWorksapce = Integer.parseInt( strIdWorksapce );
202 
203         //get the name of the template (for the slideshow) to use
204         String strSlideshow = request.getParameter( SLIDESHOW );
205 
206         //Collection<Image> imagesList = InsertAlbumHome.findImagesListOfWorkspace( nIdWorksapce, _plugin );
207         List<Document> documentsList = DocumentHome.findBySpaceKey( nIdWorksapce );
208 
209         for ( Document doc : documentsList )
210         {
211             doc.setComment( StringEscapeUtils.escapeHtml( doc.getSummary(  ) ) );
212             doc.setComment( doc.getComment(  ).replaceAll( "'", "&#146;" ) );
213         }
214 
215         HtmlTemplate template = null;
216 
217         String strBaseUrl = AppPathService.getBaseUrl( request );
218         Locale locale = AdminUserService.getLocale( request );
219 
220         Map<String, Object> model = new HashMap<String, Object>(  );
221         model.put( IMAGES_LIST, documentsList );
222         model.put( BASE_URL, strBaseUrl );
223 
224         if ( ( documentsList != null ) && ( documentsList.size(  ) > 0 ) && ( strSlideshow != null ) )
225         {
226             template = AppTemplateService.getTemplate( "admin/plugins/insertalbum/slideshow/" + strSlideshow + ".html",
227                     locale, model );
228         }
229         else
230         {
231             throw new NoImageInWorkspaceException(  );
232         }
233 
234         return template;
235     }
236 }