Difference between revisions of "General Code"
From iDempiere en
Ngordon7000 (talk | contribs) |
Ngordon7000 (talk | contribs) |
||
| Line 68: | Line 68: | ||
* Adapted from FinReportJasper | * Adapted from FinReportJasper | ||
* Sample call: NTierUtils.startJavaProcess(Env.getCtx(), get_TrxName(), "MyJasperReportProcess"); | * Sample call: NTierUtils.startJavaProcess(Env.getCtx(), get_TrxName(), "MyJasperReportProcess"); | ||
| − | |||
<nowiki>public static void startJavaProcess(Properties ctx, String trxName, | <nowiki>public static void startJavaProcess(Properties ctx, String trxName, | ||
| − | String processId) { | + | String processId, int recordId ) { |
int AD_Process_ID = MProcess.getProcess_ID( | int AD_Process_ID = MProcess.getProcess_ID( | ||
processId, null); | processId, null); | ||
| + | if ( AD_Process_ID <= 0 ) { | ||
| + | throw new AdempiereException( String.format("Could not find report: - '%s'", processId) ); | ||
| + | } | ||
MPInstance instance = new MPInstance(Env.getCtx(), | MPInstance instance = new MPInstance(Env.getCtx(), | ||
AD_Process_ID, 0); | AD_Process_ID, 0); | ||
| Line 81: | Line 83: | ||
ProcessInfo poInfo = new ProcessInfo( | ProcessInfo poInfo = new ProcessInfo( | ||
processId, AD_Process_ID); | processId, AD_Process_ID); | ||
| − | + | ProcessInfoParameter piClientId = new ProcessInfoParameter( | |
| − | + | "AD_CLIENT_ID", new BigDecimal(Env.getAD_Client_ID(ctx)), | |
| + | null, null, null); | ||
ProcessInfoParameter piOrgId = new ProcessInfoParameter( | ProcessInfoParameter piOrgId = new ProcessInfoParameter( | ||
"AD_ORG_ID", new BigDecimal(Env.getAD_Org_ID(ctx)), null, | "AD_ORG_ID", new BigDecimal(Env.getAD_Org_ID(ctx)), null, | ||
null, null); | null, null); | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
ProcessInfoParameter[] piParameters = new ProcessInfoParameter[] { | ProcessInfoParameter[] piParameters = new ProcessInfoParameter[] { | ||
| − | piClientId, piOrgId | + | piClientId, piOrgId }; |
poInfo.setParameter(piParameters); | poInfo.setParameter(piParameters); | ||
| − | poInfo.setRecord_ID( | + | poInfo.setRecord_ID( recordId ); |
// need to commit in order to allow jasper to view the data | // need to commit in order to allow jasper to view the data | ||
Revision as of 16:49, 24 August 2015
Chat
Quickly add a chat to any PO (table) record.
package za.co.ntier.common;
import java.util.Properties;
import java.util.logging.Level;
import org.compiere.model.MChat;
import org.compiere.model.MChatEntry;
import org.compiere.model.PO;
import org.compiere.model.Query;
import org.compiere.util.CLogger;
import org.compiere.util.Env;
/**
* @author nTier Software Services ngordon7000
* Date: 29 Jun 2015
* Description: Chat support
*/
public class NTierChatUtils {
/** Logger */
private static CLogger log = CLogger.getCLogger(NTierChatUtils.class);
/**
*/
public static MChat getChat(Properties ctx, String trxName,
PO po) {
final String whereClause = "AD_Client_ID=? and AD_Table_ID=? AND Record_ID=? ";
MChat retValue = new Query(ctx, MChat.Table_Name, whereClause,
trxName).setParameters(Env.getAD_Client_ID(ctx), po.get_Table_ID(), po.get_ID())
.firstOnly();
return retValue;
}
/**
* Add chat
*/
public static void addChat(Properties ctx, String trxName,
PO po, String comments) {
try {
String descr = "";
descr = comments;
descr = NTierStringUtils.left(descr, 255);
MChat chat = getChat(ctx, trxName, po);
if (chat == null) {
chat = new MChat (ctx, po.get_Table_ID(), po.get_ID(), descr, trxName);
chat.saveEx(trxName);
}
log.info(String.format("Chat - %s - '%s'", po.get_TableName(), descr));
MChatEntry chatEntry = new MChatEntry(chat, descr);
chatEntry.saveEx(trxName);
} catch (Exception e) {
log.log(Level.SEVERE, "^^^", e);
log.info(String.format("^^^ Failed to save chat record"));
}
}
}
Start a process (or report)
- Adapted from FinReportJasper
- Sample call: NTierUtils.startJavaProcess(Env.getCtx(), get_TrxName(), "MyJasperReportProcess");
public static void startJavaProcess(Properties ctx, String trxName,
String processId, int recordId ) {
int AD_Process_ID = MProcess.getProcess_ID(
processId, null);
if ( AD_Process_ID <= 0 ) {
throw new AdempiereException( String.format("Could not find report: - '%s'", processId) );
}
MPInstance instance = new MPInstance(Env.getCtx(),
AD_Process_ID, 0);
instance.saveEx();
ProcessInfo poInfo = new ProcessInfo(
processId, AD_Process_ID);
ProcessInfoParameter piClientId = new ProcessInfoParameter(
"AD_CLIENT_ID", new BigDecimal(Env.getAD_Client_ID(ctx)),
null, null, null);
ProcessInfoParameter piOrgId = new ProcessInfoParameter(
"AD_ORG_ID", new BigDecimal(Env.getAD_Org_ID(ctx)), null,
null, null);
ProcessInfoParameter[] piParameters = new ProcessInfoParameter[] {
piClientId, piOrgId };
poInfo.setParameter(piParameters);
poInfo.setRecord_ID( recordId );
// need to commit in order to allow jasper to view the data
Trx trx = Trx.get(trxName, true);
// Rather let the calling process commit
//trx.commit();
ProcessUtil.startJavaProcess(ctx, poInfo, trx);
}
Database
Call a database procedure
- From FinReportJasper in the base
// CarlosRuiz - globalqss - allow procedure preprocess
if (proc.getProcedureName() != null && proc.getProcedureName().length() > 0) {
// execute on this thread/connection
String sql = "{call " + proc.getProcedureName() + "(?)}";
CallableStatement cstmt = null;
try
{
cstmt = DB.prepareCall(sql); // ro??
cstmt.setInt(1, getAD_PInstance_ID());
cstmt.executeUpdate();
}
catch (Exception e)
{
log.log(Level.SEVERE, sql, e);
poInfo.setSummary (Msg.getMsg(Env.getCtx(), "ProcessRunError") + " " + e.getLocalizedMessage());
}
finally
{
DB.close(cstmt);
cstmt = null;
}
}
Credits
Initial contribution by: nTier Software Services http://www.ntier.co.za ngordon7000
Add your credit here.
