View Javadoc
1   package fr.paris.lutece.plugins.workflow.modules.eudonetrestdirectory.service;
2   
3   import java.sql.Timestamp;
4   import java.text.DateFormat;
5   import java.text.SimpleDateFormat;
6   import java.util.ArrayList;
7   import java.util.Date;
8   import java.util.List;
9   
10  import net.sf.json.JSONArray;
11  import net.sf.json.JSONObject;
12  
13  import org.apache.commons.codec.binary.Base64;
14  import org.apache.commons.lang.StringUtils;
15  
16  import fr.paris.lutece.plugins.directory.business.EntryFilter;
17  import fr.paris.lutece.plugins.directory.business.EntryHome;
18  import fr.paris.lutece.plugins.directory.business.File;
19  import fr.paris.lutece.plugins.directory.business.IEntry;
20  import fr.paris.lutece.plugins.directory.business.PhysicalFile;
21  import fr.paris.lutece.plugins.directory.business.PhysicalFileHome;
22  import fr.paris.lutece.plugins.directory.business.Record;
23  import fr.paris.lutece.plugins.directory.business.RecordField;
24  import fr.paris.lutece.plugins.directory.business.RecordFieldFilter;
25  import fr.paris.lutece.plugins.directory.business.RecordFieldHome;
26  import fr.paris.lutece.plugins.directory.business.RecordHome;
27  import fr.paris.lutece.plugins.directory.service.DirectoryPlugin;
28  import fr.paris.lutece.plugins.workflow.modules.eudonetrestdirectory.business.EudonetLink;
29  import fr.paris.lutece.plugins.workflow.modules.eudonetrestdirectory.business.EudonetLinkHome;
30  import fr.paris.lutece.plugins.workflow.modules.eudonetrestdirectory.business.EudonetRestData;
31  import fr.paris.lutece.portal.service.plugin.Plugin;
32  import fr.paris.lutece.portal.service.plugin.PluginService;
33  import fr.paris.lutece.portal.service.util.AppPropertiesService;
34  
35  public class BuildJsonBodyService
36  {
37      private static BuildJsonBodyService _singleton;
38      private static final String PROPERTY_ENTRY_TYPE_GEOLOCATION = "directory.entry_type.geolocation";
39      private static final String PROPERTY_ENTRY_TYPE_IMAGE = "directory.resource_rss.entry_type_image";
40  
41      // Constants
42      private static final int CONSTANT_ENTRY_DATE_CREATION = -2;
43      private static final int CONSTANT_ENTRY_DATE_MODIFICATION = -3;
44  
45      /**
46       * The plugin directory.
47       */
48      private final Plugin _pluginDirectory = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
49  
50      /**
51       * @return instance BuildJsonBodyService
52       */
53      public static BuildJsonBodyService getService( )
54      {
55          if ( _singleton == null )
56          {
57              _singleton = new BuildJsonBodyService( );
58  
59              return _singleton;
60          }
61  
62          return _singleton;
63      }
64  
65      /**
66       * get Entry list of directory
67       * 
68       * @param nidDirectory
69       * @return Entry list of directory
70       */
71      public List<IEntry> getListEntries( int nidDirectory )
72      {
73          Plugin pluginDirectory = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
74          List<IEntry> listEntries = new ArrayList<IEntry>( );
75          EntryFilter entryFilter = new EntryFilter( );
76          entryFilter.setIdDirectory( nidDirectory );
77  
78          listEntries = EntryHome.getEntryList( entryFilter, pluginDirectory );
79  
80          return listEntries;
81      }
82  
83      /**
84       * get record field value
85       * 
86       * @param nIdEntry
87       * @param nIdRecord
88       * @param nIdDirectory
89       * @return record field value
90       */
91      public String getRecordFieldValue( int nIdEntry, int nIdRecord, int nIdDirectory )
92      {
93          String strRecordFieldValue = StringUtils.EMPTY;
94          Plugin pluginDirectory = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
95  
96          if ( nIdEntry == CONSTANT_ENTRY_DATE_CREATION || nIdEntry == CONSTANT_ENTRY_DATE_MODIFICATION )
97          {
98              if ( nIdEntry == CONSTANT_ENTRY_DATE_CREATION )
99              {
100                 return getDateCreation( nIdRecord );
101             }
102             else
103             {
104                 return getDateModification( nIdRecord );
105             }
106         }
107 
108         IEntry entry = EntryHome.findByPrimaryKey( nIdEntry, pluginDirectory );
109 
110         if ( ( entry != null ) )
111         {
112             RecordFieldFilter recordFieldFilter = new RecordFieldFilter( );
113             recordFieldFilter.setIdDirectory( nIdDirectory );
114             recordFieldFilter.setIdEntry( entry.getIdEntry( ) );
115             recordFieldFilter.setIdRecord( nIdRecord );
116 
117             List<RecordField> listRecordFields = RecordFieldHome.getRecordFieldList( recordFieldFilter, pluginDirectory );
118 
119             if ( entry.getEntryType( ).getIdType( ) == AppPropertiesService.getPropertyInt( PROPERTY_ENTRY_TYPE_GEOLOCATION, 16 ) )
120             {
121                 if ( listRecordFields.size( ) >= 4 )
122                 {
123                     return listRecordFields.get( 2 ).getValue( ) + ", " + listRecordFields.get( 3 ).getValue( );
124                 }
125                 else
126                 {
127                     return StringUtils.EMPTY;
128                 }
129             }
130 
131             if ( entry.getEntryType( ).getIdType( ) == 8 )
132             {
133                 RecordField recordFieldIdDemand = listRecordFields.get( 0 );
134                 strRecordFieldValue = recordFieldIdDemand.getValue( );
135 
136                 if ( recordFieldIdDemand.getEntry( ) != null )
137                 {
138                     return recordFieldIdDemand.getEntry( ).getTitle( );
139                 }
140             }
141 
142             if ( entry.getEntryType( ).getIdType( ) == 4 )
143             {
144                 RecordField recordFieldIdDemand = listRecordFields.get( 0 );
145                 strRecordFieldValue = recordFieldIdDemand.getValue( );
146 
147                 if ( recordFieldIdDemand.getField( ) != null )
148                 {
149                     strRecordFieldValue = recordFieldIdDemand.getField( ).getTitle( );
150                 }
151                 try
152                 {
153                     long times = Long.parseLong( strRecordFieldValue );
154                     Date date = new Date( times );
155                     DateFormat sdf = new SimpleDateFormat( "yyyy/MM/dd HH:mm:ss" );
156                     String strDate = sdf.format( date );
157                     if ( strDate != null )
158                         return strDate;
159 
160                 }
161                 catch( Exception e )
162                 {
163                     // ("NumberFormatException: " + nfe.getMessage());
164                 }
165             }
166 
167             if ( ( listRecordFields != null ) && !listRecordFields.isEmpty( ) && ( listRecordFields.get( 0 ) != null ) )
168             {
169                 RecordField recordFieldIdDemand = listRecordFields.get( 0 );
170                 strRecordFieldValue = recordFieldIdDemand.getValue( );
171 
172                 if ( recordFieldIdDemand.getField( ) != null )
173                 {
174                     strRecordFieldValue = recordFieldIdDemand.getField( ).getTitle( );
175                 }
176             }
177         }
178 
179         return strRecordFieldValue;
180     }
181 
182     /**
183      * get record field value
184      * 
185      * @param nIdEntry
186      * @param nIdRecord
187      * @param nIdDirectory
188      * @return record field value
189      */
190     public List<File> getRecordFileValue( int nIdEntry, int nIdRecord, int nIdDirectory )
191     {
192     	List<File> listFile= new ArrayList<File>();
193         Plugin pluginDirectory = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
194 
195         IEntry entry = EntryHome.findByPrimaryKey( nIdEntry, pluginDirectory );
196 
197         if ( ( entry != null ) )
198         {
199             RecordFieldFilter recordFieldFilter = new RecordFieldFilter( );
200             recordFieldFilter.setIdDirectory( nIdDirectory );
201             recordFieldFilter.setIdEntry( entry.getIdEntry( ) );
202             recordFieldFilter.setIdRecord( nIdRecord );
203 
204             List<RecordField> listRecordFields = RecordFieldHome.getRecordFieldList( recordFieldFilter, pluginDirectory );
205 
206             if ( entry.getEntryType( ).getIdType( ) == 8 )
207             {
208 
209                 for(RecordField filed: listRecordFields){
210 	                if ( filed.getFile( ) != null )
211 	                {
212 	                	listFile.add(filed.getFile( ));
213 	                    //return recordFieldIdDemand.getFile( );
214 	                }
215                 }
216             }
217         }
218 
219         return listFile;
220     }
221 
222     /**
223      * get Identifiant of the record
224      * 
225      * @param nIdResource
226      * @return id Record
227      */
228     public int getRecord( int nIdResource )
229     {
230         Record record = RecordHome.findByPrimaryKey( nIdResource, _pluginDirectory );
231 
232         return record.getIdRecord( );
233     }
234 
235     private Timestamp getDateCreation( int nIdRecord, int nIdDirectory )
236     {
237         Plugin pluginDirectory = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
238 
239         RecordFieldFilter recordFieldFilter = new RecordFieldFilter( );
240         recordFieldFilter.setIdDirectory( nIdDirectory );
241         recordFieldFilter.setIdRecord( nIdRecord );
242 
243         List<RecordField> listRecordFields = RecordFieldHome.getRecordFieldList( recordFieldFilter, pluginDirectory );
244 
245         if ( ( listRecordFields != null ) && !listRecordFields.isEmpty( ) && ( listRecordFields.get( 0 ) != null ) )
246         {
247             return listRecordFields.get( 0 ).getRecord( ).getDateCreation( );
248         }
249 
250         return null;
251     }
252 
253     private String getDateCreation( int nIdRecord )
254     {
255         Plugin pluginDirectory = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
256 
257         Record record = RecordHome.findByPrimaryKey( nIdRecord, pluginDirectory );
258         if ( record != null )
259         {
260             long times = record.getDateCreation( ).getTime( );
261             Date date = new Date( times );
262             DateFormat sdf = new SimpleDateFormat( "yyyy/MM/dd HH:mm:ss" );
263             String strDate = sdf.format( date );
264             if ( strDate != null )
265                 return strDate;
266         }
267 
268         return "";
269     }
270 
271     private String getDateModification( int nIdRecord )
272     {
273         Plugin pluginDirectory = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
274 
275         Record record = RecordHome.findByPrimaryKey( nIdRecord, pluginDirectory );
276         if ( record != null )
277         {
278             long times = record.getDateModification( ).getTime( );
279             Date date = new Date( times );
280             DateFormat sdf = new SimpleDateFormat( "yyyy/MM/dd HH:mm:ss" );
281             String strDate = sdf.format( date );
282             if ( strDate != null )
283                 return strDate;
284         }
285 
286         return "";
287     }
288 
289     public String getCreateRecordJsonBody( int nIdTable, List<EudonetRestData> _entries, int nIdRessource, int nIdDirectory )
290     {
291         JSONObject jsonObjectFinal = new JSONObject( );
292         JSONArray jsonArray = new JSONArray( );
293 
294         for ( EudonetRestData entry : _entries )
295         {
296             String strIdTable = entry.getIdTable( ).split( "-" ) [0];
297 
298             if ( strIdTable.equals( "" + nIdTable ) )
299             {
300                 String strIdAtt = entry.getIdAttribut( ).split( "-" ) [0];
301                 JSONObject jsonObject = new JSONObject( );
302                 jsonObject.accumulate( "DescId", Integer.parseInt( strIdAtt ) );
303                 if ( entry.getDefaultValue( ) != null && !entry.getDefaultValue( ).isEmpty( ) )
304                     jsonObject.accumulate( "Value", entry.getDefaultValue( ) );
305                 else
306                     jsonObject.accumulate( "Value", getRecordFieldValue( entry.getOrderEntry( ), nIdRessource, nIdDirectory ) );
307 
308                 jsonArray.add( jsonObject );
309             }
310         }
311 
312         jsonObjectFinal.accumulate( "Fields", jsonArray );
313 
314         return jsonObjectFinal.toString( );
315     }
316 
317     public String getCreateRecordJsonBodyLink( int nIdTable, List<EudonetRestData> _entries, int nIdRessource, int nIdDirectory, List<Integer> listTableLinked )
318     {
319         JSONObject jsonObjectFinal = new JSONObject( );
320         JSONArray jsonArray = new JSONArray( );
321 
322         for ( EudonetRestData entry : _entries )
323         {
324             String strIdTable = entry.getIdTable( ).split( "-" ) [0];
325 
326             if ( strIdTable.equals( "" + nIdTable ) )
327             {
328                 String strIdAtt = entry.getIdAttribut( ).split( "-" ) [0];
329                 JSONObject jsonObject = new JSONObject( );
330 
331                 jsonObject.accumulate( "DescId", Integer.parseInt( strIdAtt ) );
332                 if ( entry.getDefaultValue( ) != null && !entry.getDefaultValue( ).isEmpty( ) )
333                     jsonObject.accumulate( "Value", entry.getDefaultValue( ) );
334                 else
335                     jsonObject.accumulate( "Value", getRecordFieldValue( entry.getOrderEntry( ), nIdRessource, nIdDirectory ) );
336 
337                 jsonArray.add( jsonObject );
338             }
339         }
340 
341         for ( Integer i : listTableLinked )
342         {
343             EudonetLink eudonetLink = EudonetLinkHome.findBy( nIdRessource, i );
344             if ( eudonetLink != null )
345             {
346                 JSONObject jsonObject = new JSONObject( );
347 
348                 jsonObject.accumulate( "DescId", eudonetLink.getIdTable( ) );
349                 jsonObject.accumulate( "Value", "" + eudonetLink.getIdField( ) );
350 
351                 jsonArray.add( jsonObject );
352             }
353         }
354 
355         jsonObjectFinal.accumulate( "Fields", jsonArray );
356 
357         return jsonObjectFinal.toString( );
358     }
359 
360     public JSONArray getCreateAnnexeJsonBody( int nIdFile, int nIdTable, List<EudonetRestData> _entries, int nIdRessource, int nIdDirectory )
361     {
362         JSONArray jsonArray = new JSONArray( );
363 
364         for ( EudonetRestData entry : _entries )
365         {
366             String strIdTableLink = entry.getIdTableLink( ).split( "-" ) [0];
367             String strIdTable = entry.getIdTable( ).split( "-" ) [0];
368             if ( !strIdTableLink.isEmpty( ) && strIdTable.equals( "" + nIdTable ) )
369             {
370                 List<File> listFile = getRecordFileValue( entry.getOrderEntry( ), nIdRessource, nIdDirectory );
371                 for ( File file: listFile )
372                 {
373                     PhysicalFile physicalFile = PhysicalFileHome.findByPrimaryKey( file.getPhysicalFile( ).getIdPhysicalFile( ), _pluginDirectory );
374                     String strFileName = file.getTitle( );
375                     String strContent = "";
376                     if ( physicalFile != null && physicalFile.getValue( ) != null )
377                     {
378                         byte [ ] bytes = physicalFile.getValue( );
379                         byte [ ] encoded = Base64.encodeBase64( bytes );
380                         strContent = new String( encoded );
381                     }
382 
383                     JSONObject jsonObject = new JSONObject( );
384                     jsonObject.accumulate( "FileId", nIdFile );
385                     jsonObject.accumulate( "TabId", nIdTable );
386                     jsonObject.accumulate( "FileName", strFileName );
387                     jsonObject.accumulate( "Content", strContent );
388                     jsonObject.accumulate( "IsUrl", false );
389 
390                     jsonArray.add( jsonObject );
391                 }
392             }
393         }
394 
395         return jsonArray;
396     }
397 
398     public String getMetaInfosJsonBody( String strIdTable )
399     {
400         if ( strIdTable != null )
401         {
402             JSONObject jsonObject = new JSONObject( );
403             JSONArray jsonArray = new JSONArray( );
404             JSONObject jsonElement = new JSONObject( );
405             JSONArray jsonElementArray = new JSONArray( );
406 
407             jsonElementArray.add( 0 );
408 
409             jsonElement.accumulate( "DescId", Integer.parseInt( strIdTable ) );
410             jsonElement.accumulate( "AllFields", true );
411             jsonElement.accumulate( "Fields", jsonElementArray );
412 
413             jsonArray.add( jsonElement );
414 
415             jsonObject.accumulate( "Tables", jsonArray );
416 
417             return jsonObject.toString( );
418         }
419 
420         return null;
421     }
422 }