diff --git a/.gitignore b/.gitignore index e6e96a8..0a675dd 100644 --- a/.gitignore +++ b/.gitignore @@ -53,3 +53,7 @@ dist/ nbdist/ .nb-gradle/ +.settings +.classpath +.project +bin/ diff --git a/Examples/iexec/pom.xml b/Examples/iexec/pom.xml new file mode 100755 index 0000000..4e86e71 --- /dev/null +++ b/Examples/iexec/pom.xml @@ -0,0 +1,45 @@ + + 4.0.0 + com.fourelementscapital + iexec + 3.0.0-Beta1 + iexec + http://www.fourelementscapital.com + + + + + org.apache.commons + commons-jcs-core + 2.2 + + + + + commons-logging + commons-logging + 1.2 + + + + + com.fourelementscapital + lib-db + 2.0 + + + + + org.apache.logging.log4j + log4j-core + 2.8.1 + + + + + + + UTF-8 + + \ No newline at end of file diff --git a/Examples/iexec/src/main/java/com/fourelementscapital/iexec/common/IExecMgmt.java b/Examples/iexec/src/main/java/com/fourelementscapital/iexec/common/IExecMgmt.java new file mode 100755 index 0000000..e0a4b24 --- /dev/null +++ b/Examples/iexec/src/main/java/com/fourelementscapital/iexec/common/IExecMgmt.java @@ -0,0 +1,74 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.iexec.common; + + +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Vector; +import javax.servlet.http.HttpServletRequest; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import com.fourelementscapital.db.IExecDB; +import com.fourelementscapital.db.vo.ValueObject; + + +/** + * Use lib-db to generate tree menu on iExec + */ + +public class IExecMgmt{ + + private HttpServletRequest request=null; + private Logger log = LogManager.getLogger(IExecMgmt.class.getName()); + + public IExecMgmt() throws Exception { + super(); + + } + + public Map getTreeViewData() throws Exception { + + IExecDB iedb=IExecDB.getIExcecDB(); + + try{ + HashMap rtn=new HashMap(); + + iedb.connectDB(); + + List glist=iedb.listGroups(); + List folders=iedb.listFolders(); + List strategies=iedb.listStrategies(); + + Vector groups=new Vector(); + HashMap colors=new HashMap(); + for(Iterator it=glist.iterator();it.hasNext();){ + Map data=(Map)it.next(); + + ValueObject vo=new ValueObject(); + vo.setKey((String)data.get("group_uid")); + vo.setValue((String)data.get("group_name")); + groups.add(vo); + colors.put(data.get("group_uid"), data.get("color_code")); + } + rtn.put("folders", folders); + rtn.put("groups",groups); + rtn.put("group_colors",colors); + rtn.put("strategies",strategies); + + return rtn; + + }catch(Exception e){ + throw e; + }finally{ + iedb.closeDB(); + } + } +} \ No newline at end of file diff --git a/Examples/qlib/pom.xml b/Examples/qlib/pom.xml new file mode 100755 index 0000000..037c416 --- /dev/null +++ b/Examples/qlib/pom.xml @@ -0,0 +1,78 @@ + + 4.0.0 + com.fourelementscapital + qLib + ${revision}${changelist} + war + qLib + http://www.fourelementscapital.com + + + 3.0.0 + -Beta1 + UTF-8 + + + + + + + + com.fourelementscapital + lib-config + 2.0 + + + com.fourelementscapital + lib-auth + 2.0 + + + com.fourelementscapital + lib-db + 2.0 + + + + + + org.slf4j + slf4j-simple + 1.7.25 + + + org.apache.logging.log4j + log4j-core + 2.8.1 + + + + org.apache.commons + commons-compress + 1.15 + + + + + org.apache.commons + commons-jcs-core + 2.2 + + + + commons-logging + commons-logging + 1.2 + + + + + com.fourelementscapital + lib-client + 2.0 + + + + + \ No newline at end of file diff --git a/Examples/qlib/src/main/java/com/fourelementscapital/rfunction/common/AbstractTeamOrgMgmt.java b/Examples/qlib/src/main/java/com/fourelementscapital/rfunction/common/AbstractTeamOrgMgmt.java new file mode 100755 index 0000000..ef7e044 --- /dev/null +++ b/Examples/qlib/src/main/java/com/fourelementscapital/rfunction/common/AbstractTeamOrgMgmt.java @@ -0,0 +1,305 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.rfunction.common; + +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 javax.servlet.http.HttpServletRequest; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import com.fourelementscapital.config.Constant; +import com.fourelementscapital.auth.UserThemeAccessPermission; +import com.fourelementscapital.db.AbstractTeamOrgDB; +import com.fourelementscapital.db.SchedulerDB; +import com.fourelementscapital.client.Authenticated; + + + +/** + * This class is abstract and provide basic implementation for all Team Organization related moudles + * for example: Themes tag support on scheduler, R function * + * @author Administrator + * + */ +@SuppressWarnings("unchecked") +public abstract class AbstractTeamOrgMgmt extends Authenticated{ + + protected static String ACCESS_PRIVILEGE_RWX="RWX"; + protected static String ACCESS_PRIVILEGE_RX="RX"; + protected static String ACCESS_PRIVILEGE_R="R"; + private Logger log = LogManager.getLogger(AbstractTeamOrgMgmt.class.getName()); + + + /** + * this constructor to be used only for DWR, manual invoking to get the object using of this constructor will mis-behave + * @throws Exception + */ + public AbstractTeamOrgMgmt() throws Exception { + super(); + + } + + + /** + * this constructor will be used only when JSP or Httprequest request is available. + * @param request + * @throws Exception + */ + public AbstractTeamOrgMgmt(HttpServletRequest request) throws Exception { + super(request); + } + + + /** + * to be used in child class + * @param sdb + * @return + * @throws Exception + */ + protected Map getThemeAccessData(AbstractTeamOrgDB sdb) throws Exception { + + UserThemeAccessPermission auth=getAuthenticatedUserObj(sdb); + HashMap rtn=new HashMap(); + if(auth!=null){ + rtn.put("rwx_tags",auth.getRwx()); + rtn.put("rx_tags",auth.getRx()); + rtn.put("r_tags",auth.getR()); + } + + String superuser=(String)getRequest().getSession().getAttribute(Constant.SESSION_LOGGED_SUPERUSER); + if(superuser!=null && !superuser.equals("")){ + rtn.put("superuser",superuser); + } + return rtn; + } + + + /** + * + * @param scheduler_id + * @param sdb + * @return + * @throws Exception + */ + protected String getAccessPrivilege(int scheduler_id, AbstractTeamOrgDB sdb) throws Exception { + String rtn=""; + UserThemeAccessPermission user=getAuthenticatedUserObj(sdb); + if(user!=null){ + List themes=sdb.getThemeTags(scheduler_id); + + log.debug("themes:"+themes); + log.debug("user.getRwx():"+user.getRwx()); + log.debug("user.getRx():"+user.getRx()); + log.debug("user.getR():"+user.getR()); + + for(String ttag:themes){ + if(user.getRwx().contains(ttag)) rtn=ACCESS_PRIVILEGE_RWX; + if(user.getRx().contains(ttag) && (!rtn.equals(ACCESS_PRIVILEGE_RWX) )) rtn=ACCESS_PRIVILEGE_RX; + if(user.getR().contains(ttag) && rtn.equals("")) rtn=ACCESS_PRIVILEGE_R; + } + if(themes.size()==0) rtn=ACCESS_PRIVILEGE_RWX; + + + + + + } + log.debug("user:"+user); + log.debug("getAccessPrivilege:rtn:"+rtn); + String superuser=(String)getRequest().getSession().getAttribute(Constant.SESSION_LOGGED_SUPERUSER); + if(superuser!=null && !superuser.equals("")){ + rtn=ACCESS_PRIVILEGE_RWX; + } + if(rtn.equals("")) rtn=null; + return rtn; + } + + + + /** + * @deprecated + * @param notity_tags + * @param sdb + * @return + * @throws Exception + */ + protected ArrayList getNotifications(List notity_tags, AbstractTeamOrgDB sdb) throws Exception { + + Map priv_data=getThemeHirarchy(notity_tags,sdb); + ArrayList user_notification=new ArrayList(); + + for(Iterator i=priv_data.keySet().iterator();i.hasNext();){ + String ky=i.next(); + if(priv_data.get(ky)!=null && priv_data.get(ky).equalsIgnoreCase("rwx")){ + user_notification.add(ky); + } + } + return user_notification; + } + + + + + + + /** + * + * @param sdb + * @return + * @throws Exception + */ + protected boolean isAuthorizedUser(SchedulerDB sdb) throws Exception { + + String user=getAuthenticatedUser(); + if(user!=null){ + Map u=sdb.getSVNUser4WikiUser(user); + if(u!=null && u.get("svn_username")!=null && u.get("svn_password")!=null + && !((String)u.get("svn_username")).equals("") && !((String)u.get("svn_password")).equals("") + ){ + return true; + }else{ + return false; + } + }else{ + return false; + } + } + + /** + * + * @param sdb + * @return + * @throws Exception + */ + protected String getAuthorizedUser(SchedulerDB sdb) throws Exception { + + String user=getAuthenticatedUser(); + log.debug("getAuthorizedUser() user:"+user); + if(user!=null){ + Map u=sdb.getSVNUser4WikiUser(user); + if(u!=null && u.get("svn_username")!=null && u.get("svn_password")!=null + && !((String)u.get("svn_username")).equals("") && !((String)u.get("svn_password")).equals("") + ){ + return (String)u.get("svn_username"); + }else{ + return null; + } + }else{ + return null; + } + } + + + /** + * + * @param scheduler_id + * @param sdb + * @return + * @throws Exception + */ + + protected Map getItemTags2(int scheduler_id,AbstractTeamOrgDB sdb) throws Exception { + + //SchedulerDB sdb=SchedulerDB.getSchedulerDB(); + + //try{ + HashMap h=new HashMap(); + Vector tagids=sdb.getTagIds4Item(scheduler_id); + Vector tags=sdb.getTags(); + Vector follow_tagids=sdb.getFollowTagIds4Item(scheduler_id); + + ArrayList themes=sdb.getThemeNames4Item(scheduler_id); + //Vector followers=sdb.getFollowers(scheduler_id); + + + Map priv_data=getThemeHirarchy(themes,sdb); + if(priv_data.get("theme")!=null){ + priv_data.remove("theme"); + } + + List ftags=sdb.getFollowTags4Item(scheduler_id); + List user_notifications=ftags.size()>0?getNotifications(ftags,sdb):new ArrayList(); + ArrayList all=new ArrayList(ftags); + all.addAll(themes); + + h.put("tagids", tagids); + h.put("user_privileges",priv_data); + h.put("user_notifications",user_notifications); + h.put("follow_tagids",follow_tagids); + h.put("notice_escalated", getThemeHirarchy(all,sdb)); + h.put("tags", tags); + h.putAll(priv_data); + return h; + } + + + /** + * + * @param item_id + * @param newtask_tags + * @param follow_tags + * @param atodb + * @param sdb + * @param name + * @param comment + * @param rev + * @param diff + * @param tempdata + * @param templ_file + * @throws Exception + */ + protected void updateAllItemTags(int item_id, List newtask_tags,List follow_tags,AbstractTeamOrgDB atodb,SchedulerDB sdb,String name, String comment,long rev,String diff, Map tempdata, String templ_file) throws Exception { + + ArrayList ids=new ArrayList(); + for(Iterator i=newtask_tags.iterator();i.hasNext();){ + String tid=(String)i.next(); + ids.add(Integer.parseInt(tid)); + } + atodb.updateItemTagIds(item_id, ids); + String owner_tag=null; + if(ids.size()>0){ + owner_tag=atodb.getThemeTagName(ids.get(0)); + } + ArrayList fids=new ArrayList(); + for(Iterator i=follow_tags.iterator();i.hasNext();){ + String tid=(String)i.next(); + fids.add(Integer.parseInt(tid)); + } + atodb.updateFollwerTagIds(item_id, fids); + + + } + + + + + + protected Map getItemPrivilegeNotifications(ArrayList themes,ArrayList ftags,AbstractTeamOrgDB sdb) throws Exception { + + + Map priv_data=getThemeHirarchy(themes,sdb); + HashMap h=new HashMap(); + + ArrayList all=new ArrayList(ftags); + all.addAll(themes); + List user_notifications=getNotifications(all,sdb); + h.put("user_notifications",user_notifications); + h.put("user_privileges",priv_data); + h.put("notice_escalated",getThemeHirarchy(all,sdb)); + + return h; + + } +} \ No newline at end of file diff --git a/Examples/qlib/src/main/java/com/fourelementscapital/rfunction/common/RFunctionMgmt.java b/Examples/qlib/src/main/java/com/fourelementscapital/rfunction/common/RFunctionMgmt.java new file mode 100755 index 0000000..d72a247 --- /dev/null +++ b/Examples/qlib/src/main/java/com/fourelementscapital/rfunction/common/RFunctionMgmt.java @@ -0,0 +1,164 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.rfunction.common; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Vector; +import javax.servlet.http.HttpServletRequest; +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.RFunctionDB; +import com.fourelementscapital.db.SchedulerDB; +import com.fourelementscapital.db.vo.ValueObject; + + +/** + * This class exposes data to R function editor in Ajax call + * Remember all methods in the class are required user to be logged in + */ +@SuppressWarnings("unchecked") +public class RFunctionMgmt extends AbstractTeamOrgMgmt { + + private HttpServletRequest request=null; + private Logger log = LogManager.getLogger(RFunctionMgmt.class.getName()); + private static CacheAccess lockcache=null; + private String cache_packaged_key="packaged_functions"; + public static String RFUNCTION_FILE_EXTENSION=".r"; + + private static String FUNCTION_ID="function_id"; + private static String LOCK_DURATION="duration"; + private static String USER="user"; + + /** + * DWR invocation + * @throws Exception + */ + public RFunctionMgmt() throws Exception { + super(); + } + + /** + * JSP or internal invocation + * @param request + * @throws Exception + */ + public RFunctionMgmt(HttpServletRequest request) throws Exception { + super(request); + + } + + /** + * JCS caching for speed data retrival and not necessary to hit the database for every user. + * @return + * @throws Exception + */ + private static CacheAccess getLockCache() throws Exception { + if(RFunctionMgmt.lockcache==null) RFunctionMgmt.lockcache=JCS.getInstance("lock-cache"); + return RFunctionMgmt.lockcache; + } + + /** + * Locking r function while a user is editing + * @param function_id + * @param seconds + * @return + * @throws Exception + */ + public boolean lockFunction(int function_id, long seconds) throws Exception { + SchedulerDB sdb=SchedulerDB.getSchedulerDB(); + RFunctionDB rfdb=RFunctionDB .getRFunctionDB(); + try{ + sdb.connectDB(); + rfdb.connectDB(); + String usr=new SchedulerMgmt(getRequest()).getAuthorizedUser(sdb); + if(seconds>0){ + refreshCache(function_id, seconds,usr); + }else{ + rfdb.updateLock(function_id, usr); + } + return true; + }catch(Exception e){ + + throw e; + }finally{ + sdb.closeDB(); + rfdb.closeDB(); + } + + } + + + private void removeLockFromCache(int function_id, String usr ) throws Exception { + //String ky=usr+"_"+function_id; + String ky=usr+function_id; + if(getLockCache().get(ky)!=null)getLockCache().remove(ky); + } + + private void refreshCache(int function_id, long seconds, String usr ) throws Exception { + + //String ky=usr+"_"+function_id; + String ky=usr+function_id; + HashMap h=new HashMap(); + h.put(RFunctionMgmt.FUNCTION_ID, function_id); + h.put(RFunctionMgmt.LOCK_DURATION, seconds); + h.put(RFunctionMgmt.USER, usr); + + IElementAttributes att= getLockCache().getDefaultElementAttributes(); + att.setMaxLife(seconds); + if(getLockCache().get(ky)!=null)getLockCache().remove(ky); + getLockCache().put(ky,h,att); + } + + + /** + * return data for folder generation. + * @return + * @throws Exception + */ + public Map getFolderTree() throws Exception { + + RFunctionDB rfdb=RFunctionDB.getRFunctionDB(); + + try{ + HashMap rtn=new HashMap(); + + rfdb.connectDB(); + List glist=rfdb.listFunctionGroups(); + List folders=rfdb.listOfFolders(); + + //TreeMap groups=new TreeMap(); + Vector groups=new Vector(); + + for(Iterator it=glist.iterator();it.hasNext();){ + Map data=(Map)it.next(); + //groups.put(data.get("group_uid"),data.get("group_name")); + + ValueObject vo=new ValueObject(); + vo.setKey((String)data.get("group_uid")); + vo.setValue((String)data.get("group_name")); + groups.add(vo); + } + rtn.put("folders", folders); + rtn.put("groups",groups); + + return rtn; + + + }catch(Exception e){ + throw e; + }finally{ + + } + } +} \ No newline at end of file diff --git a/Examples/qlib/src/main/java/com/fourelementscapital/rfunction/common/SchedulerMgmt.java b/Examples/qlib/src/main/java/com/fourelementscapital/rfunction/common/SchedulerMgmt.java new file mode 100755 index 0000000..e09415c --- /dev/null +++ b/Examples/qlib/src/main/java/com/fourelementscapital/rfunction/common/SchedulerMgmt.java @@ -0,0 +1,46 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.rfunction.common; + +import javax.servlet.http.HttpServletRequest; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +@SuppressWarnings("unchecked") +public class SchedulerMgmt extends AbstractTeamOrgMgmt { + + private Logger log = LogManager.getLogger(SchedulerMgmt.class.getName()); + private HttpServletRequest request=null; + private static String USER="user"; + + /** + * for DWR invocation + * @throws Exception + */ + public SchedulerMgmt() throws Exception { + super(); + + } + + /** + * Invocation only when you have HttpRequest is available, + * in JSP or internally. + * @param request + * @throws Exception + */ + public SchedulerMgmt(HttpServletRequest request) throws Exception { + super(request); + + } + + protected String getPeerIPAddress() throws Exception { + String rtn=(String) getRequest().getSession().getAttribute(REMOTE_IP); + return rtn; + } +} \ No newline at end of file diff --git a/README.md b/README.md index f781ca9..a1a58b5 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ # scheduler4e-javalibs -This is the repository of javalibs required by the scheduler4e project. \ No newline at end of file +This is the repository of javalibs required by the scheduler4e project. +JavaLibs that is being used by scheduler4e. This is based on JavaLibs 2.0 plus some minor changes including build flags with target and source optimized for JDK 1.8 diff --git a/archetype-app/pom.xml b/archetype-app/pom.xml new file mode 100755 index 0000000..db64abb --- /dev/null +++ b/archetype-app/pom.xml @@ -0,0 +1,26 @@ + + 4.0.0 + + + com.fourelementscapital + lib + ${revision} + + + com.fourelementscapital + archetype-app + maven-archetype + archetype-app + A fourelementscapital archetype as the basis of building a four elements capital application + http://www.fourelementscapital.com + + + + org.apache.maven.archetype + archetype-packaging + 2.2 + + + + diff --git a/archetype-app/src/main/resources/META-INF/maven/archetype-metadata.xml b/archetype-app/src/main/resources/META-INF/maven/archetype-metadata.xml new file mode 100755 index 0000000..5850659 --- /dev/null +++ b/archetype-app/src/main/resources/META-INF/maven/archetype-metadata.xml @@ -0,0 +1,36 @@ + + + + src/main/java + + **/*.java + + + + src/main/resources + + **/*.* + + + + src/test/java + + **/*.java + + + + + + 1.0 + + + com.fourelementscapital + + + com.fourelementscapital.${artifactId} + + + diff --git a/archetype-app/src/main/resources/archetype-resources/pom.xml b/archetype-app/src/main/resources/archetype-resources/pom.xml new file mode 100755 index 0000000..3d0fd21 --- /dev/null +++ b/archetype-app/src/main/resources/archetype-resources/pom.xml @@ -0,0 +1,149 @@ + + 4.0.0 + ${groupId} + ${artifactId} + ${version} + jar + ${artifactId} + http://www.fourelementscapital.com + + + + + maven-shade-plugin + 2.3 + + + package + + shade + + + + + ${package}.App + + + false + + + *:* + + META-INF/*.SF + META-INF/*.DSA + META-INF/*.RSA + + + + + + + + + org.apache.maven.plugins + maven-site-plugin + 3.5.1 + + + attach-javadocs + package + + site + stage + + + + + + + + + + ${artifactId} + ${artifactId} + http://www.fourelementscapital.com + + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.10.4 + + + + + aggregate + true + + + + javadoc + test-javadoc + + + + + + + + + + junit + junit + 3.8.1 + test + + + org.apache.logging.log4j + log4j-core + 2.8.1 + + + + com.fourelementscapital + lib-alarm + 2.0 + + + com.fourelementscapital + lib-auth + 2.0 + + + com.fourelementscapital + lib-db + 2.0 + + + com.fourelementscapital + lib-fileutils + 2.0 + + + com.fourelementscapital + lib-r + 2.0 + + + com.fourelementscapital + lib-socket + 2.0 + + + com.fourelementscapital + lib-tts + 2.0 + + + com.fourelementscapital + lib-imonitor + 2.0 + + + + + \ No newline at end of file diff --git a/archetype-app/src/main/resources/archetype-resources/src/main/java/App.java b/archetype-app/src/main/resources/archetype-resources/src/main/java/App.java new file mode 100755 index 0000000..70de889 --- /dev/null +++ b/archetype-app/src/main/resources/archetype-resources/src/main/java/App.java @@ -0,0 +1,26 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package ${package}; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + + +/** + * A com.fourelementscapital Class + */ +public class App { + + private static final Logger log = LogManager.getLogger(App.class.getName()); + + public static void main( String[] args ) + { + log.debug( "Hello Four Elements Capital !" ); + } +} + diff --git a/archetype-app/src/main/resources/archetype-resources/src/main/resources/log4j2.xml b/archetype-app/src/main/resources/archetype-resources/src/main/resources/log4j2.xml new file mode 100755 index 0000000..304acc5 --- /dev/null +++ b/archetype-app/src/main/resources/archetype-resources/src/main/resources/log4j2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/archetype-app/src/main/resources/archetype-resources/src/test/java/AppTest.java b/archetype-app/src/main/resources/archetype-resources/src/test/java/AppTest.java new file mode 100755 index 0000000..0f1eee7 --- /dev/null +++ b/archetype-app/src/main/resources/archetype-resources/src/test/java/AppTest.java @@ -0,0 +1,47 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package ${package}; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for App + */ +public class AppTest extends TestCase { + + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Empty Test + */ + public void testApp() + { + assertTrue( true ); + } + +} + + diff --git a/archetype-lib/pom.xml b/archetype-lib/pom.xml new file mode 100755 index 0000000..ac0c240 --- /dev/null +++ b/archetype-lib/pom.xml @@ -0,0 +1,26 @@ + + 4.0.0 + + + com.fourelementscapital + lib + ${revision} + + + com.fourelementscapital + archetype-lib + maven-archetype + archetype-lib + A fourelementscapital archetype as the basis of creating a com.fourelementscapital library / jar file + http://www.fourelementscapital.com + + + + org.apache.maven.archetype + archetype-packaging + 2.2 + + + + diff --git a/archetype-lib/src/main/resources/META-INF/maven/archetype-metadata.xml b/archetype-lib/src/main/resources/META-INF/maven/archetype-metadata.xml new file mode 100755 index 0000000..fc217d6 --- /dev/null +++ b/archetype-lib/src/main/resources/META-INF/maven/archetype-metadata.xml @@ -0,0 +1,30 @@ + + + + src/main/java + + **/*.java + + + + src/test/java + + **/*.java + + + + + + 1.0 + + + com.fourelementscapital + + + com.fourelementscapital.${artifactId} + + + \ No newline at end of file diff --git a/archetype-lib/src/main/resources/archetype-resources/pom.xml b/archetype-lib/src/main/resources/archetype-resources/pom.xml new file mode 100755 index 0000000..d9fe5d5 --- /dev/null +++ b/archetype-lib/src/main/resources/archetype-resources/pom.xml @@ -0,0 +1,119 @@ + + 4.0.0 + ${groupId} + ${artifactId} + ${version} + jar + ${artifactId} + http://www.fourelementscapital.com + + + + + org.apache.maven.plugins + maven-site-plugin + 3.5.1 + + + attach-javadocs + package + + site + stage + + + + + + + + + + ${artifactId} + ${artifactId} + http://www.fourelementscapital.com + + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.10.4 + + + + + aggregate + true + + + + javadoc + test-javadoc + + + + + + + + + + junit + junit + 3.8.1 + test + + + org.apache.logging.log4j + log4j-core + 2.8.1 + + + + com.fourelementscapital + lib-alarm + 2.0 + + + com.fourelementscapital + lib-auth + 2.0 + + + com.fourelementscapital + lib-db + 2.0 + + + com.fourelementscapital + lib-fileutils + 2.0 + + + com.fourelementscapital + lib-r + 2.0 + + + com.fourelementscapital + lib-socket + 2.0 + + + com.fourelementscapital + lib-tts + 2.0 + + + com.fourelementscapital + lib-imonitor + 2.0 + + + + + \ No newline at end of file diff --git a/archetype-lib/src/main/resources/archetype-resources/src/main/java/App.java b/archetype-lib/src/main/resources/archetype-resources/src/main/java/App.java new file mode 100755 index 0000000..1823620 --- /dev/null +++ b/archetype-lib/src/main/resources/archetype-resources/src/main/java/App.java @@ -0,0 +1,22 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package ${package}; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +/** + * A com.fourelementscapital Class + */ +public class App { + + private static final Logger log = LogManager.getLogger(App.class.getName()); + +} + + \ No newline at end of file diff --git a/archetype-lib/src/main/resources/archetype-resources/src/test/java/AppTest.java b/archetype-lib/src/main/resources/archetype-resources/src/test/java/AppTest.java new file mode 100755 index 0000000..9c6830f --- /dev/null +++ b/archetype-lib/src/main/resources/archetype-resources/src/test/java/AppTest.java @@ -0,0 +1,46 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package ${package}; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for App + */ +public class AppTest extends TestCase { + + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Empty Test + */ + public void testApp() + { + assertTrue( true ); + } + +} + diff --git a/archetype-webapp/pom.xml b/archetype-webapp/pom.xml new file mode 100755 index 0000000..3bce3a5 --- /dev/null +++ b/archetype-webapp/pom.xml @@ -0,0 +1,26 @@ + + 4.0.0 + + + com.fourelementscapital + lib + ${revision} + + + com.fourelementscapital + archetype-webapp + maven-archetype + archetype-webapp + A fourelementscapital archetype as the basis of building a four elements capital web application + http://www.fourelementscapital.com + + + + org.apache.maven.archetype + archetype-packaging + 2.2 + + + + diff --git a/archetype-webapp/src/main/resources/META-INF/maven/archetype-metadata.xml b/archetype-webapp/src/main/resources/META-INF/maven/archetype-metadata.xml new file mode 100755 index 0000000..6d14eb0 --- /dev/null +++ b/archetype-webapp/src/main/resources/META-INF/maven/archetype-metadata.xml @@ -0,0 +1,36 @@ + + + + src/main/java + + **/*.java + + + + src/main/webapp + + **/*.* + + + + src/main/resources + + **/*.* + + + + + + 1.0 + + + com.fourelementscapital + + + com.fourelementscapital.${artifactId} + + + diff --git a/archetype-webapp/src/main/resources/archetype-resources/pom.xml b/archetype-webapp/src/main/resources/archetype-resources/pom.xml new file mode 100755 index 0000000..a350232 --- /dev/null +++ b/archetype-webapp/src/main/resources/archetype-resources/pom.xml @@ -0,0 +1,125 @@ + + 4.0.0 + ${groupId} + ${artifactId} + ${version} + war + ${artifactId} + http://www.fourelementscapital.com + + + ${artifactId} + + + org.apache.maven.plugins + maven-site-plugin + 3.5.1 + + + attach-javadocs + package + + site + stage + + + + + + + + + + ${artifactId} + ${artifactId} + http://www.fourelementscapital.com + + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.10.4 + + + + + aggregate + true + + + + javadoc + test-javadoc + + + + + + + + + + junit + junit + 3.8.1 + test + + + javax.servlet + servlet-api + 2.5 + + + org.apache.logging.log4j + log4j-core + 2.8.1 + + + + com.fourelementscapital + lib-alarm + 2.0 + + + com.fourelementscapital + lib-auth + 2.0 + + + com.fourelementscapital + lib-db + 2.0 + + + com.fourelementscapital + lib-fileutils + 2.0 + + + com.fourelementscapital + lib-r + 2.0 + + + com.fourelementscapital + lib-socket + 2.0 + + + com.fourelementscapital + lib-tts + 2.0 + + + com.fourelementscapital + lib-imonitor + 2.0 + + + + + \ No newline at end of file diff --git a/archetype-webapp/src/main/resources/archetype-resources/src/main/java/servlet/HelloServlet.java b/archetype-webapp/src/main/resources/archetype-resources/src/main/java/servlet/HelloServlet.java new file mode 100755 index 0000000..18e11f8 --- /dev/null +++ b/archetype-webapp/src/main/resources/archetype-resources/src/main/java/servlet/HelloServlet.java @@ -0,0 +1,39 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package ${package}.servlet; + +import java.io.IOException; +import java.io.PrintWriter; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +public class HelloServlet extends HttpServlet { + + private Logger log = LogManager.getLogger(HelloServlet.class.getName()); + + protected void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + String str = "Hello four elements capital !"; + + log.debug(">>> String result : " + str); + + PrintWriter out = response.getWriter(); + out.println(str); + out.close(); + } + +} + + \ No newline at end of file diff --git a/archetype-webapp/src/main/resources/archetype-resources/src/main/resources/log4j2.xml b/archetype-webapp/src/main/resources/archetype-resources/src/main/resources/log4j2.xml new file mode 100755 index 0000000..304acc5 --- /dev/null +++ b/archetype-webapp/src/main/resources/archetype-resources/src/main/resources/log4j2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/archetype-webapp/src/main/resources/archetype-resources/src/main/webapp/WEB-INF/web.xml b/archetype-webapp/src/main/resources/archetype-resources/src/main/webapp/WEB-INF/web.xml new file mode 100755 index 0000000..28535d0 --- /dev/null +++ b/archetype-webapp/src/main/resources/archetype-resources/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,22 @@ + + + + Four Elements Capital Web Application + + This is a simple web application. + + + + hello + ${package}.servlet.HelloServlet + + + + hello + /hello + + + \ No newline at end of file diff --git a/archetype-webapp/src/main/resources/archetype-resources/src/main/webapp/index.jsp b/archetype-webapp/src/main/resources/archetype-resources/src/main/webapp/index.jsp new file mode 100755 index 0000000..0eb001e --- /dev/null +++ b/archetype-webapp/src/main/resources/archetype-resources/src/main/webapp/index.jsp @@ -0,0 +1 @@ + diff --git a/lib-alarm/pom.xml b/lib-alarm/pom.xml new file mode 100755 index 0000000..18d7974 --- /dev/null +++ b/lib-alarm/pom.xml @@ -0,0 +1,29 @@ + + 4.0.0 + + + com.fourelementscapital + lib + ${revision} + + + com.fourelementscapital + lib-alarm + jar + lib-alarm + Alarm functions, call iMonitor API + http://www.fourelementscapital.com + + + + commons-httpclient + commons-httpclient + + + org.apache.logging.log4j + log4j-api + + + + diff --git a/lib-alarm/src/main/java/com/fourelementscapital/alarm/Alarm.java b/lib-alarm/src/main/java/com/fourelementscapital/alarm/Alarm.java new file mode 100755 index 0000000..24169c6 --- /dev/null +++ b/lib-alarm/src/main/java/com/fourelementscapital/alarm/Alarm.java @@ -0,0 +1,300 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.alarm; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.net.Socket; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Properties; + +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.methods.PostMethod; +import org.apache.commons.httpclient.methods.StringRequestEntity; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +/** + * Send alarm to iMonitor API + */ +public class Alarm { + + private static final Logger log = LogManager.getLogger(Alarm.class.getName()); + + /** + * Private constructor + */ + private Alarm(){} + + + /** + * Construct alarm message with default parameters and send it to iMonitor API + * @param themes ThemeVO array list + * @param alarm_type Alarm type + * @param subject Subject + * @param message Message + * @param say Is Say + * @param email Is Email + * @param phone Is Phone + * @param attachmentfile Attachment file + * @param exc_add Exclude Address + * @throws IOException + */ + public static void sendAlarm(ArrayList themes, AlarmType alarm_type,String subject,String message, boolean say, boolean email, boolean phone, String attachmentfile,String exc_add) throws IOException { + + if(themes!=null && themes.size()>0 && message!=null && alarm_type!=null){ + + // ignore alarm ? : + String c_noalarm=getConfigValue("ignore.alarm"); + if(c_noalarm!=null && c_noalarm.equalsIgnoreCase("true")) { + //do nothing + }else{ + + //String xml=constructXMLWithFile(themes,subject,message,say,email,phone,attachmentfile,exc_add); + String socketMessage=constructSocketMessageWithFile(themes,subject,message,say,email,phone,attachmentfile,exc_add); + //log.debug("Sending alarm:xml:"+xml); + //log.debug("Sending alarm:socketMessage:"+socketMessage); + + // check whether dump XML to file or send to iMonitor API : + + boolean isDump = Boolean.parseBoolean(getConfigValue("alarm.dump")); + + if (isDump) { + // create folder if not exist. format : 'yyyyMMdd' (today's date) + String dumpFolder = getConfigValue("alarm.dump.folder"); + SimpleDateFormat sdfDumpFolder = new SimpleDateFormat("yyyyMMdd"); + dumpFolder = dumpFolder + sdfDumpFolder.format(new Date()) + "/"; // setup today folder + File dir = new File(dumpFolder); + dir.mkdirs(); + + // write dump file. format : 'HHmmss.xml' (HHmmss : current time) + SimpleDateFormat sdfFile = new SimpleDateFormat("HHmmssSSS"); + PrintWriter writer = new PrintWriter(dumpFolder + sdfFile.format(new Date()) + ".xml", "UTF-8"); + //writer.println(xml); + //writer.println(socketMessage); + writer.close(); + } + else { + //sendXML(xml); // send to iMonitor + sendSocketMessage(socketMessage); // send to iMonitor + } + } + } + } + + + /** + * Send phone alarm with list of theme and a message + * @param themes ThemeVO array list + * @param message Message + * @throws IOException + */ + public static void sendAlarm(ArrayList themes, String message) throws IOException { + Alarm.sendAlarm(themes,AlarmType.PHONE,null,message,false,false,true,null,null); + } + + + /** + * Send phone alarm with a theme and a message + * @param theme Theme + * @param message Message + * @throws IOException + */ + public static void sendAlarm(String theme, String message) throws IOException { + ArrayList themes = new ArrayList(); + themes.add(new ThemeVO(theme)); + Alarm.sendAlarm(themes,AlarmType.PHONE,null,message,false,false,true,null,null); + } + + + /** + * Construct XML with file attachment + * @param themes Themes + * @param subject Subject + * @param bodymsg Body message + * @param say Is say + * @param email Is email alert + * @param phone Is phone alert + * @param attachment Attachment + * @param exc_add Exclude Address + * @return XML + */ + private static String constructXMLWithFile(List themes,String subject,String bodymsg,boolean say,boolean email,boolean phone, String attachment,String exc_add) { + subject = escapeChar(subject); + String xml=""; + + for (int i=0; i"; + } + } + + xml+=""+subject+""; + xml+=""; + xml+="m "; + xml+=""+(say+"").toUpperCase()+" "; + xml+=""+(email+"").toUpperCase()+""; + xml+=""+(phone+"").toUpperCase()+""; + xml+="5"; + xml+="15"; + if(attachment!=null){ + xml+=""+attachment+""; + } + if(exc_add!=null){ + xml+=""+exc_add+""; + } + xml+=""; + return xml; + } + + /** + * Construct message with file attachment + * @param themes Themes + * @param subject Subject + * @param bodymsg Body message + * @param say Is say + * @param email Is email + * @param phone Is phone + * @param attachment Attachment + * @param exc_add Exclude Address + * @return XML + */ + private static String constructSocketMessageWithFile(List themes,String subject,String bodymsg,boolean say,boolean email,boolean phone, String attachment,String exc_add) { + subject = escapeChar(subject); + + String message = ""; + for (int i=0; i"; + } + if(exc_add!=null){ + xml+=""+exc_add+""; + } + */ + + return message; + } + + /** + * Send XML to iMonitor API + * @param xml XML + * @throws IOException + */ + private static void sendXML(String xml) throws IOException { + try{ + String url=getConfigValue("imonitor.url"); + + PostMethod post = new PostMethod(url); + try { + post.setRequestEntity(new StringRequestEntity(xml,"text/xml",null )); + post.setRequestHeader("Content-type", "text/xml; utf-8"); + HttpClient httpclient = new HttpClient(); + int result = httpclient.executeMethod(post); + } catch (IOException e) { + // e.printStackTrace(); + throw e; + } finally { + post.releaseConnection(); + } + }catch(Exception e){ + e.printStackTrace(); + throw e; + } + } + + /** + * Send message via Socket to iMonitor API + * @param message Message + * @throws Exception + */ + private static void sendSocketMessage(String socketMessage) throws IOException { + String ip = getConfigValue("imonitor.ip"); + int port = Integer.parseInt(getConfigValue("imonitor.port")); + Socket socket = new Socket(ip, port); + OutputStreamWriter osw = new OutputStreamWriter(socket.getOutputStream(), "UTF-8"); + osw.write(socketMessage, 0, socketMessage.length()); + osw.flush(); + socket.close(); + } + + /** + * Escape XML characters + * @param str String + * @return String with escape characters + */ + private static String escapeChar(String str) { + String result = null; + if (str != null) { + result = str.replaceAll("&", "&"); + result = result.replaceAll("\"", """); + result = result.replaceAll("'", "'"); + result = result.replaceAll("<", "<"); + result = result.replaceAll(">", ">"); + } + return result; + } + + + /** + * Get the value of a property defined in config file + * @param propertyName - the name or key of the property you want to retrieve + * @return the string value + * @throws IOException + */ + private static String getConfigValue(String propertyName) throws IOException + { + Properties prop = new Properties(); + String propFileName = "config_alarm.properties"; + + InputStream inputStream = Alarm.class.getClassLoader().getResourceAsStream(propFileName); + + if (inputStream != null) { + prop.load(inputStream); + } else { + throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath"); + } + + // get the property value and print it out + String value = prop.getProperty(propertyName); + return value; + } + +} + + diff --git a/lib-alarm/src/main/java/com/fourelementscapital/alarm/AlarmType.java b/lib-alarm/src/main/java/com/fourelementscapital/alarm/AlarmType.java new file mode 100755 index 0000000..6b89b1d --- /dev/null +++ b/lib-alarm/src/main/java/com/fourelementscapital/alarm/AlarmType.java @@ -0,0 +1,17 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.alarm; + +/** + * Enumeration class for type of the Alarm + */ +public enum AlarmType { + EMAIL, PHONE +} + + \ No newline at end of file diff --git a/lib-alarm/src/main/java/com/fourelementscapital/alarm/ThemeVO.java b/lib-alarm/src/main/java/com/fourelementscapital/alarm/ThemeVO.java new file mode 100755 index 0000000..3aede46 --- /dev/null +++ b/lib-alarm/src/main/java/com/fourelementscapital/alarm/ThemeVO.java @@ -0,0 +1,40 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.alarm; + +import java.io.Serializable; + +/** + * Theme Value Object to store theme name used by Alarm + */ +public class ThemeVO implements Serializable { + + /** + * + */ + private static final long serialVersionUID = 1L; + + /** + * Theme name + */ + private String name; + + /** + * Constructor with name parameter + */ + public ThemeVO(String name) { + this.name = name; + } + + public String getName() { + return name; + } + +} + + diff --git a/lib-alarm/src/main/resources/codehelp/2113.html b/lib-alarm/src/main/resources/codehelp/2113.html new file mode 100755 index 0000000..4dfde30 --- /dev/null +++ b/lib-alarm/src/main/resources/codehelp/2113.html @@ -0,0 +1,7 @@ +
+
    +
  • Windows peer couldn't start Rserve
  • +
  • Make sure there is only one tomcat and Rserve process in the peer
  • +
  • Restart tomcat and Rserve
  • +
+
\ No newline at end of file diff --git a/lib-alarm/src/main/resources/codehelp/2214.html b/lib-alarm/src/main/resources/codehelp/2214.html new file mode 100755 index 0000000..688a323 --- /dev/null +++ b/lib-alarm/src/main/resources/codehelp/2214.html @@ -0,0 +1,8 @@ +
+
    +
  • Peer connection pool for managing multiple RServe connections are not working properly
  • +
  • On unix peer, this may happen when Rserve is not started
  • +
  • On windows, Rserve port used by someother program
  • +
+
+ \ No newline at end of file diff --git a/lib-alarm/src/main/resources/codehelp/2220.html b/lib-alarm/src/main/resources/codehelp/2220.html new file mode 100755 index 0000000..d8a7f91 --- /dev/null +++ b/lib-alarm/src/main/resources/codehelp/2220.html @@ -0,0 +1,5 @@ +
+
    +
  • This is uncategorized error due to certain Runtime error messages on peer or Rengine/Rserve
  • +
+
\ No newline at end of file diff --git a/lib-alarm/src/main/resources/codehelp/2311.html b/lib-alarm/src/main/resources/codehelp/2311.html new file mode 100755 index 0000000..06ab17b --- /dev/null +++ b/lib-alarm/src/main/resources/codehelp/2311.html @@ -0,0 +1,7 @@ +
+
    +
  • Null Error message by RServe/REngine at the end of execution of script. According to RForge documentation, it is classified as unknow and something went wrong, Check http://www.rforge.net/Rserve/faq.html for more info
  • +
  • Your script might not be reached end and could have been Runtime error in the middle
  • +
  • Try running your script with low memory usage settings on your R
  • +
+
\ No newline at end of file diff --git a/lib-alarm/src/main/resources/codehelp/2312.html b/lib-alarm/src/main/resources/codehelp/2312.html new file mode 100755 index 0000000..1cf2c0f --- /dev/null +++ b/lib-alarm/src/main/resources/codehelp/2312.html @@ -0,0 +1,6 @@ +
+
    +
  • R script thrown Runtime exception
  • +
  • Make sure runtime exceptions from Database,Network and File resources are caught and written in console
  • +
+
\ No newline at end of file diff --git a/lib-alarm/src/main/resources/codehelp/2315.html b/lib-alarm/src/main/resources/codehelp/2315.html new file mode 100755 index 0000000..d9f20f7 --- /dev/null +++ b/lib-alarm/src/main/resources/codehelp/2315.html @@ -0,0 +1,8 @@ +
+
    +
  • If the script is taking more than specified time, then it will be removed from the queue not matter eventually successful or failure
  • +
  • Consider increasing execution time out by clicking speaker icon above the live queue of the scheduler
  • +
  • Code modified and the new code consumes more time than previous executions
  • +
  • This will also happen when peer restarted or stopped while task is running
  • +
+
\ No newline at end of file diff --git a/lib-alarm/src/main/resources/codehelp/2316.html b/lib-alarm/src/main/resources/codehelp/2316.html new file mode 100755 index 0000000..fdebe49 --- /dev/null +++ b/lib-alarm/src/main/resources/codehelp/2316.html @@ -0,0 +1,7 @@ +
+
    +
  • The task still in the queue because peer couldn't reach server to update the status of task and server removes it as later found no longer running in peer
  • +
  • Peer was running memory intensive script or unix peer Rserve threads were fully utilized at that time
  • +
  • Please check the queue history later to know the final status of the execution
  • +
+
\ No newline at end of file diff --git a/lib-alarm/src/main/resources/codehelp/2317.html b/lib-alarm/src/main/resources/codehelp/2317.html new file mode 100755 index 0000000..0e788bb --- /dev/null +++ b/lib-alarm/src/main/resources/codehelp/2317.html @@ -0,0 +1,5 @@ +
+
    +
  • No Room to Execute the Script because peer receives 2 tasks at the same time and accepts only one and the other sent back to server and it will be resent to different peer
  • +
+
\ No newline at end of file diff --git a/lib-alarm/src/main/resources/codehelp/2318.html b/lib-alarm/src/main/resources/codehelp/2318.html new file mode 100755 index 0000000..eca4f6e --- /dev/null +++ b/lib-alarm/src/main/resources/codehelp/2318.html @@ -0,0 +1,6 @@ +
+
    +
  • Task removed because peer says it is no longer running in peer
  • +
  • This can happen if peer restarted
  • +
+
\ No newline at end of file diff --git a/lib-alarm/src/main/resources/codehelp/2319.html b/lib-alarm/src/main/resources/codehelp/2319.html new file mode 100755 index 0000000..5700b88 --- /dev/null +++ b/lib-alarm/src/main/resources/codehelp/2319.html @@ -0,0 +1,6 @@ +
+
    +
  • Any run time exception message at peer side, usually this will be thrown when if there was problem in database connectivity, unexpected value, unchecked null vale and so on
  • +
  • Make sure runtime exceptions from Database,Network and File resources are caught and written in console
  • +
+
\ No newline at end of file diff --git a/lib-alarm/src/main/resources/codehelp/2320.html b/lib-alarm/src/main/resources/codehelp/2320.html new file mode 100755 index 0000000..2d97ae2 --- /dev/null +++ b/lib-alarm/src/main/resources/codehelp/2320.html @@ -0,0 +1,6 @@ +
+
    +
  • Any runtime exception message on server side,usually this will be thrown when there is problem in database connectivity, unexpected value, unchecked null vale and so on.
  • +
  • Make sure runtime exceptions from Database,Network and File resources are caught and written in console
  • +
+
\ No newline at end of file diff --git a/lib-alarm/src/main/resources/codehelp/2321.html b/lib-alarm/src/main/resources/codehelp/2321.html new file mode 100755 index 0000000..64e7e4d --- /dev/null +++ b/lib-alarm/src/main/resources/codehelp/2321.html @@ -0,0 +1,5 @@ +
+
    +
  • This will happen when the next trigger time of the script arrived and still current triggered execution is waiting because of one or more dependecies not successfull.
  • +
+
\ No newline at end of file diff --git a/lib-alarm/src/main/resources/config_alarm.properties b/lib-alarm/src/main/resources/config_alarm.properties new file mode 100755 index 0000000..b20b7eb --- /dev/null +++ b/lib-alarm/src/main/resources/config_alarm.properties @@ -0,0 +1,12 @@ +ignore.alarm=false + +#imonitor.url=http://10.153.64.31:5001/iMonitor/Responder.aspx +imonitor.ip=127.0.0.1 +imonitor.port=1777 + +#dump XML to file instead of sending it to iMonitor API +alarm.dump=false +#alarm.dump=true +#alarm.dump.folder=/temp/scheduler-alarm/ + + diff --git a/lib-alarm/src/main/resources/log4j2.xml b/lib-alarm/src/main/resources/log4j2.xml new file mode 100755 index 0000000..b8d69f3 --- /dev/null +++ b/lib-alarm/src/main/resources/log4j2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/lib-alarm/src/test/java/com/fourelementscapital/alarm/LibAlarmTest.java b/lib-alarm/src/test/java/com/fourelementscapital/alarm/LibAlarmTest.java new file mode 100755 index 0000000..f087f18 --- /dev/null +++ b/lib-alarm/src/test/java/com/fourelementscapital/alarm/LibAlarmTest.java @@ -0,0 +1,54 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.alarm; + +import java.util.ArrayList; +import javax.swing.JOptionPane; +import junit.framework.TestCase; + +/** + * lib-alarm unit test + */ +public class LibAlarmTest extends TestCase { + + /** + * Test send alarm + */ + public void testSendAlarm() + { + + try{ + + ArrayList themes=new ArrayList(); + themes.add(new ThemeVO("computing")); + themes.add(new ThemeVO("etrading")); + + // Remove the comment tag to test. The code are commented out to prevent sending alarm when installing this lib : + + // send alarm (default) : + //Alarm.sendAlarm( themes, AlarmType.EMAIL, "Test subject", "Test message", false, true, false, null, null); + + // send alarm with list of theme & a message : + //Alarm.sendAlarm( themes, "Test message 2"); + + // send alarm with a theme & a message : + //Alarm.sendAlarm( "computing", "Test message 3"); + //JOptionPane.showMessageDialog(null, "send socket message done"); + assertTrue(true); + }catch(Exception e){ + e.printStackTrace(); + assertTrue(false); + } + + } + + +} + + + diff --git a/lib-auth/pom.xml b/lib-auth/pom.xml new file mode 100755 index 0000000..fef3166 --- /dev/null +++ b/lib-auth/pom.xml @@ -0,0 +1,42 @@ + + + 4.0.0 + + com.fourelementscapital + lib + ${revision} + + com.fourelementscapital + lib-auth + jar + lib-auth + Wiki authentication, Superuser authentication, Get user themes access permission + http://www.fourelementscapital.com + + + mysql + mysql-connector-java + + + javax.servlet + servlet-api + + + org.kohsuke + libpam4j + + + commons-codec + commons-codec + + + org.apache.logging.log4j + log4j-api + + + + +UTF-8 + + diff --git a/lib-auth/src/main/java/com/fourelementscapital/auth/DBManager.java b/lib-auth/src/main/java/com/fourelementscapital/auth/DBManager.java new file mode 100755 index 0000000..011b608 --- /dev/null +++ b/lib-auth/src/main/java/com/fourelementscapital/auth/DBManager.java @@ -0,0 +1,148 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.auth; + +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.Map; + +/** + * The DBManager class provides an Access Interface between the 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. + */ +class DBManager { + + /** + * The database name for the current instance of DBManager + */ + private String dbName; + + /** + * The connection string for the database connection. Stored in config file + */ + 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 file if not found on this machine under resources folder + */ + public void connect() throws SQLException, ClassNotFoundException, IOException + { + //String url = "jdbc:jtds:sqlserver://"+databaseServerAddress+";DatabaseName="+databaseName; + String driver = Utils.GetConfigValue("database_"+dbName.toLowerCase()+"_"+"driver"); + + Class.forName(driver); + conn = DriverManager.getConnection(Utils.GetConfigValue("database_"+dbName.toLowerCase()+"_"+"connstring")); + connectionString = conn.toString(); + } + + /** + * Closes the current Database connection. + * @throws SQLException + */ + public void closeConnection() throws SQLException + { + if(!conn.isClosed()) + conn.close(); + if(resultSet!=null) + resultSet.close(); + } + + /** + * This function returns the result set of the SQL Query used by GetDatabase; + * @param query The SQL Query passed to it. + * @return + * @throws SQLException + */ + private 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; + } + + /** + * 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. new ArrayList(); + * be passed to select all fields (*) + * @param queryParams A hashmap containing the selection filters in the format [column_name]=[value] + * @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 selectedFields, Map 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 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); + } + +} + + + \ No newline at end of file diff --git a/lib-auth/src/main/java/com/fourelementscapital/auth/PAMAuthentication.java b/lib-auth/src/main/java/com/fourelementscapital/auth/PAMAuthentication.java new file mode 100755 index 0000000..440b213 --- /dev/null +++ b/lib-auth/src/main/java/com/fourelementscapital/auth/PAMAuthentication.java @@ -0,0 +1,126 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.auth; + +import java.util.Set; + +import org.jvnet.libpam.PAM; +import org.jvnet.libpam.PAMException; +import org.jvnet.libpam.UnixUser; + +/** + * PAMAuthentication will authenticate user to local unix machine using PAM. + * This class is a wrapper of libpam4j, a Java/PAM bindings using JNA, to use internally in 4ECaps. + */ +public class PAMAuthentication { + + /** + * Check whether user is exist + * @param username Username + * @return true if user is exist + */ + public static boolean isExist(String username) { + return UnixUser.exists(username); + } + + /** + * Get current user logged in Unix system + * @return username of user logged in + */ + public static String getCurrentUserLoggedIn() { + return System.getProperty("user.name"); + } + + /** + * Get username of current user logged in + * @return username of user logged in + * @throws PAMException + */ + public static String getUsername() throws PAMException { + UnixUser ux = new UnixUser(getCurrentUserLoggedIn()); + return ux.getUserName(); + } + + /** + * Get UID of current user logged in + * @return UID of user logged in + * @throws PAMException + */ + public static int getUID() throws PAMException { + UnixUser ux = new UnixUser(getCurrentUserLoggedIn()); + return ux.getUID(); + } + + /** + * Get GID of current user logged in + * @return GID of user logged in + * @throws PAMException + */ + public static int getGID() throws PAMException { + UnixUser ux = new UnixUser(getCurrentUserLoggedIn()); + return ux.getGID(); + } + + /** + * Get gecos (the real name) of current user logged in + * @return gecos of user logged in + * @throws PAMException + */ + public static String getGecos() throws PAMException { + UnixUser ux = new UnixUser(getCurrentUserLoggedIn()); + return ux.getGecos(); + } + + /** + * Get home directory of current user logged in + * @return home directory of user logged in + * @throws PAMException + */ + public static String getDir() throws PAMException { + UnixUser ux = new UnixUser(getCurrentUserLoggedIn()); + return ux.getDir(); + } + + /** + * Get the shell of current user logged in + * @return the shell of user logged in + * @throws PAMException + */ + public static String getShell() throws PAMException { + UnixUser ux = new UnixUser(getCurrentUserLoggedIn()); + return ux.getShell(); + } + + /** + * Get the groups that user logged in belongs to + * @return the groups of user logged in + * @throws PAMException + */ + public static Set getGroups() throws PAMException { + UnixUser ux = new UnixUser(getCurrentUserLoggedIn()); + return ux.getGroups(); + } + + /** + * Authenticate user + * @param service Service + * @param username Username + * @param password Password + * @return authenticated user in UnixUser object + * @throws PAMException + */ + public static UnixUser authenticate(String service, String username, String password) throws PAMException { + PAM p = new PAM(service); + return p.authenticate(username, password); + } + + +} + + + diff --git a/lib-auth/src/main/java/com/fourelementscapital/auth/SuperUserAuthentication.java b/lib-auth/src/main/java/com/fourelementscapital/auth/SuperUserAuthentication.java new file mode 100755 index 0000000..c2e39b4 --- /dev/null +++ b/lib-auth/src/main/java/com/fourelementscapital/auth/SuperUserAuthentication.java @@ -0,0 +1,177 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.auth; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.security.MessageDigest; + +import org.apache.commons.codec.binary.Hex; + +/** + * SuperUserAuthentication provides mechanism to store password into a file. + * This mechanism is implemented in superuser authentication of an application. + * The config file containing the path of saved password located in the resources folder. + */ +public class SuperUserAuthentication { + + /** + * Private constructor to prevent instantiation. + */ + private SuperUserAuthentication() + { + } + + /** + * Password filename that will be created in the folder path. + */ + private static String passwordFilename = "superuser.pwd"; + + + /** + * Validate password whether it equals with the encrypted file, if file is not exist then set the password by creating it. + * @param pwd Password + * @return true if process succeed + * @throws Exception + */ + public static boolean validateOrSet(String pwd) throws Exception { + File file=getPwdFile(); + String encpwd=null; + boolean rtn=false; + if(file.exists()){ + encpwd=readFile(file); + } + if(encpwd!=null && !encpwd.equals("")){ + rtn=isValid(pwd,encpwd); + }else{ + setPassword(pwd); + rtn=true; + } + return rtn; + } + + + /** + * Change password to a new one if password is valid + * @param pwd Password + * @param newpassword New password + * @return true if process succeed + * @throws Exception + */ + public static boolean changePwd(String pwd,String newpassword) throws Exception { + boolean rtn=validateOrSet(pwd); + + if(rtn){ + //Thread.sleep(1000); //wait till it closes the stream .... + setPassword(newpassword); + } + return rtn; + } + + + /** + * Check whether password is valid, compared to encrypted password + * @param password Password + * @param encpass Encrypted password + * @return true if encrypted password equals salt password + * @throws Exception + */ + private static boolean isValid(String password, String encpass) throws Exception { + String salt=encpass.split(":")[2]; + String saltpass=":B:"+salt+":"+getMD5(salt+"-"+getMD5(password)); + if(encpass.equals(saltpass)){ + return true; + }else{ + return false; + } + } + + + /** + * Get MD5 of source string + * @param source Source string + * @return MD5 string + * @throws Exception + */ + private static String getMD5(String source) throws Exception { + + byte[] bytesOfMessage = source.getBytes(); + + java.security.MessageDigest md = MessageDigest.getInstance("MD5"); + byte[] thedigest = md.digest(bytesOfMessage); + + final String result = new String(Hex.encodeHex(thedigest)); + return result; + } + + + /** + * Set password to encrypted file + * @param plain_pwd Plain password + * @throws Exception + */ + private static void setPassword(String plain_pwd) throws Exception { + BufferedWriter writer = null; + try { + String salt="d1e93dec"; + String saltpass=":B:"+salt+":"+getMD5(salt+"-"+getMD5(plain_pwd)); + File file=getPwdFile(); + writer = new BufferedWriter(new FileWriter(file)); + writer.write(saltpass); + } + finally { + if (writer != null) writer.close(); + } + + } + + + /** + * Get password file + * @return file + * @throws Exception + */ + private static File getPwdFile() throws Exception { + String root = Utils.GetConfigValue("superuser_password_folder"); + File file =new File(root + passwordFilename); + if ( !file.exists() ) { + file.createNewFile(); + } + return file; + } + + + /** + * Read file + * @param path Path file + * @throws IOException + */ + private static String readFile(File path) throws IOException { + StringBuilder contents = new StringBuilder(); + BufferedReader input = null; + try { + input = new BufferedReader(new FileReader(path)); + String line = input.readLine(); + if (line != null) { + contents.append(line); + } + } + finally { + if (input != null) input.close(); + } + return contents.toString(); + } + +} + + + \ No newline at end of file diff --git a/lib-auth/src/main/java/com/fourelementscapital/auth/SystemAuthentication.java b/lib-auth/src/main/java/com/fourelementscapital/auth/SystemAuthentication.java new file mode 100755 index 0000000..2a62feb --- /dev/null +++ b/lib-auth/src/main/java/com/fourelementscapital/auth/SystemAuthentication.java @@ -0,0 +1,30 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.auth; + +public class SystemAuthentication { + + /** + * Get current user logged in Unix system + * @return username of user logged in + */ + public static String getCurrentUserLoggedIn() { + return System.getProperty("user.name"); + } + + /** + * Check whether a user logged in to the system + * @return true if user logged in + */ + public static boolean isUserLoggedIn() { + return (null != getCurrentUserLoggedIn() && !"".equals(getCurrentUserLoggedIn())); + } + +} + + \ No newline at end of file diff --git a/lib-auth/src/main/java/com/fourelementscapital/auth/UserThemeAccessPermission.java b/lib-auth/src/main/java/com/fourelementscapital/auth/UserThemeAccessPermission.java new file mode 100755 index 0000000..38267fa --- /dev/null +++ b/lib-auth/src/main/java/com/fourelementscapital/auth/UserThemeAccessPermission.java @@ -0,0 +1,147 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.auth; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + + +/** + * UserThemeAccessPermission provides functions to convert theme access permissions into 'rwx' permission group. + * Example : a user has 'X' permission in 'computing' theme. The 'computing' theme then will be included in 'rwx' permission group. + * Another Example : a user has 'U' permission in 'etrading' theme. The 'etrading' theme then will be included in 'rx' permission group. + */ +public class UserThemeAccessPermission { + + /** + * Username + */ + private String username; + + /** + * 'rwx' permission group + */ + private ArrayList rwx = new ArrayList(); + + /** + * 'rx' permission group + */ + private ArrayList rx = new ArrayList(); + + /** + * 'r' permission group + */ + private ArrayList r = new ArrayList(); + + + /** + * Constructor. Creates a new UserThemeAccessPermission object + * @param un Username + */ + public UserThemeAccessPermission(String un) { + this.username=un; + } + + /** + * Get rwx permission group + * @return rwx list + */ + public ArrayList getRwx() { + return rwx; + } + + /** + * Get rx permission group + * @return rx list + */ + public ArrayList getRx() { + return rx; + } + + /** + * Get r permission group + * @return r list + */ + public ArrayList getR() { + return r; + } + + /** + * Add permission with user's themes + * @param themes + * @throws Exception + */ + public void addPermissionWithThemes(Map themes) throws Exception { + try{ + Map resolved = this.replacePermissions(themes); + for(Iterator it=resolved.keySet().iterator();it.hasNext();){ + String theme=it.next(); + String val=resolved.get(theme); + if(val!=null && val.trim().equalsIgnoreCase("rwx")) getRwx().add(theme); + if(val!=null && val.trim().equalsIgnoreCase("rx")) getRx().add(theme); + if(val!=null && val.trim().equalsIgnoreCase("r")) getR().add(theme); + } + } catch(Exception e) { + throw e; + } + } + + /** + * Replace the permission letter with 'rwx'. + * @param data + * @return themes with 'rwx' permissions + * @throws Exception + */ + public static Map replacePermissions(Map data) throws Exception { + HashMap perm = new HashMap(); + perm.put("X","rwx"); + perm.put("S","rwx"); + perm.put("B","rwx"); + perm.put("C","rwx"); + perm.put("U","rx"); + perm.put("N","r"); + perm.put("M","rwx"); + for(String key: data.keySet()){ + data.put(key, perm.get(data.get(key))); + } + return data; + } + + /** + * Overriding to make unique objects per username + * @param obj Instance of AuthUser or other object + * @return true if obj's username is equal with class' username + */ + public boolean equals(Object obj ) { + if(obj instanceof UserThemeAccessPermission){ + UserThemeAccessPermission other=(UserThemeAccessPermission)obj; + if(other.username.equals(this.username)){ + return true; + }else{ + return false; + } + }else{ + return false; + } + } + + /** + * Override toString() method + * @return username string + */ + public String toString() { + return ""+this.username; + } + + +} + + + diff --git a/lib-auth/src/main/java/com/fourelementscapital/auth/Utils.java b/lib-auth/src/main/java/com/fourelementscapital/auth/Utils.java new file mode 100755 index 0000000..f31c6e3 --- /dev/null +++ b/lib-auth/src/main/java/com/fourelementscapital/auth/Utils.java @@ -0,0 +1,82 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.auth; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Date; +import java.util.Properties; + +/** + * This class contains Static Utility functions + */ +class Utils { + + /** + * 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 + */ + protected static String Join(ArrayList al,String delimiter) + { + return al.toString().replaceAll("\\[|\\]", "").replaceAll(", ",delimiter); + } + + /** + * Get the value of a property defined in config file + * @param propertyName - the name or key of the property you want to retrieve + * @return the string value + * @throws IOException + */ + protected static String GetConfigValue(String propertyName) throws IOException + { + Properties prop = new Properties(); + String propFileName = "config_auth.properties"; + + InputStream inputStream = Utils.class.getClassLoader().getResourceAsStream(propFileName); + + if (inputStream != null) { + prop.load(inputStream); + } else { + throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath"); + } + + Date time = new Date(System.currentTimeMillis()); + + // get the property value and print it out + String value = prop.getProperty(propertyName); + return value; + } + + + /** + * Capitalize first char of a word. + * Example : 'this is text. another text' will be converted to 'This Is Text. Another Text' + * @param string + * @return string + */ + protected static String capitalizeString(String string) { + char[] chars = string.toLowerCase().toCharArray(); + boolean found = false; + for (int i = 0; i < chars.length; i++) { + if (!found && Character.isLetter(chars[i])) { + chars[i] = Character.toUpperCase(chars[i]); + found = true; + } else if (Character.isWhitespace(chars[i]) || chars[i]=='.' || chars[i]=='\'') { // You can add other chars here + found = false; + } + } + return String.valueOf(chars); + } + +} + + diff --git a/lib-auth/src/main/java/com/fourelementscapital/auth/WikiAuthentication.java b/lib-auth/src/main/java/com/fourelementscapital/auth/WikiAuthentication.java new file mode 100755 index 0000000..972d186 --- /dev/null +++ b/lib-auth/src/main/java/com/fourelementscapital/auth/WikiAuthentication.java @@ -0,0 +1,176 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.auth; + +import java.security.MessageDigest; +import java.sql.ResultSet; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; +import java.util.Vector; + +import org.apache.commons.codec.binary.Hex; + +/** + * WikiAuthentication will check / validate user authentication to Wiki database. + * It also provide functions to get encrypted password of specific user & get list of Wiki users. + */ +public class WikiAuthentication { + + public static final String SESSION_LOGGED_USER = "session^logged^user"; + + /** + * Private constructor to prevent instantiation. + */ + private WikiAuthentication() + { + } + + /** + * Validate Wiki User. If user_name is valid & password being encrypted equals value in database then it will return true. + * @param user_name + * @param password + * @return is user valid + * @throws Exception + */ + public static boolean validateUser(String user_name, String password) throws Exception { + String encpass = getEncryptedPwd(user_name); + return isValid(password,encpass); + } + + /** + * Validate Wiki User With Encrypted Password. If user_name is valid & encrypted password equals value in database then it will return true. + * @param user_name + * @param encryptedPassword + * @return is user valid + * @throws Exception + */ + public static boolean validateUserEncryptedPassword(String user_name, String encryptedPassword) throws Exception { + String encpass = getEncryptedPwd(user_name); + if (encryptedPassword == null) { + return false; + } + return (encryptedPassword.equals(encpass)); + } + + + /** + * Check whether password is valid, compare to encrypted password + * @param password Password + * @param encpass Encrypted password + * @return true if encrypted password equals salt password + * @throws Exception + */ + private static boolean isValid(String password, String encpass) throws Exception { + if (encpass == null || password == null) return false; + String salt=encpass.split(":")[2]; + String saltpass=":B:"+salt+":"+getMD5(salt+"-"+getMD5(password)); + if(encpass.equals(saltpass)){ + return true; + }else{ + return false; + } + } + + + /** + * Get MD5 of source string + * @param source Source string + * @return MD5 string + * @throws Exception + */ + private static String getMD5(String source) throws Exception { + + byte[] bytesOfMessage = source.getBytes(); + + java.security.MessageDigest md = MessageDigest.getInstance("MD5"); + byte[] thedigest = md.digest(bytesOfMessage); + + final String result = new String(Hex.encodeHex(thedigest)); + return result; + } + + + /** + * List / get all Wiki users. The fields returned are : user_name, user_real_name, user_password, user_email. + * @return users + * @throws Exception + */ + public static Vector> listUsers() throws Exception { + + Vector> rtn = null; + + DBManager dbm = new DBManager("wiki"); + dbm.connect(); + + //sql = "select user_name,user_real_name,user_password,user_email from user"; + + ArrayList selectedFields = new ArrayList(); + selectedFields.add("user_name"); + selectedFields.add("user_real_name"); + selectedFields.add("user_password"); + selectedFields.add("user_email"); + + ResultSet rs = dbm.GetDatabase("user", selectedFields, null, null); + + rtn = new Vector>(); + + while(rs.next()){ + String user_name=rs.getString("user_name"); + String user_real_name=rs.getString("user_real_name"); + String user_password=rs.getString("user_password"); + String user_email=rs.getString("user_email"); + HashMap h=new HashMap(); + h.put("user_name",user_name ); + h.put("user_real_name",user_real_name ); + h.put("user_password",user_password ); + h.put("user_email",user_email ); + rtn.add(h); + } + dbm.closeConnection(); + + return rtn; + } + + + /** + * Get encrypted password of specific user + * @param user + * @return encrypted password + * @throws Exception + */ + public static String getEncryptedPwd(String user) throws Exception { + + if ((user == null) || user.equals("")) return null; + + String rtn = null; + + DBManager dbm = new DBManager("wiki"); + dbm.connect(); + + //sql = "select CAST(user_password AS CHAR(10000) CHARACTER SET utf8) as pass from user where user_name=?" + + ArrayList selectedFields = new ArrayList(); + selectedFields.add("CAST(user_password AS CHAR(10000) CHARACTER SET utf8) as pass"); + + Map queryParams = new HashMap(); + queryParams.put("user_name", Utils.capitalizeString(user)); + + ResultSet rs = dbm.GetDatabase("user", selectedFields, queryParams, null); + + while(rs.next()){ + rtn=rs.getString("pass"); + } + dbm.closeConnection(); + + return rtn; + } + +} + + diff --git a/lib-auth/src/main/java/com/fourelementscapital/auth/WikiAuthenticationServlet.java b/lib-auth/src/main/java/com/fourelementscapital/auth/WikiAuthenticationServlet.java new file mode 100755 index 0000000..01f984d --- /dev/null +++ b/lib-auth/src/main/java/com/fourelementscapital/auth/WikiAuthenticationServlet.java @@ -0,0 +1,99 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.auth; + +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * WikiAuthenticationServlet is a servlet to facilitate automatic Wiki authentication. + * How to use this servlet, see documentation : http://wiki.4ecap.com/4ECwiki/Lib-auth#Automatic_Wiki_Authentication. + */ +public class WikiAuthenticationServlet extends HttpServlet { + + private static final long serialVersionUID = 1L; + + private static final String LOGOUT = "logout"; + + + /** + * Handle post request, validate username & encrypted password by comparing them with Wiki database. + * If valid, generate session to be used throughout the app then redirect to app url. + * If not valid, redirect to wiki login url. + * @param request request + * @param response response + */ + public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + try { + + // get request parameters : + String username = request.getParameter("username"); + String encryptedPassword = request.getParameter("encryptedPassword"); + String redirectUrl = request.getParameter("redirectUrl"); + + // get wiki login url from config file + String wikiLoginUrl = Utils.GetConfigValue("wiki_login_url"); + + // if there's already session then redirect to app url + if (request.getSession().getAttribute(WikiAuthentication.SESSION_LOGGED_USER) != null) { + response.sendRedirect(redirectUrl); + } + + // if there's null parameter then redirect to wiki login + else if (username == null || encryptedPassword == null || redirectUrl == null) { + response.sendRedirect(wikiLoginUrl); + } + + // validate username & encrypted password, if valid then redirect to app url + else if (WikiAuthentication.validateUserEncryptedPassword(username, encryptedPassword)) { + // set session for app using lib-auth + request.getSession().setAttribute(WikiAuthentication.SESSION_LOGGED_USER, username); + response.sendRedirect(redirectUrl); + } + + // else, redirect to wiki login + else { + response.sendRedirect(wikiLoginUrl); + } + + }catch(Exception e){ + e.printStackTrace(); + } + + } + + + /** + * Handle get request, logout (remove session) and redirect to a url. + * @param request request + * @param response response + */ + public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + String method = request.getParameter("method"); + + // logout, remove session + if(method != null && method.equals(this.LOGOUT)) { + if (request.getSession().getAttribute(WikiAuthentication.SESSION_LOGGED_USER) != null) { + request.getSession().removeAttribute(WikiAuthentication.SESSION_LOGGED_USER); + } + } + + // redirect to Wiki page + response.sendRedirect(Utils.GetConfigValue("wiki_login_url")); + } + + +} + + diff --git a/lib-auth/src/main/resources/config_auth.properties b/lib-auth/src/main/resources/config_auth.properties new file mode 100755 index 0000000..a6ea781 --- /dev/null +++ b/lib-auth/src/main/resources/config_auth.properties @@ -0,0 +1,7 @@ +database_wiki_connstring=jdbc:mysql://wiki.4ecap.com/4EwikiDB1?user=4ecremoteuser&password=4ecrmt2011 +database_wiki_driver=com.mysql.jdbc.Driver + +wiki_login_url=http://wiki.4ecap.com + +superuser_password_folder=/tmp/ +#superuser_password_folder=d:\\tmp\\ \ No newline at end of file diff --git a/lib-auth/src/main/resources/log4j2.xml b/lib-auth/src/main/resources/log4j2.xml new file mode 100755 index 0000000..b8d69f3 --- /dev/null +++ b/lib-auth/src/main/resources/log4j2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/lib-auth/src/test/java/com/fourelementscapital/auth/LibAuthTest.java b/lib-auth/src/test/java/com/fourelementscapital/auth/LibAuthTest.java new file mode 100755 index 0000000..9bff80b --- /dev/null +++ b/lib-auth/src/test/java/com/fourelementscapital/auth/LibAuthTest.java @@ -0,0 +1,215 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.auth; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; +import java.util.Vector; +import javax.swing.JOptionPane; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import junit.framework.Assert; +import junit.framework.TestCase; + + +/** + * lib-auth unit test + */ +public class LibAuthTest extends TestCase { + + private static final Logger log = LogManager.getLogger(LibAuthTest.class.getName()); + + /** + * Wiki authentication test : login, check login, get authenticated user, get user access. + */ + public void testWikiAuthentication() + { + log.debug(">>>>>> testWikiAuthentication()"); + try { + + // wiki login : + String user = "bot"; + String password = "bot2018"; + + boolean success= WikiAuthentication.validateUser(user, password); // validate wiki user + log.debug("validate wiki user : " + success); + JOptionPane.showMessageDialog(null, "validate wiki user success"); + + HashMap loginMap = new HashMap(); + if(!success){ + loginMap.put("message", "Invalid username or password"); + Assert.assertTrue(false); // scenario : login must success + }else{ + loginMap.put("authenticatedUser", user); + // example : get encrypted password and store it to map for further process + String pss = WikiAuthentication.getEncryptedPwd(user); + loginMap.put("encryptedPassword", pss); + } + + // store validate user result to map + loginMap.put("loggedin", Boolean.toString(success)); + + // check whether user login / not : + boolean loggedin = Boolean.parseBoolean(loginMap.get("loggedin")); + log.debug("is user logged in : " + loggedin); + Assert.assertTrue(loggedin); + + // get authenticated user : + String authenticatedUser = loginMap.get("authenticatedUser"); + log.debug("get authenticatedUser : " + authenticatedUser); + + // Get user access. Use dummy themes for testing purpose. + // To get themes from infrastructureDB : Map themes = infrastructureDB.getThemes4Users(user). It needs lib-db library. + Map themes= new HashMap(); + themes.put("computing", "X"); + themes.put("etrading", "U"); + themes.put("execution", "N"); + themes.put("bb", "B"); + + UserThemeAccessPermission userTheme = new UserThemeAccessPermission(user); + userTheme.addPermissionWithThemes(themes); + ArrayList rwxList = userTheme.getRwx(); + log.debug("rwxList : "); + for (String rwx : rwxList) { + log.debug("- " + rwx); + } + Assert.assertTrue(rwxList.contains("computing") && rwxList.contains("bb")); // getRwx() must contains 'computing' & 'bb' + + ArrayList rxList = userTheme.getRx(); + log.debug("rxList : "); + for (String rx : rxList) { + log.debug("- " + rx); + } + Assert.assertTrue(rxList.contains("etrading")); // getRx() must contains 'etrading' + + ArrayList rList = userTheme.getR(); + log.debug("rList : "); + for (String r : rList) { + log.debug("- " + r); + } + Assert.assertTrue(rList.contains("execution")); // getR() must contains 'execution' + } + catch (Exception e) { + e.printStackTrace(); + assertTrue( false ); + } + } + + + /** + * Super user authentication test : validate / set / change password, check login, get authenticated user + */ + public void testSuperUserAuthentication() + { + log.debug(">>>>>> testSuperUserAuthentication()"); + try { + + String user = "administrator"; + String password = "password1"; + String newpassword = "password1"; + + Map loginMap = new HashMap(); + boolean success = false; + + // if user is 'administrator' and newpassword is not null & not empty then it's a change password : + if(user.equalsIgnoreCase("administrator") && newpassword!=null && !newpassword.equals("")){ + + // Remove the comment tag to test. The code are commented out to prevent creating password file when installing this lib (edit properties file first) : + //success = SuperUserAuthentication.changePwd(password,newpassword); + //temporary var value when install this lib : + success = true; + + //log.debug("change superuser password : " + newpassword); + //JOptionPane.showMessageDialog(null, "Change superuser password done. Password located in /tmp/superuser.pwd"); + } + // else : validate if password is already set or set password if password has not been set : + else{ + + // Remove the comment tag to test. The code are commented out to prevent creating password file when installing this lib (edit properties file first) : + //success= SuperUserAuthentication.validateOrSet(password); + //temporary var value when install this lib : + success = true; + + //log.debug("validate / set superuser password : " + password); + //JOptionPane.showMessageDialog(null, "validate/set superuser password"); + } + + if(!success){ + loginMap.put("message", "Invalid username or password"); + Assert.assertTrue(false); // scenario : login must success + } + else { + loginMap.put("authenticatedUser", user); + } + loginMap.put("loggedin", Boolean.toString(success)); // store validate superuser result to map + + // check whether login / not : + boolean loggedin = Boolean.parseBoolean(loginMap.get("loggedin")); + log.debug("is superuser logged in : " + loggedin); + Assert.assertTrue(loggedin); + + // get authenticated user : + String authenticated_user = loginMap.get("authenticatedUser"); + log.debug("authenticated superuser : " + authenticated_user); + + //Assert.assertTrue(true); + } + catch (Exception e) { + e.printStackTrace(); + } + } + + + /** + * List Wiki users test + */ + public void testListWikiUsers() + { + log.debug(">>>>>> testListWikiUsers()"); + try { + Vector> result = WikiAuthentication.listUsers(); + + log.debug("wiki user count : " + result.size()); + JOptionPane.showMessageDialog(null, "Wiki user count: "+result.size()); + + for (int i=0; i h = (HashMap) result.get(i); +/* + log.debug(">>>>>>>>>> user_name : " + h.get("user_name")); + log.debug(">>>>>>>>>> user_real_name : " + h.get("user_real_name")); + log.debug(">>>>>>>>>> user_password : " + h.get("user_password")); + log.debug(">>>>>>>>>> user_email : " + h.get("user_email")); +*/ + } + assertTrue( true ); + } + catch (Exception e) { + e.printStackTrace(); + assertTrue( false ); + } + } + + + /** + * System authentication test + */ + public void testSystemAuthentication() + { + log.debug(">>>>>> testSystemAuthentication()"); + log.debug("SystemAuthentication.isUserLoggedIn() : " + SystemAuthentication.isUserLoggedIn()); + + assertTrue( SystemAuthentication.isUserLoggedIn() ); + //assertTrue( true ); + } + +} + + \ No newline at end of file diff --git a/lib-db/pom.xml b/lib-db/pom.xml new file mode 100755 index 0000000..931714c --- /dev/null +++ b/lib-db/pom.xml @@ -0,0 +1,53 @@ + + 4.0.0 + + + com.fourelementscapital + lib + ${revision} + + + com.fourelementscapital + lib-db + jar + lib-db + Lower level DAL (Data Access Layer) for database call + http://www.fourelementscapital.com + + + + org.apache.logging.log4j + log4j-api + + + commons-dbutils + commons-dbutils + 1.1 + + + + org.apache.commons + commons-jcs-core + 2.2 + + + + com.jolbox + bonecp + + + mysql + mysql-connector-java + + + com.microsoft.sqlserver + mssql-jdbc + + + org.apache.logging.log4j + log4j-core + 2.8.1 + + + diff --git a/lib-db/src/main/java/com/fourelementscapital/db/AbstractTeamOrgDB.java b/lib-db/src/main/java/com/fourelementscapital/db/AbstractTeamOrgDB.java new file mode 100755 index 0000000..cfe8b0c --- /dev/null +++ b/lib-db/src/main/java/com/fourelementscapital/db/AbstractTeamOrgDB.java @@ -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 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 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; + } + +} + + diff --git a/lib-db/src/main/java/com/fourelementscapital/db/BBSyncDB.java b/lib-db/src/main/java/com/fourelementscapital/db/BBSyncDB.java new file mode 100755 index 0000000..9394688 --- /dev/null +++ b/lib-db/src/main/java/com/fourelementscapital/db/BBSyncDB.java @@ -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 contract,String marketsector, + Timestamp lastsync, Map 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 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 getContractNames2RefSync(boolean pendingonly) + throws Exception; + + /** + * Get security names to ref synchronized + * @param pendingonly is pending only + * @throws Exception + * @return security info + */ + public abstract Map getSecurityNames2RefSync( + boolean pendingonly) throws Exception; + + /** + * Get security name to ref synchronized + * @param ticker ticker + * @throws Exception + * @return security info + */ + public abstract Map 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 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 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; + + + +} + + diff --git a/lib-db/src/main/java/com/fourelementscapital/db/Config.java b/lib-db/src/main/java/com/fourelementscapital/db/Config.java new file mode 100755 index 0000000..d7aac3e --- /dev/null +++ b/lib-db/src/main/java/com/fourelementscapital/db/Config.java @@ -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; + } + } + +} + + diff --git a/lib-db/src/main/java/com/fourelementscapital/db/ConstructQueryDB.java b/lib-db/src/main/java/com/fourelementscapital/db/ConstructQueryDB.java new file mode 100755 index 0000000..34f3eb1 --- /dev/null +++ b/lib-db/src/main/java/com/fourelementscapital/db/ConstructQueryDB.java @@ -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); + +} + + diff --git a/lib-db/src/main/java/com/fourelementscapital/db/ContractDB.java b/lib-db/src/main/java/com/fourelementscapital/db/ContractDB.java new file mode 100755 index 0000000..bbd6243 --- /dev/null +++ b/lib-db/src/main/java/com/fourelementscapital/db/ContractDB.java @@ -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); + +} + + diff --git a/lib-db/src/main/java/com/fourelementscapital/db/DBManager.java b/lib-db/src/main/java/com/fourelementscapital/db/DBManager.java new file mode 100755 index 0000000..adcc361 --- /dev/null +++ b/lib-db/src/main/java/com/fourelementscapital/db/DBManager.java @@ -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. new ArrayList(); + * be passed to select all fields (*) + * @param queryParams A hashmap containing the selection filters in the format [column_name]=[value] + * @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 selectedFields, Map 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 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 [column_name]=[value] + * @param selectionFieldValues A list of the values to be selected through the queries. new ArrayList(); + * be passed to select all fields (*) + * @param newFieldsValues + * @throws SQLException + */ + public void updateDatabase(String tableName,Map selectionFieldValues, Map newFieldsValues) throws SQLException + { + String queryBuilder = "UPDATE "+tableName+" SET "; + + if(newFieldsValues.size()>0) + { + ArrayList al = new ArrayList(); + for (Map.Entry 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 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 fieldValues,Map nonStringfieldValues) throws SQLException + { + String queryBuilder = "INSERT INTO "+tableName; + if(nonStringfieldValues==null) nonStringfieldValues = new HashMap(); + if(fieldValues.size()>0) + { + queryBuilder += " ("+Utils.Join(new ArrayList(fieldValues.keySet()), ","); + if(!nonStringfieldValues.isEmpty()) + queryBuilder +=","+Utils.Join(new ArrayList(nonStringfieldValues.keySet()), ","); + queryBuilder+= " ) VALUES "; + ArrayList ll = new ArrayList(); + for (Iterator i = fieldValues.values().iterator(); i.hasNext();) { + ll.add(i.next().toString()); + + } + ArrayList ll2 = new ArrayList(); + 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); + + } +} + + diff --git a/lib-db/src/main/java/com/fourelementscapital/db/DBManagerDB.java b/lib-db/src/main/java/com/fourelementscapital/db/DBManagerDB.java new file mode 100755 index 0000000..38e9ddb --- /dev/null +++ b/lib-db/src/main/java/com/fourelementscapital/db/DBManagerDB.java @@ -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; +} + + + \ No newline at end of file diff --git a/lib-db/src/main/java/com/fourelementscapital/db/FlexiFieldDB.java b/lib-db/src/main/java/com/fourelementscapital/db/FlexiFieldDB.java new file mode 100755 index 0000000..ee1e610 --- /dev/null +++ b/lib-db/src/main/java/com/fourelementscapital/db/FlexiFieldDB.java @@ -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 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 ; + +} + + diff --git a/lib-db/src/main/java/com/fourelementscapital/db/GeneralUtilDB.java b/lib-db/src/main/java/com/fourelementscapital/db/GeneralUtilDB.java new file mode 100755 index 0000000..d4455a8 --- /dev/null +++ b/lib-db/src/main/java/com/fourelementscapital/db/GeneralUtilDB.java @@ -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 resultsetToMap(ResultSet rs) throws Exception { + ResultSetMetaData metaData = rs.getMetaData(); + int colCount = metaData.getColumnCount(); + Map row = new HashMap(); + 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; + } + +} + + + \ No newline at end of file diff --git a/lib-db/src/main/java/com/fourelementscapital/db/IExecDB.java b/lib-db/src/main/java/com/fourelementscapital/db/IExecDB.java new file mode 100755 index 0000000..7406f0d --- /dev/null +++ b/lib-db/src/main/java/com/fourelementscapital/db/IExecDB.java @@ -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 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 getCommodityTree() throws Exception; + +} + + diff --git a/lib-db/src/main/java/com/fourelementscapital/db/InfrastructureDB.java b/lib-db/src/main/java/com/fourelementscapital/db/InfrastructureDB.java new file mode 100755 index 0000000..c846916 --- /dev/null +++ b/lib-db/src/main/java/com/fourelementscapital/db/InfrastructureDB.java @@ -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 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; + +} + + + \ No newline at end of file diff --git a/lib-db/src/main/java/com/fourelementscapital/db/RFunctionDB.java b/lib-db/src/main/java/com/fourelementscapital/db/RFunctionDB.java new file mode 100755 index 0000000..faf7bff --- /dev/null +++ b/lib-db/src/main/java/com/fourelementscapital/db/RFunctionDB.java @@ -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 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 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, ArrayListplist) 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 ; +} + + + \ No newline at end of file diff --git a/lib-db/src/main/java/com/fourelementscapital/db/ReferenceDB.java b/lib-db/src/main/java/com/fourelementscapital/db/ReferenceDB.java new file mode 100755 index 0000000..b78d023 --- /dev/null +++ b/lib-db/src/main/java/com/fourelementscapital/db/ReferenceDB.java @@ -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; + +} + + diff --git a/lib-db/src/main/java/com/fourelementscapital/db/SchedulerDB.java b/lib-db/src/main/java/com/fourelementscapital/db/SchedulerDB.java new file mode 100755 index 0000000..6a88054 --- /dev/null +++ b/lib-db/src/main/java/com/fourelementscapital/db/SchedulerDB.java @@ -0,0 +1,1056 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.db; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.Vector; + +import com.fourelementscapital.db.vo.SchedulerTrigger; + +/** + * Abstract class implements methods related to scheduler + */ +public abstract class SchedulerDB extends AbstractTeamOrgDB { + + public static int REMOVE_BEFORE_UPDATE=1; + public static int DONT_REMOVE_BEFORE_UPDATE=2; + + public static final int PEER_ASSOCIATION_HISTORY_ADDED=1; + public static final int PEER_ASSOCIATION_HISTORY_REMOVED=0; + public static final int PEER_ASSOCIATION_HISTORY_PEER_ACTIVE=1; + public static final int PEER_ASSOCIATION_HISTORY_PEER_NOACTIVE=0; + + /** + * Connect database + */ + public void connectDB() throws Exception { + super.connectDB(DB_NAME_BBSYNC); + } + + /** + * Get tag table + * @return "tags " + */ + public String getTagTable() { return "tags "; } + + /** + * Get tag item table + * @return "scheduler_tags" + */ + public String getTagItemTable() { return "scheduler_tags"; } + + /** + * Get tag item id + * @return "scheduler_id" + */ + public String getTagItemId() { return "scheduler_id";} + + /** + * Get tag item follow table + * @return "scheduler_followtags" + */ + public String getTagItemFollowTable() { return "scheduler_followtags";} + + /** + * List scheduler + * @return schedulers + * @throws Exception + */ + public abstract List listScheduler() throws Exception; + + /** + * List scheduler join folder by task UID + * @return scheduler taskUID join + * @throws Exception + */ + public abstract List listSchedulerTaskUIDJoin() throws Exception; + + /** + * List scheduler by theme tags + * @param themetags theme tags + * @return scheduler + * @throws Exception + */ + public abstract List listScheduler(List themetags) throws Exception; + + /** + * List trashed scheduler + * @return scheduler + * @throws Exception + */ + public abstract List listTrashedScheduler() throws Exception; + + /** + * Search scheduler by keyword + * @param ids ids + * @return scheduler + * @throws Exception + */ + public abstract List searchScheduler(String ids) throws Exception; + + /** + * Check a scheduler whether it's deleted + * @param id id + * @return true if it's deleted + * @throws Exception + */ + public abstract boolean isDeleted(int id) throws Exception; + + /** + * List all tasks by UID + * @param taskuid taskuid + * @return scheduler + * @throws Exception + */ + public abstract List listAllTasksByUID(String taskuid) throws Exception; + + /** + * List scheduler item by id + * @param scheduler_id scheduler id + * @return scheduler + * @throws Exception + */ + public abstract Map listSchedulerItem(long scheduler_id) throws Exception ; + + /** + * Search scheduler by keyword & tag id + * @param keyword keyword + * @param tagid tag id + * @return schedulers + * @throws Exception + */ + public abstract List searchScheduler(String keyword, int tagid) throws Exception ; + + /** + * List of folders by taskuid + * @param taskuid taskuid + * @return folders + * @throws Exception + */ + public abstract List listOfFolders(String taskuid) throws Exception; + + /** + * List of folders by theme tags + * @param themetags theme tags + * @return folders + * @throws Exception + */ + public abstract List listofMyFolders(List themetags) throws Exception ; + + /** + * Get taskuid from folder by folder id + * @param folder_id folder id + * @return taskuid + * @throws Exception + */ + public abstract String getFolderTaskUID(int folder_id) throws Exception ; + + /** + * Update task folder + * @param scheduler_id scheduler id + * @param folder_id folder id + * @throws Exception + */ + public abstract void updateTaskFolder(int scheduler_id,int folder_id) throws Exception; + + /** + * Fix broken folders + * @throws Exception + */ + public abstract void fixSchBrokenFolders() throws Exception; + + /** + * Move item to root or group + * @param scheduler_id scheduler id + * @param taskuid taskuid + * @param folder_id folder id + * @throws Exception + */ + public abstract void moveItemToRootOrGroup(int scheduler_id, String taskuid, int folder_id) throws Exception; + + /** + * Rename folder + * @param taskuid task uid + * @param oldname old name + * @param newname new name + * @throws Exception + */ + public abstract void renameFolder(String taskuid, String oldname, String newname) throws Exception; + + /** + * Delete folder + * @param folderid folder id + * @throws Exception + */ + public abstract void deleteFolder(int folderid) throws Exception; + + /** + * Add folder + * @param taskuid taskuid + * @param foldername folder name + * @return generated folder id + * @throws Exception + */ + public abstract long addFolder(String taskuid,String foldername) throws Exception; + + /** + * Delete scheduler (set deleted flag) + * @param scheduler_id scheduler id + * @throws Exception + */ + public abstract void deleteScheduler(int scheduler_id) throws Exception; + + /** + * Put back scheduler (set deleted flag to NULL) + * @param scheduler_id scheduler id + * @throws Exception + */ + public abstract void putBackScheduler(int scheduler_id) throws Exception ; + + /** + * Get scheduler by id + * @param scheduler_id scheduler id + * @return schedulers + * @throws Exception + */ + public abstract Map getScheduler(int scheduler_id) throws Exception; + + /** + * Add scheduler log + * @param scheduler_id scheduler id + * @param start start date + * @param end end date + * @param timezone timezone + * @param status status + * @param messages messages + * @return generated scheduler log id + * @throws Exception + */ + public abstract int addSchedulerLog(int scheduler_id, Date start, Date end,String timezone, String status,String messages) throws Exception; + + /** + * Update scheduler log message + * @param log_id log id + * @param messages messages + * @throws Exception + */ + public abstract void updateSchedulerLogMsg(int log_id,String messages) throws Exception; + + /** + * List of queue logs by time + * @param sids_trids scheduler id & trigger time + * @param now now time + * @return scheduler queue logs + * @throws Exception + */ + public abstract List listOfQueueLogs(String sids_trids, long now) throws Exception; + + /** + * List of history queue logs by time & date query + * @param now now time + * @param datequery date query + * @return queue logs + * @throws Exception + */ + public abstract List listOfHistoryQueueLogs(long now, String datequery) throws Exception; + + /** + * List of last 15 queue logs by time & scheduler id + * @param now now time + * @param scheduler_id scheduler id + * @return scheduler last 15 queue logs + * @throws Exception + */ + public abstract List listOfLast15Logs(long now, int scheduler_id) throws Exception; + + /** + * Remove queue logs by time & scheduler id + * @param currentTime current time + * @param scheduler_id scheduler id + * @throws Exception + */ + public abstract void removeQueueLog(long currentTime, int scheduler_id) throws Exception; + + /** + * Update queue log status by id + * @param id queue logs id + * @param status status + * @throws Exception + */ + public abstract void updateQueueLogStatus(int id,String status, String computerName) throws Exception; + + /** + * Get host name + * @return host name + * @throws Exception + */ + public abstract String getHostName() throws Exception; + + /** + * Update queue log data + * @param data data + * @param connection_ids connection ids + * @throws Exception + */ + public abstract void updateQueueLog(Collection data, Vector connection_ids, String computerName) throws Exception; + + /** + * Update queue log executed code + * @param scheduler_id scheduler id + * @param trigger_time trigger time + * @param code code + * @throws Exception + */ + public abstract void updateExecutedCodeQLog(int scheduler_id, long trigger_time, String code ) throws Exception; + + /** + * Get log messages + * @param log_id log id + * @return log messages + * @throws Exception + */ + public abstract String getLogMessages(int log_id) throws Exception; + + /** + * Toggle active + * @param scheduler_id scheduler id + * @return is active + * @throws Exception + */ + public abstract int toggleActive(int scheduler_id) throws Exception; + + /** + * Get id of add or update scheduler + * @param scheduler_id scheduler id + * @param data data + * @param taskUID taskUID + * @throws Exception + */ + public abstract Integer addOrUpdateSchedulerGetId(int scheduler_id,Map data, String taskUID) throws Exception; + + /** + * Get R script of add or update scheduler + * @param thisid scheduler id + * @param data data + * @param shortname short name + * @throws Exception + */ + public abstract void addOrUpdateSchedulerInsertTaskdata(Integer thisid,Map data, String shortname) throws Exception; + + /** + * Update queue log server start & end + * @param scheduler_id scheduler id + * @param trigger_time trigger time + * @param start date start + * @param end date end + * @throws Exception + */ + public abstract void updateServerStartEnd(int scheduler_id,long trigger_time,Date start, Date end) throws Exception; + + /** + * List dependency + * @param ids scheduler ids + * @param d date + * @return queue logs + * @throws Exception + */ + public abstract List listDependencyList(String ids, Date d) throws Exception; + + /** + * Get peer list + * @return peername + * @throws Exception + */ + public abstract Vector getPeersList() throws Exception ; + + /** + * Update peers notes + * @param peername peer name + * @param notes notes + * @param friendlyname friendly name + * @param cmail contact mail + * @throws Exception + */ + public abstract void updatePeersNotes(String peername,String notes,String friendlyname, String cmail) throws Exception; + + /** + * Update peers last online + * @param peername peer name + * @param time time + * @throws Exception + */ + public abstract void updatePeersLastOnline(String peername,long time) throws Exception; + + /** + * Update peers active by peername + * @param peername peer name + * @param active is active + * @param user user + * @throws Exception + */ + public abstract void updatePeersToggle(String peername,int active, String user) throws Exception; + + /** + * Get peers data + * @return peers data + * @throws Exception + */ + public abstract Vector getPeersData() throws Exception ; + + /** + * Update peers list peername + * @param peername peer name + * @throws Exception + */ + public abstract void updatePeersList(String peername) throws Exception; + + /** + * Remove queue logs by time & task types + * @param currentTime current time + * @param tasktypes task types + * @throws Exception + */ + public abstract void removeQueueLogs(long currentTime, String tasktypes) throws Exception; + + /** + * Get queue log + * @param scheduler_id scheduler id + * @param trigger_time trigger time + * @return scheduler queue logs + * @throws Exception + */ + public abstract Map getQueueLog(int scheduler_id, long trigger_time) throws Exception; + + /** + * Get associated peers + * @param taskuid taskuid + * @return peer name + * @throws Exception + */ + public abstract Vector getAssociatedPeers(String taskuid) throws Exception; + + /** + * Get associated available peers + * @param taskuid taskuid + * @return peer name + * @throws Exception + */ + public abstract Vector getAssoAvailablePeers(String taskuid) throws Exception; + + /** + * Get peer friendly name + * @param peername peer name + * @return friendly name + * @throws Exception + */ + public abstract String getPeerFriendlyName(String peername) throws Exception ; + + /** + * Get all peer association + * @return task peers + * @throws Exception + */ + public abstract Vector getAllPeerAssociation() throws Exception ; + + /** + * Get last successful queue log by multi scheduler ids + * @param scheduler_ids scheduler ids + * @return queue log + * @throws Exception + */ + public abstract Vector getLastSuccessfulQLogs(Vector scheduler_ids) throws Exception; + + /** + * Get last successful queue log + * @param scheduler_id scheduler id + * @return queue log + * @throws Exception + */ + public abstract Map getLastSuccessfulQLog(int scheduler_id) throws Exception; + + /** + * List all tasks by folder id + * @param folder_id folder id + * @return schedulers + * @throws Exception + */ + public abstract List folderTasks(int folder_id) throws Exception; + + /** + * Move tasks + * @param d_tuid new task uid + * @param d_fid new folder id + * @param s_tuid original task uid + * @param s_fid original folder id + * @throws Exception + */ + public abstract void moveTasks(String d_tuid, int d_fid, String s_tuid, int s_fid) throws Exception; + + /** + * Add R script + * @param name name + * @param script script + * @param restart restart + * @return generated id + * @throws Exception + */ + public abstract int addRScript(String name,String script,int restart) throws Exception; + + /** + * Get R script + * @param script_id script id + * @return R script + * @throws Exception + */ + public abstract Map getRScript(int script_id) throws Exception; + + /** + * Add R script log + * @param script_id script id + * @param peer peer + * @param status status + * @param start_time start time + * @param end_time end time + * @param message message + * @throws Exception + */ + public abstract void addRScriptLog(int script_id,String peer, String status, Date start_time, Date end_time, String message) throws Exception; + + /** + * Get last 5 R script logs + * @return r script logs + * @throws Exception + */ + public abstract Vector rScriptLast5Logs() throws Exception; + + /** + * List of queue logs by scheduler id & trigger time + * @param scheduler_id scheduler id + * @param trigger_time trigger time + * @return scheduler queue logs + * @throws Exception + */ + public abstract Map listOfQueueLog(int scheduler_id, long trigger_time ) throws Exception; + + /** + * Update queue log host & start + * @param scheduler_id scheduler id + * @param trigger_time trigger time + * @param start date start + * @param host host + * @throws Exception + */ + public abstract void updateHostAndStarted(int scheduler_id,long trigger_time,Date start, String host) throws Exception; + + /** + * Move items + * @param foldername folder name + * @param taskuid taskuid + * @param parentfolder parent folder + * @param targetuid target uid + * @param targetpath target path + * @return scheduler id + * @throws Exception + */ + public abstract ArrayList moveItems(String foldername, String taskuid, String parentfolder, String targetuid, String targetpath) throws Exception; + + /** + * Update calling another script field + * @param scheduler_id scheduler id + * @param calling_another_script is calling another script + * @throws Exception + */ + public abstract void updateCallingAnotherScript(int scheduler_id, int calling_another_script /* either 1 or 0 */) throws Exception; + + /** + * Add peer taskuid + * @param taskuid task uid + * @param peer peer + * @param user user + * @throws Exception + */ + public abstract void addPeerTaskuid(String taskuid, String peer, String user) throws Exception; + + /** + * Delete peer taskuid + * @param taskuid task uid + * @param peer peer + * @param user user + * @throws Exception + */ + public abstract void deletePeerTaskuid(String taskuid, String peer, String user) throws Exception; + + /** + * Set group order + * @param taskuids task uids + * @throws Exception + */ + public abstract void setGroupOrder( Vector taskuids) throws Exception; + + /** + * Get group order + * @return task uid + * @throws Exception + */ + public abstract Vector getGroupOrder( ) throws Exception; + + /** + * Get all groups + * @return groups + * @throws Exception + */ + public abstract Vector getAllGroups() throws Exception ; + + /** + * Get active groups + * @return groups + * @throws Exception + */ + public abstract Vector getActiveGroups() throws Exception ; + + /** + * Get groups by engine type + * @param enginetype engine type + * @return groups + * @throws Exception + */ + public abstract Vector getGroups(String enginetype) throws Exception ; + + /** + * Add scheduler execution logs + * @param scheduler_id scheduler id + * @param trigger_time trigger time + * @param date date + * @param messages messages + * @param repCode rep code + * @throws Exception + */ + public abstract void addSchedulerExeLogs(int scheduler_id, long trigger_time, Date date,String messages,int repCode, String computerName) throws Exception; + + /** + * Get execution logs + * @param scheduler_id scheduler id + * @param trigger_time trigger time + * @return execution logs + * @throws Exception + */ + public abstract Vector getSchedulerExeLogs(int scheduler_id, long trigger_time ) throws Exception ; + + /** + * Update queue log status where status is null or '' + * @param scheduler_id scheduler id + * @param trigger_time trigger time + * @param status status + * @throws Exception + */ + public abstract void updateQueueNullStatus(int scheduler_id, long trigger_time,String status) throws Exception; + + /** + * Get trigger data by scheduler id + * @param scheduler_id scheduler id + * @return triggers + * @throws Exception + */ + public abstract Vector getTriggerData(int scheduler_id) throws Exception; + + /** + * Set trigger data + * @param scheduler_id scheduler id + * @param arry triggers + * @throws Exception + */ + public abstract void setTriggerData(int scheduler_id, SchedulerTrigger arry[]) throws Exception; + + /** + * Get trigger data by id + * @param row_id id + * @return triggers + * @throws Exception + */ + public abstract Map getOneRowTriggerData(long row_id) throws Exception; + + /** + * Add peer thread status + * @param peername peer name + * @param queuename queuename + * @param taskuid taskuid + * @param allowedthread allowed thread + * @param scheduler_id scheduler id + * @throws Exception + */ + public abstract void addPeerThreadStatus(String peername,String queuename, String taskuid, int allowedthread, int scheduler_id) throws Exception ; + + /** + * Remove peer thread status + * @param peername peer name + * @param scheduler_id scheduler id + * @throws Exception + */ + public abstract void removePeerThreadStatus(String peername, int scheduler_id) throws Exception ; + + /** + * Remove all peer thread status + * @param peername peer name + * @throws Exception + */ + public abstract void removeAllPeerThreadStatus(String peername) throws Exception ; + + /** + * Get available peers + * @param peers peers + * @param taskuid taskuid + * @return peer names + * @throws Exception + */ + public abstract Vector getAvailablePeers(String peers /* "'rhino'" */, String taskuid /* "'peer1','peer2'" */) throws Exception; + + /** + * Get max duration in last 50 execution + * @param scheduler_id scheduler id + * @return max duration + * @throws Exception + */ + public abstract long getMaxDurationInLast50Exec(int scheduler_id) throws Exception; + + + //public abstract int get20RecentMaxDuration(int scheduler_id, long trigger_time) throws Exception; + + /** + * Get timeout criteria in ms + * @param scheduler_id scheduler id + * @return timeout criteria in ms + * @throws Exception + */ + public abstract long getTimeoutCriteriaInMs(int scheduler_id) throws Exception; + + /** + * Check whether name is exist + * @param name name + * @param scheduler_id scheduler id + * @return true if name is exist + * @throws Exception + */ + public abstract boolean isNameExisting(String name,int scheduler_id) throws Exception; + + /** + * Get DB log summary + * @param con_ids connection ids + * @return DB log summary + * @throws Exception + */ + public abstract Map getDBLogSummary(String con_ids) throws Exception ; + + //public abstract Map getDBLogSummary(int scheduler_id,long trigger_time) throws Exception ; + + /** + * Get data log history + * @param scheduler_id scheduler id + * @param trigger_time trigger time + * @return data log history + * @throws Exception + */ + public abstract Vector getDataLogHistory(int scheduler_id, long trigger_time) throws Exception; + + /** + * Update alert type + * @param alert_type alert type + * @param scheduler_id scheduler id + * @return true + * @throws Exception + */ + public abstract boolean updateAlertType(String alert_type,int scheduler_id) throws Exception ; + + //public abstract Vector getThemeTags(int scheduler_id) throws Exception; + + /** + * Get error message even null + * @param scheduler_id scheduler id + * @param trig_time trigger time + * @return error message + * @throws Exception + */ + public abstract String getErrorMessageEvenNull(int scheduler_id,long trig_time) throws Exception; + + /** + * Update last 2 users tag + * @param scheduler_id scheduler id + * @param user_tagid user tag id + * @throws Exception + */ + public abstract void updateLast2UsersTag(int scheduler_id, int user_tagid) throws Exception; + + /** + * Add if tag not exist + * @param tagname tag name + * @return generated tag id + * @throws Exception + */ + public abstract int addIfTagNotExist(String tagname) throws Exception; + + /** + * Get task event actions + * @param scheduler_id scheduler id + * @param trig_time trigger time + * @return task event actions + * @throws Exception + */ + public abstract Map getTaskEventActions(int scheduler_id,long trig_time) throws Exception; + + //public abstract void pushAlarmMessage(String message) throws Exception; + + /** + * Push alarm message + * @param db_name_trading_ref database name trading ref + * @param message message + * @throws Exception + */ + public abstract void pushAlarmMessage(String db_name_trading_ref, String message) throws Exception; + + /** + * Get scheduler dependency + * @param scheduler_id scheduler id + * @return scheduler ids + * @throws Exception + */ + public abstract List getDependsThis(int scheduler_id) throws Exception; + + /** + * Get scheduler depend to + * @param scheduler_id scheduler id + * @return scheduler ids + * @throws Exception + */ + public abstract List getDependsTo(int scheduler_id) throws Exception ; + + /** + * Purge scheduler + * @param scheduler_id scheduler id + * @throws Exception + */ + public abstract void purgeScheduler(int scheduler_id) throws Exception; + + /** + * Add edit logs + * @param scheduler_id scheduler id + * @param user user + * @param message message + * @throws Exception + */ + public abstract void addEditLogs(int scheduler_id, String user,String message) throws Exception; + + /** + * Get edit logs + * @param scheduler_id scheduler id + * @return edit logs + * @throws Exception + */ + public abstract List getEditLogs(int scheduler_id) throws Exception; + + /** + * Get inject code from queue log + * @param sid_triggertime scheduler id & trigger time + * @return inject code + * @throws Exception + */ + public abstract String getInjectCode4QLog(String sid_triggertime) throws Exception; + + /** + * Update timeout settings + * @param data timeout setting data + * @throws Exception + */ + public abstract void updateTimeoutSettings(Map data) throws Exception; + + /** + * Get timeout settings + * @return timeout settings + * @throws Exception + */ + public abstract Map getTimeoutSettings() throws Exception ; + + /** + * Get last 10 minutes suspected failure + * @return suspected failure + * @throws Exception + */ + public abstract ArrayList getLast10minuteSuspectedFailure() throws Exception; + + /** + * Update queue log response code + * @param scheduler_id scheduler id + * @param trigger_time trigger time + * @param code response code + * @throws Exception + */ + public abstract void updateResponseCode(int scheduler_id, long trigger_time,int code) throws Exception; + + /** + * Get peers list by engine type + * @param enginetypes engine types + * @return peers list + * @throws Exception + */ + public abstract ArrayList getPeersList4Engine(List enginetypes) throws Exception; + + /** + * Get console message + * @param scheduler_id scheduler id + * @param trigger_time trigger time + * @return console message + * @throws Exception + */ + public abstract String getConsoleMsg(int scheduler_id, long trigger_time) throws Exception; + + /* + *Get console message from 2nd Scheduler + * @param scheduler_id scheduler id + * @return console message + * @throws Exception + */ + public abstract String getConsoleMsgFrom2ndScheduler(int scheduler_id) throws Exception; + + /** + * Get single col data + * @param query + * @return data + * @throws Exception + */ + public abstract List getSingleColData(String query) throws Exception; + + /** + * User pref save or update + * @param login login + * @param ky key + * @param val value + * @throws Exception + */ + public abstract void userPrefSaveOrUpdate(String login, String ky, String val) throws Exception ; + + /** + * Get user preference by login + * @param login login + * @return user preferences + * @throws Exception + */ + public abstract Map userPrefGetAll(String login) throws Exception; + + /** + * Get auto complete XHR + * @param keyword keyword + * @return auto complete XHR + * @throws Exception + */ + public abstract List getAutoCompleteXHR(String keyword) throws Exception; + + /** + * Get owner theme + * @param scheduler_id scheduler id + * @return tag name + * @throws Exception + */ + public abstract String getOwnerTheme(int scheduler_id) throws Exception; + + /** + * Update is wiki done + * @param scheduler_id scheduler id + * @param done is wiki done + * @throws Exception + */ + public abstract void updateWikiDone(int scheduler_id, int done) throws Exception; + + /** + * Get last 2 days failed by user's themes + * @param themes user's themes + * @return queue logs + * @throws Exception + */ + public abstract List getFailedLast2Days(List themes) throws Exception ; + + /** + * Get last 50 peer action history + * @param peername peer name + * @return action history + * @throws Exception + */ + public abstract List getLast50PeerActHistory(String peername) throws Exception ; + + /** + * Get last 50 task action history + * @param taskuid task uid + * @return action history + * @throws Exception + */ + public abstract List getLast50TaskActHistory(String taskuid) throws Exception ; + + /** + * Update queue log status by scheduler id & trigger time + * @param scheduler_id scheduler id + * @param trigger_time trigger time + * @param status status + * @throws Exception + */ + public abstract void updateQueueLogStatus(int scheduler_id, long trigger_time,String status, String computerName) throws Exception ; + + /** + * Check whether execution log is exist by repcode + * @param scheduler_id scheduler id + * @param trigger_time trigger time + * @param repcode rep code + * @return true if exist + * @throws Exception + */ + public abstract boolean execLogsRepcodeExist(int scheduler_id, long trigger_time, int repcode) throws Exception ; + + /** + * Is there any execution logs in last 3 minutes + * @param scheduler_id scheduler id + * @param trigger_time trigger time + * @return true if execution logs found + * @throws Exception + */ + public abstract boolean isAnyExecLogsInLast3Mins(int scheduler_id, long trigger_time) throws Exception; + + /** + * Get query of top 20 id & name by name + * @param keyword keyword + * @return query + * @throws Exception + */ + public abstract String getTop20IdNameByNameQuery(String keyword) throws Exception; + + /** + * Get query of top 20 id & name by id + * @param keyword keyword + * @return query + * @throws Exception + */ + public abstract String getTop20IdNameByIdQuery(String keyword) throws Exception; + + /** + * Get query of history queue logs date + * @param scheduelr_id scheduler id + * @param stat status + * @return query + */ + public abstract String getHistoryQueueLogsDateQuery(long scheduelr_id, String stat) throws Exception; + + /** + * Get completed signal from scheduler_exeplanlogs table + * @param scheduler_id scheduler id + * @return query + * @throws Exception + */ + public abstract String checkCompletedSignalFromPeer(int scheduler_id) throws Exception; + +} + + diff --git a/lib-db/src/main/java/com/fourelementscapital/db/SuperDB.java b/lib-db/src/main/java/com/fourelementscapital/db/SuperDB.java new file mode 100755 index 0000000..97c39ce --- /dev/null +++ b/lib-db/src/main/java/com/fourelementscapital/db/SuperDB.java @@ -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 connPools=new ConcurrentHashMap(); + + private static Vector connections=new Vector(); + 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 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 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 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 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 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 listAllTables4Commodities(Connection con, String fieldtype,Vector 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 listAllTables4Commodities2(Connection con, String fieldtype,Vector commodities) throws Exception; + + /** + * List all contract titles + * @param con connection + * @param jointables join tables + * @return contract titles + * @throws Exception + */ + public abstract TreeMap 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 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 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 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 olddata,Map 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 conids) throws Exception ; + + +} + + \ No newline at end of file diff --git a/lib-db/src/main/java/com/fourelementscapital/db/Utils.java b/lib-db/src/main/java/com/fourelementscapital/db/Utils.java new file mode 100755 index 0000000..21847f5 --- /dev/null +++ b/lib-db/src/main/java/com/fourelementscapital/db/Utils.java @@ -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 _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 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(); + 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; + } + +} + + + diff --git a/lib-db/src/main/java/com/fourelementscapital/db/mariadb/BBSyncDBMariaDB.java b/lib-db/src/main/java/com/fourelementscapital/db/mariadb/BBSyncDBMariaDB.java new file mode 100755 index 0000000..b4cddbf --- /dev/null +++ b/lib-db/src/main/java/com/fourelementscapital/db/mariadb/BBSyncDBMariaDB.java @@ -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 contract,String marketsector,Timestamp lastsync, Map 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 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 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 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 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 v=new Vector(); + while(rs.next()){ + v.add(rs.getString("name")); + } + rs.close(); + st.close(); + return v; + } + + + /** + * {@inheritDoc} + */ + public Map 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 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 getSecurityName2RefSync(String ticker) throws Exception { + Statement st=this.connection().createStatement(); + ResultSet rs=null; + rs=st.executeQuery("select * from security_info where name='"+ticker+"'"); + + LinkedHashMap 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 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 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 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 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;a0){ + 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"; + } + +} diff --git a/lib-db/src/main/java/com/fourelementscapital/db/mariadb/ConstructQueryDBMariaDB.java b/lib-db/src/main/java/com/fourelementscapital/db/mariadb/ConstructQueryDBMariaDB.java new file mode 100755 index 0000000..26fb202 --- /dev/null +++ b/lib-db/src/main/java/com/fourelementscapital/db/mariadb/ConstructQueryDBMariaDB.java @@ -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; + } + +} diff --git a/lib-db/src/main/java/com/fourelementscapital/db/mariadb/ContractDBMariaDB.java b/lib-db/src/main/java/com/fourelementscapital/db/mariadb/ContractDBMariaDB.java new file mode 100755 index 0000000..21cc70e --- /dev/null +++ b/lib-db/src/main/java/com/fourelementscapital/db/mariadb/ContractDBMariaDB.java @@ -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 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 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 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 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 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; + + } + +} diff --git a/lib-db/src/main/java/com/fourelementscapital/db/mariadb/DBManagerDBMariaDB.java b/lib-db/src/main/java/com/fourelementscapital/db/mariadb/DBManagerDBMariaDB.java new file mode 100755 index 0000000..bc5b155 --- /dev/null +++ b/lib-db/src/main/java/com/fourelementscapital/db/mariadb/DBManagerDBMariaDB.java @@ -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; + } + +} diff --git a/lib-db/src/main/java/com/fourelementscapital/db/mariadb/FlexiFieldDBMariaDB.java b/lib-db/src/main/java/com/fourelementscapital/db/mariadb/FlexiFieldDBMariaDB.java new file mode 100755 index 0000000..810cc30 --- /dev/null +++ b/lib-db/src/main/java/com/fourelementscapital/db/mariadb/FlexiFieldDBMariaDB.java @@ -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 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 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(); + } +} diff --git a/lib-db/src/main/java/com/fourelementscapital/db/mariadb/IExecDBMariaDB.java b/lib-db/src/main/java/com/fourelementscapital/db/mariadb/IExecDBMariaDB.java new file mode 100755 index 0000000..dcb683c --- /dev/null +++ b/lib-db/src/main/java/com/fourelementscapital/db/mariadb/IExecDBMariaDB.java @@ -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 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 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 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 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; + + } +} diff --git a/lib-db/src/main/java/com/fourelementscapital/db/mariadb/InfrastructureDBMariaDB.java b/lib-db/src/main/java/com/fourelementscapital/db/mariadb/InfrastructureDBMariaDB.java new file mode 100755 index 0000000..461a99c --- /dev/null +++ b/lib-db/src/main/java/com/fourelementscapital/db/mariadb/InfrastructureDBMariaDB.java @@ -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 columns=new ArrayList(); + for(int i=0;i 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 columns=new ArrayList(); + for(int i=0;i 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 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; + } + +} diff --git a/lib-db/src/main/java/com/fourelementscapital/db/mariadb/RFunctionDBMariaDB.java b/lib-db/src/main/java/com/fourelementscapital/db/mariadb/RFunctionDBMariaDB.java new file mode 100755 index 0000000..4e6917e --- /dev/null +++ b/lib-db/src/main/java/com/fourelementscapital/db/mariadb/RFunctionDBMariaDB.java @@ -0,0 +1,1125 @@ +/****************************************************************************** +* +* 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.sql.Timestamp; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Vector; + +import org.apache.commons.dbutils.BasicRowProcessor; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.LogManager; + +import com.fourelementscapital.db.RFunctionDB; +import com.fourelementscapital.db.SchedulerDB; +import com.fourelementscapital.db.vo.PeerPackage; + + +/** + * RFunction MariaDB DAO implementation + */ +public class RFunctionDBMariaDB extends RFunctionDB { + + + private Logger log=LogManager.getLogger(RFunctionDBMariaDB.class.getName()); + + + /** + * {@inheritDoc} + */ + public String getFolderName(int folder_id) throws Exception { + PreparedStatement ps=this.connection().prepareStatement("Select folder_name FROM 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 updateWikiDone(int function_id, int done) throws Exception { + String query="UPDATE r_function set is_wiki_done=? where id=?"; + PreparedStatement ps=this.connection().prepareStatement(query); + ps.setInt(1, done); + ps.setInt(2, function_id); + ps.executeUpdate(); + ps.close(); + } + + + /** + * {@inheritDoc} + */ + public void updateFunctionOrder(int[] function_ids ) throws Exception { + String query="UPDATE r_function set disp_order=? where id=?"; + PreparedStatement ps=this.connection().prepareStatement(query); + for(int i=0;i1){ + theme=keyword.split(":")[0]; + keyword=keyword.split(":")[1]; + } + + if(keyword.contains(":") && keyword.split(":").length==1){ + theme=keyword.split(":")[0]; + keyword=null; + + } + + StringBuffer sb = new StringBuffer(); + sb.append("select a.id,a.function_name,c.group_name,b.folder_name,a.last_modified, "); + sb.append(" (select CONCAT(GROUP_CONCAT(cast(tags.tagname as CHAR(8000)) order by tags.tagname SEPARATOR ', '), ', ') from function_tags as st left outer join tags on tags.id=st.tag_id WHERE st.function_id=a.id ) as stags "); + sb.append(" from r_function as a "); + sb.append(" left outer join folder as b on a.folder_id=b.id "); + sb.append(" left outer join function_group as c on b.group_uid = c.group_uid "); + sb.append(" where c.group_uid <> 'trash' "); + + if(keyword!=null){ + sb.append(" AND a.function_name like '%"+keyword+"%' "); + } + if(theme!=null){ + sb.append(" AND a.id in (select function_id from function_tags left outer join tags on function_tags.tag_id = tags.id where tags.tagname='thm-"+theme.trim()+"' ) " ); + } + sb.append(" order by c.disp_order, b.disp_order,a.disp_order "); + + PreparedStatement ps=this.connection().prepareStatement(sb.toString()); + ResultSet rs=ps.executeQuery(); + ArrayList rtn=new ArrayList(); + while(rs.next()){ + rtn.add(new BasicRowProcessor().toMap(rs)); + } + return rtn; + } + + + /** + * {@inheritDoc} + */ + public List listOfFunctions(int folder_id) throws Exception { + + PreparedStatement ps=this.connection().prepareStatement("Select * FROM r_function where folder_id=? order by disp_order"); + ps.setInt(1, folder_id); + ResultSet rs=ps.executeQuery(); + ArrayList rtn=new ArrayList(); + while(rs.next()){ + rtn.add(new BasicRowProcessor().toMap(rs)); + } + return rtn; + } + + + /** + * {@inheritDoc} + */ + public Map getRFunction(int function_id) throws Exception { + + StringBuffer sb = new StringBuffer(); + sb.append("SELECT a.*,b.folder_name,IFNULL(TIMESTAMPDIFF(day,date(a.deletedon),now()),7) as deleteddays,b.folder_name,c.tagname as owner_tag,(select CONCAT(GROUP_CONCAT(cast(tags.tagname as CHAR(8000)) order by tags.tagname SEPARATOR ', '), ', ') from function_tags as st left outer join tags on tags.id=st.tag_id WHERE st.function_id=a.id ) as stags FROM r_function as a "); + sb.append(" left outer join tags as c on c.id=a.owner_tag_id "); + sb.append(" left outer join folder as b on a.folder_id=b.id "); + sb.append(" WHERE a.id=? ORDER BY a.function_name "); + + PreparedStatement ps=this.connection().prepareStatement(sb.toString()); + + ps.setInt(1, function_id); + ResultSet rs=ps.executeQuery(); + + Map rtn=null; + if(rs.next()){ + rtn=new BasicRowProcessor().toMap(rs); + } + return rtn; + } + + + /** + * {@inheritDoc} + */ + public Map getRFunctionForScriptFile(String scriptfile) throws Exception { + + StringBuffer sb = new StringBuffer(); + sb.append("Select a.*,b.folder_name, "); + sb.append("(select CONCAT(GROUP_CONCAT(cast(tags.tagname as CHAR(8000)) order by tags.tagname SEPARATOR ', '), ', ') from function_tags as st left outer join tags on tags.id=st.tag_id WHERE st.function_id=a.id ) as stags "); + sb.append("FROM r_function as a "); + sb.append("left outer join folder as b on a.folder_id=b.id "); + sb.append("WHERE a.script_file=? "); + sb.append("ORDER BY a.function_name "); + PreparedStatement ps=this.connection().prepareStatement(sb.toString()); + ps.setString(1, scriptfile); + ResultSet rs=ps.executeQuery(); + Map rtn=null; + if(rs.next()){ + rtn=new BasicRowProcessor().toMap(rs); + } + return rtn; + } + + + /** + * {@inheritDoc} + */ + public Vector getRFunctions(Vector functionnames) throws Exception { + + //PreparedStatement ps=this.connection().prepareStatement("Select * FROM r_function WHERE id=?"); + + String sfunc="''"; + for(Iterator i=functionnames.iterator();i.hasNext();){ + sfunc+=",'"+i.next()+"'"; + } + + StringBuffer sb = new StringBuffer(); + sb.append("Select a.*, "); + sb.append("IFNULL(TIMESTAMPDIFF(day,date(a.deletedon),now()),7) as deleteddays, "); + sb.append("b.folder_name, "); + sb.append("(select CONCAT(GROUP_CONCAT(cast(tags.tagname as CHAR(8000)) order by tags.tagname SEPARATOR ', '), ', ') from function_tags as st left outer join tags on tags.id=st.tag_id WHERE st.function_id=a.id ) as stags "); + sb.append("FROM r_function as a left outer join folder as b on a.folder_id=b.id WHERE a.function_name IN ("+sfunc+") ORDER BY a.function_name "); + 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 List listFunctionGroups() throws Exception { + + PreparedStatement ps=this.connection().prepareStatement("Select * from function_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 Map getRFunction(String function_name) throws Exception { + + PreparedStatement ps=this.connection().prepareStatement("Select * FROM r_function WHERE function_name=?"); + ps.setString(1, function_name); + ResultSet rs=ps.executeQuery(); + Map rtn=null; + if(rs.next()){ + rtn=new BasicRowProcessor().toMap(rs); + } + return rtn; + } + + + /** + * {@inheritDoc} + */ + public int createFunction(int folder_id,String functionname, String path,int func_type) throws Exception { + PreparedStatement ps=this.connection().prepareStatement("INSERT INTO r_function(folder_id,function_name,script_file,last_modified,is_class) VALUES(?,?,?,?,?)",Statement.RETURN_GENERATED_KEYS); + ps.setInt(1,folder_id); + ps.setString(2, functionname); + ps.setString(3, path); + ps.setTimestamp(4,new Timestamp(new Date().getTime())); + ps.setInt(5, func_type); + ps.executeUpdate(); + int id=0; + ResultSet generatedKeys = ps.getGeneratedKeys(); + + if (generatedKeys.next()) { + id=generatedKeys.getInt(1); + } + ps.close(); + + return id; + } + + + /** + * {@inheritDoc} + */ + public void renameFunction(int function_id, String functionname, String script_file) throws Exception { + PreparedStatement ps=this.connection().prepareStatement("UPDATE r_function SET function_name=?,script_file=?,last_modified=? WHERE id=?"); + ps.setString(1, functionname); + ps.setString(2, script_file); + ps.setTimestamp(3,new Timestamp(new Date().getTime())); + ps.setInt(4,function_id); + + ps.executeUpdate(); + + } + + + /** + * {@inheritDoc} + */ + public void updatedOwnerIDNow(int owner_id,int function_id) throws Exception { + PreparedStatement ps=this.connection().prepareStatement("UPDATE r_function SET last_modified=?,owner_tag_id=? WHERE id=?"); + ps.setTimestamp(1,new Timestamp(new Date().getTime())); + ps.setInt(2,owner_id); + ps.setInt(3,function_id); + ps.executeUpdate(); + } + + + /** + * {@inheritDoc} + */ + public void deleteFunction(int function_id) throws Exception { + PreparedStatement ps=this.connection().prepareStatement("DELETE FROM r_function WHERE id=?"); + ps.setInt(1,function_id); + ps.executeUpdate(); + + PreparedStatement ps2=this.connection().prepareStatement("DELETE FROM function_tags WHERE function_id=?"); + ps2.setInt(1,function_id); + ps2.executeUpdate(); + } + + + /** + * {@inheritDoc} + */ + public void updateFunctionFolder(int function_id,int new_folder_id) throws Exception { + PreparedStatement ps=this.connection().prepareStatement("UPDATE r_function SET folder_id=? WHERE id=?"); + + ps.setInt(1,new_folder_id); + ps.setInt(2, function_id); + ps.executeUpdate(); + ps.close(); + + StringBuffer sb = new StringBuffer(); + sb.append("update r_function set disp_order = ( "); + sb.append("select max_disp_order from ( "); + sb.append("select max(disp_order)+1 as max_disp_order from r_function where folder_id = ? "); + sb.append(") as temp_table "); + sb.append(") where id = ? "); + + PreparedStatement ps1=this.connection().prepareStatement(sb.toString()); + ps1.setInt(1,new_folder_id); + ps1.setInt(2, function_id); + ps1.executeUpdate(); + ps1.close(); + + } + + + /** + * {@inheritDoc} + */ + public void updateLock(int function_id,String lockedby) throws Exception { + PreparedStatement ps=this.connection().prepareStatement("UPDATE r_function SET lockedby=? WHERE id=?"); + ps.setString(1,lockedby); + ps.setInt(2, function_id); + ps.executeUpdate(); + ps.close(); + } + + + /** + * {@inheritDoc} + */ + public List listOfFolders( ) throws Exception { + + PreparedStatement ps=null; + //ps=this.connection().prepareStatement("Select a.* FROM folder as a left outer join function_group as b on b.group_uid = a.group_uid ORDER BY b.disp_order,a.disp_order "); + + StringBuffer sb = new StringBuffer(); + sb.append("Select a.*, "); + sb.append("(select CONCAT(GROUP_CONCAT(cast(tags.tagname as CHAR(8000)) order by tags.tagname SEPARATOR ', '), ', ') from folder_tags as st left outer join tags on tags.id=st.tag_id WHERE st.folder_id=a.id ) as stags "); + sb.append("FROM folder as a left outer join function_group as b on b.group_uid = a.group_uid ORDER BY b.disp_order,a.disp_order "); + 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 List listOfFolders(String group_id) throws Exception { + + PreparedStatement ps=null; + + StringBuffer sb = new StringBuffer(); + sb.append("Select *, "); + sb.append("(select CONCAT(GROUP_CONCAT(cast(tags.tagname as CHAR(8000)) order by tags.tagname SEPARATOR ', '), ', ') from folder_tags as st left outer join tags on tags.id=st.tag_id WHERE st.folder_id=a.id ) as stags "); + sb.append("FROM folder as a WHERE group_uid=? ORDER BY disp_order "); + ps=this.connection().prepareStatement(sb.toString()); + ps.setString(1, group_id); + ResultSet rs=ps.executeQuery(); + Vector rtn=new Vector(); + while(rs.next()){ + rtn.add(new BasicRowProcessor().toMap(rs)); + } + return rtn; + } + + + /** + * {@inheritDoc} + */ + public void moveFolder(int folder_id, String new_group_id) throws Exception{ + + PreparedStatement ps=this.connection().prepareStatement("UPDATE folder SET group_uid = ? WHERE id=?"); + ps.setString(1, new_group_id); + ps.setInt(2,folder_id); + ps.executeUpdate(); + ps.close(); + } + + + /** + * {@inheritDoc} + */ + public int getFolderID(String folder_name) throws Exception { + + PreparedStatement ps=null; + ps=this.connection().prepareStatement("Select * FROM 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 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 void renameFolder(int folder_id,String foldername) throws Exception { + PreparedStatement ps=this.connection().prepareStatement("UPDATE folder SET folder_name = ? WHERE id=?"); + ps.setString(1, foldername); + ps.setInt(2,folder_id); + ps.executeUpdate(); + ps.close(); + + } + + + /** + * {@inheritDoc} + */ + public void removeFolder(int folder_id) throws Exception { + PreparedStatement ps=this.connection().prepareStatement("DELETE FROM folder WHERE id=?"); + ps.setInt(1,folder_id); + ps.executeUpdate(); + ps.close(); + } + + + /** + * {@inheritDoc} + */ + public Vector getTags() throws Exception { + Vector rtn=new Vector(); + PreparedStatement ps=this.connection().prepareStatement("select * FROM tags ORDER BY tagname"); + ResultSet rs=ps.executeQuery(); + while(rs.next()){ + rtn.add(new BasicRowProcessor().toMap(rs)); + } + return rtn; + } + + + /** + * {@inheritDoc} + */ + public Vector getTagIds4Function(int function_id) throws Exception { + PreparedStatement ps=this.connection().prepareStatement("select tag_id FROM function_tags WHERE function_id=?"); + ps.setInt(1, function_id); + ResultSet rs=ps.executeQuery(); + Vector rtn=new Vector(); + while(rs.next()){ + rtn.add(rs.getInt("tag_id")); + } + return rtn; + } + + + /** + * {@inheritDoc} + */ + public ArrayList getTagIds4Folder(int folder_id, String tablename) throws Exception { + PreparedStatement ps=this.connection().prepareStatement("select tag_id FROM "+tablename+" WHERE folder_id=?"); + ps.setInt(1, folder_id); + ResultSet rs=ps.executeQuery(); + ArrayList rtn=new ArrayList(); + while(rs.next()){ + rtn.add(rs.getInt("tag_id")); + } + return rtn; + } + + + /** + * {@inheritDoc} + */ + public List getThemeTags4Folder(int folder_id) throws Exception { + + String q="select replace(b.tagname,'thm-','') as theme FROM folder_tags as a "; + q+="left outer join "+getTagTable()+" as b on a.tag_id=b.id "; + q+="where a.folder_id=? "; + q+="and left(b.tagname,4)='thm-' "; + + PreparedStatement ps=this.connection().prepareStatement(q); + ps.setInt(1, folder_id); + + ResultSet rs=ps.executeQuery(); + ArrayList rtn=new ArrayList(); + while(rs.next()){ + rtn.add(rs.getString("theme")); + } + return rtn; + } + + + /** + * {@inheritDoc} + */ + public void updateTagIds4Folder(int folder_id, List tag_ids,int removeExisting, String tablename) throws Exception { + if(SchedulerDB.REMOVE_BEFORE_UPDATE==removeExisting){ + PreparedStatement ps=this.connection().prepareStatement("DELETE FROM "+tablename+" WHERE folder_id=?"); + //ps.setString(1, tablename); + ps.setInt(1, folder_id); + ps.executeUpdate(); + ps.close(); + } + PreparedStatement ps1=this.connection().prepareStatement("INSERT INTO "+tablename+"(folder_id,tag_id) VALUES(?,?)"); + for(Iterator i=tag_ids.iterator();i.hasNext();){ + //ps1.setString(1, tablename); + ps1.setInt(1, folder_id); + ps1.setInt(2, Integer.parseInt((String)i.next())); + ps1.executeUpdate(); + } + + ps1.close(); + } + + + /** + * {@inheritDoc} + */ + public void updateLast2UsersTag(int function_id, int user_tagid) throws Exception { + + // works without 'limit 2', the result is equal. + String query="DELETE FROM function_tags WHERE id IN ( select id from ( SELECT st.id FROM function_tags as st "; + query+=" LEFT OUTER JOIN tags on tags.id=st.tag_id "; + query+=" WHERE st.id NOT IN( "; + query+=" select st1.id from function_tags as st1 "; + query+=" left outer join tags as t1 on t1.id=st1.tag_id "; + query+=" WHERE LEFT(t1.tagname,4)='usr-' AND st1.function_id=st.function_id "; + query+=" order by st1.id desc "; + query+=" ) "; + query+=" AND LEFT(tags.tagname,4)='usr-' AND st.function_id=? ) temp_table) "; + PreparedStatement ps=this.connection().prepareStatement(query); + ps.setInt(1, function_id); + ps.executeUpdate(); + ps.close(); + + PreparedStatement ps2=this.connection().prepareStatement("DELETE FROM function_tags WHERE function_id=? AND tag_id=?"); + ps2.setInt(1, function_id); + ps2.setInt(2, user_tagid); + ps2.executeUpdate(); + + + PreparedStatement ps1=this.connection().prepareStatement("INSERT INTO function_tags(function_id,tag_id) VALUES(?,?)"); + ps1.setInt(1, function_id); + ps1.setInt(2, user_tagid); + ps1.executeUpdate(); + ps1.close(); + + } + + + /** + * {@inheritDoc} + */ + public int addIfTagNotExist(String tagname) throws Exception { + + PreparedStatement ps=this.connection().prepareStatement("select * FROM tags WHERE tagname=?"); + ps.setString(1, tagname); + ResultSet rs=ps.executeQuery(); + int id=0; + if(rs.next()){ + id=rs.getInt("id"); + }else{ + PreparedStatement ps1=this.connection().prepareStatement("insert into tags(tagname) values(?)",Statement.RETURN_GENERATED_KEYS); + ps1.setString(1, tagname); + ps1.executeUpdate(); + ResultSet generatedKeys = ps1.getGeneratedKeys(); + if (generatedKeys.next()) { + id=generatedKeys.getInt(1); + } + } + return id; + } + + + /** + * {@inheritDoc} + */ + public void addTagIds4Function(int function_id, int tag_id) throws Exception { + + PreparedStatement ps=this.connection().prepareStatement("DELETE FROM function_tags WHERE function_id=? and tag_id=?"); + ps.setInt(1, function_id); + ps.setInt(2, tag_id); + ps.executeUpdate(); + ps.close(); + + PreparedStatement ps1=this.connection().prepareStatement("INSERT INTO function_tags(function_id,tag_id) VALUES(?,?)"); + ps1.setInt(1, function_id); + ps1.setInt(2, tag_id); + ps1.executeUpdate(); + ps1.close(); + } + + + /** + * {@inheritDoc} + */ + public void removeTagIds4Function(int function_id, int tag_id) throws Exception { + PreparedStatement ps=this.connection().prepareStatement("DELETE FROM function_tags WHERE function_id=? and tag_id=?"); + ps.setInt(1, function_id); + ps.setInt(2, tag_id); + ps.executeUpdate(); + ps.close(); + } + + + /** + * {@inheritDoc} + */ + public void setGroupOrder( Vector groupids) throws Exception { + + PreparedStatement ps1=this.connection().prepareStatement("UPDATE function_group SET disp_order=? WHERE group_uid=?"); + int count=0; + for(Iterator i=groupids.iterator();i.hasNext();){ + ps1.setInt(1, count++); + ps1.setString(2, i.next()); + ps1.executeUpdate(); + } + } + + + /** + * {@inheritDoc} + */ + public Map getAllFunctionNamesID(String wherecond) throws Exception { + + String query="select id,function_name from r_function "; + if(wherecond!=null && !wherecond.equals("")) { + query+=" WHERE "+wherecond; + } + PreparedStatement ps=this.connection().prepareStatement(query); + ResultSet rs=ps.executeQuery(); + HashMap h=new HashMap(); + while(rs.next()){ + int id=rs.getInt("id"); + String fname=rs.getString("function_name"); + h.put(id, fname); + } + return h; + } + + + /** + * {@inheritDoc} + */ + public boolean updatePeerPackage(String peername, ArrayListplist) throws Exception { + + try{ + this.connection().setAutoCommit(false); + PreparedStatement ps=this.connection().prepareStatement("DELETE FROM peer_packages WHERE peername=?"); + ps.setString(1, peername); + ps.executeUpdate(); + ps.close(); + PreparedStatement ps1=this.connection().prepareStatement("INSERT INTO peer_packages(peername,packagename,version,lastchecked) VALUES(?,?,?,?)"); + for(PeerPackage pp: plist){ + ps1.setString(1, peername); + ps1.setString(2, pp.getPackagename()); + ps1.setString(3, pp.getVersion()); + ps1.setTimestamp(4, new Timestamp(pp.getLastchecked().getTime())); + ps1.executeUpdate(); + } + ps1.close(); + this.connection().commit(); + return true; + }catch(Exception e){ + this.connection().rollback(); + throw e; + }finally{ + this.connection().setAutoCommit(false); + } + } + + + /** + * {@inheritDoc} + */ + public Map getPackageInfo(String pname) throws Exception { + + StringBuffer sb = new StringBuffer(); + sb.append("select a.version, "); + sb.append("(select CONCAT(GROUP_CONCAT(cast(b.peername as CHAR(8000)) SEPARATOR ', '), ', ') from peer_packages as b where b.packagename=? and b.version=a.version ) as peers "); + sb.append("from peer_packages as a "); + sb.append("where a.packagename=? "); + sb.append("group by a.version "); + PreparedStatement ps1=this.connection().prepareStatement(sb.toString()); + ps1.setString(1,pname); + ps1.setString(2,pname+""); + ResultSet rs=ps1.executeQuery(); + LinkedHashMap res=new LinkedHashMap(); + while(rs.next()){ + String version=rs.getString("version"); + String peers=rs.getString("peers"); + peers=peers.substring(0,peers.lastIndexOf(",")); + res.put(version, peers); + } + rs.close(); + ps1.close(); + + return res; + + //return null; + + } + + + /** + * {@inheritDoc} + */ + public List getDefaultHierarchyDependsIds(String pname) throws Exception { + /* + String query=""; + query+="select a.* from folder as a "; + query+="left outer join function_group as b on a.group_uid = b.group_uid "; + query+="where b.disp_order<=( "; + query+=" select function_group.disp_order from folder "; + query+=" left outer join function_group on folder.group_uid = function_group.group_uid "; + query+=" where folder_name=? "; + query+=") "; + query+="order by b.disp_order,a.disp_order "; + */ + + String query=""; + query+="select a.id,a.folder_name from folder as a "; + query+=" left outer join function_group as b on a.group_uid = b.group_uid "; + query+=" order by b.disp_order, a.disp_order "; + PreparedStatement ps1=this.connection().prepareStatement(query); + //ps1.setString(1,pname); + ResultSet rs=ps1.executeQuery(); + //LinkedHashMap res=new LinkedHashMap(); + ArrayList res=new ArrayList(); + boolean cont=true; + while(rs.next() && cont){ + Integer id=rs.getInt("id"); + String packname=rs.getString("folder_name"); + if(packname.equals(pname)){ + cont=false; + }else{ + res.add(id); + } + } + return res; + } + + + /** + * {@inheritDoc} + */ + public List listRelatedFolderIds(int parent_folder) throws Exception { + String query="select related_folder_id from folder_dependencies where folder_id=?" ; + PreparedStatement ps1=this.connection().prepareStatement(query); + ps1.setInt(1,parent_folder); + ResultSet rs=ps1.executeQuery(); + ArrayList res=new ArrayList(); + while(rs.next()){ + Integer id=rs.getInt("related_folder_id"); + res.add(id); + } + return res; + } + + + /** + * {@inheritDoc} + */ + public void updateRelatedFolderIds(int folder_id, List ids) throws Exception { + PreparedStatement ps1=this.connection().prepareStatement("DELETE from folder_dependencies where folder_id=?"); + ps1.setInt(1, folder_id); + ps1.executeUpdate(); + ps1.close(); + + PreparedStatement ps=this.connection().prepareStatement("INSERT INTO folder_dependencies(folder_id,related_folder_id) VALUES(?,?)"); + for(Integer i:ids){ + ps.setInt(1, folder_id); + ps.setInt(2, i); + ps.executeUpdate(); + } + ps.close(); + + } + + + /** + * {@inheritDoc} + */ + public void addTag4Package(int tag_id,int function_ids[],boolean make_owner_also) throws Exception { + + try{ + + this.connection().setAutoCommit(false); + PreparedStatement ps=this.connection().prepareStatement("DELETE FROM function_tags WHERE function_id=? and tag_id=?"); + PreparedStatement ps1=this.connection().prepareStatement("insert into function_tags(function_id,tag_id) VALUES(?,?)"); + PreparedStatement ps2=this.connection().prepareStatement("UPDATE r_function SET owner_tag_id=? WHERE id=?"); + for(int i=0;i 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 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; + + } + +} diff --git a/lib-db/src/main/java/com/fourelementscapital/db/mariadb/SchedulerDBMariaDB.java b/lib-db/src/main/java/com/fourelementscapital/db/mariadb/SchedulerDBMariaDB.java new file mode 100755 index 0000000..6185536 --- /dev/null +++ b/lib-db/src/main/java/com/fourelementscapital/db/mariadb/SchedulerDBMariaDB.java @@ -0,0 +1,3245 @@ +/****************************************************************************** +* +* 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.ResultSetMetaData; +import java.sql.SQLException; +import java.sql.Statement; +import java.sql.Timestamp; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +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.SchedulerDB; +import com.fourelementscapital.db.vo.SchedulerTrigger; + +/** + * Scheduler MariaDB DAO implementation + */ +public class SchedulerDBMariaDB extends SchedulerDB { + + + private Logger log=LogManager.getLogger(SchedulerDBMariaDB.class.getName()); + + + /** + * {@inheritDoc} + */ + public List listScheduler() throws Exception { + + + StringBuffer sb = new StringBuffer(); + sb.append("Select a.*,b.folder_name,c.tagname as owner_tag,(select CONCAT(GROUP_CONCAT(cast(tags.tagname as CHAR(8000)) order by tags.tagname SEPARATOR ', '), ', ') from scheduler_tags as st left outer join tags on tags.id=st.tag_id WHERE st.scheduler_id=a.id ) as stags "); + sb.append("FROM scheduler as a "); + sb.append("LEFT join scheduler_folder as b on a.folder_id=b.id "); + sb.append("left join tags as c on c.id=a.owner_tag_id "); + sb.append("WHERE a.deleted IS NULL OR a.deleted<>1 "); + sb.append("ORDER by a.name "); + + PreparedStatement ps=this.connection().prepareStatement(sb.toString()); + + ResultSet rs=ps.executeQuery(); + Vector rtn=new Vector(); + + ResultSetMetaData metaData = rs.getMetaData(); + int colCount = metaData.getColumnCount(); + while(rs.next()){ + //rtn.add(new BasicRowProcessor().toMap(rs)); + rtn.add(GeneralUtilDB.resultsetToMap(rs)); + } + //added by rams on 5-june-2012 + rs.close(); + ps.close(); + + return rtn; + } + + /** + * {@inheritDoc} + */ + public List listSchedulerTaskUIDJoin() throws Exception { + + StringBuffer sb = new StringBuffer(); + sb.append("Select a.*,b.folder_name,c.tagname as owner_tag,(select CONCAT(GROUP_CONCAT(cast(tags.tagname as CHAR(8000)) order by tags.tagname SEPARATOR ', '), ', ') from scheduler_tags as st left outer join tags on tags.id=st.tag_id where st.scheduler_id=a.id ) as stags "); + sb.append("from scheduler as a "); + sb.append("left outer join scheduler_folder as b on a.folder_id=b.id AND a.taskuid = b.taskuid "); + sb.append("left outer join tags as c on c.id=a.owner_tag_id "); + sb.append("WHERE a.deleted IS NULL OR a.deleted<>1 "); + sb.append("order by a.name "); + + PreparedStatement ps=this.connection().prepareStatement(sb.toString()); + + ResultSet rs=ps.executeQuery(); + Vector rtn=new Vector(); + while(rs.next()){ + //rtn.add(new BasicRowProcessor().toMap(rs)); + rtn.add(GeneralUtilDB.resultsetToMap(rs)); + } + + rs.close(); + ps.close(); + return rtn; + } + + + /** + * {@inheritDoc} + */ + public void fixSchBrokenFolders() throws Exception { + + StringBuffer sb = new StringBuffer(); + sb.append("update scheduler set folder_id=0 where id in ( select id from ( "); + sb.append("select a.id from scheduler as a "); + sb.append("left outer join scheduler_folder as f on a.folder_id = f.id and a.taskuid = f.taskuid "); + sb.append("where f.folder_name is null and a.folder_id >0 ) temp_table ) "); + + PreparedStatement ps=this.connection().prepareStatement(sb.toString()); + + ps.executeUpdate(); + ps.close(); + + } + + + /** + * {@inheritDoc} + */ + public List listScheduler(List themetags) throws Exception { + String thms=""; + for(String tg:themetags){ + thms+=(thms.equals(""))?"'thm-"+tg+"'":",'thm-"+tg+"'"; + } + + log.debug("thms:"+thms); + + StringBuffer sb = new StringBuffer(); + sb.append("Select a.*,b.folder_name,c.tagname as owner_tag,(select CONCAT(GROUP_CONCAT(cast(tags.tagname as CHAR(8000)) order by tags.tagname SEPARATOR ', '), ', ') from scheduler_tags as st left outer join tags on tags.id=st.tag_id where st.scheduler_id=a.id ) as stags "); + sb.append("from scheduler as a "); + sb.append("left outer join scheduler_folder as b on a.folder_id=b.id AND a.taskuid = b.taskuid "); + sb.append("left outer join tags as c on c.id=a.owner_tag_id "); + sb.append("WHERE (a.deleted IS NULL OR a.deleted<>1 ) "); + if(!thms.equals("")){ + sb.append("AND a.id IN(select scheduler_id from scheduler_tags as a1 left outer join tags as a2 on a1.tag_id = a2.id where a2.tagname in ("+thms+")) "); + }else{ + sb.append("AND a.id=0 "); + } + sb.append("order by a.name "); + + log.debug("query:"+sb.toString()); + PreparedStatement ps=this.connection().prepareStatement(sb.toString()); + + //ps.setString(1, following_user); + ResultSet rs=ps.executeQuery(); + Vector rtn=new Vector(); + while(rs.next()){ + //rtn.add(new BasicRowProcessor().toMap(rs)); + rtn.add(GeneralUtilDB.resultsetToMap(rs)); + } + //added by rams on 5-june-2012 + rs.close(); + ps.close(); + return rtn; + } + + + /** + * {@inheritDoc} + */ + public List listTrashedScheduler() throws Exception { + + StringBuffer sb = new StringBuffer(); + sb.append("SELECT a.*,IFNULL(TIMESTAMPDIFF(day,date(a.deletedon),now()),7) as deleteddays,b.folder_name,(select CONCAT(GROUP_CONCAT(cast(tags.tagname as CHAR(8000)) order by tags.tagname SEPARATOR ', '), ', ') from scheduler_tags as st left outer join tags on tags.id=st.tag_id WHERE st.scheduler_id=a.id ) as stags "); + sb.append("FROM scheduler as a "); + sb.append("LEFT outer join scheduler_folder as b on a.folder_id=b.id "); + sb.append("WHERE a.deleted=1 "); + sb.append("ORDER by a.deletedon DESC "); + PreparedStatement ps=this.connection().prepareStatement(sb.toString()); + + ResultSet rs=ps.executeQuery(); + Vector rtn=new Vector(); + while(rs.next()){ + //Map d=new BasicRowProcessor().toMap(rs); + Map d = GeneralUtilDB.resultsetToMap(rs); + + Object dd=d.get("deletedon"); + if(dd!=null){ + SimpleDateFormat sdf=new SimpleDateFormat("dd-MMM-yyyy HH:mm"); + d.put("deletedon_format", sdf.format((Date)dd)); + } + rtn.add(d); + } + //added by rams on 5-june-2012 + rs.close(); + ps.close(); + return rtn; + } + + + /** + * {@inheritDoc} + */ + public List searchScheduler(String ids) throws Exception { + + Vector rtn=new Vector(); + + String query="Select a.*,b.folder_name from scheduler as a left outer join scheduler_folder as b on a.folder_id=b.id "; + query+=" WHERE a.id IN("+ids+") "; + query+=" AND (a.deleted IS NULL OR a.deleted<>1) "; //added by rams to support trash bin feature. + PreparedStatement ps=this.connection().prepareStatement(query); + ResultSet rs=ps.executeQuery(); + while(rs.next()){ + rtn.add(GeneralUtilDB.resultsetToMap(rs)); + } + rs.close(); + ps.close(); + + return rtn; + } + + /** + * {@inheritDoc} + */ + public boolean isDeleted(int id) throws Exception { + + boolean result = false; + String query="Select deleted from scheduler WHERE id="+id; + PreparedStatement ps=this.connection().prepareStatement(query); + ResultSet rs=ps.executeQuery(); + while(rs.next()){ + if (rs.getInt("deleted") == 1) { + result = true; + } + } + rs.close(); + ps.close(); + return result; + } + + + /** + * {@inheritDoc} + */ + public List listAllTasksByUID(String taskuid) throws Exception { + + String query="SELECT a.*,b.folder_name FROM scheduler AS a LEFT OUTER JOIN scheduler_folder AS b ON a.folder_id=b.id "; + query+=" WHERE a.taskuid=? "; + query+=" AND (a.deleted IS NULL OR a.deleted<>1) "; //added by rams to support trash bin feature. + query+=" ORDER BY a.name "; + + PreparedStatement ps=this.connection().prepareStatement(query); + ps.setString(1, taskuid); + ResultSet rs=ps.executeQuery(); + Vector rtn=new Vector(); + while(rs.next()){ + //rtn.add(new BasicRowProcessor().toMap(rs)); + rtn.add(GeneralUtilDB.resultsetToMap(rs)); + } + //added by rams on 5-june-2012 + rs.close(); + ps.close(); + return rtn; + } + + + /** + * {@inheritDoc} + */ + public Map listSchedulerItem(long scheduler_id) throws Exception { + + PreparedStatement ps=this.connection().prepareStatement("SELECT a.*,b.folder_name FROM scheduler AS a LEFT OUTER JOIN scheduler_folder AS b ON a.folder_id=b.id where a.id=? ORDER BY a.name"); + ps.setLong(1, scheduler_id); + ResultSet rs=ps.executeQuery(); + Map rtn=null; + if(rs.next()){ + //rtn=new BasicRowProcessor().toMap(rs); + rtn = GeneralUtilDB.resultsetToMap(rs); + } + + //added by rams on 5-june-2012 + rs.close(); + ps.close(); + + return rtn; + } + + + /** + * {@inheritDoc} + */ + public List folderTasks(int folder_id) throws Exception { + + String query="Select * from scheduler WHERE folder_id=? "; + query+=" AND (deleted IS NULL OR deleted<>1) "; + + PreparedStatement ps=this.connection().prepareStatement(query); + ps.setInt(1, folder_id); + ResultSet rs=ps.executeQuery(); + Vector rtn=new Vector(); + while(rs.next()){ + //rtn.add(new BasicRowProcessor().toMap(rs)); + rtn.add(GeneralUtilDB.resultsetToMap(rs)); + } + //added by rams on 5-june-2012 + rs.close(); + ps.close(); + + return rtn; + } + + + /** + * {@inheritDoc} + */ + public List searchScheduler(String keyword, int tagid) throws Exception { + + Vector rtn=new Vector(); + String query=null; + if(keyword!=null && !keyword.equals("") && tagid>0) { + query="Select a.*,b.folder_name from scheduler as a left outer join scheduler_folder as b on a.folder_id=b.id WHERE a.name LIKE '%"+keyword+"%' AND a.id IN(select scheduler_id from scheduler_tags WHERE tag_id="+tagid+") AND (a.deleted IS NULL OR a.deleted<>1)"; + }else if (keyword!=null && !keyword.equals("")){ + query="Select a.*,b.folder_name from scheduler as a left outer join scheduler_folder as b on a.folder_id=b.id WHERE a.name LIKE '%"+keyword+"%' AND (a.deleted IS NULL OR a.deleted<>1)"; + }else if(tagid>0) { + query="Select a.*,b.folder_name from scheduler as a left outer join scheduler_folder as b on a.folder_id=b.id WHERE a.id IN(select scheduler_id from scheduler_tags WHERE tag_id="+tagid+") AND (a.deleted IS NULL OR a.deleted<>1)"; + }else{ + query="Select a.*,b.folder_name from scheduler as a left outer join scheduler_folder as b on a.folder_id=b.id WHERE a.deleted IS NULL OR a.deleted<>1 "; + } + PreparedStatement ps=this.connection().prepareStatement(query); + ResultSet rs=ps.executeQuery(); + while(rs.next()){ + //rtn.add(new BasicRowProcessor().toMap(rs)); + rtn.add(GeneralUtilDB.resultsetToMap(rs)); + } + + //added by rams on 5-june-2012 + rs.close(); + ps.close(); + + return rtn; + } + + + /** + * {@inheritDoc} + */ + public boolean isNameExisting(String name,int scheduler_id) throws Exception { + + PreparedStatement ps=this.connection().prepareStatement("Select * FROM scheduler where name=? AND id<>?"); + ps.setString(1, name); + ps.setInt(2, scheduler_id); + + ResultSet rs=ps.executeQuery(); + + boolean rtn=false; + if(rs.next()){ + rtn =true; + } + rs.close(); + ps.close(); + return rtn; + + } + + + /** + * {@inheritDoc} + */ + public List listOfFolders(String taskuid) throws Exception { + + PreparedStatement ps=null; + if(taskuid!=null){ + ps=this.connection().prepareStatement("Select * FROM scheduler_folder where taskuid=? ORDER BY folder_name"); + ps.setString(1, taskuid); + }else{ + ps=this.connection().prepareStatement("Select * FROM scheduler_folder ORDER BY folder_name"); + } + + ResultSet rs=ps.executeQuery(); + Vector rtn=new Vector(); + while(rs.next()){ + //rtn.add(new BasicRowProcessor().toMap(rs)); + rtn.add(GeneralUtilDB.resultsetToMap(rs)); + } + //added by rams on 5-june-2012 + rs.close(); + ps.close(); + + return rtn; + } + + + /** + * {@inheritDoc} + */ + public List listofMyFolders(List themetags) throws Exception { + + String thms=""; + for(String tg:themetags){ + thms+=(thms.equals(""))?"'thm-"+tg+"'":",'thm-"+tg+"'"; + } + + String query=""; + query+="Select * FROM scheduler_folder WHERE id in(select folder_id from scheduler where id in( "; + query+=" Select scheduler_id from scheduler_tags as a1 "; + query+=" left outer join tags as a2 on a1.tag_id = a2.id "; + if(thms!=null && !thms.equals("")){ + query+=" where a2.tagname in ("+thms+") ) "; + }else{ + query+=" )"; + } + query+=") ORDER BY folder_name "; + 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(GeneralUtilDB.resultsetToMap(rs)); + } + + rs.close(); + ps.close(); + + return rtn; + } + + + /** + * {@inheritDoc} + */ + public String getFolderTaskUID(int folder_id) throws Exception { + + PreparedStatement ps=null; + + ps=this.connection().prepareStatement("Select taskuid FROM scheduler_folder where id=?"); + ps.setInt(1, folder_id); + + ResultSet rs=ps.executeQuery(); + String rtn=null; + while(rs.next()){ + rtn=rs.getString("taskuid"); + } + //added by rams on 5-june-2012 + rs.close(); + ps.close(); + + return rtn; + } + + + /** + * {@inheritDoc} + */ + public void updateTaskFolder(int scheduler_id,int folder_id) throws Exception { + + PreparedStatement ps=this.connection().prepareStatement("update scheduler SET folder_id=? where id=?"); + ps.setInt(1, folder_id); + ps.setInt(2, scheduler_id); + ps.executeUpdate(); + ps.close(); + + PreparedStatement ps1=this.connection().prepareStatement("update scheduler_taskdata SET val=? WHERE field_shortname=? AND scheduler_id=?"); + if(folder_id>0){ + ps1.setString(1, folder_id+""); + }else{ + ps1.setString(1, ""); + } + ps1.setString(2, "folder_id"); + ps1.setInt(3, scheduler_id); + ps1.executeUpdate(); + ps1.close(); + } + + + /** + * {@inheritDoc} + */ + public void updateCallingAnotherScript(int scheduler_id, int calling_another_script /* either 1 or 0 */) throws Exception { + + PreparedStatement ps=this.connection().prepareStatement("update scheduler SET calling_another_script=? WHERE id=?"); + ps.setInt(1, calling_another_script); + ps.setInt(2, scheduler_id); + ps.executeUpdate(); + + ps.close(); + } + + + /** + * {@inheritDoc} + */ + public void moveTasks(String d_tuid, int d_fid, String s_tuid, int s_fid) throws Exception { + + PreparedStatement ps=this.connection().prepareStatement("update scheduler SET taskuid=?,folder_id=? WHERE taskuid=? AND folder_id=?"); + ps.setString(1, d_tuid); + ps.setInt(2, d_fid); + ps.setString(3, s_tuid); + ps.setInt(4, s_fid); + ps.executeUpdate(); + + PreparedStatement ps1=this.connection().prepareStatement("update scheduler_taskdata SET val=? WHERE field_shortname='folder_id' AND scheduler_id IN (SELECT id FROM scheduler WHERE folder_id=?)"); + ps1.setString(1, d_fid+""); + ps1.setInt(2, d_fid); + ps1.executeUpdate(); + + ps1.close(); + ps.close(); + } + + + /** + * {@inheritDoc} + */ + public void renameFolder(String taskuid, String oldname, String newname) throws Exception { + + if(!oldname.equals(newname)){ + checkFolderExisting(taskuid,newname); + } + PreparedStatement ps=this.connection().prepareStatement("update scheduler_folder set folder_name=replace(folder_name,?,?) where (folder_name LIKE ? OR folder_name=?) AND taskuid=?"); + + ps.setString(1, oldname); + ps.setString(2, newname); + ps.setString(3, oldname+"/%"); + ps.setString(4, oldname); + ps.setString(5, taskuid); + + ps.executeUpdate(); + ps.close(); + } + + + /** + * {@inheritDoc} + */ + public void moveItemToRootOrGroup(int scheduler_id, String taskuid, int folder_id) throws Exception { + + //PreparedStatement ps=this.connection().prepareStatement("update scheduler_folder set folder_name=replace(folder_name,?,?) where (folder_name LIKE ?+'/%' OR folder_name=?) AND taskuid=?"); + + this.connection().setAutoCommit(false); + try{ + String query="UPDATE scheduler SET taskuid=?,folder_id=? WHERE id=?"; + PreparedStatement ps=this.connection().prepareStatement(query); + ps.setString(1,taskuid); + ps.setInt(2,folder_id); + ps.setInt(3,scheduler_id); + + ps.executeUpdate(); + + + PreparedStatement ps1=this.connection().prepareStatement("update scheduler_taskdata SET val=? WHERE field_shortname=? AND scheduler_id=?"); + if(folder_id>0){ + ps1.setString(1, folder_id+""); + }else{ + ps1.setString(1, ""); + } + ps1.setString(2, "folder_id"); + ps1.setInt(3, scheduler_id); + ps1.executeUpdate(); + ps1.close(); + + this.connection().commit(); + }catch(Exception e){ + this.connection().rollback(); + throw e; + }finally{ + this.connection().setAutoCommit(true); + } + } + + + /** + * {@inheritDoc} + */ + public ArrayList moveItems(String foldername, String taskuid, String parentfolder, String targetuid, String targetpath) throws Exception { + + //PreparedStatement ps=this.connection().prepareStatement("update scheduler_folder set folder_name=replace(folder_name,?,?) where (folder_name LIKE ?+'/%' OR folder_name=?) AND taskuid=?"); + + ArrayList rtn=new ArrayList(); + if(parentfolder!=null && !parentfolder.equals("") && !parentfolder.endsWith("/")){ + parentfolder+="/"; + } + if(targetpath!=null && !targetpath.equals("") && !targetpath.endsWith("/")){ + targetpath+="/"; + } + + checkFolderExisting(targetuid, targetpath+foldername); + + this.connection().setAutoCommit(false); + try{ + + String wherecond="WHERE folder_id IN(SELECT id from scheduler_folder WHERE (folder_name LIKE ? OR folder_name=?) AND taskuid=?)"; + + + PreparedStatement ps0=this.connection().prepareStatement("SELECT id from scheduler "+wherecond); + ps0.setString(1,parentfolder+foldername+"/%"); + ps0.setString(2,parentfolder+foldername); + ps0.setString(3,taskuid); + ResultSet rs=ps0.executeQuery(); + while(rs.next()) { + Integer id=rs.getInt("id"); + rtn.add(id); + } + rs.close(); + ps0.close(); + + + String query="UPDATE scheduler SET taskuid=? "+wherecond; + PreparedStatement ps=this.connection().prepareStatement(query); + ps.setString(1,targetuid); + ps.setString(2,parentfolder+foldername+"/%"); + ps.setString(3,parentfolder+foldername); + ps.setString(4,taskuid); + ps.executeUpdate(); + + String query2="UPDATE scheduler_folder SET folder_name=replace(folder_name,?,?), taskuid=? WHERE (folder_name LIKE ? OR folder_name=?) AND taskuid=?"; + PreparedStatement ps2=this.connection().prepareStatement(query2); + ps2.setString(1, parentfolder+foldername); + ps2.setString(2, targetpath+foldername); + ps2.setString(3, targetuid); + ps2.setString(4, parentfolder+foldername+"/%"); + ps2.setString(5, parentfolder+foldername); + ps2.setString(6, taskuid); + ps2.executeUpdate(); + + this.connection().commit(); + return rtn; + }catch(Exception e){ + this.connection().rollback(); + throw e; + }finally{ + this.connection().setAutoCommit(true); + } + + } + + + /** + * {@inheritDoc} + */ + public void deleteFolder(int folderid) throws Exception { + + PreparedStatement ps0=this.connection().prepareStatement("select folder_name,taskuid from scheduler_folder where id=?"); + ps0.setInt(1, folderid); + ResultSet rs0=ps0.executeQuery(); + try{ + if(rs0.next()){ + PreparedStatement ps1=null; + ResultSet rs1=null; + try{ + String foldername=rs0.getString("folder_name"); + String taskuid=rs0.getString("taskuid"); + ps1=this.connection().prepareStatement("select * from scheduler_folder where left(folder_name,length(?))=? AND id<>? AND taskuid=? "); + ps1.setString(1, foldername); + ps1.setString(2, foldername); + ps1.setInt(3, folderid); + ps1.setString(4, taskuid); + rs1=ps1.executeQuery(); + if(rs1.next()){ + throw new Exception("You can't delete this folder as it has Sub folder:"+rs1.getString("folder_name")+". Please delete that first"); + } + }catch(Exception e){ + throw e; + }finally{ + if(rs1!=null) rs1.close(); + if(ps1!=null) ps1.close(); + } + } + }catch(Exception e1){ + throw e1; + }finally{ + rs0.close(); + ps0.close(); + } + + + PreparedStatement ps=this.connection().prepareStatement("delete from scheduler_folder where id=?"); + ps.setInt(1, folderid); + ps.executeUpdate(); + + PreparedStatement ps1=this.connection().prepareStatement("update scheduler SET folder_id=null where folder_id=?"); + ps1.setInt(1, folderid); + ps1.executeUpdate(); + ps1.close(); + } + + + /** + * {@inheritDoc} + */ + public long addFolder(String taskuid,String foldername) throws Exception { + + checkFolderExisting(taskuid,foldername); + + long id=0; + if(foldername.contains("/")){ + String[] folders=foldername.split("/"); + String parent=foldername.substring(0,foldername.lastIndexOf(folders[folders.length-1])); + if(parent.contains("/")){ + parent=parent.substring(0,parent.lastIndexOf("/")); + } + //throw new Exception("Parent folder:"+parent); + PreparedStatement ps0=this.connection().prepareStatement("select folder_name from scheduler_folder WHERE folder_name=? AND taskuid=?"); + ps0.setString(1, parent); + ps0.setString(2, taskuid); + ResultSet rs0=ps0.executeQuery(); + if(!rs0.next()){ + throw new Exception("Parent folder:"+parent+" does not exist, Please create parent folder inclusive of all root folders"); + } + } + + PreparedStatement ps=this.connection().prepareStatement("insert into scheduler_folder(taskuid,folder_name) values(?,?)",Statement.RETURN_GENERATED_KEYS); + ps.setString(1, taskuid); + ps.setString(2, foldername); + ps.executeUpdate(); + ResultSet generatedKeys = ps.getGeneratedKeys(); + + if (generatedKeys.next()) { + id=generatedKeys.getLong(1); + } + + //added by rams on 5-june-2012 + generatedKeys.close(); + ps.close(); + + return id; + } + + + /** + * Check whether folder is exist + * @param taskuid taskuid + * @param foldername folder name + * @throws Exception + */ + private void checkFolderExisting(String taskuid, String foldername) throws Exception { + PreparedStatement ps3=this.connection().prepareStatement("select folder_name from scheduler_folder where taskuid=? AND folder_name=? "); + ps3.setString(1, taskuid); + ps3.setString(2, foldername); + ResultSet rs3=ps3.executeQuery(); + if(rs3.next()){ + rs3.close(); + throw new Exception("Folder name already existing!"); + } + rs3.close(); + } + + + /** + * {@inheritDoc} + */ + public void deleteScheduler(int scheduler_id) throws Exception { + PreparedStatement ps=this.connection().prepareStatement("UPDATE scheduler SET deleted=1, deletedon=? WHERE id=?"); + ps.setTimestamp(1, new Timestamp(new Date().getTime())); + ps.setInt(2, scheduler_id); + ps.executeUpdate(); + ps.close(); + } + + + /** + * {@inheritDoc} + */ + public void putBackScheduler(int scheduler_id) throws Exception { + PreparedStatement ps=this.connection().prepareStatement("UPDATE scheduler SET deleted=NULL, deletedon=NULL WHERE id=?"); + ps.setInt(1, scheduler_id); + ps.executeUpdate(); + ps.close(); + } + + + /** + * {@inheritDoc} + */ + public void purgeScheduler(int scheduler_id) throws Exception { + + PreparedStatement ps1=this.connection().prepareStatement("delete from scheduler_taskdata WHERE scheduler_id=?"); + ps1.setInt(1, scheduler_id); + ps1.executeUpdate(); + ps1.close(); + + PreparedStatement ps2=this.connection().prepareStatement("delete from scheduler_logs WHERE scheduler_id=?"); + ps2.setInt(1, scheduler_id); + ps2.executeUpdate(); + ps2.close(); + + PreparedStatement ps3=this.connection().prepareStatement("delete from scheduler_queuelogs WHERE scheduler_id=?"); + ps3.setInt(1, scheduler_id); + ps3.executeUpdate(); + ps3.close(); + + PreparedStatement ps4=this.connection().prepareStatement("delete from scheduler_tags WHERE scheduler_id=?"); + ps4.setInt(1, scheduler_id); + ps4.executeUpdate(); + ps4.close(); + + PreparedStatement ps=this.connection().prepareStatement("delete from scheduler WHERE id=?"); + ps.setInt(1, scheduler_id); + ps.executeUpdate(); + ps.close(); + } + + + /** + * {@inheritDoc} + */ + public Map getScheduler(int scheduler_id) throws Exception { + + StringBuffer sb = new StringBuffer(); + sb.append("Select a.*,b.folder_name,c.tagname as owner_tag,( "); + sb.append(" select CONCAT(GROUP_CONCAT(cast(tags.tagname as CHAR(8000)) order by tags.tagname SEPARATOR ', '), ', ') "); + sb.append(" from scheduler_tags as st "); + sb.append(" left outer join tags on tags.id=st.tag_id "); + sb.append(" where st.scheduler_id=a.id "); + sb.append(") as stags "); + sb.append("FROM scheduler as a "); + sb.append("LEFT OUTER JOIN scheduler_folder as b on a.folder_id=b.id left outer join tags as c on c.id=a.owner_tag_id "); + sb.append("WHERE a.id=? "); + + PreparedStatement ps=this.connection().prepareStatement(sb.toString(), ResultSet.CONCUR_READ_ONLY); + + ps.setInt(1, scheduler_id); + ResultSet rs=ps.executeQuery(); + + Map data=null; + if(rs.next()){ + //data=new BasicRowProcessor().toMap(rs); + data = GeneralUtilDB.resultsetToMap(rs); + } + if(data!=null){ + + PreparedStatement ps1=this.connection().prepareStatement("Select * from scheduler_taskdata WHERE scheduler_id=?",ResultSet.CONCUR_READ_ONLY); + + ps1.setInt(1, scheduler_id); + ResultSet rs1=ps1.executeQuery(); + while(rs1.next()){ + String shortname=rs1.getString("field_shortname"); + String val=rs1.getString("val"); + + data.put(shortname, val); + } + //added by rams on 5-june-2012 + rs1.close(); + ps1.close(); + + } + + //added by rams on 5-june-2012 + rs.close(); + ps.close(); + + return data; + } + + + /** + * {@inheritDoc} + */ + public int addSchedulerLog(int scheduler_id, Date start, Date end,String timezone, String status,String messages) throws Exception{ + + PreparedStatement pstmt=this.connection().prepareStatement("INSERT INTO scheduler_logs(scheduler_id,start_time,end_time,timezone,status,messages) VALUES(?,?,?,?,?,?)",Statement.RETURN_GENERATED_KEYS); + pstmt.setInt(1, scheduler_id); + pstmt.setTimestamp(2,new Timestamp(start.getTime())); + pstmt.setTimestamp(3, new Timestamp(end.getTime())); + pstmt.setString(4, timezone); + pstmt.setString(5,status); + pstmt.setString(6,messages); + pstmt.executeUpdate(); + ResultSet rs1 = pstmt.getGeneratedKeys(); + int thisid=0; + if(rs1.next()){ + thisid=rs1.getInt(1); + } + pstmt.close(); + return thisid; + } + + + /** + * {@inheritDoc} + */ + public void updateSchedulerLogMsg(int log_id,String messages) throws Exception{ + PreparedStatement pstmt=this.connection().prepareStatement("Update scheduler_logs SET messages=? WHERE id=?"); + pstmt.setString(1, messages); + pstmt.setInt(2, log_id); + pstmt.executeUpdate(); + pstmt.close(); + } + + + /** + * {@inheritDoc} + */ + public Map getQueueLog(int scheduler_id, long trigger_time) throws Exception { + + StringBuffer sb = new StringBuffer(); + sb.append("select * from scheduler_queuelogs WHERE scheduler_id=? AND trigger_time=?; "); + this.connection().setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); + PreparedStatement ps=this.connection().prepareStatement(sb.toString()); + ps.setInt(1, scheduler_id); + ps.setLong(2, trigger_time); + ResultSet rs=ps.executeQuery(); + this.connection().setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); + Map rtn=null; + if(rs.next()){ + //rtn=new BasicRowProcessor().toMap(rs); + rtn = GeneralUtilDB.resultsetToMap(rs); + } + //added by rams on 5-june-2012 + rs.close(); + ps.close(); + + return rtn; + } + + + /** + * {@inheritDoc} + */ + public synchronized List listOfQueueLogs(String sids_trids, long now) throws Exception { + + StringBuffer sb = new StringBuffer(); + +/* + if(sids_trids==null){ + sb.append("select a.*,cast(c.val as char(200)) as dependencies,b.name,b.calling_another_script,b.taskuid,b.timezone,LENGTH(d.console_message) AS console_message_size from ( "); + sb.append(" select * from scheduler_queuelogs "); + sb.append(" WHERE ID in ( select id from ( "); + sb.append(" select id from scheduler_queuelogs "); + sb.append(" where trigger_time<="+now); + sb.append(" order by trigger_time desc limit 0 ) temp_table "); + sb.append(" ) union all "); + sb.append(" select * from scheduler_queuelogs "); + sb.append(" where status is null and trigger_time>"+now); + sb.append(") as a "); + sb.append("left outer join scheduler as b on a.scheduler_id=b.id "); + sb.append("left outer join scheduler_taskdata as c on c.scheduler_id=b.id and c.field_shortname ='dependentids' "); + sb.append("left outer join scheduler_queuelogs_cmessage as d on d.scheduler_id =a.scheduler_id and d.trigger_time = a.trigger_time "); + sb.append("ORDER BY a.trigger_time asc,a.start_time,a.scheduler_id; "); + }else{ + sb.append("select a.*,cast(c.val as char(200)) as dependencies,b.name,b.calling_another_script,b.taskuid,b.timezone,LENGTH(d.console_message) AS console_message_size from ( "); + sb.append(" select * from scheduler_queuelogs "); + sb.append(" WHERE ID in ( "); + sb.append(" select id from scheduler_queuelogs "); + sb.append(" where concat(scheduler_id, concat('_',trigger_time) ) IN ("+sids_trids+") and is_triggered<>1 "); + sb.append(" AND status IS NULL "); + sb.append(" ) union all "); + sb.append(" select * from scheduler_queuelogs "); + sb.append(" where status is null and trigger_time>="+now); + sb.append(") as a "); + sb.append("left outer join scheduler as b on a.scheduler_id=b.id "); + sb.append("left outer join scheduler_taskdata as c on c.scheduler_id=b.id and c.field_shortname ='dependentids' "); + sb.append("left outer join scheduler_queuelogs_cmessage as d on d.scheduler_id =a.scheduler_id and d.trigger_time = a.trigger_time "); + sb.append("ORDER BY a.trigger_time asc,a.start_time,a.scheduler_id; "); + } + */ + + if(sids_trids==null){ + sb.append("select a.host, a.scheduler_id, a.trigger_time, a.status, a.db_insert, a.db_update, a.db_delete, a.is_triggered, a.start_time, a.end_time, a.db_connection_ids, a.response_code, "); + sb.append("cast(c.val as char(200)) as dependencies,b.name,b.calling_another_script,b.taskuid,b.timezone,LENGTH(d.console_message) AS console_message_size from ( "); + sb.append(" select host, scheduler_id, trigger_time, status, db_insert, db_update, db_delete, is_triggered, start_time, end_time, db_connection_ids, response_code "); + sb.append(" from scheduler_queuelogs "); + sb.append(" WHERE ID in ( select id from ( "); + sb.append(" select id from scheduler_queuelogs "); + sb.append(" where trigger_time<="+now); + sb.append(" order by trigger_time desc limit 0 ) temp_table "); + sb.append(" ) union all "); + sb.append(" select host, scheduler_id, trigger_time, status, db_insert, db_update, db_delete, is_triggered, start_time, end_time, db_connection_ids, response_code "); + sb.append(" from scheduler_queuelogs "); + sb.append(" where status is null and trigger_time>"+now); + sb.append(") as a "); + sb.append("left outer join scheduler as b on a.scheduler_id=b.id "); + sb.append("left outer join scheduler_taskdata as c on c.scheduler_id=b.id and c.field_shortname ='dependentids' "); + sb.append("left outer join scheduler_queuelogs_cmessage as d on d.scheduler_id =a.scheduler_id and d.trigger_time = a.trigger_time "); + sb.append("ORDER BY a.trigger_time asc,a.start_time,a.scheduler_id; "); + }else{ + sb.append("select a.host, a.scheduler_id, a.trigger_time, a.status, a.db_insert, a.db_update, a.db_delete, a.is_triggered, a.start_time, a.end_time, a.db_connection_ids, a.response_code, "); + sb.append("cast(c.val as char(200)) as dependencies,b.name,b.calling_another_script,b.taskuid,b.timezone,LENGTH(d.console_message) AS console_message_size from ( "); + sb.append(" select host, scheduler_id, trigger_time, status, db_insert, db_update, db_delete, is_triggered, start_time, end_time, db_connection_ids, response_code "); + sb.append(" from scheduler_queuelogs "); + sb.append(" WHERE ID in ( "); + sb.append(" select id from scheduler_queuelogs "); + sb.append(" where concat(scheduler_id, concat('_',trigger_time) ) IN ("+sids_trids+") and is_triggered<>1 "); + sb.append(" AND status IS NULL "); + sb.append(" ) union all "); + sb.append(" select host, scheduler_id, trigger_time, status, db_insert, db_update, db_delete, is_triggered, start_time, end_time, db_connection_ids, response_code "); + sb.append(" from scheduler_queuelogs "); + sb.append(" where status is null and trigger_time>="+now); + sb.append(") as a "); + sb.append("left outer join scheduler as b on a.scheduler_id=b.id "); + sb.append("left outer join scheduler_taskdata as c on c.scheduler_id=b.id and c.field_shortname ='dependentids' "); + sb.append("left outer join scheduler_queuelogs_cmessage as d on d.scheduler_id =a.scheduler_id and d.trigger_time = a.trigger_time "); + sb.append("ORDER BY a.trigger_time asc,a.start_time,a.scheduler_id; "); + } + + //System.out.println("query - listOfQueueLogs :"+sb.toString()); + + log.debug("query:"+sb.toString()); + boolean ro=isReadOnly(); + setReadOnly(true); + this.connection().setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); + PreparedStatement ps=this.connection().prepareStatement(sb.toString()); + + //long startTime = System.nanoTime(); + ResultSet rs=ps.executeQuery(); + //long endTime = System.nanoTime(); + //System.out.println("Took - listOfQueueLogs : " + ((endTime - startTime) / 1000000) + "ms"); + + this.connection().setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); + Vector rtn=new Vector(); + while(rs.next()){ + //Map row=new BasicRowProcessor().toMap(rs); + Map row = GeneralUtilDB.resultsetToMap(rs); + row.remove("inject_code"); //to avoid too much of text being transported to client. + row.remove("executed_code"); //to avoid too much of text being transported to client. + rtn.add(row); + } + //added by rams on 5-june-2012 + setReadOnly(ro); //setting back the original. + rs.close(); + ps.close(); + return rtn; + } + + + /** + * {@inheritDoc} + */ + public synchronized Map listOfQueueLog(int scheduler_id, long trigger_time ) throws Exception { + + StringBuffer sb = new StringBuffer(); + sb.append("select a.*,b.name,b.taskuid,b.timezone from scheduler_queuelogs as a "); + sb.append("left outer join scheduler as b on a.scheduler_id=b.id "); + sb.append("WHERE a.scheduler_id=? and trigger_time=? "); + sb.append("ORDER BY a.trigger_time asc,a.start_time,a.scheduler_id; "); + this.connection().setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); + PreparedStatement ps=this.connection().prepareStatement(sb.toString()); + + ps.setInt(1, scheduler_id); + ps.setLong(2, trigger_time); + + //System.out.println("query - listOfQueueLog :"+sb.toString()); + + //long startTime = System.nanoTime(); + ResultSet rs=ps.executeQuery(); + //long endTime = System.nanoTime(); + //System.out.println("Took - listOfQueueLog : " + ((endTime - startTime) / 1000000) + "ms"); + + this.connection().setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); + Map rtn=null; + if(rs.next()){ + //rtn=new BasicRowProcessor().toMap(rs); + rtn = GeneralUtilDB.resultsetToMap(rs); + } + //added by rams on 5-june-2012 + rs.close(); + ps.close(); + + return rtn; + } + + + /** + * {@inheritDoc} + */ + public synchronized List listOfLast15Logs(long now, int scheduler_id) throws Exception { + //String query="select a.*,b.*,c.id as error_logid,a.id as qlog_id from scheduler_queuelogs as a left outer join scheduler as b on a.scheduler_id=b.id left outer join scheduler_logs as c on a.log_id=c.id AND c.messages is not null where a.trigger_time<"+now+" AND "+datequery+" AND a.host='"+getHostName()+"' order by a.trigger_time desc,a.start_time desc,a.scheduler_id"; + + StringBuffer sb = new StringBuffer(); + sb.append("select a.*,LENGTH(inject_code) as executed_code_length,LENGTH(inject_code) as inject_code_length,b.*,c.id as error_logid,a.id as qlog_id "); + sb.append("from scheduler_queuelogs as a "); + sb.append("left join scheduler as b on a.scheduler_id=b.id "); + sb.append("left join scheduler_logs as c on a.log_id=c.id AND c.messages is not null "); + sb.append("where a.trigger_time<"+now+" AND a.scheduler_id="+scheduler_id+" " ); + //sb.append("where a.trigger_time < NOW() AND a.scheduler_id="+scheduler_id ); + sb.append("order by a.trigger_time desc,a.start_time desc limit 50; "); + + //System.out.println("SchedulerDB.listOfHistoryQueueLogs()query:"+query); + this.connection().setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); + PreparedStatement ps=this.connection().prepareStatement(sb.toString()); + ResultSet rs=ps.executeQuery(); + this.connection().setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); + Vector rtn=new Vector(); + while(rs.next()){ + //Map row=new BasicRowProcessor().toMap(rs); + Map row = GeneralUtilDB.resultsetToMap(rs); + row.remove("inject_code"); //to avoid too much of text being transported to client. + row.remove("executed_code"); //to avoid too much of text being transported to client. + rtn.add(row); + //rtn.add(new BasicRowProcessor().toMap(rs)); + } + log.debug("listOfHistoryQueueLogs() called:"); + //added by rams on 5-june-2012 + rs.close(); + ps.close(); + return rtn; + } + + + /** + * {@inheritDoc} + */ + public String getConsoleMsg(int scheduler_id, long trigger_time) throws Exception { + + StringBuffer sb = new StringBuffer(); + sb.append("Select console_message from scheduler_queuelogs_cmessage WHERE scheduler_id=? and trigger_time=? "); + + this.connection().setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); + PreparedStatement ps=this.connection().prepareStatement(sb.toString()); + + ps.setInt(1,scheduler_id); + ps.setLong(2,trigger_time); + ResultSet rs=ps.executeQuery(); + this.connection().setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); + String rtn=null; + if(rs.next()){ + rtn=rs.getString("console_message"); + } + rs.close(); + ps.close(); + return rtn; + } + + //For 2nd Scheduler + public String getConsoleMsgFrom2ndScheduler(int scheduler_id) throws Exception { + + StringBuffer sb = new StringBuffer(); + sb.append("Select console_message from scheduler_queuelogs_cmessage WHERE scheduler_id=?"); + + this.connection().setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); + PreparedStatement ps=this.connection().prepareStatement(sb.toString()); + + ps.setInt(1,scheduler_id); + ResultSet rs=ps.executeQuery(); + this.connection().setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); + String rtn=null; + if(rs.next()){ + rtn=rs.getString("console_message"); + } + rs.close(); + ps.close(); + return rtn; + } + //For 2nd Scheduler + public String checkCompletedSignalFromPeer(int scheduler_id) throws Exception { + StringBuffer sb = new StringBuffer(); + sb.append("select message from scheduler_exeplanlogs where message like '%Server rcvd completed signal%' and scheduler_id=?"); + + this.connection().setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); + PreparedStatement ps=this.connection().prepareStatement(sb.toString()); + + ps.setInt(1,scheduler_id); + ResultSet rs=ps.executeQuery(); + this.connection().setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); + String rtn=null; + if(rs.next()){ + rtn=rs.getString("message"); + } + rs.close(); + ps.close(); + return rtn; + } + + + /** + * {@inheritDoc} + */ + public synchronized List listOfHistoryQueueLogs(long now, String datequery) throws Exception { + + StringBuffer sb = new StringBuffer(); + +/* + StringBuffer sb = new StringBuffer(); + sb.append("select a.*,LENGTH(inject_code) as executed_code_length,LENGTH(inject_code) as inject_code_length,b.*, "); + sb.append("c.id as error_logid,a.id as qlog_id,LENGTH(d.console_message) AS console_message_size "); + sb.append("from scheduler_queuelogs as a "); + sb.append("left join scheduler as b on a.scheduler_id=b.id "); + sb.append("left join scheduler_logs as c on a.log_id=c.id AND c.messages is not null "); + sb.append("left join scheduler_queuelogs_cmessage as d on d.scheduler_id =a.scheduler_id and d.trigger_time = a.trigger_time "); + sb.append("where a.trigger_time<"+now+" AND "+datequery+" order by a.trigger_time desc,a.start_time desc,a.scheduler_id; "); + */ + + sb.append("select a.host, a.scheduler_id, a.trigger_time, a.status, a.db_insert, a.db_update, a.db_delete, a.is_triggered, a.start_time, a.end_time, a.db_connection_ids, a.response_code, "); + sb.append("LENGTH(a.inject_code) as executed_code_length,LENGTH(a.inject_code) as inject_code_length, "); + sb.append("b.timezone, b.taskuid, b.name, "); + sb.append("c.id as error_logid,a.id as qlog_id,LENGTH(d.console_message) AS console_message_size "); + sb.append("from scheduler_queuelogs as a "); + sb.append("left join scheduler as b on a.scheduler_id=b.id "); + sb.append("left join scheduler_logs as c on a.log_id=c.id AND c.messages is not null "); + sb.append("left join scheduler_queuelogs_cmessage as d on d.scheduler_id =a.scheduler_id and d.trigger_time = a.trigger_time "); + sb.append("where a.trigger_time<"+now+" AND "+datequery+" order by a.trigger_time desc,a.start_time desc,a.scheduler_id; "); + + this.connection().setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); + PreparedStatement ps=this.connection().prepareStatement(sb.toString()); + + //System.out.println("query - listOfHistoryQueueLogs :"+sb.toString()); + //long startTime = System.nanoTime(); + ResultSet rs=ps.executeQuery(); + //long endTime = System.nanoTime(); + //System.out.println("Took - listOfHistoryQueueLogs : " + ((endTime - startTime) / 1000000) + "ms"); + + this.connection().setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); + Vector rtn=new Vector(); + + while(rs.next()){ + //Map row=new BasicRowProcessor().toMap(rs); + Map row = GeneralUtilDB.resultsetToMap(rs); + + row.remove("inject_code"); //to avoid too much of text being transported to client. + row.remove("executed_code"); //to avoid too much of text being transported to client. + rtn.add(row); + //rtn.add(new BasicRowProcessor().toMap(rs)); + } + + log.debug("listOfHistoryQueueLogs() called:"); + //added by rams on 5-june-2012 + rs.close(); + ps.close(); + return rtn; + } + + + /** + * {@inheritDoc} + */ + public void removeQueueLogs(long currentTime, String tasktypes) throws Exception{ + + // execute only if tasktypes has value + if (tasktypes != null && !"".equals(tasktypes)) { + + //PreparedStatement ps=this.connection().prepareStatement("DELETE from scheduler_queuelogs WHERE trigger_time>? AND host=?"); + StringBuffer sb = new StringBuffer(); + sb.append("delete from scheduler_queuelogs "); + sb.append("where id in( select * from ( "); + sb.append("select sq.id from scheduler_queuelogs as sq "); + sb.append("left outer join scheduler as s on s.id=sq.scheduler_id "); + sb.append("where s.taskuid IN ("+tasktypes+") "); + sb.append(") table_temp ) AND trigger_time>? "); + + //PreparedStatement ps=this.connection().prepareStatement("DELETE from scheduler_queuelogs WHERE trigger_time>? "); + PreparedStatement ps=this.connection().prepareStatement(sb.toString()); + + ps.setLong(1, currentTime); + //ps.setString(2, getHostName()); + ps.executeUpdate(); + ps.close(); + } + + } + + + /** + * {@inheritDoc} + */ + public void removeQueueLog(long currentTime, int scheduler_id) throws Exception{ + PreparedStatement ps=this.connection().prepareStatement("DELETE from scheduler_queuelogs WHERE trigger_time>? AND scheduler_id=?"); + ps.setLong(1, currentTime); + ps.setInt(2, scheduler_id); + ps.executeUpdate(); + ps.close(); + } + + + /** + * {@inheritDoc} + */ + public void updateQueueLogStatus(int id,String status, String computerName) throws Exception{ + + PreparedStatement ps=this.connection().prepareStatement("UPDATE scheduler_queuelogs SET status=?,log_insertedby=? where id=?"); + ps.setString(1, status); + ps.setString(2, computerName); + ps.setInt(3, id); + ps.executeUpdate(); + ps.close(); + } + + + /** + * {@inheritDoc} + */ + public void updateQueueLogStatus(int scheduler_id, long trigger_time, String status, String computerName) throws Exception{ + + PreparedStatement ps=this.connection().prepareStatement("UPDATE scheduler_queuelogs SET status=?,log_insertedby=? WHERE scheduler_id=? AND trigger_time=?"); + ps.setString(1, status); + ps.setString(2, computerName); + ps.setInt(3, scheduler_id); + ps.setLong(4, trigger_time); + ps.executeUpdate(); + ps.close(); + } + + + /** + * {@inheritDoc} + */ + public void updateQueueNullStatus(int scheduler_id, long trigger_time,String status) throws Exception{ + + PreparedStatement ps=this.connection().prepareStatement("UPDATE scheduler_queuelogs SET status=? WHERE scheduler_id=? AND trigger_time=? AND (status is null OR status ='')"); + ps.setString(1, status); + ps.setInt(2, scheduler_id); + ps.setLong(3, trigger_time); + ps.executeUpdate(); + ps.close(); + } + + + /** + * {@inheritDoc} + */ + public void updateResponseCode(int scheduler_id, long trigger_time,int code) throws Exception{ + + PreparedStatement ps=this.connection().prepareStatement("UPDATE scheduler_queuelogs SET response_code=? WHERE scheduler_id=? AND trigger_time=?"); + ps.setInt(1, code); + ps.setInt(2, scheduler_id); + ps.setLong(3, trigger_time); + ps.executeUpdate(); + ps.close(); + } + + + /** + * {@inheritDoc} + */ + public void updateServerStartEnd(int scheduler_id,long trigger_time,Date start, Date end) throws Exception{ + + PreparedStatement ps=this.connection().prepareStatement("UPDATE scheduler_queuelogs SET start_time=?,end_time=?,timebyserver=? WHERE scheduler_id=? AND trigger_time=?"); + ps.setTimestamp(1, new Timestamp(start.getTime())); + ps.setTimestamp(2, new Timestamp(end.getTime())); + ps.setInt(3, 1); + ps.setInt(4, scheduler_id); + ps.setLong(5, trigger_time); + ps.executeUpdate(); + ps.close(); + } + + + /** + * {@inheritDoc} + */ + public void updateHostAndStarted(int scheduler_id,long trigger_time,Date start, String host) throws Exception{ + PreparedStatement ps=this.connection().prepareStatement("UPDATE scheduler_queuelogs SET start_time=?,host=? WHERE scheduler_id=? AND trigger_time=?"); + ps.setTimestamp(1, new Timestamp(start.getTime())); + ps.setString(2, host); + ps.setInt(3, scheduler_id); + ps.setLong(4, trigger_time); + ps.executeUpdate(); + ps.close(); + } + + + /** + * {@inheritDoc} + */ + public String getHostName() throws Exception { + return java.net.InetAddress.getLocalHost().getHostName(); + } + + + /** + * {@inheritDoc} + */ + public void updateExecutedCodeQLog(int scheduler_id, long trigger_time, String code ) throws Exception{ + String query="UPDATE scheduler_queuelogs SET executed_code=? WHERE scheduler_id=? and trigger_time=?"; + PreparedStatement ps=this.connection().prepareStatement(query); + ps.setString(1, code); + ps.setInt(2,scheduler_id); + ps.setLong(3, trigger_time); + ps.executeUpdate(); + ps.close(); + } + + + /** + * {@inheritDoc} + */ + public void updateQueueLog(Collection data, Vector connection_ids, String computerName) throws Exception{ + + StringBuffer sb = new StringBuffer(); + sb.append("SELECT * from scheduler_queuelogs WHERE scheduler_id=? AND trigger_time=? "); + + PreparedStatement ps=this.connection().prepareStatement(sb.toString()); + + String queryinsert="INSERT INTO scheduler_queuelogs(is_triggered,start_time,end_time,status,log_id,host,log_insertedby,db_insert,db_update,db_delete,inject_code,scheduler_id,trigger_time) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)"; + String queryupdate="UPDATE scheduler_queuelogs SET is_triggered=?,start_time=?,end_time=?,status=?,log_id=?,host=?,log_insertedby=?,db_insert=?,db_update=?,db_delete=?,inject_code=? WHERE scheduler_id=? AND trigger_time=?"; + + Number scheduler_id1=null; + Number trigger_time1=null; + for(Iterator i=data.iterator();i.hasNext();){ + Map record=(Map)i.next(); + Number scheduler_id=(Number)record.get("scheduler_id"); + Number trigger_time=(Number)record.get("trigger_time"); + + if(trigger_time1==null && trigger_time!=null){ + trigger_time1=trigger_time; + } + + if(scheduler_id1==null && scheduler_id!=null){ + scheduler_id1=scheduler_id; + } + + + if(scheduler_id!=null &&trigger_time!=null && trigger_time.longValue()>0 && scheduler_id.intValue()>0){ + PreparedStatement ps1=null; + + ps.setInt(1, scheduler_id.intValue()); + ps.setLong(2, trigger_time.longValue()); + //ps.setString(3, getHostName()); + + this.connection().setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); + + ResultSet rs=ps.executeQuery(); + + this.connection().setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); + + + String status1=(String)record.get("status"); + String oldinj_code=null; + + if(rs.next()){ + ps1=this.connection().prepareStatement(queryupdate); + + if(rs.getString("status")!=null && (status1==null || (status1!=null && status1.equals("")))){ + status1=rs.getString("status"); + } + if(rs.getString("inject_code")!=null){ + oldinj_code=rs.getString("inject_code"); + } + + }else{ + ps1=this.connection().prepareStatement(queryinsert); + } + //release JDBC resources + rs.close(); + + int is_triggered1=(record.get("is_triggered")==null?0: ((Number)record.get("is_triggered")).intValue()); + + Date sd=(Date)record.get("start_time"); + Date ed=(Date)record.get("end_time"); + + Integer log_id=0; + try{ + log_id=(Integer)record.get("log_id"); + }catch(Exception e){ + + } + + ps1.setInt(1, is_triggered1); + if(sd!=null){ + ps1.setTimestamp(2,new Timestamp(sd.getTime())); + }else{ + ps1.setTimestamp(2,null); + } + if(ed!=null){ + ps1.setTimestamp(3, new Timestamp(ed.getTime())); + }else{ + ps1.setTimestamp(3, null); + } + + ps1.setString(4,status1); + if(log_id==null){ + ps1.setInt(5,0); + }else{ + ps1.setInt(5,log_id); + } + + String host=(String)record.get("host"); + if(host!=null){ + ps1.setString(6,host); + }else{ + ps1.setString(6,null); + } + + String comp=computerName; + if(comp!=null){ + ps1.setString(7,comp); + }else{ + ps1.setString(7,null); + } + + Integer insert=(Integer)record.get("db_insert"); + if(insert!=null){ + ps1.setInt(8,insert); + }else{ + ps1.setObject(8,null); + } + + Integer update=(Integer)record.get("db_update"); + if(update!=null){ + ps1.setInt(9,update); + }else{ + ps1.setObject(9,null); + } + + Integer delete=(Integer)record.get("db_delete"); + if(delete!=null){ + ps1.setInt(10,delete); + }else{ + ps1.setObject(10,null); + } + + + String code=(String)record.get("inject_code"); + + if(code!=null && !code.equals("")){ + ps1.setString(11,code); + }else{ + ps1.setString(11,oldinj_code); + } + + + ps1.setInt(12,scheduler_id.intValue()); + ps1.setLong(13,trigger_time.longValue()); + ps1.executeUpdate(); + ps1.close(); + + String console_message=(String)record.get("console_message"); + + if(console_message!=null){ + PreparedStatement ps3=this.connection().prepareStatement("select scheduler_id from scheduler_queuelogs_cmessage where scheduler_id=? and trigger_time=? "); + ps3.setInt(1, scheduler_id.intValue()); + ps3.setLong(2, trigger_time.longValue()); + ResultSet rs3=ps3.executeQuery(); + if(rs3.next()){ + PreparedStatement ps4=this.connection().prepareStatement("DELETE FROM scheduler_queuelogs_cmessage where scheduler_id=? and trigger_time=? "); + ps4.setInt(1, scheduler_id.intValue()); + ps4.setLong(2, trigger_time.longValue()); + ps4.executeUpdate(); + ps4.close(); + } + rs3.close(); + ps3.close(); + + PreparedStatement ps5=this.connection().prepareStatement("INSERT INTO scheduler_queuelogs_cmessage(scheduler_id,trigger_time,console_message) VALUES(?,?,?)"); + ps5.setInt(1, scheduler_id.intValue()); + ps5.setLong(2, trigger_time.longValue()); + ps5.setString(3, console_message); + ps5.executeUpdate(); + } + } + } + ps.close(); + //if(ps1!=null){ + // ps1.close(); + //} + log.debug("scheduelr_id1:"+scheduler_id1+" connection_ids:"+connection_ids+" trigger_time1:"+trigger_time1); + if(scheduler_id1!=null && scheduler_id1.intValue()>0 && connection_ids!=null && connection_ids.size()>0 && trigger_time1!=null && trigger_time1.longValue() > 0){ + //String query=; + PreparedStatement ps3=this.connection().prepareStatement("INSERT INTO scheduler_auditlog(scheduler_id,trigger_time,connection_id) VALUES(?,?,?)"); + for(Iterator i=connection_ids.iterator();i.hasNext();){ + ps3.setInt(1, scheduler_id1.intValue()); + ps3.setLong(2, trigger_time1.longValue()); + ps3.setString(3,(String)i.next()); + ps3.executeUpdate(); + } + + } + } + + + /** + * {@inheritDoc} + */ + public Map getDBLogSummary(String con_ids) throws Exception { + //public Map getDBLogSummary(int scheduler_id, long trigger_time) throws Exception { + + StringBuffer sb = new StringBuffer(); + sb.append("select "); + sb.append(" CASE action "); + sb.append(" WHEN 'I' THEN 'db_insert' "); + sb.append(" WHEN 'D' THEN 'db_delete' "); + sb.append(" WHEN 'N' OR 'NU' THEN 'db_update' "); + sb.append(" END as action_t "); + sb.append(",count(*) as count_t "); + sb.append("from audit_logs.gen_logs "); + sb.append("where connection_id IN("+con_ids+") "); + sb.append("and action<>'O' "); + sb.append("group by action "); + + log.debug("~~~getDBLogSummary():query:"+sb.toString()); + PreparedStatement ps=this.connection().prepareStatement(sb.toString()); + //ps.setInt(1, scheduler_id); + //ps.setLong(2, trigger_time); + ResultSet rs1=ps.executeQuery(); + HashMap hm=new HashMap(); + while(rs1.next()){ + String key=rs1.getString("action_t"); + Integer val=rs1.getInt("count_t"); + if(key!=null && val!=null) { + hm.put(key,val); + } + } + //added by rams on 5-june-2012 + rs1.close(); + ps.close(); + + return hm; + } + + + /** + * {@inheritDoc} + */ + public Vector getDataLogHistory(int scheduler_id, long trigger_time) throws Exception { + + StringBuffer sb = new StringBuffer(); + sb.append("select "); + sb.append("dbase+'_'+tablename+'_'+connection_id as uid, "); + sb.append("STR_TO_DATE(cdate,'%d-%m-%Y') as cdate1, "); + sb.append("STR_TO_DATE(trans_date,'%d-%m-%Y %H:%i:%s') as trans_date1, "); + sb.append("trans_date,tablename,dbase,cdate,contract,val,sval, "); + sb.append("case action "); + sb.append("when 'O' then 'N1' "); + sb.append("else action "); + sb.append("end as action_t "); + sb.append("from audit_logs.gen_logs "); + sb.append("where connection_id IN (select connection_id from bbsync.scheduler_auditlog where scheduler_id=? and trigger_time=?) "); + sb.append("order by connection_id,dbase,tablename,trans_date,action_t DESC,contract,cdate,val,sval "); + + PreparedStatement ps1=this.connection().prepareStatement(sb.toString()); + ps1.setInt(1, scheduler_id); + ps1.setLong(2, trigger_time); + + Vector rtn=new Vector(); + ResultSet rs1=ps1.executeQuery(); + while(rs1.next()){ + //rtn.add(new BasicRowProcessor().toMap(rs1)); + rtn.add(GeneralUtilDB.resultsetToMap(rs1)); + } + + //added by rams on 5-june-2012 + rs1.close(); + ps1.close(); + + return rtn; + } + + + /** + * {@inheritDoc} + */ + public String getLogMessages(int log_id) throws Exception { + PreparedStatement ps1=this.connection().prepareStatement("Select messages from scheduler_logs WHERE id=?"); + + ps1.setInt(1, log_id); + + String rtn=null; + ResultSet rs1=ps1.executeQuery(); + while(rs1.next()){ + rtn=rs1.getString("messages"); + } + + //added by rams on 5-june-2012 + rs1.close(); + ps1.close(); + + return rtn; + } + + + /** + * {@inheritDoc} + */ + public int toggleActive(int scheduler_id) throws Exception { + + PreparedStatement ps=this.connection().prepareStatement("update scheduler set active=CASE active WHEN -1 THEN 0 ELSE -1 END where id=?"); + ps.setInt(1, scheduler_id); + ps.executeUpdate(); + ps.close(); + + PreparedStatement ps1=this.connection().prepareStatement("Select active from scheduler WHERE id=?"); + ps1.setInt(1, scheduler_id); + ResultSet rs=ps1.executeQuery(); + + Map data=null; + int rtn=0; + if(rs.next()){ + rtn=rs.getInt("active"); + } + rs.close(); + ps1.close(); + return rtn; + } + + + /** + * {@inheritDoc} + */ + public Integer addOrUpdateSchedulerGetId(int scheduler_id,Map data, String taskUID) throws Exception{ + + Integer thisid=null; + + PreparedStatement pstmt; + boolean updatemode=false; + if(scheduler_id>0){ + updatemode=true; + pstmt=this.connection().prepareStatement("UPDATE scheduler SET name=?,exp_second=?,exp_minute=?,exp_hour=?,exp_day=?,exp_week=?,exp_month=?,timezone=?,taskuid=?,folder_id=?,calling_another_script=?,owner_tag_id=? WHERE id=?"); + }else{ + pstmt=this.connection().prepareStatement("INSERT INTO scheduler(name,exp_second,exp_minute,exp_hour,exp_day,exp_week,exp_month,timezone,taskuid,folder_id,calling_another_script,owner_tag_id,alert_type) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)",Statement.RETURN_GENERATED_KEYS); + //pstmt=this.connection().prepareStatement("INSERT INTO scheduler(name,exp_second,exp_minute,exp_hour,exp_day,exp_week,exp_month,timezone,taskuid,folder_id,calling_another_script,owner_tag_id,alert_type,is_wiki_done,active) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",Statement.RETURN_GENERATED_KEYS); + } + + pstmt.setString(1, data.get("name")); + pstmt.setString(2, data.get("exp_second")); + pstmt.setString(3, data.get("exp_minute")); + pstmt.setString(4, data.get("exp_hour")); + pstmt.setString(5, data.get("exp_day")); + pstmt.setString(6, data.get("exp_week")); + pstmt.setString(7, data.get("exp_month")); + pstmt.setString(8, data.get("timezone")); + pstmt.setString(9, taskUID); + + Integer itgr=0; + try{ itgr=Integer.parseInt((String)data.get("folder_id")); }catch(Exception e){} + pstmt.setInt(10, itgr); + + int calling=0; + try{calling=Integer.parseInt(data.get("calling_another_script"));}catch(Exception e){} + pstmt.setInt(11,calling); + + if(data.get("owner_tag_id")!=null){ + try{ + pstmt.setInt(12,Integer.parseInt(data.get("owner_tag_id"))); + }catch(Exception e){ + pstmt.setInt(12,0); + } + }else{ + pstmt.setInt(12,0); + } + + //pstmt.setInt(14, Integer.parseInt(data.get("is_wiki_done"))); + //pstmt.setInt(15, Integer.parseInt(data.get("active"))); + + if(updatemode){ + pstmt.setInt(13, scheduler_id); + pstmt.executeUpdate(); + + PreparedStatement pstmt1=this.connection().prepareStatement("DELETE FROM scheduler_taskdata WHERE scheduler_id=?"); + pstmt1.setInt(1, scheduler_id); + pstmt1.executeUpdate(); + pstmt1.close(); + thisid=scheduler_id; + }else{ + pstmt.setString(13, "email"); //by default email for new tasks. + pstmt.executeUpdate(); + } + + + + + + if(!updatemode){ + ResultSet rs1 = pstmt.getGeneratedKeys(); + if(rs1.next()){ + thisid=rs1.getInt(1); + } + } + return thisid; + } + + + /** + * {@inheritDoc} + */ + public void addOrUpdateSchedulerInsertTaskdata(Integer thisid,Map data, String shortname) throws Exception{ + PreparedStatement pstmt1=this.connection().prepareStatement("INSERT INTO scheduler_taskdata(field_shortname,val,scheduler_id) VALUES(?,?,?)"); + pstmt1.setString(1,shortname ); + pstmt1.setString(2,data.get(shortname) ); + pstmt1.setInt(3,thisid ); + pstmt1.executeUpdate(); + } + + + /** + * {@inheritDoc} + */ + public boolean updateAlertType(String alert_type,int scheduler_id) throws Exception { + + Vector peers=new Vector(); + String query1="UPDATE scheduler SET alert_type=? WHERE id=?"; + PreparedStatement ps1=this.connection().prepareStatement(query1); + ps1.setString(1, alert_type); + ps1.setInt(2, scheduler_id); + ps1.executeUpdate(); + ps1.close(); + + return true; + } + + + /** + * {@inheritDoc} + */ + public List listDependencyList(String ids, Date d) throws Exception { + //String query="select a.*,b.*,c.id as error_logid,a.id as qlog_id from scheduler_queuelogs as a left outer join scheduler as b on a.scheduler_id=b.id left outer join scheduler_logs as c on a.log_id=c.id AND c.messages is not null where a.trigger_time<"+now+" AND "+datequery+" AND a.host='"+getHostName()+"' order by a.trigger_time desc,a.start_time desc,a.scheduler_id"; + SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + String query="select * from scheduler_queuelogs WHERE scheduler_id IN("+ids+") AND end_time>='"+sdf.format(d)+"' AND is_triggered=1 AND (status='success' OR status='warning') "; + + //System.out.println("SchedulerDB.listOfHistoryQueueLogs()query:"+query); + PreparedStatement ps=this.connection().prepareStatement(query); + //ps.setDate(1, new java.sql.Date(d.getTime())); + ResultSet rs=ps.executeQuery(); + Vector rtn=new Vector(); + while(rs.next()){ + //rtn.add(new BasicRowProcessor().toMap(rs)); + rtn.add(GeneralUtilDB.resultsetToMap(rs)); + } + //added by rams on 5-june-2012 + rs.close(); + ps.close(); + + return rtn; + } + + + /** + * {@inheritDoc} + */ + public Vector getPeersList() throws Exception { + + String query="select peername from peerslist"; + PreparedStatement ps=this.connection().prepareStatement(query); + ResultSet rs=ps.executeQuery(); + Vector peers=new Vector(); + while(rs.next()){ + String pn=rs.getString("peername"); + peers.add(pn); + } + rs.close(); + ps.close(); + + return peers; + } + + + /** + * {@inheritDoc} + */ + public List getSingleColData(String query) throws Exception { + + PreparedStatement ps=this.connection().prepareStatement(query); + ResultSet rs=ps.executeQuery(); + ArrayList data=new ArrayList(); + while(rs.next()){ + data.add(rs.getString(1)); + } + rs.close(); + ps.close(); + return data; + } + + + /** + * {@inheritDoc} + */ + public Vector getPeersData() throws Exception { + + String query="select * from peerslist ORDER BY last_online DESC"; + //String query="select * from peerslist_1 ORDER BY last_online DESC"; + PreparedStatement ps=this.connection().prepareStatement(query); + ResultSet rs=ps.executeQuery(); + Vector data=new Vector(); + while(rs.next()){ + //data.add(new BasicRowProcessor().toMap(rs)); + data.add(GeneralUtilDB.resultsetToMap(rs)); + } + rs.close(); + ps.close(); + return data; + } + + + /** + * {@inheritDoc} + */ + public String getPeerFriendlyName(String peername) throws Exception { + + String query="select friendlyname from peerslist WHERE peername=?"; + //String query="select * from peerslist_1 ORDER BY last_online DESC"; + PreparedStatement ps=this.connection().prepareStatement(query); + ps.setString(1, peername); + ResultSet rs=ps.executeQuery(); + String rtn=null; + if(rs.next()){ + //data.add(new BasicRowProcessor().toMap(rs)); + rtn=rs.getString("friendlyname"); + } + rs.close(); + ps.close(); + return rtn; + } + + + /** + * {@inheritDoc} + */ + public void updatePeersLastOnline(String peername,long time) throws Exception { + Vector peers=new Vector(); + String query1="UPDATE peerslist set last_online=? WHERE peername=?"; + PreparedStatement ps1=this.connection().prepareStatement(query1); + ps1.setLong(1, time); + ps1.setString(2, peername); + ps1.executeUpdate(); + ps1.close(); + } + + + /** + * {@inheritDoc} + */ + public void updatePeersNotes(String peername,String notes, String friendlyname,String cmail) throws Exception { + + Vector peers=new Vector(); + String query1="UPDATE peerslist set notes=?, friendlyname=?,contact_mail=? WHERE peername=?"; + PreparedStatement ps1=this.connection().prepareStatement(query1); + ps1.setString(1, notes); + ps1.setString(2, friendlyname); + ps1.setString(3, cmail); + ps1.setString(4, peername); + ps1.executeUpdate(); + ps1.close(); + + //System.out.println("++++++++++++++++++++++++++++++++++++++++++updatePeersList() called"); + } + + + /** + * {@inheritDoc} + */ + public void updatePeersToggle(String peername,int active, String user) throws Exception { + Vector peers=new Vector(); + + String query1="UPDATE peerslist set active=? WHERE peername=?"; + PreparedStatement ps1=this.connection().prepareStatement(query1); + ps1.setInt(1, active); + ps1.setString(2, peername); + ps1.executeUpdate(); + ps1.close(); + + int action=PEER_ASSOCIATION_HISTORY_PEER_ACTIVE; + if(active==-1){ + action=PEER_ASSOCIATION_HISTORY_PEER_NOACTIVE; + } + String taskuid=null; + trackPeerAssoHistory(taskuid,peername,action, user); + } + + + /** + * {@inheritDoc} + */ + public void updatePeersList(String peername) throws Exception { + + Vector peers=new Vector(); + String query="select peername from peerslist where peername=?"; + PreparedStatement ps=this.connection().prepareStatement(query); + ps.setString(1,peername); + ResultSet rs=ps.executeQuery(); + if(!rs.next()){ + String query1="Insert into peerslist(peername) values(?)"; + PreparedStatement ps1=this.connection().prepareStatement(query1); + ps1.setString(1, peername); + ps1.executeUpdate(); + ps1.close(); + } + rs.close(); + ps.close(); + } + + + /** + * {@inheritDoc} + */ + public Vector getAssociatedPeers(String taskuid) throws Exception { + + PreparedStatement ps=null; + + ps=this.connection().prepareStatement("Select peername FROM scheduler_taskpeers where taskuid=?"); + ps.setString(1, taskuid); + + ResultSet rs=ps.executeQuery(); + Vector rtn=new Vector(); + while(rs.next()){ + rtn.add(rs.getString("peername")); + } + //added by rams on 5-june-2012 + rs.close(); + ps.close(); + + return rtn; + } + + + /** + * {@inheritDoc} + */ + public Vector getAssoAvailablePeers(String taskuid) throws Exception { + PreparedStatement ps=null; + + ps=this.connection().prepareStatement("Select peername FROM scheduler_taskpeers where taskuid=? AND peername not IN (select peername from peerslist where active=-1) "); + ps.setString(1, taskuid); + + ResultSet rs=ps.executeQuery(); + Vector rtn=new Vector(); + while(rs.next()){ + rtn.add(rs.getString("peername")); + } + //added by rams on 5-june-2012 + rs.close(); + ps.close(); + + return rtn; + } + + + /** + * {@inheritDoc} + */ + public List getLast50PeerActHistory(String peername) throws Exception { + StringBuffer sb = new StringBuffer(); + sb.append("select a.username,a.taskuid,a.peername,a.action,a.action_datetime ,b.name,c.friendlyname "); + sb.append("from scheduler_peers_actionhistory as a "); + sb.append("left join scheduler_group as b on a.taskuid=b.taskuid "); + sb.append("left join peerslist as c on a.peername = c.peername "); + sb.append("where a.peername=? "); + sb.append("order by action_datetime desc limit 50 "); + PreparedStatement ps=this.connection().prepareStatement(sb.toString()); + ps.setString(1, peername); + ResultSet rs=ps.executeQuery(); + ArrayList rtn=new ArrayList(); + while(rs.next()){ + //rtn.add(new BasicRowProcessor().toMap(rs)); + rtn.add(GeneralUtilDB.resultsetToMap(rs)); + } + rs.close(); + ps.close(); + return rtn; + } + + + /** + * {@inheritDoc} + */ + public List getLast50TaskActHistory(String taskuid) throws Exception { + StringBuffer sb = new StringBuffer(); + sb.append("select a.username,a.taskuid,a.peername,a.action,a.action_datetime ,b.name,c.friendlyname "); + sb.append("from scheduler_peers_actionhistory as a "); + sb.append("left join scheduler_group as b on a.taskuid=b.taskuid "); + sb.append("left join peerslist as c on a.peername = c.peername "); + sb.append("where a.taskuid=? "); + sb.append("order by action_datetime desc limit 50 "); + PreparedStatement ps=this.connection().prepareStatement(sb.toString()); + ps.setString(1, taskuid); + ResultSet rs=ps.executeQuery(); + ArrayList rtn=new ArrayList(); + while(rs.next()){ + //rtn.add(new BasicRowProcessor().toMap(rs)); + rtn.add(GeneralUtilDB.resultsetToMap(rs)); + } + rs.close(); + ps.close(); + return rtn; + } + + + /** + * Track peer association history + * @param taskuid task uid + * @param peer peer + * @param action action + * @param user user + * @throws Exception + */ + private void trackPeerAssoHistory(String taskuid, String peer, int action, String user) throws Exception { + + PreparedStatement ps2=this.connection().prepareStatement("INSERT INTO scheduler_peers_actionhistory(taskuid,peername,action,action_datetime,username) VALUES(?,?,?,?,?) "); + ps2.setString(1, taskuid); + ps2.setString(2, peer); + ps2.setInt(3,action ) ;// ex ScheduledTask.PEER_ASSOCIATION_HISTORY_ADDED + ps2.setTimestamp(4, new Timestamp(new Date().getTime())); + ps2.setString(5, user); + ps2.executeUpdate(); + } + + + /** + * {@inheritDoc} + */ + public void addPeerTaskuid(String taskuid, String peer, String user) throws Exception { + + PreparedStatement ps1=this.connection().prepareStatement("INSERT INTO scheduler_taskpeers(taskuid, peername) SELECT * FROM (SELECT ?,?) AS table_tmp WHERE NOT EXISTS ( SELECT taskuid, peername FROM scheduler_taskpeers WHERE taskuid = ? and peername = ? ) LIMIT 1 "); + ps1.setString(1, taskuid); + ps1.setString(2, peer); + ps1.setString(3, taskuid); + ps1.setString(4, peer); + ps1.executeUpdate(); + ps1.close(); + trackPeerAssoHistory(taskuid,peer,PEER_ASSOCIATION_HISTORY_ADDED,user); + } + + + /** + * {@inheritDoc} + */ + public void deletePeerTaskuid(String taskuid, String peer, String user) throws Exception { + + PreparedStatement ps1=this.connection().prepareStatement("DELETE FROM scheduler_taskpeers WHERE taskuid = ? AND peername=? "); + ps1.setString(1, taskuid); + ps1.setString(2, peer); + ps1.executeUpdate(); + ps1.close(); + trackPeerAssoHistory(taskuid,peer,PEER_ASSOCIATION_HISTORY_REMOVED,user); + } + + + /** + * {@inheritDoc} + */ + public Vector getAllPeerAssociation() throws Exception { + + PreparedStatement ps=null; + ps=this.connection().prepareStatement("Select * FROM scheduler_taskpeers "); + ResultSet rs=ps.executeQuery(); + Vector rtn=new Vector(); + while(rs.next()){ + //rtn.add(new BasicRowProcessor().toMap(rs)); + rtn.add(GeneralUtilDB.resultsetToMap(rs)); + } + //added by rams on 5-june-2012 + rs.close(); + ps.close(); + + return rtn; + } + + + /** + * {@inheritDoc} + */ + public Map getLastSuccessfulQLog(int scheduler_id) throws Exception { + + StringBuffer sb = new StringBuffer(); + sb.append("select a.* from ( "); + sb.append(" select scheduler_id,max(trigger_datetime) as trigger_datetime "); + sb.append(" from scheduler_queuelogs "); + sb.append(" WHERE status='success' AND start_time IS NOT NULL AND end_time IS NOT NULL AND scheduler_id ="+scheduler_id ); + sb.append(" group by scheduler_id "); + sb.append(") as b "); + sb.append("left outer join scheduler_queuelogs as a on a.trigger_datetime=b.trigger_datetime and a.scheduler_id=b.scheduler_id; "); + + this.connection().setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); + PreparedStatement st=this.connection().prepareStatement(sb.toString(),ResultSet.CONCUR_READ_ONLY); + + ResultSet rs=st.executeQuery(); + this.connection().setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); + Map rtn=null; + while(rs.next()){ + //rtn=new BasicRowProcessor().toMap(rs); + rtn = GeneralUtilDB.resultsetToMap(rs); + } + rs.close(); + st.close(); + return rtn; + } + + + /** + * {@inheritDoc} + */ + public Vector getLastSuccessfulQLogs(Vector scheduler_ids) throws Exception { + + String ids=null; + for(Iterator i=scheduler_ids.iterator();i.hasNext();){ + Object id=i.next(); + ids=(ids==null)?id+"":ids+","+id; + } + + String query="select a.* from (select scheduler_id,max(trigger_datetime) as trigger_datetime from scheduler_queuelogs WHERE status='success' AND start_time IS NOT NULL AND end_time IS NOT NULL AND scheduler_id in ("+ids+") "; + query+="group by scheduler_id ) as b left outer join scheduler_queuelogs as a on a.trigger_datetime=b.trigger_datetime and a.scheduler_id=b.scheduler_id"; + Statement st=this.connection().createStatement(); + ResultSet rs=st.executeQuery(query); + Vector rtn=new Vector(); + while(rs.next()){ + //rtn.add(new BasicRowProcessor().toMap(rs)); + rtn.add(GeneralUtilDB.resultsetToMap(rs)); + } + rs.close(); + st.close(); + + return rtn; + } + + + /** + * {@inheritDoc} + */ + public int addRScript(String name,String script, int restart) throws Exception { + PreparedStatement ps1=this.connection().prepareStatement("INSERT INTO r_script(name,script,restart) VALUES(?,?,?)",Statement.RETURN_GENERATED_KEYS); + ps1.setString(1, name); + ps1.setString(2, script); + ps1.setInt(3, restart); + ps1.executeUpdate(); + ResultSet rs1 = ps1.getGeneratedKeys(); + int thisid=0; + if(rs1.next()){ + thisid=rs1.getInt(1); + } + //added by rams on 5-june-2012 + rs1.close(); + ps1.close(); + + return thisid; + } + + + /** + * {@inheritDoc} + */ + public Map getRScript(int script_id) throws Exception { + PreparedStatement ps=this.connection().prepareStatement("Select name,script,restart FROM r_script where id=?"); + ps.setInt(1, script_id); + + ResultSet rs=ps.executeQuery(); + Map rtn=null; + if(rs.next()){ + //rtn= new BasicRowProcessor().toMap(rs); + rtn = GeneralUtilDB.resultsetToMap(rs); + } + + //added by rams on 5-june-2012 + rs.close(); + ps.close(); + + return rtn; + } + + + /** + * {@inheritDoc} + */ + public void addRScriptLog(int script_id,String peer, String status, Date start_time, Date end_time, String message) throws Exception { + PreparedStatement ps1=this.connection().prepareStatement("INSERT INTO r_script_logs(script_id,peer,status,start_time,end_time,message) VALUES(?,?,?,?,?,?)"); + ps1.setInt(1, script_id); + ps1.setString(2, peer); + ps1.setString(3, status); + ps1.setTimestamp(4,new Timestamp(start_time.getTime())); + ps1.setTimestamp(5,new Timestamp(end_time.getTime())); + ps1.setString(6, message); + ps1.executeUpdate(); + + ps1.close(); + } + + + /** + * {@inheritDoc} + */ + public Vector rScriptLast5Logs() throws Exception { + StringBuffer sb = new StringBuffer(); + sb.append("select a.*,b.name from r_script_logs as a left join r_script as b on a.script_id=b.id "); + sb.append("where a.script_id in (select id from (select id from r_script order by id desc limit 5) temp_table) order by a.script_id desc "); + PreparedStatement ps=this.connection().prepareStatement(sb.toString()); + + 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); + } + + //added by rams on 5-june-2012 + rs.close(); + ps.close(); + + return rtn; + } + + + /** + * {@inheritDoc} + */ + public void setGroupOrder( Vector taskuids) throws Exception { + //PreparedStatement ps=null; + + //ps=this.connection().prepareStatement("delete FROM scheduler_group"); + //ps.executeUpdate(); + + //PreparedStatement ps1=this.connection().prepareStatement("INSERT INTO scheduler_group(taskuid,disp_order) VALUES(?,?)"); + PreparedStatement ps1=this.connection().prepareStatement("UPDATE scheduler_group SET disp_order=? WHERE taskuid=?"); + int count=0; + for(Iterator i=taskuids.iterator();i.hasNext();){ + ps1.setInt(1, count++); + ps1.setString(2, i.next()); + ps1.executeUpdate(); + } + } + + + /** + * {@inheritDoc} + */ + public Vector getGroupOrder( ) throws Exception { + + PreparedStatement ps=this.connection().prepareStatement("select taskuid FROM scheduler_group ORDER by disp_order"); + ResultSet rs=ps.executeQuery(); + Vector rtn=new Vector(); + while(rs.next()){ + rtn.add(rs.getString("taskuid")); + } + + //added by rams on 5-june-2012 + rs.close(); + ps.close(); + + return rtn; + } + + + /** + * {@inheritDoc} + */ + public Vector getAllGroups( ) throws Exception { + + PreparedStatement ps=this.connection().prepareStatement("select * FROM scheduler_group ORDER by disp_order"); + 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); + } + //added by rams on 5-june-2012 + rs.close(); + ps.close(); + + return rtn; + } + + + /** + * {@inheritDoc} + */ + public Vector getActiveGroups( ) throws Exception { + + PreparedStatement ps=this.connection().prepareStatement("select * FROM scheduler_group WHERE active<>-1 ORDER by disp_order "); + 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); + } + //added by rams on 5-june-2012 + rs.close(); + ps.close(); + + return rtn; + } + + + /** + * {@inheritDoc} + */ + public Vector getGroups(String enginetype ) throws Exception { + + PreparedStatement ps=this.connection().prepareStatement("select * FROM scheduler_group WHERE enginetype=? ORDER by disp_order "); + ps.setString(1, enginetype); + 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); + } + //added by rams on 5-june-2012 + rs.close(); + ps.close(); + + return rtn; + } + + + /** + * {@inheritDoc} + */ + public void addSchedulerExeLogs(int scheduler_id, long trigger_time, Date date,String messages,int repCode, String computerName) throws Exception{ + + PreparedStatement pstmt=this.connection().prepareStatement("INSERT INTO scheduler_exeplanlogs(scheduler_id,trigger_time,trans_datetime,machine,message,repcode) VALUES(?,?,?,?,?,?)"); + pstmt.setInt(1, scheduler_id); + pstmt.setLong(2,trigger_time); + pstmt.setTimestamp(3, new Timestamp(date.getTime())); + pstmt.setString(4, computerName); + pstmt.setString(5,messages); + pstmt.setInt(6,repCode); + pstmt.executeUpdate(); + + pstmt.close(); + } + + + /** + * {@inheritDoc} + */ + public boolean execLogsRepcodeExist(int scheduler_id, long trigger_time, int repcode) throws Exception { + boolean rtn=false; + PreparedStatement ps=this.connection().prepareStatement("select * FROM scheduler_exeplanlogs WHERE scheduler_id=? AND trigger_time=? and repcode=?"); + ps.setInt(1, scheduler_id); + ps.setLong(2, trigger_time); + ps.setInt(3, repcode); + ResultSet rs=ps.executeQuery(); + + while(rs.next()){ + rtn=true; + } + + rs.close(); + ps.close(); + + return rtn; + } + + + /** + * {@inheritDoc} + */ + public Vector getSchedulerExeLogs(int scheduler_id, long trigger_time ) throws Exception { + + String query = "select trans_datetime, machine, message, repcode FROM scheduler_exeplanlogs WHERE scheduler_id=? AND trigger_time=? ORDER by id"; + + PreparedStatement ps=this.connection().prepareStatement(query); + ps.setInt(1, scheduler_id); + ps.setLong(2, trigger_time); + + //System.out.println("query - getSchedulerExeLogs : "+query); + //long startTime = System.nanoTime(); + ResultSet rs=ps.executeQuery(); + //long endTime = System.nanoTime(); + //System.out.println("Took - getSchedulerExeLogs : " + ((endTime - startTime) / 1000000) + "ms"); + + Vector rtn=new Vector(); + while(rs.next()){ + //Map record= new BasicRowProcessor().toMap(rs); + Map record= GeneralUtilDB.resultsetToMap(rs); + Timestamp tsm=(Timestamp)record.get("trans_datetime"); + SimpleDateFormat sdf=new SimpleDateFormat("dd-MMM HH:mm:ss"); + record.put("trans_datetime1", sdf.format(tsm)); + rtn.add(record); + } + //added by rams on 5-june-2012 + rs.close(); + ps.close(); + + return rtn; + } + + + /** + * {@inheritDoc} + */ + public Vector getTriggerData(int scheduler_id) throws Exception { + PreparedStatement ps=this.connection().prepareStatement("select * FROM scheduler_trigger where scheduler_id=?"); + ps.setInt(1,scheduler_id); + 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); + } + //added by rams on 5-june-2012 + rs.close(); + ps.close(); + + return rtn; + } + + + /** + * {@inheritDoc} + */ + public Map getOneRowTriggerData(long row_id) throws Exception { + PreparedStatement ps=this.connection().prepareStatement("select * FROM scheduler_trigger where id=?"); + ps.setLong(1,row_id); + ResultSet rs=ps.executeQuery(); + Map record=new HashMap(); + if(rs.next()){ + //record= new BasicRowProcessor().toMap(rs); + record = GeneralUtilDB.resultsetToMap(rs); + + } + //added by rams on 5-june-2012 + rs.close(); + ps.close(); + + return record; + } + + + /** + * {@inheritDoc} + */ + public void setTriggerData(int scheduler_id, SchedulerTrigger arry[]) throws Exception { + PreparedStatement ps1=this.connection().prepareStatement("DELETE FROM scheduler_trigger where scheduler_id=?"); + ps1.setInt(1, scheduler_id); + ps1.executeUpdate(); + ps1.close(); + PreparedStatement ps=this.connection().prepareStatement("INSERT INTO scheduler_trigger(scheduler_id, exp_second,exp_minute,exp_hour,exp_week,exp_day,exp_month,inject_code) VALUES(?,?,?,?,?,?,?,?)"); + for(int i=0;i? order by trigger_time) and status='overlapped'"); + + ps.setInt(1, scheduler_id); + ps.setLong(2, trigger_time); + ResultSet rs=ps.executeQuery(); + Map data=null; + int rtn=0; + if(rs.next()){ + rtn=rs.getInt("maxduration_in_last20"); + } + return rtn; + } + */ + + + /** + * {@inheritDoc} + */ + public long getTimeoutCriteriaInMs(int scheduler_id) throws Exception { + + String query="select max(cast((UNIX_TIMESTAMP(end_time)*1000 - UNIX_TIMESTAMP(start_time)*1000) as int)) as maxlast_50 from scheduler_queuelogs where id in (select * from (select id from scheduler_queuelogs where scheduler_id=? and status='success' order by start_time desc limit 50) temp_table)"; + + PreparedStatement ps=this.connection().prepareStatement(query); + ps.setInt(1, scheduler_id); + ResultSet rs=ps.executeQuery(); + + long rtn=0; + if(rs.next()){ + rtn=rs.getLong(1); + } + return rtn; + } + + + /** + * {@inheritDoc} + */ + public long getMaxDurationInLast50Exec(int scheduler_id) throws Exception { + + boolean err=true; + long rtn=0; + int count=0; + while(err && count<5){ + try{ + count++; + err=false; + + StringBuffer sb = new StringBuffer(); + sb.append("select max(cast((UNIX_TIMESTAMP(end_time)*1000 - UNIX_TIMESTAMP(start_time)*1000) as int))*2 as maxlast_50 "); + sb.append("from scheduler_queuelogs "); + sb.append("where id in( "); + sb.append(" select id from ( "); + sb.append(" select id from scheduler_queuelogs "); + sb.append(" where scheduler_id=? and status='success' order by start_time desc limit 50 "); + sb.append(" ) temp_table "); + sb.append("); "); + + this.connection().setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); + PreparedStatement ps=this.connection().prepareStatement(sb.toString()); + + ps.setInt(1, scheduler_id); + ResultSet rs=ps.executeQuery(); + this.connection().setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); + if(rs.next()){ + rtn=rs.getLong("maxlast_50"); + } + + }catch(SQLException ex){ + + log.error("getMaxDurationInLast50Exec(), Error message e:"+ex.getMessage()); + if(ex.getMessage().contains("was deadlocked on lock resources with another process")){ + log.error("getMaxDurationInLast50Exec(), try "+count); + err=true; + Thread.sleep(50); + }else{ + throw ex; + } + } + } + return rtn; + } + + /* + public Vector getFollowers(int scheduler_id) throws Exception { + PreparedStatement ps=this.connection().prepareStatement("select username FROM scheduler_followers WHERE scheduler_id=?"); + ps.setInt(1, scheduler_id); + ResultSet rs=ps.executeQuery(); + Vector v=new Vector(); + while(rs.next()){ + v.add(rs.getString("username")); + } + rs.close(); + ps.close(); + return v; + } + + public void followFunction(int scheduler_id, String username) throws Exception { + + PreparedStatement ps1=this.connection().prepareStatement("INSERT scheduler_followers(username, scheduler_id) SELECT ?,? WHERE (select count(*) FROM scheduler_followers WHERE username=? AND scheduler_id=?)=0 "); + ps1.setString(1,username); + ps1.setInt(2,scheduler_id); + ps1.setString(3,username); + ps1.setInt(4,scheduler_id); + ps1.executeUpdate(); + ps1.close(); + + } + + public void unFollowFunction(int scheduler_id, String username) throws Exception { + + PreparedStatement ps1=this.connection().prepareStatement("DELETE FROM scheduler_followers WHERE username=? AND scheduler_id=?"); + ps1.setString(1,username); + ps1.setInt(2,scheduler_id); + ps1.executeUpdate(); + ps1.close(); + + } + + */ + + + + /** + * {@inheritDoc} + */ + public String getOwnerTheme(int scheduler_id) throws Exception { + + String query=" select b.tagname from scheduler as a left outer join tags as b on b.id=a.owner_tag_id where a.id=?"; + PreparedStatement ps=this.connection().prepareStatement(query); + ps.setInt(1, scheduler_id); + ResultSet rs=ps.executeQuery(); + String tagname=null; + if(rs.next()){ + tagname=rs.getString("tagname"); + tagname=tagname.replaceAll("thm-", ""); + } + + return tagname; + } + + + /** + * {@inheritDoc} + */ + public void updateWikiDone(int scheduler_id, int done) throws Exception { + String query="UPDATE scheduler set is_wiki_done=? where id=?"; + PreparedStatement ps=this.connection().prepareStatement(query); + ps.setInt(1, done); + ps.setInt(2, scheduler_id); + ps.executeUpdate(); + ps.close(); + } + + + /** + * {@inheritDoc} + */ + public void updateLast2UsersTag(int scheduler_id, int user_tagid) throws Exception { + + // works without 'limit 2', the result is equal. + String query="DELETE FROM scheduler_tags WHERE id IN ( select id from ( SELECT st.id FROM scheduler_tags as st "; + query+=" LEFT OUTER JOIN tags on tags.id=st.tag_id "; + query+=" WHERE st.id NOT IN( "; + query+=" select st1.id from scheduler_tags as st1 "; + query+=" left outer join tags as t1 on t1.id=st1.tag_id "; + query+=" WHERE LEFT(t1.tagname,4)='usr-' AND st1.scheduler_id=st.scheduler_id "; + query+=" order by st1.id desc "; + query+=" ) "; + query+=" AND LEFT(tags.tagname,4)='usr-' AND st.scheduler_id=? ) temp_table) "; + + PreparedStatement ps=this.connection().prepareStatement(query); + ps.setInt(1, scheduler_id); + ps.executeUpdate(); + ps.close(); + + PreparedStatement ps2=this.connection().prepareStatement("DELETE FROM scheduler_tags WHERE scheduler_id=? AND tag_id=?"); + ps2.setInt(1, scheduler_id); + ps2.setInt(2, user_tagid); + ps2.executeUpdate(); + + PreparedStatement ps1=this.connection().prepareStatement("INSERT INTO scheduler_tags(scheduler_id,tag_id) VALUES(?,?)"); + ps1.setInt(1, scheduler_id); + ps1.setInt(2, user_tagid); + ps1.executeUpdate(); + ps1.close(); + } + + + /** + * {@inheritDoc} + */ + public int addIfTagNotExist(String tagname) throws Exception { + + PreparedStatement ps=this.connection().prepareStatement("select * FROM tags WHERE tagname=?"); + ps.setString(1, tagname); + ResultSet rs=ps.executeQuery(); + int id=0; + if(rs.next()){ + id=rs.getInt("id"); + }else{ + PreparedStatement ps1=this.connection().prepareStatement("insert into tags(tagname) values(?)",Statement.RETURN_GENERATED_KEYS); + ps1.setString(1, tagname); + ps1.executeUpdate(); + ResultSet generatedKeys = ps1.getGeneratedKeys(); + if (generatedKeys.next()) { + id=generatedKeys.getInt(1); + } + } + return id; + } + + + /** + * {@inheritDoc} + */ + public Map getTaskEventActions(int scheduler_id,long trig_time) throws Exception{ + + String q="select field_shortname,val from scheduler_taskdata "; + q+=" where field_shortname like 'on%' "; + q+=" and scheduler_id=? "; + q+=" union all "; + q+=" select 'status' as field_shortname,status as val from scheduler_queuelogs "; + q+=" where scheduler_id=? and trigger_time =? "; + + PreparedStatement ps=this.connection().prepareStatement(q); + ps.setInt(1, scheduler_id); + ps.setInt(2, scheduler_id); + ps.setLong(3, trig_time); + ResultSet rs=ps.executeQuery(); + HashMap al=new HashMap(); + while(rs.next()){ + al.put(rs.getString(1),rs.getString(2)); + + } + return al; + } + + + /** + * {@inheritDoc} + */ + public String getErrorMessageEvenNull(int scheduler_id,long trig_time) throws Exception { + + String q="select b.messages from scheduler_queuelogs as a "; + q+="left outer join scheduler_logs as b on a.log_id=b.id "; + q+="where a.status='fail' "; + q+="and a.scheduler_id=? and a.trigger_time=? "; + + PreparedStatement ps=this.connection().prepareStatement(q); + ps.setInt(1, scheduler_id); + ps.setLong(2, trig_time); + + ResultSet rs=ps.executeQuery(); + + String rtn=null; + if(rs.next()){ + rtn=rs.getString("messages"); + + } + return rtn; + } + + + /* + public void pushAlarmMessage(String message) throws Exception { + PreparedStatement ps1=this.connection().prepareStatement("INSERT INTO tradingRef.stackAlarms(Date,Message,status) VALUES(NOW(),?,0)"); + ps1.setString(1, message); + ps1.executeUpdate(); + ps1.close(); + } + */ + + + /** + * {@inheritDoc} + */ + public void pushAlarmMessage(String db_name_trading_ref, String message) throws Exception { + PreparedStatement ps1=this.connection().prepareStatement("INSERT INTO " + db_name_trading_ref + ".stackAlarms(Date,Message,status) VALUES(NOW(),?,0)"); + ps1.setString(1, message); + ps1.executeUpdate(); + ps1.close(); + } + + + + /** + * {@inheritDoc} + */ + public List getDependsThis(int scheduler_id) throws Exception { + String query="select scheduler_id from scheduler_taskdata where field_shortname='dependentids' AND (val LIKE '"+scheduler_id+"' OR val LIKE '"+scheduler_id+",%' OR val LIKE '%,"+scheduler_id+",%' OR val LIKE '%,"+scheduler_id+"' )"; + + log.debug("query:"+query); + PreparedStatement ps=this.connection().prepareStatement(query); + ResultSet rs=ps.executeQuery(); + ArrayList ids=new ArrayList(); + while(rs.next()){ + int sid=rs.getInt("scheduler_id"); + ids.add(sid); + } + return ids; + } + + + /** + * {@inheritDoc} + */ + public List getDependsTo(int scheduler_id) throws Exception { + String query="select val from scheduler_taskdata where field_shortname='dependentids' AND scheduler_id = "+scheduler_id; + + log.debug("query:"+query); + PreparedStatement ps=this.connection().prepareStatement(query); + ResultSet rs=ps.executeQuery(); + ArrayList ids=new ArrayList(); + while(rs.next()){ + List vals = Arrays.asList(rs.getString("val").split("\\s*,\\s*")); + for (int i=0; i it=data.keySet().iterator();it.hasNext();){ + String ky=it.next(); + String val=(String)data.get(ky); + d_ps.setString(1, ky); + d_ps.executeUpdate(); + + i_ps.setString(1,ky); + i_ps.setString(2, val); + i_ps.executeUpdate(); + } + } + + + /** + * {@inheritDoc} + */ + public Map getTimeoutSettings() throws Exception { + + HashMap h=new HashMap(); + PreparedStatement d_ps=this.connection().prepareStatement("SELECT * FROM scheduler_timeout_settings"); + ResultSet rs=d_ps.executeQuery(); + + while(rs.next()){ + h.put(rs.getString("ky"), rs.getString("val")); + + } + return h; + } + + + /** + * {@inheritDoc} + */ + public ArrayList getLast10minuteSuspectedFailure() throws Exception { + + ArrayList al=new ArrayList(); + + StringBuffer sb = new StringBuffer(); + sb.append("select a.trigger_time,a.scheduler_id from scheduler_exeplanlogs as a "); + sb.append("left outer join scheduler_queuelogs as b on ( a.scheduler_id = b.scheduler_id and a.trigger_time = b.trigger_time) "); + sb.append("where a.trans_datetime between DATE_ADD(NOW(), interval -20 minute) and DATE_ADD(NOW(), interval -10 minute) "); + sb.append(" and (b.status is null) "); + sb.append(" order by trans_datetime desc "); + + PreparedStatement ps=this.connection().prepareStatement(sb.toString()); + + ResultSet rs=ps.executeQuery(); + while(rs.next()){ + //al.add(new BasicRowProcessor().toMap(rs)); + al.add(GeneralUtilDB.resultsetToMap(rs)); + } + rs.close(); + ps.close(); + return al; + } + + + /** + * {@inheritDoc} + */ + public boolean isAnyExecLogsInLast3Mins(int scheduler_id, long trigger_time) throws Exception { + + boolean found=false; + + StringBuffer sb = new StringBuffer(); + sb.append("select count(*) ct from scheduler_exeplanlogs "); + sb.append("WHERE trans_datetime > DATE_ADD(NOW(), interval -3 minute) AND scheduler_id=? AND trigger_time=? "); + + PreparedStatement ps=this.connection().prepareStatement(sb.toString()); + ps.setInt(1, scheduler_id); + ps.setLong(2, trigger_time); + ResultSet rs=ps.executeQuery(); + while(rs.next()){ + found=rs.getInt("ct")>0?true:false; + } + return found; + } + + + /** + * {@inheritDoc} + */ + public ArrayList getPeersList4Engine(List enginetypes) throws Exception { + + String eng=""; + for(String tg:enginetypes){ + eng+=(eng.equals(""))?"'"+tg+"'":",'"+tg+"'"; + } + + + String query="select * from peerslist "; + query+=" where peername in( "; + query+=" select distinct peername from scheduler_taskpeers "; + query+=" where taskuid in(select taskuid from scheduler_group where enginetype IN ("+eng+")) "; + query+=" ) "; + PreparedStatement ps=this.connection().prepareStatement(query); + // ps.setString(1, enginetype); + ResultSet rs=ps.executeQuery(); + ArrayList al=new ArrayList(); + while(rs.next()){ + //al.add(new BasicRowProcessor().toMap(rs)); + al.add(GeneralUtilDB.resultsetToMap(rs)); + } + return al; + } + + + /** + * {@inheritDoc} + */ + public void userPrefSaveOrUpdate(String login, String ky, String val) throws Exception { + + PreparedStatement ps=this.connection().prepareStatement("DELETE FROM user_preference WHERE login=? AND ky=?"); + ps.setString(1, login); + ps.setString(2, ky); + ps.executeUpdate(); + + log.debug("after deleting.."); + if(val!=null){ + + PreparedStatement ps1=this.connection().prepareStatement("INSERT INTO user_preference(login,ky,val) VALUES(?,?,?)"); + + ps1.setString(1, login); + ps1.setString(2, ky); + ps1.setString(3, val); + ps1.executeUpdate(); + log.debug("after inserting.."); + } + } + + + /** + * {@inheritDoc} + */ + public Map userPrefGetAll(String login) throws Exception { + PreparedStatement ps=this.connection().prepareStatement("SELECT ky,val FROM user_preference where login=?"); + ps.setString(1, login); + ResultSet rs=ps.executeQuery(); + TreeMap rtn=new TreeMap(); + while(rs.next()){ + rtn.put(rs.getString("ky"), rs.getString("val").toString()); + } + rs.close(); + ps.close(); + return rtn; + } + + + /** + * {@inheritDoc} + */ + public List getAutoCompleteXHR(String keyword) throws Exception { + + String theme=null; + + //System.out.println(" legnth:"+keyword.split(":").length); + + + if(keyword.contains(":") && keyword.split(":").length>1){ + theme=keyword.split(":")[0]; + keyword=keyword.split(":")[1]; + } + + if(keyword.contains(":") && keyword.split(":").length==1){ + theme=keyword.split(":")[0]; + keyword=null; + + } + + StringBuffer sb = new StringBuffer(); + sb.append("select a.name,a.id,b.folder_name,c.name as group_name,d.edited_datetime,d.username, c.icon, "); + sb.append("(select CONCAT(GROUP_CONCAT(cast(tags.tagname as CHAR(8000)) order by tags.tagname SEPARATOR ', '), ', ') from scheduler_tags as st "); + sb.append("left outer join tags on tags.id=st.tag_id "); + sb.append("WHERE st.scheduler_id=a.id "); + sb.append(") as stags "); + sb.append("from scheduler as a "); + sb.append("left outer join scheduler_folder as b on a.folder_id=b.id "); + sb.append("left outer join scheduler_group as c on a.taskuid=c.taskuid "); + sb.append("left outer join scheduler_editlogs as d on d.scheduler_id=a.id AND d.id = "); + sb.append("(select dd.id from scheduler_editlogs as dd where dd.scheduler_id= a.id order by dd.edited_datetime DESC limit 1) "); + sb.append("where ( (a.deleted is null OR a.deleted<>1) and c.active>=0 ) "); + if(keyword!=null){ + sb.append("and(a.name like '%"+keyword+"%' OR cast(a.id as char(10)) like '%"+keyword+"%' ) "); + } + if(theme!=null){ + sb.append("AND a.id in (select scheduler_id from scheduler_tags left outer join tags on scheduler_tags.tag_id = tags.id where tags.tagname='thm-"+theme.trim()+"' )" ); + } + sb.append("ORDER BY d.edited_datetime DESC "); + PreparedStatement ps=this.connection().prepareStatement(sb.toString()); + ResultSet rs=ps.executeQuery(); + ArrayList al=new ArrayList(); + while(rs.next()){ + //al.add(new BasicRowProcessor().toMap(rs)); + al.add(GeneralUtilDB.resultsetToMap(rs)); + } + return al; + } + + + /** + * {@inheritDoc} + */ + public List getFailedLast2Days(List themes) throws Exception { + + StringBuffer sbThemes = new StringBuffer(); + for (String t : themes) { + if (sbThemes.length() > 0) sbThemes.append(','); + sbThemes.append("'").append(t).append("'"); + } + String themeStr = sbThemes.toString(); + + StringBuffer sb = new StringBuffer(); + + if(false){ + sb.append("select count(*) as num_failed,scheduler_id,b.name as name,status,max(trigger_time) as last_trigger from bbsync.scheduler_queuelogs as a "); + sb.append("LEFT JOIN bbsync.scheduler as b on a.scheduler_id=b.id WHERE scheduler_id IN "); + sb.append("( select scheduler_id FROM bbsync.scheduler_tags WHERE tag_id IN "); + sb.append(" (select id from bbsync.tags WHERE tagname in (" + themeStr + ")) "); + sb.append(") AND trigger_datetime>=DATE_ADD(NOW(), INTERVAL -2 DAY) "); + sb.append("AND a.status IS NOT NULL AND a.status NOT IN ('success','overlapped','warning','re-executed') "); + sb.append("GROUP BY scheduler_id,b.name,status "); + sb.append("ORDER BY max(trigger_time) desc "); + + }else{ + + sb.append("select count(*) as num_failed,scheduler_id,b.name as name,status,max(trigger_time) as last_trigger from scheduler_queuelogs as a "); + sb.append("LEFT JOIN scheduler as b on a.scheduler_id=b.id WHERE scheduler_id IN "); + sb.append("( select scheduler_id FROM scheduler_tags WHERE tag_id IN "); + sb.append(" (select id from tags WHERE tagname in (" + themeStr + ")) "); + sb.append(") AND trigger_datetime>=DATE_ADD(NOW(), INTERVAL -2 DAY) "); + sb.append("AND a.status IS NOT NULL AND a.status NOT IN ('success','overlapped','warning','re-executed') "); + sb.append("GROUP BY scheduler_id,b.name,status "); + sb.append("ORDER BY max(trigger_time) desc "); + + } + PreparedStatement ps=this.connection().prepareStatement(sb.toString()); + + ResultSet rs=ps.executeQuery(); + ArrayList al=new ArrayList(); + while(rs.next()){ + //al.add(new BasicRowProcessor().toMap(rs)); + al.add(GeneralUtilDB.resultsetToMap(rs)); + } + + return al; + } + + + /** + * {@inheritDoc} + */ + public String getTop20IdNameByNameQuery(String keyword) throws Exception { + return "SELECT concat(concat(cast(id as char(10)),'|'),name) from scheduler where name like '%"+keyword+"%' limit 20"; + } + + + /** + * {@inheritDoc} + */ + public String getTop20IdNameByIdQuery(String keyword) throws Exception { + return "SELECT concat(concat(cast(id as char(10)),'|'),name) from scheduler where cast(id as char(10)) like '%"+keyword+"%' limit 20"; + } + + + /** + * {@inheritDoc} + */ + public String getHistoryQueueLogsDateQuery(long scheduelr_id, String stat) { + return " a.trigger_datetime>=DATE_ADD(NOW(), interval -2 day) AND a.scheduler_id="+scheduelr_id+" AND a.status='"+stat+"'"; + } + + +} diff --git a/lib-db/src/main/java/com/fourelementscapital/db/mariadb/UtilDBMariaDB.java b/lib-db/src/main/java/com/fourelementscapital/db/mariadb/UtilDBMariaDB.java new file mode 100755 index 0000000..4e3011a --- /dev/null +++ b/lib-db/src/main/java/com/fourelementscapital/db/mariadb/UtilDBMariaDB.java @@ -0,0 +1,1163 @@ +/****************************************************************************** +* +* 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.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.StringTokenizer; +import java.util.TreeMap; +import java.util.Vector; +import java.util.regex.Pattern; + +import org.apache.commons.dbutils.BasicRowProcessor; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.LogManager; + +import com.fourelementscapital.db.GeneralUtilDB; +import com.fourelementscapital.db.UtilDB; +import com.fourelementscapital.db.vo.ValueObject; + + +/** + * Util MySQL DAO implementation + */ +public class UtilDBMariaDB extends UtilDB{ + private Logger log=LogManager.getLogger(UtilDB.class.getName()); + + + /** + * {@inheritDoc} + */ + public List listCommodities(Connection con) throws Exception { + + Statement st=con.createStatement(); + String query="SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '"+getDb()+"' AND table_name = 'asset_master'"; + + ResultSet rs=st.executeQuery(query); + Vector rtn=new Vector(); + if(rs.next()){ + Statement st1=con.createStatement(); + ResultSet rs1=st1.executeQuery("Select distinct commodity from asset_master ORDER BY commodity"); + while(rs1.next()){ + rtn.add(rs1.getString("commodity")); + } + st1.close(); + rs1.close(); + } + rs.close(); + st.close(); + return rtn; + } + + + /** + * {@inheritDoc} + */ + public List listCommoditiesWithFilter(Connection con,String filter) throws Exception { + + Statement st=con.createStatement(); + + String query="SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '"+getDb()+"' AND table_name = 'asset_master'"; + ResultSet rs=st.executeQuery(query); + + Vector rtn=new Vector(); + if(rs.next()){ + Statement st1=con.createStatement(); + ResultSet rs1=st1.executeQuery("Select distinct commodity from asset_master where SUBSTRING (commodity , 1, 1) in ("+filter+") ORDER BY commodity"); + while(rs1.next()){ + rtn.add(rs1.getString("commodity")); + } + st1.close(); + rs1.close(); + } + rs.close(); + st.close(); + return rtn; + } + + + /** + * {@inheritDoc} + */ + public List listCommoditiesFirstLetters(Connection con) throws Exception { + + Statement st=con.createStatement(); + + //ResultSet rs=st.executeQuery("select * from sysobjects where id = object_id('asset_master') and OBJECTPROPERTY(id, 'IsUserTable') = 1"); + String query="SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '"+getDb()+"' AND table_name = 'asset_master'"; + ResultSet rs=st.executeQuery(query); + + Vector rtn=new Vector(); + if(rs.next()){ + Statement st1=con.createStatement(); + ResultSet rs1=st1.executeQuery("select distinct(SUBSTRING(UPPER(commodity) , 1, 1)) as l from asset_master ORDER BY l"); + //ResultSet rs1=st1.executeQuery("select top 15 * from (select distinct(SUBSTRING(commodity , 1, 1)) as l from asset_master) as a"); + while(rs1.next()){ + rtn.add(rs1.getString("l")); + } + st1.close(); + rs1.close(); + } + rs.close(); + st.close(); + return rtn; + } + + + /** + * {@inheritDoc} + */ + public List uniqueFields(Connection con) throws Exception { + + Vector rtn=new Vector(); + + + Statement st=con.createStatement(); + String query="SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '"+getDb()+"' AND table_name = 'asset_master'"; + ResultSet rs=st.executeQuery(query); + if(rs.next()){ + Statement st1=con.createStatement(); + ResultSet rs1=st1.executeQuery("select distinct commodity_fieldoriginal as field from asset_master"); + while(rs1.next()){ + rtn.add(rs1.getString("field")); + } + st1.close(); + rs1.close(); + } + st.close(); + rs.close(); + return rtn; + } + + + /** + * {@inheritDoc} + */ + public List listAllTables4Fields(Connection con, String field_arr, String commodity_arr ) throws Exception { + + Vector rtn=new Vector(); + Statement st1=con.createStatement(); + ResultSet rs1=st1.executeQuery("select * from asset_master where commodity_fieldoriginal IN ("+field_arr+") AND commodity IN ("+commodity_arr+")"); + + while(rs1.next()){ + rtn.add(new BasicRowProcessor().toMap(rs1)); + } + st1.close(); + rs1.close(); + return rtn; + } + + + /** + * {@inheritDoc} + */ + public List listAllOrphanedAssets(Connection con ) throws Exception { + + Vector rtn=new Vector(); + Statement st1=con.createStatement(); + //ResultSet rs1=st1.executeQuery("select * from asset_master where commodity_field not in(select name from sysobjects where name in (select commodity_field from asset_master) AND xtype='U')"); + ResultSet rs1=st1.executeQuery("select * from asset_master where commodity_field not in(select table_name from information_schema.tables where table_name in (select commodity_field from asset_master) and table_schema = '"+getDb()+"')"); + + while(rs1.next()){ + rtn.add(new BasicRowProcessor().toMap(rs1)); + } + st1.close(); + rs1.close(); + return rtn; + } + + + /** + * {@inheritDoc} + */ + public void removeAllOrphanedAssets(Connection con) throws Exception { + + Vector rtn=new Vector(); + Statement st1=con.createStatement(); + st1.executeUpdate("delete from asset_master WHERE commodity_field IN (select commodity_field from asset_master where commodity_field not in(select table_name from information_schema.tables where table_name in (select commodity_field from asset_master) and table_schema = '"+getDb()+"'))"); + st1.close(); + } + + + /** + * {@inheritDoc} + */ + public List listAllTables4Commodities(Connection con, String fieldtype,Vector commodities) throws Exception { + + Vector rtn=new Vector(); + PreparedStatement ps=con.prepareStatement("select * from asset_master WHERE commodity_fieldoriginal=(select commodity_fieldoriginal from asset_master WHERE commodity_field=? LIMIT 1) AND commodity=?"); + + for(String comms:commodities){ + ps.setString(1, fieldtype); + ps.setString(2, comms); + ResultSet rs1=ps.executeQuery(); + while(rs1.next()){ + //String com=rs1.getString("commodity"); + rtn.add(rs1.getString("commodity_field")); + } + rs1.close(); + } + ps.close(); + return rtn; + + + /* + Vector rtn=new Vector(); + Statement st1=con.createStatement(); + ResultSet rs1=st1.executeQuery("select * from asset_master where commodity_fieldoriginal=(select top 1 commodity_fieldoriginal from asset_master where commodity_field='"+fieldtype+"')"); + while(rs1.next()){ + String com=rs1.getString("commodity"); + if(commodities.contains(com)){ + rtn.add(rs1.getString("commodity_field")); + } + } + st1.close(); + rs1.close(); + return rtn; + */ + + } + + + /** + * {@inheritDoc} + */ + public String getFieldOriginal(Connection con,String tablename) throws Exception { + + Vector rtn=new Vector(); + PreparedStatement ps=con.prepareStatement("select commodity_fieldoriginal from asset_master WHERE commodity_field=?"); + ps.setString(1, tablename); + ResultSet rs1=ps.executeQuery(); + String fname=null; + if(rs1.next()){ + //String com=rs1.getString("commodity"); + fname=rs1.getString("commodity_fieldoriginal"); + } + rs1.close(); + + ps.close(); + return fname; + + + /* + Vector rtn=new Vector(); + Statement st1=con.createStatement(); + ResultSet rs1=st1.executeQuery("select * from asset_master where commodity_fieldoriginal=(select top 1 commodity_fieldoriginal from asset_master where commodity_field='"+fieldtype+"')"); + while(rs1.next()){ + String com=rs1.getString("commodity"); + if(commodities.contains(com)){ + rtn.add(rs1.getString("commodity_field")); + } + } + st1.close(); + rs1.close(); + return rtn; + */ + + } + + + /** + * {@inheritDoc} + */ + public Vector listAllTables4Commodities2(Connection con, String fieldtype,Vector commodities) throws Exception { + + Vector rtn=new Vector(); + PreparedStatement ps=con.prepareStatement("select * from asset_master WHERE commodity_fieldoriginal=(select commodity_fieldoriginal from asset_master WHERE commodity_field=? LIMIT 1) AND commodity=?"); + for(String comms:commodities){ + ps.setString(1, fieldtype); + ps.setString(2, comms); + ResultSet rs1=ps.executeQuery(); + while(rs1.next()){ + //String com=rs1.getString("commodity"); + ValueObject vo=new ValueObject(); + vo.setKey(comms); + vo.setValue(rs1.getString("commodity_field")); + rtn.add(vo); + } + rs1.close(); + } + ps.close(); + return rtn; + } + + + /** + * {@inheritDoc} + */ + //public List listAllContractTitles(Connection con, String jointables) throws Exception { + public TreeMap listAllContractTitles(Connection con, String jointables) throws Exception { + + TreeMap rtn=new TreeMap(); + Statement st1=con.createStatement(); + ResultSet rs1=st1.executeQuery("Select distinct contract,tbl_name FROM "+"("+jointables+") as outertable ORDER BY tbl_name"); + while(rs1.next()){ + String tbl=rs1.getString("tbl_name"); + Vector v; + if(rtn.get(tbl)==null){ + v=new Vector(); + rtn.put(tbl,v); + }else{ + v=rtn.get(tbl); + } + v.add(rs1.getString("contract")); + //rtn.add(rs1.getString("contract")); + } + st1.close(); + rs1.close(); + return rtn; + } + + + /** + * {@inheritDoc} + */ + //public List listAllContractTitles(Connection con, String jointables) throws Exception { + public TreeMap listAllContractTitles2(Connection con, String jointables) throws Exception { + + TreeMap rtn=new TreeMap(); + Statement st1=con.createStatement(); + ResultSet rs1=st1.executeQuery("Select distinct contract,tbl_name FROM "+"("+jointables+") as outertable ORDER BY tbl_name"); + while(rs1.next()){ + String tbl=rs1.getString("tbl_name"); + Vector v; + if(rtn.get(tbl)==null){ + v=new Vector(); + rtn.put(tbl,v); + }else{ + v=rtn.get(tbl); + } + v.add(rs1.getString("contract")); + //rtn.add(rs1.getString("contract")); + } + st1.close(); + rs1.close(); + return rtn; + } + + + /** + * {@inheritDoc} + */ + public List listAllContractTitles2LCase(Connection con, String jointables) throws Exception { + + Vector rtn=new Vector(); + Statement st1=con.createStatement(); + ResultSet rs1=st1.executeQuery("Select distinct contract FROM "+"("+jointables+") as outertable"); + while(rs1.next()){ + rtn.add(rs1.getString("contract").toLowerCase()); + } + st1.close(); + rs1.close(); + return rtn; + } + + + /** + * {@inheritDoc} + */ + public boolean removeCommodityAndFields(Connection con,String commodity) throws Exception { + + + PreparedStatement ps=con.prepareStatement("SELECT commodity_field FROM asset_master WHERE commodity=?"); + ps.setString(1, commodity); + ResultSet rs=ps.executeQuery(); + while(rs.next()){ + + String field=rs.getString("commodity_field"); + + Statement st1=con.createStatement(); + st1.execute("DROP TABLE "+field+""); + st1.close(); + + String query="DELETE FROM asset_master WHERE commodity_field='"+field+"'"; + Statement st=con.createStatement(); + st.execute(query); + st.close(); + } + rs.close(); + ps.close(); + return true; + } + + + /** + * {@inheritDoc} + */ + public Vector getRawData(String datequery,Connection con,String tablename, String commodity) throws Exception { + datequery=GeneralUtilDB.convertFromSQL(datequery); + PreparedStatement ps=con.prepareStatement("SELECT *,DATE_FORMAT(cdate,'%d/%m/%Y') as cdate1 FROM "+tablename+" WHERE contract=? AND "+datequery+" ORDER BY CDATE DESC"); + ps.setString(1, commodity); + ResultSet rs=ps.executeQuery(); + Vector rtn=new Vector(); + while(rs.next()){ + rtn.add(new BasicRowProcessor().toMap(rs)); + + } + rs.close(); + ps.close(); + return rtn; + } + + + /** + * {@inheritDoc} + */ + public List getRecordCount4Contracts(Connection con, String commodity, String contract ) throws Exception { + + //String query="select 'select count(*),contract,'+char(39)+'cl'+char(39)+' from ['+commodity_field+'] group by contract' from asset_master where commodity='"+commodity+"' "; + String query="select 'select '+char(39)+char(39)+' as rw,'+char(39)+char(39)+' as field,'+char(39)+char(39) union all select 'UNION ALL select count(*) as rw,'+char(39)+commodity_fieldoriginal+char(39)+' as field,'+char(39)+'"+commodity+"'+char(39)+' from '+commodity_field+' where contract='+char(39)+'"+contract+"'+char(39)+' group by contract ' from asset_master where commodity='"+commodity+"'"; + Statement st=con.createStatement(); + ResultSet rs=st.executeQuery(query); + String query1=""; + while(rs.next()){ + query1+=" "+rs.getString(1); + } + //System.out.println("UtilDB:"+query); + //System.out.println("UtilDB:"+query1); + rs.close(); + st.close(); + + Vector rtn=new Vector(); + if(!query1.equals("")){ + + PreparedStatement ps1=con.prepareStatement(query1); + ResultSet rs1=ps1.executeQuery(); + while(rs1.next()){ + rtn.add(new BasicRowProcessor().toMap(rs1)); + + } + rs1.close(); + ps1.close(); + } + return rtn; + + + } + + + /** + * {@inheritDoc} + */ + public Map getRawData2Contract(String datequery,Connection con,String fieldname, String contrat,String commodity) throws Exception { + datequery=GeneralUtilDB.convertFromSQL(datequery); + log.debug("commodity:"+commodity+" fieldanme:"+fieldname); + TreeMap t=new TreeMap(); + Vector rtn=new Vector(); + PreparedStatement ps0=con.prepareStatement("select * from asset_master WHERE commodity=? AND commodity_fieldoriginal=?"); + ps0.setString(1, commodity); + ps0.setString(2, fieldname); + ResultSet rs0=ps0.executeQuery(); + if(rs0.next()){ + String fieldname_t=rs0.getString("commodity_field"); + PreparedStatement ps=con.prepareStatement("SELECT DATE_FORMAT(cdate,'%d/%m/%Y') as cdate1, * FROM "+fieldname_t+" WHERE contract=? AND "+datequery+" ORDER BY CDATE DESC"); + log.debug("query:SELECT CONVERT ( varchar(40),cdate,103 ) as cdate1, * FROM ["+fieldname_t+"] WHERE contract=? AND "+datequery+" ORDER BY CDATE DESC"); + ps.setString(1, contrat); + ResultSet rs=ps.executeQuery(); + + while(rs.next()){ + rtn.add(new BasicRowProcessor().toMap(rs)); + } + rs.close(); + ps.close(); + t.put("tablename",fieldname_t); + t.put("data", rtn); + } + rs0.close(); + ps0.close(); + + return t; + } + + + /** + * {@inheritDoc} + */ + public void deleteRawData(Connection con,String tablename,Map data) throws Exception { + + + PreparedStatement ps=con.prepareStatement("DELETE FROM "+tablename+" WHERE cdate=? AND contract=?"); + SimpleDateFormat format=new SimpleDateFormat("dd/MM/yyyy"); + java.sql.Date d1=new java.sql.Date(format.parse(data.get("cdate1")).getTime()); + ps.setDate(1,d1); + ps.setString(2,data.get("contract")); + ps.executeUpdate(); + ps.close(); + } + + + /** + * {@inheritDoc} + */ + public void updateRawData(Connection con,String tablename,Map olddata,Map newdata) throws Exception { + + + String query="UPDATE "+tablename+" SET cdate=?, contract=?,val=? WHERE cdate=? AND contract=?"; + String sval=null; + if(newdata.get("sval")!=null){ + sval=newdata.get("sval"); + }else{ + if(olddata.get("sval")!=null){ + sval=olddata.get("sval"); + } + } + if(sval!=null){ + query="UPDATE "+tablename+" SET cdate=?, contract=?,sval=? WHERE cdate=? AND contract=?"; + } + + PreparedStatement ps=con.prepareStatement(query); + SimpleDateFormat format=new SimpleDateFormat("dd/MM/yyyy"); + + java.sql.Date d0; + if(newdata.get("cdate1")!=null){ + d0=new java.sql.Date(format.parse(newdata.get("cdate1")).getTime()); + }else{ + d0=new java.sql.Date(format.parse(olddata.get("cdate1")).getTime()); + } + + double val; + if(newdata.get("val")!=null){ + val=Double.parseDouble(newdata.get("val")); + }else{ + if(sval==null){ + val=Double.parseDouble(olddata.get("val")); + } + val=0; + } + + + + + java.sql.Date d1=new java.sql.Date(format.parse(olddata.get("cdate1")).getTime()); + + + ps.setDate(1,d0); + ps.setString(2,(newdata.get("contract")!=null)?newdata.get("contract"):olddata.get("contract")); + if(val!=0){ + ps.setDouble(3,val); + }else{ + if(sval!=null){ + ps.setString(3,sval); + } + } + + ps.setDate(4,d1); + ps.setString(5,olddata.get("contract")); + ps.executeUpdate(); + ps.close(); + } + + + /** + * {@inheritDoc} + */ + public boolean removeFieldTable(Connection con,String ftable) throws Exception { + + String query="DELETE FROM asset_master WHERE commodity_field='"+ftable+"'"; + Statement st=con.createStatement(); + st.execute(query); + Statement st1=con.createStatement(); + st1.execute("DROP TABLE "+ftable); + st1.close(); + st.close(); + return true; + } + + + /** + * {@inheritDoc} + */ + public boolean renameFieldTable(Connection con,String ftable, String fieldoriginal) throws Exception { + + //String fieldtable=HistoricalMarketData.resolvedColName(com_sec1)+"_"+HistoricalMarketData.resolvedColName(dbfieldname); + + PreparedStatement ps1=con.prepareStatement("SELECT * FROM asset_master WHERE commodity_field=?"); + ps1.setString(1, ftable); + ResultSet rs1=ps1.executeQuery(); + String newtable=null; + if(rs1.next()){ + newtable=rs1.getString("commodity"); + newtable=resolvedColName(newtable)+"_"+resolvedColName(fieldoriginal); + } + if(newtable!=null){ + + Statement st=con.createStatement(); + //System.out.println("sp_rename '"+ftable+"','"+newtable+"'"); + st.executeUpdate("sp_rename '"+ftable+"','"+newtable+"'"); + + st.close(); + + //String query="UPDATE asset_master SET commodity_fieldoriginal=? WHERE commodity_field=?"; + //Statement st=con.createStatement(); + PreparedStatement ps=con.prepareStatement("UPDATE asset_master SET commodity_fieldoriginal=?,commodity_field=? WHERE commodity_field=?"); + ps.setString(1, fieldoriginal); + //ps.setString(2, ftable); + ps.setString(2, newtable); + ps.setString(3, ftable); + ps.executeUpdate(); + + //st1.execute("DROP TABLE [dbo].["+ftable+"]"); + ps.close(); + return true; + }else{ + return false; + } + + //st.close(); + //return true; +} + + + private String resolvedColName(String s) { + String rtn = null; + if (s != null && s.length() > 0) { + s=s.trim(); + int maxLeng = 25; + Pattern escaper = Pattern.compile("([^a-zA-z0-9])"); + s = s.replaceAll(" ", "_").toLowerCase(); + s = escaper.matcher(s).replaceAll(""); + s = s.substring(0, (s.length() > maxLeng) ? maxLeng : s.length()); + rtn = s.replaceAll(" ", "_").toLowerCase(); + } + return rtn; + } + + + /** + * {@inheritDoc} + */ + public List listFieldTables4Admin(Connection con,String mtable) throws Exception { + + Statement st1=con.createStatement(); + ResultSet rs1=st1.executeQuery("Select commodity_field,commodity_fieldoriginal from asset_master WHERE commodity='"+mtable+"'"); + String query=""; + int count=0; + while(rs1.next()){ + if(count>0){ + query+=" UNION ALL "; + } + count++; + query+="SELECT count(*) as recs,'"+rs1.getString("commodity_field")+"' as tabl,'"+rs1.getString("commodity_fieldoriginal")+"' as title FROM "+rs1.getString("commodity_field"); + } + st1.close(); + rs1.close(); + Vector rtn=new Vector(); + if(count>0){ + Statement st=con.createStatement(); + + ResultSet rs=st.executeQuery(query); + + while(rs.next()){ + TreeMap t=new TreeMap(); + t.put("total", rs.getObject("recs")); + t.put("ftable", rs.getObject("tabl")); + t.put("fname", rs.getObject("title")); + t.put("commodity", mtable); + rtn.add(t); + } + st.close(); + rs.close(); + } + return rtn; + } + + + /** + * {@inheritDoc} + */ + public List listFieldTables(Connection con,String mtable) throws Exception { + + Statement st=con.createStatement(); + + + + + //ResultSet rs=st.executeQuery("select * from sysobjects where id = object_id('asset_master') and OBJECTPROPERTY(id, 'IsUserTable') = 1"); + ResultSet rs=st.executeQuery("select table_name from information_schema.tables where table_name 'asset_master' and table_schema = '"+getDb()+"')"); + Vector rtn=new Vector(); + if(rs.next()){ + PreparedStatement st1=con.prepareStatement("Select * from asset_master where commodity=?"); + st1.setString(1,mtable); + ResultSet rs1=st1.executeQuery(); + while(rs1.next()){ + TreeMap rec=new TreeMap(); + rec.put("fieldname",rs1.getString("commodity_fieldoriginal")); + rec.put("fieldtable",rs1.getString("commodity_field")); + rtn.add(rec); + } + st1.close(); + rs1.close(); + } + rs.close(); + st.close(); + return rtn; + } + + + /** + * {@inheritDoc} + */ + public List listAllUniqueFields(Connection con) throws Exception { + + Statement st=con.createStatement(); + + //ResultSet rs=st.executeQuery("select * from sysobjects where id = object_id('asset_master') and OBJECTPROPERTY(id, 'IsUserTable') = 1"); + ResultSet rs=st.executeQuery("select table_name from information_schema.tables where table_name='asset_master' and table_schema = '"+getDb()+"'"); + Vector rtn=new Vector(); + if(rs.next()){ + Statement st1=con.createStatement(); + + ResultSet rs1=st1.executeQuery("select commodity_fieldoriginal,MAX(commodity),MAX(commodity_field) as commodity_field1 from asset_master group by commodity_fieldoriginal"); + while(rs1.next()){ + TreeMap rec=new TreeMap(); + rec.put("fieldname",rs1.getString("commodity_fieldoriginal")); + rec.put("fieldtable",rs1.getString("commodity_field1")); + rtn.add(rec); + } + st1.close(); + rs1.close(); + } + rs.close(); + st.close(); + + return rtn; + + + + } + + + + + /** + * {@inheritDoc} + */ + public void deleteContracts(Connection con, String field_tablename, String contract) throws Exception { + PreparedStatement ps=con.prepareStatement("delete from "+field_tablename+" WHERE contract=?"); + ps.setString(1, contract); + ps.executeUpdate(); + ps.close(); + } + + + /** + * {@inheritDoc} + */ + public Vector getUniqueContracts(Connection con, String field_tablename) throws Exception { + //PreparedStatement ps=con.prepareStatement("select distinct contract from ["+field_tablename+"]"); + //ps.setString(1, field_tablename); + PreparedStatement ps=con.prepareStatement("select * from (select contract,count(*) recs,min(cdate) as first,max(cdate) as last from "+field_tablename+" group by contract) as outtable order by last desc"); + + ResultSet rs=ps.executeQuery(); + Vector result=new Vector(); + + while(rs.next()){ + //result.add(rs.getString("contract")); + Map mp=new BasicRowProcessor().toMap(rs); + Date s1=(Date)mp.get("first"); + Date s2=(Date)mp.get("last"); + if(s1!=null && s2!=null){ + String dt=new SimpleDateFormat("dd MMM yyyy").format(s1)+" - "+new SimpleDateFormat("dd MMM yyyy").format(s2); + mp.put("dates",dt); + } + + result.add(mp); + } + rs.close(); + ps.close(); + return result; + } + + + /** + * {@inheritDoc} + */ + public Map listAllCommoditiesAndFields(Connection con) throws Exception { + TreeMap rtn=new TreeMap(); + + Statement st1=con.createStatement(); + //ResultSet rs1=st1.executeQuery("select * from sysobjects where id = object_id('asset_master') and OBJECTPROPERTY(id, 'IsUserTable') = 1"); + ResultSet rs1=st1.executeQuery("select table_name from information_schema.tables where table_name='asset_master' and table_schema = '"+getDb()+"'"); + + if(rs1.next()){ + Statement st=con.createStatement(); + ResultSet rs=st.executeQuery("select * from asset_master order by commodity"); + //String oldcommodity=null; + + while(rs.next()){ + String commodity=rs.getString("commodity"); + Vector records; + if(rtn.get(commodity)==null){ + records=new Vector(); + rtn.put(commodity,records); + }else{ + records=rtn.get(commodity); + } + ValueObject vo=new ValueObject(); + vo.setKey(rs.getString("commodity_field")); + vo.setValue(rs.getString("commodity_fieldoriginal")); + records.add(vo); + } + + } + st1.close(); + rs1.close(); + return rtn; + } + + + /** + * {@inheritDoc} + */ + public void LastSyncUpdate(Connection con) throws Exception { + + //String query="IF NOT EXISTS (select * from sysobjects where id = object_id('asset_sync') and OBJECTPROPERTY(id, 'IsUserTable') = 1) "; + //query+="BEGIN "; + String query="CREATE TABLE IF NOT EXISTS asset_sync ( "; + query+="syncdate datetime"; + query+=") "; + + + + Statement st=con.createStatement(); + st.execute(query); + st.close(); + + Statement st2=con.createStatement(); //.prepareStatement("INSERT into asset_sync(syncdate) VALUES(getdate()) "); + //st2.setDate(1, new java.sql.Date(date.getTime())); + st2.executeUpdate("INSERT into asset_sync(syncdate) VALUES(getdate()) "); + st2.close(); + } + + + /** + * {@inheritDoc} + */ + public int lastSyncDays(Connection con,Date today) throws Exception { + int rtn=-1; + Statement st=con.createStatement(); + //ResultSet rs=st.executeQuery("select * from sysobjects where id = object_id('asset_sync') and OBJECTPROPERTY(id, 'IsUserTable') = 1 "); + ResultSet rs=st.executeQuery("select table_name from information_schema.tables where table_name 'asset_sync' and table_schema = '"+getDb()+"')"); + + if(rs.next()){ + Statement st1=con.createStatement(); + ResultSet rs1=st1.executeQuery("select top 1 syncdate from asset_sync ORDER BY syncdate DESC"); + if(rs1.next()){ + java.sql.Date sdate=rs1.getDate("syncdate"); + Calendar tday=Calendar.getInstance(); + tday.setTime(today); + Calendar sday=Calendar.getInstance(); + sday.setTime(sdate); + rtn=daysBetween(sday, tday); + } + } + rs.close(); + st.close(); + return rtn; + } + + + /** + * {@inheritDoc} + */ + public Date lastSyncDate(Connection con) throws Exception { + Date rtn=null; + Statement st=con.createStatement(); + //ResultSet rs=st.executeQuery("select * from sysobjects where id = object_id('asset_sync') and OBJECTPROPERTY(id, 'IsUserTable') = 1 "); + ResultSet rs=st.executeQuery("select table_name from information_schema.tables where table_name 'asset_sync' and table_schema = '"+getDb()+"')"); + + if(rs.next()){ + Statement st1=con.createStatement(); + ResultSet rs1=st1.executeQuery("select top 1 syncdate from asset_sync ORDER BY syncdate DESC"); + if(rs1.next()){ + rtn=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse(rs1.getString("syncdate")); + } + } + rs.close(); + st.close(); + return rtn; + } + + + /** + * Calculate days between. Using Calendar - THE CORRECT WAY + * @param startDate start date + * @param endDate end date + * @return days + */ + private int daysBetween(Calendar startDate, Calendar endDate) { + Calendar date = (Calendar) startDate.clone(); + int daysBetween = 0; + while (date.before(endDate)) { + date.add(Calendar.DAY_OF_MONTH, 1); + daysBetween++; + } + return daysBetween; + } + + + /** + * {@inheritDoc} + */ + public TreeMap dailyStrategyData(Connection con,String date, String contractname, String fields) throws Exception { + + String q; + + StringTokenizer stt=new StringTokenizer(fields,"|"); + String fields1=""; + while(stt.hasMoreTokens()){ + String token=stt.nextToken(); + fields1+=fields1.equals("")? "'"+token+"'":",'"+token+"'"; + } + + if(!fields1.equals("")){ + fields1=" WHERE commodity_fieldoriginal IN ("+fields1+") "; + } + log.debug("fields1:"+fields1); + + if(date!=null){ + q="select 'select contract,val,sval,'+CHAR(39)+commodity+CHAR(39)+' as commodity,'+CHAR(39)+commodity_fieldoriginal+CHAR(39)+' as fieldname from '+commodity_field+' WHERE cdate='+CHAR(39)+'"+date+"'+CHAR(39)+' AND contract='+CHAR(39)+'"+contractname+"'+CHAR(39)+' union ' as q from asset_master "+fields1; + }else{ + q="select 'select top 1 contract,val,sval,'+CHAR(39)+commodity+CHAR(39)+' as commodity,'+CHAR(39)+commodity_fieldoriginal+CHAR(39)+' as fieldname from '+commodity_field+' WHERE cdate=(select max(cdate) from '+commodity_field+') AND contract='+CHAR(39)+'"+contractname+"'+CHAR(39)+' union ' as q from asset_master "+fields1; + date=""; + } + log.debug(q); + Statement st=con.createStatement(); + ResultSet rs=st.executeQuery(q); + String qr=""; + while(rs.next()){ + qr+=rs.getString("q"); + /* + rs.getDate("cdate"); + rs.getString("contract"); + rs.getDouble("val"); + rs.getString("commodity"); + rs.getString("fieldname"); + rs.getString("fieldcode"); + */ + } + if(!qr.equals("")){ + qr="select * from ("+qr+" select null,null,null,null,null) as t where commodity is not null order by contract,commodity"; + } + rs.close(); + st.close(); + + System.out.println("UtilDB.dailyStrategyData():qr:"+qr); + + TreeMap rows=new TreeMap(); + if(!qr.equals("")){ + Statement st1=con.createStatement(); + ResultSet rs1=st1.executeQuery(qr); + while(rs1.next()){ + String field=rs1.getString("fieldname"); + String ky=rs1.getString("commodity")+"|"+rs1.getString("contract"); + + + TreeMap data; + if(rows.get(field)!=null){ + data=(TreeMap)rows.get(field); + }else{ + data=new TreeMap(); + rows.put(field,data); + } + + if(rs1.getObject("sval")!=null){ + data.put(ky,rs1.getObject("sval")); + }else{ + data.put(ky,rs1.getDouble("val")); + } + // rs.getString("fieldname"); + + } + st1.close(); + rs1.close(); + } + return rows; + } + + + /** + * {@inheritDoc} + */ + public TreeMap listSavedChart(Connection con) throws Exception { + + TreeMap rtn=new TreeMap(); + + Statement st=con.createStatement(); + //ResultSet rs=st.executeQuery("select * from sysobjects where id = object_id('saved_charts') and OBJECTPROPERTY(id, 'IsUserTable') = 1"); + ResultSet rs=st.executeQuery("select table_name from information_schema.tables where table_name 'saved_charts' and table_schema = '"+getDb()+"')"); + if(rs.next()){ + Statement st1=con.createStatement(); + ResultSet rs1=st1.executeQuery("select id,chartname from saved_charts"); + while(rs1.next()){ + rtn.put(rs1.getInt("id"),rs1.getString("chartname")); + } + st1.close(); + rs1.close(); + } + st.close(); + rs.close(); + return rtn; + } + + + /** + * {@inheritDoc} + */ + public void deleteSavedChart(Connection con, int id) throws Exception { + PreparedStatement st1=con.prepareStatement("DELETE FROM saved_charts WHERE id=?" ); + st1.setInt(1, id); + st1.executeUpdate(); + st1.close(); + } + + + /** + * {@inheritDoc} + */ + public Map getSavedChartItem(Connection con, int id) throws Exception { + String query1="SELECT * FROM saved_charts WHERE id=?"; + PreparedStatement ps=con.prepareStatement(query1); + ps.setInt(1, id); + ResultSet rs=ps.executeQuery(); + TreeMap data=null; + if(rs.next()){ + data=new TreeMap(); + + data.put("chartname",rs.getString("chartname")); + data.put("datestring",rs.getString("datestring")); + data.put("source1",rs.getString("source1")); + data.put("security1",rs.getString("security1")); + data.put("field1",rs.getString("field1")); + data.put("source2",rs.getString("source2")); + data.put("security2",rs.getString("security2")); + data.put("field2",rs.getString("field2")); + data.put("commodity1",rs.getString("commodity1")); + data.put("commodity2",rs.getString("commodity2")); + } + rs.close(); + ps.close(); + return data; + } + + + /** + * {@inheritDoc} + */ + public void savedChart(Connection con,Map data) throws Exception { + + + ///String query="IF NOT EXISTS (select * from sysobjects where id = object_id('saved_charts') and OBJECTPROPERTY(id, 'IsUserTable') = 1) "; + //query+="BEGIN "; + String query="CREATE TABLE IF NOT EXISTS saved_charts ( "; + query+="id int IDENTITY, chartname varchar(250),datestring varchar(250),source1 varchar(50),security1 text,field1 varchar(250),source2 varchar(50),security2 text,field2 varchar(250),commodity1 varchar(250), commodity2 varchar(250)"; + query+=") "; + //query+="END "; + + Statement st=con.createStatement(); + st.execute(query); + st.close(); + + String query1="INSERT INTO saved_charts(chartname,datestring,source1,security1,field1,source2,security2,field2,commodity1,commodity2) VALUES(?,?,?,?,?,?,?,?,?,?)"; + PreparedStatement ps=con.prepareStatement(query1); + ps.setString(1, (String)data.get("chartname")); + ps.setString(2, (String)data.get("datestring")); + ps.setString(3, (String)data.get("source1")); + ps.setString(4, (String)data.get("security1")); + ps.setString(5, (String)data.get("field1")); + ps.setString(6, (String)data.get("source2")); + ps.setString(7, (String)data.get("security2")); + ps.setString(8, (String)data.get("field2")); + ps.setString(9, (String)data.get("commodity1")); + ps.setString(10, (String)data.get("commodity2")); + ps.executeUpdate(); + + ps.close(); + } + + + + + /** + * {@inheritDoc} + */ + @Override + public Map getRawData3(String datequery, Connection con, String tablename, + String commodity) throws Exception { + log.debug("commodity:"+commodity+" tablename:"+tablename); + TreeMap t=new TreeMap(); + Vector rtn=new Vector(); + PreparedStatement ps0=con.prepareStatement("select * from asset_master WHERE commodity=? AND commodity_field=?"); + ps0.setString(1, commodity); + ps0.setString(2, tablename); + ResultSet rs0=ps0.executeQuery(); + if(rs0.next()){ + //String fieldname_t=rs0.getString("commodity_field"); + PreparedStatement ps=con.prepareStatement("SELECT CONVERT ( varchar(40),cdate,103 ) as cdate1, * FROM ["+tablename+"] WHERE contract=? AND "+datequery+" ORDER BY CDATE DESC"); + //log.debug("query:SELECT CONVERT ( varchar(40),cdate,103 ) as cdate1, * FROM ["+fieldname_t+"] WHERE contract=? AND "+datequery+" ORDER BY CDATE DESC"); + ps.setString(1, commodity); + ResultSet rs=ps.executeQuery(); + + while(rs.next()){ + rtn.add(new BasicRowProcessor().toMap(rs)); + } + rs.close(); + ps.close(); + t.put("tablename",tablename); + t.put("data", rtn); + } + rs0.close(); + ps0.close(); + + + return t; + } + + + /** + * {@inheritDoc} + */ + @Override + public List listAllUniqueFields(Connection con, String commodity) + throws Exception { + // TODO Auto-generated method stub + return null; + } + + + + /** + * {@inheritDoc} + */ + @Override + public ArrayList showOldConnections(int minutesbefore, String computername) + throws Exception { + // TODO Auto-generated method stub + return null; + } + + + + /** + * {@inheritDoc} + */ + @Override + public void killConnections(List conids) throws Exception { + // TODO Auto-generated method stub + + } + +} diff --git a/lib-db/src/main/java/com/fourelementscapital/db/vo/BBSyncTrigger.java b/lib-db/src/main/java/com/fourelementscapital/db/vo/BBSyncTrigger.java new file mode 100755 index 0000000..046be0c --- /dev/null +++ b/lib-db/src/main/java/com/fourelementscapital/db/vo/BBSyncTrigger.java @@ -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; + } + + +} diff --git a/lib-db/src/main/java/com/fourelementscapital/db/vo/CommodityInfo.java b/lib-db/src/main/java/com/fourelementscapital/db/vo/CommodityInfo.java new file mode 100755 index 0000000..ead4c7c --- /dev/null +++ b/lib-db/src/main/java/com/fourelementscapital/db/vo/CommodityInfo.java @@ -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; + } + +} diff --git a/lib-db/src/main/java/com/fourelementscapital/db/vo/Contract.java b/lib-db/src/main/java/com/fourelementscapital/db/vo/Contract.java new file mode 100755 index 0000000..83f4357 --- /dev/null +++ b/lib-db/src/main/java/com/fourelementscapital/db/vo/Contract.java @@ -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; + } + + +} diff --git a/lib-db/src/main/java/com/fourelementscapital/db/vo/FlexiField.java b/lib-db/src/main/java/com/fourelementscapital/db/vo/FlexiField.java new file mode 100755 index 0000000..d8e1cd8 --- /dev/null +++ b/lib-db/src/main/java/com/fourelementscapital/db/vo/FlexiField.java @@ -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; + } +} diff --git a/lib-db/src/main/java/com/fourelementscapital/db/vo/PeerPackage.java b/lib-db/src/main/java/com/fourelementscapital/db/vo/PeerPackage.java new file mode 100755 index 0000000..68b08c2 --- /dev/null +++ b/lib-db/src/main/java/com/fourelementscapital/db/vo/PeerPackage.java @@ -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; + } + + +} diff --git a/lib-db/src/main/java/com/fourelementscapital/db/vo/SchedulerTrigger.java b/lib-db/src/main/java/com/fourelementscapital/db/vo/SchedulerTrigger.java new file mode 100755 index 0000000..213a168 --- /dev/null +++ b/lib-db/src/main/java/com/fourelementscapital/db/vo/SchedulerTrigger.java @@ -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; + } + + +} diff --git a/lib-db/src/main/java/com/fourelementscapital/db/vo/Strategy.java b/lib-db/src/main/java/com/fourelementscapital/db/vo/Strategy.java new file mode 100755 index 0000000..ea6c308 --- /dev/null +++ b/lib-db/src/main/java/com/fourelementscapital/db/vo/Strategy.java @@ -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; + } + + +} diff --git a/lib-db/src/main/java/com/fourelementscapital/db/vo/ValueObject.java b/lib-db/src/main/java/com/fourelementscapital/db/vo/ValueObject.java new file mode 100755 index 0000000..09c869e --- /dev/null +++ b/lib-db/src/main/java/com/fourelementscapital/db/vo/ValueObject.java @@ -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; + } + +} diff --git a/lib-db/src/main/resources/config_db.properties b/lib-db/src/main/resources/config_db.properties new file mode 100755 index 0000000..e0e6862 --- /dev/null +++ b/lib-db/src/main/resources/config_db.properties @@ -0,0 +1,2 @@ +db_close_timeout=20 +db_config_path=/mnt/public/Libs/.4E.config diff --git a/lib-db/src/main/resources/log4j2.xml b/lib-db/src/main/resources/log4j2.xml new file mode 100755 index 0000000..b8d69f3 --- /dev/null +++ b/lib-db/src/main/resources/log4j2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/lib-db/src/test/java/com/fourelementscapital/db/LibDbTest.java b/lib-db/src/test/java/com/fourelementscapital/db/LibDbTest.java new file mode 100755 index 0000000..a76c5a1 --- /dev/null +++ b/lib-db/src/test/java/com/fourelementscapital/db/LibDbTest.java @@ -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 m = new HashMap(); + 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 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 selectedFields = new ArrayList(); + selectedFields.add("Intan"); + ResultSet rs = dbm.getDatabase(tableName, selectedFields, null, null); + ArrayList al = new ArrayList(); + 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); + } + +} + + diff --git a/lib-db/src/test/java/com/fourelementscapital/db/VOTest.java b/lib-db/src/test/java/com/fourelementscapital/db/VOTest.java new file mode 100755 index 0000000..aaf4987 --- /dev/null +++ b/lib-db/src/test/java/com/fourelementscapital/db/VOTest.java @@ -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 ); + } + +} + + diff --git a/lib-fileutils/pom.xml b/lib-fileutils/pom.xml new file mode 100755 index 0000000..07eb24a --- /dev/null +++ b/lib-fileutils/pom.xml @@ -0,0 +1,29 @@ + + 4.0.0 + + + com.fourelementscapital + lib + ${revision} + + + com.fourelementscapital + lib-fileutils + jar + lib-fileutils + Common / general utilities + http://www.fourelementscapital.com + + + + org.apache.logging.log4j + log4j-api + + + commons-beanutils + commons-beanutils + + + + diff --git a/lib-fileutils/src/main/java/com/fourelementscapital/fileutils/BeanUtil.java b/lib-fileutils/src/main/java/com/fourelementscapital/fileutils/BeanUtil.java new file mode 100755 index 0000000..44eb98c --- /dev/null +++ b/lib-fileutils/src/main/java/com/fourelementscapital/fileutils/BeanUtil.java @@ -0,0 +1,138 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.fileutils; + +import java.beans.BeanInfo; +import java.beans.IntrospectionException; +import java.beans.Introspector; +import java.beans.PropertyDescriptor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.beanutils.ConversionException; +import org.apache.commons.beanutils.ConvertUtils; + +/** + * Utility class to convert bean to properties and vice versa. + */ +public class BeanUtil { + + /** + * Private constructor + */ + private BeanUtil(){ + } + + /** + * Converts a JavaBean to a collection of properties + * @param bean The JavaBean to convert + * @return A map of properties + */ + public static Map convertBeanToProperties(Object bean) + throws IntrospectionException, IllegalArgumentException, + IllegalAccessException, InvocationTargetException { + Map properties = new HashMap(); + BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass(), Object.class); + for (PropertyDescriptor p : beanInfo.getPropertyDescriptors()) { + String name = p.getName(); + Method reader = p.getReadMethod(); + if (reader != null) { + Object value = reader.invoke(bean); + properties.put(name, value); + } + } + return properties; + } + + /** + * Applies a collection of properties to a JavaBean. + * @param properties A map of the properties to set on the JavaBean + * @param bean The JavaBean to set the properties on + */ + public static void convertPropertiesToBean(Map properties, Object bean) + throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { + + BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass(), Object.class); + for (PropertyDescriptor p : beanInfo.getPropertyDescriptors()) { + String name = p.getName(); + Object value = properties.get(name); + Method reader = p.getReadMethod(); + Method writer = p.getWriteMethod(); + // we only care about "complete" properties + if (reader != null && writer != null) { + if (value != null) { + Class propertyType = writer.getParameterTypes()[0]; + Class valueType = value.getClass(); + if (!propertyType.isAssignableFrom(valueType)) { + // convert string input values to property type + try { + + if (valueType == String.class) { + value = ConvertUtils.convert((String) value, propertyType); + } else if (valueType == String[].class) { + value = ConvertUtils.convert((String[]) value, propertyType); + } else if (valueType == Object[].class) { + // best effort conversion + Object[] objectValues = (Object[]) value; + String[] stringValues = new String[objectValues.length]; + for (int i = 0; i < objectValues.length; i++) { + stringValues[i] = objectValues[i] == null ? + null : objectValues[i].toString(); + } + value = ConvertUtils.convert(stringValues, propertyType); + } + + } catch (ConversionException e) { + throw new IllegalArgumentException("Conversion failed for " + + "property '" + name + "' with value '" + value + "'", e); + } + } + // We only write values that are present in the map. This allows + // defaults or previously set values in the bean to be retained. + writer.invoke(bean, value); + } + } + } + + } + + /** + * Compares two JavaBeans for equality by comparing their properties and the class of the beans. + * @param bean1 Bean to compare + * @param bean2 Bean to compare + * @return True if {@code bean2} has the same properties with the same values as {@code bean1} and if they share the same class. + */ + public static boolean equals(Object bean1, Object bean2) + throws IntrospectionException, IllegalArgumentException, + IllegalAccessException, InvocationTargetException { + if (bean1.getClass() != bean2.getClass()) { + return false; + } + + BeanInfo bean1Info = + Introspector.getBeanInfo(bean1.getClass(), Object.class); + for (PropertyDescriptor p : bean1Info.getPropertyDescriptors()) { + String name = p.getName(); + Method reader = p.getReadMethod(); + if (reader != null) { + Object value1 = reader.invoke(bean1); + Object value2 = reader.invoke(bean2); + if ((value1 != null && !value1.equals(value2)) || + (value2 != null && !value2.equals(value1))) { + return false; + } + } + } + return true; + } + +} + + \ No newline at end of file diff --git a/lib-fileutils/src/main/java/com/fourelementscapital/fileutils/FindStringInFiles.java b/lib-fileutils/src/main/java/com/fourelementscapital/fileutils/FindStringInFiles.java new file mode 100755 index 0000000..aff4d88 --- /dev/null +++ b/lib-fileutils/src/main/java/com/fourelementscapital/fileutils/FindStringInFiles.java @@ -0,0 +1,88 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.fileutils; + +import java.io.BufferedReader; +import java.io.File; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * Utility class to find specific string in files. Only works in Unix environment. + */ +public class FindStringInFiles { + + /** + * Private constructor + */ + private FindStringInFiles(){ + } + + /** + * Search text + * @param word Text to search + * @param folder Folder where files contain string are located + * @param include_ext Include file extension of the file + * @return search result in Map + * @throws Exception + */ + public static Map search(String word, String folder, String include_ext) throws Exception { + + Process process=null; + Runtime runtime = Runtime.getRuntime(); + + String command[]={"grep","-i","-n","-r","--exclude=*.svn-base","--include=*"+include_ext,word, folder}; + process = runtime.exec(command); + BufferedReader br2 = new BufferedReader(new InputStreamReader(process.getInputStream())); + + LinkedHashMap rtn=new LinkedHashMap(); + + String line; + String content=""; + while ((line = br2.readLine()) != null) { + + if(line!=null && !line.trim().equals("") && line.contains(":") && line.split(":").length>2 ){ + String[] arg=line.split(":"); + String f,ln; + String occu=""; + + //windows + if(System.getProperty("os.name").toLowerCase().equals("freebsd")){ + f=arg[0]; + ln=arg[1]; + if(arg.length>=3){ + for(int i=2;i=4){ + for(int i=3;i=0)?",":"\r\n"; + } + + /** + * Split string into List + * @param str String to be split + * @return split string in list + */ + public static List split(String str){ + + List rtn=new ArrayList(); + if(str!=null && !str.equals("")){ + String pline=getSeparatorChar(str); + StringTokenizer st=new StringTokenizer(str,pline); + while(st.hasMoreTokens()){ + rtn.add(st.nextToken()); + } + } + return rtn; + } + + /** + * Split string then convert to lower case + * @param str String to be split + * @return split string in list + */ + public static List splitLowerCase(String str){ + + List rtn=new ArrayList(); + if(str!=null && !str.equals("")){ + String pline=getSeparatorChar(str); + StringTokenizer st=new StringTokenizer(str,pline); + while(st.hasMoreTokens()){ + rtn.add(st.nextToken().toLowerCase()); + } + } + return rtn; + } + + /** + * Split string then trim + * @param str String to be split + * @return split string in list + */ + public static List splitTrim(String str){ + + List rtn = new ArrayList(); + if(str!=null && !str.equals("")){ + String pline=getSeparatorChar(str); + StringTokenizer st=new StringTokenizer(str,pline); + while(st.hasMoreTokens()){ + rtn.add(st.nextToken().trim()); + } + } + return rtn; + } +} + + diff --git a/lib-fileutils/src/main/java/com/fourelementscapital/fileutils/StringPlaceHolder.java b/lib-fileutils/src/main/java/com/fourelementscapital/fileutils/StringPlaceHolder.java new file mode 100755 index 0000000..ea421bf --- /dev/null +++ b/lib-fileutils/src/main/java/com/fourelementscapital/fileutils/StringPlaceHolder.java @@ -0,0 +1,139 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.fileutils; + +import java.util.Map; +import java.util.Vector; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * This class provide function to replace 'place holder' in template with values. + * Example : 'Name : [[name]], Address : [[address]].' will become 'Name : Jon, Address : Bali.' + */ +public class StringPlaceHolder { + + public static final String PATTERN = "\\[\\[(.*?)\\]\\]"; // example : [[name]] + public static final String PATTERN_ATTR = "\\\"\\[\\[(.*?)\\]\\]\\\""; // example : \"[[name]]\" + + /** + * Private constructor + */ + private StringPlaceHolder() {} + + + /** + * Get elements (strings) in the template that will be replaced by another string. Use custom pattern. + * @param template Template string + * @param patternAttrStr pattern to find attribute + * @return attributes + */ + public static Vector getAttributePH(final String template, final String patternAttrStr){ + Vector v=new Vector(); + final StringBuffer sb = new StringBuffer(); + final Pattern pattern = + Pattern.compile(patternAttrStr, Pattern.DOTALL); + final Matcher matcher = pattern.matcher(template); + while(matcher.find()){ + final String key = matcher.group(1); + if(!v.contains(key)){ + v.add(key); + } + } + return v; + } + + + /** + * Overloading method of getAttributePH() with default pattern attribute values. + * @param template Template string + * @return attributes + */ + public static Vector getAttributePH(final String template) { + return getAttributePH(template, StringPlaceHolder.PATTERN_ATTR); + } + + + /** + * Get elements (strings) in the template that will be replaced by another string. Use custom pattern. + * The elements in the result found by getAttributePH() will be removed. + * @param template Template string + * @param patternAttrStr pattern to find attribute + * @return elements + */ + public static Vector getElementPH(final String template, final String patternStr){ + Vector v=new Vector(); + final StringBuffer sb = new StringBuffer(); + final Pattern pattern = + Pattern.compile(patternStr, Pattern.DOTALL); + final Matcher matcher = pattern.matcher(template); + while(matcher.find()){ + final String key = matcher.group(1); + if(!v.contains(key)){ + v.add(key); + } + } + v.removeAll(getAttributePH(template)); + return v; + } + + + /** + * Overloading method of getElementPH() with default pattern values. + * @param template Template string + * @return elements + */ + public static Vector getElementPH(final String template) { + return getElementPH(template, StringPlaceHolder.PATTERN); + } + + + /** + * Replace strings found by pattern in template with new input values. Use custom pattern. + * @param template Template string + * @param values Replacement values + * @param patternStr pattern to be replaced + * @return string from template with replaced strings + */ + public static String parse(final String template, final Map values, final String patternStr){ + + final StringBuffer sb = new StringBuffer(); + final Pattern pattern = + Pattern.compile(patternStr, Pattern.DOTALL); + final Matcher matcher = pattern.matcher(template); + while(matcher.find()){ + final String key = matcher.group(1); + if( values.get(key)!=null){ + final String replacement = values.get(key).toString(); + if(replacement == null){ + matcher.appendReplacement(sb, ""); + }else{ + matcher.appendReplacement(sb, replacement); + } + }else{ + matcher.appendReplacement(sb, ""); + } + } + matcher.appendTail(sb); + return sb.toString(); + } + + /** + * Overloading method of parse() with default pattern values. + * @param template Template string + * @param values Replacement values + * @return string from template with replaced strings + */ + public static String parse(final String template, final Map values){ + return parse(template, values, StringPlaceHolder.PATTERN); + } + + +} + + diff --git a/lib-fileutils/src/main/resources/log4j2.xml b/lib-fileutils/src/main/resources/log4j2.xml new file mode 100755 index 0000000..b8d69f3 --- /dev/null +++ b/lib-fileutils/src/main/resources/log4j2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/lib-fileutils/src/test/java/com/fourelementscapital/fileutils/LibFileUtilsTest.java b/lib-fileutils/src/test/java/com/fourelementscapital/fileutils/LibFileUtilsTest.java new file mode 100755 index 0000000..45c8e66 --- /dev/null +++ b/lib-fileutils/src/test/java/com/fourelementscapital/fileutils/LibFileUtilsTest.java @@ -0,0 +1,252 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.fileutils; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Vector; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import junit.framework.TestCase; + +/** + * lib-fileutils unit test + */ +public class LibFileUtilsTest extends TestCase { + + private static final Logger log = LogManager.getLogger(LibFileUtilsTest.class.getName()); + + /** + * Test BeanUtil functions + */ + public void testBeanUtil() { + log.debug(">>>>>> testBeanUtil()"); + + Map properties = new HashMap(); + properties.put("id", 1); + properties.put("desc", "description"); + + SampleBean bean1 = new SampleBean(); + + try { + + log.debug("convertPropertiesToBean() :"); + BeanUtil.convertPropertiesToBean(properties, bean1); + if (1 == bean1.getId() && "description".equals(bean1.getDesc())) { + log.debug("New value from bean. bean1.getId() : '" + + bean1.getId() + "', bean1.getDesc() : '" + + bean1.getDesc() + "'"); + assertTrue(true); + } + + log.debug("convertBeanToProperties() :"); + Map m = BeanUtil.convertBeanToProperties(bean1); + if (m != null) { + log.debug("New properties from bean conversion : " + m); + assertTrue(true); + } + + log.debug("equals() :"); + SampleBean bean2 = new SampleBean(); + BeanUtil.convertPropertiesToBean(properties, bean2); + log.debug("Is bean1 & bean2 equals : " + + BeanUtil.equals(bean1, bean2)); + + } catch (Exception e) { + e.printStackTrace(); + assertTrue(false); + } + } + + /** + * Bean class for testBeanUtilConvertPropertiesToBean() testing purpose + */ + private class SampleBean { + + private int id; + private String desc; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getDesc() { + return desc; + } + + public void setDesc(String desc) { + this.desc = desc; + } + + } + + /** + * Test FindStringInFiles.search(). Only works in unix environment. + */ + public void testFindStringInFilesSearch() { + log.debug(">>>>>> testFindStringInFilesSearch()"); + // Remove the comment tag to test. The code are commented out to prevent error on Windows OS when installing this lib : + /* + boolean isPass = false; + try { + + // search 'log4j' word in : './*.properties' : + String path = "./"; + String wordToSearch = "log4j"; String extension = ".properties"; + + Map r_result = FindStringInFiles.search(wordToSearch, path, extension); + if (r_result != null && r_result.size() > 0 ) { + log.debug("Search 'log4j' word in : './*.properties'. Result : " + + r_result.size() + " word(s) found."); isPass = true; + } + + } catch (Exception e) { + e.printStackTrace(); + } + assertTrue(isPass); + */ + assertTrue(true); + } + + /** + * Test InputStringTokenParser + */ + public void testInputStringTokenParser() { + log.debug(">>>>>> testInputStringTokenParser()"); + + log.debug("parseFreeTextTokens() :"); + boolean isPass = true; + String str = "Aaa,bBb,ccC"; + List a = SplitString.split(str); + if (a.size() == 3) { + if (!"Aaa".equals(a.get(0))) { + isPass = false; + } + ; + if (!"bBb".equals(a.get(1))) { + isPass = false; + } + ; + if (!"ccC".equals(a.get(2))) { + isPass = false; + } + ; + } else { + isPass = false; + } + log.debug("'" + str + "' is parsed to : " + a); + assertTrue(isPass); + + log.debug("parseFreeTextTokensLowerCase() :"); + isPass = true; + str = "Aaa,bBb,ccC"; + a = SplitString.splitLowerCase(str); + if (a.size() == 3) { + if (!"aaa".equals(a.get(0))) { + isPass = false; + } + ; + if (!"bbb".equals(a.get(1))) { + isPass = false; + } + ; + if (!"ccc".equals(a.get(2))) { + isPass = false; + } + ; + } else { + isPass = false; + } + log.debug("'" + str + "' is parsed to : " + a); + assertTrue(isPass); + + log.debug("parseFreeTextTokensTrim() :"); + isPass = true; + str = "Aaa bBb , ccC DDD "; + a = SplitString.splitTrim(str); + if (a.size() == 2) { + if (!"Aaa bBb".equals(a.get(0))) { + isPass = false; + } + ; + if (!"ccC DDD".equals(a.get(1))) { + isPass = false; + } + ; + } else { + isPass = false; + } + log.debug("'" + str + "' is parsed to : " + a); + assertTrue(isPass); + } + + /** + * Test ParseXMLPlaceHolder + */ + public void testParseXMLPlaceHolder() { + log.debug(">>>>>> testParseXMLPlaceHolder()"); + + boolean isPass = false; + + log.debug("parse() :"); + String template = "Name : [[name]], Address : [[address]]."; + Map m = new HashMap(); + m.put("name", "Jon"); + m.put("address", "Bali"); + String str = StringPlaceHolder.parse(template, m); + log.debug("parse result : '" + str + "'"); + if ("Name : Jon, Address : Bali.".equals(str)) { + isPass = true; + } + assertTrue(isPass); + + log.debug("getElementPH() :"); + template = "Name : \"[[name]]\", Address : [[address]]."; + Vector v = StringPlaceHolder.getElementPH(template); + log.debug("getElementPH result : " + v); + if (v != null && v.size() > 0) { + isPass = true; + } + assertTrue(isPass); + + log.debug("getAttributePH() :"); + template = "Name : \"[[name]]\", Address : [[address]]."; + v = StringPlaceHolder.getAttributePH(template); + log.debug("getAttributePH result : " + v); + if (v != null && v.size() > 0) { + isPass = true; + } + assertTrue(isPass); + } + + /** + * Test RandomString + */ + public void testRandomString() { + log.debug(">>>>>> testRandomString()"); + + String str = RandomString.getString(10); + log.debug("Random string result : '" + str + "'"); + + if (!"".equals(str)) { + assertTrue(true); + } else { + assertTrue(false); + } + } + +} + + diff --git a/lib-imonitor/ThemeEmails.xlsx b/lib-imonitor/ThemeEmails.xlsx new file mode 100755 index 0000000..21a64b1 Binary files /dev/null and b/lib-imonitor/ThemeEmails.xlsx differ diff --git a/lib-imonitor/code_examples/iMonitorClient/pom.xml b/lib-imonitor/code_examples/iMonitorClient/pom.xml new file mode 100755 index 0000000..9e7a91f --- /dev/null +++ b/lib-imonitor/code_examples/iMonitorClient/pom.xml @@ -0,0 +1,48 @@ + + 4.0.0 + com.fourelementscapital + iMonitorClient + 2.0 + jar + iMonitorClient + http://www.fourelementscapital.com + + + + maven-shade-plugin + 2.3 + + + package + + shade + + + + + + com.fourelementscapital.imonitor.App + + + false + + + + + + + + + junit + junit + 3.8.1 + test + + + com.fourelementscapital + lib-socket + 2.0 + + + \ No newline at end of file diff --git a/lib-imonitor/code_examples/iMonitorClient/src/main/java/com/fourelementscapital/imonitor/App.java b/lib-imonitor/code_examples/iMonitorClient/src/main/java/com/fourelementscapital/imonitor/App.java new file mode 100755 index 0000000..5c97650 --- /dev/null +++ b/lib-imonitor/code_examples/iMonitorClient/src/main/java/com/fourelementscapital/imonitor/App.java @@ -0,0 +1,53 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.imonitor; + +import com.fourelementscapital.socket.SocketClient; + +/** + * A com.fourelementscapital Class + */ +public class App { + + public static void main( String[] args ) + { + try { + + StringBuilder sb = new StringBuilder(); + sb.append("theme$#=computing,java"); + sb.append("~subject$#=This is just a test"); + sb.append("~body$#=This is {{{IMG}}} the email body"); + sb.append("~recipients$#=ari@4ecap.com"); + //sb.append("~recipients$#=surya@kronosinvestments.net,ari@4ecap.com"); + //sb.append("~emailAttachments$#=C:\\Ari\\_tmp\\test_email\\1.txt,C:\\Ari\\_tmp\\test_email\\2.txt"); + //sb.append("~emailImages$#=C:\\Users\\Public\\Pictures\\Sample Pictures\\Lighthouse.jpg"); + sb.append("~sayIt$#=true"); + sb.append("~emailIt$#=false"); + sb.append("~phoneCall$#=false"); + sb.append("~sms$#=FALSE"); + sb.append("~ym$#=FALSE"); + sb.append("~numbers$#=+65 8297 1508"); + //sb.append("~numbers$#=+6587825779899"); + sb.append("~alarmLevel$#=LOW"); + sb.append(""); + + String message = sb.toString(); + + for (int i=0; i<1; i++) { + SocketClient.sendMessage(message); + } + + System.out.println("Message is sent to socket server "); + } + catch (Exception e) { + e.printStackTrace(); + } + } +} + + \ No newline at end of file diff --git a/lib-imonitor/code_examples/iMonitorClient/src/main/resources/config_socket.properties b/lib-imonitor/code_examples/iMonitorClient/src/main/resources/config_socket.properties new file mode 100755 index 0000000..8a53ea3 --- /dev/null +++ b/lib-imonitor/code_examples/iMonitorClient/src/main/resources/config_socket.properties @@ -0,0 +1,8 @@ +socket_server_address=127.0.0.1 +#socket_server_address=10.153.64.10 +#socket_server_port_number=1777 +socket_server_port_number=1778 + + + + diff --git a/lib-imonitor/code_examples/iMonitorClient/src/main/resources/log4j2.xml b/lib-imonitor/code_examples/iMonitorClient/src/main/resources/log4j2.xml new file mode 100755 index 0000000..b8d69f3 --- /dev/null +++ b/lib-imonitor/code_examples/iMonitorClient/src/main/resources/log4j2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/lib-imonitor/code_examples/iMonitorClient/src/test/java/com/fourelementscapital/imonitor/AppTest.java b/lib-imonitor/code_examples/iMonitorClient/src/test/java/com/fourelementscapital/imonitor/AppTest.java new file mode 100755 index 0000000..948b017 --- /dev/null +++ b/lib-imonitor/code_examples/iMonitorClient/src/test/java/com/fourelementscapital/imonitor/AppTest.java @@ -0,0 +1,47 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.imonitor; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for App + */ +public class AppTest extends TestCase { + + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Empty Test + */ + public void testApp() + { + assertTrue( true ); + } + +} + + \ No newline at end of file diff --git a/lib-imonitor/code_examples/iMonitorServer/ThemeEmails.xlsx b/lib-imonitor/code_examples/iMonitorServer/ThemeEmails.xlsx new file mode 100755 index 0000000..21a64b1 Binary files /dev/null and b/lib-imonitor/code_examples/iMonitorServer/ThemeEmails.xlsx differ diff --git a/lib-imonitor/code_examples/iMonitorServer/pom.xml b/lib-imonitor/code_examples/iMonitorServer/pom.xml new file mode 100755 index 0000000..6bc88cb --- /dev/null +++ b/lib-imonitor/code_examples/iMonitorServer/pom.xml @@ -0,0 +1,68 @@ + + 4.0.0 + com.fourelementscapital + iMonitorServer + 2.0 + jar + iMonitorServer + http://www.fourelementscapital.com + + + + + maven-shade-plugin + 2.3 + + + package + + shade + + + + + + com.fourelementscapital.imonitor.App + + + false + + + + + + + + + ${project.basedir} + ${project.build.directory} + + ThemeEmails.xlsx + + + + ${project.basedir}/src/main/resources + ${project.build.directory}/classes + + *.* + + + + + + + + + junit + junit + 3.8.1 + test + + + com.fourelementscapital + lib-imonitor + 2.0 + + + \ No newline at end of file diff --git a/lib-imonitor/code_examples/iMonitorServer/src/main/java/com/fourelementscapital/imonitor/App.java b/lib-imonitor/code_examples/iMonitorServer/src/main/java/com/fourelementscapital/imonitor/App.java new file mode 100755 index 0000000..7054289 --- /dev/null +++ b/lib-imonitor/code_examples/iMonitorServer/src/main/java/com/fourelementscapital/imonitor/App.java @@ -0,0 +1,29 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.imonitor; + +import com.fourelementscapital.socket.SocketServer; + +/** + * A com.fourelementscapital Class + */ +public class App { + + public static void main( String[] args ) + { + IMonitorSocketProcess iMonitorSocketProcess = new IMonitorSocketProcess(); + try { + SocketServer.listen(iMonitorSocketProcess); + } + catch (Exception e) { + e.printStackTrace(); + } + } +} + + \ No newline at end of file diff --git a/lib-imonitor/code_examples/iMonitorServer/src/main/resources/config_email.properties b/lib-imonitor/code_examples/iMonitorServer/src/main/resources/config_email.properties new file mode 100755 index 0000000..6a29a0e --- /dev/null +++ b/lib-imonitor/code_examples/iMonitorServer/src/main/resources/config_email.properties @@ -0,0 +1,4 @@ +mail.smtp.auth=true +mail.smtp.starttls.enable=true +mail.smtp.host=smtp.gmail.com +mail.smtp.port=587 \ No newline at end of file diff --git a/lib-imonitor/code_examples/iMonitorServer/src/main/resources/config_imonitor.properties b/lib-imonitor/code_examples/iMonitorServer/src/main/resources/config_imonitor.properties new file mode 100755 index 0000000..f4932cc --- /dev/null +++ b/lib-imonitor/code_examples/iMonitorServer/src/main/resources/config_imonitor.properties @@ -0,0 +1,2 @@ +excel_file=/iMonitor2/ThemeEmails.xlsx +#excel_file=C:\\iMonitor2\\ThemeEmails.xlsx diff --git a/lib-imonitor/code_examples/iMonitorServer/src/main/resources/config_socket.properties b/lib-imonitor/code_examples/iMonitorServer/src/main/resources/config_socket.properties new file mode 100755 index 0000000..fd5c557 --- /dev/null +++ b/lib-imonitor/code_examples/iMonitorServer/src/main/resources/config_socket.properties @@ -0,0 +1,9 @@ +#socket_server_address=localhost +#socket_server_address=10.153.64.10 +#socket_server_address=10.153.64.4 +#socket_server_port_number=1777 +socket_server_port_number=1778 + + + + diff --git a/lib-imonitor/code_examples/iMonitorServer/src/main/resources/config_stackalarm.properties b/lib-imonitor/code_examples/iMonitorServer/src/main/resources/config_stackalarm.properties new file mode 100755 index 0000000..ff39261 --- /dev/null +++ b/lib-imonitor/code_examples/iMonitorServer/src/main/resources/config_stackalarm.properties @@ -0,0 +1,5 @@ +#database_infrastructure_connstring=jdbc:mysql://10.153.64.10:3306/infrastructure2?user=root&password=disaster +#database_infrastructure_driver=com.mysql.jdbc.Driver + +database_infrastructure_connstring=jdbc:mysql://192.168.1.98:3306/infrastructure?user=root&password=FOUR2008 +database_infrastructure_driver=com.mysql.jdbc.Driver diff --git a/lib-imonitor/code_examples/iMonitorServer/src/main/resources/config_tts.properties b/lib-imonitor/code_examples/iMonitorServer/src/main/resources/config_tts.properties new file mode 100755 index 0000000..b83dbbe --- /dev/null +++ b/lib-imonitor/code_examples/iMonitorServer/src/main/resources/config_tts.properties @@ -0,0 +1,2 @@ +#mbrola_path=C:\\iMonitor2\\mbrola\\ +mbrola_path=/usr/share/mbrola diff --git a/lib-imonitor/code_examples/iMonitorServer/src/main/resources/log4j2.xml b/lib-imonitor/code_examples/iMonitorServer/src/main/resources/log4j2.xml new file mode 100755 index 0000000..c94ffc9 --- /dev/null +++ b/lib-imonitor/code_examples/iMonitorServer/src/main/resources/log4j2.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/lib-imonitor/code_examples/iMonitorServer/src/test/java/com/fourelementscapital/imonitor/AppTest.java b/lib-imonitor/code_examples/iMonitorServer/src/test/java/com/fourelementscapital/imonitor/AppTest.java new file mode 100755 index 0000000..948b017 --- /dev/null +++ b/lib-imonitor/code_examples/iMonitorServer/src/test/java/com/fourelementscapital/imonitor/AppTest.java @@ -0,0 +1,47 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.imonitor; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for App + */ +public class AppTest extends TestCase { + + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Empty Test + */ + public void testApp() + { + assertTrue( true ); + } + +} + + \ No newline at end of file diff --git a/lib-imonitor/code_examples/unix_service/etc/init.d/iMonitorServ b/lib-imonitor/code_examples/unix_service/etc/init.d/iMonitorServ new file mode 100755 index 0000000..094bdcc --- /dev/null +++ b/lib-imonitor/code_examples/unix_service/etc/init.d/iMonitorServ @@ -0,0 +1,52 @@ +#!/bin/bash +SERVICE_NAME="I-Monitor Server" +PATH_TO_JAR="/tmp2/iMonitorServer-1.0-jar-with-dependencies.jar" +PID_PATH_NAME="/tmp2/iMonitorService-pid" +PID_PS="$(ps ax |grep 'iMonitorServer-' |grep java |awk '{print $1}')" +case $1 in + start) + echo "Starting $SERVICE_NAME ..." + if [ ! -f $PID_PATH_NAME ] || [ -z $PID_PS ]; then + #nohup java -jar $PATH_TO_JAR > /dev/null 2>&1 & echo $! > $PID_PATH_NAME + nohup java -jar $PATH_TO_JAR > /dev/null 2>&1 & + #nohup java -jar $PATH_TO_JAR & > /dev/null 2>&1 + echo $! > $PID_PATH_NAME; + echo "$SERVICE_NAME Started ..."; + else + echo "$SERVICE_NAME is already running! ..." + fi + ;; + stop) + if [ -n "$PID_PS" ]; then + #PID=$(cat $PID_PATH_NAME); + echo "Stopping $SERVICE_NAME ..." + kill -9 $PID_PS >/dev/null 2>&1; + echo "$SERVICE_NAME Stopped! ..." + rm -f $PID_PATH_NAME > /dev/null 2>&1 + else + echo "$SERVICE_NAME is not running ..." + fi + ;; + status) + if [ -n "$PID_PS" ]; then + echo "$SERVICE_NAME is Running ... PID: $PID_PS" + else + echo "$SERVICE_NAME is not running ..." + fi + ;; + restart) + if [ -f $PID_PATH_NAME ] || [ -n $PID_PS ]; then + #PID=$(cat $PID_PATH_NAME); + echo "Stopping $SERVICE_NAME ..."; + kill -9 $PID_PS >/dev/null 2>&1; + echo "$SERVICE_NAME stopped! ..."; + rm -f $PID_PATH_NAME > /dev/null 2>&1 + echo "Starting $SERVICE_NAME ..." + nohup java -jar $PATH_TO_JAR > /dev/null 2>&1 & + echo $! > $PID_PATH_NAME; + echo "$SERVICE_NAME Started! ..." + else + echo "$SERVICE_NAME is not running ..." + fi + ;; +esac diff --git a/lib-imonitor/pom.xml b/lib-imonitor/pom.xml new file mode 100755 index 0000000..2371027 --- /dev/null +++ b/lib-imonitor/pom.xml @@ -0,0 +1,99 @@ + + + 4.0.0 + + com.fourelementscapital + lib + ${revision} + + com.fourelementscapital + lib-imonitor + lib-imonitor + Receive message by socket and send to email, phone, say + http://www.fourelementscapital.com + + + + + ${project.basedir} + ${project.build.directory} + + ThemeEmails.xlsx + + + + ${project.basedir}/src/main/resources + ${project.build.directory}/classes + + *.* + + + + + + + + org.apache.logging.log4j + log4j-api + + + com.fourelementscapital + lib-socket + 2.0 + + + com.fourelementscapital + lib-tts + 2.0 + + + com.sun.mail + javax.mail + 1.5.6 + + + javax.activation + activation + 1.1 + + + org.apache.commons + commons-collections4 + 4.1 + + + + + org.apache.commons + commons-lang3 + 3.0 + + + + + commons-net + commons-net + 3.5 + + + mysql + mysql-connector-java + + + + + org.apache.poi + poi-ooxml + 3.15 + + + org.apache.poi + poi + 3.15 + + + + + + diff --git a/lib-imonitor/src/main/java/com/fourelementscapital/imonitor/Config.java b/lib-imonitor/src/main/java/com/fourelementscapital/imonitor/Config.java new file mode 100755 index 0000000..8c16d3c --- /dev/null +++ b/lib-imonitor/src/main/java/com/fourelementscapital/imonitor/Config.java @@ -0,0 +1,68 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.imonitor; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +public class Config { + + /** + * Get the value of a property defined in config file + * @param propertyName - the name or key of the property you want to retrieve + * @return the string value + * @throws IOException + */ + public static String getConfigValueImonitor(String propertyName) throws IOException + { + + Properties prop = new Properties(); + String propFileName = "config_imonitor.properties"; + + InputStream inputStream = Config.class.getClassLoader().getResourceAsStream(propFileName); + + if (inputStream != null) { + + prop.load(inputStream); + } else { + throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath"); + } + + // get the property value and print it out + String value = prop.getProperty(propertyName); + + return value; + } + + + public static String getConfigValueEmail(String propertyName) throws IOException + { + + Properties prop = new Properties(); + String propFileName = "config_email.properties"; + + InputStream inputStream = Config.class.getClassLoader().getResourceAsStream(propFileName); + + if (inputStream != null) { + + prop.load(inputStream); + } else { + throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath"); + } + + // get the property value and print it out + String value = prop.getProperty(propertyName); + + return value; + } + +} + + diff --git a/lib-imonitor/src/main/java/com/fourelementscapital/imonitor/IMonitor.java b/lib-imonitor/src/main/java/com/fourelementscapital/imonitor/IMonitor.java new file mode 100755 index 0000000..7fc4e1e --- /dev/null +++ b/lib-imonitor/src/main/java/com/fourelementscapital/imonitor/IMonitor.java @@ -0,0 +1,397 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.imonitor; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLEncoder; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Date; +import java.util.Iterator; +import java.util.List; +import java.util.regex.Pattern; + +import org.apache.commons.collections4.map.MultiValueMap; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import com.fourelementscapital.imonitor.email.Email; +import com.fourelementscapital.imonitor.email.EmailVO; +import com.fourelementscapital.imonitor.stackalarm.StackAlarm; +import com.fourelementscapital.imonitor.stackalarm.StackAlarmVO; +import com.fourelementscapital.tts.TTS; + +/** + * A com.fourelementscapital Class + */ +public class IMonitor { + + private static final Logger log = LogManager.getLogger(IMonitor.class.getName()); + + public static IMonitorVO processMessage(String message) throws Exception + { + + IMonitorVO iMonitorVO = new IMonitorVO(); + + iMonitorVO.setId("" + (new Date().getTime()) + (int)(Math.random() * 10000)); // to display id on log + log.debug(iMonitorVO.getId() + " - new"); + + if (message == null) return null; // return null if message = null + + message = message.replace("",""); // remove '' if exist + String[] strArr = message.split("~"); + + MultiValueMap multiMap = new MultiValueMap(); + if (strArr.length > 0) { + for (int i=0; i mapIterator = multiMap.keySet().iterator(); + while (mapIterator.hasNext()) { + String key = mapIterator.next(); + + Collection values = multiMap.getCollection(key); + + for(Iterator entryIterator = values.iterator(); entryIterator.hasNext();) { + String value = entryIterator.next(); + + switch (key.toLowerCase()) { + case "theme": + iMonitorVO.setTheme(Arrays.asList(value.split("\\s*,\\s*"))); + break; + case "alertlevel": + iMonitorVO.setAlertLevel(value); + break; + case "subject": + iMonitorVO.setSubject(value); + break; + case "body": + iMonitorVO.setBody(value); + break; + case "recipients": + iMonitorVO.setRecipients(Arrays.asList(value.split("\\s*,\\s*"))); + break; + case "emailcc": + iMonitorVO.setEmailCC(Arrays.asList(value.split("\\s*,\\s*"))); + break; + case "emailbcc": + iMonitorVO.setEmailBCC(Arrays.asList(value.split("\\s*,\\s*"))); + break; + case "emailattachments": + iMonitorVO.setAttachments(Arrays.asList(value.split("\\s*,\\s*"))); + break; + case "emailimages": + iMonitorVO.setImages(Arrays.asList(value.split("\\s*,\\s*"))); + break; + case "emailreplyto": + iMonitorVO.setEmailReplyTo(Arrays.asList(value.split("\\s*,\\s*"))); + break; + case "sayit": + iMonitorVO.setSayIt(Boolean.parseBoolean(value)); + break; + case "emailit": + iMonitorVO.setEmailIt(Boolean.parseBoolean(value)); + break; + case "phonecall": + iMonitorVO.setPhoneIt(Boolean.parseBoolean(value)); + break; + case "ym": + iMonitorVO.setYahooMessenger(Arrays.asList(value.split("\\s*,\\s*"))); + break; + case "sms": + iMonitorVO.setSmsIt(Boolean.parseBoolean(value)); + break; + case "escalation": + iMonitorVO.setEscalation(value); + break; + case "numbers": + iMonitorVO.setPhoneNumbers(Arrays.asList(value.split("\\s*,\\s*"))); + break; + } + } + } + + // use lib-stackalarm if recipients or numbers is not provided : + String theme = iMonitorVO.getTheme().get(0); // TODO: get only first theme. + + // TODO: alarm level escalation minutes by minutes + //String alarmLevel = iMonitorVO.getAlertLevel(); + String alarmLevel = (iMonitorVO.getAlertLevel() == null || "".equals(iMonitorVO.getAlertLevel())) ? "HIGH" : iMonitorVO.getAlertLevel(); + + boolean isGetRecipientsFromDb = false; + if (iMonitorVO.isEmailIt()) { + if (iMonitorVO.getRecipients() == null || iMonitorVO.getRecipients().size() == 0) { + isGetRecipientsFromDb = true; + } + } + if (iMonitorVO.isPhoneIt() || iMonitorVO.isSmsIt()) { + if (iMonitorVO.getPhoneNumbers() == null || iMonitorVO.getPhoneNumbers().size() == 0) { + isGetRecipientsFromDb = true; + } + } + + // TODO : temporary, bypass stack alarm : + isGetRecipientsFromDb = false; + + if (isGetRecipientsFromDb) { + StackAlarmVO stackAlarmVO = StackAlarm.collectRecipients(theme, alarmLevel); + + if (iMonitorVO.isEmailIt()) { + if (iMonitorVO.getRecipients() == null || iMonitorVO.getRecipients().size() == 0) { + List iMonitorVORecipients = new ArrayList(); + List stackAlarmVORecipients = stackAlarmVO.getRecipients(); + for (int i=0; i iMonitorVOPhoneNumbers = new ArrayList(); + List stackAlarmVOPhoneNumbers = stackAlarmVO.getPhoneNumbers(); + for (int i=0; i tmpRecipientsList = iMonitorVO.getRecipients(); + if (tmpRecipientsList != null && tmpRecipientsList.size() > 0) { + for (int i=0; i tmpPhoneNumberList = iMonitorVO.getPhoneNumbers(); + if (tmpPhoneNumberList != null && tmpPhoneNumberList.size() > 0) { + for (int i=0; i themeList = iMonitorVO.getTheme(); + String theme = "ialarm"; + if (themeList != null && themeList.size() > 0) { + theme = themeList.get(0); + } + List themeEmailList = ThemeEmail.readFromExcelFile(theme); // TODO : get first theme only ? + + String emailFrom = null; + String emailName = null; + String emailPassword = null; + if (themeEmailList.size() > 0) { + ThemeEmailVO themeEmailVO = (ThemeEmailVO) themeEmailList.get(0); // get first email only. should be 1 email per theme. + emailFrom = themeEmailVO.getEmail(); + emailName = themeEmailVO.getName(); + emailPassword = themeEmailVO.getPassword(); + } + + //log.debug("==== emailFrom : " + emailFrom); + //log.debug("==== emailName : " + emailName); + //log.debug("==== emailPassword : " + emailPassword); + + iMonitorVO.setEmailFrom(emailFrom); + iMonitorVO.setEmailFromName(emailName); + iMonitorVO.setEmailPassword(emailPassword); + + // to read recipients list from excel file + //iMonitorVO.setRecipients(Arrays.asList(emailFrom)); + + emailVO.setFrom(iMonitorVO.getEmailFrom()); + emailVO.setFromName(iMonitorVO.getEmailFromName()); + emailVO.setPassword(iMonitorVO.getEmailPassword()); + + String subject = iMonitorVO.isPhoneIt() ? "☎Phone: " + iMonitorVO.getSubject() : iMonitorVO.getSubject(); // add phone icon if send a phone call as well. + emailVO.setSubject(subject); + emailVO.setBody(iMonitorVO.getBody()); + + emailVO.setRecipient(iMonitorVO.getRecipients()); + emailVO.setRecipientCC(iMonitorVO.getEmailCC()); + emailVO.setRecipientBCC(iMonitorVO.getEmailBCC()); + emailVO.setReplyTo(iMonitorVO.getEmailReplyTo()); + + emailVO.setAttachmentPath(iMonitorVO.getAttachments()); + emailVO.setImages(iMonitorVO.getImages()); + + Email.send(emailVO); + } + + /** + * Process the alarm to do phone + * @param iMonitorVO IMonitor value object + * @throws Exception + */ + private static void processVOToPhone(IMonitorVO iMonitorVO) throws Exception { + + //String message = "ThisIsJustATest)"; + String message = URLEncoder.encode(iMonitorVO.getSubject() == null ? "" : iMonitorVO.getSubject(), "UTF-8"); + int maxDuration = 55; // in seconds + + List phoneNumbersList = iMonitorVO.getPhoneNumbers(); + + if (phoneNumbersList != null && phoneNumbersList.size() > 0) { + for (int i=0; i phoneNumbersList = iMonitorVO.getPhoneNumbers(); + + if (phoneNumbersList != null && phoneNumbersList.size() > 0) { + for (int i=0; i theme; + //private String sendingEmailStatus; + private String subject; + private List emailReplyTo; + private List emailBCC; + private List emailCC; + private List images; + private List attachments; + private boolean sayIt; + private boolean emailIt; + private boolean phoneIt; + private boolean smsIt; + private String alertLevel; + private List phoneNumbers; + private List recipients; + private List yahooMessenger; + private String escalation; + private String message; + private String body; + + private String emailFrom; + private String emailFromName; + private String emailPassword; + + + public String getId() { + return id; + } + public void setId(String id) { + this.id = id; + } + public List getTheme() { + return theme; + } + public void setTheme(List theme) { + this.theme = theme; + } +/* + public String getSendingEmailStatus() { + return sendingEmailStatus; + } + public void setSendingEmailStatus(String sendingEmailStatus) { + this.sendingEmailStatus = sendingEmailStatus; + } +*/ + public String getSubject() { + return subject; + } + public void setSubject(String subject) { + this.subject = subject; + } + public List getEmailReplyTo() { + return emailReplyTo; + } + public void setEmailReplyTo(List emailReplyTo) { + this.emailReplyTo = emailReplyTo; + } + public List getEmailBCC() { + return emailBCC; + } + public void setEmailBCC(List emailBCC) { + this.emailBCC = emailBCC; + } + public List getEmailCC() { + return emailCC; + } + public void setEmailCC(List emailCC) { + this.emailCC = emailCC; + } + public List getImages() { + return images; + } + public void setImages(List images) { + this.images = images; + } + public List getAttachments() { + return attachments; + } + public void setAttachments(List attachments) { + this.attachments = attachments; + } + public boolean isSayIt() { + return sayIt; + } + public void setSayIt(boolean sayIt) { + this.sayIt = sayIt; + } + public boolean isEmailIt() { + return emailIt; + } + public void setEmailIt(boolean emailIt) { + this.emailIt = emailIt; + } + public boolean isPhoneIt() { + return phoneIt; + } + public void setPhoneIt(boolean phoneIt) { + this.phoneIt = phoneIt; + } + public boolean isSmsIt() { + return smsIt; + } + public void setSmsIt(boolean smsIt) { + this.smsIt = smsIt; + } + public String getAlertLevel() { + return alertLevel; + } + public void setAlertLevel(String alertLevel) { + this.alertLevel = alertLevel; + } + public List getPhoneNumbers() { + return phoneNumbers; + } + public void setPhoneNumbers(List phoneNumbers) { + this.phoneNumbers = phoneNumbers; + } + public List getRecipients() { + return recipients; + } + public void setRecipients(List recipients) { + this.recipients = recipients; + } + public List getYahooMessenger() { + return yahooMessenger; + } + public void setYahooMessenger(List yahooMessenger) { + this.yahooMessenger = yahooMessenger; + } + public String getEscalation() { + return escalation; + } + public void setEscalation(String escalation) { + this.escalation = escalation; + } + public String getMessage() { + return message; + } + public void setMessage(String message) { + this.message = message; + } + public String getBody() { + return body; + } + public void setBody(String body) { + this.body = body; + } + + public String getEmailFrom() { + return emailFrom; + } + public void setEmailFrom(String emailFrom) { + this.emailFrom = emailFrom; + } + public String getEmailFromName() { + return emailFromName; + } + public void setEmailFromName(String emailFromName) { + this.emailFromName = emailFromName; + } + public String getEmailPassword() { + return emailPassword; + } + public void setEmailPassword(String emailPassword) { + this.emailPassword = emailPassword; + } + + + + +} + + diff --git a/lib-imonitor/src/main/java/com/fourelementscapital/imonitor/ThemeEmail.java b/lib-imonitor/src/main/java/com/fourelementscapital/imonitor/ThemeEmail.java new file mode 100755 index 0000000..93e387a --- /dev/null +++ b/lib-imonitor/src/main/java/com/fourelementscapital/imonitor/ThemeEmail.java @@ -0,0 +1,98 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.imonitor; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; + +public class ThemeEmail { + + private static Object getCellValue(Cell cell) { + switch (cell.getCellType()) { + case Cell.CELL_TYPE_STRING: + return cell.getStringCellValue(); + case Cell.CELL_TYPE_BOOLEAN: + return cell.getBooleanCellValue(); + case Cell.CELL_TYPE_NUMERIC: + return cell.getNumericCellValue(); + case Cell.CELL_TYPE_FORMULA: + switch(cell.getCachedFormulaResultType()) { + case Cell.CELL_TYPE_NUMERIC: + return cell.getNumericCellValue(); + case Cell.CELL_TYPE_STRING: + return (cell.getRichStringCellValue()).toString(); + } + } + return null; + } + + public static List readFromExcelFile(String themeSelected) throws IOException { + + List themeEmailSelected = new ArrayList(); + + List themeEmailsList = new ArrayList(); + FileInputStream inputStream = new FileInputStream(new File(Config.getConfigValueImonitor("excel_file"))); + + Workbook workbook = new XSSFWorkbook(inputStream); + Sheet firstSheet = workbook.getSheetAt(0); + Iterator iterator = firstSheet.iterator(); + + String themeIterate = ""; + while (iterator.hasNext()) { + Row nextRow = iterator.next(); + Iterator cellIterator = nextRow.cellIterator(); + ThemeEmailVO themeEmailVO = new ThemeEmailVO(); + + while (cellIterator.hasNext()) { + Cell nextCell = cellIterator.next(); + int columnIndex = nextCell.getColumnIndex(); + switch (columnIndex) { + case 0: + themeEmailVO.setEmail((String) getCellValue(nextCell)); + break; + case 1: + themeIterate = (String) getCellValue(nextCell); + themeEmailVO.setTheme(themeIterate); + break; + case 2: + themeEmailVO.setName((String) getCellValue(nextCell)); + break; + case 3: + themeEmailVO.setLastName((String) getCellValue(nextCell)); + break; + case 4: + themeEmailVO.setPassword((String) getCellValue(nextCell)); + break; + } + } + + if (themeSelected.equalsIgnoreCase(themeIterate)) { + themeEmailSelected.add(themeEmailVO); + } + + } + + workbook.close(); + inputStream.close(); + + return themeEmailSelected; + } + +} + + diff --git a/lib-imonitor/src/main/java/com/fourelementscapital/imonitor/ThemeEmailVO.java b/lib-imonitor/src/main/java/com/fourelementscapital/imonitor/ThemeEmailVO.java new file mode 100755 index 0000000..213edd7 --- /dev/null +++ b/lib-imonitor/src/main/java/com/fourelementscapital/imonitor/ThemeEmailVO.java @@ -0,0 +1,53 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.imonitor; + +public class ThemeEmailVO { + + private String email; + private String theme; + private String name; + private String lastName; + private String password; + + public String getEmail() { + return email; + } + public void setEmail(String email) { + this.email = email; + } + public String getTheme() { + return theme; + } + public void setTheme(String theme) { + this.theme = theme; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getLastName() { + return lastName; + } + public void setLastName(String lastName) { + this.lastName = lastName; + } + public String getPassword() { + return password; + } + public void setPassword(String password) { + this.password = password; + } + + + +} + + diff --git a/lib-imonitor/src/main/java/com/fourelementscapital/imonitor/email/Email.java b/lib-imonitor/src/main/java/com/fourelementscapital/imonitor/email/Email.java new file mode 100755 index 0000000..dffd28e --- /dev/null +++ b/lib-imonitor/src/main/java/com/fourelementscapital/imonitor/email/Email.java @@ -0,0 +1,157 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.imonitor.email; + +import java.io.IOException; +import java.util.List; +import java.util.Properties; + +import javax.activation.DataHandler; +import javax.activation.DataSource; +import javax.activation.FileDataSource; +import javax.mail.BodyPart; +import javax.mail.Message; +import javax.mail.MessagingException; +import javax.mail.Multipart; +import javax.mail.PasswordAuthentication; +import javax.mail.Session; +import javax.mail.Transport; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeBodyPart; +import javax.mail.internet.MimeMessage; +import javax.mail.internet.MimeMultipart; + +import org.apache.commons.lang3.StringUtils; + +import com.fourelementscapital.imonitor.Config; +/** + * A java class to compose and send email + */ +public class Email { + + /** + * Send email + * @param emailVO Email value object + * @throws IOException + */ + public static void send(EmailVO emailVO) throws Exception { + + final String inputFrom = Config.getConfigValueImonitor("email_from"); + final String inputPassword = Config.getConfigValueImonitor("email_password"); + final String inputRecipient = StringUtils.join(emailVO.getRecipient(), ","); + final String inputRecipientCC = StringUtils.join(emailVO.getRecipientCC(), ","); + final String inputRecipientBCC = StringUtils.join(emailVO.getRecipientBCC(), ","); + final String inputReplyTo = StringUtils.join(emailVO.getReplyTo(), ","); + + Properties props = new Properties(); + + props.put("mail.smtp.auth", Config.getConfigValueEmail("mail.smtp.auth")); + props.put("mail.smtp.starttls.enable", Config.getConfigValueEmail("mail.smtp.starttls.enable")); + props.put("mail.smtp.host", Config.getConfigValueEmail("mail.smtp.host")); + props.put("mail.smtp.port", Config.getConfigValueEmail("mail.smtp.port")); + + Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(inputFrom,inputPassword); + } + }); + + try { + + if (inputRecipient == null || "".equals(inputRecipient)) { + throw new Exception("There's no email recipient"); + } + + Message message = new MimeMessage(session); + message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(inputRecipient)); + // set cc if not null + if (inputRecipientCC != null && "".equals(inputRecipientCC.trim())) { + message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(inputRecipientCC)); + } + // set bcc if not null + if (inputRecipientBCC != null && "".equals(inputRecipientCC.trim())) { + message.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(inputRecipientBCC)); + } + // set replyTo if not null + if (inputReplyTo != null && "".equals(inputReplyTo.trim())) { + message.setReplyTo(InternetAddress.parse(inputReplyTo)); + } + message.setSubject(emailVO.getSubject()); + message.setFrom(new InternetAddress(inputFrom, emailVO.getFromName())); + + BodyPart messageBodyPart = new MimeBodyPart(); + + Multipart multipart = new MimeMultipart(); + + // set images : + List imageList = emailVO.getImages(); + String images = ""; + if (imageList != null && imageList.size() > 0) { + for (int i=0; i"; + } + } + + // set body with images in it + String body = emailVO.getBody(); + + // if body is null then insert "" string to prevent error + if (body == null) { + body = ""; + } + + if (body.contains("{{{IMG}}}")) { + body = body.replace("{{{IMG}}}", images); + } + else { + body += "
" + images; + } + messageBodyPart.setContent(body, "text/html"); + multipart.addBodyPart(messageBodyPart); + + // set attachments : + List attachmentPathList = emailVO.getAttachmentPath(); + + if (attachmentPathList != null && attachmentPathList.size() > 0) { + for (int i=0; i"); + multipart.addBodyPart(messageBodyPart); + } + +} + + \ No newline at end of file diff --git a/lib-imonitor/src/main/java/com/fourelementscapital/imonitor/email/EmailVO.java b/lib-imonitor/src/main/java/com/fourelementscapital/imonitor/email/EmailVO.java new file mode 100755 index 0000000..dc06149 --- /dev/null +++ b/lib-imonitor/src/main/java/com/fourelementscapital/imonitor/email/EmailVO.java @@ -0,0 +1,126 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.imonitor.email; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +/** + * Email Value Object + */ +public class EmailVO implements Serializable { + + private String from; + private String fromName; + private String password; + + private String subject; + private String body; + + private List recipient; + private List recipientCC; + private List recipientBCC; + private List replyTo; + + private List attachmentPath; + private List images; + + public String getFrom() { + return from; + } + + public void setFrom(String from) { + this.from = from; + } + + public String getFromName() { + return fromName; + } + + public void setFromName(String fromName) { + this.fromName = fromName; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getBody() { + return body; + } + + public void setBody(String body) { + this.body = body; + } + + public List getRecipient() { + return recipient; + } + + public void setRecipient(List recipient) { + this.recipient = recipient; + } + + public List getRecipientCC() { + return recipientCC; + } + + public void setRecipientCC(List recipientCC) { + this.recipientCC = recipientCC; + } + + public List getRecipientBCC() { + return recipientBCC; + } + + public void setRecipientBCC(List recipientBCC) { + this.recipientBCC = recipientBCC; + } + + public List getReplyTo() { + return replyTo; + } + + public void setReplyTo(List replyTo) { + this.replyTo = replyTo; + } + + public List getAttachmentPath() { + return attachmentPath; + } + + public void setAttachmentPath(List attachmentPath) { + this.attachmentPath = attachmentPath; + } + + public List getImages() { + return images; + } + + public void setImages(List images) { + this.images = images; + } + + +} + + + \ No newline at end of file diff --git a/lib-imonitor/src/main/java/com/fourelementscapital/imonitor/stackalarm/DBManager.java b/lib-imonitor/src/main/java/com/fourelementscapital/imonitor/stackalarm/DBManager.java new file mode 100755 index 0000000..767e333 --- /dev/null +++ b/lib-imonitor/src/main/java/com/fourelementscapital/imonitor/stackalarm/DBManager.java @@ -0,0 +1,152 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.imonitor.stackalarm; + +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.Map; + +/** + * The DBManager class provides an Access Interface between the 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. + */ +class DBManager { + + /** + * The database name for the current instance of DBManager + */ + private String dbName; + + /** + * The connection string for the database connection. Stored in config file + */ + 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 file if not found on this machine under resources folder + */ + public void connect() throws SQLException, ClassNotFoundException, IOException + { + + //String url = "jdbc:jtds:sqlserver://"+databaseServerAddress+";DatabaseName="+databaseName; + String driver = Utils.GetConfigValue("database_"+dbName.toLowerCase()+"_"+"driver"); + + Class.forName(driver); + + String connStr = Utils.GetConfigValue("database_"+dbName.toLowerCase()+"_"+"connstring"); + + System.out.println("+++++ " + connStr); + conn = DriverManager.getConnection(connStr); + connectionString = conn.toString(); + } + + /** + * Closes the current Database connection. + * @throws SQLException + */ + public void closeConnection() throws SQLException + { + if(!conn.isClosed()) + conn.close(); + if(resultSet!=null) + resultSet.close(); + } + + /** + * This function returns the result set of the SQL Query used by GetDatabase; + * @param query The SQL Query passed to it. + * @return + * @throws SQLException + */ + public 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; + } + + /** + * 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. new ArrayList(); + * be passed to select all fields (*) + * @param queryParams A hashmap containing the selection filters in the format [column_name]=[value] + * @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 selectedFields, Map 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 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); + } + +} + + diff --git a/lib-imonitor/src/main/java/com/fourelementscapital/imonitor/stackalarm/StackAlarm.java b/lib-imonitor/src/main/java/com/fourelementscapital/imonitor/stackalarm/StackAlarm.java new file mode 100755 index 0000000..ba330c5 --- /dev/null +++ b/lib-imonitor/src/main/java/com/fourelementscapital/imonitor/stackalarm/StackAlarm.java @@ -0,0 +1,90 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.imonitor.stackalarm; + +import java.sql.ResultSet; +import java.util.ArrayList; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +/** + * A class for parsing & process stack alarms + */ +public class StackAlarm { + + private static final Logger log = LogManager.getLogger(StackAlarm.class.getName()); + + /** + * Get recipients (email) & phone numbers + * @param theme Theme + * @param alarmLevel Alarm level (HIGH / MEDIUM /LOW) + * @return StackAlarmVO + */ + public static StackAlarmVO collectRecipients(String theme, String alarmLevel) throws Exception + { + + // GET RECIPIENTS : + + DBManager dbm = new DBManager("infrastructure"); + dbm.connect(); + + theme = theme.toLowerCase(); + + // get alarm level from VO + if (alarmLevel == null || "".equals(alarmLevel.trim())) { + throw new Exception("alarmLevel is null"); + } + if ("HIGH".equals(alarmLevel.toUpperCase())) { + alarmLevel = "b.Alarm_High"; + } + else if ("MEDIUM".equals(alarmLevel.toUpperCase())) { + alarmLevel = "b.Alarm_Medium"; + } + else if ("LOW".equals(alarmLevel.toUpperCase())) { + alarmLevel = "b.Alarm_Low"; + } + else { + throw new Exception("No alarm level found !"); + } + + //log.debug("theme : " + theme + " | alarmLevel : " + alarmLevel); + + StringBuilder sql = new StringBuilder(); + sql.append(" SELECT a.user, c.phonenumber, c.yahoo"); + sql.append(" FROM `tblTeamOrganization2` a left join tblRoleDefinition b on a." + theme + " = b.Letter "); + sql.append(" left join tblContactDetails c on a.user = c.username"); + sql.append(" WHERE " + alarmLevel + "='1'"); + + //System.out.println(">>> sql : " + sql); + + ResultSet rs = dbm.executeQuery(sql.toString()); + + StackAlarmVO stackAlarmVO = new StackAlarmVO(); + stackAlarmVO.setTheme(theme); + + ArrayList recipients = new ArrayList(); + ArrayList phoneNumbers = new ArrayList(); + + while(rs.next()){ + recipients.add(rs.getString("a.user") + "@4ecap.com"); + phoneNumbers.add(rs.getString("c.phonenumber")); + } + stackAlarmVO.setRecipients(recipients); + stackAlarmVO.setPhoneNumbers(phoneNumbers); + + //System.out.println(">>> recipients.size() : " + recipients.size()); + + return stackAlarmVO; + } + + + +} + + \ No newline at end of file diff --git a/lib-imonitor/src/main/java/com/fourelementscapital/imonitor/stackalarm/StackAlarmVO.java b/lib-imonitor/src/main/java/com/fourelementscapital/imonitor/stackalarm/StackAlarmVO.java new file mode 100755 index 0000000..01f9748 --- /dev/null +++ b/lib-imonitor/src/main/java/com/fourelementscapital/imonitor/stackalarm/StackAlarmVO.java @@ -0,0 +1,52 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.imonitor.stackalarm; + +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Timer; + +import javax.mail.Message; + +import org.apache.commons.net.smtp.SMTPClient; + +/** + * Stack Alarm Value Object + */ +public class StackAlarmVO { + + private String theme; + private List phoneNumbers; + private List recipients; + + public String getTheme() { + return theme; + } + public void setTheme(String theme) { + this.theme = theme; + } + public List getPhoneNumbers() { + return phoneNumbers; + } + public void setPhoneNumbers(List phoneNumbers) { + this.phoneNumbers = phoneNumbers; + } + public List getRecipients() { + return recipients; + } + public void setRecipients(List recipients) { + this.recipients = recipients; + } + + +} + + + \ No newline at end of file diff --git a/lib-imonitor/src/main/java/com/fourelementscapital/imonitor/stackalarm/Utils.java b/lib-imonitor/src/main/java/com/fourelementscapital/imonitor/stackalarm/Utils.java new file mode 100755 index 0000000..30febf9 --- /dev/null +++ b/lib-imonitor/src/main/java/com/fourelementscapital/imonitor/stackalarm/Utils.java @@ -0,0 +1,83 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.imonitor.stackalarm; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Date; +import java.util.Properties; + +/** + * This class contains Static Utility functions + */ +class Utils { + + /** + * 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 + */ + protected static String Join(ArrayList al,String delimiter) + { + return al.toString().replaceAll("\\[|\\]", "").replaceAll(", ",delimiter); + } + + /** + * Get the value of a property defined in config file + * @param propertyName - the name or key of the property you want to retrieve + * @return the string value + * @throws IOException + */ + protected static String GetConfigValue(String propertyName) throws IOException + { + Properties prop = new Properties(); + String propFileName = "config_stackalarm.properties"; + + InputStream inputStream = Utils.class.getClassLoader().getResourceAsStream(propFileName); + + if (inputStream != null) { + prop.load(inputStream); + } else { + throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath"); + } + + Date time = new Date(System.currentTimeMillis()); + + // get the property value and print it out + String value = prop.getProperty(propertyName); + return value; + } + + + /** + * Capitalize first char of a word. + * Example : 'this is text. another text' will be converted to 'This Is Text. Another Text' + * @param string + * @return string + */ + protected static String capitalizeString(String string) { + char[] chars = string.toLowerCase().toCharArray(); + boolean found = false; + for (int i = 0; i < chars.length; i++) { + if (!found && Character.isLetter(chars[i])) { + chars[i] = Character.toUpperCase(chars[i]); + found = true; + } else if (Character.isWhitespace(chars[i]) || chars[i]=='.' || chars[i]=='\'') { // You can add other chars here + found = false; + } + } + return String.valueOf(chars); + } + +} + + + \ No newline at end of file diff --git a/lib-imonitor/src/main/resources/config_email.properties b/lib-imonitor/src/main/resources/config_email.properties new file mode 100755 index 0000000..60365a3 --- /dev/null +++ b/lib-imonitor/src/main/resources/config_email.properties @@ -0,0 +1,4 @@ +mail.smtp.auth=true +mail.smtp.starttls.enable=true +mail.smtp.host=smtp.gmail.com +mail.smtp.port=587 diff --git a/lib-imonitor/src/main/resources/config_imonitor.properties b/lib-imonitor/src/main/resources/config_imonitor.properties new file mode 100755 index 0000000..14db4c5 --- /dev/null +++ b/lib-imonitor/src/main/resources/config_imonitor.properties @@ -0,0 +1,9 @@ +excel_file=/iMonitor2/ThemeEmails.xlsx +#excel_file=C:\\iMonitor2\\ThemeEmails.xlsx + +#Email configuration to send message via email +email_from=abc@gmail.com +email_password=abc2018 + + + diff --git a/lib-imonitor/src/main/resources/config_socket.properties b/lib-imonitor/src/main/resources/config_socket.properties new file mode 100755 index 0000000..3c682fd --- /dev/null +++ b/lib-imonitor/src/main/resources/config_socket.properties @@ -0,0 +1,11 @@ +socket_server_address=127.0.0.1 +#socket_server_address=10.153.64.10 +socket_server_port_number=1777 +#socket_server_port_number=1778 + +#socket_server_port_number=8080 +#socket_server_address=0.0.0.0 + + + + diff --git a/lib-imonitor/src/main/resources/config_stackalarm.properties b/lib-imonitor/src/main/resources/config_stackalarm.properties new file mode 100755 index 0000000..481ab5b --- /dev/null +++ b/lib-imonitor/src/main/resources/config_stackalarm.properties @@ -0,0 +1,2 @@ +database_infrastructure_connstring=jdbc:mysql://10.153.64.10:3306/infrastructure2?user=root&password=disaster +database_infrastructure_driver=com.mysql.jdbc.Driver diff --git a/lib-imonitor/src/main/resources/config_tts.properties b/lib-imonitor/src/main/resources/config_tts.properties new file mode 100755 index 0000000..b83dbbe --- /dev/null +++ b/lib-imonitor/src/main/resources/config_tts.properties @@ -0,0 +1,2 @@ +#mbrola_path=C:\\iMonitor2\\mbrola\\ +mbrola_path=/usr/share/mbrola diff --git a/lib-imonitor/src/test/java/com/fourelementscapital/imonitor/AppTest.java b/lib-imonitor/src/test/java/com/fourelementscapital/imonitor/AppTest.java new file mode 100755 index 0000000..8b9aec5 --- /dev/null +++ b/lib-imonitor/src/test/java/com/fourelementscapital/imonitor/AppTest.java @@ -0,0 +1,198 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.imonitor; + +import java.util.ArrayList; +import java.util.List; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import javax.swing.JOptionPane; + +import com.fourelementscapital.imonitor.email.EmailVO; +import com.fourelementscapital.imonitor.email.Email; +import com.fourelementscapital.imonitor.stackalarm.StackAlarmVO; +import com.fourelementscapital.imonitor.stackalarm.StackAlarm; + +/** + * Unit test for App + */ +public class AppTest extends TestCase { + + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Empty Test + */ + public void testApp() + { + assertTrue( true ); + } + + + + /** + * Test ProcessMessage(). Convert message string to ImonitorVO. + */ + public void testProcessMessage() + { + StringBuilder sb = new StringBuilder(); + sb.append("theme$#=computing,java"); + sb.append("~subject$#=This is just a test"); + sb.append("~body$#=This is {{{IMG}}} the email body"); + sb.append("~recipients$#=ari@4ecap.com"); + sb.append("~sayIt$#=true"); + sb.append("~emailIt$#=false"); + sb.append("~phoneCall$#=false"); + sb.append("~sms$#=FALSE"); + sb.append("~ym$#=FALSE"); + sb.append("~numbers$#=+65 8297 1508"); + sb.append("~alarmLevel$#=LOW"); + sb.append(""); + + String message = sb.toString(); + + try { + IMonitorVO iMonitorVO = IMonitor.processMessage(message); + if (iMonitorVO.isSayIt()) { + //JOptionPane.showMessageDialog(null, "IMonitor ProcessMessage done"); + assertTrue(true); + } + else { + assertTrue(false); + } + } + catch(Exception e) { + e.printStackTrace(); + assertTrue(false); + } + } + + + + /** + * Test Process(). Do email / phone / text to speech according to ImonitorVO value. + * Remove the comment tag to test. + * The code are commented out to prevent phone calling to +6287821107029 + */ + public void testProcess() + { + /* + try { + IMonitorVO iMonitorVO = new IMonitorVO(); + List phonenumbersList = new ArrayList(); + phonenumbersList.add("+6287821107029"); + iMonitorVO.setPhoneNumbers(phonenumbersList); + iMonitorVO.setPhoneIt(true); + iMonitorVO.setSubject("This is just a test"); + //IMonitor.process(iMonitorVO); + //JOptionPane.showMessageDialog(null, "IMonitor phone call done"); + assertTrue(true); + } + catch (Exception e) { + e.printStackTrace(); + assertTrue(false); + } + */ + assertTrue(true); + } + + + + /** + * Test send email + * Remove the comment tag to test after set vo.setFromName and vo.setPassword + * The code are commented out to prevent error when install this library + */ + public void testSendEmail() { + try { + EmailVO vo = new EmailVO(); + vo.setFrom("abc@gmail.com"); + vo.setFromName("abc"); + vo.setPassword("abc2018"); + + vo.setSubject("Testing Subject"); + vo.setBody("Dear GMail,
This is just a test."); + + ArrayList recipientList = new ArrayList(); + recipientList.add("intan@kronosinvestments.net"); + vo.setRecipient(recipientList); + + ArrayList recipientCCList = new ArrayList(); + recipientCCList.add("cc@gmail.com"); + recipientCCList.add("dd@gmail.com"); + vo.setRecipientCC(recipientCCList); + + ArrayList recipientBCCList = new ArrayList(); + recipientBCCList.add("bcc@gmail.com"); + recipientBCCList.add("bcc2@gmail.com"); + vo.setRecipientBCC(recipientBCCList); + + ArrayList replyToAL = new ArrayList(); + replyToAL.add("replyto@gmail.com"); + replyToAL.add("reply@yahoo.com"); + vo.setReplyTo(replyToAL); + + //ArrayList attachmentList = new ArrayList(); + //attachmentList.add("/home/intan/Documents/test_email.txt"); + //vo.setAttachmentPath(attachmentList); + + //ArrayList imagesList = new ArrayList(); + //imagesList.add("/home/intan/Documents/Selection_020.png"); + //vo.setImages(imagesList); + + //Email.send(vo); + //JOptionPane.showMessageDialog(null, "Email sent succesfully"); + assertTrue(true); + } catch (Exception e) { + e.printStackTrace(); + assertTrue(false); + } + } + + + /** + * Test parse alarm using StackAlarm + * Remove the comment tag to test + * The code are commnted out to prevent collect database data when install this lib + */ + public void testParseMessage() + { + try { + // Remove the comment tag to test. The code are commented out to prevent getting data from database (edit properties file first) : + //StackAlarmVO stackAlarmVO = StackAlarm.collectRecipients("comp", "HIGH"); + //JOptionPane.showMessageDialog(null, stackAlarmVO); + assertTrue( true ); + } + catch (Exception e) { + e.printStackTrace(); + assertTrue( false ); + } + + } + +} + + \ No newline at end of file diff --git a/lib-r/pom.xml b/lib-r/pom.xml new file mode 100755 index 0000000..f4c43ef --- /dev/null +++ b/lib-r/pom.xml @@ -0,0 +1,27 @@ + + + 4.0.0 + + com.fourelementscapital + lib + ${revision} + + com.fourelementscapital + lib-r + lib-r + a Java library used to send R code + http://www.fourelementscapital.com + + + + com.github.lucarosellini.rJava + JRI + + + org.rosuda.REngine + Rserve + + + + diff --git a/lib-r/src/main/java/com/fourelementscapital/r/TextConsole.java b/lib-r/src/main/java/com/fourelementscapital/r/TextConsole.java new file mode 100755 index 0000000..50b7831 --- /dev/null +++ b/lib-r/src/main/java/com/fourelementscapital/r/TextConsole.java @@ -0,0 +1,66 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.r; + +import java.awt.FileDialog; +import java.awt.Frame; +import java.io.BufferedReader; +import java.io.InputStreamReader; + +import org.rosuda.JRI.RMainLoopCallbacks; +import org.rosuda.JRI.Rengine; + +/** + * Text Console for Rengine + */ +public class TextConsole implements RMainLoopCallbacks +{ + public void rWriteConsole(Rengine re, String text, int oType) { + System.out.print(text); + } + + public void rBusy(Rengine re, int which) { + System.out.println("rBusy("+which+")"); + } + + public String rReadConsole(Rengine re, String prompt, int addToHistory) { + System.out.print(prompt); + try { + BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); + String s=br.readLine(); + return (s==null||s.length()==0)?s:s+"\n"; + } catch (Exception e) { + System.out.println("jriReadConsole exception: "+e.getMessage()); + } + return null; + } + + public void rShowMessage(Rengine re, String message) { + System.out.println("rShowMessage \""+message+"\""); + } + + public String rChooseFile(Rengine re, int newFile) { + FileDialog fd = new FileDialog(new Frame(), (newFile==0)?"Select a file":"Select a new file", (newFile==0)?FileDialog.LOAD:FileDialog.SAVE); + fd.show(); + String res=null; + if (fd.getDirectory()!=null) res=fd.getDirectory(); + if (fd.getFile()!=null) res=(res==null)?fd.getFile():(res+fd.getFile()); + return res; + } + + public void rFlushConsole (Rengine re) { + } + + public void rLoadHistory (Rengine re, String filename) { + } + + public void rSaveHistory (Rengine re, String filename) { + } +} + + diff --git a/lib-r/src/test/java/com/fourelementscapital/r/RServeTest.java b/lib-r/src/test/java/com/fourelementscapital/r/RServeTest.java new file mode 100755 index 0000000..6965f39 --- /dev/null +++ b/lib-r/src/test/java/com/fourelementscapital/r/RServeTest.java @@ -0,0 +1,815 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.r; + +import junit.framework.TestCase; + +import org.rosuda.REngine.REXP; +import org.rosuda.REngine.REXPDouble; +import org.rosuda.REngine.REXPFactor; +import org.rosuda.REngine.REXPGenericVector; +import org.rosuda.REngine.REXPInteger; +import org.rosuda.REngine.REXPList; +import org.rosuda.REngine.REXPLogical; +import org.rosuda.REngine.REXPMismatchException; +import org.rosuda.REngine.REXPRaw; +import org.rosuda.REngine.REXPSymbol; +import org.rosuda.REngine.REngine; +import org.rosuda.REngine.RFactor; +import org.rosuda.REngine.RList; +import org.rosuda.REngine.Rserve.RConnection; +import org.rosuda.REngine.Rserve.RserveException; + +/** + * Test Exception + */ +class TestException extends Exception { + public TestException(String msg) { super(msg); } +} + +/** + * RServe unit test + */ +public class RServeTest extends TestCase { + + /** + * Test very simple scenario + */ + public void testVerySimple() { + // Remove the comment tag to test. The code are commented out to prevent error when installing this lib (set up REngine in local machine first) : + /* + try { + RConnection c = new RConnection(); + double d[] = c.eval("rnorm(10)").asDoubles(); + c.close(); + assertTrue(d != null ); + } + catch (Exception e) { + e.printStackTrace(); + assertTrue(false); + } + */ + assertTrue(true); + } + + + /** + * Test string and list retrieval + */ + public void testString() { + // Remove the comment tag to test. The code are commented out to prevent error when installing this lib (set up REngine in local machine first) : + /* + try { + RConnection c = new RConnection(); + System.out.println(">>" + c.eval("R.version$version.string").asString() + "<<"); + System.out.println("* Test string and list retrieval"); + RList l = c.eval("{d=data.frame(\"huhu\",c(11:20)); lapply(d,as.character)}").asList(); + int cols = l.size(); + int rows = l.at(0).length(); + String[][] s = new String[cols][]; + for (int i=0; imean(iris[[1]])")); + // R knows about TRUE/FALSE/NA, so we cannot use boolean[] this way + // instead, we use int[] which is more convenient (and what R uses internally anyway) + int[] bi = x.asIntArray(); + { + int i = 0; while (i + 4.0.0 + com.test + testSocketClient + 1.0 + jar + testSocketClient + http://www.fourelementscapital.com + + + + + org.apache.maven.plugins + maven-jar-plugin + 3.0.2 + + + + true + lib/ + com.test.App + + + + + + maven-assembly-plugin + 2.6 + + + + true + com.test.App + + + + jar-with-dependencies + + + + + make-assembly + package + + single + + + + + + + + + junit + junit + 3.8.1 + test + + + + com.fourelementscapital + lib-socket + 1.0 + + + + \ No newline at end of file diff --git a/lib-socket/code_examples/testSocketClient/src/main/java/com/test/App.java b/lib-socket/code_examples/testSocketClient/src/main/java/com/test/App.java new file mode 100755 index 0000000..d6409c8 --- /dev/null +++ b/lib-socket/code_examples/testSocketClient/src/main/java/com/test/App.java @@ -0,0 +1,36 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.test; + +import com.fourelementscapital.socket.SocketClient; + +/** + * A com.fourelementscapital Class + */ +public class App { + + public static void main( String[] args ) + { + try { + + String message = "theme$#=itools~theme$#=computing~subject$#=test~body$#=-iMonitor~recipients$#=ari@4ecap.com~sayIt$#=FALSE~emailIt$#=TRUE~phoneCall$#=FALSE~ym$#=FALSE~sms$#=FALSE"; + + for (int i=0; i<5; i++) { + SocketClient.sendMessage(message); + } + + System.out.println("Message sent "); + + } + catch (Exception e) { + e.printStackTrace(); + } + } +} + + \ No newline at end of file diff --git a/lib-socket/code_examples/testSocketClient/src/main/resources/config_email.properties b/lib-socket/code_examples/testSocketClient/src/main/resources/config_email.properties new file mode 100755 index 0000000..6a29a0e --- /dev/null +++ b/lib-socket/code_examples/testSocketClient/src/main/resources/config_email.properties @@ -0,0 +1,4 @@ +mail.smtp.auth=true +mail.smtp.starttls.enable=true +mail.smtp.host=smtp.gmail.com +mail.smtp.port=587 \ No newline at end of file diff --git a/lib-socket/code_examples/testSocketClient/src/main/resources/config_socket.properties b/lib-socket/code_examples/testSocketClient/src/main/resources/config_socket.properties new file mode 100755 index 0000000..4b304ed --- /dev/null +++ b/lib-socket/code_examples/testSocketClient/src/main/resources/config_socket.properties @@ -0,0 +1,6 @@ +socket_server_address=localhost +socket_server_port_number=1777 + + + + diff --git a/lib-socket/code_examples/testSocketClient/src/main/resources/log4j2.xml b/lib-socket/code_examples/testSocketClient/src/main/resources/log4j2.xml new file mode 100755 index 0000000..b8d69f3 --- /dev/null +++ b/lib-socket/code_examples/testSocketClient/src/main/resources/log4j2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/lib-socket/code_examples/testSocketClient/src/test/java/com/test/AppTest.java b/lib-socket/code_examples/testSocketClient/src/test/java/com/test/AppTest.java new file mode 100755 index 0000000..41e8b43 --- /dev/null +++ b/lib-socket/code_examples/testSocketClient/src/test/java/com/test/AppTest.java @@ -0,0 +1,47 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.test; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for App + */ +public class AppTest extends TestCase { + + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Empty Test + */ + public void testApp() + { + assertTrue( true ); + } + +} + + \ No newline at end of file diff --git a/lib-socket/code_examples/testSocketServer/pom.xml b/lib-socket/code_examples/testSocketServer/pom.xml new file mode 100755 index 0000000..47e2723 --- /dev/null +++ b/lib-socket/code_examples/testSocketServer/pom.xml @@ -0,0 +1,68 @@ + + 4.0.0 + com.test + testSocketServer + 1.0 + jar + testSocketServer + http://www.fourelementscapital.com + + + + + org.apache.maven.plugins + maven-jar-plugin + 3.0.2 + + + + true + lib/ + com.test.App + + + + + + maven-assembly-plugin + 2.6 + + + + true + com.test.App + + + + jar-with-dependencies + + + + + make-assembly + package + + single + + + + + + + + + junit + junit + 3.8.1 + test + + + + com.fourelementscapital + lib-imonitor + 1.0 + + + + \ No newline at end of file diff --git a/lib-socket/code_examples/testSocketServer/src/main/java/com/test/App.java b/lib-socket/code_examples/testSocketServer/src/main/java/com/test/App.java new file mode 100755 index 0000000..fb101f4 --- /dev/null +++ b/lib-socket/code_examples/testSocketServer/src/main/java/com/test/App.java @@ -0,0 +1,32 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.test; + +import com.fourelementscapital.imonitor.IMonitorSocketProcess; +import com.fourelementscapital.socket.SocketServer; + +/** + * A com.fourelementscapital Class + */ +public class App { + + public static void main( String[] args ) + { + try { + + // Create StackAlarmSocketProcess object and injected to SocketServer so it will serve as StackAlarm listener : + IMonitorSocketProcess iMonitorSocketProcess = new IMonitorSocketProcess(); + SocketServer.listen(iMonitorSocketProcess); + } + catch (Exception e) { + e.printStackTrace(); + } + } +} + + \ No newline at end of file diff --git a/lib-socket/code_examples/testSocketServer/src/main/resources/config_email.properties b/lib-socket/code_examples/testSocketServer/src/main/resources/config_email.properties new file mode 100755 index 0000000..6a29a0e --- /dev/null +++ b/lib-socket/code_examples/testSocketServer/src/main/resources/config_email.properties @@ -0,0 +1,4 @@ +mail.smtp.auth=true +mail.smtp.starttls.enable=true +mail.smtp.host=smtp.gmail.com +mail.smtp.port=587 \ No newline at end of file diff --git a/lib-socket/code_examples/testSocketServer/src/main/resources/config_socket.properties b/lib-socket/code_examples/testSocketServer/src/main/resources/config_socket.properties new file mode 100755 index 0000000..4b304ed --- /dev/null +++ b/lib-socket/code_examples/testSocketServer/src/main/resources/config_socket.properties @@ -0,0 +1,6 @@ +socket_server_address=localhost +socket_server_port_number=1777 + + + + diff --git a/lib-socket/code_examples/testSocketServer/src/main/resources/log4j2.xml b/lib-socket/code_examples/testSocketServer/src/main/resources/log4j2.xml new file mode 100755 index 0000000..b8d69f3 --- /dev/null +++ b/lib-socket/code_examples/testSocketServer/src/main/resources/log4j2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/lib-socket/code_examples/testSocketServer/src/test/java/com/test/AppTest.java b/lib-socket/code_examples/testSocketServer/src/test/java/com/test/AppTest.java new file mode 100755 index 0000000..41e8b43 --- /dev/null +++ b/lib-socket/code_examples/testSocketServer/src/test/java/com/test/AppTest.java @@ -0,0 +1,47 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.test; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for App + */ +public class AppTest extends TestCase { + + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Empty Test + */ + public void testApp() + { + assertTrue( true ); + } + +} + + \ No newline at end of file diff --git a/lib-socket/pom.xml b/lib-socket/pom.xml new file mode 100755 index 0000000..ee5ab51 --- /dev/null +++ b/lib-socket/pom.xml @@ -0,0 +1,21 @@ + + + 4.0.0 + + com.fourelementscapital + lib + ${revision} + + com.fourelementscapital + lib-socket + lib-socket + a Java library containing socket server and client + http://www.fourelementscapital.com + + + org.apache.logging.log4j + log4j-api + + + diff --git a/lib-socket/src/main/java/com/fourelementscapital/socket/Config.java b/lib-socket/src/main/java/com/fourelementscapital/socket/Config.java new file mode 100755 index 0000000..1950d6a --- /dev/null +++ b/lib-socket/src/main/java/com/fourelementscapital/socket/Config.java @@ -0,0 +1,47 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.socket; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +/** + * A class to read config file + */ +class Config { + + /** + * Get the value of a property defined in config file + * @param propertyName - the name or key of the property you want to retrieve + * @return the string value + * @throws IOException + */ + protected static String getConfigValue(String propertyName) throws IOException + { + Properties prop = new Properties(); + String propFileName = "config_socket.properties"; + + InputStream inputStream = Config.class.getClassLoader().getResourceAsStream(propFileName); + + if (inputStream != null) { + prop.load(inputStream); + } else { + throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath"); + } + + // get the property value and print it out + String value = prop.getProperty(propertyName); + return value; + } + +} + + + \ No newline at end of file diff --git a/lib-socket/src/main/java/com/fourelementscapital/socket/SocketClient.java b/lib-socket/src/main/java/com/fourelementscapital/socket/SocketClient.java new file mode 100755 index 0000000..d517c0e --- /dev/null +++ b/lib-socket/src/main/java/com/fourelementscapital/socket/SocketClient.java @@ -0,0 +1,94 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.socket; + +import java.net.InetSocketAddress; +import java.net.SocketAddress; +import java.nio.ByteBuffer; +import java.nio.channels.AsynchronousSocketChannel; +import java.nio.channels.CompletionHandler; +import java.nio.charset.Charset; +import java.util.concurrent.Future; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +/** + * Socket Client to send message to Socket Server + */ +public class SocketClient { + + private static final Logger log = LogManager.getLogger(SocketClient.class.getName()); + + /** + * Send message to listening socket server + * @param message Message being sent + * @throws Exception + */ + public static void sendMessage(String message) throws Exception { + + AsynchronousSocketChannel channel = AsynchronousSocketChannel.open(); + String serverAddress = Config.getConfigValue("socket_server_address"); + int port = Integer.parseInt(Config.getConfigValue("socket_server_port_number")); + SocketAddress serverAddr = new InetSocketAddress(serverAddress, port); + Future result = channel.connect(serverAddr); + result.get(); + log.debug("Connected"); + AttachmentClient attach = new AttachmentClient(); + attach.channel = channel; + attach.buffer = ByteBuffer.allocate(2048); + + Charset cs = Charset.forName("UTF-8"); + byte[] data = message.getBytes(cs); + attach.buffer.put(data); + attach.buffer.flip(); + + ReadWriteHandlerClient readWriteHandler = new ReadWriteHandlerClient(); + channel.write(attach.buffer, attach, readWriteHandler); + } +} + +/** + * Class as value container to be passed among methods + */ +class AttachmentClient { + AsynchronousSocketChannel channel; + ByteBuffer buffer; +} + + +/** + * Class to read / write process + */ +class ReadWriteHandlerClient implements CompletionHandler { + + private static final Logger log = LogManager.getLogger(ReadWriteHandlerClient.class.getName()); + + /** + * An override method to be processed after operation has completed + * @param result Result + * @param attach AttachmentClient object + */ + @Override + public void completed(Integer result, AttachmentClient attach) { + // do process here + } + + /** + * An override method to be processed when operation fails + * @param e Throwable object + * @param attach AttachmentClient object + */ + @Override + public void failed(Throwable e, AttachmentClient attach) { + e.printStackTrace(); + } + +} + + diff --git a/lib-socket/src/main/java/com/fourelementscapital/socket/SocketProcess.java b/lib-socket/src/main/java/com/fourelementscapital/socket/SocketProcess.java new file mode 100755 index 0000000..db5f249 --- /dev/null +++ b/lib-socket/src/main/java/com/fourelementscapital/socket/SocketProcess.java @@ -0,0 +1,23 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.socket; + +/** + * An interface to be implemented to do a process then being injected to SocketServer. + */ +public interface SocketProcess { + + /** + * Run process + * @param strReadLine Message received by SocketServer to be processed further. + */ + void run(String strReadLine); + +} + + diff --git a/lib-socket/src/main/java/com/fourelementscapital/socket/SocketServer.java b/lib-socket/src/main/java/com/fourelementscapital/socket/SocketServer.java new file mode 100755 index 0000000..57cb2f0 --- /dev/null +++ b/lib-socket/src/main/java/com/fourelementscapital/socket/SocketServer.java @@ -0,0 +1,147 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.socket; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.SocketAddress; +import java.nio.ByteBuffer; +import java.nio.channels.AsynchronousServerSocketChannel; +import java.nio.channels.AsynchronousSocketChannel; +import java.nio.channels.CompletionHandler; +import java.nio.charset.Charset; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +/** + * Socket Server to listen & process client's message after receive it + */ +public class SocketServer { + + private static final Logger log = LogManager.getLogger(SocketServer.class.getName()); + + /** + * Listening message and process it. Use SocketProcess as dependency injection. + * @param socketProcess SocketProcess as dependency injection to process message + * @throws Exception + */ + public static void listen(SocketProcess socketProcess) throws Exception { + AsynchronousServerSocketChannel server = AsynchronousServerSocketChannel.open(); + String host = "0.0.0.0"; // all IPs available on this host + int port = Integer.parseInt(Config.getConfigValue("socket_server_port_number")); + InetSocketAddress sAddr = new InetSocketAddress(host, port); + server.bind(sAddr); + System.out.print(String.format("Server is listening at %s%n", sAddr)); // sysout, not log + AttachmentServer attach = new AttachmentServer(); + attach.server = server; + attach.socketProcess = socketProcess; + server.accept(attach, new ConnectionHandlerServer()); + Thread.currentThread().join(); + } +} + +/** + * Class as value container to be passed among methods + */ +class AttachmentServer { + AsynchronousServerSocketChannel server; + AsynchronousSocketChannel client; + ByteBuffer buffer; + SocketAddress clientAddr; + SocketProcess socketProcess; +} + + +/** + * Server connection handler + */ +class ConnectionHandlerServer implements CompletionHandler { + + private static final Logger log = LogManager.getLogger(ConnectionHandlerServer.class.getName()); + + private static final int BUFFER_ALLOCATED_IN_BYTE = 10000; // how many chars allowed in 1 socket message; + + /** + * An override method to be processed after operation has completed + * @param client AsynchronousSocketChannel object + * @param attach AttachmentServer object + */ + @Override + public void completed(AsynchronousSocketChannel client, AttachmentServer attach) { + try { + SocketAddress clientAddr = client.getRemoteAddress(); + System.out.print(String.format("Accepted connection from %s%n", clientAddr)); // sysout, not log + attach.server.accept(attach, this); + ReadWriteHandlerServer rwHandler = new ReadWriteHandlerServer(); + AttachmentServer newAttach = new AttachmentServer(); + newAttach.server = attach.server; + newAttach.client = client; + //newAttach.buffer = ByteBuffer.allocate(2048); + newAttach.buffer = ByteBuffer.allocate(BUFFER_ALLOCATED_IN_BYTE); + newAttach.clientAddr = clientAddr; + newAttach.socketProcess = attach.socketProcess; + client.read(newAttach.buffer, newAttach, rwHandler); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * An override method to be processed when operation fails + * @param e Throwable object + * @param attach AttachmentServer object + */ + @Override + public void failed(Throwable e, AttachmentServer attach) { + log.error("Failed to accept a connection."); + e.printStackTrace(); + } +} + + +/** + * Class to read / write process + */ +class ReadWriteHandlerServer implements CompletionHandler { + + private static final Logger log = LogManager.getLogger(ReadWriteHandlerServer.class.getName()); + + /** + * An override method to be processed after operation has completed + * @param result Result + * @param attach AttachmentServer object + */ + @Override + public void completed(Integer result, AttachmentServer attach) { + + attach.buffer.flip(); + int limits = attach.buffer.limit(); + byte bytes[] = new byte[limits]; + attach.buffer.get(bytes, 0, limits); + Charset cs = Charset.forName("UTF-8"); + String msg = new String(bytes, cs); + log.debug("SocketProcess.run() message : " + msg); + attach.socketProcess.run(msg); // run socket process + + attach.buffer.rewind(); + } + + /** + * An override method to be processed when operation fails + * @param e Throwable object + * @param attach AttachmentServer object + */ + @Override + public void failed(Throwable e, AttachmentServer attach) { + e.printStackTrace(); + } +} + + + \ No newline at end of file diff --git a/lib-socket/src/main/resources/config_socket.properties b/lib-socket/src/main/resources/config_socket.properties new file mode 100755 index 0000000..4b304ed --- /dev/null +++ b/lib-socket/src/main/resources/config_socket.properties @@ -0,0 +1,6 @@ +socket_server_address=localhost +socket_server_port_number=1777 + + + + diff --git a/lib-socket/src/main/resources/log4j2.xml b/lib-socket/src/main/resources/log4j2.xml new file mode 100755 index 0000000..b8d69f3 --- /dev/null +++ b/lib-socket/src/main/resources/log4j2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/lib-socket/src/test/java/com/fourelementscapital/socket/AppTest.java b/lib-socket/src/test/java/com/fourelementscapital/socket/AppTest.java new file mode 100755 index 0000000..1e09b8d --- /dev/null +++ b/lib-socket/src/test/java/com/fourelementscapital/socket/AppTest.java @@ -0,0 +1,81 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.socket; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for App + */ +public class AppTest extends TestCase { + + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Empty Test + */ + public void testApp() + { + assertTrue( true ); + } + + /** + * This is to show how to run socket server. + * The code is commented out because it will listening forever until we stop it using Ctrl-C. + */ + public void testSocketServerListen() + { + try { + // To test, provide socketProcess object first : + // SocketProcess socketProcess = ...; + //SocketServer.listen(socketProcess); + } + catch (Exception e) { + e.printStackTrace(); + } + assertTrue( true ); + } + + /** + * This is to show how to send message to socket server. + * The code is commented out because it will throw error if server is not running. + */ + public void testSocketClientSendMessage() + { + try { + // Remove the comment tag to test. The code are commented out to prevent sending message to socket server (set socket server & edit properties file first) : + //String message = "theme$#=itools~theme$#=computing~subject$#=test~body$#=-iMonitor~recipients$#=Manas@4ecap.com~sayIt$#=FALSE~emailIt$#=TRUE~phoneCall$#=FALSE~ym$#=FALSE~sms$#=FALSE"; + //SocketClient.sendMessage(message); + } + catch (Exception e) { + e.printStackTrace(); + } + assertTrue( true ); + } + +} + + \ No newline at end of file diff --git a/lib-tts/pom.xml b/lib-tts/pom.xml new file mode 100755 index 0000000..cd3aaf7 --- /dev/null +++ b/lib-tts/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + + com.fourelementscapital + lib + ${revision} + + com.fourelementscapital + lib-tts + lib-tts + http://www.fourelementscapital.com + + + + net.sf.sociaal + freetts + 1.2.2 + + + + diff --git a/lib-tts/src/main/java/com/fourelementscapital/tts/Config.java b/lib-tts/src/main/java/com/fourelementscapital/tts/Config.java new file mode 100755 index 0000000..37018d8 --- /dev/null +++ b/lib-tts/src/main/java/com/fourelementscapital/tts/Config.java @@ -0,0 +1,46 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.tts; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +public class Config { + + /** + * Get the value of a property defined in config file + * @param propertyName - the name or key of the property you want to retrieve + * @return the string value + * @throws IOException + */ + protected static String getConfigValue(String propertyName) throws IOException + { + + Properties prop = new Properties(); + String propFileName = "config_tts.properties"; + + InputStream inputStream = Config.class.getClassLoader().getResourceAsStream(propFileName); + + if (inputStream != null) { + + prop.load(inputStream); + } else { + throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath"); + } + + // get the property value and print it out + String value = prop.getProperty(propertyName); + + return value; + } + +} + + diff --git a/lib-tts/src/main/java/com/fourelementscapital/tts/TTS.java b/lib-tts/src/main/java/com/fourelementscapital/tts/TTS.java new file mode 100755 index 0000000..6db0d86 --- /dev/null +++ b/lib-tts/src/main/java/com/fourelementscapital/tts/TTS.java @@ -0,0 +1,71 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.tts; + +import java.beans.PropertyVetoException; +import java.io.IOException; +import java.util.Locale; + +import javax.speech.AudioException; +import javax.speech.Central; +import javax.speech.EngineException; +import javax.speech.EngineStateError; +import javax.speech.synthesis.Synthesizer; +import javax.speech.synthesis.SynthesizerModeDesc; +import javax.speech.synthesis.Voice; + + +/** + * A Text-To-Speech libraries using Free TTS & MBROLA voices + */ +public class TTS { + + private SynthesizerModeDesc desc; + private Synthesizer synthesizer; + private Voice voice; + + public static final String VOICE_MBROLA_US1 = "mbrola_us1"; + + public void init(String voiceName) throws EngineException, AudioException, EngineStateError, PropertyVetoException, IOException { + + //TODO : to properties file + System.setProperty("mbrola.base", Config.getConfigValue("mbrola_path")); + + if (desc == null) { + System.setProperty("freetts.voices", "de.dfki.lt.freetts.en.us.MbrolaVoiceDirectory"); + desc = new SynthesizerModeDesc(Locale.US); + Central.registerEngineCentral("com.sun.speech.freetts.jsapi.FreeTTSEngineCentral"); + synthesizer = Central.createSynthesizer(desc); + synthesizer.allocate(); + synthesizer.resume(); + SynthesizerModeDesc smd = (SynthesizerModeDesc) synthesizer.getEngineModeDesc(); + Voice[] voices = smd.getVoices(); + Voice voice = null; + for (int i = 0; i < voices.length; i++) { + if (voices[i].getName().equals(voiceName)) { + voice = voices[i]; + break; + } + } + synthesizer.getSynthesizerProperties().setVoice(voice); + } + } + + public void terminate() throws EngineException, EngineStateError { + synthesizer.deallocate(); + } + + public void doSpeak(String speakText) throws EngineException, AudioException, IllegalArgumentException, InterruptedException { + synthesizer.speakPlainText(speakText, null); + synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY); + } + + +} + + \ No newline at end of file diff --git a/lib-tts/src/main/resources/config_tts.properties b/lib-tts/src/main/resources/config_tts.properties new file mode 100755 index 0000000..2c086db --- /dev/null +++ b/lib-tts/src/main/resources/config_tts.properties @@ -0,0 +1,2 @@ +#mbrola_path=C:\\iMonitor2\\mbrola\\ +mbrola_path=/usr/share/mbrola \ No newline at end of file diff --git a/lib-tts/src/test/java/com/fourelementscapital/tts/AppTest.java b/lib-tts/src/test/java/com/fourelementscapital/tts/AppTest.java new file mode 100755 index 0000000..01fe5ac --- /dev/null +++ b/lib-tts/src/test/java/com/fourelementscapital/tts/AppTest.java @@ -0,0 +1,67 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fourelementscapital.tts; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for App + */ +public class AppTest extends TestCase { + + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Empty Test + */ + public void testApp() + { + assertTrue( true ); + } + + /** + * Test text to speech + */ + public void testTTS() + { + try { + // Remove the comment tag to test. The code are commented out to prevent executing text to speech (setup MBROLA & edit properties file first) : + /* + TTS tts = new TTS(); + tts.init(TTS.VOICE_MBROLA_US1); + tts.doSpeak("This is just a test"); + //tts.terminate(); // don't terminate, to prevent java.lang.IllegalThreadStateException + */ + } + catch (Exception e) { + e.printStackTrace(); + assertTrue(false); + } + assertTrue( true ); + } +} + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100755 index 0000000..01ce074 --- /dev/null +++ b/pom.xml @@ -0,0 +1,219 @@ + + + 4.0.0 + com.fourelementscapital + lib + ${revision} + pom + + lib + Group of libraries taken from Scheduler code + http://www.fourelementscapital.com + + + + The software is only allowed to be used in Four Elements - no license is given to any other parties + + + + + lib-alarm + lib-auth + lib-db + lib-fileutils + lib-r + lib-socket + lib-tts + lib-imonitor + archetype-lib + archetype-webapp + archetype-app + + + + 2.0 + UTF-8 + UTF-8 + 1.8 + 1.7.21 + 4.12 + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.10.3 + + + attach-javadocs + + aggregate-jar + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + 1.8 + 1.8 + + + + org.codehaus.mojo + flatten-maven-plugin + 1.0.0 + + true + + + + flatten + process-resources + + flatten + + + + flatten.clean + clean + + clean + + + + + + + + + + junit + junit + 3.8.1 + test + + + org.apache.logging.log4j + log4j-core + 2.8.1 + + + + + + + org.apache.logging.log4j + log4j-api + 2.8.1 + + + commons-beanutils + commons-beanutils + 1.8.0 + + + commons-dbutils + commons-dbutils + 1.1 + + + commons-httpclient + commons-httpclient + 3.0 + + + commons-codec + commons-codec + 1.2 + + + + commons-io + commons-io + 1.4 + + + com.jolbox + bonecp + 0.7.1.RELEASE + + + javax.servlet + servlet-api + 2.5 + + + javax.websocket + javax.websocket-api + 1.1 + + + mysql + mysql-connector-java + 5.1.12 + + + org.apache.jcs + jcs + 1.3 + + + org.jsoup + jsoup + 1.6.0 + + + org.mockito + mockito-all + 1.10.19 + + + com.github.lucarosellini.rJava + JRI + 0.9-7 + + + org.rosuda.REngine + Rserve + 1.8.1 + + + com.microsoft.sqlserver + mssql-jdbc + 6.1.0.jre7 + + + org.kohsuke + libpam4j + 1.8 + + + + + + + + disable-java8-doclint + + [1.8,) + + + -Xdoclint:none + + + + + + + libraries + libraries + http://www.fourelementscapital.com + + + +