<commit_before>/*
* File   : $Source: /alkacon/cvs/opencms/src-modules/com/opencms/workplace/Attic/CmsTaskAction.java,v $
* Date   : $Date: 2005/06/27 23:22:07 $
* Version: $Revision: 1.5 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (C) 2001  The OpenCms Group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* For further information about OpenCms, please see the
* OpenCms Website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/


package com.opencms.workplace;

import org.opencms.file.CmsGroup;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsRequestContext;
import org.opencms.file.CmsUser;
import org.opencms.mail.CmsMailTransport;
import org.opencms.mail.CmsSimpleMail;
import org.opencms.main.CmsException;
import org.opencms.main.CmsLog;
import org.opencms.main.OpenCms;
import org.opencms.util.CmsDateUtil;
import org.opencms.util.CmsStringUtil;
import org.opencms.workflow.CmsTask;
import org.opencms.workflow.CmsTaskLog;
import org.opencms.workflow.CmsTaskService;
import org.opencms.workplace.CmsFrameset;

import com.opencms.legacy.CmsLegacyException;

import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Vector;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

/**
 * This helper-class is used to do task-actions like create or forward. It uses the
 * workplace-languagefile to add task-logs in the language of the user.
 * <P>
 *
 * @author <NAME>
 * @version $Revision: 1.5 $ $Date: 2005/06/27 23:22:07 $
 * @see com.opencms.workplace.CmsXmlWpTemplateFile
 * 
 * @deprecated Will not be supported past the OpenCms 6 release.
 */

public class CmsTaskAction {


    /**
     * Constant for generating user javascriptlist
     */
    private final static String C_ALL_ROLES = "___all";

    /**
     * Accepts a task.
     * @param cms The cms-object.
     * @param int taskid The id of the task.
     * @throws CmsException Throws CmsExceptions, that are be
     * thrown in calling methods.
     */

    public static void accept(CmsObject cms, int taskid) throws CmsException {
        CmsTaskService taskService = cms.getTaskService();
        taskService.acceptTask(taskid);
        String comment = "";
        taskService.writeTaskLog(taskid, comment, CmsWorkplaceDefault.C_TASKLOGTYPE_ACCEPTED);

        // send an email if "Benachrichtigung bei Annahme" was selected.
        CmsXmlLanguageFile lang = new CmsXmlLanguageFile(cms);
        CmsTask task = taskService.readTask(taskid);
        if(taskService.getTaskPar(task.getId(), CmsWorkplaceDefault.C_TASKPARA_ACCEPTATION) != null) {
            StringBuffer contentBuf = new StringBuffer(lang.getLanguageValue("task.email.accept.content"));
            contentBuf.append("\n");
            contentBuf.append(lang.getLanguageValue("task.label.project"));
            contentBuf.append(": ");
            String projectname = "?";
            try {
                projectname = taskService.readTask(task.getRoot()).getName();
            }
            catch(Exception exc) {


            // no root?!
            }
            contentBuf.append(projectname);
            contentBuf.append("\n");
            contentBuf.append(lang.getLanguageValue("task.label.task"));
            contentBuf.append(": ");
            contentBuf.append(task.getName());
            int projectid = taskService.readProject(task).getId();
            contentBuf.append("\n\n\n" + getTaskUrl(cms, taskid, projectid));
            String subject = lang.getLanguageValue("task.email.accept.subject") + " " + CmsUser.getFullName(taskService.readAgent(task));
            CmsUser[] users =  {
                taskService.readOwner(task)
            };
            try {
                CmsSimpleMail mail = createMail(taskService.readAgent(task),
                        users, subject, contentBuf.toString());
                new CmsMailTransport(mail).send();
            }
            catch(Exception exc) {
                if(CmsLog.getLog(CmsTaskAction.class).isWarnEnabled()) {
                    CmsLog.getLog(CmsTaskAction.class).warn("Error while sending task mail", exc);
                }
            }
        }
    }

    /**
     * Creates a new task.
     * @param cms The cms-object.
     * @param agentName The name of the new editor for this task.
     * @param roleName The name of the new role for the user.
     * @param taskName The new name of the task.
     * @param taskcomment The new comment for this task.
     * @param timeoutString The new timeout-date as a string in the following format:
     * "dd.mm.yyyy"
     * @param priorityString the new priority as String ("1" = high,
     * "2" = normal or "3" = low)
     * @param paraAcceptation controls if a message should be send by acceptation. ("checked" | "")
     * @param paraAll controls if a message should be send to all users in a role. ("checked" | "")
     * @param paraCompletion controls if a message should be send by completing this task. ("checked" | "")
     * @param paraDelivery controls if a message should be send by delivering a task. ("checked" | "")
     * @throws CmsException Throws CmsExceptions, that are be
     * thrown in calling methods.
     */

    public static void create(CmsObject cms, String agentName, String roleName, String taskName,
            String taskcomment, String timeoutString, String priorityString, String paraAcceptation,
            String paraAll, String paraCompletion, String paraDelivery) throws CmsException {
        CmsXmlLanguageFile lang = new CmsXmlLanguageFile(cms);
        // TODO: CW: check effects of this
        // if(roleName.equals(C_ALL_ROLES)) {
        //     roleName = cms.readUser(agentName).getDefaultGroup().getName();
        //}

        // try to create the task
        int priority = Integer.parseInt(priorityString);

        // create a long from the overgiven date.
        String splittetDate[] = CmsStringUtil.splitAsArray(timeoutString, '.');
        GregorianCalendar cal = new GregorianCalendar(Integer.parseInt(splittetDate[2]),
                Integer.parseInt(splittetDate[1]) - 1, Integer.parseInt(splittetDate[0]), 0, 0, 0);
        long timeout = cal.getTime().getTime();
        CmsTaskService taskService = cms.getTaskService();
        CmsTask task = taskService.createTask(agentName, roleName, taskName, timeout, priority);
        taskService.setTaskPar(task.getId(), CmsWorkplaceDefault.C_TASKPARA_ACCEPTATION, paraAcceptation);
        taskService.setTaskPar(task.getId(), CmsWorkplaceDefault.C_TASKPARA_ALL, paraAll);
        taskService.setTaskPar(task.getId(), CmsWorkplaceDefault.C_TASKPARA_COMPLETION, paraCompletion);
        taskService.setTaskPar(task.getId(), CmsWorkplaceDefault.C_TASKPARA_DELIVERY, paraDelivery);
        String comment = lang.getLanguageValue("task.label.forrole") + ": " + roleName + "\n";
        comment += lang.getLanguageValue("task.label.editor") + ": " + CmsUser.getFullName(cms.readUser(task.getAgentUser())) + "\n";
        comment += taskcomment;
        taskService.writeTaskLog(task.getId(), comment, CmsWorkplaceDefault.C_TASKLOGTYPE_CREATED);

        // send an email
        // per default send a mail from task's organizer to task's recipient.
        StringBuffer contentBuf = new StringBuffer(lang.getLanguageValue("task.email.create.content"));
        contentBuf.append("\n");
        contentBuf.append(lang.getLanguageValue("task.label.project"));
        contentBuf.append(": ");
        String projectname = "?";
        try {
            projectname = taskService.readTask(task.getRoot()).getName();
        }
        catch(Exception exc) {


        // no root?!
        }
        contentBuf.append(projectname);
        contentBuf.append("\n");
        contentBuf.append(lang.getLanguageValue("task.label.task"));
        contentBuf.append(": ");
        contentBuf.append(task.getName());
        contentBuf.append("\n");
        contentBuf.append(lang.getLanguageValue("task.label.actuator"));
        contentBuf.append(": ");
        contentBuf.append(CmsUser.getFullName(taskService.readOwner(task)));
        int projectid = taskService.readProject(task).getId();
        contentBuf.append("\n\n\n" + getTaskUrl(cms, task.getId(), projectid));
        String subject = lang.getLanguageValue("task.email.create.subject") + " " + CmsUser.getFullName(cms.readUser(task.getAgentUser())) + " / " + roleName;
        CmsUser[] users =  {
            taskService.readAgent(task)
        };
        CmsSimpleMail mail = null;
        try {
            mail = createMail(taskService.readOwner(task), users, subject, contentBuf.toString());
        }
        catch(CmsException e) {
            if(CmsLog.getLog(CmsTaskAction.class).isWarnEnabled()) {
                CmsLog.getLog(CmsTaskAction.class).warn("Could not generate mail while creating task for "
                        + taskService.readOwner(task).getName(), e);
            }
        }

        // if "Alle Rollenmitglieder von Aufgabe Benachrichtigen" checkbox is selected.
        if(taskService.getTaskPar(task.getId(), CmsWorkplaceDefault.C_TASKPARA_ALL) != null) {

            // the news deliver always "checked" or ""
            if(taskService.getTaskPar(task.getId(), CmsWorkplaceDefault.C_TASKPARA_ALL).equals("checked")) {
                try {
                    CmsGroup group = taskService.readGroup(task);
                    List groupUser = cms.getUsersOfGroup(group.getName());
                    mail = createMail(taskService.readOwner(task), groupUser,
                            subject, contentBuf.toString(), true);
                }
                catch(CmsException e) {
                    if(CmsLog.getLog(CmsTaskAction.class).isWarnEnabled()) {
                        CmsLog.getLog(CmsTaskAction.class).warn("Could not generate mail while creating task for "
                                + taskService.readOwner(task).getName(), e);
                    }
                }
            }
        }
        if (mail != null) {
            new CmsMailTransport(mail).send();
        }
    }
    
    /**
     * Helper method to create an email object.<p>
     * 
     * @param from the mail sender
     * @param to the mail receivers
     * @param subject the mail subject
     * @param content the mail content
     * @return the mail object to send
     * @throws CmsException if creating the mail object fails
     */
    public static CmsSimpleMail createMail(CmsUser from, CmsUser[] to, String subject, String content) throws CmsException {
        Vector v = new Vector(to.length);
        for (int i = 0; i < to.length; i++) {
            if (to[i].getEmail() == null) {
                continue;
            }
            if (to[i].getEmail().equals("")) {
                continue;
            }
            if (to[i].getEmail().indexOf("@") == -1 || to[i].getEmail().indexOf(".") == -1) {
                throw new CmsLegacyException("[" + CmsTaskAction.class.getName() + "] " + "Error in sending email, Invalid recipient email address: " + to[i].getEmail(), CmsLegacyException.C_BAD_NAME);
            }
            try {
                v.addElement(new InternetAddress(to[i].getEmail()));
            } catch (AddressException e) {
                throw new CmsLegacyException("[" + CmsTaskAction.class.getName() + "] " + "Error in sending email, Invalid recipient email address: " + to[i].getEmail(), CmsLegacyException.C_BAD_NAME);
            }
        }
        
        if (v.size() == 0) {
            throw new CmsLegacyException("[" + CmsTaskAction.class.getName() + "] " + "Error in sending email,Unknown recipient email address.", CmsLegacyException.C_BAD_NAME);
        }
        return createMail(from, v, subject, content, false);
    }
    
    /**
     * Helper method to create an email object.<p>
     * @param from the mail sender
     * @param to the mail receivers
     * @param subject the mail subject
     * @param content the mail content
     * @param createAddresses if true, the Vector of receivers contains CmsUsers and they are transformed into Internetaddresses
     * 
     * @return the mail object to send
     * @throws CmsException if creating the mail object fails
     */
    public static CmsSimpleMail createMail(CmsUser from, List to, String subject, String content, boolean createAddresses) throws CmsException {
        CmsSimpleMail mail = new CmsSimpleMail();
        //      check sender email address
        String fromAddress = from.getEmail();
        if (fromAddress == null || fromAddress.equals("")) {
            fromAddress = OpenCms.getSystemInfo().getMailSettings().getMailFromDefault();
        }
        if (fromAddress == null || fromAddress.equals("")) {            
            throw new CmsLegacyException("[" + CmsTaskAction.class.getName() + "] " + "Error in sending email,Unknown sender email address.", CmsLegacyException.C_BAD_NAME);
        }
        if (fromAddress.indexOf("@") == -1 || fromAddress.indexOf(".") == -1) {
            throw new CmsLegacyException("[" + CmsTaskAction.class.getName() + "] " + "Error in sending email,Unknown sender email address: " + fromAddress, CmsLegacyException.C_BAD_NAME);
        }
        
        if (to.size() == 0) {
            throw new CmsLegacyException("[" + CmsTaskAction.class.getName() + "] " + "Error in sending email,Unknown recipient email address.", CmsLegacyException.C_BAD_NAME);
        } else if (createAddresses) {
            List receivers = new ArrayList(to.size());
            for (int i=0; i<to.size(); i++) {
                CmsUser rec = (CmsUser)to.get(i);
                try {
                    receivers.add(new InternetAddress(rec.getEmail()));
                } catch (AddressException e) {
                    throw new CmsLegacyException("[" + CmsTaskAction.class.getName() + "] " + "Error in sending email, invalid recipient email address.", CmsLegacyException.C_BAD_NAME);
                }
            }
            mail.setTo(receivers);
        } else {
            mail.setTo(to);    
        }
        try {
            mail.setFrom(fromAddress);
        } catch (MessagingException e) {
            throw new CmsLegacyException("[" + CmsTaskAction.class.getName() + "] " + "Error in sending email, invalid sender email address.", CmsLegacyException.C_BAD_NAME);
        }
        mail.setSubject(subject == null ? "" : subject);
        mail.setMsg(content == null ? "" : content);
        return mail;
    }

    /**
     * Changes the timeou-date of the task.
     * @param cms The cms-object.
     * @param int taskid The id of the task.
     * @param timeoutString The new timeout-date as a string in the following format:
     * "dd.mm.yyyy"
     * @throws CmsException Throws CmsExceptions, that are be
     * thrown in calling methods.
     */

    public static void due(CmsObject cms, int taskid, String timeoutString) throws CmsException {
        CmsXmlLanguageFile lang = new CmsXmlLanguageFile(cms);
        CmsTaskService taskService = cms.getTaskService();
        CmsTask task = taskService.readTask(taskid);
        String splittetDate[] = CmsStringUtil.splitAsArray(timeoutString, '.');
        GregorianCalendar cal = new GregorianCalendar(Integer.parseInt(splittetDate[2]),
                Integer.parseInt(splittetDate[1]) - 1, Integer.parseInt(splittetDate[0]), 0, 0, 0);
        long timeout = cal.getTime().getTime();
        taskService.setTimeout(taskid, timeout);

        // add comment
        String comment = "";
        comment += lang.getLanguageValue("task.dialog.due.logmessage1") + " ";
        comment += CmsDateUtil.getDateShort(task.getTimeOut().getTime()) + " ";
        comment += lang.getLanguageValue("task.dialog.due.logmessage2") + " ";
        comment += CmsDateUtil.getDateShort(timeout);
        taskService.writeTaskLog(taskid, comment, CmsWorkplaceDefault.C_TASKLOGTYPE_DUECHANGED);
    }

    /**
     * Ends a task.
     * @param cms The cms-object.
     * @param int taskid The id of the task.
     * @throws CmsException Throws CmsExceptions, that are be
     * thrown in calling methods.
     */

    public static void end(CmsObject cms, int taskid) throws CmsException {
        CmsTaskService taskService = cms.getTaskService();
        taskService.endTask(taskid);
        String comment = "";
        taskService.writeTaskLog(taskid, comment, CmsWorkplaceDefault.C_TASKLOGTYPE_OK);

        // send an email if "Benachrichtigung bei Abhacken" was selected.
        CmsXmlLanguageFile lang = new CmsXmlLanguageFile(cms);
        CmsTask task = taskService.readTask(taskid);
        if(taskService.getTaskPar(task.getId(), CmsWorkplaceDefault.C_TASKPARA_COMPLETION) != null) {
            StringBuffer contentBuf = new StringBuffer(lang.getLanguageValue("task.email.end.content"));
            contentBuf.append("\n");
            contentBuf.append(lang.getLanguageValue("task.label.project"));
            contentBuf.append(": ");
            String projectname = "?";
            try {
                projectname = taskService.readTask(task.getRoot()).getName();
            }
            catch(Exception exc) {


            // no root?!
            }
            contentBuf.append(projectname);
            contentBuf.append("\n");
            contentBuf.append(lang.getLanguageValue("task.label.task"));
            contentBuf.append(": ");
            contentBuf.append(task.getName());
            contentBuf.append("\n");
            contentBuf.append(lang.getLanguageValue("task.label.taskfor"));
            contentBuf.append(": ");
            contentBuf.append(CmsUser.getFullName(taskService.readOriginalAgent(task)));
            contentBuf.append("\n");
            contentBuf.append(lang.getLanguageValue("task.label.editor"));
            contentBuf.append(": ");
            contentBuf.append(CmsUser.getFullName(taskService.readAgent(task)));
            int projectid = taskService.readProject(task).getId();
            contentBuf.append("\n\n\n" + getTaskUrl(cms, task.getId(), projectid));
            String subject = lang.getLanguageValue("task.email.end.subject") + " " + CmsUser.getFullName(taskService.readAgent(task));
            CmsUser[] users =  {
                taskService.readOwner(task)
            };
            try {
                CmsSimpleMail mail = createMail(taskService.readAgent(task),
                        users, subject, contentBuf.toString());
                new CmsMailTransport(mail).send();
            }
            catch(Exception exc) {
                if(CmsLog.getLog(CmsTaskAction.class).isWarnEnabled()) {
                    CmsLog.getLog(CmsTaskAction.class).warn("Error while sending task mail", exc);
                }
            }
        }
    }

    /**
     * Forwards a task. The task is forwarded to a new editor in a new role.
     * @param cms The cms-object.
     * @param taskid The id of the task.
     * @param newEditorName The name of the new editor for this task.
     * @param newRoleName The name of the new role for the user.
     * @throws CmsException Throws CmsExceptions, that are be
     * thrown in calling methods.
     */

    public static void forward(CmsObject cms, int taskid, String newEditorName, String newRoleName) throws CmsException {
        CmsXmlLanguageFile lang = new CmsXmlLanguageFile(cms);
        CmsUser newEditor = cms.readUser(newEditorName);
        // TODO: CW: check effects of this
        // if(newRoleName.equals(C_ALL_ROLES)) {
        //    newRoleName = cms.readUser(newEditorName).getDefaultGroup().getName();
        // }
        CmsGroup oldRole = cms.readGroup(newRoleName);
        CmsTaskService taskService = cms.getTaskService();
        taskService.forwardTask(taskid, oldRole.getName(), newEditor.getName());
        String comment = lang.getLanguageValue("task.dialog.forward.logmessage");
        comment += " " + CmsUser.getFullName(newEditor);
        taskService.writeTaskLog(taskid, comment, CmsWorkplaceDefault.C_TASKLOGTYPE_FORWARDED);

        // send an email if "Benachrichtigung bei Weiterleitung" was selected.
        CmsTask task = taskService.readTask(taskid);
        if(taskService.getTaskPar(task.getId(), CmsWorkplaceDefault.C_TASKPARA_DELIVERY) != null) {
            StringBuffer contentBuf = new StringBuffer(lang.getLanguageValue("task.email.forward.content"));
            contentBuf.append("\n");
            contentBuf.append(lang.getLanguageValue("task.label.project"));
            contentBuf.append(": ");
            String projectname = "?";
            try {
                projectname = taskService.readTask(task.getRoot()).getName();
            }
            catch(Exception exc) {


            // no root?!
            }
            contentBuf.append(projectname);
            contentBuf.append("\n");
            contentBuf.append(lang.getLanguageValue("task.label.task"));
            contentBuf.append(": ");
            contentBuf.append(task.getName());
            contentBuf.append("\n");
            contentBuf.append(lang.getLanguageValue("task.label.actuator"));
            contentBuf.append(": ");
            contentBuf.append(CmsUser.getFullName(taskService.readOwner(task)));
            contentBuf.append("\n");
            contentBuf.append(lang.getLanguageValue("task.label.taskfor"));
            contentBuf.append(": ");
            contentBuf.append(CmsUser.getFullName(taskService.readOriginalAgent(task)));
            contentBuf.append("\n");
            contentBuf.append(lang.getLanguageValue("task.label.editor"));
            contentBuf.append(": ");
            contentBuf.append(CmsUser.getFullName(taskService.readAgent(task)));
            int projectid = taskService.readProject(task).getId();
            contentBuf.append("\n\n\n" + getTaskUrl(cms, task.getId(), projectid));
            String subject = lang.getLanguageValue("task.email.forward.subject") + " " + CmsUser.getFullName(cms.readUser(task.getAgentUser())) + " / " + newRoleName;

            // if "Alle Rollenmitglieder von Aufgabe Benachrichtigen" checkbox is selected.
            String sendToAll = taskService.getTaskPar(task.getId(), CmsWorkplaceDefault.C_TASKPARA_ALL);
            if(CmsStringUtil.isEmpty(sendToAll)) {
                try {
                    CmsGroup group = taskService.readGroup(task);
                    List users = cms.getUsersOfGroup(group.getName());
                    CmsSimpleMail mail = createMail(cms.getRequestContext().currentUser(),
                        users, subject, contentBuf.toString(), true);
                    new CmsMailTransport(mail).send();
                }
                catch(Exception exc) {
                    if(CmsLog.getLog(CmsTaskAction.class).isWarnEnabled()) {
                        CmsLog.getLog(CmsTaskAction.class).warn("Error while sending task mail", exc);
                    }
                }
            }
            else {

                // send a mail to user
                CmsUser[] user =  {
                    taskService.readAgent(task)
                };
                try {
                    CmsSimpleMail mail1 = createMail(cms.getRequestContext().currentUser(), 
                            user, subject, contentBuf.toString());
                    new CmsMailTransport(mail1).send();
                }
                catch(Exception exc) {
                    if(CmsLog.getLog(CmsTaskAction.class).isWarnEnabled()) {
                        CmsLog.getLog(CmsTaskAction.class).warn("Error while sending task mail", exc);
                    }
                }

                // send a mail to owner
                CmsUser[] owner =  {
                    taskService.readOwner(task)
                };
                try {
                    CmsSimpleMail mail2 = createMail(cms.getRequestContext().currentUser(),
                            owner, subject, contentBuf.toString());
                    new CmsMailTransport(mail2).send();
                }
                catch(Exception exc) {
                    if(CmsLog.getLog(CmsTaskAction.class).isWarnEnabled()) {
                        CmsLog.getLog(CmsTaskAction.class).warn("Error while sending task mail", exc);
                    }
                }
            }
        }
    }

    /**
     * Gets the description of a task.
     * The description is stored in the task-log.
     * @param cms The cms-object.
     * @param int taskid The id of the task.
     * @return String the comment-string.
     * @throws CmsException Throws CmsExceptions, that are be
     * thrown in calling methods.
     */

    public static String getDescription(CmsObject cms, int taskid) throws CmsException {
        StringBuffer retValue = new StringBuffer("");
        CmsTaskLog tasklog;
        List taskdocs = cms.getTaskService().readTaskLogs(taskid);

        // go through all tasklogs to find the comment
        for(int i = 1;i <= taskdocs.size();i++) {
            tasklog = (CmsTaskLog)taskdocs.get(taskdocs.size() - i);
            int type = tasklog.getType();

            // check if this is a type "created" or "new"
            if((type == CmsWorkplaceDefault.C_TASKLOGTYPE_CREATED) || (type == CmsWorkplaceDefault.C_TASKLOGTYPE_REACTIVATED)) {
                String comment[] = CmsStringUtil.splitAsArray(tasklog.getComment(), '\n');
                for(int j = 2;j < comment.length;j++) {
                    retValue.append(comment[j] + "\n");
                }
                break;
            }
        }
        return retValue.toString();
    }

    /**
     * Sends a message to the editor of the task.
     * @param cms The cms-object.
     * @param int taskid The id of the task.
     * @param message The text of the message.
     * @throws CmsException Throws CmsExceptions, that are be
     * thrown in calling methods.
     */

    public static void message(CmsObject cms, int taskid, String message) throws CmsException {
        CmsXmlLanguageFile lang = new CmsXmlLanguageFile(cms);
        CmsTaskService taskService = cms.getTaskService();
        CmsTask task = taskService.readTask(taskid);
        String comment = lang.getLanguageValue("task.dialog.message.head") + " ";
        if((message != null) && (message.length() != 0)) {
            comment += CmsUser.getFullName(taskService.readAgent(task)) + "\n";
            comment += message;
            taskService.writeTaskLog(taskid, comment, CmsWorkplaceDefault.C_TASKLOGTYPE_CALL);
        }

        // send an email
        StringBuffer contentBuf = new StringBuffer(lang.getLanguageValue("task.email.message.content"));
        contentBuf.append("\n");
        contentBuf.append(lang.getLanguageValue("task.label.project"));
        contentBuf.append(": ");
        String projectname = "?";
        try {
            projectname = taskService.readTask(task.getRoot()).getName();
        }
        catch(Exception exc) {


        // no root?!
        }
        contentBuf.append(projectname);
        contentBuf.append("\n");
        contentBuf.append(lang.getLanguageValue("task.label.task"));
        contentBuf.append(": ");
        contentBuf.append(task.getName());
        contentBuf.append("\n");
        contentBuf.append(lang.getLanguageValue("task.label.actuator"));
        contentBuf.append(": ");
        contentBuf.append(CmsUser.getFullName(taskService.readOwner(task)));
        int projectid = taskService.readProject(task).getId();
        contentBuf.append("\n\n\n" + getTaskUrl(cms, task.getId(), projectid));
        String subject = lang.getLanguageValue("task.email.message.subject") + " " + CmsUser.getFullName(taskService.readOwner(task));
        CmsUser[] users =  {
            taskService.readAgent(task)
        };
        try {
            CmsSimpleMail mail = createMail(taskService.readOwner(task), users, subject, contentBuf.toString());
            new CmsMailTransport(mail).send();
        }
        catch(Exception exc) {
            if(CmsLog.getLog(CmsTaskAction.class).isWarnEnabled()) {
                CmsLog.getLog(CmsTaskAction.class).warn("Error while sending task mail", exc);
            }
        }
    }

    /**
     * Changes the priority of a task.
     * @param cms The cms-object.
     * @param int taskid The id of the task.
     * @param priorityString the new priority as String ("1" = high,
     * "2" = normal or "3" = low)
     * @throws CmsException Throws CmsExceptions, that are be
     * thrown in calling methods.
     */

    public static void priority(CmsObject cms, int taskid, String priorityString) throws CmsException {
        CmsXmlLanguageFile lang = new CmsXmlLanguageFile(cms);
        CmsTaskService taskService = cms.getTaskService();
        CmsTask task = taskService.readTask(taskid);
        int priority = Integer.parseInt(priorityString);
        taskService.setPriority(taskid, priority);

        // add comment
        String comment = "";
        comment += lang.getLanguageValue("task.dialog.priority.logmessage1") + " ";
        comment += lang.getLanguageValue("task.dialog.priority.logmessageprio" + task.getPriority()) + " ";
        comment += lang.getLanguageValue("task.dialog.priority.logmessage2") + " ";
        comment += lang.getLanguageValue("task.dialog.priority.logmessageprio" + priority) + " ";
        taskService.writeTaskLog(taskid, comment, CmsWorkplaceDefault.C_TASKLOGTYPE_PRIORITYCHANGED);
    }

    /**
     * Sends a message to the initiator (owner) of the task.
     * @param cms The cms-object.
     * @param int taskid The id of the task.
     * @param message The text of the message.
     * @throws CmsException Throws CmsExceptions, that are be
     * thrown in calling methods.
     */

    public static void query(CmsObject cms, int taskid, String message) throws CmsException {
        CmsXmlLanguageFile lang = new CmsXmlLanguageFile(cms);
        CmsTaskService taskService = cms.getTaskService();
        CmsTask task = taskService.readTask(taskid);
        String comment = lang.getLanguageValue("task.dialog.query.head") + " ";
        if((message != null) && (message.length() != 0)) {
            comment += CmsUser.getFullName(taskService.readOwner(task)) + "\n";
            comment += message;
            taskService.writeTaskLog(taskid, comment, CmsWorkplaceDefault.C_TASKLOGTYPE_CALL);
        }

        // send an email.
        StringBuffer contentBuf = new StringBuffer(lang.getLanguageValue("task.email.query.content"));
        contentBuf.append("\n");
        contentBuf.append(lang.getLanguageValue("task.label.project"));
        contentBuf.append(": ");
        String projectname = "?";
        try {
            projectname = taskService.readTask(task.getRoot()).getName();
        }
        catch(Exception exc) {


        // no root?!
        }
        contentBuf.append(projectname);
        contentBuf.append("\n");
        contentBuf.append(lang.getLanguageValue("task.label.task"));
        contentBuf.append(": ");
        contentBuf.append(task.getName());
        contentBuf.append("\n");
        contentBuf.append(lang.getLanguageValue("task.label.editor"));
        contentBuf.append(": ");
        contentBuf.append(CmsUser.getFullName(taskService.readAgent(task)));
        int projectid = taskService.readProject(task).getId();
        contentBuf.append("\n\n\n" + getTaskUrl(cms, task.getId(), projectid));
        String subject = lang.getLanguageValue("task.email.query.subject") + " " + CmsUser.getFullName(taskService.readAgent(task));
        CmsUser[] users =  {
            taskService.readOwner(task)
        };
        try {
            CmsSimpleMail mail = createMail(taskService.readAgent(task),
                    users, subject, contentBuf.toString());
            new CmsMailTransport(mail).send();
        }
        catch(Exception exc) {
            if(CmsLog.getLog(CmsTaskAction.class).isWarnEnabled()) {
                CmsLog.getLog(CmsTaskAction.class).warn("Error while sending task mail", exc);
            }
        }
    }

    /**
     * Reaktivates a task.
     * @param cms The cms-object.
     * @param int taskid The id of the task.
     * @param agentName The name of the new editor for this task.
     * @param roleName The name of the new role for the user.
     * @param taskName The new name of the task.
     * @param taskcomment The new comment for this task.
     * @param timeoutString The new timeout-date as a string in the following format:
     * "dd.mm.yyyy"
     * @param priorityString the new priority as String ("1" = high,
     * "2" = normal or "3" = low)
     * @param paraAcceptation controls if a message should be send by acceptation. ("checked" | "")
     * @param paraAll controls if a message should be send to all users in a role. ("checked" | "")
     * @param paraCompletion controls if a message should be send by completing this task. ("checked" | "")
     * @param paraDelivery controls if a message should be send by delivering a task. ("checked" | "")
     * @throws CmsException Throws CmsExceptions, that are be
     * thrown in calling methods.
     */

    public static void reakt(CmsObject cms, int taskid, String agentName, String roleName, String taskName,
            String taskcomment, String timeoutString, String priorityString,
            String paraAcceptation, String paraAll, String paraCompletion, String paraDelivery) throws CmsException {
        CmsXmlLanguageFile lang = new CmsXmlLanguageFile(cms);
        CmsTaskService taskService = cms.getTaskService();
        CmsTask task = taskService.readTask(taskid);
        // TODO: CW: check effects of this
        //if(roleName.equals(C_ALL_ROLES)) {
        //    roleName = cms.readUser(agentName).getDefaultGroup().getName();
        //}
        taskService.setName(taskid, taskName);

        // try to reaktivate the task
        taskService.reactivateTask(taskid);
        int priority = Integer.parseInt(priorityString);
        taskService.setPriority(taskid, priority);

        // create a long from the overgiven date.
        String splittetDate[] = CmsStringUtil.splitAsArray(timeoutString, '.');
        GregorianCalendar cal = new GregorianCalendar(Integer.parseInt(splittetDate[2]), Integer.parseInt(splittetDate[1]) - 1,
                Integer.parseInt(splittetDate[0]), 0, 0, 0);
        long timeout = cal.getTime().getTime();
        taskService.setTimeout(taskid, timeout);
        taskService.setTaskPar(taskid, CmsWorkplaceDefault.C_TASKPARA_ACCEPTATION, paraAcceptation);
        taskService.setTaskPar(taskid, CmsWorkplaceDefault.C_TASKPARA_ALL, paraAll);
        taskService.setTaskPar(taskid, CmsWorkplaceDefault.C_TASKPARA_COMPLETION, paraCompletion);
        taskService.setTaskPar(taskid, CmsWorkplaceDefault.C_TASKPARA_DELIVERY, paraDelivery);
        taskService.forwardTask(taskid, roleName, agentName);
        String comment = lang.getLanguageValue("task.label.forrole") + ": " + roleName + "\n";
        comment += lang.getLanguageValue("task.label.editor") + ": " + CmsUser.getFullName(cms.readUser(agentName)) + "\n";
        comment += taskcomment;
        taskService.writeTaskLog(task.getId(), comment, CmsWorkplaceDefault.C_TASKLOGTYPE_REACTIVATED);

        // send an email
        StringBuffer contentBuf = new StringBuffer(lang.getLanguageValue("task.email.reakt.content"));
        contentBuf.append("\n");
        contentBuf.append(lang.getLanguageValue("task.label.project"));
        contentBuf.append(": ");
        String projectname = "?";
        try {
            projectname = taskService.readTask(task.getRoot()).getName();
        }
        catch(Exception exc) {


        // no root?!
        }
        contentBuf.append(projectname);
        contentBuf.append("\n");
        contentBuf.append(lang.getLanguageValue("task.label.task"));
        contentBuf.append(": ");
        contentBuf.append(task.getName());
        int projectid = taskService.readProject(task).getId();
        contentBuf.append("\n\n\n" + getTaskUrl(cms, task.getId(), projectid));
        String subject = lang.getLanguageValue("task.email.reakt.subject") + " " + CmsUser.getFullName(cms.readUser(task.getAgentUser())) + " / " + roleName;
        CmsUser[] users =  {
            taskService.readAgent(task)
        };
        CmsSimpleMail mail;
        mail = createMail(taskService.readOwner(task), users, subject, contentBuf.toString());

        // if "Alle Rollenmitglieder von Aufgabe Benachrichtigen" checkbox is selected.
        if(taskService.getTaskPar(task.getId(), CmsWorkplaceDefault.C_TASKPARA_ALL) != null) {
            CmsGroup group = taskService.readGroup(task);
            List groupUsers = cms.getUsersOfGroup(group.getName());
            mail = createMail(taskService.readOwner(task), groupUsers,
                    subject, contentBuf.toString(), true);
        }
        try {
            new CmsMailTransport(mail).send();
        }
        catch(Exception exc) {
            if(CmsLog.getLog(CmsTaskAction.class).isWarnEnabled()) {
                CmsLog.getLog(CmsTaskAction.class).warn("Error while sending task mail", exc);
            }
        }
    }<commit_msg>Fixed workflow bug, the checkbox "inform all role members" should now work as planned<commit_after>/*
* File   : $Source: /alkacon/cvs/opencms/src-modules/com/opencms/workplace/Attic/CmsTaskAction.java,v $
* Date   : $Date: 2005/07/18 10:10:35 $
* Version: $Revision: 1.6 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (C) 2001  The OpenCms Group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* For further information about OpenCms, please see the
* OpenCms Website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/


package com.opencms.workplace;

import org.opencms.file.CmsGroup;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsRequestContext;
import org.opencms.file.CmsUser;
import org.opencms.mail.CmsMailTransport;
import org.opencms.mail.CmsSimpleMail;
import org.opencms.main.CmsException;
import org.opencms.main.CmsLog;
import org.opencms.main.OpenCms;
import org.opencms.util.CmsDateUtil;
import org.opencms.util.CmsStringUtil;
import org.opencms.workflow.CmsTask;
import org.opencms.workflow.CmsTaskLog;
import org.opencms.workflow.CmsTaskService;
import org.opencms.workplace.CmsFrameset;

import com.opencms.legacy.CmsLegacyException;

import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Vector;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

/**
 * This helper-class is used to do task-actions like create or forward. It uses the
 * workplace-languagefile to add task-logs in the language of the user.
 * <P>
 *
 * @author <NAME>
 * @version $Revision: 1.6 $ $Date: 2005/07/18 10:10:35 $
 * @see com.opencms.workplace.CmsXmlWpTemplateFile
 * 
 * @deprecated Will not be supported past the OpenCms 6 release.
 */

public class CmsTaskAction {


    /**
     * Constant for generating user javascriptlist
     */
    private final static String C_ALL_ROLES = "___all";

    /**
     * Accepts a task.
     * @param cms The cms-object.
     * @param int taskid The id of the task.
     * @throws CmsException Throws CmsExceptions, that are be
     * thrown in calling methods.
     */

    public static void accept(CmsObject cms, int taskid) throws CmsException {
        CmsTaskService taskService = cms.getTaskService();
        taskService.acceptTask(taskid);
        String comment = "";
        taskService.writeTaskLog(taskid, comment, CmsWorkplaceDefault.C_TASKLOGTYPE_ACCEPTED);

        // send an email if "Benachrichtigung bei Annahme" was selected.
        CmsXmlLanguageFile lang = new CmsXmlLanguageFile(cms);
        CmsTask task = taskService.readTask(taskid);
        if(taskService.getTaskPar(task.getId(), CmsWorkplaceDefault.C_TASKPARA_ACCEPTATION) != null) {
            StringBuffer contentBuf = new StringBuffer(lang.getLanguageValue("task.email.accept.content"));
            contentBuf.append("\n");
            contentBuf.append(lang.getLanguageValue("task.label.project"));
            contentBuf.append(": ");
            String projectname = "?";
            try {
                projectname = taskService.readTask(task.getRoot()).getName();
            }
            catch(Exception exc) {


            // no root?!
            }
            contentBuf.append(projectname);
            contentBuf.append("\n");
            contentBuf.append(lang.getLanguageValue("task.label.task"));
            contentBuf.append(": ");
            contentBuf.append(task.getName());
            int projectid = taskService.readProject(task).getId();
            contentBuf.append("\n\n\n" + getTaskUrl(cms, taskid, projectid));
            String subject = lang.getLanguageValue("task.email.accept.subject") + " " + CmsUser.getFullName(taskService.readAgent(task));
            CmsUser[] users =  {
                taskService.readOwner(task)
            };
            try {
                CmsSimpleMail mail = createMail(taskService.readAgent(task),
                        users, subject, contentBuf.toString());
                new CmsMailTransport(mail).send();
            }
            catch(Exception exc) {
                if(CmsLog.getLog(CmsTaskAction.class).isWarnEnabled()) {
                    CmsLog.getLog(CmsTaskAction.class).warn("Error while sending task mail", exc);
                }
            }
        }
    }

    /**
     * Creates a new task.
     * @param cms The cms-object.
     * @param agentName The name of the new editor for this task.
     * @param roleName The name of the new role for the user.
     * @param taskName The new name of the task.
     * @param taskcomment The new comment for this task.
     * @param timeoutString The new timeout-date as a string in the following format:
     * "dd.mm.yyyy"
     * @param priorityString the new priority as String ("1" = high,
     * "2" = normal or "3" = low)
     * @param paraAcceptation controls if a message should be send by acceptation. ("checked" | "")
     * @param paraAll controls if a message should be send to all users in a role. ("checked" | "")
     * @param paraCompletion controls if a message should be send by completing this task. ("checked" | "")
     * @param paraDelivery controls if a message should be send by delivering a task. ("checked" | "")
     * @throws CmsException Throws CmsExceptions, that are be
     * thrown in calling methods.
     */

    public static void create(CmsObject cms, String agentName, String roleName, String taskName,
            String taskcomment, String timeoutString, String priorityString, String paraAcceptation,
            String paraAll, String paraCompletion, String paraDelivery) throws CmsException {
        CmsXmlLanguageFile lang = new CmsXmlLanguageFile(cms);
        // TODO: CW: check effects of this
        // if(roleName.equals(C_ALL_ROLES)) {
        //     roleName = cms.readUser(agentName).getDefaultGroup().getName();
        //}

        // try to create the task
        int priority = Integer.parseInt(priorityString);

        // create a long from the overgiven date.
        String splittetDate[] = CmsStringUtil.splitAsArray(timeoutString, '.');
        GregorianCalendar cal = new GregorianCalendar(Integer.parseInt(splittetDate[2]),
                Integer.parseInt(splittetDate[1]) - 1, Integer.parseInt(splittetDate[0]), 0, 0, 0);
        long timeout = cal.getTime().getTime();
        CmsTaskService taskService = cms.getTaskService();
        CmsTask task = taskService.createTask(agentName, roleName, taskName, timeout, priority);
        taskService.setTaskPar(task.getId(), CmsWorkplaceDefault.C_TASKPARA_ACCEPTATION, paraAcceptation);
        taskService.setTaskPar(task.getId(), CmsWorkplaceDefault.C_TASKPARA_ALL, paraAll);
        taskService.setTaskPar(task.getId(), CmsWorkplaceDefault.C_TASKPARA_COMPLETION, paraCompletion);
        taskService.setTaskPar(task.getId(), CmsWorkplaceDefault.C_TASKPARA_DELIVERY, paraDelivery);
        String comment = lang.getLanguageValue("task.label.forrole") + ": " + roleName + "\n";
        comment += lang.getLanguageValue("task.label.editor") + ": " + CmsUser.getFullName(cms.readUser(task.getAgentUser())) + "\n";
        comment += taskcomment;
        taskService.writeTaskLog(task.getId(), comment, CmsWorkplaceDefault.C_TASKLOGTYPE_CREATED);

        // send an email
        // per default send a mail from task's organizer to task's recipient.
        StringBuffer contentBuf = new StringBuffer(lang.getLanguageValue("task.email.create.content"));
        contentBuf.append("\n");
        contentBuf.append(lang.getLanguageValue("task.label.project"));
        contentBuf.append(": ");
        String projectname = "?";
        try {
            projectname = taskService.readTask(task.getRoot()).getName();
        }
        catch(Exception exc) {


        // no root?!
        }
        contentBuf.append(projectname);
        contentBuf.append("\n");
        contentBuf.append(lang.getLanguageValue("task.label.task"));
        contentBuf.append(": ");
        contentBuf.append(task.getName());
        contentBuf.append("\n");
        contentBuf.append(lang.getLanguageValue("task.label.actuator"));
        contentBuf.append(": ");
        contentBuf.append(CmsUser.getFullName(taskService.readOwner(task)));
        int projectid = taskService.readProject(task).getId();
        contentBuf.append("\n\n\n" + getTaskUrl(cms, task.getId(), projectid));
        String subject = lang.getLanguageValue("task.email.create.subject") + " " + CmsUser.getFullName(cms.readUser(task.getAgentUser())) + " / " + roleName;
        CmsUser[] users =  {
            taskService.readAgent(task)
        };
        CmsSimpleMail mail = null;
        try {
            mail = createMail(taskService.readOwner(task), users, subject, contentBuf.toString());
        }
        catch(CmsException e) {
            if(CmsLog.getLog(CmsTaskAction.class).isWarnEnabled()) {
                CmsLog.getLog(CmsTaskAction.class).warn("Could not generate mail while creating task for "
                        + taskService.readOwner(task).getName(), e);
            }
        }

        // if "Alle Rollenmitglieder von Aufgabe Benachrichtigen" checkbox is selected.
        if(taskService.getTaskPar(task.getId(), CmsWorkplaceDefault.C_TASKPARA_ALL) != null) {

            // the news deliver always "checked" or ""
            if(taskService.getTaskPar(task.getId(), CmsWorkplaceDefault.C_TASKPARA_ALL).equals("checked")) {
                try {
                    CmsGroup group = taskService.readGroup(task);
                    List groupUser = cms.getUsersOfGroup(group.getName());
                    mail = createMail(taskService.readOwner(task), groupUser,
                            subject, contentBuf.toString(), true);
                }
                catch(CmsException e) {
                    if(CmsLog.getLog(CmsTaskAction.class).isWarnEnabled()) {
                        CmsLog.getLog(CmsTaskAction.class).warn("Could not generate mail while creating task for "
                                + taskService.readOwner(task).getName(), e);
                    }
                }
            }
        }
        if (mail != null) {
            new CmsMailTransport(mail).send();
        }
    }
    
    /**
     * Helper method to create an email object.<p>
     * 
     * @param from the mail sender
     * @param to the mail receivers
     * @param subject the mail subject
     * @param content the mail content
     * @return the mail object to send
     * @throws CmsException if creating the mail object fails
     */
    public static CmsSimpleMail createMail(CmsUser from, CmsUser[] to, String subject, String content) throws CmsException {
        Vector v = new Vector(to.length);
        for (int i = 0; i < to.length; i++) {
            if (to[i].getEmail() == null) {
                continue;
            }
            if (to[i].getEmail().equals("")) {
                continue;
            }
            if (to[i].getEmail().indexOf("@") == -1 || to[i].getEmail().indexOf(".") == -1) {
                throw new CmsLegacyException("[" + CmsTaskAction.class.getName() + "] " + "Error in sending email, Invalid recipient email address: " + to[i].getEmail(), CmsLegacyException.C_BAD_NAME);
            }
            try {
                v.addElement(new InternetAddress(to[i].getEmail()));
            } catch (AddressException e) {
                throw new CmsLegacyException("[" + CmsTaskAction.class.getName() + "] " + "Error in sending email, Invalid recipient email address: " + to[i].getEmail(), CmsLegacyException.C_BAD_NAME);
            }
        }
        
        if (v.size() == 0) {
            throw new CmsLegacyException("[" + CmsTaskAction.class.getName() + "] " + "Error in sending email,Unknown recipient email address.", CmsLegacyException.C_BAD_NAME);
        }
        return createMail(from, v, subject, content, false);
    }
    
    /**
     * Helper method to create an email object.<p>
     * @param from the mail sender
     * @param to the mail receivers
     * @param subject the mail subject
     * @param content the mail content
     * @param createAddresses if true, the Vector of receivers contains CmsUsers and they are transformed into Internetaddresses
     * 
     * @return the mail object to send
     * @throws CmsException if creating the mail object fails
     */
    public static CmsSimpleMail createMail(CmsUser from, List to, String subject, String content, boolean createAddresses) throws CmsException {
        CmsSimpleMail mail = new CmsSimpleMail();
        //      check sender email address
        String fromAddress = from.getEmail();
        if (fromAddress == null || fromAddress.equals("")) {
            fromAddress = OpenCms.getSystemInfo().getMailSettings().getMailFromDefault();
        }
        if (fromAddress == null || fromAddress.equals("")) {            
            throw new CmsLegacyException("[" + CmsTaskAction.class.getName() + "] " + "Error in sending email,Unknown sender email address.", CmsLegacyException.C_BAD_NAME);
        }
        if (fromAddress.indexOf("@") == -1 || fromAddress.indexOf(".") == -1) {
            throw new CmsLegacyException("[" + CmsTaskAction.class.getName() + "] " + "Error in sending email,Unknown sender email address: " + fromAddress, CmsLegacyException.C_BAD_NAME);
        }
        
        if (to.size() == 0) {
            throw new CmsLegacyException("[" + CmsTaskAction.class.getName() + "] " + "Error in sending email,Unknown recipient email address.", CmsLegacyException.C_BAD_NAME);
        } else if (createAddresses) {
            List receivers = new ArrayList(to.size());
            for (int i=0; i<to.size(); i++) {
                CmsUser rec = (CmsUser)to.get(i);
                try {
                    receivers.add(new InternetAddress(rec.getEmail()));
                } catch (AddressException e) {
                    throw new CmsLegacyException("[" + CmsTaskAction.class.getName() + "] " + "Error in sending email, invalid recipient email address.", CmsLegacyException.C_BAD_NAME);
                }
            }
            mail.setTo(receivers);
        } else {
            mail.setTo(to);    
        }
        try {
            mail.setFrom(fromAddress);
        } catch (MessagingException e) {
            throw new CmsLegacyException("[" + CmsTaskAction.class.getName() + "] " + "Error in sending email, invalid sender email address.", CmsLegacyException.C_BAD_NAME);
        }
        mail.setSubject(subject == null ? "" : subject);
        mail.setMsg(content == null ? "" : content);
        return mail;
    }

    /**
     * Changes the timeou-date of the task.
     * @param cms The cms-object.
     * @param int taskid The id of the task.
     * @param timeoutString The new timeout-date as a string in the following format:
     * "dd.mm.yyyy"
     * @throws CmsException Throws CmsExceptions, that are be
     * thrown in calling methods.
     */

    public static void due(CmsObject cms, int taskid, String timeoutString) throws CmsException {
        CmsXmlLanguageFile lang = new CmsXmlLanguageFile(cms);
        CmsTaskService taskService = cms.getTaskService();
        CmsTask task = taskService.readTask(taskid);
        String splittetDate[] = CmsStringUtil.splitAsArray(timeoutString, '.');
        GregorianCalendar cal = new GregorianCalendar(Integer.parseInt(splittetDate[2]),
                Integer.parseInt(splittetDate[1]) - 1, Integer.parseInt(splittetDate[0]), 0, 0, 0);
        long timeout = cal.getTime().getTime();
        taskService.setTimeout(taskid, timeout);

        // add comment
        String comment = "";
        comment += lang.getLanguageValue("task.dialog.due.logmessage1") + " ";
        comment += CmsDateUtil.getDateShort(task.getTimeOut().getTime()) + " ";
        comment += lang.getLanguageValue("task.dialog.due.logmessage2") + " ";
        comment += CmsDateUtil.getDateShort(timeout);
        taskService.writeTaskLog(taskid, comment, CmsWorkplaceDefault.C_TASKLOGTYPE_DUECHANGED);
    }

    /**
     * Ends a task.
     * @param cms The cms-object.
     * @param int taskid The id of the task.
     * @throws CmsException Throws CmsExceptions, that are be
     * thrown in calling methods.
     */

    public static void end(CmsObject cms, int taskid) throws CmsException {
        CmsTaskService taskService = cms.getTaskService();
        taskService.endTask(taskid);
        String comment = "";
        taskService.writeTaskLog(taskid, comment, CmsWorkplaceDefault.C_TASKLOGTYPE_OK);

        // send an email if "Benachrichtigung bei Abhacken" was selected.
        CmsXmlLanguageFile lang = new CmsXmlLanguageFile(cms);
        CmsTask task = taskService.readTask(taskid);
        if(taskService.getTaskPar(task.getId(), CmsWorkplaceDefault.C_TASKPARA_COMPLETION) != null) {
            StringBuffer contentBuf = new StringBuffer(lang.getLanguageValue("task.email.end.content"));
            contentBuf.append("\n");
            contentBuf.append(lang.getLanguageValue("task.label.project"));
            contentBuf.append(": ");
            String projectname = "?";
            try {
                projectname = taskService.readTask(task.getRoot()).getName();
            }
            catch(Exception exc) {


            // no root?!
            }
            contentBuf.append(projectname);
            contentBuf.append("\n");
            contentBuf.append(lang.getLanguageValue("task.label.task"));
            contentBuf.append(": ");
            contentBuf.append(task.getName());
            contentBuf.append("\n");
            contentBuf.append(lang.getLanguageValue("task.label.taskfor"));
            contentBuf.append(": ");
            contentBuf.append(CmsUser.getFullName(taskService.readOriginalAgent(task)));
            contentBuf.append("\n");
            contentBuf.append(lang.getLanguageValue("task.label.editor"));
            contentBuf.append(": ");
            contentBuf.append(CmsUser.getFullName(taskService.readAgent(task)));
            int projectid = taskService.readProject(task).getId();
            contentBuf.append("\n\n\n" + getTaskUrl(cms, task.getId(), projectid));
            String subject = lang.getLanguageValue("task.email.end.subject") + " " + CmsUser.getFullName(taskService.readAgent(task));
            CmsUser[] users =  {
                taskService.readOwner(task)
            };
            try {
                CmsSimpleMail mail = createMail(taskService.readAgent(task),
                        users, subject, contentBuf.toString());
                new CmsMailTransport(mail).send();
            }
            catch(Exception exc) {
                if(CmsLog.getLog(CmsTaskAction.class).isWarnEnabled()) {
                    CmsLog.getLog(CmsTaskAction.class).warn("Error while sending task mail", exc);
                }
            }
        }
    }

    /**
     * Forwards a task. The task is forwarded to a new editor in a new role.
     * @param cms The cms-object.
     * @param taskid The id of the task.
     * @param newEditorName The name of the new editor for this task.
     * @param newRoleName The name of the new role for the user.
     * @throws CmsException Throws CmsExceptions, that are be
     * thrown in calling methods.
     */

    public static void forward(CmsObject cms, int taskid, String newEditorName, String newRoleName) throws CmsException {
        CmsXmlLanguageFile lang = new CmsXmlLanguageFile(cms);
        CmsUser newEditor = cms.readUser(newEditorName);
        // TODO: CW: check effects of this
        // if(newRoleName.equals(C_ALL_ROLES)) {
        //    newRoleName = cms.readUser(newEditorName).getDefaultGroup().getName();
        // }
        CmsGroup oldRole = cms.readGroup(newRoleName);
        CmsTaskService taskService = cms.getTaskService();
        taskService.forwardTask(taskid, oldRole.getName(), newEditor.getName());
        String comment = lang.getLanguageValue("task.dialog.forward.logmessage");
        comment += " " + CmsUser.getFullName(newEditor);
        taskService.writeTaskLog(taskid, comment, CmsWorkplaceDefault.C_TASKLOGTYPE_FORWARDED);

        // send an email if "Benachrichtigung bei Weiterleitung" was selected.
        CmsTask task = taskService.readTask(taskid);
        if(taskService.getTaskPar(task.getId(), CmsWorkplaceDefault.C_TASKPARA_DELIVERY) != null) {
            StringBuffer contentBuf = new StringBuffer(lang.getLanguageValue("task.email.forward.content"));
            contentBuf.append("\n");
            contentBuf.append(lang.getLanguageValue("task.label.project"));
            contentBuf.append(": ");
            String projectname = "?";
            try {
                projectname = taskService.readTask(task.getRoot()).getName();
            }
            catch(Exception exc) {


            // no root?!
            }
            contentBuf.append(projectname);
            contentBuf.append("\n");
            contentBuf.append(lang.getLanguageValue("task.label.task"));
            contentBuf.append(": ");
            contentBuf.append(task.getName());
            contentBuf.append("\n");
            contentBuf.append(lang.getLanguageValue("task.label.actuator"));
            contentBuf.append(": ");
            contentBuf.append(CmsUser.getFullName(taskService.readOwner(task)));
            contentBuf.append("\n");
            contentBuf.append(lang.getLanguageValue("task.label.taskfor"));
            contentBuf.append(": ");
            contentBuf.append(CmsUser.getFullName(taskService.readOriginalAgent(task)));
            contentBuf.append("\n");
            contentBuf.append(lang.getLanguageValue("task.label.editor"));
            contentBuf.append(": ");
            contentBuf.append(CmsUser.getFullName(taskService.readAgent(task)));
            int projectid = taskService.readProject(task).getId();
            contentBuf.append("\n\n\n" + getTaskUrl(cms, task.getId(), projectid));
            String subject = lang.getLanguageValue("task.email.forward.subject") + " " + CmsUser.getFullName(cms.readUser(task.getAgentUser())) + " / " + newRoleName;

            // if "Alle Rollenmitglieder von Aufgabe Benachrichtigen" checkbox is selected.
            String sendToAll = taskService.getTaskPar(task.getId(), CmsWorkplaceDefault.C_TASKPARA_ALL);
            if(!CmsStringUtil.isEmpty(sendToAll)) {
                try {
                    CmsGroup group = taskService.readGroup(task);
                    List users = cms.getUsersOfGroup(group.getName());
                    CmsSimpleMail mail = createMail(cms.getRequestContext().currentUser(),
                        users, subject, contentBuf.toString(), true);
                    new CmsMailTransport(mail).send();
                }
                catch(Exception exc) {
                    if(CmsLog.getLog(CmsTaskAction.class).isWarnEnabled()) {
                        CmsLog.getLog(CmsTaskAction.class).warn("Error while sending task mail", exc);
                    }
                }
            }
            else {

                // send a mail to user
                CmsUser[] user =  {
                    taskService.readAgent(task)
                };
                try {
                    CmsSimpleMail mail1 = createMail(cms.getRequestContext().currentUser(), 
                            user, subject, contentBuf.toString());
                    new CmsMailTransport(mail1).send();
                }
                catch(Exception exc) {
                    if(CmsLog.getLog(CmsTaskAction.class).isWarnEnabled()) {
                        CmsLog.getLog(CmsTaskAction.class).warn("Error while sending task mail", exc);
                    }
                }

                // send a mail to owner
                CmsUser[] owner =  {
                    taskService.readOwner(task)
                };
                try {
                    CmsSimpleMail mail2 = createMail(cms.getRequestContext().currentUser(),
                            owner, subject, contentBuf.toString());
                    new CmsMailTransport(mail2).send();
                }
                catch(Exception exc) {
                    if(CmsLog.getLog(CmsTaskAction.class).isWarnEnabled()) {
                        CmsLog.getLog(CmsTaskAction.class).warn("Error while sending task mail", exc);
                    }
                }
            }
        }
    }

    /**
     * Gets the description of a task.
     * The description is stored in the task-log.
     * @param cms The cms-object.
     * @param int taskid The id of the task.
     * @return String the comment-string.
     * @throws CmsException Throws CmsExceptions, that are be
     * thrown in calling methods.
     */

    public static String getDescription(CmsObject cms, int taskid) throws CmsException {
        StringBuffer retValue = new StringBuffer("");
        CmsTaskLog tasklog;
        List taskdocs = cms.getTaskService().readTaskLogs(taskid);

        // go through all tasklogs to find the comment
        for(int i = 1;i <= taskdocs.size();i++) {
            tasklog = (CmsTaskLog)taskdocs.get(taskdocs.size() - i);
            int type = tasklog.getType();

            // check if this is a type "created" or "new"
            if((type == CmsWorkplaceDefault.C_TASKLOGTYPE_CREATED) || (type == CmsWorkplaceDefault.C_TASKLOGTYPE_REACTIVATED)) {
                String comment[] = CmsStringUtil.splitAsArray(tasklog.getComment(), '\n');
                for(int j = 2;j < comment.length;j++) {
                    retValue.append(comment[j] + "\n");
                }
                break;
            }
        }
        return retValue.toString();
    }

    /**
     * Sends a message to the editor of the task.
     * @param cms The cms-object.
     * @param int taskid The id of the task.
     * @param message The text of the message.
     * @throws CmsException Throws CmsExceptions, that are be
     * thrown in calling methods.
     */

    public static void message(CmsObject cms, int taskid, String message) throws CmsException {
        CmsXmlLanguageFile lang = new CmsXmlLanguageFile(cms);
        CmsTaskService taskService = cms.getTaskService();
        CmsTask task = taskService.readTask(taskid);
        String comment = lang.getLanguageValue("task.dialog.message.head") + " ";
        if((message != null) && (message.length() != 0)) {
            comment += CmsUser.getFullName(taskService.readAgent(task)) + "\n";
            comment += message;
            taskService.writeTaskLog(taskid, comment, CmsWorkplaceDefault.C_TASKLOGTYPE_CALL);
        }

        // send an email
        StringBuffer contentBuf = new StringBuffer(lang.getLanguageValue("task.email.message.content"));
        contentBuf.append("\n");
        contentBuf.append(lang.getLanguageValue("task.label.project"));
        contentBuf.append(": ");
        String projectname = "?";
        try {
            projectname = taskService.readTask(task.getRoot()).getName();
        }
        catch(Exception exc) {


        // no root?!
        }
        contentBuf.append(projectname);
        contentBuf.append("\n");
        contentBuf.append(lang.getLanguageValue("task.label.task"));
        contentBuf.append(": ");
        contentBuf.append(task.getName());
        contentBuf.append("\n");
        contentBuf.append(lang.getLanguageValue("task.label.actuator"));
        contentBuf.append(": ");
        contentBuf.append(CmsUser.getFullName(taskService.readOwner(task)));
        int projectid = taskService.readProject(task).getId();
        contentBuf.append("\n\n\n" + getTaskUrl(cms, task.getId(), projectid));
        String subject = lang.getLanguageValue("task.email.message.subject") + " " + CmsUser.getFullName(taskService.readOwner(task));
        CmsUser[] users =  {
            taskService.readAgent(task)
        };
        try {
            CmsSimpleMail mail = createMail(taskService.readOwner(task), users, subject, contentBuf.toString());
            new CmsMailTransport(mail).send();
        }
        catch(Exception exc) {
            if(CmsLog.getLog(CmsTaskAction.class).isWarnEnabled()) {
                CmsLog.getLog(CmsTaskAction.class).warn("Error while sending task mail", exc);
            }
        }
    }

    /**
     * Changes the priority of a task.
     * @param cms The cms-object.
     * @param int taskid The id of the task.
     * @param priorityString the new priority as String ("1" = high,
     * "2" = normal or "3" = low)
     * @throws CmsException Throws CmsExceptions, that are be
     * thrown in calling methods.
     */

    public static void priority(CmsObject cms, int taskid, String priorityString) throws CmsException {
        CmsXmlLanguageFile lang = new CmsXmlLanguageFile(cms);
        CmsTaskService taskService = cms.getTaskService();
        CmsTask task = taskService.readTask(taskid);
        int priority = Integer.parseInt(priorityString);
        taskService.setPriority(taskid, priority);

        // add comment
        String comment = "";
        comment += lang.getLanguageValue("task.dialog.priority.logmessage1") + " ";
        comment += lang.getLanguageValue("task.dialog.priority.logmessageprio" + task.getPriority()) + " ";
        comment += lang.getLanguageValue("task.dialog.priority.logmessage2") + " ";
        comment += lang.getLanguageValue("task.dialog.priority.logmessageprio" + priority) + " ";
        taskService.writeTaskLog(taskid, comment, CmsWorkplaceDefault.C_TASKLOGTYPE_PRIORITYCHANGED);
    }

    /**
     * Sends a message to the initiator (owner) of the task.
     * @param cms The cms-object.
     * @param int taskid The id of the task.
     * @param message The text of the message.
     * @throws CmsException Throws CmsExceptions, that are be
     * thrown in calling methods.
     */

    public static void query(CmsObject cms, int taskid, String message) throws CmsException {
        CmsXmlLanguageFile lang = new CmsXmlLanguageFile(cms);
        CmsTaskService taskService = cms.getTaskService();
        CmsTask task = taskService.readTask(taskid);
        String comment = lang.getLanguageValue("task.dialog.query.head") + " ";
        if((message != null) && (message.length() != 0)) {
            comment += CmsUser.getFullName(taskService.readOwner(task)) + "\n";
            comment += message;
            taskService.writeTaskLog(taskid, comment, CmsWorkplaceDefault.C_TASKLOGTYPE_CALL);
        }

        // send an email.
        StringBuffer contentBuf = new StringBuffer(lang.getLanguageValue("task.email.query.content"));
        contentBuf.append("\n");
        contentBuf.append(lang.getLanguageValue("task.label.project"));
        contentBuf.append(": ");
        String projectname = "?";
        try {
            projectname = taskService.readTask(task.getRoot()).getName();
        }
        catch(Exception exc) {


        // no root?!
        }
        contentBuf.append(projectname);
        contentBuf.append("\n");
        contentBuf.append(lang.getLanguageValue("task.label.task"));
        contentBuf.append(": ");
        contentBuf.append(task.getName());
        contentBuf.append("\n");
        contentBuf.append(lang.getLanguageValue("task.label.editor"));
        contentBuf.append(": ");
        contentBuf.append(CmsUser.getFullName(taskService.readAgent(task)));
        int projectid = taskService.readProject(task).getId();
        contentBuf.append("\n\n\n" + getTaskUrl(cms, task.getId(), projectid));
        String subject = lang.getLanguageValue("task.email.query.subject") + " " + CmsUser.getFullName(taskService.readAgent(task));
        CmsUser[] users =  {
            taskService.readOwner(task)
        };
        try {
            CmsSimpleMail mail = createMail(taskService.readAgent(task),
                    users, subject, contentBuf.toString());
            new CmsMailTransport(mail).send();
        }
        catch(Exception exc) {
            if(CmsLog.getLog(CmsTaskAction.class).isWarnEnabled()) {
                CmsLog.getLog(CmsTaskAction.class).warn("Error while sending task mail", exc);
            }
        }
    }

    /**
     * Reaktivates a task.
     * @param cms The cms-object.
     * @param int taskid The id of the task.
     * @param agentName The name of the new editor for this task.
     * @param roleName The name of the new role for the user.
     * @param taskName The new name of the task.
     * @param taskcomment The new comment for this task.
     * @param timeoutString The new timeout-date as a string in the following format:
     * "dd.mm.yyyy"
     * @param priorityString the new priority as String ("1" = high,
     * "2" = normal or "3" = low)
     * @param paraAcceptation controls if a message should be send by acceptation. ("checked" | "")
     * @param paraAll controls if a message should be send to all users in a role. ("checked" | "")
     * @param paraCompletion controls if a message should be send by completing this task. ("checked" | "")
     * @param paraDelivery controls if a message should be send by delivering a task. ("checked" | "")
     * @throws CmsException Throws CmsExceptions, that are be
     * thrown in calling methods.
     */

    public static void reakt(CmsObject cms, int taskid, String agentName, String roleName, String taskName,
            String taskcomment, String timeoutString, String priorityString,
            String paraAcceptation, String paraAll, String paraCompletion, String paraDelivery) throws CmsException {
        CmsXmlLanguageFile lang = new CmsXmlLanguageFile(cms);
        CmsTaskService taskService = cms.getTaskService();
        CmsTask task = taskService.readTask(taskid);
        // TODO: CW: check effects of this
        //if(roleName.equals(C_ALL_ROLES)) {
        //    roleName = cms.readUser(agentName).getDefaultGroup().getName();
        //}
        taskService.setName(taskid, taskName);

        // try to reaktivate the task
        taskService.reactivateTask(taskid);
        int priority = Integer.parseInt(priorityString);
        taskService.setPriority(taskid, priority);

        // create a long from the overgiven date.
        String splittetDate[] = CmsStringUtil.splitAsArray(timeoutString, '.');
        GregorianCalendar cal = new GregorianCalendar(Integer.parseInt(splittetDate[2]), Integer.parseInt(splittetDate[1]) - 1,
                Integer.parseInt(splittetDate[0]), 0, 0, 0);
        long timeout = cal.getTime().getTime();
        taskService.setTimeout(taskid, timeout);
        taskService.setTaskPar(taskid, CmsWorkplaceDefault.C_TASKPARA_ACCEPTATION, paraAcceptation);
        taskService.setTaskPar(taskid, CmsWorkplaceDefault.C_TASKPARA_ALL, paraAll);
        taskService.setTaskPar(taskid, CmsWorkplaceDefault.C_TASKPARA_COMPLETION, paraCompletion);
        taskService.setTaskPar(taskid, CmsWorkplaceDefault.C_TASKPARA_DELIVERY, paraDelivery);
        taskService.forwardTask(taskid, roleName, agentName);
        String comment = lang.getLanguageValue("task.label.forrole") + ": " + roleName + "\n";
        comment += lang.getLanguageValue("task.label.editor") + ": " + CmsUser.getFullName(cms.readUser(agentName)) + "\n";
        comment += taskcomment;
        taskService.writeTaskLog(task.getId(), comment, CmsWorkplaceDefault.C_TASKLOGTYPE_REACTIVATED);

        // send an email
        StringBuffer contentBuf = new StringBuffer(lang.getLanguageValue("task.email.reakt.content"));
        contentBuf.append("\n");
        contentBuf.append(lang.getLanguageValue("task.label.project"));
        contentBuf.append(": ");
        String projectname = "?";
        try {
            projectname = taskService.readTask(task.getRoot()).getName();
        }
        catch(Exception exc) {


        // no root?!
        }
        contentBuf.append(projectname);
        contentBuf.append("\n");
        contentBuf.append(lang.getLanguageValue("task.label.task"));
        contentBuf.append(": ");
        contentBuf.append(task.getName());
        int projectid = taskService.readProject(task).getId();
        contentBuf.append("\n\n\n" + getTaskUrl(cms, task.getId(), projectid));
        String subject = lang.getLanguageValue("task.email.reakt.subject") + " " + CmsUser.getFullName(cms.readUser(task.getAgentUser())) + " / " + roleName;
        CmsUser[] users =  {
            taskService.readAgent(task)
        };
        CmsSimpleMail mail;
        mail = createMail(taskService.readOwner(task), users, subject, contentBuf.toString());

        String sendToAll = taskService.getTaskPar(task.getId(), CmsWorkplaceDefault.C_TASKPARA_ALL);
        if(!CmsStringUtil.isEmpty(sendToAll)) {
            CmsGroup group = taskService.readGroup(task);
            List groupUsers = cms.getUsersOfGroup(group.getName());
            mail = createMail(taskService.readOwner(task), groupUsers,
                    subject, contentBuf.toString(), true);
        }
        try {
            new CmsMailTransport(mail).send();
        }
        catch(Exception exc) {
            if(CmsLog.getLog(CmsTaskAction.class).isWarnEnabled()) {
                CmsLog.getLog(CmsTaskAction.class).warn("Error while sending task mail", exc);
            }
        }
    }
{'fraction_non_alphanumeric': 0.11453343362761788, 'fraction_numerical': 0.0027185029758369952, 'mean_word_length': 2.4586033774532177, 'pattern_counts': {'":': 52, '<': 19, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7315786', 'n_tokens_mistral': 21315, 'n_tokens_neox': 21025, 'n_words': 4738}