Difference between revisions of "Howto write an Adempiere process"

From ADempiere
Jump to: navigation, search
This Wiki is read-only for reference purposes to avoid broken links.
(add postProcess())
m (wikify)
 
(One intermediate revision by one other user not shown)
Line 3: Line 3:
 
<pre>
 
<pre>
 
/******************************************************************************
 
/******************************************************************************
  * Product: Adempiere ERP & CRM Smart Business Solution                       *
+
  * Product: Adempiere ERP & CRM Smart Business Solution                       *
 
  * Copyright (C) 1999-2006 ComPiere, Inc. All Rights Reserved.                *
 
  * Copyright (C) 1999-2006 ComPiere, Inc. All Rights Reserved.                *
 
  * This program is free software; you can redistribute it and/or modify it    *
 
  * This program is free software; you can redistribute it and/or modify it    *
Line 26: Line 26:
 
import org.compiere.process.ProcessInfoParameter;
 
import org.compiere.process.ProcessInfoParameter;
 
import org.compiere.process.SvrProcess;
 
import org.compiere.process.SvrProcess;
import org.eevolution.model.GenericPO;
 
  
 
public class TemplateProcess extends SvrProcess {
 
public class TemplateProcess extends SvrProcess {
Line 71: Line 70:
 
// you can also retrieve the id of the current record for processes called from a window
 
// you can also retrieve the id of the current record for processes called from a window
 
int recordId = getRecord_ID();
 
int recordId = getRecord_ID();
if ( recordId > 0 )
 
record = new GenericPO(getCtx(), recordId, get_TrxName());
 
 
 
}
 
}
 
 
Line 81: Line 77:
 
@Override
 
@Override
 
protected String doIt() throws Exception {
 
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 (preferably indicating success or failure)";
+
return "A message to the user (indicating success - failures must throw Exceptions)";
 
}
 
}
  
Line 97: Line 101:
 
@Override
 
@Override
 
protected void postProcess(boolean success) {
 
protected void postProcess(boolean success) {
if(success) {
+
if (success) {
 
 
 
} else {
 
} else {
 
                
 
                
                }
+
}
 
}
 
}
  
 
}
 
}
 
</pre>
 
</pre>
 +
 +
[[Category:Developer documentation]]

Latest revision as of 17:13, 9 October 2009

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

}