KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opencms > workplace > CmsTaskAction


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/workplace/CmsTaskAction.java,v $
3  * Date : $Date: 2005/08/31 07:29:39 $
4  * Version: $Revision: 1.7 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (C) 2001 The OpenCms Group
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about OpenCms, please see the
22  * OpenCms Website: http://www.opencms.org
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27  */

28
29 package com.opencms.workplace;
30
31 import org.opencms.file.CmsGroup;
32 import org.opencms.file.CmsObject;
33 import org.opencms.file.CmsRequestContext;
34 import org.opencms.file.CmsUser;
35 import org.opencms.mail.CmsMailTransport;
36 import org.opencms.mail.CmsSimpleMail;
37 import org.opencms.main.CmsException;
38 import org.opencms.main.CmsLog;
39 import org.opencms.main.OpenCms;
40 import org.opencms.util.CmsDateUtil;
41 import org.opencms.util.CmsStringUtil;
42 import org.opencms.workflow.CmsTask;
43 import org.opencms.workflow.CmsTaskLog;
44 import org.opencms.workflow.CmsTaskService;
45 import org.opencms.workplace.CmsFrameset;
46
47 import com.opencms.legacy.CmsLegacyException;
48
49 import java.util.ArrayList JavaDoc;
50 import java.util.GregorianCalendar JavaDoc;
51 import java.util.List JavaDoc;
52 import java.util.Vector JavaDoc;
53
54 import javax.mail.MessagingException JavaDoc;
55 import javax.mail.internet.AddressException JavaDoc;
56 import javax.mail.internet.InternetAddress JavaDoc;
57
58 /**
59  * This helper-class is used to do task-actions like create or forward. It uses the
60  * workplace-languagefile to add task-logs in the language of the user.
61  * <P>
62  *
63  * @author Andreas Schouten
64  * @version $Revision: 1.7 $ $Date: 2005/08/31 07:29:39 $
65  * @see com.opencms.workplace.CmsXmlWpTemplateFile
66  *
67  * @deprecated Will not be supported past the OpenCms 6 release.
68  */

69
70 public class CmsTaskAction {
71
72     /**
73      * Constant for generating user javascriptlist
74      */

75     private final static String JavaDoc C_ALL_ROLES = "___all";
76
77     /**
78      * Accepts a task.
79      * @param cms The cms-object.
80      * @param int taskid The id of the task.
81      * @throws CmsException Throws CmsExceptions, that are be
82      * thrown in calling methods.
83      */

84
85     public static void accept(CmsObject cms, int taskid) throws CmsException {
86
87         CmsTaskService taskService = cms.getTaskService();
88         taskService.acceptTask(taskid);
89         String JavaDoc comment = "";
90         taskService.writeTaskLog(taskid, comment, CmsWorkplaceDefault.C_TASKLOGTYPE_ACCEPTED);
91
92         // send an email if "Benachrichtigung bei Annahme" was selected.
93
CmsXmlLanguageFile lang = new CmsXmlLanguageFile(cms);
94         CmsTask task = taskService.readTask(taskid);
95         if (taskService.getTaskPar(task.getId(), CmsWorkplaceDefault.C_TASKPARA_ACCEPTATION) != null) {
96             StringBuffer JavaDoc contentBuf = new StringBuffer JavaDoc(lang.getLanguageValue("task.email.accept.content"));
97             contentBuf.append("\n");
98             contentBuf.append(lang.getLanguageValue("task.label.project"));
99             contentBuf.append(": ");
100             String JavaDoc projectname = "?";
101             try {
102                 projectname = taskService.readTask(task.getRoot()).getName();
103             } catch (Exception JavaDoc exc) {
104
105                 // no root?!
106
}
107             contentBuf.append(projectname);
108             contentBuf.append("\n");
109             contentBuf.append(lang.getLanguageValue("task.label.task"));
110             contentBuf.append(": ");
111             contentBuf.append(task.getName());
112             int projectid = taskService.readProject(task).getId();
113             contentBuf.append("\n\n\n" + getTaskUrl(cms, taskid, projectid));
114             String JavaDoc subject = lang.getLanguageValue("task.email.accept.subject")
115                 + " "
116                 + CmsUser.getFullName(taskService.readAgent(task));
117             CmsUser[] users = {taskService.readOwner(task)};
118             try {
119                 CmsSimpleMail mail = createMail(taskService.readAgent(task), users, subject, contentBuf.toString());
120                 new CmsMailTransport(mail).send();
121             } catch (Exception JavaDoc exc) {
122                 if (CmsLog.getLog(CmsTaskAction.class).isWarnEnabled()) {
123                     CmsLog.getLog(CmsTaskAction.class).warn("Error while sending task mail", exc);
124                 }
125             }
126         }
127     }
128
129     /**
130      * Sends a comment to the editor of the task.
131      * @param cms The cms-object.
132      * @param int taskid The id of the task.
133      * @param message The text of the message.
134      * @throws CmsException Throws CmsExceptions, that are be
135      * thrown in calling methods.
136      */

137
138     public static void comment(CmsObject cms, int taskid, String JavaDoc message) throws CmsException {
139
140         CmsXmlLanguageFile lang = new CmsXmlLanguageFile(cms);
141         CmsTaskService taskService = cms.getTaskService();
142         CmsTask task = taskService.readTask(taskid);
143         String JavaDoc comment = "";
144         if ((message != null) && (message.length() != 0)) {
145             comment += message;
146             taskService.writeTaskLog(taskid, comment, CmsWorkplaceDefault.C_TASKLOGTYPE_COMMENT);
147         }
148
149         // send an email
150
StringBuffer JavaDoc contentBuf = new StringBuffer JavaDoc(lang.getLanguageValue("task.email.message.content"));
151         contentBuf.append("\n");
152         contentBuf.append(lang.getLanguageValue("task.label.project"));
153         contentBuf.append(": ");
154         String JavaDoc projectname = "?";
155         try {
156             projectname = taskService.readTask(task.getRoot()).getName();
157         } catch (Exception JavaDoc exc) {
158
159             // no root?!
160
}
161         contentBuf.append(projectname);
162         contentBuf.append("\n");
163         contentBuf.append(lang.getLanguageValue("task.label.task"));
164         contentBuf.append(": ");
165         contentBuf.append(task.getName());
166         contentBuf.append("\n");
167         contentBuf.append(lang.getLanguageValue("task.label.actuator"));
168         contentBuf.append(": ");
169         contentBuf.append(CmsUser.getFullName(taskService.readOwner(task)));
170         int projectid = taskService.readProject(task).getId();
171         contentBuf.append("\n\n\n" + getTaskUrl(cms, task.getId(), projectid));
172
173         contentBuf.append(addTaskText(task, taskService));
174
175         String JavaDoc subject = lang.getLanguageValue("task.email.message.subject")
176             + " "
177             + CmsUser.getFullName(taskService.readOwner(task));
178         CmsUser[] users = {taskService.readAgent(task)};
179         try {
180             CmsSimpleMail mail = createMail(taskService.readOwner(task), users, subject, contentBuf.toString());
181             new CmsMailTransport(mail).send();
182         } catch (Exception JavaDoc exc) {
183             if (CmsLog.getLog(CmsTaskAction.class).isWarnEnabled()) {
184                 CmsLog.getLog(CmsTaskAction.class).warn("Error while sending task mail", exc);
185             }
186         }
187     }
188
189     /**
190      * Creates a new task.
191      * @param cms The cms-object.
192      * @param agentName The name of the new editor for this task.
193      * @param roleName The name of the new role for the user.
194      * @param taskName The new name of the task.
195      * @param taskcomment The new comment for this task.
196      * @param timeoutString The new timeout-date as a string in the following format:
197      * "dd.mm.yyyy"
198      * @param priorityString the new priority as String ("1" = high,
199      * "2" = normal or "3" = low)
200      * @param paraAcceptation controls if a message should be send by acceptation. ("checked" | "")
201      * @param paraAll controls if a message should be send to all users in a role. ("checked" | "")
202      * @param paraCompletion controls if a message should be send by completing this task. ("checked" | "")
203      * @param paraDelivery controls if a message should be send by delivering a task. ("checked" | "")
204      * @throws CmsException Throws CmsExceptions, that are be
205      * thrown in calling methods.
206      */

207
208     public static void create(
209         CmsObject cms,
210         String JavaDoc agentName,
211         String JavaDoc roleName,
212         String JavaDoc taskName,
213         String JavaDoc taskcomment,
214         String JavaDoc timeoutString,
215         String JavaDoc priorityString,
216         String JavaDoc paraAcceptation,
217         String JavaDoc paraAll,
218         String JavaDoc paraCompletion,
219         String JavaDoc paraDelivery) throws CmsException {
220
221         CmsXmlLanguageFile lang = new CmsXmlLanguageFile(cms);
222         // TODO: CW: check effects of this
223
// if(roleName.equals(C_ALL_ROLES)) {
224
// roleName = cms.readUser(agentName).getDefaultGroup().getName();
225
//}
226

227         // try to create the task
228
int priority = Integer.parseInt(priorityString);
229
230         // create a long from the overgiven date.
231
String JavaDoc splittetDate[] = CmsStringUtil.splitAsArray(timeoutString, '.');
232         GregorianCalendar JavaDoc cal = new GregorianCalendar JavaDoc(
233             Integer.parseInt(splittetDate[2]),
234             Integer.parseInt(splittetDate[1]) - 1,
235             Integer.parseInt(splittetDate[0]),
236             0,
237             0,
238             0);
239         long timeout = cal.getTime().getTime();
240         CmsTaskService taskService = cms.getTaskService();
241         CmsTask task = taskService.createTask(agentName, roleName, taskName, timeout, priority);
242         taskService.setTaskPar(task.getId(), CmsWorkplaceDefault.C_TASKPARA_ACCEPTATION, paraAcceptation);
243         taskService.setTaskPar(task.getId(), CmsWorkplaceDefault.C_TASKPARA_ALL, paraAll);
244         taskService.setTaskPar(task.getId(), CmsWorkplaceDefault.C_TASKPARA_COMPLETION, paraCompletion);
245         taskService.setTaskPar(task.getId(), CmsWorkplaceDefault.C_TASKPARA_DELIVERY, paraDelivery);
246         String JavaDoc comment = lang.getLanguageValue("task.label.forrole") + ": " + roleName + "\n";
247         comment += lang.getLanguageValue("task.label.editor")
248             + ": "
249             + CmsUser.getFullName(cms.readUser(task.getAgentUser()))
250             + "\n";
251         comment += taskcomment;
252         taskService.writeTaskLog(task.getId(), comment, CmsWorkplaceDefault.C_TASKLOGTYPE_CREATED);
253
254         // send an email
255
// per default send a mail from task's organizer to task's recipient.
256
StringBuffer JavaDoc contentBuf = new StringBuffer JavaDoc(lang.getLanguageValue("task.email.create.content"));
257         contentBuf.append("\n");
258         contentBuf.append(lang.getLanguageValue("task.label.project"));
259         contentBuf.append(": ");
260         String JavaDoc projectname = "?";
261         try {
262             projectname = taskService.readTask(task.getRoot()).getName();
263         } catch (Exception JavaDoc exc) {
264
265             // no root?!
266
}
267         contentBuf.append(projectname);
268         contentBuf.append("\n");
269         contentBuf.append(lang.getLanguageValue("task.label.task"));
270         contentBuf.append(": ");
271         contentBuf.append(task.getName());
272         contentBuf.append("\n");
273         contentBuf.append(lang.getLanguageValue("task.label.actuator"));
274         contentBuf.append(": ");
275         contentBuf.append(CmsUser.getFullName(taskService.readOwner(task)));
276         int projectid = taskService.readProject(task).getId();
277         contentBuf.append("\n\n\n" + getTaskUrl(cms, task.getId(), projectid));
278
279         contentBuf.append(addTaskText(task, taskService));
280
281         String JavaDoc subject = lang.getLanguageValue("task.email.create.subject")
282             + " "
283             + CmsUser.getFullName(cms.readUser(task.getAgentUser()))
284             + " / "
285             + roleName;
286         CmsUser[] users = {taskService.readAgent(task)};
287         CmsSimpleMail mail = null;
288         try {
289             mail = createMail(taskService.readOwner(task), users, subject, contentBuf.toString());
290         } catch (CmsException e) {
291             if (CmsLog.getLog(CmsTaskAction.class).isWarnEnabled()) {
292                 CmsLog.getLog(CmsTaskAction.class).warn(
293                     "Could not generate mail while creating task for " + taskService.readOwner(task).getName(),
294                     e);
295             }
296         }
297
298         // if "Alle Rollenmitglieder von Aufgabe Benachrichtigen" checkbox is selected.
299
if (taskService.getTaskPar(task.getId(), CmsWorkplaceDefault.C_TASKPARA_ALL) != null) {
300
301             // the news deliver always "checked" or ""
302
if (taskService.getTaskPar(task.getId(), CmsWorkplaceDefault.C_TASKPARA_ALL).equals("checked")) {
303                 try {
304                     CmsGroup group = taskService.readGroup(task);
305                     List JavaDoc groupUser = cms.getUsersOfGroup(group.getName());
306                     mail = createMail(taskService.readOwner(task), groupUser, subject, contentBuf.toString(), true);
307                 } catch (CmsException e) {
308                     if (CmsLog.getLog(CmsTaskAction.class).isWarnEnabled()) {
309                         CmsLog.getLog(CmsTaskAction.class).warn(
310                             "Could not generate mail while creating task for " + taskService.readOwner(task).getName(),
311                             e);
312                     }
313                 }
314             }
315         }
316         if (mail != null) {
317             new CmsMailTransport(mail).send();
318         }
319     }
320
321     /**
322      * Helper method to create an email object.<p>
323      *
324      * @param from the mail sender
325      * @param to the mail receivers
326      * @param subject the mail subject
327      * @param content the mail content
328      * @return the mail object to send
329      * @throws CmsException if creating the mail object fails
330      */

331     public static CmsSimpleMail createMail(CmsUser from, CmsUser[] to, String JavaDoc subject, String JavaDoc content)
332     throws CmsException {
333
334         Vector JavaDoc v = new Vector JavaDoc(to.length);
335         for (int i = 0; i < to.length; i++) {
336             if (to[i].getEmail() == null) {
337                 continue;
338             }
339             if (to[i].getEmail().equals("")) {
340                 continue;
341             }
342             if (to[i].getEmail().indexOf("@") == -1 || to[i].getEmail().indexOf(".") == -1) {
343                 throw new CmsLegacyException("["
344                     + CmsTaskAction.class.getName()
345                     + "] "
346                     + "Error in sending email, Invalid recipient email address: "
347                     + to[i].getEmail(), CmsLegacyException.C_BAD_NAME);
348             }
349             try {
350                 v.addElement(new InternetAddress JavaDoc(to[i].getEmail()));
351             } catch (AddressException JavaDoc e) {
352                 throw new CmsLegacyException("["
353                     + CmsTaskAction.class.getName()
354                     + "] "
355                     + "Error in sending email, Invalid recipient email address: "
356                     + to[i].getEmail(), CmsLegacyException.C_BAD_NAME);
357             }
358         }
359
360         if (v.size() == 0) {
361             throw new CmsLegacyException("["
362                 + CmsTaskAction.class.getName()
363                 + "] "
364                 + "Error in sending email,Unknown recipient email address.", CmsLegacyException.C_BAD_NAME);
365         }
366         return createMail(from, v, subject, content, false);
367     }
368
369     /**
370      * Helper method to create an email object.<p>
371      * @param from the mail sender
372      * @param to the mail receivers
373      * @param subject the mail subject
374      * @param content the mail content
375      * @param createAddresses if true, the Vector of receivers contains CmsUsers and they are transformed into Internetaddresses
376      *
377      * @return the mail object to send
378      * @throws CmsException if creating the mail object fails
379      */

380     public static CmsSimpleMail createMail(
381         CmsUser from,
382         List JavaDoc to,
383         String JavaDoc subject,
384         String JavaDoc content,
385         boolean createAddresses) throws CmsException {
386
387         CmsSimpleMail mail = new CmsSimpleMail();
388         // check sender email address
389
String JavaDoc fromAddress = from.getEmail();
390         if (fromAddress == null || fromAddress.equals("")) {
391             fromAddress = OpenCms.getSystemInfo().getMailSettings().getMailFromDefault();
392         }
393         if (fromAddress == null || fromAddress.equals("")) {
394             throw new CmsLegacyException("["
395                 + CmsTaskAction.class.getName()
396                 + "] "
397                 + "Error in sending email,Unknown sender email address.", CmsLegacyException.C_BAD_NAME);
398         }
399         if (fromAddress.indexOf("@") == -1 || fromAddress.indexOf(".") == -1) {
400             throw new CmsLegacyException("["
401                 + CmsTaskAction.class.getName()
402                 + "] "
403                 + "Error in sending email,Unknown sender email address: "
404                 + fromAddress, CmsLegacyException.C_BAD_NAME);
405         }
406
407         if (to.size() == 0) {
408             throw new CmsLegacyException("["
409                 + CmsTaskAction.class.getName()
410                 + "] "
411                 + "Error in sending email,Unknown recipient email address.", CmsLegacyException.C_BAD_NAME);
412         } else if (createAddresses) {
413             List JavaDoc receivers = new ArrayList JavaDoc(to.size());
414             for (int i = 0; i < to.size(); i++) {
415                 CmsUser rec = (CmsUser)to.get(i);
416                 try {
417                     receivers.add(new InternetAddress JavaDoc(rec.getEmail()));
418                 } catch (AddressException JavaDoc e) {
419                     throw new CmsLegacyException("["
420                         + CmsTaskAction.class.getName()
421                         + "] "
422                         + "Error in sending email, invalid recipient email address.", CmsLegacyException.C_BAD_NAME);
423                 }
424             }
425             mail.setTo(receivers);
426         } else {
427             mail.setTo(to);
428         }
429         try {
430             mail.setFrom(fromAddress);
431         } catch (MessagingException JavaDoc e) {
432             throw new CmsLegacyException("["
433                 + CmsTaskAction.class.getName()
434                 + "] "
435                 + "Error in sending email, invalid sender email address.", CmsLegacyException.C_BAD_NAME);
436         }
437         mail.setSubject(subject == null ? "" : subject);
438         mail.setMsg(content == null ? "" : content);
439         return mail;
440     }
441
442     /**
443      * Changes the timeou-date of the task.
444      * @param cms The cms-object.
445      * @param int taskid The id of the task.
446      * @param timeoutString The new timeout-date as a string in the following format:
447      * "dd.mm.yyyy"
448      * @throws CmsException Throws CmsExceptions, that are be
449      * thrown in calling methods.
450      */

451
452     public static void due(CmsObject cms, int taskid, String JavaDoc timeoutString) throws CmsException {
453
454         CmsXmlLanguageFile lang = new CmsXmlLanguageFile(cms);
455         CmsTaskService taskService = cms.getTaskService();
456         CmsTask task = taskService.readTask(taskid);
457         String JavaDoc splittetDate[] = CmsStringUtil.splitAsArray(timeoutString, '.');
458         GregorianCalendar JavaDoc cal = new GregorianCalendar JavaDoc(
459             Integer.parseInt(splittetDate[2]),
460             Integer.parseInt(splittetDate[1]) - 1,
461             Integer.parseInt(splittetDate[0]),
462             0,
463             0,
464             0);
465         long timeout = cal.getTime().getTime();
466         taskService.setTimeout(taskid, timeout);
467
468         // add comment
469
String JavaDoc comment = "";
470         comment += lang.getLanguageValue("task.dialog.due.logmessage1") + " ";
471         comment += CmsDateUtil.getDateShort(task.getTimeOut().getTime()) + " ";
472         comment += lang.getLanguageValue("task.dialog.due.logmessage2") + " ";
473         comment += CmsDateUtil.getDateShort(timeout);
474         taskService.writeTaskLog(taskid, comment, CmsWorkplaceDefault.C_TASKLOGTYPE_DUECHANGED);
475     }
476
477     /**
478      * Ends a task.
479      * @param cms The cms-object.
480      * @param int taskid The id of the task.
481      * @throws CmsException Throws CmsExceptions, that are be
482      * thrown in calling methods.
483      */

484
485     public static void end(CmsObject cms, int taskid) throws CmsException {
486
487         CmsTaskService taskService = cms.getTaskService();
488         taskService.endTask(taskid);
489         String JavaDoc comment = "";
490         taskService.writeTaskLog(taskid, comment, CmsWorkplaceDefault.C_TASKLOGTYPE_OK);
491
492         // send an email if "Benachrichtigung bei Abhacken" was selected.
493
CmsXmlLanguageFile lang = new CmsXmlLanguageFile(cms);
494         CmsTask task = taskService.readTask(taskid);
495         if (taskService.getTaskPar(task.getId(), CmsWorkplaceDefault.C_TASKPARA_COMPLETION) != null) {
496             StringBuffer JavaDoc contentBuf = new StringBuffer JavaDoc(lang.getLanguageValue("task.email.end.content"));
497             contentBuf.append("\n");
498             contentBuf.append(lang.getLanguageValue("task.label.project"));
499             contentBuf.append(": ");
500             String JavaDoc projectname = "?";
501             try {
502                 projectname = taskService.readTask(task.getRoot()).getName();
503             } catch (Exception JavaDoc exc) {
504
505                 // no root?!
506
}
507             contentBuf.append(projectname);
508             contentBuf.append("\n");
509             contentBuf.append(lang.getLanguageValue("task.label.task"));
510             contentBuf.append(": ");
511             contentBuf.append(task.getName());
512             contentBuf.append("\n");
513             contentBuf.append(lang.getLanguageValue("task.label.taskfor"));
514             contentBuf.append(": ");
515             contentBuf.append(CmsUser.getFullName(taskService.readOriginalAgent(task)));
516             contentBuf.append("\n");
517             contentBuf.append(lang.getLanguageValue("task.label.editor"));
518             contentBuf.append(": ");
519             contentBuf.append(CmsUser.getFullName(taskService.readAgent(task)));
520             int projectid = taskService.readProject(task).getId();
521             contentBuf.append("\n\n\n" + getTaskUrl(cms, task.getId(), projectid));
522             String JavaDoc subject = lang.getLanguageValue("task.email.end.subject")
523                 + " "
524                 + CmsUser.getFullName(taskService.readAgent(task));
525             CmsUser[] users = {taskService.readOwner(task)};
526             try {
527                 CmsSimpleMail mail = createMail(taskService.readAgent(task), users, subject, contentBuf.toString());
528                 new CmsMailTransport(mail).send();
529             } catch (Exception JavaDoc exc) {
530                 if (CmsLog.getLog(CmsTaskAction.class).isWarnEnabled()) {
531                     CmsLog.getLog(CmsTaskAction.class).warn("Error while sending task mail", exc);
532                 }
533             }
534         }
535     }
536
537     /**
538      * Forwards a task. The task is forwarded to a new editor in a new role.
539      * @param cms The cms-object.
540      * @param taskid The id of the task.
541      * @param newEditorName The name of the new editor for this task.
542      * @param newRoleName The name of the new role for the user.
543      * @throws CmsException Throws CmsExceptions, that are be
544      * thrown in calling methods.
545      */

546
547     public static void forward(CmsObject cms, int taskid, String JavaDoc newEditorName, String JavaDoc newRoleName) throws CmsException {
548
549         CmsXmlLanguageFile lang = new CmsXmlLanguageFile(cms);
550         CmsUser newEditor = cms.readUser(newEditorName);
551         // TODO: CW: check effects of this
552
// if(newRoleName.equals(C_ALL_ROLES)) {
553
// newRoleName = cms.readUser(newEditorName).getDefaultGroup().getName();
554
// }
555
CmsGroup oldRole = cms.readGroup(newRoleName);
556         CmsTaskService taskService = cms.getTaskService();
557         taskService.forwardTask(taskid, oldRole.getName(), newEditor.getName());
558         String JavaDoc comment = lang.getLanguageValue("task.dialog.forward.logmessage");
559         comment += " " + CmsUser.getFullName(newEditor);
560         taskService.writeTaskLog(taskid, comment, CmsWorkplaceDefault.C_TASKLOGTYPE_FORWARDED);
561
562         // send an email if "Benachrichtigung bei Weiterleitung" was selected.
563
CmsTask task = taskService.readTask(taskid);
564         if (taskService.getTaskPar(task.getId(), CmsWorkplaceDefault.C_TASKPARA_DELIVERY) != null) {
565             StringBuffer JavaDoc contentBuf = new StringBuffer JavaDoc(lang.getLanguageValue("task.email.forward.content"));
566             contentBuf.append("\n");
567             contentBuf.append(lang.getLanguageValue("task.label.project"));
568             contentBuf.append(": ");
569             String JavaDoc projectname = "?";
570             try {
571                 projectname = taskService.readTask(task.getRoot()).getName();
572             } catch (Exception JavaDoc exc) {
573
574                 // no root?!
575
}
576             contentBuf.append(projectname);
577             contentBuf.append("\n");
578             contentBuf.append(lang.getLanguageValue("task.label.task"));
579             contentBuf.append(": ");
580             contentBuf.append(task.getName());
581             contentBuf.append("\n");
582             contentBuf.append(lang.getLanguageValue("task.label.actuator"));
583             contentBuf.append(": ");
584             contentBuf.append(CmsUser.getFullName(taskService.readOwner(task)));
585             contentBuf.append("\n");
586             contentBuf.append(lang.getLanguageValue("task.label.taskfor"));
587             contentBuf.append(": ");
588             contentBuf.append(CmsUser.getFullName(taskService.readOriginalAgent(task)));
589             contentBuf.append("\n");
590             contentBuf.append(lang.getLanguageValue("task.label.editor"));
591             contentBuf.append(": ");
592             contentBuf.append(CmsUser.getFullName(taskService.readAgent(task)));
593             int projectid = taskService.readProject(task).getId();
594             contentBuf.append("\n\n\n" + getTaskUrl(cms, task.getId(), projectid));
595             String JavaDoc subject = lang.getLanguageValue("task.email.forward.subject")
596                 + " "
597                 + CmsUser.getFullName(cms.readUser(task.getAgentUser()))
598                 + " / "
599                 + newRoleName;
600
601             // if "Alle Rollenmitglieder von Aufgabe Benachrichtigen" checkbox is selected.
602
String JavaDoc sendToAll = taskService.getTaskPar(task.getId(), CmsWorkplaceDefault.C_TASKPARA_ALL);
603             if (!CmsStringUtil.isEmpty(sendToAll)) {
604                 try {
605                     CmsGroup group = taskService.readGroup(task);
606                     List JavaDoc users = cms.getUsersOfGroup(group.getName());
607                     CmsSimpleMail mail = createMail(
608                         cms.getRequestContext().currentUser(),
609                         users,
610                         subject,
611                         contentBuf.toString(),
612                         true);
613                     new CmsMailTransport(mail).send();
614                 } catch (Exception JavaDoc exc) {
615                     if (CmsLog.getLog(CmsTaskAction.class).isWarnEnabled()) {
616                         CmsLog.getLog(CmsTaskAction.class).warn("Error while sending task mail", exc);
617                     }
618                 }
619             } else {
620
621                 // send a mail to user
622
CmsUser[] user = {taskService.readAgent(task)};
623                 try {
624                     CmsSimpleMail mail1 = createMail(
625                         cms.getRequestContext().currentUser(),
626                         user,
627                         subject,
628                         contentBuf.toString());
629                     new CmsMailTransport(mail1).send();
630                 } catch (Exception JavaDoc exc) {
631                     if (CmsLog.getLog(CmsTaskAction.class).isWarnEnabled()) {
632                         CmsLog.getLog(CmsTaskAction.class).warn("Error while sending task mail", exc);
633                     }
634                 }
635
636                 // send a mail to owner
637
CmsUser[] owner = {taskService.readOwner(task)};
638                 try {
639                     CmsSimpleMail mail2 = createMail(
640                         cms.getRequestContext().currentUser(),
641                         owner,
642                         subject,
643                         contentBuf.toString());
644                     new CmsMailTransport(mail2).send();
645                 } catch (Exception JavaDoc exc) {
646                     if (CmsLog.getLog(CmsTaskAction.class).isWarnEnabled()) {
647                         CmsLog.getLog(CmsTaskAction.class).warn("Error while sending task mail", exc);
648                     }
649                 }
650             }
651         }
652     }
653
654     /**
655      * Gets the description of a task.
656      * The description is stored in the task-log.
657      * @param cms The cms-object.
658      * @param int taskid The id of the task.
659      * @return String the comment-string.
660      * @throws CmsException Throws CmsExceptions, that are be
661      * thrown in calling methods.
662      */

663
664     public static String JavaDoc getDescription(CmsObject cms, int taskid) throws CmsException {
665
666         StringBuffer JavaDoc retValue = new StringBuffer JavaDoc("");
667         CmsTaskLog tasklog;
668         List JavaDoc taskdocs = cms.getTaskService().readTaskLogs(taskid);
669
670         // go through all tasklogs to find the comment
671
for (int i = 1; i <= taskdocs.size(); i++) {
672             tasklog = (CmsTaskLog)taskdocs.get(taskdocs.size() - i);
673             int type = tasklog.getType();
674
675             // check if this is a type "created" or "new"
676
if ((type == CmsWorkplaceDefault.C_TASKLOGTYPE_CREATED)
677                 || (type == CmsWorkplaceDefault.C_TASKLOGTYPE_REACTIVATED)) {
678                 String JavaDoc comment[] = CmsStringUtil.splitAsArray(tasklog.getComment(), '\n');
679                 for (int j = 2; j < comment.length; j++) {
680                     retValue.append(comment[j] + "\n");
681                 }
682                 break;
683             }
684         }
685         return retValue.toString();
686     }
687
688     public static String JavaDoc getTaskUrl(CmsObject cms, int taskid, int projectid) {
689
690         String JavaDoc frameLink = OpenCms.getLinkManager().substituteLink(cms, CmsFrameset.JSP_WORKPLACE_URI);
691         String JavaDoc taskViewLink = OpenCms.getLinkManager().substituteLink(
692             cms,
693             CmsWorkplaceAction.PATH_XML_WORKPLACE + "tasks.html")
694             + "%3Ftaskid%3D"
695             + taskid;
696
697         return OpenCms.getSiteManager().getWorkplaceSiteMatcher().toString()
698             + frameLink
699             + "?"
700             + CmsFrameset.PARAM_WP_VIEW
701             + "="
702             + taskViewLink;
703     }
704
705     /**
706      * Sends a message to the editor of the task.
707      * @param cms The cms-object.
708      * @param int taskid The id of the task.
709      * @param message The text of the message.
710      * @throws CmsException Throws CmsExceptions, that are be
711      * thrown in calling methods.
712      */

713
714     public static void message(CmsObject cms, int taskid, String JavaDoc message) throws CmsException {
715
716         CmsXmlLanguageFile lang = new CmsXmlLanguageFile(cms);
717         CmsTaskService taskService = cms.getTaskService();
718         CmsTask task = taskService.readTask(taskid);
719         String JavaDoc comment = lang.getLanguageValue("task.dialog.message.head") + " ";
720         if ((message != null) && (message.length() != 0)) {
721             comment += CmsUser.getFullName(taskService.readAgent(task)) + "\n";
722             comment += message;
723             taskService.writeTaskLog(taskid, comment, CmsWorkplaceDefault.C_TASKLOGTYPE_CALL);
724         }
725
726         // send an email
727
StringBuffer JavaDoc contentBuf = new StringBuffer JavaDoc(lang.getLanguageValue("task.email.message.content"));
728         contentBuf.append("\n");
729         contentBuf.append(lang.getLanguageValue("task.label.project"));
730         contentBuf.append(": ");
731         String JavaDoc projectname = "?";
732         try {
733             projectname = taskService.readTask(task.getRoot()).getName();
734         } catch (Exception JavaDoc exc) {
735
736             // no root?!
737
}
738         contentBuf.append(projectname);
739         contentBuf.append("\n");
740         contentBuf.append(lang.getLanguageValue("task.label.task"));
741         contentBuf.append(": ");
742         contentBuf.append(task.getName());
743         contentBuf.append("\n");
744         contentBuf.append(lang.getLanguageValue("task.label.actuator"));
745         contentBuf.append(": ");
746         contentBuf.append(CmsUser.getFullName(taskService.readOwner(task)));
747         int projectid = taskService.readProject(task).getId();
748         contentBuf.append("\n\n\n" + getTaskUrl(cms, task.getId(), projectid));
749
750         contentBuf.append(addTaskText(task, taskService));
751
752         String JavaDoc subject = lang.getLanguageValue("task.email.message.subject")
753             + " "
754             + CmsUser.getFullName(taskService.readOwner(task));
755         CmsUser[] users = {taskService.readAgent(task)};
756         try {
757             CmsSimpleMail mail = createMail(taskService.readOwner(task), users, subject, contentBuf.toString());
758             new CmsMailTransport(mail).send();
759         } catch (Exception JavaDoc exc) {
760             if (CmsLog.getLog(CmsTaskAction.class).isWarnEnabled()) {
761                 CmsLog.getLog(CmsTaskAction.class).warn("Error while sending task mail", exc);
762             }
763         }
764     }
765
766     /**
767      * Changes the priority of a task.
768      * @param cms The cms-object.
769      * @param int taskid The id of the task.
770      * @param priorityString the new priority as String ("1" = high,
771      * "2" = normal or "3" = low)
772      * @throws CmsException Throws CmsExceptions, that are be
773      * thrown in calling methods.
774      */

775
776     public static void priority(CmsObject cms, int taskid, String JavaDoc priorityString) throws CmsException {
777
778         CmsXmlLanguageFile lang = new CmsXmlLanguageFile(cms);
779         CmsTaskService taskService = cms.getTaskService();
780         CmsTask task = taskService.readTask(taskid);
781         int priority = Integer.parseInt(priorityString);
782         taskService.setPriority(taskid, priority);
783
784         // add comment
785
String JavaDoc comment = "";
786         comment += lang.getLanguageValue("task.dialog.priority.logmessage1") + " ";
787         comment += lang.getLanguageValue("task.dialog.priority.logmessageprio" + task.getPriority()) + " ";
788         comment += lang.getLanguageValue("task.dialog.priority.logmessage2") + " ";
789         comment += lang.getLanguageValue("task.dialog.priority.logmessageprio" + priority) + " ";
790         taskService.writeTaskLog(taskid, comment, CmsWorkplaceDefault.C_TASKLOGTYPE_PRIORITYCHANGED);
791     }
792
793     /**
794      * Sends a message to the initiator (owner) of the task.
795      * @param cms The cms-object.
796      * @param int taskid The id of the task.
797      * @param message The text of the message.
798      * @throws CmsException Throws CmsExceptions, that are be
799      * thrown in calling methods.
800      */

801
802     public static void query(CmsObject cms, int taskid, String JavaDoc message) throws CmsException {
803
804         CmsXmlLanguageFile lang = new CmsXmlLanguageFile(cms);
805         CmsTaskService taskService = cms.getTaskService();
806         CmsTask task = taskService.readTask(taskid);
807         String JavaDoc comment = lang.getLanguageValue("task.dialog.query.head") + " ";
808         if ((message != null) && (message.length() != 0)) {
809             comment += CmsUser.getFullName(taskService.readOwner(task)) + "\n";
810             comment += message;
811             taskService.writeTaskLog(taskid, comment, CmsWorkplaceDefault.C_TASKLOGTYPE_CALL);
812         }
813
814         // send an email.
815
StringBuffer JavaDoc contentBuf = new StringBuffer JavaDoc(lang.getLanguageValue("task.email.query.content"));
816         contentBuf.append("\n");
817         contentBuf.append(lang.getLanguageValue("task.label.project"));
818         contentBuf.append(": ");
819         String JavaDoc projectname = "?";
820         try {
821             projectname = taskService.readTask(task.getRoot()).getName();
822         } catch (Exception JavaDoc exc) {
823
824             // no root?!
825
}
826         contentBuf.append(projectname);
827         contentBuf.append("\n");
828         contentBuf.append(lang.getLanguageValue("task.label.task"));
829         contentBuf.append(": ");
830         contentBuf.append(task.getName());
831         contentBuf.append("\n");
832         contentBuf.append(lang.getLanguageValue("task.label.editor"));
833         contentBuf.append(": ");
834         contentBuf.append(CmsUser.getFullName(taskService.readAgent(task)));
835         int projectid = taskService.readProject(task).getId();
836         contentBuf.append("\n\n\n" + getTaskUrl(cms, task.getId(), projectid));
837
838         contentBuf.append(addTaskText(task, taskService));
839
840         String JavaDoc subject = lang.getLanguageValue("task.email.query.subject")
841             + " "
842             + CmsUser.getFullName(taskService.readAgent(task));
843         CmsUser[] users = {taskService.readOwner(task)};
844         try {
845             CmsSimpleMail mail = createMail(taskService.readAgent(task), users, subject, contentBuf.toString());
846             new CmsMailTransport(mail).send();
847         } catch (Exception JavaDoc exc) {
848             if (CmsLog.getLog(CmsTaskAction.class).isWarnEnabled()) {
849                 CmsLog.getLog(CmsTaskAction.class).warn("Error while sending task mail", exc);
850             }
851         }
852     }
853
854     /**
855      * Reaktivates a task.
856      * @param cms The cms-object.
857      * @param int taskid The id of the task.
858      * @param agentName The name of the new editor for this task.
859      * @param roleName The name of the new role for the user.
860      * @param taskName The new name of the task.
861      * @param taskcomment The new comment for this task.
862      * @param timeoutString The new timeout-date as a string in the following format:
863      * "dd.mm.yyyy"
864      * @param priorityString the new priority as String ("1" = high,
865      * "2" = normal or "3" = low)
866      * @param paraAcceptation controls if a message should be send by acceptation. ("checked" | "")
867      * @param paraAll controls if a message should be send to all users in a role. ("checked" | "")
868      * @param paraCompletion controls if a message should be send by completing this task. ("checked" | "")
869      * @param paraDelivery controls if a message should be send by delivering a task. ("checked" | "")
870      * @throws CmsException Throws CmsExceptions, that are be
871      * thrown in calling methods.
872      */

873
874     public static void reakt(
875         CmsObject cms,
876         int taskid,
877         String JavaDoc agentName,
878         String JavaDoc roleName,
879         String JavaDoc taskName,
880         String JavaDoc taskcomment,
881         String JavaDoc timeoutString,
882         String JavaDoc priorityString,
883         String JavaDoc paraAcceptation,
884         String JavaDoc paraAll,
885         String JavaDoc paraCompletion,
886         String JavaDoc paraDelivery) throws CmsException {
887
888         CmsXmlLanguageFile lang = new CmsXmlLanguageFile(cms);
889         CmsTaskService taskService = cms.getTaskService();
890         CmsTask task = taskService.readTask(taskid);
891         // TODO: CW: check effects of this
892
//if(roleName.equals(C_ALL_ROLES)) {
893
// roleName = cms.readUser(agentName).getDefaultGroup().getName();
894
//}
895
taskService.setName(taskid, taskName);
896
897         // try to reaktivate the task
898
taskService.reactivateTask(taskid);
899         int priority = Integer.parseInt(priorityString);
900         taskService.setPriority(taskid, priority);
901
902         // create a long from the overgiven date.
903
String JavaDoc splittetDate[] = CmsStringUtil.splitAsArray(timeoutString, '.');
904         GregorianCalendar JavaDoc cal = new GregorianCalendar JavaDoc(
905             Integer.parseInt(splittetDate[2]),
906             Integer.parseInt(splittetDate[1]) - 1,
907             Integer.parseInt(splittetDate[0]),
908             0,
909             0,
910             0);
911         long timeout = cal.getTime().getTime();
912         taskService.setTimeout(taskid, timeout);
913         taskService.setTaskPar(taskid, CmsWorkplaceDefault.C_TASKPARA_ACCEPTATION, paraAcceptation);
914         taskService.setTaskPar(taskid, CmsWorkplaceDefault.C_TASKPARA_ALL, paraAll);
915         taskService.setTaskPar(taskid, CmsWorkplaceDefault.C_TASKPARA_COMPLETION, paraCompletion);
916         taskService.setTaskPar(taskid, CmsWorkplaceDefault.C_TASKPARA_DELIVERY, paraDelivery);
917         taskService.forwardTask(taskid, roleName, agentName);
918         String JavaDoc comment = lang.getLanguageValue("task.label.forrole") + ": " + roleName + "\n";
919         comment += lang.getLanguageValue("task.label.editor")
920             + ": "
921             + CmsUser.getFullName(cms.readUser(agentName))
922             + "\n";
923         comment += taskcomment;
924         taskService.writeTaskLog(task.getId(), comment, CmsWorkplaceDefault.C_TASKLOGTYPE_REACTIVATED);
925
926         // send an email
927
StringBuffer JavaDoc contentBuf = new StringBuffer JavaDoc(lang.getLanguageValue("task.email.reakt.content"));
928         contentBuf.append("\n");
929         contentBuf.append(lang.getLanguageValue("task.label.project"));
930         contentBuf.append(": ");
931         String JavaDoc projectname = "?";
932         try {
933             projectname = taskService.readTask(task.getRoot()).getName();
934         } catch (Exception JavaDoc exc) {
935
936             // no root?!
937
}
938         contentBuf.append(projectname);
939         contentBuf.append("\n");
940         contentBuf.append(lang.getLanguageValue("task.label.task"));
941         contentBuf.append(": ");
942         contentBuf.append(task.getName());
943         int projectid = taskService.readProject(task).getId();
944         contentBuf.append("\n\n\n" + getTaskUrl(cms, task.getId(), projectid));
945         String JavaDoc subject = lang.getLanguageValue("task.email.reakt.subject")
946             + " "
947             + CmsUser.getFullName(cms.readUser(task.getAgentUser()))
948             + " / "
949             + roleName;
950         CmsUser[] users = {taskService.readAgent(task)};
951         CmsSimpleMail mail;
952         mail = createMail(taskService.readOwner(task), users, subject, contentBuf.toString());
953
954         String JavaDoc sendToAll = taskService.getTaskPar(task.getId(), CmsWorkplaceDefault.C_TASKPARA_ALL);
955         if (!CmsStringUtil.isEmpty(sendToAll)) {
956             CmsGroup group = taskService.readGroup(task);
957             List JavaDoc groupUsers = cms.getUsersOfGroup(group.getName());
958             mail = createMail(taskService.readOwner(task), groupUsers, subject, contentBuf.toString(), true);
959         }
960         try {
961             new CmsMailTransport(mail).send();
962         } catch (Exception JavaDoc exc) {
963             if (CmsLog.getLog(CmsTaskAction.class).isWarnEnabled()) {
964                 CmsLog.getLog(CmsTaskAction.class).warn("Error while sending task mail", exc);
965             }
966         }
967     }
968
969     /**
970      * Takes a task. The calling user is now the agent for this task.
971      * @param cms The cms-object.
972      * @param int taskid The id of the task.
973      * @throws CmsException Throws CmsExceptions, that are be
974      * thrown in calling methods.
975      */

976
977     public static void take(CmsObject cms, int taskid) throws CmsException {
978
979         CmsXmlLanguageFile lang = new CmsXmlLanguageFile(cms);
980         CmsRequestContext context = cms.getRequestContext();
981         CmsTaskService taskService = cms.getTaskService();
982         CmsTask task = taskService.readTask(taskid);
983         CmsUser newEditor = context.currentUser();
984         CmsGroup oldRole = taskService.readGroup(task);
985
986         // has the user the correct role?
987
if (cms.userInGroup(newEditor.getName(), oldRole.getName())) {
988             taskService.forwardTask(taskid, oldRole.getName(), newEditor.getName());
989             taskService.acceptTask(taskid);
990             String JavaDoc comment = lang.getLanguageValue("task.dialog.take.logmessage");
991             comment += " " + CmsUser.getFullName(newEditor);
992             taskService.writeTaskLog(taskid, comment, CmsWorkplaceDefault.C_TASKLOGTYPE_TAKE);
993         }
994
995         // send an email
996
StringBuffer JavaDoc contentBuf = new StringBuffer JavaDoc(lang.getLanguageValue("task.email.take.content"));
997         contentBuf.append("\n");
998         contentBuf.append(lang.getLanguageValue("task.label.project"));
999         contentBuf.append(": ");
1000        String JavaDoc projectname = "?";
1001        try {
1002            projectname = taskService.readTask(task.getRoot()).getName();
1003        } catch (Exception JavaDoc exc) {
1004
1005            // no root?!
1006
}
1007        contentBuf.append(projectname);
1008        contentBuf.append("\n");
1009        contentBuf.append(lang.getLanguageValue("task.label.task"));
1010        contentBuf.append(": ");
1011        contentBuf.append(task.getName());
1012        contentBuf.append("\n");
1013        contentBuf.append(lang.getLanguageValue("task.label.taskfor"));
1014        contentBuf.append(": ");
1015        contentBuf.append(CmsUser.getFullName(taskService.readAgent(task)));
1016        int projectid = taskService.readProject(task).getId();
1017        contentBuf.append("\n\n\n" + getTaskUrl(cms, task.getId(), projectid));
1018        String JavaDoc subject = lang.getLanguageValue("task.email.take.subject") + " " + CmsUser.getFullName(newEditor);
1019        CmsUser[] users = {taskService.readAgent(task)};
1020        try {
1021            CmsSimpleMail mail = createMail(taskService.readOwner(task), users, subject, contentBuf.toString());
1022            new CmsMailTransport(mail).send();
1023        } catch (Exception JavaDoc exc) {
1024            if (CmsLog.getLog(CmsTaskAction.class).isWarnEnabled()) {
1025                CmsLog.getLog(CmsTaskAction.class).warn("Error while sending task mail", exc);
1026            }
1027        }
1028    }
1029
1030    /**
1031     * Adds the message text to the generated mail text.<p>
1032     * @param task the current task
1033     * @param taskService the task log containing the task message
1034     * @return StringBuffer containing the message texgt
1035     * @throws CmsException if something goes wrong
1036     */

1037    private static StringBuffer JavaDoc addTaskText(CmsTask task, CmsTaskService taskService) throws CmsException {
1038
1039        StringBuffer JavaDoc text = new StringBuffer JavaDoc();
1040        if (OpenCms.getWorkplaceManager().isEnableWorkflowMessages()) {
1041            text.append("\n\n\n");
1042            List JavaDoc taskLog = taskService.readTaskLogs(task.getId());
1043            if (taskLog != null && taskLog.size() > 0) {
1044                CmsTaskLog log = (CmsTaskLog)taskLog.get(taskLog.size() - 1);
1045                if (log != null) {
1046                    text.append(log.getComment());
1047                }
1048            }
1049        }
1050        return text;
1051    }
1052}
1053
Popular Tags