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
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
61
62
63
64 public static NewsletterHtmlDocService getInstance( )
65 {
66 return _singleton;
67 }
68
69
70
71
72
73
74
75
76
77
78
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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
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
203
204
205
206
207
208
209
210
211
212
213
214
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
221 if ( porletCollec.size( ) > 0 )
222 {
223
224 Map<String, Object> model = new HashMap<String, Object>( );
225 model.put( MARK_DOCUMENT, document );
226
227
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
259
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 }