Difference between revisions of "Process"

From iDempiere en
Line 134: Line 134:
  
 
=See Also=
 
=See Also=
[Script Process]
+
[[Script Process]]
  
 
[[Category:Development]]
 
[[Category:Development]]
 
[[Category:Code snippets]]
 
[[Category:Code snippets]]

Revision as of 14:33, 3 November 2013

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.
  • 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

Template Process

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 {
              
		}
	}

}

See Also

Script Process

Cookies help us deliver our services. By using our services, you agree to our use of cookies.