* INITIAL POPULATIONS OF THIS REPO

This commit is contained in:
2021-12-23 13:04:01 +08:00
parent 3165defa2e
commit ed70cad6ea
182 changed files with 22964 additions and 0 deletions
+57
View File
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.fourelementscapital</groupId>
<artifactId>lib-db</artifactId>
<version>2.0</version>
<licenses>
<license>
<name>The software is only allowed to be used in Four Elements - no license is given to any other parties</name>
</license>
</licenses>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-jcs-core</artifactId>
<version>2.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.jolbox</groupId>
<artifactId>bonecp</artifactId>
<version>0.7.1.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.12</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.1.0.jre7</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
Executable
+53
View File
@@ -0,0 +1,53 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.fourelementscapital</groupId>
<artifactId>lib</artifactId>
<version>${revision}</version>
</parent>
<groupId>com.fourelementscapital</groupId>
<artifactId>lib-db</artifactId>
<packaging>jar</packaging>
<name>lib-db</name>
<description>Lower level DAL (Data Access Layer) for database call</description>
<url>http://www.fourelementscapital.com</url>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.1</version>
</dependency>
<!--Cache system used to get queue-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-jcs-core</artifactId>
<version>2.2</version>
</dependency>
<!--JCS's required dependency-->
<dependency>
<groupId>com.jolbox</groupId>
<artifactId>bonecp</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.1</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,345 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.db;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import org.apache.commons.dbutils.BasicRowProcessor;
/**
* This abstract class implements the methods that are related to Theme organisation tags
*/
public abstract class AbstractTeamOrgDB extends SuperDB{
/**
* Get tag table
* @return tag table
*/
public abstract String getTagTable(); //tags
/**
* Get tag item table
* @return tag item table
*/
public abstract String getTagItemTable(); //example: scheduler_tags
/**
* Get tag item id
* @return tag item id
*/
public abstract String getTagItemId(); // example: scheduler_id or function_id;
/**
* Get tag item follow table
* @return tag item follow table
*/
public abstract String getTagItemFollowTable(); //example: scheduler_tags
/**
* Returns list of All tags of the tool ( for example, if it was implemented for scheduler then it will return all scheduler themes)
* @return tags
* @throws Exception
*/
public Vector getTags() throws Exception {
Vector rtn=new Vector();
PreparedStatement ps=this.connection().prepareStatement("select * FROM "+getTagTable()+" ORDER BY tagname");
ResultSet rs=ps.executeQuery();
while(rs.next()){
rtn.add(new BasicRowProcessor().toMap(rs));
}
//added by rams on 5-june-2012
rs.close();
ps.close();
return rtn;
}
/**
* Get tags details
* @param tags tag names
* @return tags
* @throws Exception
*/
public Vector getTagsDetails(String tags[]) throws Exception {
Vector rtn=new Vector();
String stags=null;
for(int i=0;i<tags.length;i++){
stags=(stags==null)?"'"+tags[i]+"'":stags+",'"+tags[i]+"'";
}
if(stags!=null){
String query="select * FROM "+getTagTable()+" WHERE tagname IN ("+stags+") ORDER BY tagname";
PreparedStatement ps=this.connection().prepareStatement(query);
ResultSet rs=ps.executeQuery();
while(rs.next()){
rtn.add(new BasicRowProcessor().toMap(rs));
}
//added by rams on 5-june-2012
rs.close();
ps.close();
}
return rtn;
}
/**
* Get tag ids by item id
* @param item_id item id
* @return tag ids
* @throws Exception
*/
public Vector getTagIds4Item(int item_id) throws Exception {
PreparedStatement ps=this.connection().prepareStatement("select tag_id FROM "+getTagItemTable()+" WHERE "+getTagItemId()+"=?");
ps.setInt(1, item_id);
ResultSet rs=ps.executeQuery();
Vector rtn=new Vector();
while(rs.next()){
rtn.add(rs.getInt("tag_id"));
}
//added by rams on 5-june-2012
rs.close();
ps.close();
return rtn;
}
/**
* Get follow tag ids by item id
* @param item_id item id
* @return tag ids
* @throws Exception
*/
public Vector getFollowTagIds4Item(int item_id) throws Exception {
PreparedStatement ps=this.connection().prepareStatement("select tag_id FROM "+getTagItemFollowTable()+" WHERE "+getTagItemId()+"=?");
ps.setInt(1, item_id);
ResultSet rs=ps.executeQuery();
Vector rtn=new Vector();
while(rs.next()){
rtn.add(rs.getInt("tag_id"));
}
//added by rams on 5-june-2012
rs.close();
ps.close();
return rtn;
}
/**
* Update tag ids by item id
* @param item_id item id
* @param tag_ids tag ids
* @param removeExisting is remove existing
* @throws Exception
*/
public void updateTagIds4Item(int item_id, Vector tag_ids,int removeExisting) throws Exception {
if(SchedulerDB.REMOVE_BEFORE_UPDATE==removeExisting){
PreparedStatement ps=this.connection().prepareStatement("DELETE FROM "+getTagItemTable()+" WHERE "+getTagItemId()+"=?");
ps.setInt(1, item_id);
ps.executeUpdate();
ps.close();
}
PreparedStatement ps1=this.connection().prepareStatement("INSERT INTO "+getTagItemTable()+"("+getTagItemId()+",tag_id) VALUES(?,?)");
for(Iterator i=tag_ids.iterator();i.hasNext();){
ps1.setInt(1, item_id);
ps1.setInt(2, Integer.parseInt((String)i.next()));
ps1.executeUpdate();
}
ps1.close();
}
/**
* Remove tag ids by item id
* @param item_id item id
* @param tag_id tag id
* @throws Exception
*/
public void removeTagIds4Item(int item_id, int tag_id) throws Exception {
PreparedStatement ps=this.connection().prepareStatement("DELETE FROM "+getTagItemTable()+" WHERE "+getTagItemId()+"=? and tag_id=?");
ps.setInt(1, item_id);
ps.setInt(2, tag_id);
ps.executeUpdate();
ps.close();
}
/**
* Update tag item by tag id
* @param item_id item id
* @param tagids tag ids
* @throws Exception
*/
public void updateItemTagIds(int item_id, List<Integer> tagids) throws Exception {
String query="DELETE FROM "+getTagItemTable()+" WHERE id IN (select * from (select a.id from "+getTagItemTable()+" as a left outer join "+getTagTable()+" as b on a.tag_id = b.id where "+getTagItemId()+"=? and left(b.tagname,3)<>'usr') temp_table ) ";
PreparedStatement ps=this.connection().prepareStatement(query);
ps.setInt(1, item_id);
ps.executeUpdate();
ps.close();
PreparedStatement ps1=this.connection().prepareStatement("INSERT INTO "+getTagItemTable()+"("+getTagItemId()+",tag_id) VALUES(?,?)");
for(Integer i:tagids){
ps1.setInt(1, item_id);
ps1.setInt(2, i);
ps1.executeUpdate();
}
ps1.close();
}
/**
* Update follower tag id
* @param item_id item id
* @param tagids tag ids
* @throws Exception
*/
public void updateFollwerTagIds(int item_id, List<Integer> tagids) throws Exception {
String query="DELETE FROM "+getTagItemFollowTable()+" WHERE id IN (select * from (select a.id from "+getTagItemFollowTable()+" as a left outer join "+getTagTable()+" as b on a.tag_id = b.id where "+getTagItemId()+"=? and left(b.tagname,3)<>'usr') temp_table ) ";
PreparedStatement ps=this.connection().prepareStatement(query);
ps.setInt(1, item_id);
ps.executeUpdate();
ps.close();
PreparedStatement ps1=this.connection().prepareStatement("INSERT INTO "+getTagItemFollowTable()+"("+getTagItemId()+",tag_id) VALUES(?,?)");
for(Integer i:tagids){
ps1.setInt(1, item_id);
ps1.setInt(2, i);
ps1.executeUpdate();
}
ps1.close();
}
/**
* Get theme tags
* @param item_id item id
* @return theme tags
* @throws Exception
*/
public List getThemeTags(int item_id) throws Exception {
String q="select replace(b.tagname,'thm-','') as theme FROM "+getTagItemTable()+" as a ";
q+="left outer join "+getTagTable()+" as b on a.tag_id=b.id ";
q+="where a."+getTagItemId()+"=? ";
q+="and left(b.tagname,4)='thm-' ";
PreparedStatement ps=this.connection().prepareStatement(q);
ps.setInt(1, item_id);
ResultSet rs=ps.executeQuery();
ArrayList rtn=new ArrayList();
while(rs.next()){
rtn.add(rs.getString("theme"));
}
return rtn;
}
/**
* Get theme tag name
* @param tag_id tag id
* @return theme tag name
* @throws Exception
*/
public String getThemeTagName(int tag_id) throws Exception {
String q="select replace(a.tagname,'thm-','') as theme FROM "+getTagTable()+" as a where id=?";
PreparedStatement ps=this.connection().prepareStatement(q);
ps.setInt(1, tag_id);
ResultSet rs=ps.executeQuery();
String rtn=null;
if(rs.next()){
rtn= rs.getString("theme");
}
return rtn;
}
/**
* Get theme names by item id
* @param item_id item id
* @return theme names
* @throws Exception
*/
public ArrayList getThemeNames4Item(int item_id) throws Exception {
PreparedStatement ps=this.connection().prepareStatement("select b.tagname FROM "+getTagItemTable()+" as a left outer join "+getTagTable()+" as b on a.tag_id=b.id WHERE a."+getTagItemId()+"=? and left(b.tagname,4)<>'usr-'");
ps.setInt(1, item_id);
ResultSet rs=ps.executeQuery();
ArrayList rtn=new ArrayList();
while(rs.next()){
String tagname=rs.getString("tagname");
tagname=tagname.replaceAll("thm-", "");
rtn.add(tagname);
}
//added by rams on 5-june-2012
rs.close();
ps.close();
return rtn;
}
/**
* Get followers tag by item id
* @param item_id item id
* @return followers tag
* @throws Exception
*/
public ArrayList getFollowTags4Item(int item_id) throws Exception {
PreparedStatement ps=this.connection().prepareStatement("select b.tagname FROM "+getTagItemFollowTable()+" as a left outer join "+getTagTable()+" as b on a.tag_id=b.id WHERE a."+getTagItemId()+"=? and left(b.tagname,4)<>'usr-'");
ps.setInt(1, item_id);
ResultSet rs=ps.executeQuery();
ArrayList rtn=new ArrayList();
while(rs.next()){
String tagname=rs.getString("tagname");
tagname=tagname.replaceAll("thm-", "");
rtn.add(tagname);
}
//added by rams on 5-june-2012
rs.close();
ps.close();
return rtn;
}
/**
* Get SVN user for Wiki user
* @param w_user wiki user
* @return svn user
* @throws Exception
*/
public Map getSVNUser4WikiUser(String w_user) throws Exception {
PreparedStatement ps5=this.connection().prepareStatement("SELECT * FROM users where svn_username=?");
ps5.setString(1, w_user);
ResultSet rs5= ps5.executeQuery();
Map rtn=new HashMap();
if(rs5.next()){
rtn=new BasicRowProcessor().toMap(rs5);
}
//added by rams on 5-june-2012
rs5.close();
ps5.close();
return rtn;
}
}
@@ -0,0 +1,227 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.db;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Vector;
import com.fourelementscapital.db.vo.BBSyncTrigger;
/**
* Abstract class implements methods related to BBSync
*/
public abstract class BBSyncDB extends SuperDB{
/**
* Connect database
*/
public void connectDB() throws Exception {
super.connectDB(DB_NAME_BBSYNC);
}
/**
* List all bbsync
* @return bbsync
* @throws Exception
*/
public abstract Vector listAll() throws Exception;
/**
* Get field mapping by bbsync id
* @param bbsyncid bbsync id
* @return field mapping
* @throws Exception
*/
public abstract ArrayList fieldMapping4BBSync(int bbsyncid) throws Exception;
/**
* Get download query
* @param id bbsync id
* @return bbsync
* @throws Exception
*/
public abstract Map getDownloadQuery(long id) throws Exception;
/**
* Get field mapping
* @return field mapping
* @throws Exception
*/
public abstract Vector getFieldMapping() throws Exception;
/**
* Update triggered date
* @param id bbsync id
* @param start trigger start date
* @param end trigger end date
* @throws Exception
*/
public abstract void updateTriggeredDate(int id, Timestamp start,
Timestamp end) throws Exception;
/**
* Delete field mapping
* @param id field mapping id
* @throws Exception
*/
public abstract void deleteFieldMapping(int id) throws Exception;
/**
* Add field mapping
* @param dbfield field mapping db field
* @param bbfield field mapping bb field
* @throws Exception
*/
public abstract void addFieldMapping(String dbfield, String bbfield)
throws Exception;
/**
* Update contract logs
* @param contract contracts
* @param marketsector market sector
* @param lastsync last sync
* @param scommodities scommodities
* @param fields contract fields
* @throws Exception
*/
public abstract void updateContractLogs(Vector<String> contract,String marketsector,
Timestamp lastsync, Map<String, String> scommodities,Collection fields)
throws Exception;
/**
* Update securities logs
* @param contract contracts
* @param marketsector market sector
* @param lastsync last sync
* @param fields contract fields
* @throws Exception
*/
public abstract void updateSecuritiesLogs(Vector<String> contract,
String marketsector, Timestamp lastsync,Collection fields) throws Exception;
/**
* Get contract names to ref synchronized
* @param pendingonly is pending only
* @throws Exception
* @return contract info
*/
public abstract Vector<String> getContractNames2RefSync(boolean pendingonly)
throws Exception;
/**
* Get security names to ref synchronized
* @param pendingonly is pending only
* @throws Exception
* @return security info
*/
public abstract Map<String, String> getSecurityNames2RefSync(
boolean pendingonly) throws Exception;
/**
* Get security name to ref synchronized
* @param ticker ticker
* @throws Exception
* @return security info
*/
public abstract Map<String,String> getSecurityName2RefSync(String ticker) throws Exception ;
/**
* Update contract reference
* @param contractname contract name
* @param fielddata field data
* @throws Exception
*/
public abstract void updateContractReference(String contractname,
Map<String, String> fielddata) throws Exception;
/**
* Update security reference
* @param securityname security name
* @param marketsector market sector
* @param fielddata field data
* @throws Exception
*/
public abstract void updateSecurityReference(String securityname,
String marketsector, Map<String, String> fielddata)
throws Exception;
/**
* Add sync logs
* @param bbsyncid bbsync id
* @param start start time
* @param end end time
* @param message message
* @param status status
* @param manual_scheduler manual scheduler
* @throws Exception
*/
public abstract void addSyncLogs(int bbsyncid, Timestamp start,
Timestamp end, String message, String status,
String manual_scheduler) throws Exception;
/**
* Remove schedule ticker
* @param bbsync_id bbsync id
* @param ticker contract
* @throws Exception
*/
public abstract void removeScheduleTicker(int bbsync_id, String ticker)
throws Exception;
/**
* Save schedule
* @param id bbsync id
* @param name bbsync name
* @param mkt_secdb is market security db
* @param dateoption date option
* @param datefrom date from
* @param dateto date to
* @param datenumber date recent number
* @param fields bbsync fields
* @param contracts bbsync contracts
* @param t BBSync trigger
* @param marketsector market sector
* @param timezone timezone
* @return bbsync id
* @throws Exception
*/
public abstract int saveSchedule(int id, String name, String mkt_secdb,
String dateoption, Date datefrom, Date dateto, int datenumber,
Vector fields, String contracts, BBSyncTrigger t,
String marketsector, String timezone) throws Exception;
public abstract void deleteQuery(int id) throws Exception;
/**
* Record started peer
* @param peername peer name
* @param sessionid session id
* @param time time
* @throws Exception
*/
public abstract void peerStarted(String peername,long sessionid, java.util.Date time) throws Exception ;
/**
* Record stopped peer
* @param peername peer name
* @param sessionid session id
* @param time time
* @throws Exception
*/
public abstract void peerStopped(String peername, long sessionid, java.util.Date time) throws Exception;
}
+147
View File
@@ -0,0 +1,147 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.db;
import java.io.File;
import java.io.FileInputStream;
import java.util.MissingResourceException;
import java.util.Properties;
import java.util.ResourceBundle;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
/**
* This class populates the configuration value from property file
*/
class Config {
/**
* Configuration file
*/
private static final String BUNDLE_NAME = "config_db"; //$NON-NLS-1$
/**
* windows peer configuration
*/
private static final String PROPERTY_FILE_WIN = "peer_db_windows.properties"; //$NON-NLS-1$
/**
* unix peer/server configuration file
*/
private static final String PROPERTY_FILE_UNIX = "peer_db_unix.properties"; //$NON-NLS-1$
public static String CONFIG_PROPERTY_LOCATION=null;
private static Properties confpro=null;
/**
* Private constructor
*/
private Config() {
}
/**
* Get property value of specified key. Return '!key!' if not found.
* @param key key
* @return property value
*/
protected static String getString(String key) {
if(CONFIG_PROPERTY_LOCATION==null){
try {
//return RESOURCE_BUNDLE.getString(key);
return getResourceBuddle().getString(key);
} catch (MissingResourceException e) {
return '!' + key + '!';
}
}else{
try {
String ky= getPeerProperty(key);
if(ky==null) throw new Exception();
else return ky;
} catch (Exception e) {
return '!' + key + '!';
}
}
}
private static ResourceBundle resourceBundle=null;
/**
* Get resources bundle
* @return resources bundle
*/
private synchronized static ResourceBundle getResourceBuddle(){
if(resourceBundle==null){
resourceBundle=ResourceBundle.getBundle(BUNDLE_NAME);
LogManager.getLogger(Config.class.getName()).info("resourceBundle:"+resourceBundle);
}
return resourceBundle;
}
/**
* Get property value of specified key. Return null if not found.
* @param key key
* @return property value
*/
protected static String getValue(String key) {
if(CONFIG_PROPERTY_LOCATION==null){
try {
return getResourceBuddle().getString(key);
} catch (MissingResourceException e) {
return null;
}
}else{
try {
return getPeerProperty(key);
} catch (Exception e) {
return null;
}
}
}
/**
* Get peer property by key
* @param key key
* @return peer property
*/
private static String getPeerProperty(String key) throws Exception {
try{
if(confpro==null){
String propertyfilename=PROPERTY_FILE_WIN;
if(System.getProperty("os.name").toLowerCase().equals("freebsd"))
propertyfilename=PROPERTY_FILE_UNIX;
if(System.getProperty("os.name").toLowerCase().contains("linux"))
propertyfilename=PROPERTY_FILE_UNIX;
String folder=CONFIG_PROPERTY_LOCATION;
folder=folder.endsWith(File.separator)?folder:folder+File.separator;
confpro=new Properties();
String filename=folder+"conf"+File.separator+propertyfilename;
confpro.load(new FileInputStream(filename));
}
return confpro.getProperty(key);
}catch(Exception e){
e.printStackTrace();
throw e;
}
}
}
@@ -0,0 +1,48 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.db;
import java.util.Map;
/**
* Abstract class to construct query
*/
public abstract class ConstructQueryDB extends SuperDB{
/**
* Connect database
*/
public void connectDB() throws Exception {
super.connectDB(DB_NAME_BBSYNC);
}
/**
* Construct date input query
* @param rtnobj return object
* @return query
*/
public abstract String constructDateInputQuery(Map rtnobj);
/**
* Construct recent query
* @param field table field
* @param dateQuery date query
* @return query
*/
public abstract String constructRecentQuery(String field, String dateQuery);
/**
* Construct queue history query
* @param rtnobj return object
* @return query
*/
public abstract String constructQueueHistoryQuery(Map rtnobj);
}
@@ -0,0 +1,130 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.db;
import java.sql.Connection;
import java.util.List;
import java.util.Vector;
/**
* Abstract class implements methods related to Contract
*/
public abstract class ContractDB extends SuperDB{
/**
* Update records
* @param con connection
* @param records records
* @throws Exception
*/
public abstract void updateRecords(Connection con,Vector records) throws Exception;
/**
* Add records
* @param con connection
* @param records records
* @throws Exception
*/
public abstract void addRecords(Connection con,Vector records) throws Exception;
/**
* Create table
* @param con connection
* @param decimalpoint decimal point
* @throws Exception
*/
public abstract void createTable(Connection con, int decimalpoint) throws Exception;
/**
* Update sval records
* @param con connection
* @param records records
* @throws Exception
*/
public abstract void updateSValRecords(Connection con,Vector records) throws Exception;
/**
* Add sval records
* @param con connection
* @param records records
* @throws Exception
*/
public abstract void addSValRecords(Connection con,Vector records) throws Exception;
/**
* Create sval table
* @param con connection
* @param decimalpoint decimal point
* @throws Exception
*/
public abstract void createSValTable(Connection con, int decimalpoint) throws Exception;
/**
* Check s value field
* @param con connection
* @return record count
* @throws Exception
*/
public abstract int checkSValueField(Connection con) throws Exception;
/**
* Count records
* @param con connection
* @return record count
* @throws Exception
*/
public abstract int countRecords(Connection con) throws Exception;
/**
* Check whether s value field type exist
* @param con connection
* @return true if record exist
* @throws Exception
*/
public abstract boolean checkSValueFieldTypeExist(Connection con) throws Exception;
/**
* Get contract titles by connection
* @param con connection
* @return contract titles
* @throws Exception
*/
public abstract List getContractTitles(Connection con) throws Exception;
/**
* Get contract titles by connection & date query
* @param con connection
* @param datequery date query
* @return contract titles
* @throws Exception
*/
public abstract List getContractTitles(Connection con, String datequery) throws Exception;
/**
* Update master table
* @param con connection
* @param mtable master table
* @param fieldname field name
* @throws Exception
*/
public abstract void updateMasterTable(Connection con,String mtable,String fieldname) throws Exception;
/**
* Generate SQL XL Query Plain query
* @param fieldtable field table
* @param nmonths n months
* @param contractTitleList contract title list
* @return contract date
*/
public abstract String generateSQLXLQueryPlainQuery(String fieldtable, int nmonths, List contractTitleList);
}
@@ -0,0 +1,273 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.db;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* The DBManager class provides an Access Interface between the Trading Database and Java.
* It can be used to get data from the database, insert new data and update existing data.
* The config file containing the details for the database hostname and credentials is located in the resources folder.
* @author Manas Maral Misra, Antonius Ari Wicaksono
*
*/
public class DBManager {
/**
* The database name for the current instance of DBManager
*/
private String dbName;
/**
* The connection string for the database connection. Stored in config.lib-dal
*/
private String connectionString;
/**
* The resultSet which contains the returned results from the SQL Query of GetDatabase
*/
ResultSet resultSet = null;
/**
* The connection object of the current connection.
*/
private Connection conn = null;
/**
* Initialize the DBManager with the database name.
* @param dbName The name of the database e.x. trading, tradingRef, fundamentals
* @throws IOException
*/
public DBManager(String dbName) throws IOException
{
this.dbName = dbName;
}
/**
* This function connects to the dbName database;
* @throws SQLException This is thrown incase we are not able to connect to the database;
* @throws ClassNotFoundException This is thrown incase the driver is not found on this machine
* @throws IOException This is thrown incase the config.lib-dal if not found on this machine under resources folder
*/
public void connect() throws SQLException, ClassNotFoundException, IOException
{
String connString = Utils.getConfig4E(".CONFIG4E_JDBC_CONNECTIONSTRING_" + dbName.toUpperCase());
String driver = "com.mysql.jdbc.Driver";
// if connString contains 'sqlserver'. i.e. 'jdbc:sqlserver://10.153.64.3:1433;databaseName=infrastructure;integratedSecurity=false;user=dbuser;password=dbuser'
if (connString.contains("sqlserver")) {
driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
}
Class.forName(driver);
conn = DriverManager.getConnection(connString);
connectionString = conn.toString();
}
/**
* This function returns the result set of the SQL Query used by GetDatabase;
* @param query The SQL Query passed to it.
* @return
* @throws SQLException
*/
protected ResultSet executeQuery(String query) throws SQLException
{
Date start = new Date();
Statement statement = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
resultSet = statement.executeQuery(query);
Date end = new Date();
long duration = (end.getTime() - start.getTime());
//Utils.Log("DEBUG: Query ("+query+") took "+duration+ " miliseconds");
return resultSet;
}
/**
* This function returns the result set of the SQL Query used by GetDatabase;
* @param query The SQL Query passed to it.
* @return
* @throws SQLException
*/
protected void executeNonQuery(String query) throws SQLException
{
Date start = new Date();
Statement statement = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
statement.execute(query);
Date end = new Date();
long duration = (end.getTime() - start.getTime());
//Utils.Log("DEBUG: Query ("+query+") took "+duration+ " miliseconds");
}
/**
* This function executes the Update query from UpdateDatabase
* @param query
* @throws SQLException
*/
private void updateQuery(String query) throws SQLException
{
Date start = new Date();
Statement statement = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
statement.executeUpdate(query);
Date end = new Date();
long duration = (end.getTime() - start.getTime());
System.out.println("DEBUG: Query took "+duration+ " miliseconds");
}
/**
* Closes the current Database connection.
* @throws SQLException
*/
public void closeConnection()
{
try {
if(!conn.isClosed())
conn.close();
if(resultSet!=null)
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Used to query a database and access the resultant data in a ResultSet
* @param tableName The name of the database table to be accessed
* @param selectedFields A list of the values to be selected through the queries. <code>new ArrayList<String>();</code>
* be passed to select all fields (*)
* @param queryParams A hashmap containing the selection filters in the format <code>[column_name]=[value]</code>
* @param customQuery Any additional parameters to be added after the Where clause. Could be a GROUP BY or ORDER BY
* @return The resulting data in a ResultSet
* @throws SQLException
*/
public ResultSet getDatabase(String tableName,ArrayList<String> selectedFields, Map<String,Object> queryParams, String customQuery) throws SQLException
{
String queryBuilder = "";
if(selectedFields.size()==0)
{
selectedFields.add("*");
}
queryBuilder += "SELECT "+Utils.Join(selectedFields,",")+" FROM "+tableName;
if(queryParams != null && queryParams.size()>0)
{
queryBuilder += " WHERE ";
for (Map.Entry<String,Object> entry : queryParams.entrySet()) {
try{
Float f = Float.parseFloat(entry.getValue().toString());
queryBuilder += " "+entry.getKey() + " = "+entry.getValue()+" AND";
}
catch(Exception ex)
{
queryBuilder += " "+entry.getKey() + " = '"+entry.getValue()+"' AND";
}
}
if(customQuery!= null && customQuery!="")
queryBuilder += customQuery+" AND";
queryBuilder = queryBuilder.substring(0,queryBuilder.length()-3);
}
//System.out.println(queryBuilder);
return executeQuery(queryBuilder);
}
/**
* Used to update a single cell in a table in the database.
* @param tableName The name of the database table to be accessed
* @param selectedFields
* @param queryParams A hashmap containing the selection filters in the format <code>[column_name]=[value]</code>
* @param selectionFieldValues A list of the values to be selected through the queries. <code>new ArrayList<String>();</code>
* be passed to select all fields (*)
* @param newFieldsValues
* @throws SQLException
*/
public void updateDatabase(String tableName,Map<String,Object> selectionFieldValues, Map<String,Object> newFieldsValues) throws SQLException
{
String queryBuilder = "UPDATE "+tableName+" SET ";
if(newFieldsValues.size()>0)
{
ArrayList<String> al = new ArrayList<String>();
for (Map.Entry<String,Object> entry : newFieldsValues.entrySet()) {
try{
Float f = Float.parseFloat(entry.getValue().toString());
al.add(entry.getKey()+" = "+entry.getValue());
}
catch(Exception ex)
{
al.add(entry.getKey()+" = '"+entry.getValue()+"'");
}
}
queryBuilder += Utils.Join(al, ",");
}
if(selectionFieldValues.size()>0)
{
queryBuilder += " WHERE ";
for (Map.Entry<String,Object> entry : selectionFieldValues.entrySet()) {
try{
Float f = Float.parseFloat(entry.getValue().toString());
queryBuilder += " "+entry.getKey() + " = "+entry.getValue()+" AND";
}
catch(Exception ex)
{
queryBuilder += " "+entry.getKey() + " = '"+entry.getValue()+"' AND";
}
}
queryBuilder = queryBuilder.substring(0,queryBuilder.length()-3);
}
//Utils.Log(queryBuilder);
updateQuery(queryBuilder);
}
/**
* Used to insert a new row in a table in the database.
* @param tableName The name of the table where the row has to be inserted
* @param fieldValues the hashmap of column names and values
* @throws SQLException
*/
public void insertDatabase(String tableName,Map<String,Object> fieldValues,Map<String,Object> nonStringfieldValues) throws SQLException
{
String queryBuilder = "INSERT INTO "+tableName;
if(nonStringfieldValues==null) nonStringfieldValues = new HashMap<String, Object>();
if(fieldValues.size()>0)
{
queryBuilder += " ("+Utils.Join(new ArrayList<String>(fieldValues.keySet()), ",");
if(!nonStringfieldValues.isEmpty())
queryBuilder +=","+Utils.Join(new ArrayList<String>(nonStringfieldValues.keySet()), ",");
queryBuilder+= " ) VALUES ";
ArrayList<String> ll = new ArrayList<String>();
for (Iterator i = fieldValues.values().iterator(); i.hasNext();) {
ll.add(i.next().toString());
}
ArrayList<String> ll2 = new ArrayList<String>();
for (Iterator i = nonStringfieldValues.values().iterator(); i.hasNext();) {
ll2.add(i.next().toString());
}
queryBuilder += " ('"+Utils.Join(ll, "','")+"'";
if(!ll2.isEmpty())
queryBuilder += ","+Utils.Join(ll2, ",");
queryBuilder += ") ";
}
//Utils.Log(queryBuilder);
executeNonQuery(queryBuilder);
}
}
@@ -0,0 +1,33 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.db;
import java.util.List;
/**
* Abstract class implements methods related to database manager
*/
public abstract class DBManagerDB extends SuperDB {
/**
* Connect database
*/
public void connectDB() throws Exception {
super.connectDB(DB_NAME_BBSYNC);
}
/**
* List database groups
* @return database groups
* @throws Exception
*/
public abstract List listDBGroups() throws Exception;
}
@@ -0,0 +1,99 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.db;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import com.fourelementscapital.db.vo.FlexiField;
/**
* Abstract class implements methods related to Flexi Field
*/
public abstract class FlexiFieldDB extends SuperDB {
/**
* Connect database
*/
public void connectDB() throws Exception {
super.connectDB(DB_NAME_BBSYNC);
}
/**
* Get flexi fields
* @param tablename tablename string
* @return flexi fields
* @throws Exception
*/
public abstract List getFlexiFields(String tablename) throws Exception;
/**
* Add flexi field
* @param ffield flexi field
* @throws Exception
*/
public abstract void addFlexiField(FlexiField ffield) throws Exception;
/**
* Update flexi field
* @param id flexi field id
* @param ffield flexi field
* @throws Exception
*/
public abstract void updateFlexiField(long id,FlexiField ffield) throws Exception;
/**
* Delete flexi field
* @param id flexi field id
* @throws Exception
*/
public abstract void deleteFlexiField(long id) throws Exception;
/**
* Get flexi field data
* @param tablename table name
* @param idfield id field
* @param idvalue id
* @return flexi field data
* @throws Exception
*/
public abstract Map getFlexiFieldData (String tablename, String idfield, String idvalue) throws Exception;
/**
* Get flexi field data for lucene token
* @param tablename table name
* @param idfield id field
* @param idvalue id
* @return flexi field data
* @throws Exception
*/
public abstract Vector<Map> getFlexiFieldData4LuceneToken (String tablename, String idfield, String idvalue) throws Exception;
/**
* Save flexi field data
* @param tablename table name
* @param idfield id field
* @param idvalue id
* @param data data
* @throws Exception
*/
public abstract void saveFlexiFieldData (String tablename, String idfield, String idvalue, Map data) throws Exception;
/**
* Update flexi field order
* @param id flexi field id
* @param order display order
* @throws Exception
*/
public abstract void updateFlexiFieldOrder(long id,int order) throws Exception ;
}
@@ -0,0 +1,77 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.db;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
/**
* General utility class for database
*/
public class GeneralUtilDB {
/**
* Convert resultset to map
* @param rs ResultSet
* @return map
* @throws Exception
*/
public static Map<String, Object> resultsetToMap(ResultSet rs) throws Exception {
ResultSetMetaData metaData = rs.getMetaData();
int colCount = metaData.getColumnCount();
Map<String, Object> row = new HashMap<String, Object>();
for (int i = 1; i <= colCount; i++) {
row.put(metaData.getColumnLabel(i), rs.getObject(i));
}
return row;
}
/**
* Convert query from MS-SQL
* @param sqlquery sql query
* @return query
*/
public static String convertFromSQL(String sqlquery){
String rtn=sqlquery;
if(sqlquery.toUpperCase().contains("BETWEEN") && sqlquery.toUpperCase().contains("GETDATE()") ){
rtn=sqlquery.replace("GETDATE()", "SYSDATE()");
}else if(sqlquery.toUpperCase().contains("DATEADD")) {
StringTokenizer st=new StringTokenizer(sqlquery,",");
if(st.countTokens()>=3){
String p1=st.nextToken();
String p2=st.nextToken();
String p3=st.nextToken();
p3=p3.replace(")", "");
if(p3.trim().equalsIgnoreCase("current_timestamp")){
p3="SYSDATE()";
}
StringTokenizer st1=new StringTokenizer(p1,"(");
if(st1.countTokens()>=2){
String p2a=st1.nextToken();
String p2b=st1.nextToken();
p2a=p2a.replace("DATEADD", "DATE_ADD");
rtn=p2a+"("+p3+", INTERVAL "+p2+" "+p2b+")";
}
}
}
return rtn;
}
}
@@ -0,0 +1,223 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.db;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Vector;
/**
* Abstract class implements methods related to IExec
*/
public abstract class IExecDB extends SuperDB {
/**
* Connect database
*/
public void connectDB() throws Exception {
super.connectDB(DB_NAME_IEXEC);
}
/**
* List groups
* @return groups
* @throws Exception
*/
public abstract List listGroups() throws Exception;
/**
* Set group order
* @param groupids group id list
* @throws Exception
*/
public abstract void setGroupOrder( Vector groupids) throws Exception;
/**
* List folders
* @return ie folder list
* @throws Exception
*/
public abstract List listFolders( ) throws Exception;
/**
* List strategies
* @return strategies
* @throws Exception
*/
public abstract List listStrategies() throws Exception;
/**
* Get strategy
* @param strategy_id strategy id
* @return strategy
* @throws Exception
*/
public abstract Map getStrategy(int strategy_id) throws Exception;
/**
* Check whether it is parent
* @param strategy_name strategy name
* @return true if it is parent strategy
* @throws Exception
*/
public abstract boolean isParent(String strategy_name) throws Exception;
/**
* Get strategies by names
* @param strategy_names strategy names
* @return strategies
* @throws Exception
*/
public abstract Vector getStrategies(Vector strategy_names) throws Exception;
/**
* Get folder name
* @param folder_id folder id
* @return folder name list
* @throws Exception
*/
public abstract String getFolderName(int folder_id) throws Exception;
/**
* Get unique contracts
* @param strategy_name strategy name
* @return contracts
* @throws Exception
*/
public abstract Vector getUniqueContracts(String strategy_name) throws Exception;
/**
* Add parameters
* @param data parameter data
* @param strategy_name strategy name
* @throws Exception
*/
public abstract void addParameters(ArrayList<Map> data,String strategy_name) throws Exception;
/**
* Get parameter values
* @param strategy_name strategy name
* @param contract contract
* @return parameter values
* @throws Exception
*/
public abstract Map getParameterValues(String strategy_name, String contract) throws Exception;
/**
* Remove contract
* @param strategy_name strategy name
* @param contract contract
* @throws Exception
*/
public abstract void removeContract(String strategy_name, String contract) throws Exception;
/**
* Get strategy by name
* @param strategy_name strategy name
* @return strategy
* @throws Exception
*/
public abstract Map getStrategy(String strategy_name) throws Exception;
/**
* Create strategy
* @param folder_id folder id
* @param strategy_name strategy name
* @param path file name path
* @return strategy id
* @throws Exception
*/
public abstract int createStrategy(int folder_id,String strategy_name, String path) throws Exception;
/**
* Create child strategy
* @param parent_name parent name
* @param strategy_name strategy name
* @return strategy id
* @throws Exception
*/
public abstract int createChildStrategy(String parent_name,String strategy_name) throws Exception;
/**
* Get folder id
* @param folder_name folder name
* @return folder id
* @throws Exception
*/
public abstract int getFolderID(String folder_name) throws Exception;
/**
* Create folder
* @param folder folder name
* @param new_group_id new group id
* @return folder id
* @throws Exception
*/
public abstract int createFolder(String folder, String new_group_id) throws Exception;
/**
* List of folders
* @param group_id group id
* @return folders
* @throws Exception
*/
public abstract List listOfFolders(String group_id) throws Exception;
/**
* Update strategy folder
* @param strategy_id strategy id
* @param new_folder_id new folder id
* @throws Exception
*/
public abstract void updateStrategyFolder(int strategy_id,int new_folder_id) throws Exception;
/**
* Move folder
* @param folder_id folder id
* @param new_group_id new group id
* @throws Exception
*/
public abstract void moveFolder(int folder_id, String new_group_id) throws Exception;
/**
* Rename strategy
* @param strategy_id strategy id
* @param strategy_name strategy name
* @param file_name file name
* @throws Exception
*/
public abstract void renameStrategy(int strategy_id, String strategy_name, String file_name) throws Exception;
/**
* Rename parent strategy
* @param strategy_id strategy id
* @param strategy_name strategy name
* @param file_name file name
* @param old_strategy_name old strategy name
* @throws Exception
*/
public abstract void renameParentStrategy(int strategy_id, String strategy_name, String file_name, String old_strategy_name) throws Exception;
/**
* Get contract tree
* @return contract tree
* @throws Exception
*/
public abstract Vector getContractTree() throws Exception;
/**
* Get commodity tree
* @return commodity tree
* @throws Exception
*/
public abstract Vector<String> getCommodityTree() throws Exception;
}
@@ -0,0 +1,73 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.db;
import java.util.List;
import java.util.Map;
/**
* Abstract class implements methods related to Infrastructure
*/
public abstract class InfrastructureDB extends SuperDB {
public static final String APPLICATION_IEXEC = "iExec";
public static final String APPLICATION_SCHEDULER_PEER_ASSOCIATION = "Scheduler.PeerAssociation";
/**
* Connect database
*/
public void connectDB() throws Exception {
super.connectDB(DB_NAME_INFRASTRUCTURE);
}
/**
* Get themes by user
* @param user user
* @return themes
* @throws Exception
*/
public abstract Map getThemes4Users(String user) throws Exception;
/**
* Get team organization
* @param themes themes
* @param hierarchy hierarchy
* @return team organization
* @throws Exception
*/
public abstract Map getTeamOrg(String themes, List hierarchy) throws Exception;
/**
* Update Bloomberg daily counter
* @param date date
* @param count BBG daily counter
* @throws Exception
*/
public abstract void updateBloomberDailyCounter(String date, int count) throws Exception;
/**
* get themes by username access 'X','B','C'
* @param username username
* @return themes
* @throws Exception
*/
public abstract List<String> getThemeByUsernameAccess(String username) throws Exception;
/**
* Get access by app and username
* @param application application
* @param username username
* @return application user access
* @throws Exception
*/
public abstract List getAccessByAppAndUsername(String application, String username) throws Exception;
}
@@ -0,0 +1,446 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.db;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import com.fourelementscapital.db.vo.PeerPackage;
/**
* Abstract class implements methods related to R Function
*/
public abstract class RFunctionDB extends AbstractTeamOrgDB {
public static final int FUNCTION_TYPE_NORMAL=0;
public static final int FUNCTION_TYPE_CLASS=1;
public static final int FUNCTION_TYPE_METHOD=2;
/**
* Connect database
*/
public void connectDB() throws Exception {
super.connectDB(DB_NAME_R_FUNCTION);
}
/**
* Get tag table
* @return "tags "
*/
public String getTagTable() { return "tags "; }
/**
* Get tag item table
* @return "function_tags"
*/
public String getTagItemTable() { return "function_tags"; }
/**
* Get tag item id
* @return "function_id"
*/
public String getTagItemId() { return "function_id";}
/**
* Get tag item follow table
* @return "function_followtags"
*/
public String getTagItemFollowTable() { return "function_followtags";}
/**
* Get R function
* @param function_id function id
* @return r function
* @throws Exception
*/
public abstract Map getRFunction(int function_id) throws Exception;
/**
* Get R functions by names
* @param functionnames function names
* @return r functions
* @throws Exception
*/
public abstract Vector getRFunctions(Vector functionnames) throws Exception;
/**
* Get R functions by name
* @param function_name function name
* @return r function
* @throws Exception
*/
public abstract Map getRFunction(String function_name) throws Exception;
/**
* Get folder name
* @param folder_id folder id
* @return folder name
* @throws Exception
*/
public abstract String getFolderName(int folder_id) throws Exception;
/**
* List R functions
* @return R functions
* @throws Exception
*/
public abstract List listRFunctions() throws Exception;
/**
* Update function order
* @param function_ids function ids
* @throws Exception
*/
public abstract void updateFunctionOrder(int[] function_ids ) throws Exception;
/**
* Update folder order
* @param folder_ids folder ids
* @throws Exception
*/
public abstract void updateFolderOrder(int[] folder_ids ) throws Exception;
/**
* List all R functions names
* @return R functions names
* @throws Exception
*/
public abstract List listAllRFunctionNames() throws Exception;
/**
* List all R script names
* @param folder_id folder id
* @return R script names
* @throws Exception
*/
public abstract List listAllRScriptNames(int folder_id) throws Exception;
/**
* List of functions by folder id
* @param folder_id folder id
* @return r functions
* @throws Exception
*/
public abstract List<Map> listOfFunctions(int folder_id) throws Exception;
/**
* List function groups
* @return function groups
* @throws Exception
*/
public abstract List listFunctionGroups() throws Exception;
/**
* List of folders
* @return folders
* @throws Exception
*/
public abstract List listOfFolders( ) throws Exception;
/**
* List of folders by group id
* @param group_id group id
* @return folders
* @throws Exception
*/
public abstract List listOfFolders(String group_id) throws Exception;
/**
* Get folder id by name
* @param folder_name folder name
* @return folder id
* @throws Exception
*/
public abstract int getFolderID(String folder_name) throws Exception;
/**
* Create R function
* @param folder_id folder id
* @param functionname function name
* @param path script file path
* @param func_type function type
* @return r function id
* @throws Exception
*/
public abstract int createFunction(int folder_id,String functionname, String path,int func_type) throws Exception;
/**
* Move folder
* @param folder_id folder id
* @param new_group_id new group id
* @throws Exception
*/
public abstract void moveFolder(int folder_id, String new_group_id) throws Exception;
/**
* Update R function folder
* @param function_id function id
* @param new_folder_id new folder id
* @throws Exception
*/
public abstract void updateFunctionFolder(int function_id,int new_folder_id) throws Exception;
/**
* Rename folder
* @param folder_id folder id
* @param foldername folder name
* @throws Exception
*/
public abstract void renameFolder(int folder_id,String foldername) throws Exception;
/**
* Update last 2 users tag
* @param function_id function id
* @param user_tagid user tag id
* @throws Exception
*/
public abstract void updateLast2UsersTag(int function_id, int user_tagid) throws Exception;
/**
* Get all tags
* @return tags
* @throws Exception
*/
public abstract Vector getTags() throws Exception;
/**
* Get tag id by function id
* @param function_id function id
* @return tag ids
* @throws Exception
*/
public abstract Vector getTagIds4Function(int function_id) throws Exception;
/**
* Add if tag not exist
* @param tagname tag name
* @return tag id
* @throws Exception
*/
public abstract int addIfTagNotExist(String tagname) throws Exception;
/**
* Add tag ids for function
* @param function_id function id
* @param tag_id tag id
* @throws Exception
*/
public abstract void addTagIds4Function(int function_id, int tag_id) throws Exception;
/**
* Remove tag ids for function
* @param function_id function id
* @param tag_id tag id
* @throws Exception
*/
public abstract void removeTagIds4Function(int function_id, int tag_id) throws Exception;
/**
* Update lock
* @param function_id function id
* @param lockedby locked by
* @throws Exception
*/
public abstract void updateLock(int function_id,String lockedby) throws Exception;
/**
* Set group order
* @param groupids group ids
* @throws Exception
*/
public abstract void setGroupOrder( Vector groupids) throws Exception ;
/**
* Create folder
* @param folder folder name
* @param new_group_id new group id
* @return folder id
* @throws Exception
*/
public abstract int createFolder(String folder, String new_group_id) throws Exception;
/**
* Remove folder
* @param folder_id folder id
* @throws Exception
*/
public abstract void removeFolder(int folder_id) throws Exception;
/**
* Update function deleted
* @param function_id function id
* @throws Exception
*/
public abstract void updateFunctionDeleted(int function_id) throws Exception;
/**
* Rename R function
* @param function_id function id
* @param functionname function name
* @param script_file script file path
* @throws Exception
*/
public abstract void renameFunction(int function_id, String functionname, String script_file) throws Exception;
/**
* Delete R function
* @param function_id function id
* @throws Exception
*/
public abstract void deleteFunction(int function_id) throws Exception ;
/**
* Update is wiki done
* @param function_id function id
* @param done is done
* @throws Exception
*/
public abstract void updateWikiDone(int function_id, int done) throws Exception ;
/**
* Get all function names ID
* @param wherecond where condition
* @return function names & ids
* @throws Exception
*/
public abstract Map getAllFunctionNamesID(String wherecond) throws Exception;
/**
* Auto complete functions
* @param func_keyword function keyword
* @return r function
* @throws Exception
*/
public abstract List autoCompleteFunctions(String func_keyword) throws Exception ;
/**
* Updated owner ID
* @param owner_id owner id
* @param function_id function id
* @throws Exception
*/
public abstract void updatedOwnerIDNow(int owner_id,int function_id) throws Exception;
/**
* Get package info by package name
* @param pname package name
* @return package info
* @throws Exception
*/
public abstract Map getPackageInfo(String pname) throws Exception;
/**
* Get default hierarchy depends ids by package name
* @param pname package name
* @return id
* @throws Exception
*/
public abstract List getDefaultHierarchyDependsIds(String pname) throws Exception;
/**
* List related folder ids
* @param parent_folder parent folder id
* @return related folder ids
* @throws Exception
*/
public abstract List listRelatedFolderIds(int parent_folder) throws Exception;
/**
* Update related folder ids
* @param folder_id folder id
* @param ids related folder ids
* @throws Exception
*/
public abstract void updateRelatedFolderIds(int folder_id, List<Integer> ids) throws Exception;
/**
* Add tag for package
* @param tag_id tag id
* @param function_ids function ids
* @param make_owner_also is make owner also
* @throws Exception
*/
public abstract void addTag4Package(int tag_id,int function_ids[],boolean make_owner_also) throws Exception;
/**
* Add notification tag for package
* @param tag_id tag id
* @param function_ids function ids
* @param add_also is add a new record also
* @throws Exception
*/
public abstract void addNotificationTag4Package(int tag_id,int function_ids[],boolean add_also) throws Exception;
/**
* Remove tag for package
* @param tag_id tag id
* @param function_ids function ids
* @throws Exception
*/
public abstract void removeTag4Package(int tag_id,int function_ids[]) throws Exception ;
/**
* Update peer package
* @param peername peer name
* @param plist peer package list
* @throws Exception
*/
public abstract boolean updatePeerPackage(String peername, ArrayList<PeerPackage>plist) throws Exception;
/**
* Get R function for script file
* @param scriptfile script file
* @return r functions
* @throws Exception
*/
public abstract Map getRFunctionForScriptFile(String scriptfile) throws Exception;
/**
* Update tag id by folder id
* @param folder_id folder id
* @param tag_ids tag ids
* @param removeExisting is remove existing
* @param tablename table name
* @throws Exception
*/
public abstract void updateTagIds4Folder(int folder_id, List tag_ids,int removeExisting, String tablename) throws Exception ;
/**
* Get tag id by folder id
* @param folder_id folder id
* @param tablename table name
* @return tag ids
* @throws Exception
*/
public abstract ArrayList getTagIds4Folder(int folder_id, String tablename) throws Exception ;
/**
* Get theme tags by folder id
* @param folder_id folder id
* @return theme tags
* @throws Exception
*/
public abstract List getThemeTags4Folder(int folder_id) throws Exception ;
/**
* Get folder theme by R function id
* @param r_function_id r function id
* @return theme
* @throws Exception
*/
public abstract String getFolderThemeByRFunctionId(int r_function_id) throws Exception ;
public abstract void syncLib(int counter, int theme, int wikiDone, String rScript) throws Exception ;
public abstract void truncateFunctions() throws Exception ;
}
@@ -0,0 +1,137 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.db;
import java.util.Map;
import java.util.Vector;
import com.fourelementscapital.db.vo.CommodityInfo;
/**
* Abstract class implements methods related to reference
*/
public abstract class ReferenceDB extends SuperDB {
/**
* Connect database
*/
public void connectDB() throws Exception {
super.connectDB(DB_NAME_BBSYNC);
}
/**
* Get commodity contracts
* @return commodity contracts
* @throws Exception
*/
public abstract Map getComContracts() throws Exception;
/**
* Delete field mapping
* @param id field mapping id
* @throws Exception
*/
public abstract void deleteFieldMapping(int id) throws Exception;
/**
* Add field mapping
* @param dbfield friendly name
* @param bbfield bb field name
* @throws Exception
*/
public abstract void addFieldMapping(String dbfield, String bbfield) throws Exception;
/**
* Get field mapping
* @return field mapping
* @throws Exception
*/
public abstract Vector getFieldMapping() throws Exception;
/**
* Delete security field mapping
* @param id security field mapping id
* @throws Exception
*/
public abstract void deleteSecFieldMapping(int id) throws Exception;
/**
* Add security field mapping
* @param dbfield friendly name
* @param bbfield bb field name
* @throws Exception
*/
public abstract void addSecFieldMapping(String dbfield, String bbfield) throws Exception;
/**
* Get security field mapping
* @return security field mapping
* @throws Exception
*/
public abstract Vector getSecFieldMapping() throws Exception;
/**
* Update commodity
* @param comm commodity info
* @throws Exception
*/
public abstract void updateCommodity(CommodityInfo comm) throws Exception;
/**
* Get commodity info
* @param commodity commodity
* @return commodity info
* @throws Exception
*/
public abstract Map getCommodityInfo(String commodity) throws Exception;
/**
* Get contract info
* @param contract contract name
* @return contract info
* @throws Exception
*/
public abstract Map getContractInfo(String contract) throws Exception;
/**
* Get security info
* @param security security name
* @return security info
* @throws Exception
*/
public abstract Map getSecurityInfo(String security) throws Exception;
/**
* Get contract BB info, this method may not be useful in future.
* @param contract contract
* @return contract BB info
* @throws Exception
*/
public abstract Vector getContractBBInfo(String contract) throws Exception;
/**
* Get security BB info, this method may not be usefull in future.
* @param security security
* @return security BB info
* @throws Exception
*/
public abstract Vector getSecurityBBInfo(String security) throws Exception;
/**
* Get all security field values
* @param friendlyname security field mapping friendly name
* @return security field values
* @throws Exception
*/
public abstract Map getAllSecFieldValues(String friendlyname) throws Exception;
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,613 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.jcs.JCS;
import org.apache.commons.jcs.access.CacheAccess;
import org.apache.commons.jcs.engine.behavior.IElementAttributes;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.db.mariadb.BBSyncDBMariaDB;
import com.fourelementscapital.db.mariadb.ConstructQueryDBMariaDB;
import com.fourelementscapital.db.mariadb.ContractDBMariaDB;
import com.fourelementscapital.db.mariadb.DBManagerDBMariaDB;
import com.fourelementscapital.db.mariadb.FlexiFieldDBMariaDB;
import com.fourelementscapital.db.mariadb.IExecDBMariaDB;
import com.fourelementscapital.db.mariadb.InfrastructureDBMariaDB;
import com.fourelementscapital.db.mariadb.RFunctionDBMariaDB;
import com.fourelementscapital.db.mariadb.ReferenceDBMariaDB;
import com.fourelementscapital.db.mariadb.SchedulerDBMariaDB;
import com.fourelementscapital.db.mariadb.UtilDBMariaDB;
import com.jolbox.bonecp.BoneCP;
import com.jolbox.bonecp.BoneCPConfig;
import com.jolbox.bonecp.Statistics;
/**
* Super class of all db classes
*/
public class SuperDB {
// get db name from config file :
public static final String DB_NAME_BBSYNC = "bbsync";
//public static final String DB_NAME_R_FUNCTION = "rfunction";
public static final String DB_NAME_R_FUNCTION = "quantlib";
public static final String DB_NAME_TRADING = "trading";
public static final String DB_NAME_TRADINGREF = "tradingRef";
public static final String DB_NAME_IEXEC = "iexec";
public static final String DB_NAME_INFRASTRUCTURE = "infrastructure";
public static boolean CONNECTION_POOL_ACTIVE=false;
public static boolean CONNECTION_POOL_CONN_COUNT=true;
private static int db_close_timeout = 5; // default : 5 minutes
private Connection con = null;
private String db=null;
private String driver = "com.mysql.jdbc.Driver";
private static ConcurrentHashMap<String,BoneCP> connPools=new ConcurrentHashMap<String,BoneCP>();
private static Vector<SuperDB> connections=new Vector<SuperDB>();
private Date connectedDate=null;
private String callStack=null;
public static String MY_SQL_DRIVER="com.mysql.jdbc.Driver";
private String tablename=null;
private Logger log=LogManager.getLogger(SuperDB.class.getName());
//private Logger log = LogManager.getLogger(ExecuteRMgmt.class.getName());
private static long count=0;
private static long delays=0;
//private static JCS cache=null;
private static CacheAccess<String, String> cache=null;
/**
* Get DB close timeout
* @return timeout
*/
public static int getDbCloseTimeout() {
if(Config.getString("db_close_timeout") != null) {
db_close_timeout = Integer.parseInt(Config.getString("db_close_timeout"));
}
return db_close_timeout;
}
/**
* Get connected date
* @return connected date
*/
public Date getConnectedDate() {
return connectedDate;
}
/**
* Set connected date
* @param connectedDate connected date
*/
public void setConnectedDate(Date connectedDate) {
this.connectedDate = connectedDate;
}
/**
* Get call stack
* @return call stack
*/
public String getCallStack() {
return callStack;
}
/**
* Set call stack
* @param callStack call stack
*/
public void setCallStack(String callStack) {
this.callStack = callStack;
}
/**
* Connect to specific database
* @param db database
* @throws Exception
*/
public void connectDB(String db) throws Exception {
// Establish the connection.
this.db=db;
Date start=new Date();
String marketConnectionURL = Utils.getConfig4E(".CONFIG4E_JDBC_CONNECTIONSTRING_" + db.toUpperCase()); // throw exception
// add parameter to prevent timestamp converting error
marketConnectionURL += "&useUnicode=true&useFastDateParsing=false&characterEncoding=UTF-8";
// replace driver if marketConnectionURL contains 'sqlserver'. i.e. 'jdbc:sqlserver://10.153.64.3:1433;databaseName=infrastructure;integratedSecurity=false;user=dbuser;password=dbuser'
if (marketConnectionURL.contains("sqlserver")) {
this.driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
}
if(CONNECTION_POOL_ACTIVE){
if(connPools.get(db)==null){
setupConnectionPool(marketConnectionURL);
}
if(this.con!=null && !this.con.isClosed()) {
//don't assing new
printOrphanConns();
}else{
this.con=connPools.get(db).getAsyncConnection().get();
this.setCallStack(collectErrorStack(new Exception().getStackTrace()));
this.setConnectedDate(new Date());
//connPools.get(db).getAsyncConnection()
connections.add(this);
}
}else{
Class.forName(this.driver);
log.debug("marketConnectionURL:"+marketConnectionURL);
if(this.con!=null && !this.con.isClosed()) {
//don't assing new
//System.out.println("~~~SuperDB.connectDB(): Reuse connection, Already connection is active");
printOrphanConns();
}else{
this.con = DriverManager.getConnection(marketConnectionURL);
connections.add(this);
this.setCallStack(collectErrorStack(new Exception().getStackTrace()));
this.setConnectedDate(new Date());
//DatabaseMetaData md=this.con.getMetaData();
//md.get
}
}
//log.debug("conn opened in "+diff+" ms" +" counter:"+count);
if(CONNECTION_POOL_CONN_COUNT) {
Date end=new Date();
long diff=end.getTime()-start.getTime();
delays+=diff;
count++;
}
}
/**
* Get cache
* @return cache
* @throws Exception
*/
private static CacheAccess getCache() throws Exception {
if(cache==null) cache=JCS.getInstance("SuperDB-cache");
return cache;
}
/**
* Print orphan connections
* @throws Exception
*/
private void printOrphanConns() throws Exception {
if(getCache().get("delayPrint")!=null){
System.out.println("~~~SuperDB.connectDB(): Reuse connection, Already connection is active");
}else{
IElementAttributes att= getCache().getDefaultElementAttributes();
att.setMaxLife(600);
getCache().put("delayPrint","delay",att);
try{
System.out.println("=====SuperDB.connectDB(): Reuse connection, Already connection is active====\n"+collectErrorStack(new Exception().getStackTrace()));
//Thread.dumpStack();
}catch(Exception e){
//e.printStackTrace();
}
}
}
/**
* Setup connection pool
* @param marketConnectionURL market connection URL
* @throws Exception
*/
private synchronized void setupConnectionPool(String marketConnectionURL) throws Exception {
Class.forName(this.driver); // load the DB driver
BoneCPConfig config = new BoneCPConfig(); // create a new configuration object
config.setJdbcUrl(marketConnectionURL); // set the JDBC url
config.setMinConnectionsPerPartition(1);
config.setMaxConnectionsPerPartition(25);
config.setPartitionCount(2); //set connectinos returned to pool lives very short
//config.setAcquireIncrement(1);
config.setMaxConnectionAgeInSeconds(10);
config.setLogStatementsEnabled(true);
connPools.put(db, new BoneCP(config));
}
/**
* Close all connections
*/
public static void closeAllConnections(){
for(Iterator it=connPools.keySet().iterator();it.hasNext();) {
String key=(String)it.next();
BoneCP bcp=(BoneCP)connPools.get(key);
bcp.shutdown();
connPools.remove(key);
}
}
/**
* Get connection count
* @return connection count
*/
public static String getConnectionCount() {
String t="Total Conn:"+count+" total delay:"+delays+" average:"+(delays/count)+" ms";
return t;
}
/**
* Get connection objects
* @return connection objects
*/
public static List<SuperDB> getConnectionObjs() {
return connections;
}
/**
* Reset connection count
*/
public static void connectionCountReset() {
count=0;
delays=0;
}
/**
* Get connection status
* @return connection status
*/
public static String getConnStatus(){
String t="\n";
for(Iterator it=connPools.keySet().iterator();it.hasNext();) {
String key=(String)it.next();
BoneCP bcp=(BoneCP)connPools.get(key);
Statistics st=bcp.getStatistics();
t+="Db:"+key+" --->";
t+=" Total Conn:"+st.getTotalCreatedConnections()+" Free:"+st.getTotalFree()+" Cached hits:"+st.getCacheHits()+" Cached miss:"+st.getCacheMiss()+" Req Conn:"+st.getConnectionsRequested()+" Leased:"+st.getTotalLeased();
t+="\n";
//bcp.shutdown();
//connPools.remove(key);
}
return t;
}
/**
* Get opened connections
* @return connection count
*/
public static String getOpenedConnections() {
return "Size:"+connections.size();
}
/**
* Collect error stack
* @return error stack
* @throws Exception
*/
public static String collectStack() throws Exception {
return collectErrorStack(new Exception().getStackTrace());
}
/**
* Kill connections
* @param minsbefore minutes before
* @throws Exception
*/
public static void killConnections(int minsbefore) throws Exception {
Logger log=LogManager.getLogger(SuperDB.class.getName());
Vector<SuperDB> v=new Vector();
synchronized(connections) {
//System.out.println("------" );
for(SuperDB sdb: connections) {
Calendar opened=Calendar.getInstance(); opened.setTime(sdb.getConnectedDate());
Calendar now=Calendar.getInstance(); now.setTime(new Date());
now.add(Calendar.MINUTE, -minsbefore);
long diff=new Date().getTime()-sdb.getConnectedDate().getTime();
diff=diff/1000;
SimpleDateFormat sdf=new SimpleDateFormat("dd-MMM HH:mm:ss");
//System.out.println("Opened at:"+sdf.format(sdb.getConnectedDate())+" now:"+sdf.format(new Date())+"con id: is expired:"+now.after(opened)+" diff:"+diff+" seconds" );
if(now.after(opened)){
//System.out.println("closing connection: as it is expired");
//sdb.closeDB();
v.add(sdb);
}
}
}
if(v.size()>0){
log.error("~~~~~~Closing "+v.size()+" connections after "+minsbefore+" minutes");
}
for(SuperDB sdb: v) {
sdb.closeDB();
}
}
/**
* Collect error stack
* @return error stack
* @throws Exception
*/
private static String collectErrorStack(StackTraceElement[] sts) throws Exception {
String rtn="";
SimpleDateFormat sdf=new SimpleDateFormat("dd-MMM HH:mm:ss.SSS ");
rtn+="\n-- "+sdf.format(new Date())+" Thread:"+Thread.currentThread().getId()+" - "+Thread.currentThread().getName()+" --\n";
for(int loop=0;loop<sts.length;loop++){
if(sts[loop].getClassName().contains("com.fourelementscapital.")){
rtn+=" " +sts[loop].getClassName()+"."+sts[loop].getMethodName()+"\n";
}
}
return rtn;
}
/**
* Collect all stack
* @return stack
* @throws Exception
*/
public static String collectStack4All() throws Exception {
String rtn="";
for(SuperDB sdb: connections){
rtn+=sdb.getCallStack()+"\n";
}
return rtn;
}
//public BoneCP getConnectionPool(){
// return connPools.get(this.db);
//}
/**
* Set database
* @param db database
*/
public void setDb(String db){
this.db=db;
}
/**
* Get database
* @return database
*/
public String getDb(){
return this.db;
}
/**
* Get driver
* @param db database
* @return driver
*/
public String getDriver(String db) {
return Config.getValue(db+".db_driver");
}
/**
* Get connection
* @return connection
*/
public Connection connection(){
return this.con;
}
/**
* Set read only
* @param flag flag
* @throws Exception
*/
public void setReadOnly(boolean flag) throws Exception {
this.con.setReadOnly(flag);
}
/**
* Check whether read only
* @return true if read only
* @throws Exception
*/
public boolean isReadOnly() throws Exception {
return this.con.isReadOnly();
}
/**
* Close database
* @throws Exception
*/
public void closeDB() throws Exception {
if(this.con!=null){
//if(CONNECTION_POOL_ACTIVE){
//}else{
connections.remove(this);
this.con.close();
this.con=null;
//}
}
}
/**
* Check whether driver is MySQL
* @return true if driver is MySQL
*/
public boolean isMySQLDriver(){
if(this.db!=null && getDriver(this.db)!=null && getDriver(this.db).equals(MY_SQL_DRIVER)){
return true;
}else{
return false;
}
}
/**
* Get contract database class
* @param tablename table name
* @param db database
* @return contract database class
*/
public final ContractDB getContractDB(String tablename, String db) {
this.tablename=tablename;
this.db=db;
//if(this.db!=null && getDriver(this.db)!=null && getDriver(this.db).equals(MY_SQL_DRIVER)){
ContractDBMariaDB cdb=new ContractDBMariaDB(this.tablename);
cdb.setDb(db);
return cdb;
/*
}else{
return null;
}
*/
}
//public final ContractDB getContractDB(String tablename) {
// return null;
//}
/**
* Get utility database class
* @param db database
* @return utility database class
*/
public final UtilDB getUtilDB(String db) {
//return this.utilDB;
this.db=db;
//if(this.db!=null && getDriver(this.db)!=null && getDriver(this.db).equals(MY_SQL_DRIVER)){
UtilDBMariaDB udsql=new UtilDBMariaDB();
udsql.setDb(db);
return udsql;
/*
}else{
return null;
}
*/
}
/**
* Get BBSync database class
* @return BBSync database class
*/
public static BBSyncDB getBBSyncDB() {
return new BBSyncDBMariaDB();
}
/**
* Get Flexi Field database class
* @return Flexi Field database class
*/
public static FlexiFieldDB getFlexiFieldDB() {
return new FlexiFieldDBMariaDB();
}
/**
* Get Reference database class
* @return Reference database class
*/
public static ReferenceDB getReferenceDB() {
return new ReferenceDBMariaDB();
}
/**
* Get Scheduler database class
* @return Scheduler database class
*/
public static SchedulerDB getSchedulerDB() {
return new SchedulerDBMariaDB();
}
/**
* Get RFunction database class
* @return RFunction database class
*/
public static RFunctionDB getRFunctionDB() {
return new RFunctionDBMariaDB();
}
/**
* Get IExec database class
* @return IExec database class
*/
public static IExecDB getIExcecDB() {
return new IExecDBMariaDB();
}
/**
* Get DBManager database class
* @return DBManager database class
*/
public static DBManagerDB getDBManagerDB(){
return new DBManagerDBMariaDB();
}
/**
* Get Util database class
* @return Util database class
*/
public static UtilDB getUtilDB4SQLServer() {
return new UtilDBMariaDB();
}
/**
* Get ConstructQuery database class
* @return ConstructQuery database class
*/
public static ConstructQueryDB getConstructQueryDB() {
return new ConstructQueryDBMariaDB();
}
/**
* Get Infrastructure database class
* @return Infrastructure database class
*/
public static InfrastructureDB getInfrastructureDB() {
return new InfrastructureDBMariaDB();
}
}
+376
View File
@@ -0,0 +1,376 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.db;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.Vector;
import com.fourelementscapital.db.vo.ValueObject;
/**
* Abstract class implements methods related to utility
*/
public abstract class UtilDB extends SuperDB {
/**
* List commodities
* @param con connection
* @return commodities
* @throws Exception
*/
public abstract List listCommodities(Connection con) throws Exception ;
/**
* List commodities with filter
* @param con connection
* @param filter filter
* @return commodities
* @throws Exception
*/
public abstract List listCommoditiesWithFilter(Connection con,String filter) throws Exception;
/**
* List first letters of commodities
* @param con connection
* @return commodities
* @throws Exception
*/
public abstract List listCommoditiesFirstLetters(Connection con) throws Exception ;
/**
* Get unique fields of commodity field original
* @param con connection
* @return commodity field original
* @throws Exception
*/
public abstract List uniqueFields(Connection con) throws Exception ;
/**
* List all tables by commodity field original & commodity
* @param con connection
* @param field_arr field arr
* @param commodity_arr commodity arr
* @return tables
* @throws Exception
*/
public abstract List<Map> listAllTables4Fields(Connection con, String field_arr, String commodity_arr ) throws Exception;
/**
* List all orphaned assets
* @param con connection
* @return tables
* @throws Exception
*/
public abstract List<Map> listAllOrphanedAssets(Connection con ) throws Exception;
/**
* Remove all orphaned assets
* @param con connection
* @throws Exception
*/
public abstract void removeAllOrphanedAssets(Connection con) throws Exception;
/**
* List all table strings by commodities
* @param con connection
* @param fieldtype field type
* @param commodities commodities
* @throws Exception
*/
public abstract List<String> listAllTables4Commodities(Connection con, String fieldtype,Vector<String> commodities) throws Exception;
/**
* Get field original
* @param con connection
* @param tablename table name
* @return commodity field original
* @throws Exception
*/
public abstract String getFieldOriginal(Connection con,String tablename) throws Exception;
/**
* List all table value objects by commodities
* @param con connection
* @param fieldtype field type
* @param commodities commodities
* @throws Exception
*/
public abstract Vector<ValueObject> listAllTables4Commodities2(Connection con, String fieldtype,Vector<String> commodities) throws Exception;
/**
* List all contract titles
* @param con connection
* @param jointables join tables
* @return contract titles
* @throws Exception
*/
public abstract TreeMap<String,Vector> listAllContractTitles(Connection con, String jointables) throws Exception;
/**
* List all contract titles
* @param con connection
* @param jointables join tables
* @return contract titles
* @throws Exception
*/
public abstract TreeMap<String,Vector> listAllContractTitles2(Connection con, String jointables) throws Exception;
/**
* List all contract titles in lower case
* @param con connection
* @param jointables join tables
* @return contract titles
* @throws Exception
*/
public abstract List<String> listAllContractTitles2LCase(Connection con, String jointables) throws Exception;
/**
* Remove commodity and fields
* @param con connection
* @param commodity commodity
* @return true
* @throws Exception
*/
public abstract boolean removeCommodityAndFields(Connection con,String commodity) throws Exception;
/**
* Get raw data by table name, contract & date query
* @param datequery date query
* @param con connection
* @param tablename table name
* @param commodity commodity
* @return raw data
* @throws Exception
*/
public abstract Vector getRawData(String datequery,Connection con,String tablename, String commodity) throws Exception;
/**
* Get record count by contract & commodity
* @param con connection
* @param commodity commodity
* @param contract contract
* @return record count
* @throws Exception
*/
public abstract List getRecordCount4Contracts(Connection con, String commodity, String contract ) throws Exception;
/**
* Get raw data
* @param datequery date query
* @param con connection
* @param tablename table name
* @param commodity commodity
* @return raw data
* @throws Exception
*/
public abstract Map getRawData3(String datequery,Connection con,String tablename, String commodity) throws Exception;
/**
* Get raw data by contract
* @param datequery date query
* @param con connection
* @param fieldname field name
* @param contrat contrat
* @param commodity commodity
* @return raw data
* @throws Exception
*/
public abstract Map getRawData2Contract(String datequery,Connection con,String fieldname, String contrat,String commodity) throws Exception;
/**
* Delete raw data
* @param con connection
* @param tablename table name
* @param data data
* @throws Exception
*/
public abstract void deleteRawData(Connection con,String tablename,Map<String,String> data) throws Exception;
/**
* Update raw data
* @param con connection
* @param tablename table name
* @param olddata old data
* @param newdata new data
* @throws Exception
*/
public abstract void updateRawData(Connection con,String tablename,Map<String,String> olddata,Map<String,String> newdata) throws Exception;
/**
* Remove field table
* @param con connection
* @param ftable field table
* @return true
* @throws Exception
*/
public abstract boolean removeFieldTable(Connection con,String ftable) throws Exception;
/**
* Rename field table
* @param con connection
* @param ftable field table
* @param fieldoriginal field original
* @return true if success
* @throws Exception
*/
public abstract boolean renameFieldTable(Connection con,String ftable, String fieldoriginal) throws Exception;
/**
* List field tables for admin
* @param con connection
* @param mtable commodity
* @return field tables
* @throws Exception
*/
public abstract List listFieldTables4Admin(Connection con,String mtable) throws Exception;
/**
* List field tables
* @param con connection
* @param mtable commodity
* @return field tables
* @throws Exception
*/
public abstract List listFieldTables(Connection con,String mtable) throws Exception;
/**
* List all unique fields
* @param con connection
* @return unique fields
* @throws Exception
*/
public abstract List listAllUniqueFields(Connection con) throws Exception;
/**
* List all unique fields by commodity
* @param con connection
* @param commodity commodity
* @return all unique fields
* @throws Exception
*/
public abstract List listAllUniqueFields(Connection con, String commodity) throws Exception;
/**
* Delete contracts
* @param con connection
* @param field_tablename field table name
* @param contract contract
* @throws Exception
*/
public abstract void deleteContracts(Connection con, String field_tablename, String contract) throws Exception;
/**
* Get unique contracts
* @param con connection
* @param field_tablename field table name
* @return contracts
* @throws Exception
*/
public abstract Vector getUniqueContracts(Connection con, String field_tablename) throws Exception;
/**
* List all commodities and fields
* @param con connection
* @return commodities and fields
* @throws Exception
*/
public abstract Map listAllCommoditiesAndFields(Connection con) throws Exception;
/**
* Last sync update
* @param con connection
* @throws Exception
*/
public abstract void LastSyncUpdate(Connection con) throws Exception;
/**
* Last sync days
* @param con connection
* @param today date
* @return sync date
* @throws Exception
*/
public abstract int lastSyncDays(Connection con,Date today) throws Exception;
/**
* Last sync date
* @param con connection
* @return sync date
* @throws Exception
*/
public abstract Date lastSyncDate(Connection con) throws Exception;
/**
* Get daily strategy data
* @param con connection
* @param date date
* @param contractname contract name
* @param fields fields
* @return strategy data
* @throws Exception
*/
public abstract TreeMap dailyStrategyData(Connection con,String date, String contractname, String fields) throws Exception;
/**
* List saved chart
* @param con connection
* @return saved charts
* @throws Exception
*/
public abstract TreeMap listSavedChart(Connection con) throws Exception;
/**
* Delete saved chart
* @param con connection
* @param id id
* @throws Exception
*/
public abstract void deleteSavedChart(Connection con, int id) throws Exception;
/**
* Get saved chart item
* @param con connection
* @param id id
* @return saved chart
* @throws Exception
*/
public abstract Map getSavedChartItem(Connection con, int id) throws Exception;
/**
* Add saved chart
* @param con connection
* @param data data
* @throws Exception
*/
public abstract void savedChart(Connection con,Map data) throws Exception;
/**
* Show old connections
* @param minutesbefore minutes before
* @param computername computer name
* @return SPID
* @throws Exception
*/
public abstract ArrayList showOldConnections(int minutesbefore, String computername) throws Exception;
/**
* Kill connections
* @param conids connection ids
* @throws Exception
*/
public abstract void killConnections(List<Integer> conids) throws Exception ;
}
+74
View File
@@ -0,0 +1,74 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.db;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
/**
* This class contains Static Utility functions
* @author manas
*
*/
public class Utils {
private static HashMap<String,String> _config4E;
/**
* Accepts an ArrayList of strings and then returns a delimiter separated string
* @param al The arraylist of strings
* @param delimiter The delimiter
* @return The delimiter separated string
*/
public static String Join(ArrayList<String> al,String delimiter)
{
return al.toString().replaceAll("\\[|\\]", "").replaceAll(", ",delimiter);
}
/**
* Gets the variables defined in the global config located at /mnt/public/Libs/.4E.config
* @param variable
* @return
* @throws IOException
*/
public static String getConfig4E(String variable) throws IOException
{
if(_config4E==null) _config4E = new HashMap<String, String>();
if((_config4E.size()==0))
{
//FileReader fr = new FileReader("/mnt/public/Libs/.4E.config");
FileReader fr = new FileReader(Config.getValue("db_config_path"));
BufferedReader br = new BufferedReader(fr);
String s = br.readLine();
while(s!=null)
{
if(s.startsWith("#") || s.trim().equals("")){
s = br.readLine();
continue;
}
String[] splits = s.split("=",2);
_config4E.put(splits[0].trim(), splits[1].trim().replaceAll("\"", ""));
s = br.readLine();
}
}
if(_config4E.containsKey(variable.trim()))
return _config4E.get(variable.trim());
else return null;
}
}
@@ -0,0 +1,691 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.db.mariadb;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.Vector;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import com.fourelementscapital.db.BBSyncDB;
import com.fourelementscapital.db.GeneralUtilDB;
import com.fourelementscapital.db.vo.BBSyncTrigger;
/**
* BBSync MariaDB DAO implementation
*/
public class BBSyncDBMariaDB extends BBSyncDB {
private Logger log=LogManager.getLogger(BBSyncDB.class.getName());
/**
* {@inheritDoc}
*/
public Vector listAll() throws Exception {
Statement st=this.connection().createStatement();
ResultSet rs=st.executeQuery("Select * from bbsync order by name");
Vector rtn=new Vector();
while(rs.next()){
//Map record=new BasicRowProcessor().toMap(rs);
Map record = GeneralUtilDB.resultsetToMap(rs);
int id=(Integer)record.get("id");
record.put("fieldsids",getFieldIds(id));
record.put("tickers", getContracts(id));
rtn.add(record);
}
return rtn;
}
/**
* {@inheritDoc}
*/
public ArrayList fieldMapping4BBSync(int bbsyncid) throws Exception {
PreparedStatement ps=this.connection().prepareStatement("select * from field_mapping where id in (select field_id from bbsync_fields where bbsync_id=?)");
ps.setLong(1, bbsyncid);
ResultSet rs=ps.executeQuery();
ArrayList rtn=new ArrayList();
while(rs.next()){
//Map record=new BasicRowProcessor().toMap(rs);
Map record = GeneralUtilDB.resultsetToMap(rs);
rtn.add(record);
}
rs.close();
ps.close();
return rtn;
}
/**
* {@inheritDoc}
*/
public Map getDownloadQuery(long id) throws Exception {
PreparedStatement ps=this.connection().prepareStatement("Select * from bbsync where id=?");
ps.setLong(1, id);
ResultSet rs=ps.executeQuery();
Map record=null;
if(rs.next()){
//record=new BasicRowProcessor().toMap(rs);
record = GeneralUtilDB.resultsetToMap(rs);
}
rs.close();
ps.close();
if(record!=null){
record.put("fieldsids",getFieldIds(id));
record.put("tickers", getContracts(id));
}
return record;
}
/**
* Get field id
* @param bbsyncid bbsync id
* @return field ids
* @throws Exception
*/
private Vector getFieldIds(long bbsyncid) throws Exception {
PreparedStatement ps1=this.connection().prepareStatement("Select field_id from bbsync_fields where bbsync_id=?");
ps1.setLong(1, bbsyncid);
ResultSet rs1=ps1.executeQuery();
Vector v=new Vector();
while(rs1.next()){
v.add((Integer)rs1.getInt("field_id")+""); //convert to string
}
ps1.close();
rs1.clearWarnings();
return v;
}
/**
* Get contracts
* @param bbsyncid bbsync id
* @return contract
* @throws Exception
*/
private String getContracts(long bbsyncid) throws Exception {
PreparedStatement ps1=this.connection().prepareStatement("Select contract from bbsync_contracts where bbsync_id=?");
ps1.setLong(1, bbsyncid);
ResultSet rs1=ps1.executeQuery();
String tickers="";
while(rs1.next()){
tickers+=(tickers.equals(""))?rs1.getString("contract"):","+rs1.getString("contract");
}
ps1.close();
rs1.clearWarnings();
return tickers;
}
/**
* {@inheritDoc}
*/
public Vector getFieldMapping() throws Exception {
PreparedStatement ps=this.connection().prepareStatement("Select * from field_mapping");
ResultSet rs=ps.executeQuery();
Vector rtn=new Vector();
while(rs.next()){
//rtn.add(new BasicRowProcessor().toMap(rs));
rtn.add(GeneralUtilDB.resultsetToMap(rs));
}
return rtn;
}
/**
* {@inheritDoc}
*/
public void updateTriggeredDate(int id, Timestamp start, Timestamp end) throws Exception {
PreparedStatement ps=this.connection().prepareStatement("UPDATE bbsync SET trigger_startedat=?, trigger_endedat=?,last_executed=? WHERE id=?");
ps.setTimestamp(1, start);
ps.setTimestamp(2, end);
ps.setTimestamp(3, end);
ps.setInt(4, id);
ps.executeUpdate();
ps.close();
}
/**
* {@inheritDoc}
*/
public void deleteFieldMapping(int id) throws Exception {
PreparedStatement ps=this.connection().prepareStatement("DELETE FROM field_mapping where id=?");
ps.setInt(1, id);
ps.executeUpdate();
ps.close();
}
/**
* {@inheritDoc}
*/
public void addFieldMapping(String dbfield, String bbfield) throws Exception {
PreparedStatement ps=this.connection().prepareStatement("INSERT INTO field_mapping(db_field, bb_field) VALUES (?,?)");
ps.setString(1, dbfield);
ps.setString(2, bbfield);
ps.executeUpdate();
ps.close();
}
/**
* {@inheritDoc}
*/
public void updateContractLogs(Vector<String> contract,String marketsector,Timestamp lastsync, Map<String,String> scommodities, Collection fields) throws Exception {
for(Iterator it=fields.iterator();it.hasNext();){
String field=(String)it.next();
PreparedStatement ps3=this.connection().prepareStatement("SELECT * FROM reference.contractSync WHERE contract_name=? AND contract_field=?");
PreparedStatement ps4=this.connection().prepareStatement("INSERT INTO reference.contractSync(last_sync,contract_name,contract_field) VALUES(?,?,?)");
PreparedStatement ps5=this.connection().prepareStatement("UPDATE reference.contractSync SET last_sync=? WHERE contract_name=? AND contract_field=?");
for(Iterator<String> i=contract.iterator();i.hasNext();){
String cont=i.next();
cont=cont.trim()+" "+marketsector;
ps3.setString(1, cont);
ps3.setString(2, field);
ResultSet rs=ps3.executeQuery();
if(rs.next()){
ps5.setTimestamp(1, lastsync);
ps5.setString(2, cont.trim());
ps5.setString(3, field);
ps5.execute();
}else{
ps4.setTimestamp(1, lastsync);
ps4.setString(2, cont.trim());
ps4.setString(3, field);
ps4.execute();
}
rs.close();
}
ps3.close();
ps4.close();
ps5.close();
}
}
/**
* {@inheritDoc}
*/
public void updateSecuritiesLogs(Vector<String> contract,String marketsector,Timestamp lastsync,Collection fields) throws Exception {
for(Iterator it=fields.iterator();it.hasNext();){
String field=(String)it.next();
PreparedStatement ps3=this.connection().prepareStatement("SELECT * FROM reference.SecuritySync WHERE security_name=? AND security_field=?");
PreparedStatement ps4=this.connection().prepareStatement("INSERT INTO reference.SecuritySync(last_sync,security_name,security_field) VALUES(?,?,?)");
PreparedStatement ps5=this.connection().prepareStatement("UPDATE reference.SecuritySync SET last_sync=? WHERE security_name=? AND security_field=?");
for(Iterator<String> i=contract.iterator();i.hasNext();){
String cont=i.next();
cont=cont.trim()+" "+marketsector;
ps3.setString(1, cont);
ps3.setString(2, field);
ResultSet rs=ps3.executeQuery();
if(rs.next()){
ps5.setTimestamp(1, lastsync);
ps5.setString(2, cont.trim());
ps5.setString(3, field);
ps5.execute();
}else{
ps4.setTimestamp(1, lastsync);
ps4.setString(2, cont.trim());
ps4.setString(3, field);
ps4.execute();
}
rs.close();
}
ps3.close();
ps4.close();
ps5.close();
}
}
/**
* {@inheritDoc}
*/
public Vector<String> getContractNames2RefSync(boolean pendingonly) throws Exception {
Statement st=this.connection().createStatement();
ResultSet rs=null;
if(pendingonly){
rs=st.executeQuery("select * from contract_info WHERE ref_synchronized<=0 OR ref_synchronized is NULL");
}else{
rs=st.executeQuery("select * from contract_info");
}
Vector<String> v=new Vector();
while(rs.next()){
v.add(rs.getString("name"));
}
rs.close();
st.close();
return v;
}
/**
* {@inheritDoc}
*/
public Map<String,String> getSecurityNames2RefSync(boolean pendingonly) throws Exception {
Statement st=this.connection().createStatement();
ResultSet rs=null;
if(pendingonly){
rs=st.executeQuery("select * from security_info WHERE ref_synchronized<=0 OR ref_synchronized is NULL");
}else{
rs=st.executeQuery("select * from security_info");
}
LinkedHashMap<String,String> rtn=new LinkedHashMap();
while(rs.next()){
String name=rs.getString("name");
String marketsector=rs.getString("marketsector");
rtn.put( name+" "+marketsector,name);
}
rs.close();
st.close();
return rtn;
}
/**
* {@inheritDoc}
*/
public Map<String,String> getSecurityName2RefSync(String ticker) throws Exception {
Statement st=this.connection().createStatement();
ResultSet rs=null;
rs=st.executeQuery("select * from security_info where name='"+ticker+"'");
LinkedHashMap<String,String> rtn=new LinkedHashMap();
while(rs.next()){
String name=rs.getString("name");
String marketsector=rs.getString("marketsector");
rtn.put( name+" "+marketsector,name);
}
rs.close();
st.close();
return rtn;
}
/**
* {@inheritDoc}
*/
public void updateContractReference(String contractname, Map<String,String> fielddata ) throws Exception {
PreparedStatement ps=this.connection().prepareStatement("SELECT * FROM contract_info WHERE name=?");
//PreparedStatement ps1=this.connection().prepareStatement("INSERT INTO contract_info(ref_synchronized,first_notice_date,last_trade_date,name) VALUES(?,?,?,?)");
SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
Date fndate=null;
Date ltdate=null;
if(fielddata.get("FUT_LAST_TRADE_DT")!=null){
ltdate=new Date(sf.parse(fielddata.get("FUT_LAST_TRADE_DT")).getTime());
}
if(fielddata.get("FUT_NOTICE_FIRST")!=null){
fndate=new Date(sf.parse(fielddata.get("FUT_NOTICE_FIRST")).getTime());
}
ps.setString(1, contractname);
ResultSet rs=ps.executeQuery();
PreparedStatement ps1=null;
int thisid=0;
boolean updatemode=true;
if(rs.next()){
ps1=this.connection().prepareStatement("UPDATE contract_info SET ref_synchronized=?,first_notice_date=?,last_trade_date=? WHERE name=?");
PreparedStatement ps11=this.connection().prepareStatement("DELETE FROM contract_info_fields WHERE contract_id=(select id from contract_info where name=? limit 1)" );
ps11.setString(1, contractname);
ps11.execute();
ps11.close();
PreparedStatement st12=this.connection().prepareStatement("select id from contract_info where name=?");
st12.setString(1,contractname);
ResultSet rs12=st12.executeQuery();
if(rs12.next()){
thisid=rs12.getInt("id");
}
rs12.close();
st12.close();
}else{
ps1=this.connection().prepareStatement("INSERT INTO contract_info(ref_synchronized,first_notice_date,last_trade_date,name) VALUES(?,?,?,?)",Statement.RETURN_GENERATED_KEYS);
updatemode=false;
}
rs.close();
ps1.setInt(1, 1);
ps1.setDate(2, fndate);
ps1.setDate(3, ltdate);
ps1.setString(4, contractname);
ps1.executeUpdate();
if(!updatemode){
ResultSet rs1 = ps1.getGeneratedKeys();
if(rs1.next()){
thisid=rs1.getInt(1);
}
rs1.close();
}
ps1.close();
if(thisid>0){
PreparedStatement ps3=this.connection().prepareStatement("INSERT INTO contract_info_fields(bb_fieldname,value,contract_id) VALUES(?,?,?)");
for(Iterator<String> it=fielddata.keySet().iterator();it.hasNext();){
String bbfield=it.next();
ps3.setString(1,bbfield);
ps3.setString(2,fielddata.get(bbfield));
ps3.setInt(3, thisid);
ps3.executeUpdate();
}
ps3.close();
}
}
/**
* {@inheritDoc}
*/
public void updateSecurityReference(String securityname, String marketsector, Map<String,String> fielddata ) throws Exception {
PreparedStatement ps=this.connection().prepareStatement("SELECT * FROM security_info WHERE name=?");
SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
Date fndate=null;
Date ltdate=null;
ps.setString(1, securityname);
ResultSet rs=ps.executeQuery();
PreparedStatement ps1=null;
int thisid=0;
boolean updatemode=true;
if(rs.next()){
ps1=this.connection().prepareStatement("UPDATE security_info SET ref_synchronized=? WHERE name=?");
PreparedStatement ps11=this.connection().prepareStatement("DELETE FROM security_info_fields WHERE security_id=(select id from security_info where name=? limit 1)" );
ps11.setString(1, securityname);
ps11.execute();
ps11.close();
PreparedStatement st12=this.connection().prepareStatement("select id from security_info where name=?");
st12.setString(1,securityname);
ResultSet rs12=st12.executeQuery();
if(rs12.next()){
thisid=rs12.getInt("id");
}
rs12.close();
st12.close();
}else{
ps1=this.connection().prepareStatement("INSERT INTO security_info(ref_synchronized, name,marketsector) VALUES(?,?,?)",Statement.RETURN_GENERATED_KEYS);
updatemode=false;
}
rs.close();
ps1.setInt(1, 1);
ps1.setString(2, securityname);
if(!updatemode){
ps1.setString(3, marketsector);
}
ps1.executeUpdate();
if(!updatemode){
ResultSet rs1 = ps1.getGeneratedKeys();
if(rs1.next()){
thisid=rs1.getInt(1);
}
rs1.close();
}
ps1.close();
if(thisid>0){
PreparedStatement ps3=this.connection().prepareStatement("INSERT INTO security_info_fields(bb_fieldname,value,security_id) VALUES(?,?,?)");
for(Iterator<String> it=fielddata.keySet().iterator();it.hasNext();){
String bbfield=it.next();
ps3.setString(1,bbfield);
ps3.setString(2,fielddata.get(bbfield));
ps3.setInt(3, thisid);
ps3.executeUpdate();
}
ps3.close();
}
}
/**
* {@inheritDoc}
*/
public void addSyncLogs(int bbsyncid, Timestamp start, Timestamp end, String message, String status, String manual_scheduler) throws Exception {
String dquery="DELETE FROM bbsync_logs WHERE bbsync_id=? AND id NOT IN (select id from (select id from bbsync_logs WHERE bbsync_id=? ORDER BY start_time DESC limit 11) temp_table)";
PreparedStatement st=this.connection().prepareStatement(dquery);
st.setInt(1, bbsyncid);
st.setInt(2, bbsyncid);
st.executeUpdate();
st.close();
PreparedStatement ps=this.connection().prepareStatement("INSERT INTO bbsync_logs(bbsync_id,start_time,end_time,message,status,manual_scheduler) VALUES(?,?,?,?,?,?)");
ps.setInt(1, bbsyncid);
ps.setTimestamp(2, start);
ps.setTimestamp(3, end);
ps.setString(4,message);
ps.setString(5,status);
ps.setString(6,manual_scheduler);
ps.executeUpdate();
ps.close();
}
/**
* {@inheritDoc}
*/
public void removeScheduleTicker(int bbsync_id, String ticker) throws Exception {
PreparedStatement ps=this.connection().prepareStatement("DELETE FROM bbsync_contracts WHERE bbsync_id=? AND contract=?" );
ps.setInt(1, bbsync_id);
ps.setString(2, ticker.trim());
ps.executeUpdate();
ps.close();
}
/**
* {@inheritDoc}
*/
public int saveSchedule(
int id,
String name,
String mkt_secdb,
String dateoption,
Date datefrom,
Date dateto,
int datenumber,
Vector fields,
String contracts,
BBSyncTrigger t,
String marketsector,
String timezone
) throws Exception {
log.debug("saveSchedule() called:");
String query1;
PreparedStatement ps;
if(id>0){
query1="UPDATE bbsync SET name=?,is_mkt_securitydb=?,date_option=?,date_recentnumber=?,date_from=?,date_to=?,tickers=?,trigger_type=?,fields=?,trigger_time=?,trigger_days=?,trigger_day=?,marketsector=?,trigger_week=?,trigger_dailyhour=?,timezone=? WHERE id=?";
ps=this.connection().prepareStatement(query1);
}else{
query1="INSERT INTO bbsync(name,is_mkt_securitydb,date_option,date_recentnumber,date_from,date_to,tickers,trigger_type,fields,trigger_time,trigger_days,trigger_day,marketsector,trigger_week,trigger_dailyhour,timezone) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ";
ps=this.connection().prepareStatement(query1,Statement.RETURN_GENERATED_KEYS);
}
log.debug("connection:"+connection());
log.debug("ps:"+ps);
log.debug("t:"+t);
log.debug("query:"+query1);
log.debug("id:"+id);
ps.setString(1, name);
ps.setString(2, mkt_secdb);
ps.setString(3, dateoption);
ps.setInt(4, datenumber);
ps.setDate(5, datefrom);
ps.setDate(6, dateto);
//ps.setString(7, contracts);
ps.setString(7, "");
ps.setInt(8, t.getSynctype());
ps.setString(9, ""); // to be removed later.
ps.setString(10, t.getTime());
ps.setString(11, t.getDays());
//ps.setString(13, t.getMonths());
ps.setInt(12, t.getDay());
ps.setString(13, marketsector);
ps.setInt(14, (t.getWeek()!=null)?t.getWeek():0 );
ps.setInt(15, (t.getDailyhour()!=null)?t.getDailyhour():0);
ps.setString(16, timezone);
if(id>0){
ps.setInt(17, id);
ps.executeUpdate();
}else{
ps.executeUpdate();
ResultSet rs = ps.getGeneratedKeys();
if(rs.next()){
id=rs.getInt(1);
}
rs.close();
}
ps.close();
if(id>0){
Statement st=this.connection().createStatement();
st.execute("DELETE FROM bbsync_fields WHERE bbsync_id="+id);
st.close();
PreparedStatement ps2=this.connection().prepareStatement("INSERT into bbsync_fields(bbsync_id,field_id) values(?,?)");
for(int a=0;a<fields.size();a++){
ps2.setInt(1, id);
ps2.setInt(2, Integer.parseInt((String)fields.get(a)));
ps2.executeUpdate();
}
ps2.close();
ArrayList tickers= parseFreeTextTokens(contracts);
Statement st1=this.connection().createStatement();
st1.execute("DELETE FROM bbsync_contracts WHERE bbsync_id="+id);
st1.close();
PreparedStatement ps3=this.connection().prepareStatement("INSERT into bbsync_contracts(bbsync_id,contract) values(?,?)");
for(int a=0;a<tickers.size();a++){
ps3.setInt(1, id);
ps3.setString(2, ((String)tickers.get(a)).trim());
ps3.executeUpdate();
}
ps3.close();
}
return id;
}
/**
* {@inheritDoc}
*/
public void deleteQuery(int id) throws Exception {
if(id>0){
Statement st=this.connection().createStatement();
st.execute("DELETE FROM bbsync_fields WHERE bbsync_id="+id);
st.close();
Statement st1=this.connection().createStatement();
st1.execute("DELETE FROM bbsync_contracts WHERE bbsync_id="+id);
st1.close();
Statement st2=this.connection().createStatement();
st2.execute("DELETE FROM bbsync WHERE id="+id);
st2.close();
}
}
/**
* {@inheritDoc}
*/
public void peerStarted(String peername, long sessionid, java.util.Date time) throws Exception {
Timestamp ts=new Timestamp(time.getTime());
PreparedStatement ps=this.connection().prepareStatement("INSERT INTO peer_sessions(peername, sessionid,started_time) VALUES (?,?,?)");
ps.setString(1, peername);
ps.setLong(2, sessionid);
ps.setTimestamp(3,ts );
ps.executeUpdate();
ps.close();
}
/**
* {@inheritDoc}
*/
public void peerStopped(String peername,long sessionid, java.util.Date time) throws Exception {
Timestamp ts=new Timestamp(time.getTime());
PreparedStatement ps=this.connection().prepareStatement("UPDATE peer_sessions SET stopped_time=? WHERE peername=? AND sessionid=?");
ps.setTimestamp(1,ts );
ps.setString(2, peername);
ps.setLong(3, sessionid);
ps.executeUpdate();
ps.close();
}
/**
* Parse free text to list by token
* @param stringToken string token
* @return text separated by token in list
*/
private ArrayList parseFreeTextTokens(String stringToken){
ArrayList rtn=new ArrayList();
if(stringToken!=null && !stringToken.equals("")){
String pline=fieldSeparator4FreeText(stringToken);
StringTokenizer st=new StringTokenizer(stringToken,pline);
while(st.hasMoreTokens()){
rtn.add(st.nextToken());
}
}
return rtn;
}
/**
* Get field separator from free text
* @param stringToken string token
* @return field separator
*/
private String fieldSeparator4FreeText(String stringToken){
return (stringToken.indexOf(",")>=0)?",":"\r\n";
}
}
@@ -0,0 +1,216 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.db.mariadb;
import java.util.Map;
import com.fourelementscapital.db.ConstructQueryDB;
/**
* Construct Query MariaDB DAO implementation
*/
public class ConstructQueryDBMariaDB extends ConstructQueryDB {
/**
* {@inheritDoc}
*/
public String constructDateInputQuery(Map rtnobj) {
if (rtnobj == null) {
return null;
}
String q = null;
String filtertype = rtnobj.get("filtertype") == null ? "" : rtnobj.get("filtertype").toString();
if ("daterange".equals(filtertype)) {
String dobj1Success = rtnobj.get("dobj1Success").toString();
String dobj2Success = rtnobj.get("dobj2Success").toString();
String cdatefield = rtnobj.get("cdatefield").toString();
String dobj1Sqldate = rtnobj.get("dobj1Sqldate").toString();
String dobj2Sqldate = rtnobj.get("dobj2Sqldate").toString();
if ("true".equals(dobj1Success) && "true".equals(dobj2Success)) {
q = cdatefield+" BETWEEN '"+ dobj1Sqldate +"' AND '"+ dobj2Sqldate +"'";
}
}
else if ("datefrom".equals(filtertype)) {
String dobj1Success = rtnobj.get("dobj1Success").toString();
String cdatefield = rtnobj.get("cdatefield").toString();
String dobj1Sqldate = rtnobj.get("dobj1Sqldate").toString();
if ("true".equals(dobj1Success)) {
q = cdatefield+" BETWEEN '"+ dobj1Sqldate +"' AND now() ";
}
}
else if ("number".equals(filtertype)) {
String filtervalue = rtnobj.get("filtervalue").toString();
String numbr = rtnobj.get("number").toString();
String cdatefield = rtnobj.get("cdatefield").toString();
if ("ndays".equals(filtervalue)) {
q = cdatefield+">=DATE_ADD(NOW(), interval -"+numbr+" day)";
}
else if ("nweeks".equals(filtervalue)) {
q = cdatefield+">=DATE_ADD(NOW(), interval -"+numbr+" week)";
}
else if ("nmonths".equals(filtervalue)) {
q = cdatefield+">=DATE_ADD(NOW(), interval -"+numbr+" month)";
}
else if ("nyears".equals(filtervalue)) {
q = cdatefield+">=DATE_ADD(NOW(), interval -"+numbr+" year)";
}
}
else {
Object filtervalue = rtnobj.get("filtervalue");
String cdatefield = rtnobj.get("cdatefield").toString();
if (filtervalue!=null && !"".equals(filtervalue.toString()) && cdatefield!=null && !"".equals(cdatefield.toString())) {
q = constructRecentQuery(cdatefield, filtervalue.toString());
}
else {
q = null;
}
}
return q;
}
/**
* {@inheritDoc}
*/
public String constructRecentQuery(String field, String dateQuery) {
if (field == null || "".equals(field)) {
return null;
}
if (dateQuery == null || "".equals(dateQuery)) {
return null;
}
String q = null;
if ("last1Hour".equals(dateQuery)) {
q = field + ">=DATE_ADD(NOW(), interval -1 hour)";
}
else if ("last2Hour".equals(dateQuery)) {
q = field + ">=DATE_ADD(NOW(), interval -2 hour)";
}
else if ("last5Hour".equals(dateQuery)) {
q = field + ">=DATE_ADD(NOW(), interval -5 hour)";
}
else if ("last10Hour".equals(dateQuery)) {
q = field + ">=DATE_ADD(NOW(), interval -10 hour)";
}
else if ("last1Day".equals(dateQuery)) {
q = field + ">=DATE_ADD(NOW(), interval -1 day)";
}
else if ("last2Day".equals(dateQuery)) {
q = field + ">=DATE_ADD(NOW(), interval -2 day)";
}
else if ("last3Day".equals(dateQuery)) {
q = field + ">=DATE_ADD(NOW(), interval -3 day)";
}
else if ("last1Week".equals(dateQuery)) {
q = field + ">=DATE_ADD(NOW(), interval -1 week)";
}
else if ("last10Week".equals(dateQuery)) {
q = field + ">=DATE_ADD(NOW(), interval -10 week)";
}
else if ("last1Month".equals(dateQuery)) {
q = field + ">=DATE_ADD(NOW(), interval -1 month)";
}
else if ("last2Month".equals(dateQuery)) {
q = field + ">=DATE_ADD(NOW(), interval -2 month)";
}
else if ("last3Month".equals(dateQuery)) {
q = field + ">=DATE_ADD(NOW(), interval -3 month)";
}
else if ("last6Month".equals(dateQuery)) {
q = field + ">=DATE_ADD(NOW(), interval -6 month)";
}
else if ("last12Month".equals(dateQuery)) {
q = field + ">=DATE_ADD(NOW(), interval -12 month)";
}
else if ("last18Month".equals(dateQuery)) {
q = field + ">=DATE_ADD(NOW(), interval -18 month)";
}
else if ("last24Month".equals(dateQuery)) {
q = field + ">=DATE_ADD(NOW(), interval -24 month)";
}
else if ("last3Year".equals(dateQuery)) {
q = field + ">=DATE_ADD(NOW(), interval -3 year)";
}
else if ("last4Year".equals(dateQuery)) {
q = field + ">=DATE_ADD(NOW(), interval -4 year)";
}
else if ("last5Year".equals(dateQuery)) {
q = field + ">=DATE_ADD(NOW(), interval -5 year)";
}
else if ("last10Year".equals(dateQuery)) {
q = "trigger_datetime>=DATE_ADD(NOW(), interval -10 year)";
}
return q;
}
/**
* {@inheritDoc}
*/
public String constructQueueHistoryQuery(Map rtnobj) {
if (rtnobj == null) {
return null;
}
String q = null;
//q = rtnobj.get("query") == null ? "null" : rtnobj.get("query").toString();
q = constructDateInputQuery(rtnobj);
String statfilter = rtnobj.get("statfilter") == null ? "" : rtnobj.get("statfilter").toString();
String scd_typefilter = rtnobj.get("scd_typefilter") == null ? "" : rtnobj.get("scd_typefilter").toString();
if ("true".equals(rtnobj.get("queryCheck").toString())) {
if(!"".equals(statfilter)){
if("{null}".equals(statfilter)){
q+=" AND a.status IS NULL";
}else if("#success".equals(statfilter)){
q+=" AND (a.status<>'success' OR a.status IS NULL )";
}else {
q+=" AND a.status='"+statfilter+"'";
}
}
q += (!"".equals(scd_typefilter)) ? " AND b.taskuid='"+ scd_typefilter +"'" : "";
String fld1 = rtnobj.get("fld1") == null ? "" : rtnobj.get("fld1").toString();
String scd_filterfieldval = rtnobj.get("scd_filterfieldval") == null ? "" : rtnobj.get("scd_filterfieldval").toString();
String q1 = "";
if(!"".equals(fld1) && !"tag".equals(fld1) && !"".equals(scd_filterfieldval)){
String val1=scd_filterfieldval;
if("name".equals(fld1)){q1=" b.name LIKE '%"+val1+"%' ";}
if("scheduler_id".equals(fld1)){q1=" a.scheduler_id="+val1+"";}
q += ("".equals(q1)) ? "" : " AND "+ q1 +" ";
}
String scd_filterddval = rtnobj.get("scd_filterddval") == null ? "" : rtnobj.get("scd_filterddval").toString();
if("host".equals(fld1) && !"".equals(scd_filterddval)) {
q1 = " a.host='"+ scd_filterddval +"'";
q += ("".equals(q1)) ? "" : " AND "+ q1 +" ";
}
if("tag".equals(fld1) && !"".equals(scd_filterddval)) {
String val1 = rtnobj.get("scd_filterddval") == null ? "" : rtnobj.get("scd_filterddval").toString();
q1=" a.scheduler_id IN(SELECT scheduler_id from scheduler_tags where tag_id="+val1+") ";
q += ("".equals(q1)) ? "" : " AND "+ q1 +" ";
}
}
return q;
}
}
@@ -0,0 +1,382 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.db.mariadb;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import com.fourelementscapital.db.ContractDB;
import com.fourelementscapital.db.GeneralUtilDB;
import com.fourelementscapital.db.vo.Contract;
import com.fourelementscapital.db.vo.Strategy;
/**
* Contract MySQL DAO implementation
*/
public class ContractDBMariaDB extends ContractDB {
private Logger log=LogManager.getLogger(ContractDB.class.getName());
private String tablename;
/**
* Constructor
* @param tablename table name
*/
public ContractDBMariaDB(String tablename){
this.tablename=tablename;
}
/**
* Constructor
*/
public ContractDBMariaDB(){
}
/**
* {@inheritDoc}
*/
public void updateRecords(Connection con,Vector records) throws Exception {
PreparedStatement st=con.prepareStatement("INSERT into "+this.tablename+" (cdate,contract,val) VALUES(?,?,?) ");
PreparedStatement st1=con.prepareStatement("SELECT cdate FROM "+this.tablename+" WHERE cdate=? AND contract=?");
PreparedStatement st2=con.prepareStatement("UPDATE "+this.tablename+" SET val=? WHERE cdate=? AND contract=?");
log.debug("records: size:"+records.size());
for(Iterator<Contract> i=records.iterator();i.hasNext();){
Contract contract=i.next();
log.debug("contract: date():"+contract.getCdate());
st1.setDate(1, new java.sql.Date(contract.getCdate().getTime()));
st1.setString(2, contract.getName());
if(!st1.executeQuery().next()){
st.setDate(1, new java.sql.Date(contract.getCdate().getTime()));
st.setString(2, contract.getName());
st.setDouble(3, contract.getValue());
st.executeUpdate();
}else{
st2.setDouble(1, contract.getValue());
st2.setDate(2, new java.sql.Date(contract.getCdate().getTime()));
st2.setString(3, contract.getName());
st2.executeUpdate();
}
}
st1.close();
st2.close();
st.close();
}
/**
* {@inheritDoc}
*/
public void addRecords(Connection con,Vector records) throws Exception {
log.debug("records: size:"+records.size());
PreparedStatement st=con.prepareStatement("INSERT into "+this.tablename+"(cdate,contract,val) VALUES(?,?,?) ");
for(Iterator<Contract> i=records.iterator();i.hasNext();){
Contract contract=i.next();
log.debug("contract: date():"+contract.getCdate());
log.debug("contract: sqldbs():"+new java.sql.Date(contract.getCdate().getTime()));
log.debug("contract: this.tablename():"+this.tablename);
st.setDate(1, new java.sql.Date(contract.getCdate().getTime()));
st.setString(2, contract.getName());
st.setDouble(3, contract.getValue());
st.executeUpdate();
}
st.close();
}
/**
* {@inheritDoc}
*/
public void createTable(Connection con, int decimalpoint) throws Exception {
String query="CREATE TABLE IF NOT EXISTS "+this.tablename +" ( ";
query+="cdate datetime NOT NULL ,";
query+="contract varchar(50), ";
query+="val numeric(20,"+decimalpoint+") ";
query+=") ";
//System.out.println("ContractDB.class: query:"+query);
Statement st=con.createStatement();
st.execute(query);
st.close();
}
/*
* implemented but that of changing to different field.
public void createNewContract(Connection con,String subtablename, String contract) throws Exception {
String query="IF NOT EXISTS (select * from sysobjects where id = object_id('asset_contracts') and OBJECTPROPERTY(id, 'IsUserTable') = 1) ";
query+="BEGIN ";
query+="CREATE TABLE asset_contracts ( ";
query+="tablename varchar(50) ,";
query+="contract varchar(50) ";
query+=") ";
query+="END ";
Statement st=con.createStatement();
st.execute(query);
st.close();
PreparedStatement st1=con.prepareStatement("INSERT into asset_contracts(tablename,contract) VALUES(?,?) ");
st1.setString(1,subtablename);
st1.setString(2, contract);
st1.executeUpdate();
st1.close();
}
public List getCustomContracts(Connection con,String subtablename) throws Exception {
String query="select * from sysobjects where id = object_id('asset_contracts') and OBJECTPROPERTY(id, 'IsUserTable') = 1 ";
Vector rtn=new Vector();
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(query);
if(rs.next()){
Statement st1=con.createStatement();
ResultSet rs1=st1.executeQuery("select contract from asset_contracts WHERE tablename='"+subtablename+"'");
while(rs1.next()){
rtn.add(rs.getString("contract"));
}
rs1.close();
st1.close();
}
rs.close();
st.close();
return rtn;
}
*/
/**
* {@inheritDoc}
*/
public void updateSValRecords(Connection con,Vector records) throws Exception {
PreparedStatement st=con.prepareStatement("INSERT into "+this.tablename+"(cdate,contract,val,sval) VALUES(?,?,?,?) ");
PreparedStatement st1=con.prepareStatement("SELECT cdate FROM "+this.tablename+" WHERE cdate=? AND contract=?");
PreparedStatement st2=con.prepareStatement("UPDATE "+this.tablename+" SET val=?,sval=? WHERE cdate=? AND contract=?");
for(Iterator<Strategy> i=records.iterator();i.hasNext();){
Strategy contract=i.next();
st1.setDate(1, new java.sql.Date(contract.getCdate().getTime()));
st1.setString(2, contract.getName());
if(!st1.executeQuery().next()){
st.setDate(1, new java.sql.Date(contract.getCdate().getTime()));
st.setString(2, contract.getName());
if(contract.getSvalue()!=null){
st.setObject(3, null);
}else{
st.setDouble(3, contract.getValue());
}
st.setString(4, contract.getSvalue());
st.executeUpdate();
}else{
if(contract.getSvalue()!=null){
st2.setObject(1, null);
}else{
st2.setDouble(1, contract.getValue());
}
st2.setString(2, contract.getSvalue());
st2.setDate(3, new java.sql.Date(contract.getCdate().getTime()));
st2.setString(4, contract.getName());
st2.executeUpdate();
}
}
st1.close();
st2.close();
st.close();
}
/**
* {@inheritDoc}
*/
public void addSValRecords(Connection con,Vector records) throws Exception {
PreparedStatement st=con.prepareStatement("INSERT into "+this.tablename+"(cdate,contract,sval) VALUES(?,?,?) ");
for(Iterator<Strategy> i=records.iterator();i.hasNext();){
Strategy contract=i.next();
st.setDate(1, new java.sql.Date(contract.getCdate().getTime()));
st.setString(2, contract.getName());
//st.setDouble(3, contract.getValue());
st.setString(3, contract.getSvalue());
st.executeUpdate();
}
st.close();
}
/**
* {@inheritDoc}
*/
public void createSValTable(Connection con, int decimalpoint) throws Exception {
String query="CREATE TABLE IF NOT EXISTS "+this.tablename +" ( ";
query+="cdate datetime NOT NULL ,";
query+="contract varchar(50), ";
query+="sval varchar(50), ";
query+="val numeric(20,"+decimalpoint+") ";
query+=") ";
//System.out.println("ContractDB.class: query:"+query);
Statement st=con.createStatement();
st.execute(query);
st.close();
}
/**
* {@inheritDoc}
*/
public int checkSValueField(Connection con) throws Exception {
String query="select count(*) as cnum from "+this.tablename+" where val is not null";
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(query);
int valcount=1;
if(rs.next()){
valcount=rs.getInt("cnum");
}
rs.close();
st.close();
return valcount;
}
/**
* {@inheritDoc}
*/
public int countRecords(Connection con) throws Exception {
String query="select count(*) as cnum from "+this.tablename+" ";
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(query);
int valcount=1;
if(rs.next()){
valcount=rs.getInt("cnum");
}
rs.close();
st.close();
return valcount;
}
/**
* {@inheritDoc}
*/
public boolean checkSValueFieldTypeExist(Connection con) throws Exception {
String query="SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '"+this.tablename+"' AND COLUMN_NAME = 'sval' AND table_schema = '"+getDb()+"'";
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(query);
boolean exist=false;
if(rs.next()){
exist=true;
}
return exist;
}
/**
* {@inheritDoc}
*/
public List getContractTitles(Connection con) throws Exception {
Vector rtn=new Vector();
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("SELECT distinct contract FROM "+this.tablename);
while(rs.next()){
rtn.add(rs.getString("contract"));
}
rs.close();
st.close();
return rtn;
}
/**
* {@inheritDoc}
*/
public List getContractTitles(Connection con, String datequery) throws Exception {
Vector rtn=new Vector();
Statement st=con.createStatement();
datequery=GeneralUtilDB.convertFromSQL(datequery);
ResultSet rs=st.executeQuery("SELECT distinct contract FROM "+this.tablename+((datequery!=null && !datequery.equals(""))? " WHERE "+datequery:""));
while(rs.next()){
rtn.add(rs.getString("contract"));
}
rs.close();
st.close();
return rtn;
}
/**
* {@inheritDoc}
*/
public void updateMasterTable(Connection con,String mtable,String fieldname) throws Exception {
String query="CREATE TABLE IF NOT EXISTS asset_master ( ";
query+="commodity varchar(50) NOT NULL ,";
query+="commodity_field varchar(100), ";
query+="commodity_fieldoriginal varchar(100) ";
query+=") ";
Statement st=con.createStatement();
st.execute(query);
st.close();
PreparedStatement st1=con.prepareStatement("SELECT commodity FROM asset_master WHERE commodity=? AND commodity_field=?");
PreparedStatement st2=con.prepareStatement("INSERT into asset_master(commodity,commodity_field,commodity_fieldoriginal) VALUES(?,?,?) ");
st1.setString(1, mtable);
st1.setString(2, this.tablename);
if(!st1.executeQuery().next()){
st2.setString(1, mtable);
st2.setString(2, this.tablename);
st2.setString(3, fieldname);
st2.executeUpdate();
}
st1.close();
st2.close();
}
/**
* {@inheritDoc}
*/
public String generateSQLXLQueryPlainQuery(String fieldtable, int nmonths, List contractTitleList) {
String rtn="SELECT cdate ";
for(Iterator<String> it=contractTitleList.iterator();it.hasNext();){
String contract=it.next();
rtn+=",\r\nMAX(CASE WHEN contract='"+contract+"' THEN val END) AS "+contract+" ";
}
rtn+="\r\nFROM "+fieldtable+" WHERE cdate>=DATE_ADD(CURRENT_TIMESTAMP(), INTERVAL -" + nmonths + " month) GROUP BY cdate ORDER BY cdate DESC";
return rtn;
}
}
@@ -0,0 +1,39 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.db.mariadb;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import java.util.Vector;
import com.fourelementscapital.db.DBManagerDB;
import com.fourelementscapital.db.GeneralUtilDB;
/**
* DBManager MariaDB DAO implementation
*/
public class DBManagerDBMariaDB extends DBManagerDB{
/**
* {@inheritDoc}
*/
public List listDBGroups() throws Exception {
PreparedStatement ps=this.connection().prepareStatement("Select * from dbman_group ORDER BY disp_order");
ResultSet rs=ps.executeQuery();
Vector rtn=new Vector();
while(rs.next()){
//rtn.add(new BasicRowProcessor().toMap(rs));
rtn.add(GeneralUtilDB.resultsetToMap(rs));
}
return rtn;
}
}
@@ -0,0 +1,152 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.db.mariadb;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import org.apache.commons.dbutils.BasicRowProcessor;
import com.fourelementscapital.db.FlexiFieldDB;
import com.fourelementscapital.db.GeneralUtilDB;
import com.fourelementscapital.db.vo.FlexiField;
/**
* FlexiField MariaDB DAO implementation
*/
public class FlexiFieldDBMariaDB extends FlexiFieldDB {
/**
* {@inheritDoc}
*/
public List getFlexiFields(String tablename) throws Exception {
PreparedStatement pst=this.connection().prepareStatement("select * from flexi_field WHERE tablename=? ORDER BY displayorder");
pst.setString(1, tablename);
ResultSet rs=pst.executeQuery();
List<FlexiField> list= new BasicRowProcessor().toBeanList(rs, FlexiField.class);
return list;
}
/**
* {@inheritDoc}
*/
public void addFlexiField(FlexiField ffield) throws Exception {
PreparedStatement pst=this.connection().prepareStatement("insert into flexi_field(fieldlabel,fieldtype,fieldoptions,tablename) values(?,?,?,?)");
pst.setString(1, ffield.getFieldlabel());
pst.setString(2, ffield.getFieldtype());
pst.setString(3, ffield.getFieldoptions());
pst.setString(4, ffield.getTablename());
pst.executeUpdate();
}
/**
* {@inheritDoc}
*/
public void updateFlexiField(long id,FlexiField ffield) throws Exception {
PreparedStatement pst=this.connection().prepareStatement("UPDATE flexi_field SET fieldlabel=?,fieldoptions=? WHERE id=?");
pst.setString(1, ffield.getFieldlabel());
pst.setString(2, ffield.getFieldoptions());
pst.setLong(3, id);
pst.executeUpdate();
}
/**
* {@inheritDoc}
*/
public void updateFlexiFieldOrder(long id,int order) throws Exception {
PreparedStatement pst=this.connection().prepareStatement("UPDATE flexi_field SET displayorder=? WHERE id=?");
pst.setInt(1, order);
pst.setLong(2,id);
pst.executeUpdate();
}
/**
* {@inheritDoc}
*/
public void deleteFlexiField(long id) throws Exception {
PreparedStatement pst=this.connection().prepareStatement("DELETE FROM flexi_field WHERE id=?");
pst.setLong(1, id);
pst.executeUpdate();
}
/**
* {@inheritDoc}
*/
public Map getFlexiFieldData (String tablename, String idfield, String idvalue) throws Exception {
PreparedStatement pst=this.connection().prepareStatement("select * from "+tablename+" WHERE "+idfield+"=?");
pst.setString(1, idvalue);
ResultSet rs=pst.executeQuery();
HashMap data=new HashMap();
while(rs.next()){
long flexid= rs.getLong("flexi_field_id");
String val=rs.getString("val");
data.put(flexid, val);
}
return data;
}
/**
* {@inheritDoc}
*/
public Vector<Map> getFlexiFieldData4LuceneToken (String tablename, String idfield, String idvalue) throws Exception {
String query="select a.*,b.fieldlabel from "+tablename+" as a left outer join flexi_field as b on b.id=a.flexi_field_id WHERE a."+idfield+"=?";
PreparedStatement pst=this.connection().prepareStatement(query);
//Log.debug("getFlexiFieldData4LuceneToken() query:"+query);
pst.setString(1, idvalue);
ResultSet rs=pst.executeQuery();
Vector rtn=new Vector();
while(rs.next()){
//Map rdata= new BasicRowProcessor().toMap(rs);
Map rdata = GeneralUtilDB.resultsetToMap(rs);
rtn.add(rdata);
}
return rtn;
}
/**
* {@inheritDoc}
*/
public void saveFlexiFieldData (String tablename, String idfield, String idvalue, Map data) throws Exception {
PreparedStatement pst1=this.connection().prepareStatement("DELETE FROM "+tablename+" WHERE "+idfield+"=?" );
pst1.setString(1, idvalue);
pst1.executeUpdate();
pst1.close();
PreparedStatement pst=this.connection().prepareStatement("insert into "+tablename+"(flexi_field_id,"+idfield+",val) values (?,?,?)");
for(Iterator i=data.keySet().iterator();i.hasNext();){
Object ky=i.next();
Object val=data.get(ky);
Long fid=Long.parseLong(ky+"");
pst.setLong(1, fid);
pst.setString(2, idvalue);
pst.setString(3, val+"");
pst.execute();
}
pst1.close();
}
}
@@ -0,0 +1,497 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.db.mariadb;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import org.apache.commons.dbutils.BasicRowProcessor;
import com.fourelementscapital.db.IExecDB;
/**
* IExec MariaDB DAO implementation
*/
public class IExecDBMariaDB extends IExecDB{
/**
* {@inheritDoc}
*/
public List listGroups() throws Exception {
//PreparedStatement ps=this.connection().prepareStatement("Select * from `ie_group` ORDER BY disp_order");
PreparedStatement ps=this.connection().prepareStatement("Select * from ie_group ORDER BY disp_order");
ResultSet rs=ps.executeQuery();
Vector rtn=new Vector();
while(rs.next()){
rtn.add(new BasicRowProcessor().toMap(rs));
}
return rtn;
}
/**
* {@inheritDoc}
*/
public void setGroupOrder( Vector groupids) throws Exception {
//PreparedStatement ps1=this.connection().prepareStatement("UPDATE `ie_group` SET disp_order=? WHERE group_uid=?");
PreparedStatement ps1=this.connection().prepareStatement("UPDATE ie_group SET disp_order=? WHERE group_uid=?");
int count=0;
for(Iterator<String> i=groupids.iterator();i.hasNext();){
ps1.setInt(1, count++);
ps1.setString(2, i.next());
ps1.executeUpdate();
}
}
/**
* {@inheritDoc}
*/
public List listFolders( ) throws Exception {
PreparedStatement ps=null;
ps=this.connection().prepareStatement("Select * FROM ie_folder ORDER BY folder_name");
ResultSet rs=ps.executeQuery();
Vector rtn=new Vector();
while(rs.next()){
rtn.add(new BasicRowProcessor().toMap(rs));
}
return rtn;
}
/**
* {@inheritDoc}
*/
public String getFolderName(int folder_id) throws Exception {
PreparedStatement ps=this.connection().prepareStatement("Select folder_name FROM ie_folder WHERE id=?");
ps.setInt(1,folder_id);
ResultSet rs=ps.executeQuery();
String rtn=null;
while(rs.next()){
rtn=rs.getString(1);
}
return rtn;
}
/**
* {@inheritDoc}
*/
public void renameStrategy(int strategy_id, String strategy_name, String file_name) throws Exception {
PreparedStatement ps=this.connection().prepareStatement("UPDATE ie_strategy SET strategy_name=?,file_name=? WHERE id=?");
ps.setString(1, strategy_name);
ps.setString(2, file_name);
ps.setInt(3,strategy_id);
ps.executeUpdate();
}
/**
* {@inheritDoc}
*/
public void renameParentStrategy(int strategy_id, String strategy_name, String file_name, String old_strategy_name) throws Exception {
PreparedStatement ps=this.connection().prepareStatement("UPDATE ie_strategy SET strategy_name=?,file_name=? WHERE id=?");
ps.setString(1, strategy_name);
ps.setString(2, file_name);
ps.setInt(3,strategy_id);
ps.executeUpdate();
PreparedStatement ps2=this.connection().prepareStatement("UPDATE ie_strategy SET parent_strategy=? WHERE parent_strategy=?");
ps2.setString(1, strategy_name);
ps2.setString(2, old_strategy_name);
ps2.executeUpdate();
}
/**
* {@inheritDoc}
*/
public void moveFolder(int folder_id, String new_group_id) throws Exception{
PreparedStatement ps=this.connection().prepareStatement("UPDATE ie_folder SET group_uid = ? WHERE id=?");
ps.setString(1, new_group_id);
ps.setInt(2,folder_id);
ps.executeUpdate();
ps.close();
}
/**
* {@inheritDoc}
*/
public void updateStrategyFolder(int strategy_id,int new_folder_id) throws Exception {
PreparedStatement ps=this.connection().prepareStatement("UPDATE ie_strategy SET folder_id=? WHERE id=?");
ps.setInt(1,new_folder_id);
ps.setInt(2, strategy_id);
ps.executeUpdate();
ps.close();
}
/**
* {@inheritDoc}
*/
public List listStrategies() throws Exception {
PreparedStatement ps=this.connection().prepareStatement("Select a.*,b.folder_name FROM ie_strategy as a left outer join ie_folder as b on a.folder_id=b.id order by a.parent_strategy, a.strategy_name");
ResultSet rs=ps.executeQuery();
Vector rtn=new Vector();
while(rs.next()){
rtn.add(new BasicRowProcessor().toMap(rs));
}
return rtn;
}
/**
* {@inheritDoc}
*/
public Map getStrategy(int strategy_id) throws Exception {
//PreparedStatement ps=this.connection().prepareStatement("Select * FROM r_function WHERE id=?");
PreparedStatement ps=this.connection().prepareStatement("Select a.*,b.folder_name FROM ie_strategy as a left outer join ie_folder as b on a.folder_id=b.id WHERE a.id=? ORDER BY a.strategy_name ");
ps.setInt(1, strategy_id);
ResultSet rs=ps.executeQuery();
Map rtn=null;
if(rs.next()){
rtn=new BasicRowProcessor().toMap(rs);
}
return rtn;
}
/**
* {@inheritDoc}
*/
public Vector getUniqueContracts(String strategy_name) throws Exception {
PreparedStatement ps=this.connection().prepareStatement("Select DISTINCT contract FROM ie_parameter WHERE strategy_name=? ");
ps.setString(1,strategy_name);
ResultSet rs=ps.executeQuery();
Vector rtn=new Vector();
while(rs.next()){
rtn.add(rs.getString("contract"));
}
return rtn;
}
/**
* {@inheritDoc}
*/
public void addParameters(ArrayList<Map> data,String strategy_name) throws Exception {
PreparedStatement ps1=this.connection().prepareStatement("DELETE FROM ie_parameter WHERE strategy_name=?");
ps1.setString(1,strategy_name);
ps1.executeUpdate();
PreparedStatement ps=this.connection().prepareStatement("INSERT INTO ie_parameter(strategy_name,contract,placeholder,value) VALUES(?,?,?,?)");
for(Iterator i=data.iterator();i.hasNext();){
Map row=(Map)i.next();
ps.setString(1, (String)row.get("strategy_name"));
ps.setString(2, (String)row.get("contract"));
ps.setString(3, (String)row.get("placeholder"));
ps.setString(4, (String)row.get("value"));
ps.executeUpdate();
}
}
/**
* {@inheritDoc}
*/
public void removeContract(String strategy_name, String contract) throws Exception {
PreparedStatement ps1=this.connection().prepareStatement("DELETE FROM ie_parameter WHERE strategy_name=? AND contract=?");
ps1.setString(1,strategy_name);
ps1.setString(2,contract);
ps1.executeUpdate();
}
/**
* {@inheritDoc}
*/
public Map getParameterValues(String strategy_name, String contract) throws Exception {
PreparedStatement ps=this.connection().prepareStatement("Select placeholder,value FROM ie_parameter WHERE strategy_name=? AND contract=?");
ps.setString(1, strategy_name);
ps.setString(2, contract);
ResultSet rs=ps.executeQuery();
HashMap h=new HashMap();
while(rs.next()){
h.put(rs.getString("placeholder"), rs.getString("value"));
}
rs.close();
ps.close();
return h;
}
/**
* {@inheritDoc}
*/
public Map getStrategy(String strategy_name) throws Exception {
PreparedStatement ps=this.connection().prepareStatement("Select * FROM ie_strategy WHERE strategy_name=?");
ps.setString(1, strategy_name);
ResultSet rs=ps.executeQuery();
Map rtn=null;
if(rs.next()){
rtn=new BasicRowProcessor().toMap(rs);
}
return rtn;
}
/**
* {@inheritDoc}
*/
public Vector getStrategies(Vector strategy_names) throws Exception {
//PreparedStatement ps=this.connection().prepareStatement("Select * FROM r_function WHERE id=?");
String sfunc="''";
for(Iterator i=strategy_names.iterator();i.hasNext();){
sfunc+=",'"+i.next()+"'";
}
PreparedStatement ps=this.connection().prepareStatement("Select * FROM ie_strategy WHERE strategy_name IN ("+sfunc+") ORDER BY strategy_name ");
ResultSet rs=ps.executeQuery();
Vector rtn=new Vector();
while(rs.next()){
rtn.add(new BasicRowProcessor().toMap(rs));
}
return rtn;
}
/**
* {@inheritDoc}
*/
public int createStrategy(int folder_id,String strategy_name, String path) throws Exception {
PreparedStatement ps=this.connection().prepareStatement("INSERT INTO ie_strategy(folder_id,strategy_name,file_name) VALUES(?,?,?)",Statement.RETURN_GENERATED_KEYS);
ps.setInt(1,folder_id);
ps.setString(2, strategy_name);
ps.setString(3, path);
ps.executeUpdate();
int id=0;
ResultSet generatedKeys = ps.getGeneratedKeys();
if (generatedKeys.next()) {
id=generatedKeys.getInt(1);
}
ps.close();
return id;
}
/**
* {@inheritDoc}
*/
public int createChildStrategy(String parent_name,String strategy_name) throws Exception {
PreparedStatement ps=this.connection().prepareStatement("INSERT INTO ie_strategy(parent_strategy,strategy_name) VALUES(?,?)",Statement.RETURN_GENERATED_KEYS);
ps.setString(1,parent_name);
ps.setString(2, strategy_name);
ps.executeUpdate();
int id=0;
ResultSet generatedKeys = ps.getGeneratedKeys();
if (generatedKeys.next()) {
id=generatedKeys.getInt(1);
}
ps.close();
return id;
}
/**
* {@inheritDoc}
*/
public int getFolderID(String folder_name) throws Exception {
PreparedStatement ps=null;
ps=this.connection().prepareStatement("Select * FROM ie_folder WHERE folder_name=?");
ps.setString(1, folder_name);
ResultSet rs=ps.executeQuery();
int rtn=0;
if(rs.next()){
rtn=rs.getInt("id");
}
return rtn;
}
/**
* {@inheritDoc}
*/
public int createFolder(String folder, String new_group_id) throws Exception{
PreparedStatement ps=this.connection().prepareStatement("INSERT INTO ie_folder(folder_name,group_uid) VALUES(?,?)",Statement.RETURN_GENERATED_KEYS);
ps.setString(1, folder);
ps.setString(2, new_group_id);
ps.executeUpdate();
ResultSet generatedKeys = ps.getGeneratedKeys();
int id=0;
if (generatedKeys.next()) {
id=generatedKeys.getInt(1);
}
ps.close();
return id;
}
/**
* {@inheritDoc}
*/
public List listOfFolders(String group_id) throws Exception {
PreparedStatement ps=null;
ps=this.connection().prepareStatement("Select * FROM ie_folder WHERE group_uid=? ORDER BY folder_name");
ps.setString(1, group_id);
ResultSet rs=ps.executeQuery();
Vector rtn=new Vector();
while(rs.next()){
rtn.add(new BasicRowProcessor().toMap(rs));
}
return rtn;
}
/*
public Vector getContractTree() throws Exception {
StringBuffer sb = new StringBuffer();
sb.append("SELECT contract,commodity as commodity FROM trading.tblContractAllocation WHERE commodity in (SELECT commodity from tradingRef.commodityRef) ");
sb.append("AND date= ");
sb.append("CASE DAYOFWEEK(SUBDATE(CURRENT_DATE, 1)) ");
sb.append(" WHEN 1 THEN SUBDATE(CURRENT_DATE, 3) ");
sb.append(" WHEN 7 THEN SUBDATE(CURRENT_DATE, 2) ");
sb.append(" ELSE SUBDATE(CURRENT_DATE, 1) ");
sb.append("END ");
sb.append("ORDER BY commodity, year ASC, month ASC ");
PreparedStatement ps=this.connection().prepareStatement(sb.toString());
ResultSet rs=ps.executeQuery();
Vector rtn=new Vector();
while(rs.next()){
rtn.add(new BasicRowProcessor().toMap(rs));
}
return rtn;
}
*/
/**
* {@inheritDoc}
*/
public Vector getContractTree() throws Exception {
StringBuffer sb = new StringBuffer();
/*sb.append("SELECT contract,commodity as commodity FROM " + DB_NAME_TRADING + ".dbo.tblContractAllocation WHERE commodity in (SELECT commodity from " + DB_NAME_TRADINGREF + ".dbo.commodityRef) ");
sb.append("AND date= ");
sb.append("CASE DAYOFWEEK(SUBDATE(CURRENT_DATE, 1)) ");
sb.append(" WHEN 1 THEN SUBDATE(CURRENT_DATE, 3) ");
sb.append(" WHEN 7 THEN SUBDATE(CURRENT_DATE, 2) ");
sb.append(" ELSE SUBDATE(CURRENT_DATE, 1) ");
sb.append("END ");
sb.append("ORDER BY commodity, year ASC, month ASC ");*/
sb.append("SELECT contract,commodity as commodity ");
sb.append("FROM trading.dbo.tblContractAllocation ");
sb.append("WHERE commodity in (SELECT commodity from tradingRef.dbo.commodityRef) ");
sb.append("AND date= CASE DATEPART(w,DATEADD(DAY,1,CONVERT(date, GETDATE()))) ");
sb.append("WHEN 1 THEN DATEADD(DAY,-3,CONVERT(date, GETDATE())) ");
sb.append("WHEN 7 THEN DATEADD(DAY,-2,CONVERT(date, GETDATE())) ");
sb.append("ELSE DATEADD(DAY,-1,CONVERT(date, GETDATE())) END ");
sb.append("ORDER BY commodity, year ASC, month ASC");
PreparedStatement ps=this.connection().prepareStatement(sb.toString());
ResultSet rs=ps.executeQuery();
Vector rtn=new Vector();
while(rs.next()){
rtn.add(new BasicRowProcessor().toMap(rs));
}
return rtn;
}
/*
public Vector<String> getCommodityTree() throws Exception {
String query="SELECT commodity from tradingRef.commodityRef ORDER BY orderNo";
PreparedStatement ps=this.connection().prepareStatement(query);
ResultSet rs=ps.executeQuery();
Vector rtn=new Vector();
while(rs.next()){
//rtn.add(new BasicRowProcessor().toMap(rs));
rtn.add(rs.getString("commodity"));
}
return rtn;
}
*/
/**
* {@inheritDoc}
*/
public Vector<String> getCommodityTree() throws Exception {
String query="SELECT commodity from " + DB_NAME_TRADINGREF + ".dbo.commodityRef ORDER BY orderNo";
PreparedStatement ps=this.connection().prepareStatement(query);
ResultSet rs=ps.executeQuery();
Vector rtn=new Vector();
while(rs.next()){
//rtn.add(new BasicRowProcessor().toMap(rs));
rtn.add(rs.getString("commodity"));
}
return rtn;
}
/**
* {@inheritDoc}
*/
public boolean isParent(String strategy_name) throws Exception {
boolean rtn=false;
String query="select * from ie_strategy where parent_strategy=?";
PreparedStatement ps=this.connection().prepareStatement(query);
ps.setString(1, strategy_name);
ResultSet rs=ps.executeQuery();
if(rs.next()){
//rtn.add(new BasicRowProcessor().toMap(rs));
rtn=true;
}
return rtn;
}
}
@@ -0,0 +1,140 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.db.mariadb;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.apache.commons.dbutils.BasicRowProcessor;
import com.fourelementscapital.db.InfrastructureDB;
/**
* Infrastructure MariaDB DAO implementation
*/
public class InfrastructureDBMariaDB extends InfrastructureDB {
/**
* {@inheritDoc}
*/
public Map getThemes4Users(String user) throws Exception {
//String query="select * from tblTeamOrganization where theme in ('bb','port','itools')";
String query="select * from tblTeamOrganization ";
PreparedStatement ps=this.connection().prepareStatement(query);
ResultSet rs=ps.executeQuery();
ResultSetMetaData rm= rs.getMetaData();
ArrayList<String> columns=new ArrayList();
for(int i=0;i<rm.getColumnCount();i++){
String column=rm.getColumnName(i+1);
columns.add(column);
}
TreeMap<String, String> themes=new TreeMap();
while(rs.next()){
String leter=rs.getString(user);
String theme=rs.getString("theme");
if(leter!=null && !leter.trim().equals("")){
themes.put(theme, leter);
}
}
return themes;
}
/**
* {@inheritDoc}
*/
public Map getTeamOrg(String themes, List hierarchy) throws Exception {
//String query="select * from tblTeamOrganization where theme in ('bb','port','itools')";
String query="select * from tblTeamOrganization where theme in ("+themes+")";
PreparedStatement ps=this.connection().prepareStatement(query);
ResultSet rs=ps.executeQuery();
ResultSetMetaData rm= rs.getMetaData();
ArrayList<String> columns=new ArrayList();
for(int i=0;i<rm.getColumnCount();i++){
String column=rm.getColumnName(i+1);
columns.add(column);
}
TreeMap<String, String> usrs=new TreeMap();
while(rs.next()){
for(String user:columns){
String val=rs.getString(user);
if(val!=null && !val.trim().equals("") ){
if(usrs.get(user)!=null){
String old=usrs.get(user);
int oldorder=hierarchy.indexOf(old);
int neworder=hierarchy.indexOf(val);
if(neworder<oldorder){
usrs.put(user,val);
}
}else{
usrs.put(user,val);
}
}
}
}
return usrs;
}
/**
* {@inheritDoc}
*/
public void updateBloomberDailyCounter(String date, int count) throws Exception {
String query="INSERT INTO tblInfrastructureInfo (date, BBGDailyCounter) VALUES ("+ date +","+ count +") ON DUPLICATE KEY UPDATE BBGDailyCounter=BBGDailyCounter+" + count;
PreparedStatement ps=this.connection().prepareStatement(query);
ps.execute();
ps.close();
}
/**
* {@inheritDoc}
*/
public List<String> getThemeByUsernameAccess(String username) throws Exception {
String query="select 'thm-'+theme from tblTeamOrganization where "+username+" IN ('X','B','C'))";
PreparedStatement ps=this.connection().prepareStatement(query);
ResultSet rs=ps.executeQuery();
ArrayList rtn=new ArrayList();
while(rs.next()){
rtn.add(new BasicRowProcessor().toMap(rs));
}
rs.close();
ps.close();
return rtn;
}
/**
* {@inheritDoc}
*/
public List getAccessByAppAndUsername(String application, String username) throws Exception {
List al=new ArrayList();
String query="SELECT access FROM ApplicationUserAccess WHERE application=? and username=?";
PreparedStatement ps=this.connection().prepareStatement(query);
ps.setString(1, application);
ps.setString(2, username);
ResultSet rs=ps.executeQuery();
while(rs.next()){
al.add(rs.getString(1));
}
rs.close();
ps.close();
return al;
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,307 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.db.mariadb;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.Vector;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import com.fourelementscapital.db.GeneralUtilDB;
import com.fourelementscapital.db.ReferenceDB;
import com.fourelementscapital.db.vo.CommodityInfo;
/**
* Reference MariaDB DAO implementation
*/
public class ReferenceDBMariaDB extends ReferenceDB {
private Logger log=LogManager.getLogger(ReferenceDB.class.getName());
/**
* {@inheritDoc}
*/
public Map getComContracts() throws Exception {
Statement st=this.connection().createStatement();
//ResultSet rs=st.executeQuery("select left(ltrim(name),2) as com,name from contract_info order by name,last_trade_date");
ResultSet rs=st.executeQuery("select ifnull(commodity_specify, left(ltrim(name),2)) as com,name from contract_info order by name,last_trade_date");
Vector commodity=new Vector();
TreeMap contract=new TreeMap();
while(rs.next()){
String com=rs.getString("com");
String cname=rs.getString("name");
Vector t=null;
if(commodity.contains(com)){
t=(Vector)contract.get(com);
}else{
t=new Vector();
contract.put(com,t);
commodity.add(com);
}
t.add(cname);
}
st.close();
rs.close();
TreeMap rtn=new TreeMap();
rtn.put("commodity", commodity);
rtn.put("contract", contract);
return rtn;
}
/**
* {@inheritDoc}
*/
public void deleteFieldMapping(int id) throws Exception {
PreparedStatement ps=this.connection().prepareStatement("DELETE FROM static_field_mapping WHERE id=?");
ps.setInt(1, id);
ps.executeUpdate();
ps.close();
}
/**
* {@inheritDoc}
*/
public void addFieldMapping(String dbfield, String bbfield) throws Exception {
PreparedStatement ps=this.connection().prepareStatement("INSERT INTO static_field_mapping(friendly_name, bb_fieldname) VALUES (?,?)");
ps.setString(1, dbfield);
ps.setString(2, bbfield);
ps.executeUpdate();
ps.close();
}
/**
* {@inheritDoc}
*/
public Vector getFieldMapping() throws Exception {
PreparedStatement ps=this.connection().prepareStatement("Select * from static_field_mapping");
ResultSet rs=ps.executeQuery();
Vector rtn=new Vector();
while(rs.next()){
//rtn.add(new BasicRowProcessor().toMap(rs));
rtn.add(GeneralUtilDB.resultsetToMap(rs));
}
return rtn;
}
/**
* {@inheritDoc}
*/
public void deleteSecFieldMapping(int id) throws Exception {
PreparedStatement ps=this.connection().prepareStatement("DELETE FROM security_static_fieldmapping WHERE id=?");
ps.setInt(1, id);
ps.executeUpdate();
ps.close();
}
/**
* {@inheritDoc}
*/
public void addSecFieldMapping(String dbfield, String bbfield) throws Exception {
PreparedStatement ps=this.connection().prepareStatement("INSERT INTO security_static_fieldmapping(friendly_name, bb_fieldname) VALUES (?,?)");
ps.setString(1, dbfield);
ps.setString(2, bbfield);
ps.executeUpdate();
ps.close();
}
/**
* {@inheritDoc}
*/
public Vector getSecFieldMapping() throws Exception {
PreparedStatement ps=this.connection().prepareStatement("Select * from security_static_fieldmapping");
ResultSet rs=ps.executeQuery();
Vector rtn=new Vector();
while(rs.next()){
//rtn.add(new BasicRowProcessor().toMap(rs));
rtn.add(GeneralUtilDB.resultsetToMap(rs));
}
return rtn;
}
/**
* {@inheritDoc}
*/
public void updateCommodity(CommodityInfo comm) throws Exception {
PreparedStatement ps=this.connection().prepareStatement("SELECT * from commodity_info WHERE commodity=?");
ps.setString(1, comm.getCommodity());
ResultSet rs=ps.executeQuery();
String query="INSERT INTO commodity_info(bbticker,fepricesample,fetradeunit,glsample,glticker,multiplefactorbn,name,nepricesample,netradeunit,nename,sector,units,multiplefactor,commodity) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
if(rs.next()){
query="UPDATE commodity_info SET bbticker=?,fepricesample=?,fetradeunit=?,glsample=?,glticker=?,multiplefactorbn=?,name=?,nepricesample=?,netradeunit=?,nename=?,sector=?,units=?,multiplefactor=? WHERE commodity=?";
}
ps.close();
PreparedStatement ps1=this.connection().prepareStatement(query);
ps1.setString(1,comm.getBbticker());
ps1.setString(2,comm.getFepricesample());
ps1.setString(3,comm.getFetradeunit());
ps1.setString(4,comm.getGlsample());
ps1.setString(5,comm.getGlticker());
ps1.setString(6,comm.getMultiplefactorbn());
ps1.setString(7,comm.getName());
ps1.setString(8,comm.getNepricesample());
ps1.setString(9,comm.getNetradeunit());
ps1.setString(10,comm.getNename());
ps1.setString(11,comm.getSector());
ps1.setString(12,comm.getUnits());
ps1.setString(13,comm.getMultiplefactor());
ps1.setString(14,comm.getCommodity());
ps1.executeUpdate();
ps1.close();
}
/**
* {@inheritDoc}
*/
public Map getCommodityInfo(String commodity) throws Exception {
PreparedStatement ps=this.connection().prepareStatement("Select * from commodity_info WHERE commodity=?");
ps.setString(1, commodity);
ResultSet rs=ps.executeQuery();
Map record=null;
while(rs.next()){
//record=new BasicRowProcessor().toMap(rs);
record = GeneralUtilDB.resultsetToMap(rs);
}
return record;
}
/**
* {@inheritDoc}
*/
public Map getContractInfo(String contract) throws Exception {
PreparedStatement ps=this.connection().prepareStatement("Select * from contract_info WHERE ltrim(name)=?");
ps.setString(1, contract.trim());
ResultSet rs=ps.executeQuery();
Map record=null;
while(rs.next()){
//record=new BasicRowProcessor().toMap(rs);
record = GeneralUtilDB.resultsetToMap(rs);
}
return record;
}
/**
* {@inheritDoc}
*/
public Map getSecurityInfo(String security) throws Exception {
PreparedStatement ps=this.connection().prepareStatement("Select * from security_info WHERE ltrim(name)=?");
ps.setString(1, security.trim());
ResultSet rs=ps.executeQuery();
Map record=null;
while(rs.next()){
//record=new BasicRowProcessor().toMap(rs);
record = GeneralUtilDB.resultsetToMap(rs);
}
return record;
}
/**
* {@inheritDoc}
*/
public Vector getContractBBInfo(String contract) throws Exception {
//PreparedStatement ps=this.connection().prepareStatement("select bb_fieldname,value from contract_info_fields where contract_id in (select id from contract_info where name=?)");
PreparedStatement ps=this.connection().prepareStatement("select b.friendly_name,value from contract_info_fields as a left outer join static_field_mapping as b on a.bb_fieldname=b.bb_fieldname where a.contract_id in (select id from contract_info where name=?)");
ps.setString(1, contract);
ResultSet rs=ps.executeQuery();
Vector rtn=new Vector();
while(rs.next()){
//Map record=new BasicRowProcessor().toMap(rs);
Map record = GeneralUtilDB.resultsetToMap(rs);
rtn.add(record);
}
return rtn;
}
/**
* {@inheritDoc}
*/
public Vector getSecurityBBInfo(String security) throws Exception {
//PreparedStatement ps=this.connection().prepareStatement("select bb_fieldname,value from contract_info_fields where contract_id in (select id from contract_info where name=?)");
PreparedStatement ps=this.connection().prepareStatement("select b.friendly_name,value,b.bb_fieldname from security_info_fields as a left outer join security_static_fieldmapping as b on a.bb_fieldname=b.bb_fieldname where a.security_id in (select id from security_info where name=?)");
ps.setString(1, security);
ResultSet rs=ps.executeQuery();
Vector rtn=new Vector();
while(rs.next()){
//Map record=new BasicRowProcessor().toMap(rs);
Map record = GeneralUtilDB.resultsetToMap(rs);
rtn.add(record);
}
return rtn;
}
/**
* {@inheritDoc}
*/
public Map getAllSecFieldValues(String friendlyname) throws Exception {
log.debug("getAllSecFieldValue() called");
PreparedStatement ps=this.connection().prepareStatement("select a.name,b.value from security_info as a left outer join security_info_fields as b on a.id=b.security_id left outer join security_static_fieldmapping as c on c.bb_fieldname=b.bb_fieldname where c.friendly_name=?");
ps.setString(1, friendlyname);
ResultSet rs=ps.executeQuery();
LinkedHashMap<String,String> rtn=new LinkedHashMap();
while(rs.next()){
String sec=rs.getString("name");
//String fname=rs.getString("friendly_name");
String fval=rs.getString("value");
rtn.put(sec, fval);
/*
if(fname!=null ){
Map<String,String> record=null;
if(rtn.containsKey(sec)){
record=rtn.get(sec);
}else{
record=new LinkedHashMap();
rtn.put(sec, record);
}
record.put(fname, fval);
}
*/
}
//log.debug("record:"+rtn);
return rtn;
}
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,125 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.db.vo;
/**
* Trigger POJO (Bean)
*/
public class BBSyncTrigger {
public static final int HOURLY=1;
public static final int WEEKLY=2;
public static final int MONTHLY=3;
private int synctype;
private String time;
private String days;
private int day;
private Integer week;
private Integer dailyhour;
/**
* Get daily hour
* @return daily hour
*/
public Integer getDailyhour() {
return dailyhour;
}
/**
* Set daily hour
* @param dailyhour daily hour
*/
public void setDailyhour(Integer dailyhour) {
this.dailyhour = dailyhour;
}
/**
* Get week
* @return week
*/
public Integer getWeek() {
return week;
}
/**
* Set week
* @param week week
*/
public void setWeek(Integer week) {
this.week = week;
}
/**
* Get days
* @return days
*/
public String getDays() {
return days;
}
/**
* Set days
* @param days days
*/
public void setDays(String days) {
this.days = days;
}
/**
* Get sync type
* @return sync type
*/
public int getSynctype() {
return synctype;
}
/**
* Set sync type
* @param synctype sync type
*/
public void setSynctype(int synctype) {
this.synctype = synctype;
}
/**
* Get time
* @return time
*/
public String getTime() {
return time;
}
/**
* Set time
* @param time time
*/
public void setTime(String time) {
this.time = time;
}
/**
* Get day
* @return day
*/
public int getDay() {
return day;
}
/**
* Set day
* @param day day
*/
public void setDay(int day) {
this.day = day;
}
}
@@ -0,0 +1,276 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.db.vo;
import java.io.Serializable;
/**
* Commodity POJO (Bean)
*/
public class CommodityInfo implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String bbticker;
private String fepricesample;
private String fetradeunit;
private String glsample;
private String glticker;
private String multiplefactor;
private String multiplefactorbn;
private String name;
private String nepricesample;
private String netradeunit;
/**
* Get multiple factor
* @return multiple factor
*/
public String getMultiplefactor() {
return multiplefactor;
}
/**
* Set multiple factor
* @param multiplefactor multiple factor
*/
public void setMultiplefactor(String multiplefactor) {
this.multiplefactor = multiplefactor;
}
private String nename;
private String sector;
private String units;
private String commodity;
/**
* Get commodity
* @return commodity
*/
public String getCommodity() {
return commodity;
}
/**
* Set commodity
* @param commodity commodity
*/
public void setCommodity(String commodity) {
this.commodity = commodity;
}
/**
* Get id
* @return id
*/
public Long getId() {
return id;
}
/**
* Set id
* @param id id
*/
public void setId(Long id) {
this.id = id;
}
/**
* Get bb ticker
* @return bb ticker
*/
public String getBbticker() {
return bbticker;
}
/**
* Set bb ticker
* @param bbticker bb ticker
*/
public void setBbticker(String bbticker) {
this.bbticker = bbticker;
}
/**
* Get fe price sample
* @return fe price sample
*/
public String getFepricesample() {
return fepricesample;
}
/**
* Set fe price sample
* @param fepricesample fe price sample
*/
public void setFepricesample(String fepricesample) {
this.fepricesample = fepricesample;
}
/**
* Get fe trade unit
* @return fe trade unit
*/
public String getFetradeunit() {
return fetradeunit;
}
/**
* Set fe trade unit
* @param fetradeunit fe trade unit
*/
public void setFetradeunit(String fetradeunit) {
this.fetradeunit = fetradeunit;
}
/**
* Get gl sample
* @return gl sample
*/
public String getGlsample() {
return glsample;
}
/**
* Set gl sample
* @param glsample gl sample
*/
public void setGlsample(String glsample) {
this.glsample = glsample;
}
/**
* Get gl ticker
* @return gl ticker
*/
public String getGlticker() {
return glticker;
}
/**
* Set gl ticker
* @param glticker gl ticker
*/
public void setGlticker(String glticker) {
this.glticker = glticker;
}
/**
* Get multiple factor bn
* @return multiple factor bn
*/
public String getMultiplefactorbn() {
return multiplefactorbn;
}
/**
* Set multiple factor bn
* @param multiplefactorbn multiple factor bn
*/
public void setMultiplefactorbn(String multiplefactorbn) {
this.multiplefactorbn = multiplefactorbn;
}
/**
* Get name
* @return name
*/
public String getName() {
return name;
}
/**
* Set name
* @param name name
*/
public void setName(String name) {
this.name = name;
}
/**
* Get ne price sample
* @return ne price sample
*/
public String getNepricesample() {
return nepricesample;
}
/**
* Set ne price sample
* @param nepricesample ne price sample
*/
public void setNepricesample(String nepricesample) {
this.nepricesample = nepricesample;
}
/**
* Get ne trade unit
* @return ne trade unit
*/
public String getNetradeunit() {
return netradeunit;
}
/**
* Set ne trade unit
* @param netradeunit ne trade unit
*/
public void setNetradeunit(String netradeunit) {
this.netradeunit = netradeunit;
}
/**
* Get ne name
* @return ne name
*/
public String getNename() {
return nename;
}
/**
* Set ne name
* @param nename ne name
*/
public void setNename(String nename) {
this.nename = nename;
}
/**
* Get sector
* @return sector
*/
public String getSector() {
return sector;
}
/**
* Set sector
* @param sector sector
*/
public void setSector(String sector) {
this.sector = sector;
}
/**
* Get units
* @return units
*/
public String getUnits() {
return units;
}
/**
* Set units
* @param units units
*/
public void setUnits(String units) {
this.units = units;
}
}
@@ -0,0 +1,78 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.db.vo;
import java.util.Date;
/**
* Contract POJO (Bean)
*/
public class Contract {
private String name;
private double value;
private Date cdate;
/**
* Get name
* @return name
*/
public String getName() {
return name;
}
/**
* Set name
* @param name name
*/
public void setName(String name) {
this.name = name;
}
/**
* Get value
* @return value
*/
public double getValue() {
return value;
}
/**
* Set value
* @param value value
*/
public void setValue(double value) {
this.value = value;
}
/**
* Get cdate
* @return cdate
*/
public Date getCdate() {
return cdate;
}
/**
* Set cdate
* @param cdate cdate
*/
public void setCdate(Date cdate) {
this.cdate = cdate;
}
/**
* Overriding toString() method
* @return string
*/
public String toString(){
return "Name:"+this.name+", CDate:"+this.cdate+", Value:"+this.value;
}
}
@@ -0,0 +1,130 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.db.vo;
/**
* Flexible field POJO (Bean)
*/
public class FlexiField {
public final static String TYPE_TEXTBOX ="textbox";
public final static String TYPE_DROP_DOWNLIST ="dropdown";
public final static String TYPE_CHECKBOX ="checkbox";
public final static String TYPE_RADIO ="radio";
public final static String TYPE_FREETEXT ="textarea";
public final static String TYPE_HIDDENFIED ="hidden";
public final static String TYPE_RSCRIPTEDITOR ="rscript";
public final static String TYPE_RSCRIPTEDITOR_PARAM ="rscript_param";
public final static String TYPE_RHINOSCRIPTEDITOR ="rhinoscript";
public final static String TYPE_BLOOMBERG_PLUGGIN="bloomberg_pluggin";
private long id;
private String fieldlabel;
private String fieldtype;
private String fieldoptions;
private String tablename;
private String dropdowninitial;
/**
* Get dropdown initial
* @return dropdown initial
*/
public String getDropdowninitial() {
return dropdowninitial;
}
/**
* Set dropdown initial
* @param dropdowninitial dropdown initial
*/
public void setDropdowninitial(String dropdowninitial) {
this.dropdowninitial = dropdowninitial;
}
/**
* Get id
* @return id
*/
public long getId() {
return id;
}
/**
* Set id
* @param id id
*/
public void setId(long id) {
this.id = id;
}
/**
* Get field label
* @return field label
*/
public String getFieldlabel() {
return fieldlabel;
}
/**
* Set field label
* @param fieldlabel field label
*/
public void setFieldlabel(String fieldlabel) {
this.fieldlabel = fieldlabel;
}
/**
* Get field type
* @return field type
*/
public String getFieldtype() {
return fieldtype;
}
/**
* Set field type
* @param fieldtype field type
*/
public void setFieldtype(String fieldtype) {
this.fieldtype = fieldtype;
}
/**
* Get field options
* @return field options
*/
public String getFieldoptions() {
return fieldoptions;
}
/**
* Set field options
* @param fieldoptions field options
*/
public void setFieldoptions(String fieldoptions) {
this.fieldoptions = fieldoptions;
}
/**
* Get table name
* @return table name
*/
public String getTablename() {
return tablename;
}
/**
* Set table name
* @param tablename table name
*/
public void setTablename(String tablename) {
this.tablename = tablename;
}
}
@@ -0,0 +1,87 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.db.vo;
import java.util.Date;
/**
* Peer package POJO (Bean)
*/
public class PeerPackage {
private String peername;
private String packagename;
private String version;
private Date lastchecked;
/**
* Get peer name
* @return peer name
*/
public String getPeername() {
return peername;
}
/**
* Set peer name
* @param peername peer name
*/
public void setPeername(String peername) {
this.peername = peername;
}
/**
* Get package name
* @return package name
*/
public String getPackagename() {
return packagename;
}
/**
* Set package name
* @param packagename package name
*/
public void setPackagename(String packagename) {
this.packagename = packagename;
}
/**
* Get version
* @return version
*/
public String getVersion() {
return version;
}
/**
* Set version
* @param version version
*/
public void setVersion(String version) {
this.version = version;
}
/**
* Get last checked
* @return last checked
*/
public Date getLastchecked() {
return lastchecked;
}
/**
* Set last checked
* @param lastchecked last checked
*/
public void setLastchecked(Date lastchecked) {
this.lastchecked = lastchecked;
}
}
@@ -0,0 +1,136 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.db.vo;
/**
* Trigger POJO (Bean)
*/
public class SchedulerTrigger {
private String exp_second;
private String exp_minute;
private String exp_hour;
private String exp_week;
private String exp_day;
private String exp_month;
private String inject_code;
/**
* Get inject code
* @return inject code
*/
public String getInject_code() {
return inject_code;
}
/**
* Set inject code
* @param inject_code inject code
*/
public void setInject_code(String inject_code) {
this.inject_code = inject_code;
}
/**
* Get exp second
* @return exp second
*/
public String getExp_second() {
return exp_second;
}
/**
* Set exp second
* @param exp_second exp second
*/
public void setExp_second(String exp_second) {
this.exp_second = exp_second;
}
/**
* Get exp minute
* @return exp minute
*/
public String getExp_minute() {
return exp_minute;
}
/**
* Set exp minute
* @param exp_minute exp minute
*/
public void setExp_minute(String exp_minute) {
this.exp_minute = exp_minute;
}
/**
* Get exp hour
* @return exp hour
*/
public String getExp_hour() {
return exp_hour;
}
/**
* Set exp hour
* @param exp_hour exp hour
*/
public void setExp_hour(String exp_hour) {
this.exp_hour = exp_hour;
}
/**
* Get exp week
* @return exp week
*/
public String getExp_week() {
return exp_week;
}
/**
* Set exp week
* @param exp_week exp week
*/
public void setExp_week(String exp_week) {
this.exp_week = exp_week;
}
/**
* Get exp day
* @return exp day
*/
public String getExp_day() {
return exp_day;
}
/**
* Set exp day
* @param exp_day exp day
*/
public void setExp_day(String exp_day) {
this.exp_day = exp_day;
}
/**
* Get exp month
* @return exp month
*/
public String getExp_month() {
return exp_month;
}
/**
* Set exp month
* @param exp_month exp month
*/
public void setExp_month(String exp_month) {
this.exp_month = exp_month;
}
}
@@ -0,0 +1,34 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.db.vo;
/**
* Strategy extended from Contract POJO
*/
public class Strategy extends Contract {
private String svalue=null;
/**
* Get svalue
* @return svalue
*/
public String getSvalue() {
return svalue;
}
/**
* Set svalue
* @param svalue svalue
*/
public void setSvalue(String svalue) {
this.svalue = svalue;
}
}
@@ -0,0 +1,75 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.db.vo;
import java.io.Serializable;
/**
* Value object used for various purposes to handle key value peer operations
*/
public class ValueObject implements Serializable {
private static final long serialVersionUID = 1L;
private String key;
private String value;
/**
* Get key
* @return key
*/
public String getKey() {
return this.key;
}
/**
* Set key
* @param key key
*/
public void setKey(String key) {
this.key = key;
}
/**
* Get value
* @return value
*/
public String getValue() {
return value;
}
/**
* Set value
* @param value value
*/
public void setValue(String value) {
this.value = value;
}
/**
* Overriding equals method to get uniqueness based on the key
*/
public boolean equals(Object old){
ValueObject oldvo=(ValueObject)old;
if(this.key!=null && oldvo.key!=null && this.key.trim().equals(oldvo.key.trim())){
return true;
}else{
return false;
}
}
/**
* Overriding toString() method
* @return string
*/
public String toString(){
return this.key;
}
}
+2
View File
@@ -0,0 +1,2 @@
db_close_timeout=20
db_config_path=/mnt/public/Libs/.4E.config
+13
View File
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="%4p %d{HH:mm:ss,SSS} %C - %m%n" />
</Console>
</Appenders>
<Loggers>
<Root level="ERROR">
<AppenderRef ref="CONSOLE"/>
</Root>
</Loggers>
</Configuration>
@@ -0,0 +1,309 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.db;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Vector;
import javax.swing.JOptionPane;
import junit.framework.TestCase;
/**
* lib-db unit test
*/
public class LibDbTest extends TestCase {
/**
* Test BBSyncDB.listAll()
*/
public void testBBSyncDBListAll() throws Exception
{
// Remove the comment tag to test. The code are commented out to prevent accessing db when installing this lib (edit properties file first) :
/*
String properties_path = Config.getString("db_config_path");
File file =new File(properties_path);
if ( !file.exists() ) {
JOptionPane.showMessageDialog(null, "Please set the config_db.properties file first");
assertTrue(false);
//System.exit(0);
}
else{
boolean isPass = false;
BBSyncDB db = BBSyncDB.getBBSyncDB();
try {
db.connectDB();
Vector v = db.listAll();
if (v != null) isPass = true; // check null / not. can contains no data.
}
catch (Exception e) {
e.printStackTrace();
}
finally {
db.closeDB();
}
JOptionPane.showMessageDialog(null, "Successfully connect to BBSyncDB");
assertTrue( isPass );
}
*/
assertTrue(true);
}
/**
* Test ConstructQueryDB.constructDateInputQuery()
*/
public void testConstructQueryDBConstructDateInputQuery() throws Exception
{
// Remove the comment tag to test. The code are commented out to prevent accessing db when installing this lib (edit properties file first) :
/*
boolean isPass = false;
ConstructQueryDB db = ConstructQueryDB.getConstructQueryDB();
try {
Map<String, String> m = new HashMap<String, String>();
m.put("filtertype", "number");
m.put("filtervalue", "ndays");
m.put("number", "1");
m.put("cdatefield", "date");
String result = db.constructDateInputQuery(m);
if (result != null && !"".equals(result)) isPass = true;
JOptionPane.showMessageDialog(null,"Construct Query MariaDB result: "+ result);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
db.closeDB();
}
assertTrue( isPass );
*/
assertTrue(true);
}
/**
* Test FlexiFieldDB.getFlexiFields()
*/
public void testFlexiFieldDBGetFlexiFields() throws Exception
{
// Remove the comment tag to test. The code are commented out to prevent accessing db when installing this lib (edit properties file first) :
/*
boolean isPass = false;
FlexiFieldDB db = FlexiFieldDB.getFlexiFieldDB();
try {
db.connectDB();
List l = db.getFlexiFields("tablename");
if (l != null) isPass = true; // check null / not. can contains no data.
JOptionPane.showMessageDialog(null,"FlexiFields contain data");
}
catch (Exception e) {
e.printStackTrace();
}
finally {
db.closeDB();
}
assertTrue( isPass );
*/
assertTrue(true);
}
/**
* Test GeneralUtilDB.resultsetToMap()
*/
public void testGeneralUtilDBResultsetToMap() throws Exception
{
// Remove the comment tag to test. The code are commented out to prevent accessing db when installing this lib (edit properties file first) :
/*
boolean isPass = false;
GeneralUtilDB g = new GeneralUtilDB();
try {
Map m = g.resultsetToMap(null);
}
catch (Exception e) {
isPass = true; // produce error, input resultset can not be null.
}
finally {
}
assertTrue( isPass );
*/
assertTrue(true);
}
/**
* Test IExecDB.listGroups()
*/
public void testIExecDBListGroups() throws Exception
{
// Remove the comment tag to test. The code are commented out to prevent accessing db when installing this lib (edit properties file first) :
/*
boolean isPass = false;
IExecDB db = IExecDB.getIExcecDB();
try {
db.connectDB();
List l = db.listGroups();
if (l != null) isPass = true; // check null / not. can contains no data.
JOptionPane.showMessageDialog(null, "iExec ie_group is not null");
}
catch (Exception e) {
e.printStackTrace();
}
finally {
db.closeDB();
}
assertTrue( isPass );
*/
assertTrue(true);
}
/**
* Test InfrastructureDB.getThemes4Users()
*/
public void testInfrastructureDBGetThemes4Users() throws Exception
{
// Remove the comment tag to test. The code are commented out to prevent accessing db when installing this lib (edit properties file first) :
/*
boolean isPass = false;
InfrastructureDB db = InfrastructureDB.getInfrastructureDB();
try {
db.connectDB();
Map m = (Map) db.getThemes4Users("Intan");
if ("B".equals(m.get("it"))) isPass = true;
}
catch (Exception e) {
e.printStackTrace();
}
finally {
db.closeDB();
}
JOptionPane.showMessageDialog(null, "InfrastructureDBGetThemes4Users success");
assertTrue( isPass );
*/
assertTrue(true);
}
/**
* Test ReferenceDB.getFieldMapping()
*/
public void testReferenceDBGetFieldMapping() throws Exception
{
// Remove the comment tag to test. The code are commented out to prevent accessing db when installing this lib (edit properties file first) :
/*
boolean isPass = false;
ReferenceDB db = ReferenceDB.getReferenceDB();
try {
db.connectDB();
Vector v = db.getFieldMapping();
if (v != null) isPass = true; // check null / not. can contains no data.
JOptionPane.showMessageDialog(null, "static_field_mapping contain data");
}
catch (Exception e) {
e.printStackTrace();
}
finally {
db.closeDB();
}
assertTrue( isPass );
*/
assertTrue(true);
}
/**
* Test RFunctionDB.listOfFolders()
*/
public void testRFunctionDBListOfFolders() throws Exception
{
// Remove the comment tag to test. The code are commented out to prevent accessing db when installing this lib (edit properties file first) :
/*
boolean isPass = false;
RFunctionDB db = RFunctionDB.getRFunctionDB();
try {
db.connectDB();
List<Map> folders = db.listOfFolders();
// for(Map fold: folders){
// System.out.println("folder name : " + fold.get("folder_name"));
// }
if (folders.size() > 0)
for(Map fold: folders){
//System.out.println("folder name : " + fold.get("folder_name"));
JOptionPane.showMessageDialog(null, "RFunction folder name : " + fold.get("folder_name"));
}
isPass = true;
}
catch (Exception e) {
e.printStackTrace();
}
finally {
db.closeDB();
}
assertTrue( isPass );
*/
assertTrue(true);
}
/**
* Test SchedulerDB.getGroupOrder()
*/
public void testSchedulerDBGetGroupOrder() throws Exception
{
// Remove the comment tag to test. The code are commented out to prevent accessing db when installing this lib (edit properties file first) :
/*
boolean isPass = false;
SchedulerDB db = SchedulerDB.getSchedulerDB();
try {
db.connectDB();
Vector v = db.getGroupOrder();
if (v.size() > 0) isPass = true;
JOptionPane.showMessageDialog(null, "scheduler_group is not null");
}
catch (Exception e) {
e.printStackTrace();
}
finally {
db.closeDB();
}
assertTrue( isPass );
*/
assertTrue(true);
}
/**
* Test DBManager.GetDatabase()
*/
public void testDBManagerGetDatabase() throws Exception
{
// Remove the comment tag to test. The code are commented out to prevent accessing db when installing this lib (edit properties file first) :
/*
boolean isPass = false;
DBManager dbm = new DBManager("infrastructure");
dbm.connect();
String tableName = "tblTeamOrganization";
ArrayList<String> selectedFields = new ArrayList<String>();
selectedFields.add("Intan");
ResultSet rs = dbm.getDatabase(tableName, selectedFields, null, null);
ArrayList<String> al = new ArrayList<String>();
while (rs.next()) {
al.add(rs.getString(1));
}
if (al.size() > 0) isPass = true;
JOptionPane.showMessageDialog(null, "DBManager test");
dbm.closeConnection();
assertTrue(isPass);
*/
assertTrue(true);
}
}
+214
View File
@@ -0,0 +1,214 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.db;
import java.util.Date;
import junit.framework.TestCase;
import com.fourelementscapital.db.vo.BBSyncTrigger;
import com.fourelementscapital.db.vo.CommodityInfo;
import com.fourelementscapital.db.vo.Contract;
import com.fourelementscapital.db.vo.FlexiField;
import com.fourelementscapital.db.vo.PeerPackage;
import com.fourelementscapital.db.vo.SchedulerTrigger;
import com.fourelementscapital.db.vo.Strategy;
import com.fourelementscapital.db.vo.ValueObject;
public class VOTest extends TestCase {
/**
* Test BBSyncTrigger Setter Getter
*/
public void testBBSyncTriggerSetterGetter()
{
boolean isEquals = true;
BBSyncTrigger o = new BBSyncTrigger();
o.setDailyhour(1);
o.setDay(2);
o.setDays("Mon");
o.setSynctype(3);
o.setTime("time");
o.setWeek(4);
if (1 != o.getDailyhour()) isEquals = false;
if (2 != o.getDay()) isEquals = false;
if (!"Mon".equals(o.getDays())) isEquals = false;
if (3 != o.getSynctype()) isEquals = false;
if (!"time".equals(o.getTime())) isEquals = false;
if (4 != o.getWeek()) isEquals = false;
assertTrue( isEquals );
}
/**
* Test CommodityInfo Setter Getter
*/
public void testCommodityInfoSetterGetter()
{
boolean isEquals = true;
CommodityInfo o = new CommodityInfo();
o.setBbticker("bbticker");
o.setCommodity("commodity");
o.setFepricesample("fepricesample");
o.setFetradeunit("fetradeunit");
o.setGlsample("glsample");
o.setGlticker("glticker");
o.setId(1L);
o.setMultiplefactor("multiplefactor");
o.setMultiplefactorbn("multiplefactorbn");
o.setName("name");
o.setNename("nename");
o.setNepricesample("nepricesample");
o.setNetradeunit("netradeunit");
o.setSector("sector");
o.setUnits("units");
if (!"bbticker".equals(o.getBbticker())) isEquals = false;
if (!"commodity".equals(o.getCommodity())) isEquals = false;
if (!"fepricesample".equals(o.getFepricesample())) isEquals = false;
if (!"fetradeunit".equals(o.getFetradeunit())) isEquals = false;
if (!"glsample".equals(o.getGlsample())) isEquals = false;
if (!"glticker".equals(o.getGlticker())) isEquals = false;
if (1L != o.getId()) isEquals = false;
if (!"multiplefactor".equals(o.getMultiplefactor())) isEquals = false;
if (!"multiplefactorbn".equals(o.getMultiplefactorbn())) isEquals = false;
if (!"name".equals(o.getName())) isEquals = false;
if (!"nename".equals(o.getNename())) isEquals = false;
if (!"nepricesample".equals(o.getNepricesample())) isEquals = false;
if (!"netradeunit".equals(o.getNetradeunit())) isEquals = false;
if (!"sector".equals(o.getSector())) isEquals = false;
if (!"units".equals(o.getUnits())) isEquals = false;
assertTrue( isEquals );
}
/**
* Test Contract Setter Getter
*/
public void testContractSetterGetter()
{
boolean isEquals = true;
Date date = new Date();
Contract o = new Contract();
o.setCdate(date);
o.setName("name");
o.setValue(1.0d);
if (date != o.getCdate()) isEquals = false;
if (!"name".equals(o.getName())) isEquals = false;
if (1.0d != o.getValue()) isEquals = false;
assertTrue( isEquals );
}
/**
* Test FlexiField Setter Getter
*/
public void testFlexiFieldSetterGetter()
{
boolean isEquals = true;
FlexiField o = new FlexiField();
o.setDropdowninitial("dropdowninitial");
o.setFieldlabel("fieldlabel");
o.setFieldoptions("fieldoptions");
o.setFieldtype("fieldtype");
o.setId(1L);
o.setTablename("tablename");
if (!"dropdowninitial".equals(o.getDropdowninitial())) isEquals = false;
if (!"fieldlabel".equals(o.getFieldlabel())) isEquals = false;
if (!"fieldoptions".equals(o.getFieldoptions())) isEquals = false;
if (!"fieldtype".equals(o.getFieldtype())) isEquals = false;
if (1.0d != o.getId()) isEquals = false;
if (!"tablename".equals(o.getTablename())) isEquals = false;
assertTrue( isEquals );
}
/**
* Test PeerPackage Setter Getter
*/
public void testPeerPackageSetterGetter()
{
boolean isEquals = true;
Date date = new Date();
PeerPackage o = new PeerPackage();
o.setLastchecked(date);
o.setPackagename("packagename");
o.setPeername("peername");
o.setVersion("version");
if (date != o.getLastchecked()) isEquals = false;
if (!"packagename".equals(o.getPackagename())) isEquals = false;
if (!"peername".equals(o.getPeername())) isEquals = false;
if (!"version".equals(o.getVersion())) isEquals = false;
assertTrue( isEquals );
}
/**
* Test SchedulerTrigger Setter Getter
*/
public void testSchedulerTriggerSetterGetter()
{
boolean isEquals = true;
SchedulerTrigger o = new SchedulerTrigger();
o.setExp_day("exp_day");
o.setExp_hour("exp_hour");
o.setExp_minute("exp_minute");
o.setExp_month("exp_month");
o.setExp_second("exp_second");
o.setExp_week("exp_week");
o.setInject_code("inject_code");
if (!"exp_day".equals(o.getExp_day())) isEquals = false;
if (!"exp_hour".equals(o.getExp_hour())) isEquals = false;
if (!"exp_minute".equals(o.getExp_minute())) isEquals = false;
if (!"exp_month".equals(o.getExp_month())) isEquals = false;
if (!"exp_second".equals(o.getExp_second())) isEquals = false;
if (!"exp_week".equals(o.getExp_week())) isEquals = false;
if (!"inject_code".equals(o.getInject_code())) isEquals = false;
assertTrue( isEquals );
}
/**
* Test Strategy Setter Getter
*/
public void testStrategySetterGetter()
{
boolean isEquals = true;
Strategy o = new Strategy();
o.setSvalue("svalue");
if (!"svalue".equals(o.getSvalue())) isEquals = false;
assertTrue( isEquals );
}
/**
* Test ValueObject Setter Getter
*/
public void testValueObjectSetterGetter()
{
boolean isEquals = true;
ValueObject o = new ValueObject();
o.setKey("key");
o.setValue("value");
if (!"key".equals(o.getKey())) isEquals = false;
if (!"value".equals(o.getValue())) isEquals = false;
assertTrue( isEquals );
}
}