Difference between revisions of "General Code"
From iDempiere en
Ngordon7000 (talk | contribs) m (→Chat) |
Ngordon7000 (talk | contribs) m (Ngordon7000 moved page Java: General to General Code: Tidy up main page) |
||
| (11 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
[[Category:Code Scratchpad]] | [[Category:Code Scratchpad]] | ||
| + | __FORCETOC__ | ||
= Chat = | = Chat = | ||
| − | Quickly add a chat to | + | Quickly add a chat to any PO (table) record. |
<nowiki>package za.co.ntier.common; | <nowiki>package za.co.ntier.common; | ||
| Line 18: | Line 19: | ||
/** | /** | ||
| − | |||
| − | |||
* Description: Chat support | * Description: Chat support | ||
*/ | */ | ||
| Line 62: | Line 61: | ||
} | } | ||
</nowiki> | </nowiki> | ||
| + | |||
| + | = Start a process (or report) = | ||
| + | |||
| + | * See FinReportJasper in the base code, for an example of how this is done. | ||
| + | |||
| + | = Database = | ||
| + | |||
| + | == Call a database procedure == | ||
| + | |||
| + | * From FinReportJasper in the base | ||
| + | |||
| + | <nowiki>// 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; | ||
| + | } | ||
| + | }</nowiki> | ||
| + | |||
| + | = nTierConfigurator = | ||
| + | |||
| + | == Example use == | ||
| + | |||
| + | <nowiki>public static int getSequenceNumberNext(Properties ctx, String trxName) { | ||
| + | MSysConfig config = NTierConfigurator.newOrGet(ctx, trxName, false, | ||
| + | "NTIER_SEQUENCE_NEXT", "5000", "Next sequence number"); | ||
| + | int r = config.get_ValueAsInt("Value"); | ||
| + | config.setValue( (new Integer(r+1)).toString() ); | ||
| + | config.saveEx(); | ||
| + | return r; | ||
| + | } | ||
| + | |||
| + | public static boolean isRequireUniqueSequence(Properties ctx, String trxName) { | ||
| + | return NTierConfigurator.newOrGetBoolean(ctx, trxName, false, | ||
| + | "NTIER_REQUIRE_UNIQUE_SEQUENCE", "Y", "Require a unique sequence number?"); | ||
| + | } | ||
| + | |||
| + | public static String getDevelopmentMode(Properties ctx, String trxName) { | ||
| + | return NTierConfigurator.newOrGetString(ctx, trxName, false, | ||
| + | "NTIER_DEVELOPMENT_MODE", "Test", "Development mode - Test or Live"); | ||
| + | } | ||
| + | |||
| + | public static Timestamp getFirstDayDateOfCurrentFinancialYear(Properties ctx, String trxName) { | ||
| + | MSysConfig config = NTierConfigurator.newOrGet(ctx, trxName, false, | ||
| + | "NTIER_FIRST_DAY_CURRENT_YEAR", "2014-03-01", "First day of current financial year: for take-on"); | ||
| + | String v = config.get_ValueAsString("Value"); | ||
| + | Timestamp r = NTierDateUtils.parse(v, "yyyy-MM-dd") ; | ||
| + | return r; | ||
| + | } | ||
| + | |||
| + | |||
| + | |||
| + | public static boolean isHasAssetAdditions(Properties ctx, String trxName, int assetID, String docStatus) { | ||
| + | int i = getAssetAdditions(ctx, trxName, assetID, docStatus).length; | ||
| + | return i>0; | ||
| + | } </nowiki> | ||
| + | |||
| + | == Class == | ||
| + | |||
| + | <nowiki>/****************************************************************************** | ||
| + | * Copyright (C) 2015 nTier Software Services http://www.ntier.co.za * | ||
| + | * 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. * | ||
| + | *****************************************************************************/ | ||
| + | package za.co.ntier.common; | ||
| + | |||
| + | import java.sql.PreparedStatement; | ||
| + | import java.sql.ResultSet; | ||
| + | import java.sql.SQLException; | ||
| + | import java.util.Properties; | ||
| + | import java.util.logging.Level; | ||
| + | |||
| + | import org.adempiere.exceptions.AdempiereException; | ||
| + | import org.compiere.model.MSysConfig; | ||
| + | import org.compiere.model.Query; | ||
| + | import org.compiere.util.CLogger; | ||
| + | import org.compiere.util.DB; | ||
| + | import org.compiere.util.Env; | ||
| + | |||
| + | /** | ||
| + | * (c) 2015 nTier Software Services | ||
| + | * @author Neil Gordon | ||
| + | * Date: 24 Feb 2015 | ||
| + | * Description: Manage configurator settings | ||
| + | */ | ||
| + | public class NTierConfigurator { | ||
| + | |||
| + | /** Logger */ | ||
| + | private static CLogger log = CLogger.getCLogger(NTierConfigurator.class); | ||
| + | /** | ||
| + | * Get specified MSysConfig record | ||
| + | * @param ctx context | ||
| + | * @param ID | ||
| + | * @return record | ||
| + | */ | ||
| + | public static MSysConfig getConfigurator(Properties ctx, String trxName, String name) { | ||
| + | int AD_Client_ID = Env.getContextAsInt(ctx, "#AD_Client_ID"); | ||
| + | MSysConfig po = new Query(ctx, MSysConfig.Table_Name, "ad_client_id=? and name=?", trxName) | ||
| + | .setParameters(AD_Client_ID, name).setOnlyActiveRecords(true).first(); | ||
| + | return po; | ||
| + | } | ||
| + | |||
| + | /** | ||
| + | * Get the configurator setting | ||
| + | */ | ||
| + | public static String getConfiguratorSettingValue(String name, String defaultValue) { | ||
| + | String result = MSysConfig.getValue(name, defaultValue ); | ||
| + | return result; | ||
| + | } | ||
| + | |||
| + | /** | ||
| + | * Get the configurator setting - specified Client, or System Client. | ||
| + | * Note, Client will override setting in System if both exist. | ||
| + | */ | ||
| + | public static String getConfiguratorSettingValue(String name, String defaultValue, int AD_Client_ID) { | ||
| + | String result = MSysConfig.getValue(name, defaultValue, AD_Client_ID ); | ||
| + | return result; | ||
| + | } | ||
| + | |||
| + | /** | ||
| + | * Does the specified setting exist? | ||
| + | */ | ||
| + | public static boolean isSettingExists(Properties ctx, String trxName, String name) { | ||
| + | return isSettingExists(ctx, trxName, name, 0, 0); | ||
| + | } | ||
| + | |||
| + | /** | ||
| + | * Does the specified setting exist? | ||
| + | */ | ||
| + | public static boolean isSettingExists(Properties ctx, String trxName, String name, int AD_Client_ID, int AD_Org_ID) | ||
| + | { | ||
| + | // | ||
| + | String sql = "SELECT Value FROM AD_SysConfig" | ||
| + | + " WHERE Name=? AND AD_Client_ID IN (0, ?) AND AD_Org_ID IN (0, ?) " //AND IsActive='Y' | ||
| + | + " ORDER BY AD_Client_ID DESC, AD_Org_ID DESC"; | ||
| + | PreparedStatement pstmt = null; | ||
| + | ResultSet rs = null; | ||
| + | try | ||
| + | { | ||
| + | pstmt = DB.prepareStatement(sql, null); | ||
| + | pstmt.setString(1, name); | ||
| + | pstmt.setInt(2, AD_Client_ID); | ||
| + | pstmt.setInt(3, AD_Org_ID); | ||
| + | rs = pstmt.executeQuery(); | ||
| + | if (rs.next()) | ||
| + | return true; | ||
| + | } | ||
| + | catch (SQLException e) | ||
| + | { | ||
| + | log.log(Level.SEVERE, "isSettingExists", e); | ||
| + | } | ||
| + | finally | ||
| + | { | ||
| + | DB.close(rs, pstmt); | ||
| + | rs = null; pstmt = null; | ||
| + | } | ||
| + | |||
| + | return false; | ||
| + | } | ||
| + | |||
| + | /** | ||
| + | * Create the given configurator setting | ||
| + | */ | ||
| + | public static void createConfiguratorSetting(Properties ctx, String trxName, boolean isSystem, String name, String value, String description) { | ||
| + | |||
| + | String confLevel; | ||
| + | Properties ctx1; | ||
| + | ctx1 = (Properties)ctx.clone(); | ||
| + | if ( isSystem ) { | ||
| + | if (isSettingExists(ctx, trxName, name)) | ||
| + | return; | ||
| + | ctx1.setProperty(Env.AD_CLIENT_ID, "0"); | ||
| + | confLevel = MSysConfig.CONFIGURATIONLEVEL_System; | ||
| + | } else { | ||
| + | if (isSettingExists(ctx, trxName, name, Env.getAD_Client_ID(ctx), 0)) | ||
| + | return; | ||
| + | confLevel = MSysConfig.CONFIGURATIONLEVEL_Client; | ||
| + | } | ||
| + | |||
| + | ctx1.setProperty(Env.AD_ORG_ID, "0"); | ||
| + | MSysConfig conf = new MSysConfig(ctx1, 0, null); | ||
| + | conf.setConfigurationLevel(confLevel); | ||
| + | conf.setName(name); | ||
| + | conf.setDescription(description); | ||
| + | conf.setValue(value); | ||
| + | conf.saveEx(); | ||
| + | //CacheMgt.get().reset(MSysConfig.Table_Name); | ||
| + | } | ||
| + | |||
| + | public static MSysConfig newOrGet(Properties ctx, String trxName, boolean isSystem, String name, String defaultValue, String description) { | ||
| + | NTierUtils.resetCacheForTable( MSysConfig.Table_Name ); | ||
| + | MSysConfig config; | ||
| + | Properties ctx1; | ||
| + | createConfiguratorSetting(ctx, trxName, isSystem, name, defaultValue, description); | ||
| + | ctx1 = (Properties)ctx.clone(); | ||
| + | if (isSystem) { | ||
| + | ctx1.setProperty(Env.AD_CLIENT_ID, "0"); | ||
| + | } else { | ||
| + | } | ||
| + | config = getConfigurator(ctx1, trxName, name); | ||
| + | if ( config==null ) { | ||
| + | throw new AdempiereException( String.format("Returned configurator setting is null (should've been created) - '%s'", name) ); | ||
| + | } | ||
| + | return config; | ||
| + | } | ||
| + | |||
| + | public static String newOrGetString(Properties ctx, String trxName, boolean isSystem, String name, String defaultValue, String description) { | ||
| + | MSysConfig config; | ||
| + | config = newOrGet(ctx, trxName, isSystem, name, defaultValue, description); | ||
| + | String r = config.get_ValueAsString("Value"); | ||
| + | return r; | ||
| + | } | ||
| + | |||
| + | public static boolean newOrGetBoolean(Properties ctx, String trxName, boolean isSystem, String name, String defaultValue, String description) { | ||
| + | MSysConfig config; | ||
| + | config = newOrGet(ctx, trxName, isSystem, name, defaultValue, description); | ||
| + | boolean r = config.get_ValueAsBoolean("Value"); | ||
| + | return r; | ||
| + | } | ||
| + | |||
| + | public static int newOrGetInt(Properties ctx, String trxName, boolean isSystem, String name, String defaultValue, String description) { | ||
| + | MSysConfig config; | ||
| + | config = newOrGet(ctx, trxName, isSystem, name, defaultValue, description); | ||
| + | int r = config.get_ValueAsInt("Value"); | ||
| + | return r; | ||
| + | } | ||
| + | |||
| + | }</nowiki> | ||
| + | |||
= Credits = | = Credits = | ||
| − | Initial contribution by: nTier Software Services http://www.ntier.co.za | + | Initial contribution by: nTier Software Services http://www.ntier.co.za |
Add your credit here. | Add your credit here. | ||
Latest revision as of 10:27, 27 July 2021
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;
/**
* 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)
- See FinReportJasper in the base code, for an example of how this is done.
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;
}
}
nTierConfigurator
Example use
public static int getSequenceNumberNext(Properties ctx, String trxName) {
MSysConfig config = NTierConfigurator.newOrGet(ctx, trxName, false,
"NTIER_SEQUENCE_NEXT", "5000", "Next sequence number");
int r = config.get_ValueAsInt("Value");
config.setValue( (new Integer(r+1)).toString() );
config.saveEx();
return r;
}
public static boolean isRequireUniqueSequence(Properties ctx, String trxName) {
return NTierConfigurator.newOrGetBoolean(ctx, trxName, false,
"NTIER_REQUIRE_UNIQUE_SEQUENCE", "Y", "Require a unique sequence number?");
}
public static String getDevelopmentMode(Properties ctx, String trxName) {
return NTierConfigurator.newOrGetString(ctx, trxName, false,
"NTIER_DEVELOPMENT_MODE", "Test", "Development mode - Test or Live");
}
public static Timestamp getFirstDayDateOfCurrentFinancialYear(Properties ctx, String trxName) {
MSysConfig config = NTierConfigurator.newOrGet(ctx, trxName, false,
"NTIER_FIRST_DAY_CURRENT_YEAR", "2014-03-01", "First day of current financial year: for take-on");
String v = config.get_ValueAsString("Value");
Timestamp r = NTierDateUtils.parse(v, "yyyy-MM-dd") ;
return r;
}
public static boolean isHasAssetAdditions(Properties ctx, String trxName, int assetID, String docStatus) {
int i = getAssetAdditions(ctx, trxName, assetID, docStatus).length;
return i>0;
}
Class
/******************************************************************************
* Copyright (C) 2015 nTier Software Services http://www.ntier.co.za *
* 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. *
*****************************************************************************/
package za.co.ntier.common;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import org.adempiere.exceptions.AdempiereException;
import org.compiere.model.MSysConfig;
import org.compiere.model.Query;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;
/**
* (c) 2015 nTier Software Services
* @author Neil Gordon
* Date: 24 Feb 2015
* Description: Manage configurator settings
*/
public class NTierConfigurator {
/** Logger */
private static CLogger log = CLogger.getCLogger(NTierConfigurator.class);
/**
* Get specified MSysConfig record
* @param ctx context
* @param ID
* @return record
*/
public static MSysConfig getConfigurator(Properties ctx, String trxName, String name) {
int AD_Client_ID = Env.getContextAsInt(ctx, "#AD_Client_ID");
MSysConfig po = new Query(ctx, MSysConfig.Table_Name, "ad_client_id=? and name=?", trxName)
.setParameters(AD_Client_ID, name).setOnlyActiveRecords(true).first();
return po;
}
/**
* Get the configurator setting
*/
public static String getConfiguratorSettingValue(String name, String defaultValue) {
String result = MSysConfig.getValue(name, defaultValue );
return result;
}
/**
* Get the configurator setting - specified Client, or System Client.
* Note, Client will override setting in System if both exist.
*/
public static String getConfiguratorSettingValue(String name, String defaultValue, int AD_Client_ID) {
String result = MSysConfig.getValue(name, defaultValue, AD_Client_ID );
return result;
}
/**
* Does the specified setting exist?
*/
public static boolean isSettingExists(Properties ctx, String trxName, String name) {
return isSettingExists(ctx, trxName, name, 0, 0);
}
/**
* Does the specified setting exist?
*/
public static boolean isSettingExists(Properties ctx, String trxName, String name, int AD_Client_ID, int AD_Org_ID)
{
//
String sql = "SELECT Value FROM AD_SysConfig"
+ " WHERE Name=? AND AD_Client_ID IN (0, ?) AND AD_Org_ID IN (0, ?) " //AND IsActive='Y'
+ " ORDER BY AD_Client_ID DESC, AD_Org_ID DESC";
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
pstmt = DB.prepareStatement(sql, null);
pstmt.setString(1, name);
pstmt.setInt(2, AD_Client_ID);
pstmt.setInt(3, AD_Org_ID);
rs = pstmt.executeQuery();
if (rs.next())
return true;
}
catch (SQLException e)
{
log.log(Level.SEVERE, "isSettingExists", e);
}
finally
{
DB.close(rs, pstmt);
rs = null; pstmt = null;
}
return false;
}
/**
* Create the given configurator setting
*/
public static void createConfiguratorSetting(Properties ctx, String trxName, boolean isSystem, String name, String value, String description) {
String confLevel;
Properties ctx1;
ctx1 = (Properties)ctx.clone();
if ( isSystem ) {
if (isSettingExists(ctx, trxName, name))
return;
ctx1.setProperty(Env.AD_CLIENT_ID, "0");
confLevel = MSysConfig.CONFIGURATIONLEVEL_System;
} else {
if (isSettingExists(ctx, trxName, name, Env.getAD_Client_ID(ctx), 0))
return;
confLevel = MSysConfig.CONFIGURATIONLEVEL_Client;
}
ctx1.setProperty(Env.AD_ORG_ID, "0");
MSysConfig conf = new MSysConfig(ctx1, 0, null);
conf.setConfigurationLevel(confLevel);
conf.setName(name);
conf.setDescription(description);
conf.setValue(value);
conf.saveEx();
//CacheMgt.get().reset(MSysConfig.Table_Name);
}
public static MSysConfig newOrGet(Properties ctx, String trxName, boolean isSystem, String name, String defaultValue, String description) {
NTierUtils.resetCacheForTable( MSysConfig.Table_Name );
MSysConfig config;
Properties ctx1;
createConfiguratorSetting(ctx, trxName, isSystem, name, defaultValue, description);
ctx1 = (Properties)ctx.clone();
if (isSystem) {
ctx1.setProperty(Env.AD_CLIENT_ID, "0");
} else {
}
config = getConfigurator(ctx1, trxName, name);
if ( config==null ) {
throw new AdempiereException( String.format("Returned configurator setting is null (should've been created) - '%s'", name) );
}
return config;
}
public static String newOrGetString(Properties ctx, String trxName, boolean isSystem, String name, String defaultValue, String description) {
MSysConfig config;
config = newOrGet(ctx, trxName, isSystem, name, defaultValue, description);
String r = config.get_ValueAsString("Value");
return r;
}
public static boolean newOrGetBoolean(Properties ctx, String trxName, boolean isSystem, String name, String defaultValue, String description) {
MSysConfig config;
config = newOrGet(ctx, trxName, isSystem, name, defaultValue, description);
boolean r = config.get_ValueAsBoolean("Value");
return r;
}
public static int newOrGetInt(Properties ctx, String trxName, boolean isSystem, String name, String defaultValue, String description) {
MSysConfig config;
config = newOrGet(ctx, trxName, isSystem, name, defaultValue, description);
int r = config.get_ValueAsInt("Value");
return r;
}
}
Credits
Initial contribution by: nTier Software Services http://www.ntier.co.za
Add your credit here.
