View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.directory.business;
35  
36  import fr.paris.lutece.plugins.directory.service.DirectoryPlugin;
37  import fr.paris.lutece.plugins.directory.utils.DirectoryErrorException;
38  import fr.paris.lutece.plugins.directory.utils.DirectoryUtils;
39  import fr.paris.lutece.portal.service.i18n.I18nService;
40  import fr.paris.lutece.portal.service.message.AdminMessage;
41  import fr.paris.lutece.portal.service.message.AdminMessageService;
42  import fr.paris.lutece.portal.service.plugin.Plugin;
43  import fr.paris.lutece.portal.service.plugin.PluginService;
44  import fr.paris.lutece.util.ReferenceList;
45  import fr.paris.lutece.util.html.Paginator;
46  import fr.paris.lutece.util.sql.DAOUtil;
47  
48  import java.util.List;
49  import java.util.Locale;
50  
51  import javax.servlet.http.HttpServletRequest;
52  
53  /**
54   *
55   * class EntryTypeSQL
56   *
57   */
58  public class EntryTypeSQL extends Entry
59  {
60      private final String _template_create = "admin/plugins/directory/entrytypeSQL/create_entry_type_SQL.html";
61      private final String _template_modify = "admin/plugins/directory/entrytypeSQL/modify_entry_type_SQL.html";
62      private final String _template_html_code_form_entry = "admin/plugins/directory/entrytypeSQL/html_code_form_entry_type_SQL.html";
63      private final String _template_html_code_form_search_entry = "admin/plugins/directory/entrytypeSQL/html_code_form_search_entry_type_SQL.html";
64      private final String _template_html_code_entry_value = "admin/plugins/directory/entrytypeSQL/html_code_entry_value_type_SQL.html";
65      private final String _template_html_front_code_form_entry = "skin/plugins/directory/entrytypeSQL/html_code_form_entry_type_SQL.html";
66      private final String _template_html_front_code_form_search_entry = "skin/plugins/directory/entrytypeSQL/html_code_form_search_entry_type_SQL.html";
67      private final String _template_html_front_code_entry_value = "skin/plugins/directory/entrytypeSQL/html_code_entry_value_type_SQL.html";
68  
69      /**
70       * {@inheritDoc}
71       */
72      @Override
73      public String getTemplateHtmlFormEntry( boolean isDisplayFront )
74      {
75          if ( isDisplayFront )
76          {
77              return _template_html_front_code_form_entry;
78          }
79  
80          return _template_html_code_form_entry;
81      }
82  
83      /**
84       * {@inheritDoc}
85       */
86      @Override
87      public String getTemplateHtmlRecordFieldValue( boolean isDisplayFront )
88      {
89          if ( isDisplayFront )
90          {
91              return _template_html_front_code_entry_value;
92          }
93  
94          return _template_html_code_entry_value;
95      }
96  
97      /**
98       * {@inheritDoc}
99       */
100     @Override
101     public String getTemplateHtmlFormSearchEntry( boolean isDisplayFront )
102     {
103         if ( isDisplayFront )
104         {
105             return _template_html_front_code_form_search_entry;
106         }
107 
108         return _template_html_code_form_search_entry;
109     }
110 
111     /**
112      * {@inheritDoc}
113      */
114     @Override
115     public String getEntryData( HttpServletRequest request, Locale locale )
116     {
117         String strTitle = request.getParameter( PARAMETER_TITLE );
118         String strHelpMessage = ( request.getParameter( PARAMETER_HELP_MESSAGE ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE ).trim( ) : null;
119         String strHelpMessageSearch = ( request.getParameter( PARAMETER_HELP_MESSAGE_SEARCH ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE_SEARCH )
120                 .trim( ) : null;
121         String strComment = request.getParameter( PARAMETER_COMMENT );
122         String strRequestSQL = request.getParameter( PARAMETER_REQUEST_SQL );
123         String strMandatory = request.getParameter( PARAMETER_MANDATORY );
124         String strIndexed = request.getParameter( PARAMETER_INDEXED );
125         String strDocumentTitle = request.getParameter( PARAMETER_INDEXED_AS_TITLE );
126         String strDocumentSummary = request.getParameter( PARAMETER_INDEXED_AS_SUMMARY );
127         String strShowInAdvancedSearch = request.getParameter( PARAMETER_SHOWN_IN_ADVANCED_SEARCH );
128         String strShowInResultList = request.getParameter( PARAMETER_SHOWN_IN_RESULT_LIST );
129         String strShowInResultRecord = request.getParameter( PARAMETER_SHOWN_IN_RESULT_RECORD );
130         String strShowInHistory = request.getParameter( PARAMETER_SHOWN_IN_HISTORY );
131         String strShowInExport = request.getParameter( PARAMETER_SHOWN_IN_EXPORT );
132         String strShowInCompleteness = request.getParameter( PARAMETER_SHOWN_IN_COMPLETENESS );
133 
134         String strFieldError = DirectoryUtils.EMPTY_STRING;
135 
136         if ( ( strTitle == null ) || strTitle.trim( ).equals( DirectoryUtils.EMPTY_STRING ) )
137         {
138             strFieldError = FIELD_TITLE;
139         }
140         else
141             if ( ( strRequestSQL == null ) || strRequestSQL.trim( ).equals( DirectoryUtils.EMPTY_STRING ) )
142             {
143                 strFieldError = FIELD_REQUEST_SQL;
144             }
145 
146         if ( !strFieldError.equals( DirectoryUtils.EMPTY_STRING ) )
147         {
148             Object [ ] tabRequiredFields = {
149                 I18nService.getLocalizedString( strFieldError, locale )
150             };
151 
152             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
153         }
154 
155         if ( !strFieldError.equals( DirectoryUtils.EMPTY_STRING ) )
156         {
157             Object [ ] tabRequiredFields = {
158                 I18nService.getLocalizedString( strFieldError, locale )
159             };
160 
161             return AdminMessageService.getMessageUrl( request, MESSAGE_NUMERIC_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
162         }
163 
164         this.setTitle( strTitle );
165         this.setHelpMessage( strHelpMessage );
166         this.setHelpMessageSearch( strHelpMessageSearch );
167         this.setComment( strComment );
168         this.setRequestSQL( strRequestSQL );
169         this.setMandatory( strMandatory != null );
170         this.setIndexed( strIndexed != null );
171         this.setIndexedAsTitle( strDocumentTitle != null );
172         this.setIndexedAsSummary( strDocumentSummary != null );
173         this.setShownInAdvancedSearch( strShowInAdvancedSearch != null );
174         this.setShownInResultList( strShowInResultList != null );
175         this.setShownInResultRecord( strShowInResultRecord != null );
176         this.setShownInHistory( strShowInHistory != null );
177         this.setShownInExport( strShowInExport != null );
178         this.setShownInCompleteness( strShowInCompleteness != null );
179 
180         return null;
181     }
182 
183     /**
184      * {@inheritDoc}
185      */
186     @Override
187     public String getTemplateCreate( )
188     {
189         return _template_create;
190     }
191 
192     /**
193      * {@inheritDoc}
194      */
195     @Override
196     public String getTemplateModify( )
197     {
198         return _template_modify;
199     }
200 
201     /**
202      * {@inheritDoc}
203      */
204     @Override
205     public Paginator getPaginator( int nItemPerPage, String strBaseUrl, String strPageIndexParameterName, String strPageIndex )
206     {
207         return new Paginator( this.getFields( ).get( 0 ).getRegularExpressionList( ), nItemPerPage, strBaseUrl, strPageIndexParameterName, strPageIndex );
208     }
209 
210     /**
211      * {@inheritDoc}
212      */
213     @Override
214     public void getRecordFieldData( Record record, List<String> lstValue, boolean bTestDirectoryError, boolean bAddNewValue, List<RecordField> listRecordField,
215             Locale locale ) throws DirectoryErrorException
216     {
217         Plugin plugin = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
218 
219         String strValueEntry = ( ( lstValue != null ) && ( lstValue.size( ) > 0 ) ) ? lstValue.get( 0 ) : null;
220         RecordField recordField = new RecordField( );
221         recordField.setEntry( this );
222 
223         if ( ( record != null ) && bAddNewValue )
224         {
225             RecordFieldFilter recordFieldFilter = new RecordFieldFilter( );
226             recordFieldFilter.setIdDirectory( record.getDirectory( ).getIdDirectory( ) );
227             recordFieldFilter.setIdEntry( this.getIdEntry( ) );
228             recordFieldFilter.setIdRecord( record.getIdRecord( ) );
229 
230             List<RecordField> recordFieldList = RecordFieldHome.getRecordFieldList( recordFieldFilter, plugin );
231 
232             if ( ( recordFieldList != null ) && !recordFieldList.isEmpty( ) && !recordFieldList.get( 0 ).getValue( ).equals( "" ) )
233             {
234                 strValueEntry = recordFieldList.get( 0 ).getValue( ) + ", " + strValueEntry;
235             }
236         }
237 
238         if ( strValueEntry != null )
239         {
240             if ( bTestDirectoryError && this.isMandatory( ) && strValueEntry.equals( DirectoryUtils.EMPTY_STRING ) )
241             {
242                 throw new DirectoryErrorException( this.getTitle( ) );
243             }
244 
245             recordField.setValue( strValueEntry );
246         }
247 
248         listRecordField.add( recordField );
249     }
250 
251     /**
252      * Return fields from a SQL query
253      * 
254      * @return A list of fields
255      */
256     public ReferenceList getSqlQueryFields( )
257     {
258         ReferenceList list = new ReferenceList( );
259         String strSQL = this.getRequestSQL( );
260         DAOUtil daoUtil = new DAOUtil( strSQL );
261         daoUtil.executeQuery( );
262 
263         while ( daoUtil.next( ) )
264         {
265             list.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) );
266         }
267 
268         daoUtil.free( );
269 
270         return list;
271     }
272 
273     /**
274      * {@inheritDoc}
275      */
276     @Override
277     public String convertRecordFieldValueToString( RecordField recordField, Locale locale, boolean bDisplayFront, boolean bDisplayExport )
278     {
279         Plugin plugin = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
280 
281         if ( recordField.getValue( ) != null )
282         {
283             if ( bDisplayExport )
284             {
285                 return recordField.getValue( );
286             }
287 
288             IEntry entry = EntryHome.findByPrimaryKey( this.getIdEntry( ), plugin );
289             DAOUtil daoUtil = new DAOUtil( entry.getRequestSQL( ) );
290             daoUtil.executeQuery( );
291 
292             while ( daoUtil.next( ) )
293             {
294                 if ( daoUtil.getString( 1 ).equals( recordField.getValue( ) ) )
295                 {
296                     String result = daoUtil.getString( 2 );
297                     daoUtil.free( );
298                     return result;
299                 }
300             }
301 
302             daoUtil.free( );
303 
304         }
305 
306         return DirectoryUtils.EMPTY_STRING;
307     }
308 
309     /**
310      * {@inheritDoc}
311      */
312     @Override
313     public String convertRecordFieldTitleToString( RecordField recordField, Locale locale, boolean bDisplayFront )
314     {
315         return convertRecordFieldValueToString( recordField, locale, bDisplayFront, false );
316     }
317 }