View Javadoc
1   package fr.paris.lutece.plugins.newsletter.modules.htmldocs.service;
2   
3   import fr.paris.lutece.plugins.htmldocs.business.HtmlDoc;
4   import fr.paris.lutece.plugins.htmldocs.business.HtmlDocFilter;
5   import fr.paris.lutece.plugins.htmldocs.business.HtmlDocHome;
6   import fr.paris.lutece.plugins.htmldocs.business.portlet.HtmlDocsListPortletHome;
7   import fr.paris.lutece.plugins.htmldocs.service.HtmldocsPlugin;
8   import fr.paris.lutece.plugins.htmldocs.service.PublishingService;
9   import fr.paris.lutece.plugins.newsletter.modules.htmldocs.business.NewsletterHtmlDoc;
10  import fr.paris.lutece.plugins.newsletter.modules.htmldocs.business.NewsletterHtmlDocHome;
11  import fr.paris.lutece.plugins.newsletter.modules.htmldocs.util.NewsletterHtmlDocUtils;
12  import fr.paris.lutece.plugins.newsletter.service.NewsletterPlugin;
13  import fr.paris.lutece.plugins.newsletter.service.NewsletterService;
14  import fr.paris.lutece.plugins.newsletter.util.NewsLetterConstants;
15  import fr.paris.lutece.plugins.newsletter.util.NewsletterUtils;
16  import fr.paris.lutece.portal.business.portlet.Portlet;
17  import fr.paris.lutece.portal.business.portlet.PortletTypeHome;
18  import fr.paris.lutece.portal.business.user.AdminUser;
19  import fr.paris.lutece.portal.service.plugin.Plugin;
20  import fr.paris.lutece.portal.service.plugin.PluginService;
21  import fr.paris.lutece.portal.service.portlet.PortletService;
22  import fr.paris.lutece.portal.service.template.AppTemplateService;
23  import fr.paris.lutece.portal.service.util.AppLogService;
24  import fr.paris.lutece.portal.service.util.AppPathService;
25  import fr.paris.lutece.util.ReferenceList;
26  import fr.paris.lutece.util.html.HtmlTemplate;
27  import fr.paris.lutece.util.url.UrlItem;
28  
29  import java.io.File;
30  import java.io.FileOutputStream;
31  import java.io.IOException;
32  import java.sql.Timestamp;
33  import java.util.Collection;
34  import java.util.HashMap;
35  import java.util.List;
36  import java.util.Locale;
37  import java.util.Map;
38  
39  import org.apache.commons.io.IOUtils;
40  import org.apache.commons.lang.StringUtils;
41  
42  /**
43   * Newsletter document service. This class implements the singleton design pattern.
44   */
45  public class NewsletterHtmlDocService
46  {
47      private static final String FULLSTOP = ".";
48  
49      private static final String MARK_IMG_PATH = "img_path";
50      private static final String MARK_DOCUMENT_PORTLETS_COLLEC = "portlets_collec";
51      private static final String MARK_DOCUMENT = "document";
52      private static final String MARK_FILE_ID = "id_file";
53  
54      private static final String DOCUMENT_RESOURCE_SERVLET_URL = "servlet/plugins/htmldocs/file";
55  
56      private static NewsletterHtmlDocService _singleton = new NewsletterHtmlDocService( );
57      private NewsletterService _newsletterService = NewsletterService.getService( );
58  
59      /**
60       * Returns the instance of the singleton
61       * 
62       * @return The instance of the singleton
63       */
64      public static NewsletterHtmlDocService getInstance( )
65      {
66          return _singleton;
67      }
68  
69      /**
70       * Copy specified document's type file into a given folder
71       * 
72       * @param document
73       *            the htmldoc
74       * @param strFileType
75       *            the file type
76       * @param strDestFolderPath
77       *            the destination folder
78       * @return name of the copy file or null if there is no copied file
79       */
80      public String copyFileFromDocument( HtmlDoc document, String strFileType, String strDestFolderPath )
81      {
82          String strFileName = null;
83  
84          byte [ ] tabByte = document.getDocContent( ).getBinaryValue( );
85          strFileName = NewsletterHtmlDocUtils.formatInteger( document.getId( ), 5 )
86                  + NewsletterHtmlDocUtils.formatInteger( document.getDocContent( ).getId( ), 5 ) + NewsletterHtmlDocUtils.formatInteger( 1, 5 ) + FULLSTOP
87                  + StringUtils.substringAfterLast( document.getDocContent( ).getTextValue( ), FULLSTOP );
88  
89          FileOutputStream fos = null;
90  
91          try
92          {
93              File file = new File( strDestFolderPath );
94              if ( !file.exists( ) )
95              {
96                  if ( !file.mkdir( ) )
97                  {
98                      throw new IOException( );
99                  }
100             }
101 
102             file = new File( strDestFolderPath + strFileName );
103             fos = new FileOutputStream( file );
104             IOUtils.write( tabByte, fos );
105         }
106         catch( IOException e )
107         {
108             AppLogService.error( e );
109         }
110         catch( Exception e )
111         {
112             AppLogService.error( e );
113         }
114         finally
115         {
116             IOUtils.closeQuietly( fos );
117         }
118 
119         return strFileName;
120     }
121 
122     /**
123      * Generate the html code for documents corresponding to the documents associated with the topic and to a given publishing date
124      * 
125      * @param newsletterDocument
126      *            the topic to generate
127      * @param nTemplateId
128      *            the document id to use
129      * @param datePublishing
130      *            minimum date of publishing of documents. Documents published before this date will not be considered
131      * @param strBaseUrl
132      *            the url of the portal
133      * @param user
134      *            The current admin user
135      * @param locale
136      *            The locale
137      * @return the html code for the document list of null if no document template available
138      */
139     public String generateDocumentsList( NewsletterHtmlDoc newsletterDocument, int nTemplateId, Timestamp datePublishing, String strBaseUrl, AdminUser user,
140             Locale locale )
141     {
142         Plugin pluginNewsLetter = PluginService.getPlugin( NewsletterPlugin.PLUGIN_NAME );
143         HtmlDocFilter documentFilter = new HtmlDocFilter( );
144         String strTemplatePath = NewsletterUtils.getHtmlTemplatePath( nTemplateId, pluginNewsLetter );
145 
146         if ( strTemplatePath == null )
147         {
148             return null;
149         }
150         Collection<HtmlDoc> listDocuments = null;
151         if ( newsletterDocument.getUseDocumentTags( ) )
152         {
153             int [ ] arrayTagIds = NewsletterHtmlDocHome.findNewsletterTagIds( newsletterDocument.getId( ), pluginNewsLetter );
154             if ( arrayTagIds != null && arrayTagIds.length > 0 )
155             {
156                 documentFilter.setTagsId( arrayTagIds );
157             }
158             listDocuments = PublishingService.getInstance( ).getPublishedDocumentsSinceDate( datePublishing, datePublishing, documentFilter, locale );
159         }
160         else
161         {
162             int [ ] arrayPortletsIds = NewsletterHtmlDocHome.findNewsletterPortletsIds( newsletterDocument.getId( ), pluginNewsLetter );
163             if ( arrayPortletsIds != null && arrayPortletsIds.length > 0 )
164             {
165                 Plugin documentPlugin = PluginService.getPlugin( HtmldocsPlugin.PLUGIN_NAME );
166                 List<Integer> listDocumentIds = NewsletterHtmlDocHome.getPublishedDocumentsIdsListByPortletIds( arrayPortletsIds, datePublishing,
167                         datePublishing, documentPlugin );
168                 if ( listDocumentIds != null && listDocumentIds.size( ) > 0 )
169                 {
170                     int [ ] arrayDocumentsId = new int [ listDocumentIds.size( )];
171                     int nIndex = 0;
172                     for ( int nDocumentId : listDocumentIds )
173                     {
174                         arrayDocumentsId [nIndex] = nDocumentId;
175                         nIndex++;
176                     }
177                     documentFilter.setIds( arrayDocumentsId );
178                     documentFilter.setLoadBinaries( true );
179                     listDocuments = HtmlDocHome.findByFilter( documentFilter, locale );
180                 }
181             }
182         }
183 
184         if ( listDocuments == null )
185         {
186             return StringUtils.EMPTY;
187         }
188 
189         StringBuffer sbDocumentLists = new StringBuffer( );
190 
191         // get html from templates
192         for ( HtmlDoc document : listDocuments )
193         {
194             String strContent = fillTemplateWithDocumentInfos( strTemplatePath, document, locale, strBaseUrl, user );
195             sbDocumentLists.append( strContent );
196         }
197 
198         return sbDocumentLists.toString( );
199     }
200 
201     /**
202      * Fills a given document template with the document data
203      * 
204      * @return the html code corresponding to the document data
205      * @param strBaseUrl
206      *            The base url of the portal
207      * @param strTemplatePath
208      *            The path of the template file
209      * @param document
210      *            the object gathering the document data
211      * @param locale
212      *            the locale used to build the template
213      * @param user
214      *            The current user
215      */
216     public String fillTemplateWithDocumentInfos( String strTemplatePath, HtmlDoc document, Locale locale, String strBaseUrl, AdminUser user )
217     {
218         Collection<Portlet> porletCollec = PublishingService.getInstance( ).getPortletsByDocumentId( Integer.toString( document.getId( ) ) );
219         porletCollec = PortletService.getInstance( ).getAuthorizedPortletCollection( porletCollec, user );
220         // the document insert in the buffer must be publish in a authorized portlet
221         if ( porletCollec.size( ) > 0 )
222         {
223             // the document insert in the buffer must be publish in a authorized portlet
224             Map<String, Object> model = new HashMap<String, Object>( );
225             model.put( MARK_DOCUMENT, document );
226 
227             // if noSecuredImg is true, it will copy all document's picture in a no secured folder
228             if ( _newsletterService.useUnsecuredImages( ) )
229             {
230                 String strImgFolder = _newsletterService.getUnsecuredImagefolder( );
231                 String pictureName = NewsletterHtmlDocService.getInstance( ).copyFileFromDocument( document, NewsletterHtmlDocUtils.CONSTANT_IMG_FILE_TYPE,
232                         _newsletterService.getUnsecuredFolderPath( ) + strImgFolder );
233                 if ( pictureName != null )
234                 {
235                     model.put( MARK_IMG_PATH, _newsletterService.getUnsecuredWebappUrl( ) + strImgFolder + pictureName );
236                 }
237             }
238             else
239             {
240                 String strProdUrl = AppPathService.getProdUrl( strBaseUrl );
241 
242                 UrlItem urlItem = new UrlItem( strProdUrl + DOCUMENT_RESOURCE_SERVLET_URL );
243                 urlItem.addParameter( MARK_FILE_ID, ( document.getDocContent( ) != null ) ? document.getDocContent( ).getId( ) : 0 );
244                 model.put( MARK_IMG_PATH, urlItem.getUrl( ) );
245 
246             }
247 
248             model.put( NewsLetterConstants.MARK_BASE_URL, strBaseUrl );
249             model.put( MARK_DOCUMENT_PORTLETS_COLLEC, porletCollec );
250 
251             HtmlTemplate template = AppTemplateService.getTemplate( strTemplatePath, locale, model );
252 
253             return template.getHtml( );
254         }
255         return StringUtils.EMPTY;
256     }
257     /**
258      * Load the portlet of type HTMLDOC_LIST
259      * @return
260      */
261     public ReferenceList getPortletHtmlDocList(){
262     	
263     	ReferenceList list= new ReferenceList();
264     	String className = HtmlDocsListPortletHome.class.getName( );
265         String strPortletTypeId = PortletTypeHome.getPortletTypeId( className );
266         
267     	for(Portlet pt:PublishingService.getInstance().getHtmlDocsPortlets()){
268     		
269     		if(pt.getPortletTypeId().equals(strPortletTypeId)){
270     			list.addItem(pt.getId( ), pt.getName( ));
271     		}
272     		
273     	}
274     	return list;
275     	
276     }
277 }