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.RssFeed;
37  import fr.paris.lutece.plugins.rss.business.RssFeedHome;
38  import fr.paris.lutece.plugins.rss.business.RssGeneratedFile;
39  import fr.paris.lutece.plugins.rss.business.resourceRss.IResourceRssFactory;
40  import fr.paris.lutece.portal.business.rss.IResourceRss;
41  import fr.paris.lutece.portal.business.rss.IResourceRssType;
42  import fr.paris.lutece.portal.service.i18n.I18nService;
43  import fr.paris.lutece.portal.service.spring.SpringContextService;
44  import fr.paris.lutece.portal.service.util.AppLogService;
45  import fr.paris.lutece.util.ReferenceList;
46  
47  import java.util.Collection;
48  import java.util.List;
49  import java.util.Locale;
50  
51  
52  /**
53   * The rss service
54   *
55   */
56  public class RssService implements IRssService
57  {
58      private static RssServicervice/RssService.html#RssService">RssService _singleton = new RssService(  );
59      private static final String BEAN_RESOURCE_RSS_FACTORY = "resourceRssFactory";
60      private IResourceRssFactory _resourceRssFactory;
61  
62      /**
63      * Initialize the Rss service
64      *
65      */
66      public void init(  )
67      {
68          RssGeneratedFile.init(  );
69      }
70  
71      /**
72       * Returns the instance of the singleton
73       *
74       * @return The instance of the singleton
75       */
76      public static RssService getInstance(  )
77      {
78          if ( _singleton == null )
79          {
80              _singleton = new RssService(  );
81          }
82  
83          return _singleton;
84      }
85  
86      /**
87       * Gets the RSS content
88       * @param nIdRssFeed The RSS id
89       * @return The content
90       */
91      public String getRssContent( int nIdRssFeed )
92      {
93          String rssContent = "";
94  
95          try
96          {
97              rssContent = RssContentLoader.fetchRssFeed( nIdRssFeed );
98          }
99          catch ( RssParsingException e )
100         {
101             AppLogService.error( e.getMessage(  ) );
102         }
103 
104         return rssContent;
105     }
106 
107     public ReferenceList getRefListRssFeed(  )
108     {
109         return RssFeedHome.getRssFeedsReferenceList(  );
110     }
111 
112     public static List<RssFeed> getRssFeed(  )
113     {
114         return RssFeedHome.getRssFeeds(  );
115     }
116 
117     /**
118     *
119     * @return a IResourceRssFactory Object
120     */
121     private IResourceRssFactory getResourceRssFactory(  )
122     {
123         if ( _resourceRssFactory == null )
124         {
125             _resourceRssFactory = (IResourceRssFactory) SpringContextService.getPluginBean( RssPlugin.PLUGIN_NAME,
126                     BEAN_RESOURCE_RSS_FACTORY );
127         }
128 
129         return _resourceRssFactory;
130     }
131 
132     /**
133      * return an instance of ResourceRss Object depending on the resource rss type
134      * @param strKey the type resource rss key
135      * @param locale the locale
136      * @return an instance of ResourceRss Object
137      */
138     public IResourceRss getResourceRssInstance( String strKey, Locale locale )
139     {
140         IResourceRss resourceRss = getResourceRssFactory(  ).getResourceRss( strKey );
141         resourceRss.getResourceRssType(  )
142                    .setTitle( I18nService.getLocalizedString( resourceRss.getResourceRssType(  ).getTitleI18nKey(  ),
143                 locale ) );
144 
145         return resourceRss;
146     }
147 
148     /**
149     *
150     * @return a list of ResourceRss type
151     */
152     public Collection<IResourceRssType> getResourceRssTypeList(  )
153     {
154         return getResourceRssFactory(  ).getAllResourceRssType(  );
155     }
156 
157     /**
158      * Return a reference list wich contains the ResourceRss types
159      * @param locale the locale
160      * @return a reference list wich contains the ResourceRss types
161      */
162     public ReferenceList getRefListResourceRssType( Locale locale )
163     {
164         Collection<IResourceRssType> listResourceRssType = getResourceRssTypeList(  );
165         ReferenceList refListResourceRssType = new ReferenceList(  );
166 
167         if ( listResourceRssType != null )
168         {
169             for ( IResourceRssType resourceRssType : listResourceRssType )
170             {
171                 refListResourceRssType.addItem( resourceRssType.getKey(  ),
172                     I18nService.getLocalizedString( resourceRssType.getTitleI18nKey(  ), locale ) );
173             }
174         }
175 
176         return refListResourceRssType;
177     }
178 }