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  
45  import org.apache.commons.lang.StringUtils;
46  
47  import java.util.ArrayList;
48  import java.util.List;
49  import java.util.Locale;
50  
51  import javax.servlet.http.HttpServletRequest;
52  
53  /**
54   *
55   * class EntryTypeInternalLink
56   *
57   */
58  public class EntryTypeInternalLink extends Entry
59  {
60      private final String _template_create = "admin/plugins/directory/entrytypeinternallink/create_entry_type_internallink.html";
61      private final String _template_modify = "admin/plugins/directory/entrytypeinternallink/modify_entry_type_internallink.html";
62      private final String _template_html_code_form_entry = "admin/plugins/directory/entrytypeinternallink/html_code_form_entry_type_internallink.html";
63      private final String _template_html_code_form_search_entry = "admin/plugins/directory/entrytypeinternallink/html_code_form_search_entry_type_internallink.html";
64      private final String _template_html_code_entry_value = "admin/plugins/directory/entrytypeinternallink/html_code_entry_value_type_internallink.html";
65      private final String _template_html_front_code_form_entry = "skin/plugins/directory/entrytypeinternallink/html_code_form_entry_type_internallink.html";
66      private final String _template_html_front_code_form_search_entry = "skin/plugins/directory/entrytypeinternallink/html_code_form_search_entry_type_internallink.html";
67      private final String _template_html_front_code_entry_value = "skin/plugins/directory/entrytypeinternallink/html_code_entry_value_type_internallink.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 strValue = request.getParameter( PARAMETER_VALUE );
123         String strMandatory = request.getParameter( PARAMETER_MANDATORY );
124         String strIndexed = request.getParameter( PARAMETER_INDEXED );
125         String strIndexedAsTitle = request.getParameter( PARAMETER_INDEXED_AS_TITLE );
126         String strIndexedAsSummary = 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 
141         if ( !strFieldError.equals( DirectoryUtils.EMPTY_STRING ) )
142         {
143             Object [ ] tabRequiredFields = {
144                 I18nService.getLocalizedString( strFieldError, locale )
145             };
146 
147             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
148         }
149 
150         if ( !strFieldError.equals( DirectoryUtils.EMPTY_STRING ) )
151         {
152             Object [ ] tabRequiredFields = {
153                 I18nService.getLocalizedString( strFieldError, locale )
154             };
155 
156             return AdminMessageService.getMessageUrl( request, MESSAGE_NUMERIC_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
157         }
158 
159         this.setTitle( strTitle );
160         this.setHelpMessage( strHelpMessage );
161         this.setHelpMessageSearch( strHelpMessageSearch );
162         this.setComment( strComment );
163 
164         if ( this.getFields( ) == null )
165         {
166             ArrayList<Field> listFields = new ArrayList<Field>( );
167             Field field = new Field( );
168             listFields.add( field );
169             this.setFields( listFields );
170         }
171 
172         this.getFields( ).get( 0 ).setValue( strValue );
173         this.setMandatory( strMandatory != null );
174         this.setIndexed( strIndexed != null );
175         this.setIndexedAsTitle( strIndexedAsTitle != null );
176         this.setIndexedAsSummary( strIndexedAsSummary != null );
177         this.setShownInAdvancedSearch( strShowInAdvancedSearch != null );
178         this.setShownInResultList( strShowInResultList != null );
179         this.setShownInResultRecord( strShowInResultRecord != null );
180         this.setShownInHistory( strShowInHistory != null );
181         this.setShownInExport( strShowInExport != null );
182         this.setShownInCompleteness( strShowInCompleteness != null );
183 
184         return null;
185     }
186 
187     /**
188      * {@inheritDoc}
189      */
190     @Override
191     public String getTemplateCreate( )
192     {
193         return _template_create;
194     }
195 
196     /**
197      * {@inheritDoc}
198      */
199     @Override
200     public String getTemplateModify( )
201     {
202         return _template_modify;
203     }
204 
205     /**
206      * {@inheritDoc}
207      */
208     @Override
209     public void getRecordFieldData( Record record, List<String> lstValue, boolean bTestDirectoryError, boolean bAddNewValue, List<RecordField> listRecordField,
210             Locale locale ) throws DirectoryErrorException
211     {
212         Plugin plugin = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
213 
214         String strValueEntry = ( ( lstValue != null ) && ( lstValue.size( ) > 0 ) ) ? lstValue.get( 0 ) : null;
215         RecordField recordField = new RecordField( );
216         recordField.setEntry( this );
217 
218         if ( ( record != null ) && bAddNewValue )
219         {
220             RecordFieldFilter recordFieldFilter = new RecordFieldFilter( );
221             recordFieldFilter.setIdDirectory( record.getDirectory( ).getIdDirectory( ) );
222             recordFieldFilter.setIdEntry( this.getIdEntry( ) );
223             recordFieldFilter.setIdRecord( record.getIdRecord( ) );
224 
225             List<RecordField> recordFieldList = RecordFieldHome.getRecordFieldList( recordFieldFilter, plugin );
226 
227             if ( ( recordFieldList != null ) && !recordFieldList.isEmpty( ) && StringUtils.isNotBlank( recordFieldList.get( 0 ).getValue( ) ) )
228             {
229                 strValueEntry = recordFieldList.get( 0 ).getValue( ) + ", " + strValueEntry;
230             }
231         }
232 
233         if ( strValueEntry != null )
234         {
235             if ( bTestDirectoryError && this.isMandatory( ) && strValueEntry.equals( DirectoryUtils.EMPTY_STRING ) )
236             {
237                 throw new DirectoryErrorException( this.getTitle( ) );
238             }
239 
240             recordField.setValue( strValueEntry );
241         }
242 
243         listRecordField.add( recordField );
244     }
245 }