gt: 				if (count == 0)
context: /******************************************************************************
   * Product: Adempiere ERP & CRM Smart Business Solution                       *
   * 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    *
   * Copyright (C) 2003-2007 e-Evolution,SC. All Rights Reserved.               *
   * Contributor(s): Victor Perez www.e-evolution.com                           *
   *****************************************************************************/
  package org.eevolution.process;
  
  /*
   * #%L
   * de.metas.adempiere.libero.libero
   * %%
   * Copyright (C) 2015 metas GmbH
   * %%
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as
   * published by the Free Software Foundation, either version 2 of the
   * License, or (at your option) any later version.
   * 
   * 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, see
   * <http://www.gnu.org/licenses/gpl-2.0.html>.
   * #L%
   */
  
  
  import java.math.BigDecimal;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.util.Properties;
  
  import javax.sql.RowSet;
  
  import org.compiere.model.MQuery;
  import org.compiere.model.MQuery.Operator;
  import org.compiere.model.PrintInfo;
  import org.compiere.print.MPrintFormat;
  import org.compiere.print.ReportCtl;
  import org.compiere.print.ReportEngine;
  import org.compiere.util.DB;
  import org.compiere.util.Env;
  import org.compiere.util.ValueNamePair;
  import org.eevolution.model.X_T_BOMLine;
  
  import de.metas.i18n.Language;
  import de.metas.logging.MetasfreshLastError;
  import de.metas.process.ProcessInfoParameter;
  import de.metas.process.JavaProcess;
  
  /**
   * Multi-Level BOM & Formula Detail
   * 
   * @author victor.perez@e-evolution.com,Sergio Ramazzina
   * @version $Id: PrintBOM.java,v 1.2 2005/04/19 12:54:30 srama Exp $
   * 
   */
  public class PrintBOM extends JavaProcess
  {
  	private static final Properties ctx = Env.getCtx();
  	private int p_M_Product_ID = 0;
  	private boolean p_implosion = false;
  	private int LevelNo = 1;
  	private int SeqNo = 0;
  	private String levels = new String("....................");
  	private int AD_PInstance_ID = 0;
  
  	/**
  	 * Prepare - e.g., get Parameters.
  	 */
  	@Override
  	protected void prepare()
  	{
  		ProcessInfoParameter[] para = getParametersAsArray();
  
  		for (int i = 0; i < para.length; i++)
  		{
  			String name = para[i].getParameterName();
  			if (para[i].getParameter() == null)
  				;
  			else if (name.equals("M_Product_ID"))
  			{
  				p_M_Product_ID = ((BigDecimal) para[i].getParameter()).intValue();
  			}
  			else if (name.equals("Implosion"))
  			{
  				p_implosion = ((String) para[i].getParameter()).equals("N") ? false : true;
  			}
  			else
  				log.error("prepare - Unknown Parameter: " + name);
  		}
  	} // prepare
  
  	/**
  	 * Perform process.
  	 * 
  	 * @return Message (clear text)
  	 * @throws Exception
  	 *             if not successful
  	 */
  	@Override
  	protected String doIt() throws Exception
  	{
  		AD_PInstance_ID = getAD_PInstance_ID();
  
  		try 
  		{
  			loadBOM();
  			print();
  		}
  		catch (Exception e)
  		{
  			log.error("PrintBOM", e.toString());
  			throw new Exception(e.getLocalizedMessage());
  		}
  		finally
  		{
  			String sql = "DELETE FROM T_BomLine WHERE AD_PInstance_ID = " + AD_PInstance_ID;
  			DB.executeUpdate(sql, null);		
  		}
  
  		return "@OK@";
  	} // doIt
  
  	private static final int X_RV_PP_Product_BOMLine_Table_ID = 53063;
  	private static final String X_RV_PP_Product_BOMLine_Table_Name = "RV_PP_Product_BOMLine";
  	
  	/**
  	 * Print result generate for this report
  	 */
  	void print() throws Exception
  	{
  		Language language = Env.getLanguage(getCtx()); // Base Language
  		MPrintFormat pf = null;
  		int pfid = 0;
  		
  		// get print format for client, else copy system to client  
  		RowSet pfrs = MPrintFormat.getAccessiblePrintFormats(X_RV_PP_Product_BOMLine_Table_ID, -1, null);
  		pfrs.next();
  		pfid = pfrs.getInt("AD_PrintFormat_ID");
  		
  		if(pfrs.getInt("AD_Client_ID") != 0) pf = MPrintFormat.get(getCtx(), pfid, false);
  		else pf = MPrintFormat.copyToClient(getCtx(), pfid, getAD_Client_ID());
  		pfrs.close();		
  
  		if (pf == null) raiseError("Error: ","No Print Format");
  
  		pf.setLanguage(language);
  		pf.setTranslationLanguage(language);
  		// query
  		MQuery query = MQuery.get(getCtx(), AD_PInstance_ID, X_RV_PP_Product_BOMLine_Table_Name);
  		query.addRestriction("AD_PInstance_ID", Operator.EQUAL, AD_PInstance_ID);
  
  		PrintInfo info = new PrintInfo(X_RV_PP_Product_BOMLine_Table_Name, 
  				X_RV_PP_Product_BOMLine_Table_ID, getRecord_ID());
  		ReportEngine re = new ReportEngine(getCtx(), pf, query, info);
  
  		ReportCtl.preview(re);
  		// wait for report window to be closed as t_bomline   
  		// records are deleted when process ends 
  		while (re.getView().isDisplayable()) 
  		{
  			Env.sleep(1);
  		}	
  	}
  
  	/**
  	 * Action: Fill Tree with all nodes
  	 */
  	private void loadBOM() throws Exception
  	{
  		int count = 0;
  		if (p_M_Product_ID == 0) 
  			raiseError("Error: ","Product ID not found");
  
  		X_T_BOMLine tboml = new X_T_BOMLine(ctx, 0, null);
  		tboml.setPP_Product_BOM_ID(0);
  		tboml.setPP_Product_BOMLine_ID(0);
  		tboml.setM_Product_ID(p_M_Product_ID);
  		tboml.setSel_Product_ID(p_M_Product_ID);
  		tboml.setImplosion(p_implosion);
  		tboml.setLevelNo(0);
  		tboml.setLevels("0");
  		tboml.setSeqNo(0);
  		tboml.setAD_PInstance_ID(AD_PInstance_ID);
  		tboml.save();
  
  		if (p_implosion)
  		{
  			PreparedStatement stmt = null;
  			ResultSet rs = null;
  			String sql = "SELECT PP_Product_BOMLine_ID FROM PP_Product_BOMLine " 
  					+ "WHERE IsActive = 'Y' AND M_Product_ID = ? ";
  			try
  			{
  				stmt = DB.prepareStatement(sql, get_TrxName());
  				stmt.setInt(1, p_M_Product_ID);
  				rs = stmt.executeQuery();
  
  				while (rs.next())
  				{
  					parentImplotion(rs.getInt(1));
  					++count;
  				}
  				if (count == 0)
  					raiseError("Error: ","Product is not a component");
  			}
  			catch (SQLException e)
  			{
  				log.error(e.getLocalizedMessage() + sql, e);
  				throw new Exception("SQLException: "+e.getLocalizedMessage());
  			}
  			finally
  			{
  				DB.close(rs, stmt);
  				rs = null;
  				stmt = null;
  			}
  		}
  		else
  		{
  			PreparedStatement stmt = null;
  			ResultSet rs = null;
  			String sql = "SELECT PP_Product_BOM_ID FROM PP_Product_BOM " 
  					+ "WHERE IsActive = 'Y' AND M_Product_ID = ? ";
  			try
  			{
  				stmt = DB.prepareStatement(sql, get_TrxName());
  				stmt.setInt(1, p_M_Product_ID);
  				rs = stmt.executeQuery();
  				while (rs.next())
  				{
  					parentExplotion(rs.getInt(1));
  					++count;
  				}
  