KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cofax > cms > CofaxToolsWorkflow


1 /*
2  * CofaxToolsWorkflow is part of the Cofax content management system library.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Please see http://www.cofax.org for contact information and other related informaion.
19  *
20  * $Header: /cvsroot/cofax/cofax/src/org/cofax/cms/CofaxToolsWorkflow.java,v 1.15.2.1 2006/12/11 16:28:24 fxrobin Exp $
21  */

22
23 package org.cofax.cms;
24
25 import org.cofax.*;
26 import org.cofax.cds.*;
27 import javax.servlet.http.*;
28 import java.util.*;
29 import javax.mail.*;
30 import javax.mail.internet.*;
31
32 /**
33  * CofaxToolsWorkflow utilities for the workflow
34  *
35  * Copyright 2002 Smile Motoristes Internet http://www.smile.fr/ Contact
36  * cofax@smile.fr for further information
37  *
38  * @author Smile - Badr Chentouf
39  * @author Smile - Matthieu Bureau
40  * @author F.X. Robin - i18n
41  *
42  */

43
44 public class CofaxToolsWorkflow {
45
46     /**
47      * call the method setArticle and if the article can be saved, validate it
48      * Validate an article means to set the field workflow_state to 1
49      *
50      */

51     public static String JavaDoc validateArticle(DataStore db, HttpServletRequest req, HttpSession session) {
52
53         String JavaDoc itemID = (String JavaDoc) req.getAttribute("ITEMID");
54         String JavaDoc fileName = (String JavaDoc) req.getAttribute("FILENAME");
55         String JavaDoc delete = (String JavaDoc) req.getAttribute("DELETEARTICLE");
56         if ((itemID != null) && (!(itemID.equals("")))) {
57             // set workflow_state=1 and set_article
58
String JavaDoc message = CofaxToolsDbUtils.setArticle(db, req, session, "1");
59             if ((delete != null) && (delete.equals("on")))
60                 return (message);
61         }
62         String JavaDoc requestedUrl = req.getScheme() + "://" + req.getHeader("host");
63
64         // get user name
65
CofaxToolsUser user = (CofaxToolsUser) (session.getAttribute("user"));
66         String JavaDoc userFirstName = (String JavaDoc) user.userInfoHash.get("FIRSTNAME");
67         String JavaDoc userLastName = (String JavaDoc) user.userInfoHash.get("LASTNAME");
68
69         String JavaDoc emailList = getArticleValidators(db, req, session, itemID);
70         if (!(emailList.equals(""))) {
71             String JavaDoc subject = CofaxToolsUtil.getI18NMessage(req.getLocale(),"workflow_mail_subject_new_article_to_validate");
72             subject=subject.replaceAll("%fileName%",fileName);
73             subject=subject.replaceAll("%itemID%",itemID);
74             
75             String JavaDoc mailFrom = getAdminEmail(db, session);
76             
77             String JavaDoc messageBody = CofaxToolsUtil.getI18NMessage(req.getLocale(),"workflow_mail_body_new_article_to_validate");
78             messageBody = messageBody.replaceAll("%userFirstName%", userFirstName);
79             messageBody = messageBody.replaceAll("%userLastName%", userLastName);
80             messageBody = messageBody.replaceAll("%publication%", (String JavaDoc) req.getAttribute("PUBNAME"));
81             messageBody = messageBody.replaceAll("%headline%", (String JavaDoc) req.getAttribute("HEADLINE"));
82             messageBody = messageBody.replaceAll("%itemID%", itemID);
83             
84             boolean bln = sendMail(emailList, mailFrom, subject, messageBody);
85             
86             if (bln)
87                 emailList = CofaxToolsUtil.getI18NMessage(req.getLocale(),"workflow_mail_has_been_sent_to") + emailList;
88             else
89                 emailList = CofaxToolsUtil.getI18NMessage(req.getLocale(),"workflow_error_while_sending_mail") + emailList;
90         }
91         return ("'" + fileName + "' (" + itemID + ") : " + CofaxToolsUtil.getI18NMessage(req.getLocale(), "tools_articlevalidated") + " " + emailList + ".");
92
93     }
94
95     /**
96      * call the method setArticle and if the article can be saved, publish it
97      * Publish an article means to set the field workflow_state to 2 and to
98      * update the table tblarticlepreload
99      *
100      */

101     public static String JavaDoc publishArticle(DataStore db, HttpServletRequest req, HttpSession session) {
102
103         String JavaDoc itemID = (String JavaDoc) req.getAttribute("ITEMID");
104         String JavaDoc fileName = (String JavaDoc) req.getAttribute("FILENAME");
105         String JavaDoc delete = (String JavaDoc) req.getAttribute("DELETEARTICLE");
106         if ((itemID != null) && (!(itemID.equals("")))) {
107             // set workflow_state=2 and set_article
108
String JavaDoc message = CofaxToolsDbUtils.setArticle(db, req, session, "2");
109             if ((delete != null) && (delete.equals("on")))
110                 return (message);
111             else {
112
113                 message = "Article " + fileName + " (" + itemID + ") "+ CofaxToolsUtil.getI18NMessage(req.getLocale(),"workflow_has_been_saved_and_published");;
114                 // send a mail if the author is not the same than the publisher
115
String JavaDoc approved = (String JavaDoc) req.getAttribute("APPROVED");
116                 CofaxToolsUser user = (CofaxToolsUser) (session.getAttribute("user"));
117                 String JavaDoc userName = (String JavaDoc) user.userInfoHash.get("USERNAME");
118                 String JavaDoc userFirstName = (String JavaDoc) user.userInfoHash.get("FIRSTNAME");
119                 String JavaDoc userLastName = (String JavaDoc) user.userInfoHash.get("LASTNAME");
120                 if (!(userName.equals(approved))) {
121                     HashMap fillReq = new HashMap();
122                     fillReq.put("USERNAME", approved);
123                     String JavaDoc tag = CofaxToolsDbUtils.fillTag(db, "getUserInfoByUserName");
124                     HashMap results = CofaxToolsDbUtils.getNameValuePackageHash(db, fillReq, tag);
125                     String JavaDoc emailList = (String JavaDoc) results.get("EMAIL");
126
127                     if ((emailList != null) && !(emailList.equals(""))) {
128                         
129                         String JavaDoc mailFrom = getAdminEmail(db, session);
130                         
131                         String JavaDoc subject = CofaxToolsUtil.getI18NMessage(req.getLocale(),"workflow_mail_subject_published_article");
132                         subject=subject.replaceAll("%fileName%",fileName);
133                         subject=subject.replaceAll("%itemID%",itemID);
134                                                 
135                         String JavaDoc messageBody = CofaxToolsUtil.getI18NMessage(req.getLocale(),"workflow_mail_body_published_article");
136                         messageBody = messageBody.replaceAll("%userFirstName%", userFirstName);
137                         messageBody = messageBody.replaceAll("%userLastName%", userLastName);
138                         messageBody = messageBody.replaceAll("%publication%", (String JavaDoc) req.getAttribute("PUBNAME"));
139                         messageBody = messageBody.replaceAll("%headline%", (String JavaDoc) req.getAttribute("HEADLINE"));
140                         messageBody = messageBody.replaceAll("%itemID%", itemID);
141                         
142                         boolean bln = sendMail(emailList, mailFrom, subject, messageBody);
143                         
144                         if (bln)
145                             message += CofaxToolsUtil.getI18NMessage(req.getLocale(),"workflow_mail_has_been_sent_to") + emailList;
146                         else
147                             message += CofaxToolsUtil.getI18NMessage(req.getLocale(),"workflow_error_while_sending_mail_to") + emailList;
148                     }
149                 }
150                 return (message);
151             }
152         } else {
153             // how to set workflow_state=2 ?
154
return ("ERROR : no itemID found");
155         }
156
157     }
158
159     public static String JavaDoc getArticleValidators(DataStore db, HttpServletRequest req, HttpSession session, String JavaDoc itemID) {
160         String JavaDoc listEmail = "";
161
162         CofaxToolsUser user = (CofaxToolsUser) (session.getAttribute("user"));
163
164         HashMap ht = new HashMap();
165         ht.put("ITEMID", (String JavaDoc) req.getAttribute("ITEMID"));
166         ht.put("PUBID", user.workingPub);
167
168         ht = CofaxToolsUtil.getPublicationInfo(db, user.workingPub);
169         String JavaDoc Wlevels = smile.stored.utils.getString(ht, "WORKFLOW_LEVELS", "0");
170         String JavaDoc Wmail = smile.stored.utils.getString(ht, "WORKFLOW_MAIL", "1");
171         String JavaDoc WadminEmail = smile.stored.utils.getString(ht, "WORKFLOW_ADMINEMAIL", "");
172         if (Wmail.equals("1")) {
173             if (Wlevels.equals("1")) {
174                 // only one level for the workflow
175
// send a mail to the webmaster
176
return (WadminEmail);
177             } else {
178                 // multiple levels of validation
179
// find the next one who can validate
180
String JavaDoc tag1 = "select S.mappingCode ";
181                 tag1 = tag1 + "from tblsections AS S, ";
182                 tag1 = tag1 + "tblarticles AS A ";
183                 tag1 = tag1 + "where S.pubName=A.pubName ";
184                 tag1 = tag1 + "and S.section = A.section ";
185                 tag1 = tag1 + "and A.itemID=" + (String JavaDoc) req.getAttribute("ITEMID") + " ";
186                 HashMap fillReq = new HashMap();
187                 Vector mappingCodeVect = CofaxToolsDbUtils.getPackageVector(db, fillReq, tag1);
188                 String JavaDoc mappingCode = mappingCodeVect.get(0) + "";
189
190                 while (listEmail.equals("") && (!(mappingCode.equals("0"))) && (!(mappingCode.equals("0")))) {
191                     String JavaDoc tag = "select PU.email ";
192                     tag = tag + "from tblpermusersection AS PUS, tblpermusers AS PU ";
193                     tag = tag + "where PUS.manager=1 ";
194                     tag = tag + "and PUS.userID = PU.userID ";
195                     tag = tag + "and PUS.mappingCode=" + mappingCode + " ";
196                     Vector emailVect2 = CofaxToolsDbUtils.getPackageVector(db, fillReq, tag);
197                     int emailSize2 = emailVect2.size();
198                     for (int i = 0; i < emailSize2; i++) {
199                         if (!(listEmail.equals("")))
200                             listEmail += ";";
201                         listEmail += emailVect2.get(i) + "";
202                     }
203                     if (listEmail.equals("")) {
204                         String JavaDoc tag3 = "select tblsections.subMapOf ";
205                         tag3 = tag3 + "from tblsections ";
206                         tag3 = tag3 + "where tblsections.mappingCode=" + mappingCode + " ";
207                         mappingCodeVect = CofaxToolsDbUtils.getPackageVector(db, fillReq, tag3);
208                         mappingCode = mappingCodeVect.get(0) + "";
209                     }
210                 }
211                 if (listEmail.equals(""))
212                     listEmail = WadminEmail;
213                 return (listEmail);
214             }
215         } else {
216             // no mail to send for this publication
217
return ("");
218         }
219     }
220
221     public static String JavaDoc getAdminEmail(DataStore db, HttpSession session) {
222
223         CofaxToolsUser user = (CofaxToolsUser) (session.getAttribute("user"));
224         String JavaDoc adminEmail = "cofax@cofax.org";
225         try {
226             HashMap ht = new HashMap();
227             ht.put("PUBID", user.workingPub);
228
229             ht = CofaxToolsUtil.getPublicationInfo(db, user.workingPub);
230             adminEmail = smile.stored.utils.getString(ht, "WORKFLOW_ADMINEMAIL", "");
231         } catch (Exception JavaDoc e) {
232             CofaxToolsUtil.log("CofaxToolsWorkflow : getAdminEmail : error " + e);
233         }
234         return (adminEmail);
235     }
236
237     /**
238      * Description of the Method
239      *
240      * @param glossary
241      * Description of the Parameter
242      * @param hostName
243      * Description of the Parameter
244      * @return Description of the Return Value
245      */

246     public static boolean sendMail(String JavaDoc mailList, String JavaDoc mailFrom, String JavaDoc subject, String JavaDoc messageBody) {
247
248         try {
249             Session smtpSession;
250             Properties props = new Properties();
251             props.put("mail.smtp.host", CDSServlet.mailHost);
252             smtpSession = Session.getDefaultInstance(props, null);
253             String JavaDoc mailTo = "";
254             if (mailList.indexOf(";") > 0) {
255                 while (mailList.indexOf(";") > 0) {
256                     try {
257                         mailTo = mailList.substring(0, mailList.indexOf(";"));
258                         mailList = mailList.substring(mailList.indexOf(";") + 1);
259
260                         // Define message
261
MimeMessage message = new MimeMessage(smtpSession);
262                         message.setFrom(new InternetAddress(mailFrom));
263                         message.addRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));
264                         message.setSubject(subject);
265                         message.setContent(messageBody, "text/html");
266
267                         // send message
268
CofaxToolsUtil.log("Sending mail to " + mailTo + "...");
269                         Transport.send(message);
270                         CofaxToolsUtil.log("Sending mail to " + mailTo + "...OK");
271                     } catch (Exception JavaDoc e) {
272                         CofaxToolsUtil.log("Error while sending mail to " + mailTo + " : " + e);
273                     }
274                 }
275                 // Define message
276
MimeMessage message = new MimeMessage(smtpSession);
277                 message.setFrom(new InternetAddress(mailFrom));
278                 message.addRecipient(Message.RecipientType.TO, new InternetAddress(mailList));
279                 message.setSubject(subject);
280                 message.setContent(messageBody, "text/html");
281
282                 // send message
283
CofaxToolsUtil.log("Sending mail to " + mailList + "...");
284                 Transport.send(message);
285                 CofaxToolsUtil.log("Sending mail to " + mailList + "...OK");
286             } else {
287                 try {
288                     // Define message
289
MimeMessage message = new MimeMessage(smtpSession);
290                     message.setFrom(new InternetAddress(mailFrom));
291                     message.addRecipient(Message.RecipientType.TO, new InternetAddress(mailList));
292                     message.setSubject(MimeUtility.encodeText(subject, "ISO-8859-1", null));
293                     message.setHeader("Content-Type", "text/html; charset=ISO-8859-1");
294                     message.setContent(messageBody, "text/html; charset=ISO-8859-1");
295
296                     // send message
297
CofaxToolsUtil.log("Sending mail to " + mailList + "...");
298                     Transport.send(message);
299                     CofaxToolsUtil.log("Sending mail to " + mailList + "...OK");
300                 } catch (Exception JavaDoc e) {
301                     CofaxToolsUtil.log("Error while sending mail to " + mailList + " : " + e);
302                 }
303             }
304
305         } catch (Exception JavaDoc e) {
306             return false;
307         }
308         return true;
309     }
310
311     public static Vector disableArticle(HashMap ht, String JavaDoc disableArticle, String JavaDoc validated, DataStore db, boolean permission) {
312         int toValidOrPubtemp = 0;
313         Vector disable = new Vector(2);
314         int check = 0;
315
316         if (disableArticle != null && disableArticle.equals("on")) {
317             if (validated.equals("0")) {
318                 toValidOrPubtemp = 0;
319                 check = 1;
320
321             } else if (validated.equals("1")) {
322                 toValidOrPubtemp = 1;
323                 check = 1;
324             } else if (validated.equals("2")) {
325                 if (permission) {
326                     toValidOrPubtemp = 2;
327                     check = 1;
328                 } else {
329                     toValidOrPubtemp = 0;
330                     check = 0;
331                 }
332             } else {
333                 toValidOrPubtemp = 0;
334                 check = 1;
335             }
336         } else {
337             if (validated.equals("2")) {
338                 if (permission) {
339                     StringBuffer JavaDoc tag_get = new StringBuffer JavaDoc();
340                     tag_get
341                             .append("SELECT disableArticle FROM tblactivearticles WHERE pubName='req:PUBNAME' and section='req:SECTION' AND itemID='req:ITEMID' AND filename='req:FILENAME'");
342                     ht = CofaxToolsDbUtils.getNameValuePackageHash(db, ht, tag_get.toString());
343                     String JavaDoc disableArticleOld = (String JavaDoc) ht.get("DISABLEARTICLE");
344
345                     if (disableArticleOld.equals("1")) {
346                         toValidOrPubtemp = 2;
347                         check = 1;
348                     } else {
349                         toValidOrPubtemp = 0;
350                         check = 1;
351                     }
352                 } else {
353                     toValidOrPubtemp = 0;
354                     check = 1;
355                 }
356             } else {
357                 toValidOrPubtemp = 0;
358                 check = 1;
359             }
360         }
361
362         Integer JavaDoc toValidOrPub = new Integer JavaDoc(toValidOrPubtemp);
363         Integer JavaDoc check2 = new Integer JavaDoc(check);
364         disable.add(toValidOrPub);
365         disable.add(check2);
366         return (disable);
367     }
368
369 }
370
Popular Tags