View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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  /*
35   * AnnounceUtils.java
36   *
37   * Created on 23 octobre 2009, 11:31
38   *
39   * To change this template, choose Tools | Template Manager
40   * and open the template in the editor.
41   */
42  package fr.paris.lutece.plugins.announce.utils;
43  
44  import fr.paris.lutece.plugins.announce.service.AnnouncePlugin;
45  import fr.paris.lutece.portal.service.plugin.Plugin;
46  import fr.paris.lutece.portal.service.plugin.PluginService;
47  
48  import java.util.List;
49  
50  import javax.servlet.http.HttpServletRequest;
51  import javax.servlet.http.HttpServletResponse;
52  
53  /**
54   * Utility class for announce plugin
55   */
56  public final class AnnounceUtils
57  {
58      // Constants
59      public static final String CONSTANT_WHERE = " WHERE ";
60      public static final String CONSTANT_AND = " AND ";
61      public static final int CONSTANT_ID_NULL = -1;
62  
63      // properties
64      public static final String MESSAGE_MANDATORY_QUESTION = "announce.mandatory_message";
65  
66      // parameters
67      /**
68       * Name of the XPage
69       */
70      public static final String PARAMETER_PAGE_ANNOUNCE = "announce";
71  
72      /**
73       * Private default constructor
74       */
75      private AnnounceUtils( )
76      {
77          // Do nothing
78      }
79  
80      /**
81       * Builds a query with filters placed in parameters
82       * 
83       * @param strSelect
84       *            the select of the query
85       * @param listStrFilter
86       *            the list of filter to add in the query
87       * @param strOrder
88       *            the order by of the query
89       * @return a query
90       */
91      public static String buildRequetteWithFilter( String strSelect, List<String> listStrFilter, String strOrder )
92      {
93          StringBuilder strBuffer = new StringBuilder( );
94          strBuffer.append( strSelect );
95  
96          int nCount = 0;
97  
98          for ( String strFilter : listStrFilter )
99          {
100             if ( ++nCount == 1 )
101             {
102                 strBuffer.append( CONSTANT_WHERE );
103             }
104 
105             strBuffer.append( strFilter );
106 
107             if ( nCount != listStrFilter.size( ) )
108             {
109                 strBuffer.append( CONSTANT_AND );
110             }
111         }
112 
113         if ( strOrder != null )
114         {
115             strBuffer.append( strOrder );
116         }
117 
118         return strBuffer.toString( );
119     }
120 
121     /**
122      * write the http header in the response
123      *
124      * @param request
125      *            the httpServletRequest
126      * @param response
127      *            the http response
128      * @param strFileName
129      *            the name of the file who must insert in the response
130      *
131      */
132     public static void addHeaderResponse( HttpServletRequest request, HttpServletResponse response, String strFileName )
133     {
134         response.setHeader( "Content-Disposition", "attachment ;filename=\"" + strFileName + "\"" );
135         response.setHeader( "Pragma", "public" );
136         response.setHeader( "Expires", "0" );
137         response.setHeader( "Cache-Control", "must-revalidate,post-check=0,pre-check=0" );
138     }
139 
140     /**
141      * Gets the plugin
142      *
143      * @return the plugin
144      */
145     public static Plugin getPlugin( )
146     {
147         return PluginService.getPlugin( AnnouncePlugin.PLUGIN_NAME );
148     }
149 }