⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 requestaction.java

📁 Java写的ERP系统
💻 JAVA
字号:
/******************************************************************************
 * The contents of this file are subject to the   Compiere License  Version 1.1
 * ("License"); You may not use this file except in compliance with the License
 * You may obtain a copy of the License at http://www.compiere.org/license.html
 * Software distributed under the License is distributed on an  "AS IS"  basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 * The Original Code is                  Compiere  ERP & CRM  Business Solution
 * The Initial Developer of the Original Code is Jorg Janke  and ComPiere, Inc.
 * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
 * created by ComPiere are Copyright (C) ComPiere, Inc.;   All Rights Reserved.
 * Contributor(s): ______________________________________.
 *****************************************************************************/
package org.compiere.process;

import java.sql.*;
import java.math.*;
import java.util.*;

import org.apache.log4j.Logger;

import org.compiere.process.*;
import org.compiere.model.*;
import org.compiere.util.EMail;
import org.compiere.util.Env;
import org.compiere.util.DB;
import org.compiere.util.Msg;

/**
 *  Request Action.
 *  <br>
 *  The static methods <code>cation</code> and <code>copyText</code> are Callouts
 *  when a choice was changed.
 *  <p>
 *  The Object with the method <code>startProcess</code> is
 *  called then pressing the Processing Button in R_Request.
 *  If startProcess returns true, the procedure R_Request_Process.sql is
 *  executed (Copy history, etc.).
 *  <pre>
 *  ActionType          action          startProcess
 *  ----------          ----------      ----------
 *  Remainder           -               -
 *  Call                -               -
 *  EMail               verify address  send mail
 *  Transfer            -               send mail
 *  Close               -               -
 *
 *  </pre>
 *  @author Jorg Janke
 *  @version  $Id: RequestAction.java,v 1.9 2003/04/30 01:01:30 jjanke Exp $
 */
public class RequestAction implements ProcessCall
{
	/**	Logger							*/
	static private Logger			s_log = Logger.getLogger (RequestAction.class);

	//  ActionType (AD_Reference_ID=220)
	public final static String      A_REMAINDER     = "A";
	public final static String      A_CALL          = "C";
	public final static String      A_EMAIL         = "E";
	public final static String      A_TRANSFER      = "T";
	public final static String      A_CLOSE         = "X";

	/**
	 *  Request Action <b>Callout</b>.
	 *  (Action has been changed in window)
	 *
	 *  @param ctx      Context
	 *  @param WindowNo current Window No
	 *  @param mTab     Model Tab
	 *  @param mField   Model Field
	 *  @param value    The new value
	 *  @return Error message or ""
	 */
	public static String action (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value)
	{
		String colName = mField.getColumnName();
		s_log.info("action - " + colName + "=" + value);
		if (value == null)
			return "";
		String action = (String)value;
		//
		if (action.equals(A_EMAIL))
			return actionCheckEMail(ctx, mTab);
		return "";
	}   //  action

	/**
	 *  Check the ability to send email.
	 *  @param ctx  Context
	 *  @param mTab Model Tab
	 *  @return Error message or ""
	 */
	private static String actionCheckEMail(Properties ctx, MTab mTab)
	{
		//  Mail Host
		String host = EMail.getCurrentSmtpHost(ctx);
		if (host == null || host.length() == 0)
			return "RequestActionEMailNoSMTP";

		//  Mail To
		Object toAddr = mTab.getValue("C_BPartner_ID");
		if (toAddr == null)
			return "RequestActionEMailNoTo";
		int C_BPartner_ID = ((Integer)toAddr).intValue();
		if (C_BPartner_ID == 0)
			return "RequestActionEMailNoTo";
		int C_BPartner_Contact_ID = 0;
		toAddr = mTab.getValue("C_BPartner_Contact_ID");
		if (toAddr != null)
			C_BPartner_Contact_ID = ((Integer)toAddr).intValue();
		String emailTo = getEMailOfBPartner (C_BPartner_ID, C_BPartner_Contact_ID);
		if (emailTo == null || emailTo.length() == 0)
			return "RequestActionEMailNoTo";

		//  Mail From
		String emailFrom = EMail.getCurrentUserEMail(ctx, false);
		if (emailFrom == null || emailFrom.length() == 0)
			return "RequestActionEMailNoFrom";
		int user = 0;   //  Check that UI user is Request User
		Object reqUser = mTab.getValue("AD_User_ID");
		if (reqUser != null)
			user = ((Integer)reqUser).intValue();
		int AD_User_ID = Env.getContextAsInt (ctx, "#AD_User_ID");
		if (user != AD_User_ID)
			mTab.setValue("AD_User_ID", new Integer(AD_User_ID));

		//  RequestActionEMailInfo - EMail from {0} to {1}
		Object[] args = new Object[] {emailFrom, emailTo};
		String msg = Msg.getMsg(ctx, "RequestActionEMailInfo", args);
		mTab.setValue("Result", msg);
		return "";
	}   //  actionCheckEMail

	/**
	 *  Get Email Address from Business Partner (Contact)
	 *  @param C_BPartner_ID            Business Partner
	 *  @param C_BPartner_Contact_ID    Contact (optional)
	 *  @return EMail Address or null
	 */
	private static String getEMailOfBPartner (int C_BPartner_ID, int C_BPartner_Contact_ID)
	{
		String email = null;
		String sql = "SELECT bpc.EMail "
			+ "FROM C_BPartner bp"
			+ " INNER JOIN C_BPartner_Contact bpc ON (bp.C_BPartner_ID=bpc.C_BPartner_ID) "
			+ "WHERE bp.C_BPartner_ID=?";
		if (C_BPartner_Contact_ID != 0)
			sql += " AND bpc.C_BPartner_Contact_ID=?";
		try
		{
			PreparedStatement pstmt = DB.prepareStatement(sql);
			pstmt.setInt(1, C_BPartner_ID);
			if (C_BPartner_Contact_ID != 0)
				pstmt.setInt(2, C_BPartner_Contact_ID);
			ResultSet rs = pstmt.executeQuery();
			if (rs.next())
				email = rs.getString(1);
			else
				s_log.warn("getEMailOfBPartner - None for C_BPartner_ID=" + C_BPartner_ID + ", C_BPartner_Contact_ID=" + C_BPartner_Contact_ID);
			rs.close();
			pstmt.close();
		}
		catch (SQLException e)
		{
			s_log.error("getEmailFromBPartner", e);
		}
		return email;
	}   //  getEmailFromBPartner

	/*************************************************************************/

	/**
	 *  Request - Copy Text - <b>Callout</b>
	 *
	 *  @param ctx      Context
	 *  @param WindowNo current Window No
	 *  @param mTab     Model Tab
	 *  @param mField   Model Field
	 *  @param value    The new value
	 *  @return Error message or ""
	 */
	public static String copyText (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value)
	{
		String colName = mField.getColumnName();
		s_log.info("copyText - " + colName + "=" + value);
		if (value == null)
			return "";

		Integer R_MailText_ID = (Integer)value;
		String sql = "SELECT MailHeader, MailText FROM R_MailText WHERE R_MailText_ID=?";
		try
		{
			PreparedStatement pstmt = DB.prepareStatement(sql);
			pstmt.setInt(1, R_MailText_ID.intValue());
			ResultSet rs = pstmt.executeQuery();
			if (rs.next())
			{
				String hdr = rs.getString(1);
				String txt = rs.getString(2);
				//
				hdr = Env.parseContext(ctx, WindowNo, hdr, false, true);
				txt = Env.parseContext(ctx, WindowNo, txt, false, true);
				//
				mTab.setValue("MailSubject", hdr);
				mTab.setValue("MailText", txt);
			}
			rs.close();
			pstmt.close();
		}
		catch (SQLException e)
		{
			s_log.error("copyText", e);
		}
		return "";
	}   //  	copyText

	/*************************************************************************/

	/**
	 *  Constructor for Request
	 */
	public RequestAction()
	{
	}   //  RequestAction

	/**	Logger							*/
	private Logger			log = Logger.getLogger (getClass());

	/**
	 *  Start the process.
	 *  Called then pressing the Process button in R_Request.
	 *  It should only return false, if the function could not be performed
	 *  as this causes the process to abort.
	 *
	 *  @param ctx  Context
	 *  @param pi	Process Info
	 *  @return true if success
	 */
	public boolean startProcess (Properties ctx, ProcessInfo pi)
	{
		log.info("startProcess - " + pi);
		//
		int AD_Client_ID = 0;
		String DocumentNo = null;
		String ActionType = null;
		String Summary = null;
		String MailSubject = null;
		String MailText = null;
		int C_BPartner_ID = 0;
		int C_BPartner_Contact_ID = 0;
		int AD_User_ID = 0;
		int UpdatedBy = 0;
		//
		String sql = "SELECT AD_Client_ID, DocumentNo, ActionType, Summary, "   //  1..4
			+ "MailSubject,MailText, C_BPartner_ID,C_BPartner_Contact_ID, "		//  5..8
			+ "AD_User_ID, UpdatedBy "                                         	//  9..10
			+ "FROM R_Request WHERE R_Request_ID=?";
		try
		{
			PreparedStatement pstmt = DB.prepareStatement(sql);
			pstmt.setInt(1, pi.Record_ID);
			ResultSet rs = pstmt.executeQuery();
			if (rs.next())
			{
				AD_Client_ID = rs.getInt(1);
				DocumentNo = rs.getString(2);
				ActionType = rs.getString(3);
				Summary = rs.getString(4);
				MailSubject = rs.getString(5);
				MailText = rs.getString(6);
				C_BPartner_ID = rs.getInt(7);
				C_BPartner_Contact_ID = rs.getInt(8);
				AD_User_ID = rs.getInt(9);
				UpdatedBy = rs.getInt(10);
			}
			rs.close();
			pstmt.close();
		}
		catch (SQLException e)
		{
			log.error("startProcess", e);
			return false;
		}

		if (ActionType == null)
			return true;

		//  Send EMail
		if (ActionType.equals(A_EMAIL))
		{
			String smtpHost = EMail.getSmtpHost(AD_Client_ID);
			String from = EMail.getEMailOfUser(AD_User_ID);
			String to = getEMailOfBPartner(C_BPartner_ID, C_BPartner_Contact_ID);
			//
			EMail email = new EMail(smtpHost, from, to, MailSubject, MailText);
			String msg = email.send();
			if (EMail.SENT_OK.equals(msg))
				updateRequest (pi.Record_ID, Msg.getMsg(ctx, "RequestActionEMailOK"));
			else
			{
				updateRequest (pi.Record_ID, Msg.getMsg(ctx, "RequestActionEMailError") + " - " + msg);
				return false;
			}
		}

		//  Send Transfer EMail
		else if (ActionType.equals(A_TRANSFER))
		{
			//  RequestActionTransfer - Request {0} was transfered by {1} to {2}
			Object[] args = new Object[] {DocumentNo, EMail.getNameOfUser(UpdatedBy), EMail.getNameOfUser(AD_User_ID)};
			String subject = Msg.getMsg(Env.getAD_Language(ctx), "RequestActionTransfer", args);
			updateRequest (pi.Record_ID, subject);
			//  Optional Info mail
			String smtpHost = EMail.getSmtpHost(AD_Client_ID);
			String to = EMail.getEMailOfUser(AD_User_ID);
			String from = EMail.getEMailOfUser(UpdatedBy);
			String message = subject + "\n" + Summary;
			//
			EMail email = new EMail(smtpHost, from, to, subject, message);
			String msg = email.send();
		}

		return true;
	}   //  startProcess

	/**
	 *  Update result of Request
	 *
	 *  @param  R_Request_ID    Request to be updated
	 *  @param  result          new result
	 *  @return true if request was updated
	 */
	private boolean updateRequest (int R_Request_ID, String result)
	{
		StringBuffer sql = new StringBuffer ("UPDATE R_Request SET Result=");
		String msg = DB.TO_STRING(result);  //  in ''
		if (msg.length() < 258)             //  max length 255
			sql.append(msg);
		else
			sql.append(msg.substring(0,256)).append("'");
		sql.append(" WHERE R_Request_ID=").append(R_Request_ID);
		int no = DB.executeUpdate(sql.toString());
		if (no != 1)
		{
			log.error("updateRequest - not updated - R_Request_ID=" + R_Request_ID);
			return false;
		}
		return true;
	}   //  updateRequest

}   //  RequestAction

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -