Process

From iDempiere en

Process

  • An iDempiere Process is a piece of code which run on a user interface action such as clicking on a button.
  • A process is a java class that extends org.compiere.process.SvrProcess. It is linked to a button in the ADempiere client and executed when the user hits this button or to a menu entry.
  • It has two methods prepare() - to check process parameters - and doIt() - to do the work.
  • For complex logic that may take some time

Methods

prepare() method

  • prepare() method is for getting the parameters into variables.
  • The prepare function is called first and is used to load parameters which are passed to the process by the framework.
  • Parameters to be passed are configured in Report & Process -> Parameter.
  • To load parameters, you can use:
  ProcessInfoParameter[] para = getParameter(); 
  for (int i = 0; i < para.length; i++)
  { String name = para[i].getParameterName(); 
    if (para[i].getParameter() == null)
    else System.out.println(name); 
  }
  • Getting Record_ID is just working for buttons. To retrieve the id of the current record for processes called from a window, you can use:
   int recordId = getRecord_ID();

doIt() method

  • doIt() method for execution of the process
  • Commonly the doIt method firstly do some validations on the parameters and throws AdempiereUserException or AdempiereSystemException if errors found
  • After this the process code is written and on any error an Exception must be thrown
  • Use the addLog method to register important information about the running of your process
  • return a message to the user indicating success or failure. For failures must throw Exceptions.

Steps to Create a Process

  • Step 1: Create a java class extends org.compiere.process.SvrProcess
  • Step 2: Implements two methods: void doIt() , String prepare()
    • In Prepare() method you can get different parameters
    • In doIt method you try to insert this parameters into your table after you create this class Login with system administrator role
  • Step 3: 'Report and Process' window define your process with parameter
  • Step 4: 'Table and Column' window choose your table and create a column with Button Reference
  • Step 5: Choose your process for this Button


Template Process 1

Here's a template process:

/******************************************************************************
 * Product: Adempiere ERP & CRM Smart Business Solution                       *
 * Copyright (C) 1999-2006 ComPiere, Inc. All Rights Reserved.                *
 * This program is free software; you can redistribute it and/or modify it    *
 * under the terms version 2 of the GNU General Public License as published   *
 * by the Free Software Foundation. This program 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 General Public License for more details.                       *
 * You should have received a copy of the GNU General Public License along    *
 * with this program; if not, write to the Free Software Foundation, Inc.,    *
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.                     *
 * For the text or an alternative of this public license, you may reach us    *
 * ComPiere, Inc., 2620 Augustine Dr. #245, Santa Clara, CA 95054, USA        *
 * or via info@compiere.org or http://www.compiere.org/license.html           *
 *****************************************************************************/
package org.compiere.process;

import java.math.BigDecimal;
import java.util.Date;

import org.compiere.model.PO;
import org.compiere.process.ProcessInfoParameter;
import org.compiere.process.SvrProcess;

public class TemplateProcess extends SvrProcess {


	private boolean boolParam;
	private Date dateParam;
	private String rangeFrom;
	private String rangeTo;
	private int intParam;
	private BigDecimal bigDecParam;
	private PO record;
	
	/**
	 * The prepare function is called first and is used to load parameters
	 * which are passed to the process by the framework. Parameters to be
	 * passed are configured in Report & Process -> Parameter.
	 * 
	 */
	@Override
	protected void prepare() {

		// Each Report & Process parameter name is set by the field DB Column Name
		for ( ProcessInfoParameter para : getParameter())
		{
			if ( para.getParameterName().equals("isBooleanParam") )
				boolParam = "Y".equals((String) para.getParameter());			// later versions can use getParameterAsString
			else if ( para.getParameterName().equals("dateParam") )
				dateParam = (Date) para.getParameter();
			// parameters may also specify the start and end value of a range
			else if ( para.getParameterName().equals("rangeParam") )
			{
				rangeFrom = (String) para.getParameter();
				rangeTo = (String) para.getParameter_To();
			}
			else if ( para.getParameterName().equals("intParam") )
				intParam = para.getParameterAsInt();
			else if ( para.getParameterName().equals("bigDecParam") )
				bigDecParam = (BigDecimal) para.getParameter();
			else 
				log.info("Parameter not found " + para.getParameterName());
		}

		// you can also retrieve the id of the current record for processes called from a window
		int recordId = getRecord_ID();
	}
	
	/**
	 * The doIt method is where your process does its work
	 */
	@Override
	protected String doIt() throws Exception {

		/* Commonly the doIt method firstly do some validations on the parameters
		   and throws AdempiereUserException or AdempiereSystemException if errors found
		
		   After this the process code is written and on any error an Exception must be thrown
		   Use the addLog method to register important information about the running of your process
		   This information is preserved in a log and shown to the user at the end.
		*/
		
		return "A message to the user (indicating success - failures must throw Exceptions)";
	}

	/**
	 * Post process actions (outside trx).
	 * Please note that at this point the transaction is committed so
	 * you can't rollback.
	 * This method is useful if you need to do some custom work when 
	 * the process complete the work (e.g. open some windows).
	 *  
	 * @param success true if the process was success
	 * @since 3.1.4
	 */
	@Override
	protected void postProcess(boolean success) {
		if (success) {
			
		} else {
              
		}
	}

}


Template Process 2

This is another template process provided by Trifon

package org.adempiere.process;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;

import org.compiere.model.MClient;
import org.compiere.model.MDocType;
import org.compiere.model.X_C_BPartner_EDI;
import org.compiere.model.X_C_EDIFormat;
import org.compiere.model.X_C_EDIProcessor;
import org.compiere.process.ProcessInfoParameter;
import org.compiere.process.SvrProcess;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.Msg;

/**
 *  @author Trifon Trifonov
 *  @version $Id:$
 */
public class ModelImporter extends SvrProcess
{
	/** Client Parameter			*/
	protected int	p_AD_Client_ID = 0;
	
	/** Business Partner Parameter */
	protected int p_C_BPartner_ID = 0;
	
	/** Document Type Parameter */
	protected int p_C_DocType_ID = 0;
	
	/** Record ID */
	protected int p_Record_ID = 0;
	
	/** Table ID */
	int AD_Table_ID = 0;
	
	/**
	 * 	Get Parameters
	 */
	protected void prepare ()
	{
		
		p_Record_ID = getRecord_ID();
		if (p_AD_Client_ID == 0)
			p_AD_Client_ID = Env.getAD_Client_ID(getCtx());
		AD_Table_ID = getTable_ID();
		
		StringBuffer sb = new StringBuffer ("AD_Table_ID=").append(AD_Table_ID);
		sb.append("; Record_ID=").append(getRecord_ID());
		//	Parameter
		ProcessInfoParameter[] para = getParameter();
		for (int i = 0; i < para.length; i++)
		{
			String name = para[i].getParameterName();
			if (para[i].getParameter() == null)
				;
			else if (name.equals("C_BPartner_ID"))
				p_C_BPartner_ID = para[i].getParameterAsInt();
			else
				log.log(Level.SEVERE, "Unknown Parameter: " + name);
		}
		
		log.info(sb.toString());
	}

	/**
	 * 	Process 
	 *	@return info
	 */
	protected String doIt () throws Exception
	{
		StringBuffer result = new StringBuffer("");
		
		MClient client = MClient.get (getCtx(), p_AD_Client_ID);
		log.info(client.toString());
		// get proper EDI Format from Document Type
		MDocType docType = MDocType.get(getCtx(), p_C_DocType_ID);
		
		int C_EDIFormat_ID = 0;
		int C_EDIProcessor_ID = 0;
		String sql1 = "SELECT C_EDIFormat_ID, C_EDIProcessor_ID "
			   + "FROM " + X_C_BPartner_EDI.Table_Name + " "
			   + "WHERE AD_Client_ID = ? "
			   + " AND C_BPartner_ID = ? "
			   + " AND C_DocType_ID = ? "
			   + " AND Inbound = 'N' " 
		;
		ResultSet rs1 = null;
		PreparedStatement pstmt1 = null;
		try
		{
			pstmt1 = DB.prepareStatement(sql1, get_TrxName());
			pstmt1.setInt(1, p_AD_Client_ID);
			pstmt1.setInt(2, p_C_BPartner_ID);
			pstmt1.setInt(3, p_C_DocType_ID);
			rs1 = pstmt1.executeQuery();
			if (rs1.next())
			{
				// Found specific C_EDIFormat for given BPartner and C_DocType_ID
				C_EDIFormat_ID = rs1.getInt(X_C_EDIFormat.COLUMNNAME_C_EDIFormat_ID);
				C_EDIProcessor_ID = rs1.getInt(X_C_EDIProcessor.COLUMNNAME_C_EDIProcessor_ID);
			} else {
				// Get C_EDIFormat_ID from Document Type
				C_EDIFormat_ID = docType.getC_EDIFormat_ID();
			}
			
		} finally {
			try {
				if (rs1 != null) rs1.close();
				if (pstmt1 != null) pstmt1.close();
			} catch (SQLException ex) {/*ignored*/}
			rs1 = null;
			pstmt1 = null;
		}
		if (C_EDIFormat_ID == 0) {
			throw new Exception("EDI Format is not set for C_DocType_ID = [" + p_C_DocType_ID + "]");
		}
		if (C_EDIProcessor_ID == 0) {
			throw new Exception("EDI Processor is not set for C_DocType_ID = [" + p_C_DocType_ID + "]");
		}
		
		addLog(0, null, null, Msg.getMsg (getCtx(), "EDISubmitProcessResult") + "\n" + result.toString());
		return result.toString();
	}
}

See Also

Script Process