View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.genericattributes.util;
35  
36  import fr.paris.lutece.plugins.genericattributes.business.Entry;
37  import fr.paris.lutece.plugins.genericattributes.business.Field;
38  import fr.paris.lutece.plugins.genericattributes.service.GenericAttributesPlugin;
39  import fr.paris.lutece.plugins.genericattributes.service.entrytype.IEntryTypeService;
40  import fr.paris.lutece.plugins.referencelist.business.ReferenceItem;
41  import fr.paris.lutece.portal.service.plugin.Plugin;
42  import fr.paris.lutece.portal.service.plugin.PluginService;
43  import fr.paris.lutece.portal.service.util.AppLogService;
44  
45  import org.apache.commons.lang3.StringUtils;
46  
47  import java.util.List;
48  
49  /**
50   * Utility class of plugin generic attributes
51   */
52  public final class GenericAttributesUtils
53  {
54      /**
55       * Equals constant
56       */
57      public static final String CONSTANT_EQUAL = "=";
58  
59      /**
60       * Value to represent a null id
61       */
62      public static final int CONSTANT_ID_NULL = -1;
63  
64      /**
65       * Value for anonymized responses
66       */
67      public static final String CONSTANT_RESPONSE_VALUE_ANONYMIZED = "anonymized";
68      private static final String REGEX_ID = "^[\\d]+$";
69  
70      /**
71       * Private constructor
72       */
73      private GenericAttributesUtils( )
74      {
75          // Do nothing
76      }
77  
78      /**
79       * Return the field which title is specified in parameter
80       * 
81       * @param strTitle
82       *            the title
83       * @param listFields
84       *            the list of fields
85       * @return the field which title is specified in parameter
86       */
87      public static Field findFieldByTitleInTheList( String strTitle, List<Field> listFields )
88      {
89          if ( ( listFields == null ) || listFields.isEmpty( ) )
90          {
91              return null;
92          }
93  
94          for ( Field field : listFields )
95          {
96              if ( StringUtils.isNotBlank( strTitle ) )
97              {
98                  if ( StringUtils.equals( StringUtils.trim( strTitle ), StringUtils.trim( field.getTitle( ) ) ) )
99                  {
100                     return field;
101                 }
102             }
103             else
104                 if ( StringUtils.isBlank( field.getTitle( ) ) )
105                 {
106                     return field;
107                 }
108         }
109 
110         return null;
111     }
112 
113     /**
114      * return the field which key is specified in parameter
115      * 
116      * @param nIdField
117      *            the id of the field who is search
118      * @param listField
119      *            the list of field
120      * @return the field which key is specified in parameter
121      */
122     public static Field findFieldByIdInTheList( int nIdField, List<Field> listField )
123     {
124         for ( Field field : listField )
125         {
126             if ( field.getIdField( ) == nIdField )
127             {
128                 return field;
129             }
130         }
131 
132         return null;
133     }
134 
135     /**
136      * Gets the generic attributes plugin
137      * 
138      * @return the plugin
139      */
140     public static Plugin getPlugin( )
141     {
142         return PluginService.getPlugin( GenericAttributesPlugin.PLUGIN_NAME );
143     }
144 
145     /**
146      * Convert a string to int
147      * 
148      * @param strParameter
149      *            the string parameter to convert
150      * @return the conversion
151      */
152     public static int convertStringToInt( String strParameter )
153     {
154         int nIdParameter = -1;
155 
156         try
157         {
158             if ( ( strParameter != null ) && strParameter.matches( REGEX_ID ) )
159             {
160                 nIdParameter = Integer.parseInt( strParameter );
161             }
162         }
163         catch( NumberFormatException ne )
164         {
165             AppLogService.error( ne );
166         }
167 
168         return nIdParameter;
169     }
170 
171     /**
172      * Create or update the entry field identified by its code.
173      * 
174      * @param entry
175      * @param strCode
176      * @param strTitle
177      * @param strValue
178      * @return
179      */
180     public static Field createOrUpdateField( Entry entry, String strCode, String strTitle, String strValue )
181     {
182         Field field = entry.getFieldByCode( strCode );
183         if ( field == null )
184         {
185             field = new Field( );
186             field.setCode( strCode );
187             field.setParentEntry( entry );
188             entry.getFields( ).add( field );
189         }
190         field.setTitle( strTitle );
191         field.setValue( strValue );
192         return field;
193     }
194 
195     /**
196      * Create a Field from a {@link ReferenceItem}
197      * 
198      * @param entry
199      * @param item
200      * @return
201      */
202     public static Field createFieldFromReferenceItem( Entry entry, ReferenceItem item )
203     {
204         Fieldins/genericattributes/business/Field.html#Field">Field field = new Field( );
205         field.setCode( IEntryTypeService.FIELD_ANSWER_CHOICE );
206         field.setValue( item.getCode( ) );
207         field.setTitle( item.getName( ) );
208         field.setDefaultValue( false );
209         field.setParentEntry( entry );
210         field.setLinkedItem( item );
211 
212         return field;
213     }
214 }