View Javadoc
1   /*
2    * Copyright (c) 2002-2014, 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.rss.service;
35  
36  import fr.paris.lutece.plugins.rss.business.RssGeneratedFile;
37  import fr.paris.lutece.plugins.rss.business.RssGeneratedFileHome;
38  import fr.paris.lutece.plugins.rss.web.FeedUtil;
39  import fr.paris.lutece.portal.business.rss.IResourceRss;
40  import fr.paris.lutece.portal.service.plugin.Plugin;
41  import fr.paris.lutece.portal.service.util.AppLogService;
42  import fr.paris.lutece.portal.service.util.AppPathService;
43  import fr.paris.lutece.util.filesystem.DirectoryNotFoundException;
44  import fr.paris.lutece.util.filesystem.FileSystemUtil;
45  
46  import java.io.File;
47  
48  import java.util.ArrayList;
49  import java.util.List;
50  
51  
52  /**
53   * This class provides management methods for auto updating pushrss files
54   */
55  public final class AutoUpdatingRss
56  {
57      ////////////////////////////////////////////////////////////////////////////
58      // Constants
59      private static final String UPDATING = "Updating rss file";
60  
61      /**
62       * Constructor
63       */
64      private AutoUpdatingRss(  )
65      {
66      }
67  
68      /**
69       * Processes the Updating RSS's files method
70       * @param plugin the plugin
71       * @return The result of this process (strLogs)
72       */
73      public static synchronized String processUpdatingRssFileFile( Plugin plugin )
74      {
75          // Get all the pushrss files saved in database
76          List<RssGeneratedFile> listPushRssDatabase = RssGeneratedFileHome.getRssFileList(  );
77          List<String> listFileNames = new ArrayList<String>(  );
78  
79          for ( RssGeneratedFile generatedFile : listPushRssDatabase )
80          {
81              listFileNames.add( generatedFile.getName(  ) );
82          }
83  
84          //String buffer for building the response page
85          StringBuffer strLogs = new StringBuffer(  );
86  
87          // fetch the pushRss directory path
88          String strFolderPath = AppPathService.getPath( RssGeneratorService.PROPERTY_RSS_STORAGE_FOLDER_PATH, "" );
89  
90          // Test if the pushRss directory exist and create it if it doesn't exist
91          if ( !new File( strFolderPath ).exists(  ) )
92          {
93              File fileFolder = new File( strFolderPath );
94              fileFolder.mkdir(  );
95          }
96  
97          List<File> listFiles;
98  
99          try
100         {
101             listFiles = FileSystemUtil.getFiles( strFolderPath, "" );
102 
103             if ( listFiles.size(  ) != 0 )
104             {
105                 for ( File file : listFiles )
106                 {
107                     if ( !listFileNames.contains( file.getName(  ) ) )
108                     {
109                         file.delete(  );
110                     }
111                 }
112             }
113         }
114         catch ( DirectoryNotFoundException e )
115         {
116             AppLogService.error( e.getMessage(  ), e );
117         }
118 
119         // Create again all the pushrss object saved in the database
120         for ( RssGeneratedFile generatedFile : listPushRssDatabase )
121         {
122             try
123             {
124                 // Call the create method
125                 String strRss = null;
126 
127                 if ( ( generatedFile.getPortletId(  ) == 0 ) && ( generatedFile.getTypeResourceRss(  ) != null ) )
128                 {
129                     IResourceRss resourceRss = RssService.getInstance(  )
130                                                          .getResourceRssInstance( generatedFile.getTypeResourceRss(  ),
131                             null );
132                     resourceRss.setId( generatedFile.getId(  ) );
133                     resourceRss.setMaxItems( generatedFile.getMaxItems(  ) );
134                     resourceRss.setEncoding( generatedFile.getEncoding(  ) );
135                     resourceRss.setFeedType( generatedFile.getFeedType(  ) );
136 
137                     strRss = FeedUtil.getFeed( resourceRss );
138                 }
139                 else
140                 {
141                     strRss = RssGeneratorService.createRssDocument( generatedFile.getPortletId(  ),
142                             generatedFile.getDescription(  ), generatedFile.getEncoding(  ),
143                             generatedFile.getFeedType(  ), generatedFile.getMaxItems(  ) );
144                 }
145 
146                 // Call the create file method
147                 RssGeneratorService.createFileRss( generatedFile.getName(  ), strRss );
148 
149                 // Update the database with the new pushrss object
150                 RssGeneratedFileHome.update( generatedFile );
151                 strLogs.append( UPDATING );
152             }
153             catch ( Exception e )
154             {
155                 AppLogService.error( "Error processing AutoUpdatingRssFile ", e );
156             }
157         }
158 
159         return strLogs.toString(  );
160     }
161 }