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.business;
35  
36  import fr.paris.lutece.plugins.rss.business.portlet.IRssPortletDAO;
37  import fr.paris.lutece.plugins.rss.business.portlet.RssPortlet;
38  import fr.paris.lutece.portal.service.spring.SpringContextService;
39  import fr.paris.lutece.util.ReferenceList;
40  
41  import java.util.ArrayList;
42  import java.util.List;
43  
44  
45  /**
46   * This class provides instances management methods (create, find, ...) for RssFeed objects
47   */
48  public final class RssFeedHome
49  {
50      // Static variable pointed at the DAO instance
51      private static IRssFeedDAO../../../../../fr/paris/lutece/plugins/rss/business/IRssFeedDAO.html#IRssFeedDAO">IRssFeedDAO _dao = (IRssFeedDAO) SpringContextService.getPluginBean( "rss", "rssFeedDAO" );
52      private static IRssPortletDAO./../fr/paris/lutece/plugins/rss/business/portlet/IRssPortletDAO.html#IRssPortletDAO">IRssPortletDAO _daoPortlet = (IRssPortletDAO) SpringContextService.getPluginBean( "rss",
53              "rssPortletDAO" );
54  
55      /**
56       * Private constructor - this class need not be instantiated
57       */
58      private RssFeedHome(  )
59      {
60      }
61  
62      /**
63       * Creation of an instance of rssFeed
64       * @param rssFeed The instance of the rssFeed which contains the informations to store
65       * @return The  instance of rssFeed which has been created with its primary key.
66       */
67      public static RssFeed"../../../../../../fr/paris/lutece/plugins/rss/business/RssFeed.html#RssFeed">RssFeed create( RssFeed rssFeed )
68      {
69          _dao.insert( rssFeed );
70  
71          return rssFeed;
72      }
73  
74      /**
75       * Update of the rssFeed which is specified in parameter
76       * @param rssFeed The instance of the rssFeed which contains the data to store
77       * @return The instance of the  rssFeed which has been updated
78       */
79      public static RssFeed"../../../../../../fr/paris/lutece/plugins/rss/business/RssFeed.html#RssFeed">RssFeed update( RssFeed rssFeed )
80      {
81          _dao.store( rssFeed );
82  
83          return rssFeed;
84      }
85  
86      /**
87       * Update of the rssFeed which is specified in parameter
88       * @param rssFeed The instance of the rssFeed which contains the data to store
89       * @return The instance of the  rssFeed which has been updated
90       */
91      public static RssFeed./../fr/paris/lutece/plugins/rss/business/RssFeed.html#RssFeed">RssFeed updateLastFetchInfos( RssFeed rssFeed )
92      {
93          _dao.storeLastFetchInfos( rssFeed );
94  
95          return rssFeed;
96      }
97  
98      /**
99       * Remove the RssFeed
100      * @param rssFeed The rssFeed to remove
101      */
102     public static void remove( RssFeed rssFeed )
103     {
104         if ( rssFeed.getIsActive(  ) )
105         {
106             int nMaxOrder = _dao.newPrimaryKey( true ) - 1;
107             rssFeed = updateOrder( rssFeed, nMaxOrder );
108         }
109 
110         _dao.delete( rssFeed );
111     }
112 
113     ///////////////////////////////////////////////////////////////////////////
114     // Finders
115 
116     /**
117      * Returns an instance of a rssFeed whose identifier is specified in parameter
118      * @param nKey The Primary key of the rssFeed
119      * @param bActive <code>true</code> if the rssFeed is active, <code>false</code> otherwise
120      * @return An instance of rssFeed
121      */
122     public static RssFeed findByPrimaryKey( int nKey, boolean bActive )
123     {
124         return _dao.load( nKey, bActive );
125     }
126 
127     /**
128      * Returns a list of rss feeds
129      * @return A List of rssFeeds
130      */
131     public static List<RssFeed> getRssFeeds(  )
132     {
133         return _dao.selectRssFeeds( true );
134     }
135 
136     /**
137      * Returns a list of rss feeds
138      * @param bActive <code>true</code> for active feeds
139      * @return A List of rssFeeds
140      */
141     public static List<RssFeed> getRssFeeds( boolean bActive )
142     {
143         return _dao.selectRssFeeds( bActive );
144     }
145 
146     /**
147      * Returns a list of rssFeeds objects
148      * @return A list of rssFeeds
149      */
150     public static ReferenceList getRssFeedsReferenceList(  )
151     {
152         return _dao.selectRssFeedReferenceList( true );
153     }
154 
155     /**
156      * Returns a list of rssFeeds objects
157      * @param bActive <code>true</code> for active feeds
158      * @return A list of rssFeeds
159      */
160     public static ReferenceList getRssFeedsReferenceList( boolean bActive )
161     {
162         return _dao.selectRssFeedReferenceList( bActive );
163     }
164 
165     /**
166      * Checks whether the a url is found in one of the external feeds
167      * @param strUrl The url to be tested
168      * @return The boolean result
169      */
170     public static boolean checkUrlNotUsed( String strUrl )
171     {
172         return _dao.checkUrlNotUsed( strUrl );
173     }
174 
175     /**
176      * Updates an active feed to a new order and shifts in-between feeds consequently.
177      * Linked portlets get updated as well.
178      *
179      * @param rssFeed The feed to update order
180      * @param nNewOrder the new order for the feed
181      * @return the feed updated to the new order or <code>null</code> if the feed is inactive
182      * @throws IndexOutOfBoundsException if  the new order parameter is less than 1 or greater than the current max order
183      */
184     public static RssFeed./../../../../fr/paris/lutece/plugins/rss/business/RssFeed.html#RssFeed">RssFeed updateOrder( RssFeed rssFeed, int nNewOrder )
185         throws IndexOutOfBoundsException
186     {
187         if ( !rssFeed.getIsActive(  ) )
188         {
189             return null;
190         }
191 
192         int nMaxOrder = _dao.newPrimaryKey( true ) - 1;
193         int nOldOrder = rssFeed.getId(  );
194 
195         if ( ( nNewOrder < 1 ) || ( nNewOrder > nMaxOrder ) )
196         {
197             throw new IndexOutOfBoundsException(  );
198         }
199 
200         if ( nNewOrder == nOldOrder )
201         {
202             return rssFeed;
203         }
204 
205         List<RssPortlet> listLinkedPortlet = new ArrayList<RssPortlet>(  );
206 
207         for ( RssFeed currentRssFeed : getRssFeeds( true ) )
208         {
209             int nCurrentOrder = currentRssFeed.getId(  );
210 
211             if ( ( nOldOrder > nNewOrder ) && ( nCurrentOrder < nOldOrder ) && ( nCurrentOrder >= nNewOrder ) )
212             {
213                 currentRssFeed.setId( nCurrentOrder + 1 );
214                 _dao.store( currentRssFeed );
215                 _dao.storeLastFetchInfos( currentRssFeed );
216 
217                 for ( RssPortlet linkedPortlet : _daoPortlet.checkLinkedPortlet( nCurrentOrder ) )
218                 {
219                     linkedPortlet.setRssFeedId( Integer.toString( nCurrentOrder + 1 ) );
220                     listLinkedPortlet.add( linkedPortlet );
221                 }
222             }
223             else if ( ( nOldOrder < nNewOrder ) && ( nCurrentOrder > nOldOrder ) && ( nCurrentOrder <= nNewOrder ) )
224             {
225                 currentRssFeed.setId( nCurrentOrder - 1 );
226                 _dao.store( currentRssFeed );
227                 _dao.storeLastFetchInfos( currentRssFeed );
228 
229                 for ( RssPortlet linkedPortlet : _daoPortlet.checkLinkedPortlet( nCurrentOrder ) )
230                 {
231                     linkedPortlet.setRssFeedId( Integer.toString( nCurrentOrder - 1 ) );
232                     listLinkedPortlet.add( linkedPortlet );
233                 }
234             }
235         }
236 
237         rssFeed.setId( nNewOrder );
238         _dao.store( rssFeed );
239         _dao.storeLastFetchInfos( rssFeed );
240 
241         for ( RssPortlet linkedPortlet : _daoPortlet.checkLinkedPortlet( nOldOrder ) )
242         {
243             linkedPortlet.setRssFeedId( Integer.toString( nNewOrder ) );
244             listLinkedPortlet.add( linkedPortlet );
245         }
246 
247         for ( RssPortlet portletToUpdate : listLinkedPortlet )
248         {
249             _daoPortlet.store( portletToUpdate );
250         }
251 
252         return rssFeed;
253     }
254 
255     /**
256      * De/activate a feed
257      * @param rssFeed the feed to de/activate
258      * @param bActive <code>true</code> to activate the feed, <code>false</code> otherwise
259      * @return the updated feed
260      */
261     public static RssFeed/../../../../../fr/paris/lutece/plugins/rss/business/RssFeed.html#RssFeed">RssFeed setActive( RssFeed rssFeed, boolean bActive )
262     {
263         remove( rssFeed );
264         rssFeed.setIsActive( bActive );
265 
266         return create( rssFeed );
267     }
268 }