View Javadoc
1   /*
2    * Copyright (c) 2002-2024, City of 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.blog.web.insertservice;
35  
36  import java.util.ArrayList;
37  import java.util.Collection;
38  import java.util.HashMap;
39  import java.util.List;
40  import java.util.Locale;
41  import java.util.Map;
42  
43  import javax.servlet.http.HttpServletRequest;
44  
45  import org.apache.commons.lang3.StringUtils;
46  
47  import fr.paris.lutece.plugins.blog.business.Blog;
48  import fr.paris.lutece.plugins.blog.business.BlogSearchFilter;
49  import fr.paris.lutece.plugins.blog.business.TagHome;
50  import fr.paris.lutece.plugins.blog.business.insertservice.BlogLinkPOJO;
51  import fr.paris.lutece.plugins.blog.service.BlogService;
52  import fr.paris.lutece.plugins.blog.service.docsearch.BlogSearchService;
53  import fr.paris.lutece.plugins.blog.utils.BlogUtils;
54  import fr.paris.lutece.portal.service.admin.AdminUserService;
55  import fr.paris.lutece.portal.service.template.AppTemplateService;
56  import fr.paris.lutece.portal.web.insert.InsertServiceJspBean;
57  import fr.paris.lutece.portal.web.insert.InsertServiceSelectionBean;
58  import fr.paris.lutece.util.html.HtmlTemplate;
59  
60  /**
61   * This class provides the user interface to insert a link toward a blog post or a blog's Resource
62   *
63   */
64  public abstract class AbstractBlogInsertServiceJspBean extends InsertServiceJspBean implements InsertServiceSelectionBean
65  {
66      /**
67       * Serial version UID
68       */
69      private static final long serialVersionUID = 5418289952051358700L;
70  
71      // Templates
72      private static final String TEMPLATE_SELECT_BLOG_LINK = "admin/plugins/blog/insertservice/select_blog_link.html";
73  
74      // Parameters
75      protected static final String PARAMETER_INPUT = "input";
76      protected static final String PARAMETER_SELECTED_BLOG_ID = "selected_blog_id";
77      protected static final String PARAMETER_LINK_TEXT = "custom_link_text";
78      protected static final String PARAMETER_LINK_TITLE = "custom_link_title";
79      private static final String PARAMETER_BLOG_SEARCH_TEXT = "search_text";
80      private static final String PARAMETER_SELECTED_TAGS = "selected_tags";
81      private static final String PARAMETER_DATE_BLOG_PUBLISHED_AFTER = "date_blog_published_after";
82      private static final String PARAMETER_DATE_BLOG_PUBLISHED_BEFORE = "date_blog_published_before";
83  
84      // Marks
85      private static final String MARK_INPUT = "input";
86      private static final String MARK_LANGUAGE = "language";
87      private static final String MARK_BLOG_SEARCH_TEXT = "search_text";
88      private static final String MARK_TAGS = "tags";
89      private static final String MARK_TAGS_REFERENCE_LIST = "tags_reference_list";
90      private static final String MARK_BLOG_LIST_FILTERED = "blog_list_filtered";
91      private static final String MARK_BLOG_INSERT_SERVICE_TYPE = "blog_insert_service_type";
92  
93      // List of the blogs found after the search
94      private Collection<Blog> _blogsList;
95  
96      // List of selected tags for the filtered search
97      private String [ ] _tagsArray;
98  
99      // Stores the type of Insert Service to be used in the main view's template
100     private String _insertServiceType = "blog_insert_service";
101 
102     /**
103      * Get the HTML form where the blog to insert is selected
104      *
105      * @param request
106      *            The http request
107      * @return the HTML code to display
108      */
109     @Override
110     public String getInsertServiceSelectorUI( HttpServletRequest request )
111     {
112         Locale locale = AdminUserService.getLocale( request );
113 
114         Map<String, Object> model = new HashMap<>( );
115 
116         model.put( MARK_INPUT, request.getParameter( PARAMETER_INPUT ) );
117         model.put( MARK_LANGUAGE, locale );
118         // Set the type of Blog Insert Service being used
119         model.put( MARK_BLOG_INSERT_SERVICE_TYPE, getInsertServiceType( ) );
120 
121         // Set the value of the keywords being searched for
122         String blogSearchTextString = request.getParameter( PARAMETER_BLOG_SEARCH_TEXT );
123         model.put( MARK_BLOG_SEARCH_TEXT, blogSearchTextString );
124 
125         // Display the available Tags in the selection field
126         model.put( MARK_TAGS_REFERENCE_LIST, TagHome.getTagsReferenceList( ) );
127         // Set the Tags selected by the user
128         model.put( MARK_TAGS, _tagsArray );
129 
130         // Set the list of blog posts matching the user's search
131         model.put( MARK_BLOG_LIST_FILTERED, _blogsList );
132 
133         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_SELECT_BLOG_LINK, locale, model );
134 
135         return template.getHtml( );
136     }
137 
138     /**
139      * Insert the link toward the selected blog post element into the html editor
140      *
141      * @param request
142      *            The http request
143      * @return the link to display in the html code
144      */
145     public String doInsertBlogLink( HttpServletRequest request )
146     {
147         String strInput = request.getParameter( PARAMETER_INPUT );
148 
149         BlogLinkPOJO blogLinkContent = getSelectedBlogData( request );
150 
151         String strBlogLink = buildBlogLink( blogLinkContent.getBlog( ), blogLinkContent.getBlogLinkText( ), blogLinkContent.getBlogLinkTitle( ),
152                 blogLinkContent.getBlogLinkTargetedWindow( ) );
153 
154         return insertUrl( request, strInput, strBlogLink );
155     }
156 
157     /**
158      * Get the values used to buil the blog link (text, title...), from the submitted form
159      *
160      * @param request
161      *            The http request containing the submitted parameters
162      * @return a BlogLinkPOJO containing the data necessary to build a blog link
163      */
164     protected BlogLinkPOJO getSelectedBlogData( HttpServletRequest request )
165     {
166         // Get the ID of the chosen blog
167         String strSelectedBlogId = request.getParameter( PARAMETER_SELECTED_BLOG_ID );
168         int selectedBlogId = 0;
169 
170         if ( StringUtils.isNumeric( strSelectedBlogId ) )
171         {
172             selectedBlogId = Integer.parseInt( strSelectedBlogId );
173         }
174 
175         // Get the text to display on the blog link
176         String blogLinkText = request.getParameter( PARAMETER_LINK_TEXT );
177 
178         // Get the title of the link
179         String blogLinkTitle = request.getParameter( PARAMETER_LINK_TITLE );
180 
181         // Get the type of target window to use for the links
182         String targetedWindow = BlogUtils.CONSTANT_LINK_TARGET_NEW_WINDOW;
183 
184         Blog blog = BlogService.getInstance( ).findByPrimaryKeyWithoutBinaries( selectedBlogId );
185 
186         return new BlogLinkPOJO( blogLinkText, blogLinkTitle, targetedWindow, blog );
187     }
188 
189     /**
190      * Get the values specified in the search filter and look for the blogs matching them
191      *
192      * @param request
193      *            The http request containing the submitted filter values
194      * @return a List of Integer corresponding to the blogs matching the search filter
195      */
196     protected List<Integer> getBlogIdsWithFilter( HttpServletRequest request )
197     {
198         // Get the potential filters from the form
199         String blogSearchText = request.getParameter( PARAMETER_BLOG_SEARCH_TEXT );
200         _tagsArray = request.getParameterValues( PARAMETER_SELECTED_TAGS );
201         String strPublishedDateAfter = request.getParameter( PARAMETER_DATE_BLOG_PUBLISHED_AFTER );
202         String strPublishedDateBefore = request.getParameter( PARAMETER_DATE_BLOG_PUBLISHED_BEFORE );
203 
204         // List of the Blog IDs matching the search
205         List<Integer> blogIdsList = new ArrayList<>( );
206 
207         // Create a search filter to find specific blogs
208         BlogSearchFilter filter = BlogUtils.buildBlogSearchFilter( blogSearchText, _tagsArray, null, 0, strPublishedDateAfter, strPublishedDateBefore, null,
209                 AdminUserService.getLocale( request ) );
210 
211         // Get a List of the IDs of indexed blogs that match the filters
212         BlogSearchService.getInstance( ).getSearchResults( filter, blogIdsList );
213 
214         return blogIdsList;
215     }
216 
217     /**
218      * Search for the blogs matching the user's search and reload the HTML form where the blog link to insert is selected
219      *
220      * @param request
221      *            The http request
222      * @return the HTML code to display the blogs that can be selected to create a link from
223      */
224     public abstract String doSearchBlogLink( HttpServletRequest request );
225 
226     /**
227      * Create an HTML link to reach the URL of the specified blog post
228      *
229      * @param blog
230      *            Blog object for which the link is created
231      * @param customlinkText
232      *            Text to display in the link
233      * @param customLinkTitle
234      *            Title of the link
235      * @param targetedWindow
236      *            Window where the link will be opened ("_self", "_blank", etc.). Opens in new window by default ("_blank")
237      * @return the HTML link to the specified Blog
238      */
239     protected abstract String buildBlogLink( Blog blog, String customlinkText, String customLinkTitle, String targetedWindow );
240 
241     /**
242      * Get the type of the Blog Insert Service being used
243      *
244      * @return the name of the Blog Insert Service type
245      */
246     protected String getInsertServiceType( )
247     {
248         return _insertServiceType;
249     }
250 
251     /**
252      * Set the type of the Blog Insert Service being used
253      *
254      * @param strInsertServiceType
255      *            The type of Insert Service used
256      */
257     protected void setInsertServiceType( String strInsertServiceType )
258     {
259         _insertServiceType = strInsertServiceType;
260     }
261 
262     /**
263      * Set the List of Blogs found by the search
264      *
265      * @param blogsList
266      *            The List of Blog objects found
267      */
268     protected void setBlogList( Collection<Blog> blogsList )
269     {
270         _blogsList = blogsList;
271     }
272 }