Talk:Howto use a Button to Call a Database Function

From ADempiere
Jump to: navigation, search
This Wiki is read-only for reference purposes to avoid broken links.
   My dear friend :: 
   The process call procedure/function only one parameter is ad_pinstance_id,
   ProcessInfoParameter will be read from DB procedure/function by ad_pinstance_id.
   Don't let developer confuse.
   Thanks
   Albert 2011.4.28

I use this instruction to call db function, it's not work, because it only pass AD_PInstance_ID parameter to db function, so I modify this file ProcessUtil.java StartDatabaseProcedure to

	public static boolean startDatabaseProcedure (ProcessInfo pi, String ProcedureName, Trx trx, boolean managedTrx) {
		// Add process parameters
		ProcessInfoParameter[] para = pi.getParameter();
		if (para == null) {
			ProcessInfoUtil.setParameterFromDB(pi);
			para = pi.getParameter();
		}
		String trxName = trx != null ? trx.getTrxName() : null;
		String sql = "";
		if (para != null) {
			sql = "{call " + ProcedureName + "(?";
			for (int i = 0; i < para.length; i++)
			{
				sql = sql + ",?";
			}
			sql = sql + ")}";
		}
		try
		{
			CallableStatement cstmt = DB.prepareCall(sql, ResultSet.CONCUR_UPDATABLE, trxName);	
			cstmt.setInt(1, pi.getAD_PInstance_ID());
			if (para != null) {
				for (int i = 0; i < para.length; i++)
				{
					Object value = para[i].getParameter();
					if (value instanceof BigDecimal){
						cstmt.setLong(i+2, ((BigDecimal)value).intValue());
					}
					if (value instanceof Boolean){
						cstmt.setBoolean(i+2, ((Boolean)value).booleanValue());
					}
					if (value instanceof String){
						cstmt.setString(i+2, ((String)value).toString());
					}
				}
			}
			cstmt.executeUpdate();
			cstmt.close();
			if (trx != null && trx.isActive() && managedTrx)
			{
				trx.commit(true);
			}
		}
		catch (Exception e)
		{
			log.log(Level.SEVERE, sql, e);
			if (trx != null && trx.isActive() && managedTrx)
			{
				trx.rollback();
			}
			pi.setSummary (Msg.getMsg(Env.getCtx(), "ProcessRunError") + " " + e.getLocalizedMessage());
			pi.setError (true);
			return false;
		}
		finally
		{
			if (trx != null && managedTrx)
				trx.close();
		}
		return true;
	}

and it work fine now.

at the end of executing the function, you must set the result of ad_pinstance table to 1, without this, the client UI will be freezed.