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.formengine.service.output;
35
36 import fr.paris.lutece.portal.service.html.XmlTransformerService;
37 import fr.paris.lutece.portal.service.mail.MailService;
38 import fr.paris.lutece.portal.service.util.AppLogService;
39
40 import java.io.File;
41 import java.io.StringReader;
42
43 import javax.xml.transform.stream.StreamSource;
44
45
46 /**
47 * Abstract class to define basis of mail output processors
48 */
49 public abstract class MailOutputProcessor extends OutputProcessor
50 {
51 private String _strXslFilePath;
52 private String _strXmlContent;
53 private String _strSenderName;
54 private String _strSenderEmail;
55 private String _strEmailSubject;
56 private String _strRecipientEmail;
57
58 /**
59 * @see fr.paris.lutece.plugins.formengine.service.output.OutputProcessor#generateOutput(java.lang.Object)
60 * @param transactionObject transaction object
61 */
62 protected void generateOutput( Object transactionObject )
63 {
64 // if no mail specified, do nothing
65 if ( ( _strRecipientEmail != null ) && ( !_strRecipientEmail.trim( ).equals( "" ) ) )
66 {
67 String strXslPath = getXslFilePath( );
68
69 File fileXsl = new File( strXslPath );
70 StreamSource sourceStyleSheet = new StreamSource( fileXsl );
71 StringReader srInput = new StringReader( getXmlContent( ) );
72 StreamSource sourceDocument = new StreamSource( srInput );
73
74 try
75 {
76 XmlTransformerService xmlTransformerService = new XmlTransformerService( );
77 String strMessageText = xmlTransformerService.transformBySourceWithXslCache( sourceDocument,
78 sourceStyleSheet, strXslPath, null, null );
79
80 MailService.sendMailHtml( getRecipientEmail( ), getSenderName( ), getSenderEmail( ),
81 getEmailSubject( ), strMessageText );
82 }
83 catch ( Exception e )
84 {
85 AppLogService.error( e.getMessage( ), e );
86 }
87 }
88 }
89
90 /**
91 * @return Returns the _strXslFilePath.
92 */
93 public String getXslFilePath( )
94 {
95 return _strXslFilePath;
96 }
97
98 /**
99 * @param strXslFilePath The _strXslFilePath to set.
100 */
101 public void setXslFilePath( String strXslFilePath )
102 {
103 _strXslFilePath = strXslFilePath;
104 }
105
106 /**
107 * @return Returns the _strEmailSubject.
108 */
109 public String getEmailSubject( )
110 {
111 return _strEmailSubject;
112 }
113
114 /**
115 * @param strEmailSubject The _strEmailSubject to set.
116 */
117 public void setEmailSubject( String strEmailSubject )
118 {
119 _strEmailSubject = strEmailSubject;
120 }
121
122 /**
123 * @return Returns the _strRecipientEmail.
124 */
125 public String getRecipientEmail( )
126 {
127 return _strRecipientEmail;
128 }
129
130 /**
131 * @param strRecipientEmail The _strRecipientEmail to set.
132 */
133 public void setRecipientEmail( String strRecipientEmail )
134 {
135 _strRecipientEmail = strRecipientEmail;
136 }
137
138 /**
139 * @return Returns the _strSenderEmail.
140 */
141 public String getSenderEmail( )
142 {
143 return _strSenderEmail;
144 }
145
146 /**
147 * @param strSenderEmail The _strSenderEmail to set.
148 */
149 public void setSenderEmail( String strSenderEmail )
150 {
151 _strSenderEmail = strSenderEmail;
152 }
153
154 /**
155 * @return Returns the _strSenderName.
156 */
157 public String getSenderName( )
158 {
159 return _strSenderName;
160 }
161
162 /**
163 * @param strSenderName The _strSenderName to set.
164 */
165 public void setSenderName( String strSenderName )
166 {
167 _strSenderName = strSenderName;
168 }
169
170 /**
171 * @return Returns the _strXmlContent.
172 */
173 public String getXmlContent( )
174 {
175 return _strXmlContent;
176 }
177
178 /**
179 * @param strXmlContent The _strXmlContent to set.
180 */
181 public void setXmlContent( String strXmlContent )
182 {
183 _strXmlContent = strXmlContent;
184 }
185 }