<commit_before>/**
 * Copyright 1996-2014 FoxBPM ORG.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * @author yangguangftlp
 */
package org.foxbpm.connector.flowconnector.AutoSendMail;

import java.util.Date;
import java.util.List;

import org.foxbpm.connector.mail.MailEngine;
import org.foxbpm.connector.mail.MailEntity;
import org.foxbpm.engine.Constant;
import org.foxbpm.engine.IdentityService;
import org.foxbpm.engine.exception.FoxBPMException;
import org.foxbpm.engine.execution.ConnectorExecutionContext;
import org.foxbpm.engine.identity.User;
import org.foxbpm.engine.impl.Context;
import org.foxbpm.engine.impl.connector.FlowConnectorHandler;
import org.foxbpm.engine.impl.entity.IdentityLinkEntity;
import org.foxbpm.engine.impl.entity.TaskEntity;
import org.foxbpm.engine.impl.identity.Authentication;
import org.foxbpm.engine.impl.util.StringUtil;
import org.foxbpm.model.config.foxbpmconfig.MailInfo;
import org.foxbpm.model.config.foxbpmconfig.SysMailConfig;

/**
 * 自动发送邮件
 * 
 * @author yangguangftlp
 * @date 2014年7月1日
 */
public class AutoSendMail implements FlowConnectorHandler {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4576139954014666005L;

	private java.lang.String title;

	private java.lang.String content;

	public void execute(ConnectorExecutionContext executionContext) throws Exception {
		// 获取邮件配置
		SysMailConfig sysMailConfig = Context.getProcessEngineConfiguration().getSysMailConfig();
		MailInfo mailInfoObj = null;
		for (MailInfo mailInfo : sysMailConfig.getMailInfo()) {
			if (mailInfo.getMailName().equals(sysMailConfig.getSelected())) {
				mailInfoObj = mailInfo;
			}
		}
		// 判断邮件配置信息是否为空
		if (null == mailInfoObj) {
			throw new FoxBPMException("系统邮件配置错误请检查流程邮件配置！");
		}
		TaskEntity taskEntity = executionContext.getAssignTask();
		StringBuffer mailTitle = new StringBuffer();
		// 如果主题为空
		if (StringUtil.isEmpty(StringUtil.trim(title))) {
			mailTitle.append("[" + taskEntity.getProcessDefinitionName() + "] ").append(taskEntity.getDescription()).append(" is pending for your approval or handle");
		}

		String taskUrl = "http://localhost:8889/foxbpm-webapps-base";
		if (StringUtil.isEmpty(content)) {
			content = "<br>Hello,<br>你好,<br><br> " + mailTitle + "<br><br>" + "Please click url to deal with job: <br>请访问此链接地址进入任务:<br> <a href=" + taskUrl + ">" + taskUrl + "</a><br><br>"
					+ "Best Regards!<br>诚挚问候!<br>Note: Please do not reply to this email , This mailbox does not allow incoming messages." + "<br>注意: 本邮件为工作流系统发送，请勿回复。 ";
		}
		// 获取用户
		IdentityService identityService = Context.getProcessEngineConfiguration().getIdentityService();
		// 判断是否独占任务
		User user = null;
		if (StringUtil.isNotEmpty(taskEntity.getAssignee())) {
			user = identityService.getUser(taskEntity.getAssignee());
			// 判断用户是否为空
			if (null != user) {
				// 如果用户存在邮件地址即发生邮件
				if (StringUtil.isNotEmpty(user.getEmail())) {
					// 保存邮件实体
					saveMail(user.getEmail(), title, content, taskEntity.getId());
				}
			}
		} else {
			// 处理共享任务
			StringBuffer to = new StringBuffer();
			String userId = null;
			for (IdentityLinkEntity identityLink : taskEntity.getTaskIdentityLinks()) {
				userId = identityLink.getUserId();
				if (StringUtil.isNotEmpty(userId) && !Constant.FOXBPM_ALL_USER.equals(userId)) {
					user = Authentication.selectUserByUserId(identityLink.getUserId());
					if (null != user) {
						if (StringUtil.isNotEmpty(user.getEmail())) {
							to.append(user.getEmail()).append(",");
						}
					}
				} else if (Constant.FOXBPM_ALL_USER.equals(userId)) {
					// 处理所有者
					List<User> users = identityService.getUsers(null, null);
					for (User u : users) {
						if (StringUtil.isNotEmpty(u.getEmail())) {
							to.append(u.getEmail()).append(",");
						}
					}
				} else {
					// 获取组下面所有用户
					List<User> users = Authentication.selectUserByGroupIdAndType(identityLink.getGroupId(), identityLink.getGroupType());
					if (null != users) {
						for (User u : users) {
							if (StringUtil.isNotEmpty(u.getEmail())) {
								to.append(u.getEmail()).append(",");
							}
						}
					}
				}
			}
			if (to.length() > 0) {
				// 删除最后一个','
				to.deleteCharAt(to.length() - 1);
				// 保存邮件实体
				saveMail(to.toString(), title, content, taskEntity.getId());
			}<commit_msg>提交修改自动发送邮件内容连接url<commit_after>/**
 * Copyright 1996-2014 FoxBPM ORG.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * @author yangguangftlp
 */
package org.foxbpm.connector.flowconnector.AutoSendMail;

import java.util.Date;
import java.util.List;

import org.foxbpm.connector.mail.MailEngine;
import org.foxbpm.connector.mail.MailEntity;
import org.foxbpm.engine.Constant;
import org.foxbpm.engine.IdentityService;
import org.foxbpm.engine.exception.FoxBPMException;
import org.foxbpm.engine.execution.ConnectorExecutionContext;
import org.foxbpm.engine.identity.User;
import org.foxbpm.engine.impl.Context;
import org.foxbpm.engine.impl.connector.FlowConnectorHandler;
import org.foxbpm.engine.impl.entity.IdentityLinkEntity;
import org.foxbpm.engine.impl.entity.TaskEntity;
import org.foxbpm.engine.impl.identity.Authentication;
import org.foxbpm.engine.impl.util.StringUtil;
import org.foxbpm.model.config.foxbpmconfig.MailInfo;
import org.foxbpm.model.config.foxbpmconfig.SysMailConfig;

/**
 * 自动发送邮件
 * 
 * @author yangguangftlp
 * @date 2014年7月1日
 */
public class AutoSendMail implements FlowConnectorHandler {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4576139954014666005L;

	private java.lang.String title;

	private java.lang.String content;

	public void execute(ConnectorExecutionContext executionContext) throws Exception {
		// 获取邮件配置
		SysMailConfig sysMailConfig = Context.getProcessEngineConfiguration().getSysMailConfig();
		MailInfo mailInfoObj = null;
		for (MailInfo mailInfo : sysMailConfig.getMailInfo()) {
			if (mailInfo.getMailName().equals(sysMailConfig.getSelected())) {
				mailInfoObj = mailInfo;
			}
		}
		// 判断邮件配置信息是否为空
		if (null == mailInfoObj) {
			throw new FoxBPMException("系统邮件配置错误请检查流程邮件配置！");
		}
		TaskEntity taskEntity = executionContext.getAssignTask();
		StringBuffer mailTitle = new StringBuffer();
		// 如果主题为空
		if (StringUtil.isEmpty(StringUtil.trim(title))) {
			mailTitle.append("[" + taskEntity.getProcessDefinitionName() + "] ").append(taskEntity.getDescription()).append(" is pending for your approval or handle");
		}

		String taskUrl = "http://www.baidu.com";
		if (StringUtil.isEmpty(content)) {
			content = "<br>Hello,<br>你好,<br><br> " + mailTitle + "<br><br>" + "Please click url to deal with job: <br>请访问此链接地址进入任务:<br> <a href=" + taskUrl + ">" + taskUrl + "</a><br><br>"
					+ "Best Regards!<br>诚挚问候!<br>Note: Please do not reply to this email , This mailbox does not allow incoming messages." + "<br>注意: 本邮件为工作流系统发送，请勿回复。 ";
		}
		// 获取用户
		IdentityService identityService = Context.getProcessEngineConfiguration().getIdentityService();
		// 判断是否独占任务
		User user = null;
		if (StringUtil.isNotEmpty(taskEntity.getAssignee())) {
			user = identityService.getUser(taskEntity.getAssignee());
			// 判断用户是否为空
			if (null != user) {
				// 如果用户存在邮件地址即发生邮件
				if (StringUtil.isNotEmpty(user.getEmail())) {
					// 保存邮件实体
					saveMail(user.getEmail(), title, content, taskEntity.getId());
				}
			}
		} else {
			// 处理共享任务
			StringBuffer to = new StringBuffer();
			String userId = null;
			for (IdentityLinkEntity identityLink : taskEntity.getTaskIdentityLinks()) {
				userId = identityLink.getUserId();
				if (StringUtil.isNotEmpty(userId)) {
					if (!Constant.FOXBPM_ALL_USER.equals(userId)) {
						user = Authentication.selectUserByUserId(userId);
						if (null != user) {
							if (StringUtil.isNotEmpty(user.getEmail())) {
								to.append(user.getEmail()).append(",");
							}
						}
					} else {
						// 处理所有者
						List<User> users = identityService.getUsers(null, null);
						if (null != users) {
							for (User u : users) {
								if (StringUtil.isNotEmpty(u.getEmail())) {
									to.append(u.getEmail()).append(",");
								}
							}
						}
					}
				} else {
					// 获取组下面所有用户
					List<User> users = Authentication.selectUserByGroupIdAndType(identityLink.getGroupId(), identityLink.getGroupType());
					if (null != users) {
						for (User u : users) {
							if (StringUtil.isNotEmpty(u.getEmail())) {
								to.append(u.getEmail()).append(",");
							}
						}
					}
				}
			}
			if (to.length() > 0) {
				// 删除最后一个','
				to.deleteCharAt(to.length() - 1);
				// 保存邮件实体
				saveMail(to.toString(), title, content, taskEntity.getId());
			}
{'fraction_non_alphanumeric': 0.1303485702358589, 'fraction_numerical': 0.008557712377374243, 'mean_word_length': 3.82527693856999, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5457344', 'n_tokens_mistral': 3735, 'n_tokens_neox': 3317, 'n_words': 648}