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.htmldocs.service;
35  
36  import fr.paris.lutece.plugins.htmldocs.business.HtmlDoc;
37  import fr.paris.lutece.plugins.htmldocs.business.HtmlDocFilter;
38  import fr.paris.lutece.plugins.htmldocs.business.HtmlDocHome;
39  import fr.paris.lutece.plugins.htmldocs.business.IndexerAction;
40  import fr.paris.lutece.plugins.htmldocs.business.portlet.HtmlDocPublication;
41  import fr.paris.lutece.plugins.htmldocs.business.portlet.HtmlDocPublicationHome;
42  import fr.paris.lutece.plugins.htmldocs.business.portlet.HtmldocsPortletHome;
43  import fr.paris.lutece.plugins.htmldocs.service.docsearch.HtmlDocSearchService;
44  import fr.paris.lutece.portal.business.portlet.Portlet;
45  import fr.paris.lutece.portal.business.portlet.PortletHome;
46  import fr.paris.lutece.portal.business.portlet.PortletType;
47  import fr.paris.lutece.portal.business.portlet.PortletTypeHome;
48  import fr.paris.lutece.portal.service.plugin.Plugin;
49  import fr.paris.lutece.portal.service.plugin.PluginService;
50  
51  import java.util.ArrayList;
52  import java.util.Collection;
53  import java.util.Date;
54  import java.util.List;
55  import java.util.Locale;
56  
57  
58  /**
59   * Publishing service
60   */
61  public class PublishingService
62  {
63      private static PublishingService _singleton = new PublishingService(  );
64  
65  
66      /**
67       * Get the unique instance of the service
68       * @return The unique instance
69       */
70      public static PublishingService getInstance(  )
71      {
72          return _singleton;
73      }
74  
75      /**
76       * Assign {@link Document} to a {@link Portlet}
77       *
78       * @param nDocumentId The {@link Document} identifier
79       * @param nPortletId The {@link Portlet} identifier
80       */
81      public void assign( int nDocumentId, int nPortletId )
82      {
83      	HtmlDocPublication documentPublication = new HtmlDocPublication(  );
84          documentPublication.setIdPortlet( nPortletId );
85          documentPublication.setIdDocument( nDocumentId );
86          HtmlDocPublicationHome.create( documentPublication );
87      }
88  
89      /**
90       * Publishing documents assigned to a portlet at the begin of the list
91       *
92       * @param nDocumentId the Document id
93       * @param nPortletId the portlet identifier
94       */
95      public void publish( int nDocumentId, int nPortletId )
96      {
97          // Publishing of document : set status to Published
98      	HtmlDocPublication documentPublication = HtmlDocPublicationHome.findDocPublicationByPimaryKey( nPortletId, nDocumentId );
99  
100         if ( documentPublication != null )
101         {
102         	 documentPublication.setIdPortlet( nPortletId );
103              documentPublication.setIdDocument( nDocumentId );
104              HtmlDocPublicationHome.update( documentPublication );
105 
106            
107         }
108 
109         HtmlDocSearchService.getInstance(  ).addIndexerAction( nDocumentId, IndexerAction.TASK_MODIFY, HtmldocsPlugin.getPlugin() );
110 
111     }
112 
113   
114     /**
115      * unAssign {@link Document} to a {@link Portlet}
116      *
117      * @param nDocumentId The {@link Document} identifier
118      * @param nPortletId The {@link Portlet} identifier
119      */
120     public void unAssign( int nDocumentId, int nPortletId )
121     {
122     	HtmlDocPublicationHome.remove( nPortletId, nDocumentId );
123     }
124 
125    
126     /**
127      * Check if the specified {@link Document} is published into the specified {@link Portlet}
128      * @param nDocumentId The {@link Document} identifier
129      * @param nPortletId The {@link Portlet} identifier
130      * @return True if {@link Document} is published, false else (unpublished or not assigned)
131      */
132     public boolean isPublished( int nDocumentId, int nPortletId )
133     {
134     	HtmlDocPublication documentPublication = HtmlDocPublicationHome.findDocPublicationByPimaryKey( nPortletId, nDocumentId );
135 
136         return documentPublication != null ?true:false;
137     }
138 
139    
140     /**
141      * Check if the specified {@link Document} is assigned (unpublished or published) into at least one {@link Portlet}
142      * @param nDocumentId The {@link Document} identifier
143      * @return True if {@link Document} is assigned (published or unpublished), false else (not assigned)
144      */
145     public boolean isAssigned( int nDocumentId )
146     {
147         Collection<HtmlDocPublication> listDocumentPublication = HtmlDocPublicationHome.getDocPublicationByIdDoc( nDocumentId );
148 
149         return ( listDocumentPublication.size(  ) > 0 );
150     }
151 
152     /**
153      * Check if the specified {@link Document} is assigned (unpublished or published) into the specified {@link Portlet}
154      * @param nDocumentId The {@link Document} identifier
155      * @param nPortletId The {@link Portlet} identifier
156      * @return True if {@link Document} is assigned (published or unpublished), false else (not assigned)
157      */
158     public boolean isAssigned( int nDocumentId, int nPortletId )
159     {
160     	HtmlDocPublication documentPublication = HtmlDocPublicationHome.findDocPublicationByPimaryKey( nPortletId, nDocumentId );
161 
162         return ( documentPublication != null );
163     }
164 
165     /**
166      * Return a {@link DocumentPublication} from a {@link Portlet} identifier and {@link Document} identifier
167      * @param nPortletId the {@link Portlet} identifier
168      * @param nDocumentId the {@link Document} identifier
169      * @return a {@link DocumentPublication} or null if no object match
170      */
171     public HtmlDocPublication getDocumentPublication( int nPortletId, int nDocumentId )
172     {
173         return HtmlDocPublicationHome.findDocPublicationByPimaryKey( nPortletId, nDocumentId );
174     }
175 
176     /**
177      * Loads the list of portlets 
178      *
179      * @return the {@link Collection} of the portlets
180      */
181     public Collection<Portlet> getHtmlDocsPortlets(  )
182     {
183         Plugin plugin = PluginService.getPlugin( HtmldocsPlugin.PLUGIN_NAME );
184         Collection<Portlet> listPortletsAll = new ArrayList<Portlet>(  );
185 
186         for ( PortletType portletType : plugin.getPortletTypes(  ) )
187         {
188             listPortletsAll.addAll( PortletHome.findByType( portletType.getId(  ) ) );
189         }       
190 
191         return listPortletsAll;
192     }
193     
194     /**
195      * Loads the list of portlets htmldocs empty and htmldocslist 
196      *
197      * @return the {@link Collection} of the portlets
198      */
199     public Collection<Portlet> getHtmlDocsPortletstoPublish(  )
200     {
201         Plugin plugin = PluginService.getPlugin( HtmldocsPlugin.PLUGIN_NAME );
202         Collection<Portlet> listPortletsAll = new ArrayList<Portlet>(  );
203 
204         for ( PortletType portletType : plugin.getPortletTypes(  ) )
205         {
206         	List<Portlet> listPortlet=PortletHome.findByType( portletType.getId(  ) );
207         	String className = HtmldocsPortletHome.class.getName( );
208 	        String strPortletTypeId = PortletTypeHome.getPortletTypeId( className );
209 	        
210         	if(portletType.getId(  ).equals(strPortletTypeId)){
211         		for(Portlet pt:listPortlet){
212         			if(HtmlDocPublicationHome.getDocPublicationByPortlet(pt.getId()).size( ) == 0){
213         				
214         				listPortletsAll.addAll( listPortlet );
215         			}
216         			
217         		}
218         		
219         	}else{
220         		
221         		listPortletsAll.addAll( listPortlet );
222         	}
223         }       
224 
225         return listPortletsAll;
226     }
227     
228     /**
229      * Loads the list of the portlets whoes contain htmldoc specified by id
230      *
231      * @param strDocumentId the htmlDoc identifier
232      * @return the {@link Collection} of the portlets
233      */
234     public Collection<Portlet> getPortletsByDocumentId( String strDocumentId )
235     {
236         Collection<HtmlDocPublication> listDocumentPublication = HtmlDocPublicationHome.getDocPublicationByIdDoc( Integer.parseInt( strDocumentId ) );
237         Collection<Portlet> listPortlets = new ArrayList<Portlet>(  );
238 
239         for ( HtmlDocPublication documentPublication : listDocumentPublication )
240         {
241             listPortlets.add( PortletHome.findByPrimaryKey( documentPublication.getIdPortlet( ) ) );
242         }
243 
244         return listPortlets;
245     }
246     
247     /**
248      * Loads the list of the htmlDoc whose filter and date publication is specified
249      * Return published documents since the publication date. The is also filtered with the documentFilter
250      *
251      * @param datePublishing The start publication date
252      * @param dateEndPublishing The end publication date
253      * @param documentFilter The filter for the published htmldoc. The filter can be null or empty. The array of Ids will not be taked in account.
254      * @param locale The locale is used to get the list of htmldocs with the findByFilter method
255      * @return the list of the htmldoc in form of a List. return null if datePublishing is null
256      */
257     public Collection<HtmlDoc> getPublishedDocumentsSinceDate( Date datePublishing, Date dateEndPublishing, HtmlDocFilter documentFilter,
258         Locale locale )
259     {
260         if ( datePublishing == null )
261         {
262             return null;
263         }
264 
265         Collection<HtmlDocPublication> listDocumentPublication = HtmlDocPublicationHome.findSinceDatePublishingAndStatus( datePublishing,dateEndPublishing,
266                 1 );
267 
268         if ( ( listDocumentPublication == null ) || ( listDocumentPublication.size(  ) == 0 ) )
269         {
270             return new ArrayList<HtmlDoc>(  );
271         }
272 
273         int[] arrayIds = new int[listDocumentPublication.size(  )];
274         int i = 0;
275         HtmlDocFilter publishedDocumentFilter = documentFilter;
276 
277         if ( publishedDocumentFilter == null )
278         {
279             publishedDocumentFilter = new HtmlDocFilter(  );
280         }
281 
282         for ( HtmlDocPublication documentPublication : listDocumentPublication )
283         {
284             arrayIds[i++] = documentPublication.getIdDocument(  );
285         }
286 
287         publishedDocumentFilter.setIds( arrayIds );
288 
289         Collection<HtmlDoc> listDocuments = HtmlDocHome.findByFilter( publishedDocumentFilter, locale );
290 
291         return listDocuments;
292     }
293     
294     /**
295      * Get the list of id of published htmldocs associated with a given
296      * collection of portlets.
297      * @param nPortletsIds The list of portlet ids.
298      * @param datePublishing TODO
299      * @param plugin The document plugin
300      * @return The list of documents id.
301      */
302     public static List<Integer> getPublishedDocumentsIdsListByPortletIds( int[] nPortletsIds, Date datePublishing, Date dateEndPublishing, Plugin plugin )
303     {
304         return HtmlDocPublicationHome.getPublishedDocumentsIdsListByPortletIds( nPortletsIds, datePublishing, dateEndPublishing, plugin );
305     }
306 
307 }