diff --git a/.gitignore b/.gitignore index e68fb12..40b96d5 100644 --- a/.gitignore +++ b/.gitignore @@ -53,3 +53,7 @@ dist/ nbdist/ .nb-gradle/ +.settings +.classpath +.project +bin/ diff --git a/README.md b/README.md index dc662b1..825e636 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ # scheduler4e -This is the repository of 4E scheduler moved from Alphien github \ No newline at end of file +This is the repository of 4E scheduler moved from Alphien github. +FourElements scheduler from release 2.1.2 plus source and target setup for java8 diff --git a/deployment_scripts/deployPeer.sh b/deployment_scripts/deployPeer.sh new file mode 100644 index 0000000..0b60100 --- /dev/null +++ b/deployment_scripts/deployPeer.sh @@ -0,0 +1,157 @@ +#****************************************************************************** +# +# Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +# All rights reserved. +# +#****************************************************************************** + +#!/bin/bash + +#Check these params value first : +P2PSERVER_PATH="/4EUtils/p2pserver" +STOP_COMMAND="stopPeer.sh" +START_COMMAND="startPeer.sh" +ERROR_LOG_PATH=$P2PSERVER_PATH"/logs" +P2P_PARENT_FOLDER="/4EUtils/" +P2P_PUBLIC_FOLDER="/mnt/public/Infrastructure/Linux_Unix/Current Version/R and Peer Software/Ubuntu_Peer/p2pserver/" +GIT_FOLDER_PATH="/git" +BACKUP_PARENT_PATH="/temp/scheduler_deployment_backup" +GITHUB_REPO_URL="https://4ecapkronos:2016gitcoDE@github.com/FourElementsCapital/Scheduler.git" + +echo "*** SCHEDULER PEER DEPLOYMENT START ***" + +set -e + +if [ $# -lt 1 ] + then + echo "Error argument. Sample : ./deployPeer.sh 2.1.0" + exit 1 +fi + +v_out=$1 + +if [ $# -gt 1 ] + then + key="$2" + case $key in + -f|--fresh) + is_fresh="true" + shift # past argument + ;; + *) + echo "Error argument. use -f or --fresh" + exit 1 + ;; + esac +fi + +SCHEDULER_REPO_PATH=$GIT_FOLDER_PATH"/Scheduler" +SCHEDULER_RELEASE_PATH=$SCHEDULER_REPO_PATH"/release/"$v_out +BACKUP_PATH=$BACKUP_PARENT_PATH"/"$v_out + +#echo "*. Check Scheduler local repository whether it's exist or not" +#if [ -d $SCHEDULER_REPO_PATH ]; then +# echo "Scheduler repo IS EXIST. PULLING from Github.." +# cd $SCHEDULER_REPO_PATH +# git pull $GITHUB_REPO_URL +#else +# echo "Scheduler repo IS NOT EXIST. CLONING from Github.." +# mkdir -p $GIT_FOLDER_PATH +# cd $GIT_FOLDER_PATH +# git clone $GITHUB_REPO_URL +#fi +#chmod -R 777 $GIT_FOLDER_PATH + +echo "*. Check Scheduler release path "$SCHEDULER_RELEASE_PATH +cd $SCHEDULER_RELEASE_PATH + +if [ "$is_fresh" == "true" ] +then + echo "*. Copy p2pfolder from public drive " + mkdir -p $P2P_PARENT_FOLDER + cp -R "$P2P_PUBLIC_FOLDER" $P2P_PARENT_FOLDER +fi + +echo "*. Check Tomcat version and directory" +TOMCAT_7="/tomcat7" +TOMCAT_6="/tomcat6" +VAR_LIB_PATH="/var/lib" +VAR_LOG_PATH="/var/log" +TOMCAT_PATH=$VAR_LIB_PATH$TOMCAT_7 +TOMCAT_WEB_PATH=$TOMCAT_PATH"/webapps" +TOMCAT_LOG_PATH=$VAR_LOG_PATH$TOMCAT_7 +if [ ! -d $TOMCAT_PATH ]; then + if [ -d $VAR_LIB_PATH$TOMCAT_6 ]; then + TOMCAT_PATH=$VAR_LIB_PATH$TOMCAT_6 + TOMCAT_WEB_PATH=$TOMCAT_PATH"/webapps" + TOMCAT_LOG_PATH=$VAR_LOG_PATH$TOMCAT_6 + else + echo "Error : Neither tomcat 6 or 7 is installed !!" + exit 1 + fi +fi +echo "Tomcat path : "$TOMCAT_PATH + +echo "*. Stop Tomcat server" +cd $P2PSERVER_PATH +sh ./$STOP_COMMAND + +if [ -z $is_fresh ] +then + echo "*. Create backup folder" + if [ -d $BACKUP_PATH ] + then + echo "ERROR : backup folder '"$BACKUP_PATH"' is already exist ! Please rename or remove it first. Note that it contains the BACKUP COPY of previous deployment." + exit 1 + else + mkdir -p $BACKUP_PATH + fi +fi + +if [ -z $is_fresh ] +then + echo "*. Backup current scheduler app" + cp -R $TOMCAT_WEB_PATH"/bldb" $BACKUP_PATH +fi + +if [ -z $is_fresh ] +then + echo "*. Backup & clear tomcat log" + mkdir $BACKUP_PATH/log_tomcat + if [ "$(ls -A $TOMCAT_LOG_PATH)" ] + then + mv $TOMCAT_LOG_PATH/* $BACKUP_PATH/log_tomcat + fi +fi + +if [ -z $is_fresh ] +then + echo "*. Backup & clear error log" + mkdir $BACKUP_PATH/log_error + if [ "$(ls -A $ERROR_LOG_PATH)" ] + then + mv $ERROR_LOG_PATH/* $BACKUP_PATH/log_error + fi +fi + +echo "*. Deploy new scheduler app" +if [ -z $is_fresh ] +then + rm -R $TOMCAT_WEB_PATH"/bldb" +fi +cd $SCHEDULER_RELEASE_PATH +unzip -qq bldb.zip -d $TOMCAT_WEB_PATH + +echo "*. Copy properties file" +cp -R $SCHEDULER_RELEASE_PATH"/properties/peer_unix/bldb" $TOMCAT_WEB_PATH + +echo "*. Set access permission" +chmod -R 777 $TOMCAT_WEB_PATH"/bldb" + +echo "*. Start Tomcat server" +cd $P2PSERVER_PATH +sh ./$START_COMMAND + +echo "*** DEPLOYMENT SUCCESS ! ***" + + diff --git a/deployment_scripts/deployServer.sh b/deployment_scripts/deployServer.sh new file mode 100644 index 0000000..11e7567 --- /dev/null +++ b/deployment_scripts/deployServer.sh @@ -0,0 +1,136 @@ +#****************************************************************************** +# +# Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +# All rights reserved. +# +#****************************************************************************** + +#!/bin/bash + +#Check these params value first : +P2PSERVER_PATH="/home/p2pserver" +STOP_COMMAND="stop.sh" +START_COMMAND="start.sh" +ERROR_LOG_PATH=$P2PSERVER_PATH"/logs" +SVN_PATH=$P2PSERVER_PATH"/4svntemp" +SVN_R_PATH=$P2PSERVER_PATH"/r_function_svntemp" +SVN_IEXEC_PATH=$P2PSERVER_PATH"/iexec_svn" +QUEUE_FILES=$P2PSERVER_PATH"/queue/queue1" +GIT_FOLDER_PATH="/git" +BACKUP_PARENT_PATH="/temp/scheduler_deployment_backup" +GITHUB_REPO_URL="https://4ecapkronos:2016gitcoDE@github.com/FourElementsCapital/Scheduler.git" + +echo "*** SCHEDULER SERVER DEPLOYMENT START ***" + +set -e + +if [ $# -lt 1 ] + then + echo "Error argument. Sample : ./deployServer.sh 2.1.0" + exit 1 +fi + +v_out=$1 + +SCHEDULER_REPO_PATH=$GIT_FOLDER_PATH"/Scheduler" +SCHEDULER_RELEASE_PATH=$SCHEDULER_REPO_PATH"/release/"$v_out +BACKUP_PATH=$BACKUP_PARENT_PATH"/"$v_out + +#echo "*. Check Scheduler local repository whether it's exist or not" +#if [ -d $SCHEDULER_REPO_PATH ]; then +# echo "Scheduler repo IS EXIST. PULLING from Github.." +# cd $SCHEDULER_REPO_PATH +# git pull $GITHUB_REPO_URL +#else +# echo "Scheduler repo IS NOT EXIST. CLONING from Github.." +# mkdir -p $GIT_FOLDER_PATH +# cd $GIT_FOLDER_PATH +# git clone $GITHUB_REPO_URL +#fi +#chmod -R 777 $GIT_FOLDER_PATH + +echo "*. Check Scheduler release path "$SCHEDULER_RELEASE_PATH +cd $SCHEDULER_RELEASE_PATH + +echo "*. Check Tomcat version and directory" +TOMCAT_7="/tomcat7" +TOMCAT_6="/tomcat6" +VAR_LIB_PATH="/var/lib" +VAR_LOG_PATH="/var/log" +TOMCAT_PATH=$VAR_LIB_PATH$TOMCAT_7 +TOMCAT_WEB_PATH=$TOMCAT_PATH"/webapps" +TOMCAT_LOG_PATH=$VAR_LOG_PATH$TOMCAT_7 +if [ ! -d $TOMCAT_PATH ]; then + if [ -d $VAR_LIB_PATH$TOMCAT_6 ]; then + TOMCAT_PATH=$VAR_LIB_PATH$TOMCAT_6 + TOMCAT_WEB_PATH=$TOMCAT_PATH"/webapps" + TOMCAT_LOG_PATH=$VAR_LOG_PATH$TOMCAT_6 + else + echo "Error : Neither tomcat 6 or 7 is installed !!" + exit 1 + fi +fi +echo "Tomcat path : "$TOMCAT_PATH + +echo "*. Stop Tomcat server" +cd $P2PSERVER_PATH +sh ./$STOP_COMMAND + +echo "*. Create backup folder" +if [ -d $BACKUP_PATH ] +then + echo "ERROR : backup folder '"$BACKUP_PATH"' is already exist ! Please rename or remove it first. Note that it contains the BACKUP COPY of previous deployment." + exit 1 +else + mkdir -p $BACKUP_PATH +fi + +echo "*. Backup current scheduler app" +cp -R $TOMCAT_WEB_PATH"/bldb" $BACKUP_PATH + +echo "*. Backup SVN files" +cp -R $SVN_PATH $BACKUP_PATH/svn +cp -R $SVN_R_PATH $BACKUP_PATH/svn_r +cp -R $SVN_IEXEC_PATH $BACKUP_PATH/svn_iexec + +echo "*. Backup & clear tomcat log" +mkdir $BACKUP_PATH/log_tomcat +if [ "$(ls -A $TOMCAT_LOG_PATH)" ] +then + mv $TOMCAT_LOG_PATH/* $BACKUP_PATH/log_tomcat +fi + +echo "*. Backup & clear error log" +mkdir $BACKUP_PATH/log_error +if [ "$(ls -A $ERROR_LOG_PATH)" ] +then + mv $ERROR_LOG_PATH/* $BACKUP_PATH/log_error +fi + +echo "*. Retain superuser.pwd" +cp $BACKUP_PATH/log_error/superuser.pwd $ERROR_LOG_PATH +chmod 777 $ERROR_LOG_PATH"/superuser.pwd" + +echo "*. Backup & clear queue files" +mkdir $BACKUP_PATH/queue +if [ "$(ls $QUEUE_FILES.* 2> /dev/null)" ] +then + mv $QUEUE_FILES.* $BACKUP_PATH/queue +fi + +echo "*. Deploy new scheduler app" +rm -R $TOMCAT_WEB_PATH"/bldb" +cd $SCHEDULER_RELEASE_PATH +unzip -qq bldb.zip -d $TOMCAT_WEB_PATH + +echo "*. Copy properties file" +cp -R $SCHEDULER_RELEASE_PATH"/properties/server_unix/bldb" $TOMCAT_WEB_PATH + +echo "*. Set access permission" +chmod -R 777 $TOMCAT_WEB_PATH"/bldb" + +echo "*** DEPLOYMENT SUCCESS ! ***" +echo "*** Please start Scheduler Server manually ! ***" + + + diff --git a/deployment_scripts/revertPeer.sh b/deployment_scripts/revertPeer.sh new file mode 100644 index 0000000..7b71ee2 --- /dev/null +++ b/deployment_scripts/revertPeer.sh @@ -0,0 +1,68 @@ +#****************************************************************************** +# +# Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +# All rights reserved. +# +#****************************************************************************** + +#!/bin/bash +echo "*** SCHEDULER REVERT START ***" + +set -e + +if [ $# -lt 1 ] + then + echo "Error argument. Sample : ./revertPeer.sh 2.1.0" + exit 1 +fi + +v_out=$1 + +P2PSERVER_PATH="/4EUtils/p2pserver" +START_COMMAND="startPeer.sh" +STOP_COMMAND="stopPeer.sh" +BACKUP_PATH="/temp/scheduler_deployment_backup/"$v_out + +echo "*. Check backup path "$BACKUP_PATH +cd $BACKUP_PATH + +echo "*. Check Tomcat version and directory" +TOMCAT_7="/tomcat7" +TOMCAT_6="/tomcat6" +VAR_LIB_PATH="/var/lib" +VAR_LOG_PATH="/var/log" +TOMCAT_PATH=$VAR_LIB_PATH$TOMCAT_7 +TOMCAT_WEB_PATH=$TOMCAT_PATH"/webapps" +if [ ! -d $TOMCAT_PATH ]; then + if [ -d $VAR_LIB_PATH$TOMCAT_6 ]; then + TOMCAT_PATH=$VAR_LIB_PATH$TOMCAT_6 + TOMCAT_WEB_PATH=$TOMCAT_PATH"/webapps" + else + echo "Error : Neither tomcat 6 or 7 is installed !!" + exit 1 + fi +fi +echo "Tomcat path : "$TOMCAT_PATH + +echo "*. Stop Tomcat server" +cd $P2PSERVER_PATH +sh ./$STOP_COMMAND + +echo "*. Remove Scheduler app" +rm -R $TOMCAT_WEB_PATH"/bldb" + +echo "*. Copy files from backup" +cp -R $BACKUP_PATH"/bldb" $TOMCAT_WEB_PATH + +echo "*. Set access permission" +chmod -R 777 $TOMCAT_WEB_PATH"/bldb" + +echo "*. Start Tomcat server" +cd $P2PSERVER_PATH +sh ./$START_COMMAND + +echo "*** REVERT SUCCESS ! ***" + + + + diff --git a/deployment_scripts/revertServer.sh b/deployment_scripts/revertServer.sh new file mode 100644 index 0000000..518e19a --- /dev/null +++ b/deployment_scripts/revertServer.sh @@ -0,0 +1,65 @@ +#****************************************************************************** +# +# Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +# All rights reserved. +# +#****************************************************************************** + +#!/bin/bash +echo "*** SCHEDULER REVERT START ***" + +set -e + +if [ $# -lt 1 ] + then + echo "Error argument. Sample : ./revertServer.sh 2.1.0" + exit 1 +fi + +v_out=$1 + +P2PSERVER_PATH="/home/p2pserver" +START_COMMAND="start.sh" +STOP_COMMAND="stop.sh" +BACKUP_PATH="/temp/scheduler_deployment_backup/"$v_out + +echo "*. Check backup path "$BACKUP_PATH +cd $BACKUP_PATH + +echo "*. Check Tomcat version and directory" +TOMCAT_7="/tomcat7" +TOMCAT_6="/tomcat6" +VAR_LIB_PATH="/var/lib" +VAR_LOG_PATH="/var/log" +TOMCAT_PATH=$VAR_LIB_PATH$TOMCAT_7 +TOMCAT_WEB_PATH=$TOMCAT_PATH"/webapps" +if [ ! -d $TOMCAT_PATH ]; then + if [ -d $VAR_LIB_PATH$TOMCAT_6 ]; then + TOMCAT_PATH=$VAR_LIB_PATH$TOMCAT_6 + TOMCAT_WEB_PATH=$TOMCAT_PATH"/webapps" + else + echo "Error : Neither tomcat 6 or 7 is installed !!" + exit 1 + fi +fi +echo "Tomcat path : "$TOMCAT_PATH + +echo "*. Stop Tomcat server" +cd $P2PSERVER_PATH +sh ./$STOP_COMMAND + +echo "*. Remove Scheduler app" +rm -R $TOMCAT_WEB_PATH"/bldb" + +echo "*. Copy files from backup" +cp -R $BACKUP_PATH"/bldb" $TOMCAT_WEB_PATH + +echo "*. Set access permission" +chmod -R 777 $TOMCAT_WEB_PATH"/bldb" + + +echo "*** REVERT SUCCESS ! ***" +echo "*** Please start Scheduler Server manually ! ***" + + + diff --git a/project/bldb/lib/blpapi3.jar b/project/bldb/lib/blpapi3.jar new file mode 100644 index 0000000..4277629 Binary files /dev/null and b/project/bldb/lib/blpapi3.jar differ diff --git a/project/bldb/lib/classmexer.jar b/project/bldb/lib/classmexer.jar new file mode 100644 index 0000000..6528ab6 Binary files /dev/null and b/project/bldb/lib/classmexer.jar differ diff --git a/project/bldb/lib/cobra.jar b/project/bldb/lib/cobra.jar new file mode 100644 index 0000000..548c8e2 Binary files /dev/null and b/project/bldb/lib/cobra.jar differ diff --git a/project/bldb/lib/jcs-1.3.3.1-RC.jar b/project/bldb/lib/jcs-1.3.3.1-RC.jar new file mode 100644 index 0000000..c3b6cb9 Binary files /dev/null and b/project/bldb/lib/jcs-1.3.3.1-RC.jar differ diff --git a/project/bldb/lib/js-14.jar b/project/bldb/lib/js-14.jar new file mode 100644 index 0000000..0bc0199 Binary files /dev/null and b/project/bldb/lib/js-14.jar differ diff --git a/project/bldb/pom.xml b/project/bldb/pom.xml new file mode 100644 index 0000000..0e3ae03 --- /dev/null +++ b/project/bldb/pom.xml @@ -0,0 +1,502 @@ + + 4.0.0 + com.fe + bldb + 1.0 + war + A com.fourelementscapital Webapp + http://www.fourelementscapital.com + + + 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-scheduler-exception + 1.0 + + + com.fourelementscapital + lib-scheduler-common + 1.0 + + + com.fourelementscapital + lib-scheduler-p2p + 1.0 + + + com.fourelementscapital + lib-scheduler-engines + 1.0 + + + com.fourelementscapital + lib-scheduler-queue + 1.0 + + + + junit + junit + 3.8.1 + test + + + javax.servlet + servlet-api + 2.5 + + + + org.apache.logging.log4j + log4j-core + 2.8.1 + + + org.apache.james + apache-mime4j + 0.6 + + + org.apache.axis + axis + 1.4 + + + org.apache.axis + axis-ant + 1.4 + + + org.bouncycastle + bcprov-jdk14 + 1.54 + + + com.jolbox + bonecp + 0.7.1.RELEASE + + + c3p0 + c3p0 + 0.9.1.1 + + + commons-beanutils + commons-beanutils + 1.8.0 + + + commons-cli + commons-cli + 1.2 + + + commons-codec + commons-codec + 1.3 + + + commons-collections + commons-collections + 3.2.1 + + + org.apache.commons + commons-compress + 1.3 + + + org.apache.commons + commons-lang3 + 3.0 + + + commons-dbcp + commons-dbcp + 1.4 + + + commons-dbutils + commons-dbutils + 1.1 + + + commons-digester + commons-digester + 2.0 + + + commons-discovery + commons-discovery + 0.2 + + + commons-fileupload + commons-fileupload + 1.2.1 + + + commons-httpclient + commons-httpclient + 3.0 + + + commons-io + commons-io + 1.4 + + + commons-logging + commons-logging + 1.1.1 + + + commons-modeler + commons-modeler + 2.0 + + + commons-pool + commons-pool + 1.6 + + + commons-validator + commons-validator + 1.3.1 + + + concurrent + concurrent + 1.3.4 + + + com.googlecode.cqengine + cqengine + 1.2.2 + + + org.directwebremoting + dwr + 2.0.8 + + + com.google.guava + guava + 12.0 + + + org.hsqldb + hsqldb + 2.2.9 + + + org.apache.httpcomponents + httpclient + 4.0 + + + org.apache.httpcomponents + httpcore + 4.0.1 + + + org.apache.httpcomponents + httpmime + 4.0 + + + org.apache.tomcat + tomcat-catalina + 7.0.42 + provided + + + org.apache.tomcat + tomcat-coyote + 7.0.39 + provided + + + javax.xml + jaxrpc-api + 1.1 + + + jcifs + jcifs + 1.3.17 + + + com.jcraft + jsch + 0.1.50 + + + org.jsoup + jsoup + 1.6.0 + + + javax.transaction + jta + 1.1 + + + net.jxta + jxta-jxse + 2.5 + + + org.apache.lucene + lucene-core + 2.4.1 + + + javax.mail + mailapi + 1.4.3 + + + mysql + mysql-connector-java + 5.1.12 + + + be.cyberelf.nanoxml + nanoxml + 2.2.3 + + + io.netty + netty-all + 4.0.9.Final + + + org.mortbay.jetty + jetty + 6.1.26 + + + org.apache.poi + poi + 3.1-FINAL + + + org.apache.poi + poi-contrib + 3.1-FINAL + + + org.apache.poi + poi-scratchpad + 3.1-FINAL + + + org.quartz-scheduler + quartz + 2.1.1 + + + org.mozilla + rhino + 1.7.7.1 + + + javax.xml.soap + saaj-api + 1.3.5 + + + org.slf4j + slf4j-api + 1.6.1 + + + + org.slf4j + slf4j-simple + 1.6.6 + + + com.sun.mail + smtp + 1.4.7 + + + org.tmatesoft.svnkit + svnkit + 1.3.0.5847 + + + org.tuckey + urlrewrite + 2.5.2 + + + wsdl4j + wsdl4j + 1.5.1 + + + javax.xml + jaxp-api + 1.4.2 + + + com.lowagie + itext + 2.1.3 + + + jfree + jfreechart-swt + 1.0.17 + + + org.jfree + jfreechart + 1.0.19 + + + org.jfree + jcommon + 1.0.23 + + + org.eclipse.swt + org.eclipse.swt.win32.win32.x86 + 4.3 + + + org.eclipse.birt.runtime.3_7_1 + org.mozilla.javascript + 1.7.2 + + + org.java-websocket + Java-WebSocket + 1.3.0 + + + net.bull.javamelody + javamelody-core + 1.60.0 + + + com.bloomberglp.blpapi + blpapi + 3.2.2.0 + ${pom.basedir}/lib/blpapi3.jar + system + + + com.javamex.classmexer + classmexer + 1.6.0 + ${pom.basedir}/lib/classmexer.jar + system + + + com.cobra + cobra + 1.5.0 + ${pom.basedir}/lib/cobra.jar + system + + + org.mozilla.javascript.tools.shell + js-14 + 1.2.3 + ${pom.basedir}/lib/js-14.jar + system + + + org.apache.jcs + jcs + 1.3.3.1 + ${pom.basedir}/lib/jcs-1.3.3.1-RC.jar + system + + + org.json + json + 20090211 + + + + + + 1.8 + + + + bldb + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + 1.8 + 1.8 + + + + maven-war-plugin + 2.4 + + true + + + lib + WEB-INF/lib + + **/*.jar + + + + + + + + diff --git a/project/bldb/src/main/java/com/fe/client/AbstractTeamOrgMgmt.java b/project/bldb/src/main/java/com/fe/client/AbstractTeamOrgMgmt.java new file mode 100644 index 0000000..35c4656 --- /dev/null +++ b/project/bldb/src/main/java/com/fe/client/AbstractTeamOrgMgmt.java @@ -0,0 +1,399 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fe.client; + +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; +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.fe.common.Constant; +import com.fe.scheduler.TemplateParser; +import com.fourelementscapital.alarm.Alarm; +import com.fourelementscapital.alarm.AlarmType; +import com.fourelementscapital.alarm.ThemeVO; +import com.fourelementscapital.auth.UserThemeAccessPermission; +import com.fourelementscapital.db.AbstractTeamOrgDB; +import com.fourelementscapital.db.SchedulerDB; + + + +/** + * 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 + * + */ +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); + //h.put("followers", followers); + + return h; + //}catch(Exception e){ + // ClientErrorMgmt.reportError(e, null); + // throw e; + //}finally{ + // sdb.closeDB(); + //} + } + + + /** + * + * @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); + notifyLastModification(atodb,sdb,name,item_id,comment,rev,diff,tempdata,owner_tag,templ_file); + + } + + + private void notifyLastModification(AbstractTeamOrgDB atdb,SchedulerDB sdb,String task_name,int item_id, String comments,long revision, String diff,Map hdata,String owner_tag, String templ_filename) throws Exception { + + + + /* + SVNSync sync=new SVNSync(); + String diff=null; + if(revision>=0){ + diff=sync.getLastChanged(item_id); + }else { + diff="@@@No modification on script@@ there may be change on other than script for example trigger times, name and etc.. "; + + } + */ + + //String currentuser=new SchedulerMgmt(getRequest()).getAuthorizedUser(sdb); + String currentuser=getAuthorizedUser(sdb); + + + + ArrayList themes=atdb.getFollowTags4Item(item_id); + + if(owner_tag!=null){ + if(themes.contains(owner_tag)) themes.remove(owner_tag); + themes.add(0, owner_tag); + } + + List m_themes=atdb.getThemeNames4Item(item_id); + for(String t:m_themes){ + if(!themes.contains(t)) themes.add(t); + } + + + //System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~AbstractTeamOrgMgmt.notifyLastModification():themes"+themes); + + String exc_user=currentuser+"@4ecap.com"; + + String content=""; + //InputStream in=SendMail.class.getResourceAsStream("scheduler_modified_alert.txt"); + InputStream in=AbstractTeamOrgMgmt.class.getResourceAsStream(templ_filename); + + BufferedReader br = new BufferedReader(new InputStreamReader(in)); + String strLine; + while ((strLine = br.readLine()) != null) { + content+=(content.equals("")) ?strLine:"\n"+strLine; + } + br.close(); + in.close(); + //System.out.println("content:"+content); + + TemplateParser pt=new TemplateParser(content,hdata); + String message=pt.parseTemplate(); + String subject=pt.getSubject(); + + try{ + + // convert String array list to ThemeVO array list required by Alarm.sendAlarm() : + ArrayList themeList = new ArrayList(); + for (int i=0; i records=new Vector(); + records.add(contract); + cdb.addSValRecords(cdb.connection(), records); + }else{ + contract.setValue(Double.parseDouble(val)); + Vector records=new Vector(); + records.add(contract); + cdb.addRecords(cdb.connection(), records); + } + + cdb.closeDB(); + return true; + + }catch(Exception e){ + ClientError.reportError(e, null); + throw e; + } + + } + + + /** + * + * @param db + * @param tablename + * @return + * @throws Exception + */ + public int checkTableDataType(String db,String tablename) throws Exception { + ContractDB cdb=new SuperDB().getContractDB(tablename,db); + try{ + cdb.connectDB(db); + boolean issval=cdb.checkSValueFieldTypeExist(cdb.connection()); + int rtn=0; + if(issval){ + if(cdb.checkSValueField(cdb.connection())>0){ + rtn=3; //database set to accepts numeric; + }else{ + rtn=2; //database set to accepts sval + } + //override settings to acept both if the database is empty and sval field exist in the table. + if(cdb.countRecords(cdb.connection())<1){ + rtn=0; + } + }else{ + rtn=1; //database accepts only numeric + } + + return rtn; + + }catch(Exception e){ + ClientError.reportError(e, null); + throw e; + }finally{ + cdb.closeDB(); + } + } + + + /** + * + * @param db + * @param tablename + * @param data + * @return + * @throws Exception + */ + public boolean deleteRawData(String db,String tablename, Map data) throws Exception { + UtilDB udb=new SuperDB().getUtilDB(db); + try{ + log.debug("deleteRawData called, db:"+db+" tablename:"+tablename+ " data:"+data); + + udb.connectDB(db); + if(data.get("contract")!=null && data.get("cdate1")!=null){ + udb.deleteRawData(udb.connection(), tablename, data); + log.debug("delete method in utildb exec uted"); + } + //udb.closeDB(); + return true; + + } catch (Exception e) { + ClientError.reportError(e, null); + throw e; + }finally{ + udb.closeDB(); + } + } + + /** + * + * @param db + * @param tablename + * @param olddata + * @param newdata + * @return + * @throws Exception + */ + public boolean setUpdateRawData(String db,String tablename, Map olddata, Map newdata) throws Exception { + UtilDB udb=new SuperDB().getUtilDB(db); + try{ + log.debug("setUpdateRawData called, db:"+db+" tablename:"+tablename+ " old:"+olddata+" new:"+newdata); + + udb.connectDB(db); + if(olddata.get("contract")!=null && olddata.get("cdate1")!=null){ + udb.updateRawData(udb.connection(), tablename, olddata, newdata); + log.debug("update method in utildb exec uted"); + } + + return true; + + } catch (Exception e) { + ClientError.reportError(e, null); + throw e; + }finally{ + udb.closeDB(); + } + } + + /** + * + * @param db + * @param commodity + * @return + * @throws Exception + */ + public boolean removCommodity(String db, String commodity) throws Exception { + try{ + UtilDB udb=new SuperDB().getUtilDB(db); + udb.connectDB(db); + boolean result=udb.removeCommodityAndFields(udb.connection(), commodity); + udb.closeDB(); + return result; + + } catch (Exception e) { + ClientError.reportError(e, "db:"+db+",commodity:"+commodity); + throw e; + } + } + + /** + * + * @return + * @throws Exception + */ + private String getLoggedInUser() throws Exception{ + return (String)getRequest().getSession().getAttribute(AdminMgmt.LOGGED_IN_USERNAME); + } + + + /** + * + * @param db + * @return + * @throws Exception + */ + public Map getNodeInfo(String db) throws Exception { + TreeMap rtn=new TreeMap(); + + UtilDB udb=new SuperDB().getUtilDB(db); + + + udb.connectDB(db); + Map mar_c=udb.listAllCommoditiesAndFields(udb.connection()); + if(mar_c.size()<=5000){ + //avoid out of memory error + List mar_c_error=udb.listAllOrphanedAssets(udb.connection()); + rtn.put(db+"_err", mar_c_error); + } + + rtn.put(db, mar_c); + udb.closeDB(); + return rtn; + + } + + + private HttpServletRequest getRequest() throws Exception { + return (request == null && WebContextFactory.get()!=null) ? WebContextFactory.get().getHttpServletRequest() : request; + + } + + +} + + + diff --git a/project/bldb/src/main/java/com/fe/client/Authenticated.java b/project/bldb/src/main/java/com/fe/client/Authenticated.java new file mode 100644 index 0000000..96ada34 --- /dev/null +++ b/project/bldb/src/main/java/com/fe/client/Authenticated.java @@ -0,0 +1,222 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fe.client; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.directwebremoting.WebContextFactory; + +import com.fe.common.Constant; +import com.fourelementscapital.auth.UserThemeAccessPermission; +import com.fourelementscapital.db.AbstractTeamOrgDB; +import com.fourelementscapital.db.InfrastructureDB; +import com.fourelementscapital.scheduler.error.ClientError; + +/** + * This class is abstraction layer for the secured data, which means can be accessed only after logged in. + * Any class that exposes user privileged data should be extended from Authenticated class + * @author Rams + * + */ +public abstract class Authenticated { + + private HttpServletRequest request=null; + public static String REMOTE_IP="$clientIpAddress"; + private Logger log = LogManager.getLogger(Authenticated.class.getName()); + public static final String SESS_AUTHENTICATED_USER="$$AuthenticatedUserObj"; + + + /** + * No authentication, which means this constructed to be used internally within JVM. + * @param ignoreAuthentication + */ + protected Authenticated(boolean ignoreAuthentication){ + + } + + + /** + * Constructor for DWR + * @throws Exception + */ + public Authenticated() throws Exception { + + if(getAuthenticatedUser()==null){ + throw new Exception("SESSION LOGGED OUT: Access denied for this request"); + } + + try{ + if(getRequest().getSession().getAttribute(REMOTE_IP)==null){ + getRequest().getSession().setAttribute(REMOTE_IP, getRequest().getRemoteAddr()); + } + }catch(Exception e){ + log.error("Error while setting up session variable remote-ipaddress "); + } + + } + + + /** + * This constructor to be used within JSP or interal call within java runtime. + * @param request + * @throws Exception + */ + public Authenticated(HttpServletRequest request) throws Exception { + this.request=request; + if(getAuthenticatedUser()==null){ + throw new Exception("Requested user is not authenticated "); + } + + try{ + if(getRequest().getSession().getAttribute(REMOTE_IP)==null){ + getRequest().getSession().setAttribute(REMOTE_IP, getRequest().getRemoteAddr()); + } + }catch(Exception e){ + log.error("Error while setting up session variable remote-ipaddress "); + } + + } + + + protected UserThemeAccessPermission getAuthenticatedUserObj( AbstractTeamOrgDB sdb) throws Exception { + String currentuser=getAuthenticatedUser(); + //String currentuser="ls"; //getAuthenticatedUser(); + UserThemeAccessPermission auser=(UserThemeAccessPermission)getRequest().getSession().getAttribute(Authenticated.SESS_AUTHENTICATED_USER); + InfrastructureDB infrastructureDB = InfrastructureDB.getInfrastructureDB(); + try{ + if(auser==null){ + infrastructureDB.connectDB(); + Map themes=infrastructureDB.getThemes4Users(currentuser); + + auser = new UserThemeAccessPermission(currentuser); + auser.addPermissionWithThemes(themes); + + getRequest().getSession().setAttribute(Authenticated.SESS_AUTHENTICATED_USER,auser); + } + } catch(Exception e) { + throw e; + } finally { + infrastructureDB.closeDB(); + } + return auser; + } + + + + /** + * Get authenticated user in any DWR call. + * @return + * @throws Exception + */ + protected String getAuthenticatedUser() throws Exception { + String ky=(String)getRequest().getSession().getAttribute(Constant.SESSION_LOGGED_USER); + String usr=null; + log.debug("getAuthenticatedUser ----ky:"+ky); + if((ky==null || (ky!=null && ky.equals(""))) ){ + + String user=null; + String pwd=null; + Cookie cookie[]=getRequest().getCookies(); + if(cookie!=null){ + for(int i=0;i themes, AbstractTeamOrgDB sdb) throws Exception { + + //SchedulerDB sdb=SchedulerDB.getSchedulerDB(); + InfrastructureDB infrastructureDB = InfrastructureDB.getInfrastructureDB(); + try{ + String th=""; + HashMap rtn=new HashMap(); + //StringTokenizer st=new StringTokenizer(themes,","); + //while(st.hasMoreTokens()){ + for(String tkn: themes ){ + //String tkn=st.nextToken(); + th+=th.equals("")?"'"+tkn+"'":",'"+tkn+"'"; + } + infrastructureDB.connectDB(); + Map data=!th.equals("")?infrastructureDB.getTeamOrg(th, getPermissionHierarchy()):new HashMap(); + + Map user_privileges=UserThemeAccessPermission.replacePermissions(data); + + return user_privileges; + + }catch(Exception e){ + ClientError.reportError(e, null); + e.printStackTrace(); + throw e; + }finally{ + //sdb.closeDB(); + infrastructureDB.closeDB(); + } + } + + + private List getPermissionHierarchy(){ + + ArrayList h=new ArrayList(); + h.add("X"); + h.add("M"); + h.add("S"); + h.add("B"); + h.add("C"); + h.add("U"); + h.add("N"); + return h; + } + +} + + diff --git a/project/bldb/src/main/java/com/fe/client/ExecuteRMgmt.java b/project/bldb/src/main/java/com/fe/client/ExecuteRMgmt.java new file mode 100644 index 0000000..5988017 --- /dev/null +++ b/project/bldb/src/main/java/com/fe/client/ExecuteRMgmt.java @@ -0,0 +1,247 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fe.client; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.Semaphore; +import java.util.concurrent.TimeUnit; + +import javax.servlet.http.HttpServletRequest; + +import org.apache.jcs.JCS; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.directwebremoting.WebContextFactory; + +import com.fourelementscapital.db.SchedulerDB; +import com.fourelementscapital.scheduler.balance.LoadBalancingQueue; +import com.fourelementscapital.scheduler.balance.executeR.LoadBalancingExecuteRQueue; +import com.fourelementscapital.scheduler.rscript.RScript; + + +/** + * Provides AJAX data for ExecuteR monitoring interface. + * The jsp file for this class executeR.jsp and javascript file executer.jsp + * @author Administrator + * + */ +public class ExecuteRMgmt { + + private Logger log = LogManager.getLogger(ExecuteRMgmt.class.getName()); + private HttpServletRequest request=null; + + private static Semaphore refreshLock=new Semaphore(1,true); + private static final long TIMEOUT_MS=1000; + + /** + * Semaphore lock and release machanism to make some the method calls exclusive to the current thread. + */ + private void acquireLock(){ + try{ + ExecuteRMgmt.refreshLock.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS); + }catch(Exception e){ log.error("Error:"+e.getMessage()); } + } + private void releaseLock(){ + try{ ExecuteRMgmt.refreshLock.release(); }catch(Exception e){ log.error("Error:"+e.getMessage()); } + } + + + + + /** + * for DWR call + * @throws Exception + */ + public ExecuteRMgmt() throws Exception { + + } + + /** + * for internal or jsp call + * @param request + * @throws Exception + */ + public ExecuteRMgmt(HttpServletRequest request) throws Exception { + + } + + + /** + * get queue data in json format + * @return + * @throws Exception + */ + public Map getQueue() throws Exception { + acquireLock(); + try{ + + HashMap rtn=new HashMap(); + ArrayList list2=new ArrayList(); + LoadBalancingQueue queue=LoadBalancingQueue.getExecuteRScriptDefault(); + Collection queued=queue.getScriptQueue(); + + Collection p1=queue.getScriptProcessingQueue(); + + + ArrayList executing=new ArrayList(); + for(RScript rs:p1){ + //log.debug("p1:"+rs.getPeer()); + executing.add(rs.getUid()); + } + list2.addAll(p1); + list2.addAll(queued); + + JCS gjcs=queue.getGroupedCache(); + + + //just to retrieve so that, expired won't in the memory + int counter=0; + long totaldelay=0; + for(Object key: gjcs.getGroupKeys(LoadBalancingQueue.CACHE_GROUP_FINISHED)){ + Long delay=(Long)gjcs.getFromGroup(key,LoadBalancingQueue.CACHE_GROUP_FINISHED); + if(delay!=null){ + totaldelay=totaldelay+delay; + counter++; + } + } + log.debug("totaldelay:"+totaldelay+" counter:"+counter); + long ave_delay=(counter>0 && totaldelay>0)?(totaldelay/counter)/1000:0; + + //just to retrieve so that, expired won't in the memory + for(Object key: gjcs.getGroupKeys(LoadBalancingQueue.CACHE_GROUP_TIMEOUT)){ + gjcs.getFromGroup(key,LoadBalancingQueue.CACHE_GROUP_TIMEOUT); + } + Set finished=gjcs.getGroupKeys(LoadBalancingQueue.CACHE_GROUP_FINISHED); + Set timedout=gjcs.getGroupKeys(LoadBalancingQueue.CACHE_GROUP_TIMEOUT); + Set activePeers=gjcs.getGroupKeys(LoadBalancingQueue.CACHE_GROUP_ACTIVEPEERS); + //long perc=(memory/runtime.totalMemory())*100; + //perc=100-perc; + //log.debug("timedout:"+timedout.size()); + //log.debug("finised:"+finished.size()); + Map peerdata=null; + if(getRequest()!=null && getRequest().getSession()!=null && getRequest().getSession().getAttribute("peer_data")!=null){ + peerdata=(Map)getRequest().getSession().getAttribute("peer_data"); + }else{ + SchedulerDB sdb=SchedulerDB.getSchedulerDB(); + try{ + sdb.connectDB(); + List pd=sdb.getPeersData(); + peerdata=new HashMap(); + for(Map record : pd){ + + String fname=(record.get("friendlyname")!=null && ((String)record.get("friendlyname")).equals("")) + ?(String)record.get("friendlyname") + :(String)record.get("peername"); + peerdata.put(record.get("peername"), record.get("friendlyname")); + } + if(getRequest()!=null){ + getRequest().getSession().setAttribute("peer_data", peerdata); + } + }catch(Exception e){ + log.error("Error:"+e.getMessage()); + }finally{ + sdb.closeDB(); + } + } + + rtn.put("queue", list2); + rtn.put("ave_delay", (ave_delay>0?ave_delay:0)); + rtn.put("queued_count", queued.size()); + rtn.put("executing", executing); + rtn.put("executing_count", p1.size()); + rtn.put("active_peers", activePeers.size()); + rtn.put("finished", finished); + rtn.put("timedout", timedout); + rtn.put("peernames",peerdata); + return rtn; + + }catch(Exception e){ + e.printStackTrace(); + throw e; + }finally{ + releaseLock(); + } + } + + + /** + * remove failed item in the queue + * @param uid + * @throws Exception + */ + public void removeFromAllQueue(String uid) throws Exception { + try{ + RScript rs=new RScript(); + rs.setUid(uid); + LoadBalancingQueue queue=LoadBalancingQueue.getExecuteRScriptDefault(); + queue.removeScriptFromAllQueue(rs); + }catch(Exception e){ + e.printStackTrace(); + } + } + + + /** + * returns the queue data in xml format + * @return + * @throws Exception + */ + public String getQueueXML() throws Exception { + Map data=getQueue(); + String result=""; + for(Iterator keys=data.keySet().iterator();keys.hasNext();){ + String key=keys.next(); + + + ArrayList a=new ArrayList(); + + + a.add("ave_delay"); + a.add("queued_count"); + a.add("executing_count"); + a.add("active_peers"); + a.add("finished"); + a.add("timedout"); + + if(a.contains(key)){ + String val=data.get(key)+""; + //System.out.println("key:"+key+":"+data.get(key).getClass().getName()+" isCollection:"+(data.get(key) instanceof Collection) ); + if(data.get(key) instanceof Collection) val=((Collection)data.get(key)).size()+""; + result+="<"+key.toLowerCase()+">"+val+""; + } + } + result+=""; + return result; + + } + + /** + * @deprecated + * @return + * @throws Exception + */ + public Map test_queueDebug() throws Exception { + LoadBalancingExecuteRQueue queue=(LoadBalancingExecuteRQueue)LoadBalancingQueue.getExecuteRScriptDefault(); + return queue.debug_data(); + } + + + private HttpServletRequest getRequest() throws Exception { + return (request == null && WebContextFactory.get()!=null) ? WebContextFactory.get().getHttpServletRequest() : request; + } + + +} + + diff --git a/project/bldb/src/main/java/com/fe/client/IExecAccessMgmt.java b/project/bldb/src/main/java/com/fe/client/IExecAccessMgmt.java new file mode 100644 index 0000000..d168521 --- /dev/null +++ b/project/bldb/src/main/java/com/fe/client/IExecAccessMgmt.java @@ -0,0 +1,110 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fe.client; + +import java.util.ArrayList; +import java.util.List; + +import javax.servlet.http.HttpServletRequest; + +import com.fourelementscapital.auth.UserThemeAccessPermission; +import com.fourelementscapital.db.InfrastructureDB; +import com.fourelementscapital.db.SchedulerDB; + +public class IExecAccessMgmt extends Authenticated { + + public IExecAccessMgmt() throws Exception { + super(); + // TODO Auto-generated constructor stub + } + + /** + * constructor for jsp + * @param request + * @throws Exception + */ + public IExecAccessMgmt(HttpServletRequest request) throws Exception { + super(request); + } + + /** + * update user preference, this preference available on pressing + button on the menu bar + * @param key + * @param val + * @return + * @throws Exception + */ + public boolean checkUserPermission() throws Exception { + + + SchedulerDB sdb=SchedulerDB.getSchedulerDB(); + try{ + sdb.connectDB(); + UserThemeAccessPermission au = getAuthenticatedUserObj(sdb); + ArrayList fullaccess = au.getRwx(); + ArrayList readexecaccess = au.getRx(); + ArrayList readaccess = au.getR(); + fullaccess.addAll(readexecaccess); + fullaccess.addAll(readaccess); + if(fullaccess.contains("iExec")) + return true; + else + return false; + }catch(Exception e){ + e.printStackTrace(); + throw e; + } + finally{ + sdb.closeDB(); + } + + + } + + /** + * update user preference, this preference available on pressing + button on the menu bar + * @param key + * @param val + * @return + * @throws Exception + */ + public String checkUserPermissionNew(String application) throws Exception { + + String access = "---"; + SchedulerDB sdb=SchedulerDB.getSchedulerDB(); + InfrastructureDB infrastructureDB = InfrastructureDB.getInfrastructureDB(); + try{ + sdb.connectDB(); + UserThemeAccessPermission au = getAuthenticatedUserObj(sdb); + + infrastructureDB.connectDB(); + + List al = infrastructureDB.getAccessByAppAndUsername(application, au.toString()); + for (int i=0; i0){ + throw new Exception("ERROR: Strategy name: "+strategy_name+" alerady existing, please try different name"); + } + + Map p_data=iedb.getStrategy(parent_st_id); + String parent_name=(String)p_data.get("strategy_name"); + + + int id=iedb.createChildStrategy(parent_name,strategy_name); + + + Map data=iedb.getStrategy(id); + + + + String content=""; + Map func_data=new HashMap(); + func_data.put("content", content); + func_data.put("data", data); + + + Vector functions=new Vector(); + functions.add(data); + + + rtn.put("strategies", functions); + boolean readonly=true; + rtn.put("strategy_data", getStrategy(id,!readonly)); + + + + //} + //rtn=getRFunction(id); + //String content=getContent(filepath); + //rtn.put("content", content); + //rtn.put("data", data); + //return getRFunction(id); + + return rtn; + }catch(Exception e){ + ClientError.reportError(e, "parent_st_id:"+parent_st_id+" fname:"+strategy_name+" strategy_name:"+strategy_name); + throw e; + }finally{ + iedb.closeDB(); + } + + } + + + + /** + * to create new strategy + * + * @param folder_id + * @param strategy_name + * @param script + * @return + * @throws Exception + */ + + public Map createStrategy(String folder_id,String strategy_name, String script) throws Exception { + + String folder=Config.getString("iexec_source_folder"); + IExecDB iedb=IExecDB.getIExcecDB(); + + + try{ + + //log.debug(" script: "+script); + if(script==null || (script!=null && script.trim().equalsIgnoreCase(""))){ + //InputStream in=Config.class.getResourceAsStream("strategy_name_template.xml"); + //FileUtils + + String rtn=""; + try{ + InputStream in=Config.class.getResourceAsStream("strategy_new_template.xml"); + rtn=convertStreamToString(in); + }catch(Exception e){ + log.error("Error while reading strategy_new_template.xml file"); + } + //log.debug("rtn:"+rtn); + if(rtn!=null && !rtn.trim().equals("")){ + log.debug("template found "); + HashMap para=new HashMap(); + para.put("name", strategy_name); + script=StringPlaceHolder.parse(rtn, para); + //log.debug("script:"+script); + } + + } + + + HashMap rtn=new HashMap(); + + iedb.connectDB(); + + Map existing=iedb.getStrategy(strategy_name); + + if(existing!=null && existing.size()>0){ + throw new Exception("ERROR: Strategy name: "+strategy_name+" alerady existing, please try different name"); + } + String subfolder=iedb.getFolderName(Integer.parseInt(folder_id)); + + String filename=strategy_name+new SVNIExecXML().getExtension(); + String filepath=folder+((subfolder!=null)?subfolder+File.separator:"")+filename; + + if(!new File(filepath).isFile()){ + createContent(filepath,script); + } + + int id=iedb.createStrategy(Integer.parseInt(folder_id),strategy_name, filename); + + synchrnizeSVN(strategy_name,script,null,id); + + //if(false){ + Map data=iedb.getStrategy(id); + + //String filepath1=(String)data.get("script_file"); + //filepath1=folder+""+filepath1; + + String filepath1=(String)data.get("file_name"); + filepath1=folder+((subfolder!=null)?subfolder+File.separator:"")+filepath1; + + + String content=getContent(filepath1); + Map func_data=new HashMap(); + func_data.put("content", content); + func_data.put("data", data); + + + Vector functions=new Vector(); + functions.add(data); + + + rtn.put("strategies", functions); + boolean readonly=true; + rtn.put("strategy_data", getStrategy(id,!readonly)); + + + //} + //rtn=getRFunction(id); + //String content=getContent(filepath); + //rtn.put("content", content); + //rtn.put("data", data); + //return getRFunction(id); + + return rtn; + }catch(Exception e){ + ClientError.reportError(e, "folder_id:"+folder_id+" fname:"+strategy_name+" script:"+script); + throw e; + }finally{ + iedb.closeDB(); + } + + } + + + + public Map modifyStrategy(int strategy_id, String script, String message) throws Exception { + + String folder=Config.getString("iexec_source_folder"); + IExecDB iedb=IExecDB.getIExcecDB(); + SchedulerDB sdb=SchedulerDB.getSchedulerDB(); + try{ + + HashMap rtn=new HashMap(); + + iedb.connectDB(); + sdb.connectDB(); + + String usr=new SchedulerMgmt(getRequest()).getAuthorizedUser(sdb); + String lockedby=getLockedBy(strategy_id, sdb, iedb); + if(lockedby!=null && !usr.equalsIgnoreCase(getLockedBy(strategy_id, sdb, iedb))){ + throw new Exception("This function is currently locked by user "+lockedby); + } + + Map data=iedb.getStrategy(strategy_id); + + + String strategy_name=(String)data.get("strategy_name"); + //String filename=(String)data.get("file_name"); + String filename=strategy_name+new SVNIExecXML().getExtension(); + String sub_folder=""; + try{ + sub_folder=iedb.getFolderName((Integer)data.get("folder_id")); + + }catch(Exception e){} + + + String filepath=folder+((sub_folder!=null)?sub_folder+File.separator:"")+filename; + createContent(filepath,script); + + synchrnizeSVN(strategy_name,script,message,strategy_id); + + Map data1=iedb.getStrategy(strategy_id);; //this gets with latest tag after SVN sycnrhonize + + Vector v=new Vector(); + v.add(data1); + + //return listScheduledItems(); + rtn.put("rfunctions", v); + + return rtn; + + //String content=getContent(filepath); + //rtn.put("content", content); + //rtn.put("data", data); + + }catch(Exception e){ + e.printStackTrace(); + //ClientErrorMgmt.reportError(e,null); + throw e; + + }finally{ + sdb.closeDB(); + iedb.closeDB(); + } + + } + + + public Map createFolder(String folder_name, String group_id) throws Exception { + IExecDB iedb=IExecDB.getIExcecDB(); + try{ + String folder=Config.getString("iexec_source_folder"); + //String oldfile=folder+old_folder; + String newfile=folder+folder_name; + + if(new File(newfile).isDirectory() || new File(newfile).isFile()){ + throw new Exception("Folder already existing folder failed....Path:"+newfile); + }else{ + iedb.connectDB(); + + int folder_id=iedb.getFolderID(folder_name); + if(folder_id<=0){ + folder_id=iedb.createFolder(folder_name, group_id); + } + File nfile=new File(newfile); + if(!nfile.isDirectory() && !nfile.isFile()) { + nfile.mkdirs(); + } + HashMap rtn=new HashMap(); + List folders=iedb.listOfFolders(group_id); + rtn.put("folders", folders); + rtn.put("folder_id", folder_id); + return rtn; + } + + }catch(Exception e){ + ClientError.reportError(e, "folder_id:"+folder_name+" group_id:"+group_id); + throw e; + }finally{ + iedb.closeDB(); + } + } + + public boolean moveFolder(int folder_id, String new_group_id) throws Exception { + + IExecDB iedb=IExecDB.getIExcecDB(); + + try{ + + iedb.connectDB(); + iedb.moveFolder(folder_id, new_group_id); + return true; + + }catch(Exception e){ + ClientError.reportError(e, "folder_id:"+folder_id+" new_group_id:"+new_group_id); + throw e; + }finally{ + iedb.closeDB(); + } + } + + + public boolean moveFile2Folder(int strategy_id, int new_folder_id) throws Exception { + IExecDB iedb=IExecDB.getIExcecDB(); + + try{ + + String folder=Config.getString("iexec_source_folder"); + + iedb.connectDB(); + + Map func=iedb.getStrategy(strategy_id); + String src_folder=null; + try{ + src_folder=iedb.getFolderName((Integer)func.get("folder_id")); + }catch(Exception e){} + String dest_folder=iedb.getFolderName(new_folder_id); + String script_file=(String)func.get("file_name"); + if(src_folder.equals(dest_folder)) { + throw new Exception("Moving failed! source and destination are the same.."); + } + + String oldfile=folder+((src_folder!=null)?src_folder+File.separator:"")+script_file; + String newfile=folder+((dest_folder!=null)?dest_folder+File.separator:"")+script_file; + + + log.debug("old file:"+oldfile+" new :"+newfile); + + if(new File(oldfile).renameTo(new File(newfile))){ + log.debug("new folder_id:"+strategy_id+" function_id:"+new_folder_id); + iedb.updateStrategyFolder(strategy_id,new_folder_id); + }else{ + throw new Exception("Moving failed failed...."); + } + + return true; + }catch(Exception e){ + ClientError.reportError(e, "function_id:"+strategy_id+" new_folder_id:"+new_folder_id); + throw e; + }finally{ + iedb.closeDB(); + } + + + } + + public void setGroupOrder(Vector groupids) throws Exception { + IExecDB iedb=IExecDB.getIExcecDB(); + try{ + iedb.connectDB(); + iedb.setGroupOrder(groupids); + }catch(Exception e){ + ClientError.reportError(e, null); + throw e; + }finally{ + iedb.closeDB(); + } + } + + + public boolean updateParameter(ArrayList data, String strategy_name) throws Exception { + + + IExecDB iedb=IExecDB.getIExcecDB(); + + try{ + + + iedb.connectDB(); + iedb.addParameters(data, strategy_name); + return true; + + + //String content=getContent(filepath); + //rtn.put("content", content); + //rtn.put("data", data); + + + + }catch(Exception e){ + //e.printStackTrace(); + //ClientErrorMgmt.reportError(e,null); + + throw e; + }finally{ + + iedb.closeDB(); + } + + } + + private boolean synchrnizeSVN(String strategy_name, String xml, String message, int strategy_id) throws Exception { + + SchedulerDB sdb=SchedulerDB.getSchedulerDB(); + + try{ + sdb.connectDB(); + + String svnuser=Config.getString("svn_user_r"); + String svnpwd=Config.getString("svn_pwd_r"); + + SchedulerMgmt smgmt=new SchedulerMgmt(getRequest()); + + String clientip=smgmt.getPeerIPAddress(); + String user=getAuthenticatedUser(); + + if(message==null){ + message="created on IP:"+clientip+" By:"+user; + } + + + //Map u=sdb.getSVNUser(clientip); + Map u=sdb.getSVNUser4WikiUser(user); + if(u!=null && u.get("svn_username")!=null && u.get("svn_password")!=null){ + svnuser=(String)u.get("svn_username"); + svnpwd=(String)u.get("svn_password"); + } + //rtn.put("usr",rs5.getString("svn_username")); + //rtn.put("pwd",rs5.getString("svn_password")); + + + SVNIExecXML sync=new SVNIExecXML(svnuser,svnpwd); + sync.syncFile(strategy_name, xml,message); + + return true; + }catch(Exception e){ + log.error("Error while committing function into SVN "); + throw e; + }finally{ + try{sdb.closeDB();}catch(Exception e) { log.error("error while cloing db:"+e.getMessage()); } + } + + } + + public boolean removeContract(String strategy_name, String contract )throws Exception { + IExecDB iedb=IExecDB.getIExcecDB(); + try{ + iedb.connectDB(); + iedb.removeContract(strategy_name, contract); + + return true; + }catch(Exception e){ + + throw e; + }finally{ + iedb.closeDB(); + } + } + + + public Vector getSVNLogs(String function_name) throws Exception { + SVNIExecXML sync=new SVNIExecXML(); + TreeMap rtn=new TreeMap(); + Vector d=sync.log(function_name); + if(d!=null && d.size()>0){ + + for(Iterator i=d.iterator();i.hasNext();){ + SVNLogEntry entry=i.next(); + SimpleDateFormat format=new SimpleDateFormat("dd-MMM-yyyy hh:mm a"); + ValueObject vo=new ValueObject(); + HashMap data=new HashMap(); + data.put("author", entry.getAuthor()); + data.put("date", format.format(entry.getDate())); + data.put("message", entry.getMessage()); + data.put("revision", entry.getRevision()); + data.put("function_name", function_name); + + String path=null; + if(entry.getChangedPaths()!=null){ + for(Iterator it=entry.getChangedPaths().keySet().iterator();it.hasNext();){ + //if(entry.getChangedPaths().keySet().size()>0){ + String thispath=(String)it.next(); + thispath=thispath.substring(thispath.lastIndexOf("/")+1); + path=(path==null)?thispath:path+","+thispath; + } + if(path!=null){ + + data.put("path", path); + } + } + rtn.put(entry.getRevision(), data); + //System.out.println(" Rev:"+entry.getRevision()+" Date:"+format.format(entry.getDate())+" User:"+entry.getAuthor()+" Msg:"+entry.getMessage()); + } + return new Vector(rtn.descendingMap().values()); + }else{ + return null; + } + + } + public String getScriptRev(String strategy_name, String revision,boolean flag,String path) throws Exception { + try{ + SVNIExecXML sync=new SVNIExecXML(); + String script=null; + if(flag) + script=sync.getScript(strategy_name, Long.parseLong(revision),path); + else + //script=sync.diffWC(function_name, Long.parseLong(revision)); + script=sync.getWhatChanged(strategy_name, Long.parseLong(revision)); + if(script==null){ + script="No changes were made or first revision of this function"; + } + + return script; + }catch(Exception e){ + e.printStackTrace(); + throw e; + } + + } + + private Map getDataBundle4Strategy(Map data, IExecDB iedb,SchedulerDB sdb) throws Exception{ + HashMap rtn=new HashMap(); + String folder=Config.getString("iexec_source_folder"); + String subfolder=""; + if(data.get("folder_id")!=null){ + subfolder=iedb.getFolderName((Integer)data.get("folder_id")); + } + int strategy_id=(Integer)data.get("id"); + + + String strategy_name=(String)data.get("strategy_name"); + String original_strategy=(String)data.get("strategy_name"); + + String parent_name=(String)data.get("parent_strategy"); + boolean child_strategy=false; + if(parent_name!=null && !parent_name.equals("")) { + Map parent_data=iedb.getStrategy(parent_name); + strategy_name=(String)parent_data.get("strategy_name"); + subfolder=iedb.getFolderName((Integer)parent_data.get("folder_id")); + child_strategy=true; + } + + + + + // String filepath=(String)data.get("file_name"); + String filepath=strategy_name+new SVNIExecXML().getExtension(); + + log.debug("getDataBundle4Strategy() child_strategy:"+child_strategy); + log.debug("getDataBundle4Strategy() content strategy_name:"+strategy_name); + log.debug("getDataBundle4Strategy() Content:"+filepath); + + + String scriptname=filepath; + filepath=folder+((subfolder!=null)?subfolder+File.separator:"")+filepath; + String content=""; + log.debug("getDataBundle4Strategy() filepath:"+filepath); + + try{ + content=getContent(filepath); + }catch(Exception e){ + content=""+e.getMessage()+""; + } + //log.debug("getDataBundle4Strategy() Content:"+content); + + String tl=(String)data.get("lockedby"); + if(tl==null || (tl!=null && tl.equals("")) ){ + data.put("lockedby",getLockedBy(strategy_id, sdb, iedb)); + } + + + rtn.put("element_ph_found", false); + + if (child_strategy) { + if(StringPlaceHolder4ChildStrategy.getElementPH(content).size()>0){ + rtn.put("element_ph_found", true); + } + } + else { + if(StringPlaceHolder.getElementPH(content).size()>0){ + rtn.put("element_ph_found", true); + } + } + + + rtn.put("content", content); + rtn.put("data", data); + rtn.put("isParent", iedb.isParent(original_strategy)); + + try{ + rtn.put("isAuthorized", new SchedulerMgmt(getRequest()).isAuthorizedUser(sdb)); + rtn.put("authorizedUser", new SchedulerMgmt(getRequest()).getAuthorizedUser(sdb)); + }catch(Exception e){ + //log.error("Error:"+e.getMessage()); + } + Vector v=new Vector(); + v.add("Default"); + Vector v1=iedb.getUniqueContracts(original_strategy); + if(v1.contains("Default")) v1.remove("Default"); + v.addAll(v1); + rtn.put("contracts",v); + + + return rtn; + + } + + + public boolean renameStrategy(String new_st_name, int strategy_id) throws Exception { + String folder=Config.getString("iexec_source_folder"); + IExecDB iedb=IExecDB.getIExcecDB(); + SchedulerDB sdb=SchedulerDB.getSchedulerDB(); + + try{ + + + + iedb.connectDB(); + + Map existing=iedb.getStrategy(new_st_name); + if(existing!=null && existing.size()>0){ + throw new Exception("ERROR: Strategy name: "+new_st_name+" alerady existing, please try different name"); + } + HashMap rtn=new HashMap(); + iedb.connectDB(); + sdb.connectDB(); + + String usr=new SchedulerMgmt(getRequest()).getAuthorizedUser(sdb); + String lockedby=getLockedBy(strategy_id, sdb, iedb); + if(lockedby!=null && !usr.equalsIgnoreCase(getLockedBy(strategy_id, sdb, iedb))){ + throw new Exception("This strategy is currently locked by user "+lockedby); + } + + Map old_data=iedb.getStrategy(strategy_id); + + String parent=(String)old_data.get("parent_strategy"); + if(parent!=null && !parent.trim().equals("")){ + iedb.renameStrategy(strategy_id, new_st_name, null); + }else{ + String new_filename=new_st_name+new SVNIExecXML().getExtension(); + String old_filename=(String)old_data.get("file_name"); + String old_strategy_name=(String)old_data.get("strategy_name"); + String sub_folder=""; + try{ + sub_folder=iedb.getFolderName((Integer)old_data.get("folder_id")); + }catch(Exception e){} + + + String oldfile=folder+((sub_folder!=null)?sub_folder+File.separator:"")+old_filename; + String newfile=folder+((sub_folder!=null)?sub_folder+File.separator:"")+new_filename; + + Map script_data=getDataBundle4Strategy(old_data,iedb,sdb); + String script=(String)script_data.get("content"); + String message=""; + //if(script!=null){ + //script="#"+new_st_name+" "+System.getProperty("line.separator")+script; + message="Renamed "+old_strategy_name+" --> " +new_st_name; + //} + + + log.debug("old file:"+oldfile+" new :"+newfile); + + if(new File(oldfile).renameTo(new File(newfile))){ + + //iedb.renameStrategy(strategy_id, new_st_name, new_filename); + iedb.renameParentStrategy(strategy_id, new_st_name, new_filename,old_strategy_name); + + }else{ + throw new Exception("Renaming failed"); + } + + + Map new_data=iedb.getStrategy(strategy_id); + + + //String filepath=folder+((sub_folder!=null)?sub_folder+File.separator:"")+new_filename; + //createContent(filepath,script,sub_folder,new_filename); + + renameSVN(old_strategy_name,new_st_name,script,message,strategy_id); + + } + //Map data1=iedb.getStrategy(strategy_id); //this gets with latest tag after SVN sycnrhonize + + //Vector v=new Vector(); + //v.add(data1); + + //return listScheduledItems(); + //rtn.put("strategies", v); + + + return true; + + + }catch(Exception e){ + e.printStackTrace(); + //ClientErrorMgmt.reportError(e,null); + + throw e; + }finally{ + sdb.closeDB(); + iedb.closeDB(); + } + } + + private boolean renameSVN(String old_strategy_name,String new_strategy_name, String strategy, String message, int function_id) throws Exception { + + SchedulerDB sdb=SchedulerDB.getSchedulerDB(); + + try{ + sdb.connectDB(); + + String svnuser=Config.getString("svn_user_r"); + String svnpwd=Config.getString("svn_pwd_r"); + + SchedulerMgmt smgmt=new SchedulerMgmt(getRequest()); + + String clientip=smgmt.getPeerIPAddress(); + String user=getAuthenticatedUser(); + + if(message==null){ + message="created on IP:"+clientip+" By:"+user; + } + + + //Map u=sdb.getSVNUser(clientip); + Map u=sdb.getSVNUser4WikiUser(user); + if(u!=null && u.get("svn_username")!=null && u.get("svn_password")!=null){ + svnuser=(String)u.get("svn_username"); + svnpwd=(String)u.get("svn_password"); + } + //rtn.put("usr",rs5.getString("svn_username")); + //rtn.put("pwd",rs5.getString("svn_password")); + + SVNIExecXML sync=new SVNIExecXML(svnuser,svnpwd); + sync.renameFile(old_strategy_name, new_strategy_name, strategy, message); + + + + + return true; + }catch(Exception e){ + log.error("Error while committing function into SVN "); + throw e; + }finally{ + try{sdb.closeDB();}catch(Exception e) { log.error("error while cloing db:"+e.getMessage()); } + } + + + } + + private void createContent(String fullpath, String script) throws Exception { + + Writer output = null; + File file = new File(fullpath); + output = new BufferedWriter(new FileWriter(file)); + output.write(script); + output.close(); + + } + + public String getStrategyWiki(String strategy) throws Exception { + WikiRFunctionManual wiki=new WikiRFunctionManual(); + //String rtn=wiki.getWikiHTML(strategy); + + String username=Config.getString("wiki.username"); + String password=Config.getString("wiki.password"); + String wikiurl=Config.getString("wiki.wikiurl"); + + String rtn=wiki.getWikiHTML(username, password, wikiurl, strategy); + return rtn; + + } + + public Vector getStrategies(String strategy_names,boolean readonly) throws Exception { + + IExecDB iedb=IExecDB.getIExcecDB(); + SchedulerDB sdb=SchedulerDB.getSchedulerDB(); + try{ + + StringTokenizer st=new StringTokenizer(strategy_names,","); + Vector fnames=new Vector(); + while(st.hasMoreTokens()){ + fnames.add(st.nextToken()); + } + Vector rtn=new Vector(); + iedb.connectDB(); + sdb.connectDB(); + //Map data=rfdb.getRFunction(function_id); + //rtn.put("lockedby", getLockedBy(function_id)) + Vector data1=iedb.getStrategies(fnames); + + String usr=new SchedulerMgmt(getRequest()).getAuthorizedUser(sdb); + + for(Iterator i=data1.iterator();i.hasNext();){ + Map data=(Map)i.next(); + Map func=getDataBundle4Strategy(data,iedb,sdb); + int function_id=(Integer)data.get("id"); + String usr_alreadylocked=(String)((Map)func.get("data")).get("lockedby"); + if(!readonly){ + if(usr_alreadylocked!=null && !usr_alreadylocked.equals("") && !usr_alreadylocked.equalsIgnoreCase(usr)){ + //dont relock + }else{ + refreshCache(function_id, 300,usr); + } + }else{ + func.put("readonly", true); + } + + rtn.add(func); + } + return rtn; + + + }catch(Exception e){ + ClientError.reportError(e, null); + throw e; + }finally{ + iedb.closeDB(); + sdb.closeDB(); + } + + } + + + + public Map getContractComm() throws Exception { + IExecDB iedb=IExecDB.getIExcecDB(); + try{ + + + iedb.connectDB(); + TreeMap rtn=new TreeMap(); + + + TreeMap cont=new TreeMap(); + + Vector comm=iedb.getCommodityTree(); + Vector contract=iedb.getContractTree(); + //TreeMap t=new TreeMap(); + for(Iterator i=contract.iterator();i.hasNext();){ + Map row=(Map)i.next(); + String comm1=(String)row.get("commodity"); + String contract1=(String)row.get("contract"); + Vector v; + + if(cont.get(comm1)==null) { v=new Vector();cont.put(comm1.trim(),v); } + else{v=(Vector)cont.get(comm1);} + v.add(comm1+(v.size()+1)+"S"); + //t.put(contract1, comm1); + } + + rtn.put("commodity", comm); + rtn.put("contract", cont); + //rtn.put("all", t); //this is not needed, just for testing, remove related code to this line + + return rtn; + }catch(Exception e){ + e.printStackTrace(); + throw e; + }finally{ + iedb.closeDB(); + } + + + } + + + private String getContent(String fullpath) throws Exception { + BufferedReader in = new BufferedReader(new FileReader(fullpath)); + String rtn=""; + String str; + while ((str = in.readLine()) != null) { + rtn+=str+"\r\n"; + } + in.close(); + return rtn; + } + + + public boolean lockFunction(int strategy_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(strategy_id, seconds,usr); + + //rtn=cachedPeers.getMatching("^[A-Za-z0-9]+$"); + }else{ + rfdb.updateLock(strategy_id, usr); + } + return true; + }catch(Exception e){ + + throw e; + }finally{ + sdb.closeDB(); + rfdb.closeDB(); + } + } + + + public boolean unLockStrategyFromCache(int function_id) throws Exception { + SchedulerDB sdb=SchedulerDB.getSchedulerDB(); + try{ + sdb.connectDB(); + String usr=new SchedulerMgmt(getRequest()).getAuthorizedUser(sdb); + removeLockFromCache(function_id,usr); + return true; + }catch(Exception e){ + e.printStackTrace(); + throw e; + }finally{ + sdb.closeDB(); + + } + + } + + private void removeLockFromCache(int strategy_id, String usr ) throws Exception { + //String ky=usr+"_"+function_id; + String ky=usr+strategy_id; + if(getLockCache().get(ky)!=null)getLockCache().remove(ky); + + } + + private String getLockedBy(int iexec_id,SchedulerDB sdb,IExecDB iedb) throws Exception { + //SchedulerDB sdb=SchedulerDB.getSchedulerDB(); + //RFunctionDB rfdb=RFunctionDB .getRFunctionDB(); + try{ + //rfdb.connectDB(getDBName()); + //String usr=new SchedulerMgmt(getRequest()).getAuthorizedUser(sdb); + Map data=iedb.getStrategy(iexec_id); + String rtn=null; + if(data.get("lockedby")!=null ){ + //refreshCache(function_id, seconds,usr); + //rtn=cachedPeers.getMatching("^[A-Za-z0-9]+$"); + rtn=(String)data.get("lockedby"); + }else{ + //rfdb.updateLock(function_id, usr); + Map caches=getLockCache().getMatching("^[A-Za-z0-9]+$"); + if(caches!=null){ + for(Iterator i=caches.keySet().iterator();i.hasNext();){ + String ky=(String)i.next(); + + Map d=(Map)caches.get(ky); + int f_id=(Integer)d.get(IExecMgmt.STRATEGY_ID); + long dur=(Long)d.get(IExecMgmt.LOCK_DURATION); + String usr1=(String)d.get(IExecMgmt.USER); + //if(ky.startsWith(usr)){ + if(f_id==iexec_id) rtn=usr1; + //} + } + } + } + return rtn; + }catch(Exception e){ + + throw e; + + }finally{ + //sdb.closeDB(); + //rfdb.closeDB(); + } + } + + private void refreshCache(int strategy_id, long seconds, String usr ) throws Exception { + + //String ky=usr+"_"+function_id; + String ky=usr+strategy_id; + + HashMap h=new HashMap(); + h.put(IExecMgmt.STRATEGY_ID, strategy_id); + h.put(IExecMgmt.LOCK_DURATION, seconds); + h.put(IExecMgmt.USER, usr); + + IElementAttributes att= getLockCache().getDefaultElementAttributes(); + att.setMaxLifeSeconds(seconds); + if(getLockCache().get(ky)!=null)getLockCache().remove(ky); + getLockCache().put(ky,h,att); + + + } + + private static String STRATEGY_ID="function_id"; + private static String LOCK_DURATION="duration"; + private static String USER="user"; + + private static JCS lockcache=null; + + private static JCS getLockCache() throws Exception { + if(IExecMgmt.lockcache==null) IExecMgmt.lockcache=JCS.getInstance(IExecMgmt.class.getName()); + return IExecMgmt.lockcache; + } + + + + + + +} + + + diff --git a/project/bldb/src/main/java/com/fe/client/JspXHRRequest.java b/project/bldb/src/main/java/com/fe/client/JspXHRRequest.java new file mode 100644 index 0000000..7da5a37 --- /dev/null +++ b/project/bldb/src/main/java/com/fe/client/JspXHRRequest.java @@ -0,0 +1,248 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fe.client; + +import java.util.Date; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + +import org.json.JSONArray; +import org.json.JSONObject; + +import com.fourelementscapital.db.InfrastructureDB; +import com.fourelementscapital.db.RFunctionDB; +import com.fourelementscapital.db.SchedulerDB; +import com.fourelementscapital.scheduler.error.ClientError; + + + +/** + * This class is used for making several XHR requests, for example jquery autocomplete and autosuggest feature. + * @author Administrator + * + */ +public class JspXHRRequest extends Authenticated { + + /** + * make sure you are not passing null request, otherwise it won't work properly + * @param request + * @throws Exception + */ + public JspXHRRequest(HttpServletRequest request) throws Exception { + super(request); + } + + + /** + * Autocomplete search for scheduler name search on UI + * @return + * @throws Exception + */ + public String getXHRDataScheduler() throws Exception { + + SchedulerDB sdb=SchedulerDB.getSchedulerDB(); + + try{ + sdb.connectDB(); + JSONArray rtn=new JSONArray(); + if( getRequest().getParameter("term")!=null){ + if(getRequest().getParameter("term")!=null){ + String kword=getRequest().getParameter("term"); + List data=sdb.getAutoCompleteXHR(kword); + for(Iterator it=data.iterator();it.hasNext(); ){ + Map record=it.next(); + //System.out.println("SchedulerMgmt.getXHRDataScheduler():data:"+record); + rtn.put(record); + } + } + } + return rtn.toString(); + + }catch(Exception e){ + ClientError.reportError(e, null); + throw e; + }finally{ + sdb.closeDB(); + } + + } + + + + /** + * Generic XHR data returned + * @return + * @throws Exception + */ + public String getXHRDataR() throws Exception { + + RFunctionDB rfdb=RFunctionDB .getRFunctionDB(); + + try{ + rfdb.connectDB(); + JSONArray rtn=new JSONArray(); + if( getRequest().getParameter("term")!=null){ + if(getRequest().getParameter("term")!=null){ + String kword=getRequest().getParameter("term"); + List data=rfdb.autoCompleteFunctions(kword); + for(Iterator it=data.iterator();it.hasNext(); ){ + Map record=it.next(); + rtn.put(record); + } + } + } + return rtn.toString(); + + }catch(Exception e){ + ClientError.reportError(e, null); + throw e; + }finally{ + rfdb.closeDB(); + } + } + + + + /** + * Notification related data in JSON format. + * @return + * @throws Exception + */ + public String getNotificationData() throws Exception { + + SchedulerDB sdb=SchedulerDB.getSchedulerDB(); + InfrastructureDB infrastructureDB = InfrastructureDB.getInfrastructureDB(); + + try{ + sdb.connectDB(); + JSONObject json=new JSONObject(); + JSONArray rtn=new JSONArray(); + + infrastructureDB.connectDB(); + List themes = infrastructureDB.getThemeByUsernameAccess(getAuthenticatedUser()); + + List data=sdb.getFailedLast2Days(themes); + for(Iterator it=data.iterator();it.hasNext(); ){ + Map record=it.next(); + rtn.put(record); + } + json.put("failed_scripts", rtn); + return json.toString(); + + }catch(Exception e){ + //ClientErrorMgmt.reportError(e, null); + //throw e; + //log.error(""); + throw e; + }finally{ + sdb.closeDB(); + infrastructureDB.closeDB(); + } + } + + + /** + * JSON formatted data. + * @return + * @throws Exception + */ + public String getJSON() throws Exception { + String callback=getRequest().getParameter("callback"); + SchedulerDB sdb=SchedulerDB.getSchedulerDB(); + + try{ + sdb.connectDB(); + JSONArray rtn=new JSONArray(); + if( getRequest().getParameter("search_name")!=null || getRequest().getParameter("search_id")!=null){ + String query=""; + if(getRequest().getParameter("search_name")!=null){ + String kword=getRequest().getParameter("search_name"); + query = sdb.getTop20IdNameByNameQuery(kword); + } + if(getRequest().getParameter("search_id")!=null){ + String kword=getRequest().getParameter("search_id"); + query = sdb.getTop20IdNameByIdQuery(kword); + } + List data=sdb.getSingleColData(query); + + for(Iterator it=data.iterator();it.hasNext(); ){ + String rec=(String)it.next(); + rtn.put(rec); + } + } + return callback+"("+rtn.toString()+");"; + }catch(Exception e){ + ClientError.reportError(e, null); + throw e; + }finally{ + sdb.closeDB(); + } + } + + + + /** + * returns execution logs for the selected log item in the queue + * @param scheduler_id + * @param trigger_time + * @return + * @throws Exception + */ + public String getExecutionLogs(int scheduler_id, long trigger_time) throws Exception { + SchedulerDB sdb=SchedulerDB.getSchedulerDB(); + try{ + sdb.connectDB(); + JSONArray rtn=new JSONArray(); + List list=sdb.getSchedulerExeLogs(scheduler_id,trigger_time); + rtn.put(list); + return rtn.toString(); + }catch(Exception e){ + ClientError.reportError(e, null); + throw e; + }finally{ + sdb.closeDB(); + } + + } + + + /** + * List last 2 days failed details. + * @param scheduelr_id + * @param stat + * @return + * @throws Exception + */ + public String listLast2DaysFailedDetails(long scheduelr_id, String stat) throws Exception { + + SchedulerDB sdb=SchedulerDB.getSchedulerDB(); + try{ + sdb.connectDB(); + String datequery= sdb.getHistoryQueueLogsDateQuery(scheduelr_id, stat); + Date now=new Date(); + List list=sdb.listOfHistoryQueueLogs(now.getTime(),datequery); + JSONObject json=new JSONObject(); + json.put("list", list); + return json.toString(); + + }catch(Exception e){ + ClientError.reportError(e, null); + throw e; + }finally{ + sdb.closeDB(); + } + + } + +} + + + diff --git a/project/bldb/src/main/java/com/fe/client/LoginMgmt.java b/project/bldb/src/main/java/com/fe/client/LoginMgmt.java new file mode 100644 index 0000000..79c6b1a --- /dev/null +++ b/project/bldb/src/main/java/com/fe/client/LoginMgmt.java @@ -0,0 +1,226 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fe.client; + +import java.net.URLDecoder; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; + +import org.directwebremoting.WebContextFactory; +import org.directwebremoting.util.Logger; + +import com.fe.common.Constant; +import com.fourelementscapital.auth.SuperUserAuthentication; +import com.fourelementscapital.auth.WikiAuthentication; + + +/** + * This class used authenticating the user. + * @author Administrator + * + */ +public class LoginMgmt { + + + private Logger log=Logger.getLogger(LoginMgmt.class); + + /** + * for DWR call + */ + public LoginMgmt(){ + + } + + + /** + * for JSP or internal call + * @param request + */ + public LoginMgmt(HttpServletRequest request){ + this.request=request; + } + + //private static String dbname=Config.getString("db_wiki"); + private HttpServletRequest request=null; + + /** + * returns wiki database name for authentication and validation purpose + * @return + */ + //public static String getDBName() { + // return dbname; + //} + + + + /** + * Login validation for given username and password + * @param user + * @param password + * @return + * @throws Exception + */ + public Map login(String user,String password) throws Exception { + + HashMap rtn=new HashMap(); + try{ + //log.debug("connecting wiki db...db:"+wdb.getDriver(dbname)+" driver:"); + + //log.debug("connected catalog:"+wdb.connection().getCatalog()); + + boolean success= WikiAuthentication.validateUser(user, password); + if(!success){ + rtn.put("message", "Invalid username or password"); + }else{ + HttpSession session=getRequest().getSession(true); + session.setMaxInactiveInterval(24*60*60); //1 day; + session.setAttribute(Constant.SESSION_LOGGED_USER, user); + boolean cookielogin=false; + session.setAttribute(Constant.SESSION_LOGGED_HEADER, getHeaders(getRequest(),cookielogin)); + String pss=WikiAuthentication.getEncryptedPwd(user); + rtn.put("fesessionuid", pss); + } + rtn.put("loggedin", success); + return rtn; + }catch(Exception e){ + e.printStackTrace(); + throw e; + } + } + + + + private Map getHeaders(HttpServletRequest req, boolean cookielogin) throws Exception { + HashMap rtn=new HashMap(); + Enumeration h=req.getHeaderNames(); + while(h.hasMoreElements()){ + String header=h.nextElement(); + rtn.put(header, req.getHeader(header)); + } + rtn.put(Constant.SESSION_LOGGED_HEADER_COOKIELOGIN, cookielogin); + rtn.put(Constant.SESSION_LOGGED_HEADER_IP,getRequest().getRemoteAddr()); + return rtn; + } + + + /** + * Logging in as super user to over-ride some of the user privileged options + * + * @param user + * @param password + * @param newpassword + * @return + * @throws Exception + */ + public Map loginSuperUser(String user,String password,String newpassword) throws Exception { + + + HashMap rtn=new HashMap(); + try{ + + boolean success=false; + if(user.equalsIgnoreCase("administrator") && newpassword!=null && !newpassword.equals("")){ + success = SuperUserAuthentication.changePwd(password,newpassword); + + }else{ + success = SuperUserAuthentication.validateOrSet(password); //validation implementation to be done.. + } + if(!success){ + rtn.put("message", "Invalid username or password"); + }else{ + HttpSession session=getRequest().getSession(true); + session.setAttribute(Constant.SESSION_LOGGED_SUPERUSER, user); + } + rtn.put("loggedin", success); + return rtn; + }catch(Exception e){ + e.printStackTrace(); + throw e; + } + finally{ + //wdb.closeDB(); + } + + } + + + + /** + * validates remembered password on browser cookie, the password is stored in md5 hash value + * @param user + * @param password + * @return + * @throws Exception + */ + public Map validateRememberedUser(String user,String password) throws Exception { + + HashMap rtn=new HashMap(); + try{ + + boolean success=false; + String passcode=URLDecoder.decode(password); + log.debug("user:"+user+" passcode:"+passcode); + + String pss=WikiAuthentication.getEncryptedPwd(user); + + if(!passcode.equals(pss)){ + rtn.put("message", "Invalid username or password"); + }else{ + + success=true; + HttpSession session=getRequest().getSession(true); + session.setMaxInactiveInterval(24*60*60); //1 day; + session.setAttribute(Constant.SESSION_LOGGED_USER, user); + + boolean cookielogin=true; + session.setAttribute(Constant.SESSION_LOGGED_HEADER, getHeaders(getRequest(),cookielogin)); + + //wdb.getEncryptedPwd(user_name) + //String pss=wdb.getEncryptedPwd(user); + rtn.put("fesessionuid", pss); + } + rtn.put("loggedin", success); + log.debug("rtn:"+rtn); + return rtn; + }catch(Exception e){ + e.printStackTrace(); + throw e; + } + + } + + + + /** + * a standard logout + * @throws Exception + */ + public void logout() throws Exception { + HttpSession session=getRequest().getSession(); + session.removeAttribute(Constant.SESSION_LOGGED_USER); + session.invalidate(); + + } + + + private HttpServletRequest getRequest() throws Exception { + return (request == null && WebContextFactory.get()!=null) ? WebContextFactory.get().getHttpServletRequest() : request; + + } + + + + +} + + + diff --git a/project/bldb/src/main/java/com/fe/client/MenuMgmt.java b/project/bldb/src/main/java/com/fe/client/MenuMgmt.java new file mode 100644 index 0000000..c3ab771 --- /dev/null +++ b/project/bldb/src/main/java/com/fe/client/MenuMgmt.java @@ -0,0 +1,96 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fe.client; + +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + +import com.fourelementscapital.db.SchedulerDB; + +/** + * This class provides data for building menu on the web page. + * + */ +public class MenuMgmt extends Authenticated { + + /** + * default constructor + * @throws Exception + */ + public MenuMgmt() throws Exception { + super(); + } + + /** + * constructor for jsp + * @param request + * @throws Exception + */ + public MenuMgmt(HttpServletRequest request) throws Exception { + super(request); + } + + + /** + * update user preference, this preference available on pressing + button on the menu bar + * @param key + * @param val + * @return + * @throws Exception + */ + public boolean updateUserPreference(String key, String val) throws Exception { + + + SchedulerDB sdb=SchedulerDB.getSchedulerDB(); + try{ + String user=getAuthenticatedUser(); + sdb.connectDB(); + sdb.userPrefSaveOrUpdate(user, key, val); + return true; + }catch(Exception e){ + e.printStackTrace(); + throw e; + } + finally{ + sdb.closeDB(); + } + + + } + + + /** + * get user preference to set necessary settings in UI + * @return + * @throws Exception + */ + public Map getUserPref() throws Exception { + + SchedulerDB sdb=SchedulerDB.getSchedulerDB(); + try{ + String user=getAuthenticatedUser(); + sdb.connectDB(); + Map data=sdb.userPrefGetAll(user); + return data; + }catch(Exception e){ + e.printStackTrace(); + throw e; + } + finally{ + sdb.closeDB(); + } + + + } + + +} + + + diff --git a/project/bldb/src/main/java/com/fe/client/RFunctionMgmt.java b/project/bldb/src/main/java/com/fe/client/RFunctionMgmt.java new file mode 100644 index 0000000..c6cd5c6 --- /dev/null +++ b/project/bldb/src/main/java/com/fe/client/RFunctionMgmt.java @@ -0,0 +1,2743 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fe.client; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.FilenameFilter; +import java.io.StringWriter; +import java.io.Writer; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +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.StringTokenizer; +import java.util.TreeMap; +import java.util.Vector; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.zip.GZIPInputStream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +import javax.servlet.http.HttpServletRequest; + +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; +import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.apache.jcs.JCS; +import org.apache.jcs.engine.behavior.IElementAttributes; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.tmatesoft.svn.core.SVNLogEntry; + +import com.fe.common.Constant; +import com.fe.svn.SVNSync4RFunction; +import com.fe.util.WikiRFunctionManual; +import com.fourelementscapital.auth.UserThemeAccessPermission; +import com.fourelementscapital.db.RFunctionDB; +import com.fourelementscapital.db.SchedulerDB; +import com.fourelementscapital.db.vo.ValueObject; +import com.fourelementscapital.fileutils.FindStringInFiles; +import com.fourelementscapital.scheduler.config.Config; +import com.fourelementscapital.scheduler.error.ClientError; + + +/** + * This class exposes data to R function editor in Ajax call + * Remember all methods in the class are required user to be logged in + */ +public class RFunctionMgmt extends AbstractTeamOrgMgmt { + + private HttpServletRequest request=null; + private Logger log = LogManager.getLogger(RFunctionMgmt.class.getName()); + private static JCS 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 JCS 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); + + //rtn=cachedPeers.getMatching("^[A-Za-z0-9]+$"); + }else{ + rfdb.updateLock(function_id, usr); + } + return true; + }catch(Exception e){ + + throw e; + }finally{ + sdb.closeDB(); + rfdb.closeDB(); + } + + } + + + /** + * provides the user name who is currently locked R function or editing + * @param function_id + * @param sdb + * @param rfdb + * @return + * @throws Exception + */ + private String getLockedBy(int function_id,SchedulerDB sdb,RFunctionDB rfdb) throws Exception { + + try{ + + String usr=new SchedulerMgmt(getRequest()).getAuthorizedUser(sdb); + Map data=rfdb.getRFunction(function_id); + String rtn=null; + if(data.get("lockedby")!=null ){ + rtn=(String)data.get("lockedby"); + }else{ + Map caches=getLockCache().getMatching("^[A-Za-z0-9]+$"); + if(caches!=null){ + for(Iterator i=caches.keySet().iterator();i.hasNext();){ + String ky=(String)i.next(); + + Map d=(Map)caches.get(ky); + try{ + int f_id=(Integer)d.get(RFunctionMgmt.FUNCTION_ID); + long dur=(Long)d.get(RFunctionMgmt.LOCK_DURATION); + String usr1=(String)d.get(RFunctionMgmt.USER); + if(f_id==function_id) rtn=usr1; + }catch(Exception e){ + + } + + } + } + } + return rtn; + }catch(Exception e){ + + throw e; + }finally{ + + } + } + + /** + * deletes R function, which basically moves the r function to trash, internal there is flag set for example deleted=1 to identify the deleted item and later can be removed permanently + * @param function_id + * @return + * @throws Exception + */ + public boolean deleteFunction(int function_id) throws Exception { + + + RFunctionDB rfdb=RFunctionDB.getRFunctionDB(); + try{ + rfdb.connectDB(); + Map fld=createFolderIfNotExist("Trash","trash"); + int folder_id=(Integer)fld.get("folder_id"); + moveFile2Folder(function_id, folder_id); + rfdb.updateFunctionDeleted(function_id); + return true; + + }catch(Exception e){ + + throw e; + + }finally{ + + rfdb.closeDB(); + } + } + + + /** + * unlocks the function from the cache after certain time lapsed + * @param function_id + * @return + * @throws Exception + */ + public boolean unLockFunctionFromCache(int function_id) throws Exception { + SchedulerDB sdb=SchedulerDB.getSchedulerDB(); + try{ + sdb.connectDB(); + String usr=new SchedulerMgmt(getRequest()).getAuthorizedUser(sdb); + removeLockFromCache(function_id,usr); + return true; + }catch(Exception e){ + e.printStackTrace(); + throw e; + }finally{ + sdb.closeDB(); + + } + + } + + + /** + * Locking the function + * @param function_id + * @return + * @throws Exception + */ + public boolean unLockFunction(int function_id) throws Exception { + SchedulerDB sdb=SchedulerDB.getSchedulerDB(); + RFunctionDB rfdb=RFunctionDB.getRFunctionDB(); + try{ + sdb.connectDB(); + rfdb.connectDB(); + String usr=new SchedulerMgmt(getRequest()).getAuthorizedUser(sdb); + removeLockFromCache(function_id,usr); + rfdb.updateLock(function_id, null); + return true; + }catch(Exception e){ + + throw e; + }finally{ + sdb.closeDB(); + rfdb.closeDB(); + } + + } + + /** + * editor active detected, so the system can alert if the user is not saved while closing the tab without saving his work + * @return + * @throws Exception + */ + public boolean editorActiveDetected() throws Exception{ + + SchedulerDB sdb=SchedulerDB.getSchedulerDB(); + + try{ + sdb.connectDB(); + + String usr=new SchedulerMgmt(getRequest()).getAuthorizedUser(sdb); + + Map data=getLockCache().getMatching("^[A-Za-z0-9]+$"); + if(data!=null){ + for(Iterator i=data.keySet().iterator();i.hasNext();){ + String ky=(String)i.next(); + if(ky.startsWith(usr)){ + Map d=(Map)data.get(ky); + int f_id=(Integer)d.get(RFunctionMgmt.FUNCTION_ID); + long dur=(Long)d.get(RFunctionMgmt.LOCK_DURATION); + String usr1=(String)d.get(RFunctionMgmt.USER); + if(usr1.equals(usr)) {refreshCache(f_id,dur,usr1);} + } + } + } + return true; + }catch(Exception e){ + + throw e; + }finally{ + sdb.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.setMaxLifeSeconds(seconds); + if(getLockCache().get(ky)!=null)getLockCache().remove(ky); + getLockCache().put(ky,h,att); + + + } + + + /** + * Get folder panel data, this data will be used to render tree menus + * @param open_functions + * @param readonly + * @param ignore_treedata + * @return + * @throws Exception + */ + public Map getFolderPanelData(String open_functions,boolean readonly,boolean ignore_treedata) throws Exception { + + RFunctionDB rfdb=RFunctionDB.getRFunctionDB(); + + + try{ + + HashMap rtn=new HashMap(); + + rfdb.connectDB(); + + + if(!ignore_treedata){ + List functions=rfdb.listRFunctions(); + List glist=rfdb.listFunctionGroups(); + List folders=rfdb.listOfFolders(); + + HashMap ufolders=new HashMap(); + for(Map fold: folders){ + if(fold.get("folder_name")!=null)ufolders.put(fold.get("folder_name"),new Integer(0)); + } + + + List unbuildfunc=getUnPackagedSources(ufolders); + + //TreeMap groups=new TreeMap(); + Vector groups=new Vector(); + HashMap colors=new HashMap(); + 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); + colors.put(data.get("group_uid"), data.get("color_code")); + } + + rtn.put("func_2build", unbuildfunc); + rtn.put("folder_2build", ufolders); + rtn.put("rfunctions", functions); + rtn.put("folders", folders); + rtn.put("groups",groups); + rtn.put("tags",rfdb.getTags()); + rtn.put("group_colors",colors); + rtn.putAll(getThemeAccessData(rfdb)); //putting team organization data example: tags + } + if(open_functions!=null){ + rtn.put("open_functions", getRFunctions(open_functions, readonly)); + } + rtn.put("sourced_functions",getSourcedFunctions()); + rtn.put("lite",ignore_treedata); + + return rtn; + + + }catch(Exception e){ + ClientError.reportError(e, null); + throw e; + }finally{ + rfdb.closeDB(); + + } + } + + /** + * getting notifiable tags for the current user for that + * @param themes + * @param ftags + * @return + * @throws Exception + */ + public Map getItemPrivilegeNotifications(ArrayList themes,ArrayList ftags) throws Exception { + RFunctionDB rfdb=RFunctionDB.getRFunctionDB(); + try{ + rfdb.connectDB(); + return getItemPrivilegeNotifications(themes,ftags,rfdb); + //return code; + }catch(Exception e){ + ClientError.reportError(e, null); + throw e; + }finally{ + rfdb.closeDB(); + } + + + } + + + + /** + * Update function order upon dragging and dropping vertical direction in the UI. + * @param folder_id + * @param function_ids + * @return + * @throws Exception + */ + public boolean updateFunctionOrder(int folder_id, int[] function_ids) throws Exception { + RFunctionDB rfdb=RFunctionDB.getRFunctionDB(); + + try{ + rfdb.connectDB(); + + rfdb.updateFunctionOrder(function_ids); + String dest_folder=rfdb.getFolderName(folder_id); + List function_order=rfdb.listAllRScriptNames(folder_id); + addLineInSource(dest_folder, null, function_order,new Integer(0)); //synchronize the order on source file. + return true; + }catch(Exception e){ + ClientError.reportError(e, null); + throw e; + }finally{ + rfdb.closeDB(); + } + + } + + /** + * Saves the order of the folders, this will be called after drag event completes on the UI. + * @param folder_ids + * @return + * @throws Exception + */ + public boolean updateFolderOrder(int[] folder_ids) throws Exception { + RFunctionDB rfdb=RFunctionDB.getRFunctionDB(); + + try{ + rfdb.connectDB(); + rfdb.updateFolderOrder(folder_ids); + syncFoldersWith4ESource(rfdb); + return true; + }catch(Exception e){ + ClientError.reportError(e, null); + throw e; + }finally{ + rfdb.closeDB(); + } + + } + + + /** + * 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){ + ClientError.reportError(e, null); + throw e; + }finally{ + + } + } + + + /** + * Saves the group order, this method will be called only on + * @param groupids + * @throws Exception + */ + public void setGroupOrder(Vector groupids) throws Exception { + RFunctionDB rfdb=RFunctionDB.getRFunctionDB();; + try{ + rfdb.connectDB(); + rfdb.setGroupOrder(groupids); + syncFoldersWith4ESource(rfdb); + }catch(Exception e){ + ClientError.reportError(e, null); + throw e; + }finally{ + rfdb.closeDB(); + } + } + + + /** + * get R function for generating editor on UI + * @param function_id + * @param readonly + * @return + * @throws Exception + */ + public Map getRFunction(int function_id,boolean readonly) throws Exception { + + RFunctionDB rfdb=RFunctionDB.getRFunctionDB(); + SchedulerDB sdb=SchedulerDB.getSchedulerDB(); + try{ + + + rfdb.connectDB(); + sdb.connectDB(); + Map data=rfdb.getRFunction(function_id); + //rtn.put("lockedby", getLockedBy(function_id)); + + String function_name=(String)data.get("function_name"); + String wiki=getFunctionWiki(function_name); + if(wiki.contains("\"noarticletext\"")){ + rfdb.updateWikiDone(function_id, 0); + }else{ + rfdb.updateWikiDone(function_id, 1); + } + + + + Map rtn=getDataBundle4Function(data,rfdb,sdb); + + String usr_alreadylocked=(String)((Map)rtn.get("data")).get("lockedby"); + String usr=new SchedulerMgmt(getRequest()).getAuthorizedUser(sdb); + + + + String access=getAccessPrivilege(function_id, rfdb); + if(access==null || (access!=null && access.equals(""))){ + rtn.put("access", ACCESS_PRIVILEGE_R); + access=ACCESS_PRIVILEGE_R; + }else{ + rtn.put("access", access); + } + rtn.put("isAuthorized", isAuthorizedUser(sdb)) ; + rtn.put("authorizedUser", getAuthorizedUser(sdb)); + rtn.put("tag_follow", getItemTags2(function_id,rfdb)); + + + + //System.out.println("RFunctionMgmt.getRFunction():usr:"+usr+" usr_alreadylocked:"+usr_alreadylocked); + if(!readonly){ + if(usr_alreadylocked!=null && !usr_alreadylocked.equals("") && !usr_alreadylocked.equalsIgnoreCase(usr)){ + //dont relock + }else{ + if(access.equalsIgnoreCase(ACCESS_PRIVILEGE_RWX)){ + refreshCache(function_id, 300,usr); + } + } + }else{ + rtn.put("readonly", true); + } + + return rtn; + + + }catch(Exception e){ + ClientError.reportError(e, null); + throw e; + }finally{ + rfdb.closeDB(); + sdb.closeDB(); + } + + } + + + /** + * This function updates wiki page done icon in the tree list. + * @param start + * @param end + * @return + * @throws Exception + */ + public boolean updateAllWikiIcon(int start,int end) throws Exception { + + RFunctionDB rfdb=RFunctionDB.getRFunctionDB(); + try{ + rfdb.connectDB(); + Map fnames=rfdb.getAllFunctionNamesID(" (is_wiki_done<>1 or is_wiki_done is null) AND id BETWEEN "+start+" AND "+end ); + for(Integer function_id: fnames.keySet()){ + + String fname=fnames.get(function_id); + String wiki=getFunctionWiki(fname); + log.info("updating rfunction:"+fname); + if(wiki.contains("\"noarticletext\"")){ + rfdb.updateWikiDone(function_id, 0); + }else{ + rfdb.updateWikiDone(function_id, 1); + } + } + return true; + }catch(Exception e){ + ClientError.reportError(e, null); + throw e; + }finally{ + rfdb.closeDB(); + + } + } + + /** + * + * @param function_names + * @param readonly + * @return + * @throws Exception + */ + public Vector getRFunctions(String function_names,boolean readonly) throws Exception { + + RFunctionDB rfdb=RFunctionDB.getRFunctionDB(); + SchedulerDB sdb=SchedulerDB.getSchedulerDB(); + try{ + + StringTokenizer st=new StringTokenizer(function_names,","); + Vector fnames=new Vector(); + while(st.hasMoreTokens()){ + fnames.add(st.nextToken()); + } + Vector rtn=new Vector(); + rfdb.connectDB(); + sdb.connectDB(); + //Map data=rfdb.getRFunction(function_id); + //rtn.put("lockedby", getLockedBy(function_id)) + Vector data1=rfdb.getRFunctions(fnames); + + String usr=new SchedulerMgmt(getRequest()).getAuthorizedUser(sdb); + + for(Iterator i=data1.iterator();i.hasNext();){ + Map data=(Map)i.next(); + Map func=getDataBundle4Function(data,rfdb,sdb); + int function_id=(Integer)data.get("id"); + String usr_alreadylocked=(String)((Map)func.get("data")).get("lockedby"); + + String access=getAccessPrivilege(function_id, rfdb); + if(access==null || (access!=null && access.equals(""))){ + func.put("access", ACCESS_PRIVILEGE_R); + access=ACCESS_PRIVILEGE_R; + }else{ + func.put("access", access); + } + + if(!readonly){ + if(usr_alreadylocked!=null && !usr_alreadylocked.equals("") && !usr_alreadylocked.equalsIgnoreCase(usr)){ + //dont relock + }else{ + if(access.equalsIgnoreCase(ACCESS_PRIVILEGE_RWX)){ + refreshCache(function_id, 300,usr); + } + } + }else{ + func.put("readonly", true); + } + + + func.put("isAuthorized", isAuthorizedUser(sdb)) ; + func.put("authorizedUser", getAuthorizedUser(sdb)); + func.put("tag_follow", getItemTags2(function_id,rfdb)); + + rtn.add(func); + } + return rtn; + + + }catch(Exception e){ + ClientError.reportError(e, null); + throw e; + }finally{ + rfdb.closeDB(); + sdb.closeDB(); + } + + } + + + /** + * get data bundle for R function, this includes r code and lock information + * @param data + * @param rfdb + * @param sdb + * @return + * @throws Exception + */ + private Map getDataBundle4Function(Map data, RFunctionDB rfdb,SchedulerDB sdb) throws Exception{ + HashMap rtn=new HashMap(); + String folder=Config.getString("r_function_source_folder"); + String pathurl=Config.getString("r_function_path_url_prefix"); + + String subfolder=rfdb.getFolderName((Integer)data.get("folder_id")); + int function_id=(Integer)data.get("id"); + + String filepath=(String)data.get("script_file"); + String scriptname=filepath; + filepath=folder+((subfolder!=null)?subfolder+File.separator:"")+filepath; + String content=getContent(filepath); + String tl=(String)data.get("lockedby"); + if(tl==null || (tl!=null && tl.equals("")) ){ + try{ + data.put("lockedby",getLockedBy(function_id, sdb, rfdb)); + }catch(Exception e){ + log.error("getDataBundle4Function() error:"+e.getMessage()); + } + } + rtn.put("content", content); + rtn.put("data", data); + rtn.put("isAuthorized", new SchedulerMgmt(getRequest()).isAuthorizedUser(sdb)); + rtn.put("authorizedUser", new SchedulerMgmt(getRequest()).getAuthorizedUser(sdb)); + rtn.put("path", pathurl+"\\"+subfolder+"\\"+scriptname); + return rtn; + } + + + + /** + * called on moving folder up or down + * @param folder_id + * @param new_group_id + * @return + * @throws Exception + */ + public boolean moveFolder(int folder_id, String new_group_id) throws Exception { + + RFunctionDB rfdb=RFunctionDB.getRFunctionDB(); + + try{ + + ///Thread.sleep(3000); + + rfdb.connectDB(); + rfdb.moveFolder(folder_id, new_group_id); + return true; + + }catch(Exception e){ + ClientError.reportError(e, "folder_id:"+folder_id+" new_group_id:"+new_group_id); + throw e; + }finally{ + rfdb.closeDB(); + } + } + + + + + /** + * new function creation + * @param folder_id + * @param fname + * @param script + * @param function_type + * @return + * @throws Exception + */ + public Map createRFunction(String folder_id,String fname, String script,int function_type) throws Exception { + + String folder=Config.getString("r_function_source_folder"); + RFunctionDB rfdb=RFunctionDB.getRFunctionDB(); + SchedulerDB sdb=SchedulerDB.getSchedulerDB(); + + + try{ + + Pattern pattern = Pattern.compile( "^[a-z][a-zA-Z0-9.]{2,50}+$" ); + Matcher m = pattern.matcher( fname ); // Matchers are used both for matching and finding. + if(!m.matches()){ + throw new Exception("Illegal function name, It should start with lower case, It should be Alpha-numeric, Min 2 characters, Max 50 chars, No space and No special characters except dot (.)"); + } + + HashMap rtn=new HashMap(); + + rfdb.connectDB(); + sdb.connectDB(); + + Map existing=rfdb.getRFunction(fname); + if(existing!=null && existing.size()>0){ + throw new Exception("ERROR: Function name: "+fname+" alerady existing, please try different name"); + } + if(script==null || (script!=null && script.equals(""))){ + script="#"+fname+".r\n"; + script+="#created on "+new SimpleDateFormat("dd-MMM-yyyy HH:mm").format(new Date()); + } + + //String filepath=(String)data.get("script_file"); + String subfolder=rfdb.getFolderName(Integer.parseInt(folder_id)); + + String filename=fname+RFUNCTION_FILE_EXTENSION; + String filepath=folder+((subfolder!=null)?subfolder+File.separator:"")+filename; + List function_names=rfdb.listAllRScriptNames(Integer.parseInt(folder_id)); + if(!new File(filepath).isFile()){ + createContent(filepath,script,subfolder,filename,function_names, function_type); + } + + int id=rfdb.createFunction(Integer.parseInt(folder_id),fname, filename,function_type); + + synchrnizeSVN(fname,script,null,rfdb,sdb,id); + + + Map data=rfdb.getRFunction(id); + + + String filepath1=(String)data.get("script_file"); + filepath1=folder+((subfolder!=null)?subfolder+File.separator:"")+filepath1; + + + String content=getContent(filepath1); + Map func_data=new HashMap(); + func_data.put("content", content); + func_data.put("data", data); + + + Vector functions=new Vector(); + functions.add(data); + + + rtn.put("rfunctions", functions); + boolean readonly=true; + rtn.put("func_data", getRFunction(id,!readonly)); + + + + return rtn; + }catch(Exception e){ + ClientError.reportError(e, "folder_id:"+folder_id+" fname:"+fname+" script:"+script); + throw e; + }finally{ + rfdb.closeDB(); + sdb.closeDB(); + } + + } + + + /** + * Called while modifying R function, and saves the data with message also will be synchronized with SVN repository + * @param function_id + * @param script + * @param message + * @param theme_tag_ids + * @param follow_tag_ids + * @return + * @throws Exception + */ + public Map modifyRFunction(int function_id, String script, String message, List theme_tag_ids, List follow_tag_ids) throws Exception { + + String folder=Config.getString("r_function_source_folder"); + RFunctionDB rfdb=RFunctionDB.getRFunctionDB(); + SchedulerDB sdb=SchedulerDB.getSchedulerDB(); + try{ + + HashMap rtn=new HashMap(); + rfdb.connectDB(); + sdb.connectDB(); + + String usr=new SchedulerMgmt(getRequest()).getAuthorizedUser(sdb); + String lockedby=getLockedBy(function_id, sdb, rfdb); + if(lockedby!=null && !usr.equalsIgnoreCase(getLockedBy(function_id, sdb, rfdb))){ + throw new Exception("This function is currently locked by user "+lockedby); + } + + Map data=rfdb.getRFunction(function_id); + + String filename=(String)data.get("script_file"); + String function_name=(String)data.get("function_name"); + Number is_class=(Number)data.get("is_class"); + + String sub_folder=""; + + try{ + sub_folder=rfdb.getFolderName((Integer)data.get("folder_id")); + }catch(Exception e){} + + String filepath=folder+((sub_folder!=null)?sub_folder+File.separator:"")+filename; + List function_order=rfdb.listAllRScriptNames((Integer)data.get("folder_id")); + createContent(filepath,script,sub_folder,filename,function_order,is_class); + + SVNSync4RFunction sync=synchrnizeSVN(function_name,script,message,rfdb,sdb,function_id); + + String diff=sync.getLastChanged(function_name); + + Map data1=rfdb.getRFunction(function_id); //this gets with latest tag after SVN sycnrhonize + int oid=0; + if(theme_tag_ids!=null && theme_tag_ids.size()>0){ + try{ + oid=Integer.parseInt(theme_tag_ids.get(0)+"");; + }catch(Exception e){log.error("error while converting owner id, e:"+e.getMessage());} + } + rfdb.updatedOwnerIDNow(oid,function_id); + Vector v=new Vector(); + v.add(data1); + + + HashMap ufolders=new HashMap(); ufolders.put(sub_folder,(Integer)data.get("folder_id")); + + List unbuildfunc=getUnPackagedSources(ufolders); + + + HashMap hdata=new HashMap(); + + hdata.put("function_name", function_name); + hdata.put("current_user", getAuthorizedUser(sdb)); + hdata.put("diff", diff); + hdata.put("comments", message); + + String templ_filename="function_modified_alert.txt"; + long rev=sync.lastRevision(function_name); + updateAllItemTags(function_id, theme_tag_ids,follow_tag_ids,rfdb,sdb,function_name, message,rev, diff, hdata, templ_filename) ; + + //return listScheduledItems(); + rtn.put("rfunctions", v); + rtn.put("function_id", function_id); + rtn.put("func_2build", unbuildfunc); + rtn.put("revisions", getSVNLogs(function_name)); + rtn.put("function_name", function_name); + + rtn.putAll(getThemeAccessData(rfdb)); + + + //notifyLastModification(rfdb,sdb,function_name,function_id,message); + + + return rtn; + + + + }catch(Exception e){ + e.printStackTrace(); + //ClientErrorMgmt.reportError(e,null); + + throw e; + }finally{ + sdb.closeDB(); + rfdb.closeDB(); + } + + } + + + + private void createRFunction11(String folder_id,String fname, String script, String file_exten) throws Exception { + + String folder=Config.getString("r_function_source_folder"); + RFunctionDB rfdb=RFunctionDB.getRFunctionDB(); + + try{ + HashMap rtn=new HashMap(); + + rfdb.connectDB(); + + Map existing=rfdb.getRFunction(fname); + if(existing!=null && existing.size()>0){ + throw new Exception("ERROR: Function name: "+fname+" alerady existing, please try different name"); + } + if(script==null || (script!=null && script.equals(""))){ + script="#"+fname+".r\n"; + script+="#created on "+new SimpleDateFormat("dd-MMM-yyyy HH:mm").format(new Date()); + } + + //String filepath=(String)data.get("script_file"); + String subfolder=rfdb.getFolderName(Integer.parseInt(folder_id)); + + String filename=fname+(file_exten==null?RFUNCTION_FILE_EXTENSION:file_exten); + String filepath=folder+((subfolder!=null)?subfolder+File.separator:"")+filename; + + if(!new File(filepath).isFile()){ + createContent(filepath,script,subfolder,filename,new Vector(),new Integer(0)); + } + + //boolean isclass=false; + + int id=rfdb.createFunction(Integer.parseInt(folder_id),fname, filename,RFunctionDB.FUNCTION_TYPE_NORMAL); + + synchrnizeSVN11(fname,script,null,rfdb,id); + + + + + + }catch(Exception e){ + //ClientErrorMgmt.reportError(e, "folder_id:"+folder_id+" fname:"+fname+" script:"+script); + throw e; + }finally{ + rfdb.closeDB(); + } + + } + + + private boolean renameSVN(String old_function_name,String new_function_name, String function, String message, RFunctionDB rfdb,int function_id) throws Exception { + + SchedulerDB sdb=SchedulerDB.getSchedulerDB(); + + try{ + sdb.connectDB(); + + String svnuser=Config.getString("svn_user_r"); + String svnpwd=Config.getString("svn_pwd_r"); + + SchedulerMgmt smgmt=new SchedulerMgmt(getRequest()); + String clientip=smgmt.getPeerIPAddress(); + String user=getAuthenticatedUser(); + + if(message==null){ + message="created on IP:"+clientip+" By:"+user; + } + + + + Map u=sdb.getSVNUser4WikiUser(user); + + if(u!=null && u.get("svn_username")!=null && u.get("svn_password")!=null){ + svnuser=(String)u.get("svn_username"); + svnpwd=(String)u.get("svn_password"); + } + + + + SVNSync4RFunction sync=new SVNSync4RFunction(svnuser,svnpwd); + sync.renameFile(old_function_name, new_function_name, function, message); + + + + String user1="usr-"+svnuser.trim().toLowerCase(); + int tag_id=rfdb.addIfTagNotExist(user1); + rfdb.updateLast2UsersTag(function_id,tag_id); + + + return true; + }catch(Exception e){ + log.error("Error while committing function into SVN "); + throw e; + }finally{ + try{sdb.closeDB();}catch(Exception e) { log.error("error while cloing db:"+e.getMessage()); } + } + + + } + + + private SVNSync4RFunction synchrnizeSVN(String function_name, String function, String message, RFunctionDB rfdb,SchedulerDB sdb,int function_id) throws Exception { + + + + try{ + + + String svnuser=Config.getString("svn_user_r"); + String svnpwd=Config.getString("svn_pwd_r"); + + SchedulerMgmt smgmt=new SchedulerMgmt(getRequest()); + String clientip=smgmt.getPeerIPAddress(); + String user=getAuthenticatedUser(); + + if(message==null){ + message="created on IP:"+clientip+" By:"+user; + } + + + + Map u=sdb.getSVNUser4WikiUser(user); + + if(u!=null && u.get("svn_username")!=null && u.get("svn_password")!=null){ + svnuser=(String)u.get("svn_username"); + svnpwd=(String)u.get("svn_password"); + } + + + + SVNSync4RFunction sync=new SVNSync4RFunction(svnuser,svnpwd); + sync.syncFile(function_name, function,message); + + + if(false){ + String user1="usr-"+svnuser.trim().toLowerCase(); + int tag_id=rfdb.addIfTagNotExist(user1); + rfdb.updateLast2UsersTag(function_id,tag_id); + } + + return sync; + }catch(Exception e){ + log.error("Error while committing function into SVN "); + throw e; + } + + + } + + + /** + * + * @param function_name + * @param function + * @param message + * @param rfdb + * @param function_id + * @return + * @throws Exception + */ + private boolean synchrnizeSVN11(String function_name, String function, String message, RFunctionDB rfdb,int function_id) throws Exception { + + SchedulerDB sdb=SchedulerDB.getSchedulerDB(); + + try{ + sdb.connectDB(); + + String svnuser=Config.getString("svn_user_r"); + String svnpwd=Config.getString("svn_pwd_r"); + + SchedulerMgmt smgmt=new SchedulerMgmt(getRequest()); + String clientip=smgmt.getPeerIPAddress(); + String user=getAuthenticatedUser(); + + if(message==null){ + message="created on IP:"+clientip+" By:"+user; + } + + + + Map u=sdb.getSVNUser4WikiUser(user); + + if(u!=null && u.get("svn_username")!=null && u.get("svn_password")!=null){ + svnuser=(String)u.get("svn_username"); + svnpwd=(String)u.get("svn_password"); + } + + + + SVNSync4RFunction sync=new SVNSync4RFunction(svnuser,svnpwd); + sync.syncFile(function_name, function,message); + + + return true; + }catch(Exception e){ + log.error("Error while committing function into SVN "); + throw e; + }finally{ + try{sdb.closeDB();}catch(Exception e) { log.error("error while cloing db:"+e.getMessage()); } + } + + + } + + + + + /** + * moving file to different folder, invoked on drag and drop of r function to folder. + * @param function_id + * @param new_folder_id + * @return + * @throws Exception + */ + public boolean moveFile2Folder(int function_id, int new_folder_id) throws Exception { + RFunctionDB rfdb=RFunctionDB.getRFunctionDB(); + + try{ + String folder=Config.getString("r_function_source_folder"); + + rfdb.connectDB(); + + Map func=rfdb.getRFunction(function_id); + Number is_class=(Number)func.get("is_class"); + + String src_folder=null; + try{ + src_folder=rfdb.getFolderName((Integer)func.get("folder_id")); + }catch(Exception e){} + String dest_folder=rfdb.getFolderName(new_folder_id); + String script_file=(String)func.get("script_file"); + if(src_folder.equals(dest_folder)) { + throw new Exception("Moving failed! source and destination are the same.."); + } + + String oldfile=folder+((src_folder!=null)?src_folder+File.separator:"")+script_file; + String newfile=folder+((dest_folder!=null)?dest_folder+File.separator:"")+script_file; + + + log.debug("old file:"+oldfile+" new :"+newfile); + + if(new File(oldfile).renameTo(new File(newfile))){ + log.debug("new folder_id:"+function_id+" function_id:"+new_folder_id); + rfdb.updateFunctionFolder(function_id,new_folder_id); + }else{ + throw new Exception("Moving failed failed...."); + } + + List functions_newfolder=rfdb.listAllRScriptNames(new_folder_id); + updateSource4Moved(src_folder,dest_folder,script_file,functions_newfolder,is_class); + + return true; + }catch(Exception e){ + ClientError.reportError(e, "function_id:"+function_id+" new_folder_id:"+new_folder_id); + throw e; + }finally{ + rfdb.closeDB(); + } + + + } + + + + + /** + * put back the deleted function into normal + * @param function_id + * @param new_folder_id + * @return + * @throws Exception + */ + public boolean putbackFunction(int function_id, int new_folder_id) throws Exception { + RFunctionDB rfdb=RFunctionDB.getRFunctionDB(); + + try{ + String folder=Config.getString("r_function_source_folder"); + + rfdb.connectDB(); + + Map func=rfdb.getRFunction(function_id); + String src_folder=null; + try{ + src_folder=rfdb.getFolderName((Integer)func.get("folder_id")); + }catch(Exception e){} + + String dest_folder=rfdb.getFolderName(new_folder_id); + String script_file=(String)func.get("script_file"); + + String oldfile=folder+((src_folder!=null)?src_folder+File.separator:"")+script_file; + String newfile=folder+((dest_folder!=null)?dest_folder+File.separator:"")+script_file; + + if(new File(oldfile).renameTo(new File(newfile))){ + log.debug("new folder_id:"+function_id+" function_id:"+new_folder_id); + rfdb.updateFunctionFolder(function_id,new_folder_id); + List fnames=rfdb.listAllRScriptNames(new_folder_id); + Number is_class=(Number)func.get("is_class"); + try{ + addLineInSource(dest_folder,script_file,fnames,is_class); + }catch(Exception e){ + log.error("ERROR:"+e.getMessage()); + ClientError.reportError(e, null); + } + }else{ + throw new Exception("Putting back the file from trash failed...."); + } + + return true; + }catch(Exception e){ + ClientError.reportError(e, "function_id:"+function_id+" new_folder_id:"+new_folder_id); + throw e; + }finally{ + rfdb.closeDB(); + } + + + } + + + /** + * renaming the folder, this will invoked on double clikcing the function name and input box appears to over-write the name. + * @param folder_id + * @param foldername + * @return + * @throws Exception + */ + public boolean renameFolder(int folder_id, String foldername) throws Exception { + RFunctionDB rfdb=RFunctionDB.getRFunctionDB(); + + try{ + String folder=Config.getString("r_function_source_folder"); + + rfdb.connectDB(); + + String old_folder=rfdb.getFolderName(folder_id); + + String oldfile=folder+old_folder; + String newfile=folder+foldername; + + + + log.debug("renaming folder from "+oldfile+" to "+newfile); + + if(new File(oldfile).renameTo(new File(newfile))){ + rfdb.renameFolder(folder_id, foldername); + + //creating source file that contains all functions of that folder so that, this source file will be included in source loading on R + String rfolder=Config.getString("r_auto_source_inc_folder"); + if(rfolder!=null ){ + File oldsource=new File(rfolder+old_folder+RFUNCTION_FILE_EXTENSION); + File newsource=new File(rfolder+foldername+RFUNCTION_FILE_EXTENSION); + if(oldsource.exists()){ + oldsource.renameTo(newsource); + } + if(!newsource.exists()) newsource.createNewFile(); + + } + + }else{ + throw new Exception("Renaming folder failed...."); + } + syncFoldersWith4ESource(rfdb); + + return true; + }catch(Exception e){ + ClientError.reportError(e, "folder_id:"+folder_id+" foldername:"+foldername); + throw e; + }finally{ + rfdb.closeDB(); + } + + + } + + + /** + * creating new folder, this is linked with new folder button on group bar + * @param folder_name + * @param group_id + * @return + * @throws Exception + */ + public Map createFolder(String folder_name, String group_id) throws Exception { + RFunctionDB rfdb=RFunctionDB.getRFunctionDB(); + try{ + String folder=Config.getString("r_function_source_folder"); + + String newfile=folder+folder_name; + + if(new File(newfile).isDirectory() || new File(newfile).isFile()){ + throw new Exception("Folder already existing folder failed....Path:"+newfile); + }else{ + rfdb.connectDB(); + + int folder_id=rfdb.getFolderID(folder_name); + if(folder_id<=0){ + folder_id=rfdb.createFolder(folder_name, group_id); + } + File nfile=new File(newfile); + if(!nfile.isDirectory() && !nfile.isFile()) { + nfile.mkdirs(); + } + + + //creating source file that contains all functions of that folder so that, this source file will be included in source loading on R + String rfolder=Config.getString("r_auto_source_inc_folder"); + if(rfolder!=null ){ + File sfile=new File(rfolder+folder_name+RFUNCTION_FILE_EXTENSION); + if(!sfile.exists()) sfile.createNewFile(); + } + + syncFoldersWith4ESource(rfdb); + + HashMap rtn=new HashMap(); + List folders=rfdb.listOfFolders(group_id); + rtn.put("folders", folders); + rtn.put("folder_id", folder_id); + return rtn; + } + + }catch(Exception e){ + ClientError.reportError(e, "folder_id:"+folder_name+" group_id:"+group_id); + throw e; + }finally{ + rfdb.closeDB(); + } + + + } + + /** + * deletes empty folder, associated with right click and delete action + * @param folder_name + * @param folder_id + * @return + * @throws Exception + */ + public boolean deleteFolder(String folder_name, int folder_id) throws Exception { + RFunctionDB rfdb=RFunctionDB.getRFunctionDB(); + try{ + + String folder=Config.getString("r_function_source_folder"); + String delfolder=folder+folder_name; + if(new File(delfolder).exists() && new File(delfolder).delete()){ + + String rfolder=Config.getString("r_auto_source_inc_folder"); + log.debug("folder_name:"+folder_name+" folder_id:"+folder_id); + rfdb.connectDB(); + rfdb.removeFolder(folder_id); + + if(rfolder!=null ){ + File sfile=new File(rfolder+folder_name+RFUNCTION_FILE_EXTENSION); + sfile.delete(); + log.debug("Folder:" +sfile.getPath()); + syncFoldersWith4ESource(rfdb); + + } + }else{ + throw new Exception("No physical folder "+delfolder+" found! or not able to delete"); + } + return true; + }catch(Exception e){ + ClientError.reportError(e, "folder_id:"+folder_name+" folder_id:"+folder_id); + throw e; + }finally{ + rfdb.closeDB(); + } + } + + private Map createFolderIfNotExist(String folder_name, String group_id) throws Exception { + RFunctionDB rfdb=RFunctionDB.getRFunctionDB(); + + try{ + String folder=Config.getString("r_function_source_folder"); + + //String oldfile=folder+old_folder; + String newfile=folder+folder_name; + + + rfdb.connectDB(); + + int folder_id=rfdb.getFolderID(folder_name); + if(folder_id<=0){ + folder_id=rfdb.createFolder(folder_name, group_id); + } + File nfile=new File(newfile); + if(!nfile.isDirectory() && !nfile.isFile()) { + nfile.mkdirs(); + } + + + HashMap rtn=new HashMap(); + List folders=rfdb.listOfFolders(group_id); + rtn.put("folders", folders); + rtn.put("folder_id", folder_id); + return rtn; + + + }catch(Exception e){ + ClientError.reportError(e, "folder_id:"+folder_name+" group_id:"+group_id); + throw e; + }finally{ + rfdb.closeDB(); + } + + + } + + private String getContent(String fullpath) throws Exception { + BufferedReader in = new BufferedReader(new FileReader(fullpath)); + String rtn=""; + String str; + while ((str = in.readLine()) != null) { + rtn+=str+"\r\n"; + } + in.close(); + return rtn; + } + + + /** + * Rename function by double click on the function, textbox appears and over-write name and press save button. + * @param new_func_name + * @param function_id + * @return + * @throws Exception + */ + public boolean renameFunction(String new_func_name, int function_id) throws Exception { + String folder=Config.getString("r_function_source_folder"); + RFunctionDB rfdb=RFunctionDB.getRFunctionDB(); + SchedulerDB sdb=SchedulerDB.getSchedulerDB(); + + try{ + + Pattern pattern = Pattern.compile( "^[a-z][a-zA-Z0-9.]{1,50}+$" ); + Matcher m = pattern.matcher( new_func_name ); // Matchers are used both for matching and finding. + if(!m.matches()){ + throw new Exception("Illegal function name, It should start with lower case, It should be Alpha-numeric, Min 2 characters, Max 50 chars, No space and No special characters except dot (.)"); + } + + + rfdb.connectDB(); + + Map existing=rfdb.getRFunction(new_func_name); + if(existing!=null && existing.size()>0){ + throw new Exception("ERROR: Function name: "+new_func_name+" alerady existing, please try different name"); + } + HashMap rtn=new HashMap(); + + sdb.connectDB(); + + String usr=new SchedulerMgmt(getRequest()).getAuthorizedUser(sdb); + String lockedby=getLockedBy(function_id, sdb, rfdb); + if(lockedby!=null && !usr.equalsIgnoreCase(getLockedBy(function_id, sdb, rfdb))){ + throw new Exception("This function is currently locked by user "+lockedby); + } + Map old_data=rfdb.getRFunction(function_id); + String new_filename=new_func_name+RFUNCTION_FILE_EXTENSION; + + String old_filename=(String)old_data.get("script_file"); + String old_function_name=(String)old_data.get("function_name"); + Number is_class=(Number)old_data.get("is_class"); + String sub_folder=""; + try{ + sub_folder=rfdb.getFolderName((Integer)old_data.get("folder_id")); + }catch(Exception e){} + + + String oldfile=folder+((sub_folder!=null)?sub_folder+File.separator:"")+old_filename; + String newfile=folder+((sub_folder!=null)?sub_folder+File.separator:"")+new_filename; + + Map script_data=getDataBundle4Function(old_data,rfdb,sdb); + String script=(String)script_data.get("content"); + String message=""; + + message="Renamed "+old_function_name+" --> " +new_func_name; + + log.debug("old file:"+oldfile+" new :"+newfile); + + if(new File(oldfile).renameTo(new File(newfile))){ + rfdb.renameFunction(function_id, new_func_name, new_filename); + }else{ + throw new Exception("Renaming failed"); + } + + + Map new_data=rfdb.getRFunction(function_id); + int folder_id=(Integer)new_data.get("folder_id"); + List function_inFolder=rfdb.listAllRScriptNames(folder_id); + + + renameSVN(old_function_name,new_func_name,script,message,rfdb,function_id); + + Map data1=rfdb.getRFunction(function_id); //this gets with latest tag after SVN sycnrhonize + + Vector v=new Vector(); + v.add(data1); + + rtn.put("rfunctions", v); + + removeLineInSource(sub_folder,old_filename); + addLineInSource(sub_folder,new_filename,function_inFolder,is_class); + + return true; + + + }catch(Exception e){ + ClientError.reportError(e,null); + throw e; + }finally{ + sdb.closeDB(); + rfdb.closeDB(); + } + } + + /** + * Purse deleted function so that it completely removed from the disk + * @param function_id + * @return + * @throws Exception + */ + public boolean purgeFunction(int function_id) throws Exception { + String folder=Config.getString("r_function_source_folder"); + RFunctionDB rfdb=RFunctionDB.getRFunctionDB(); + SchedulerDB sdb=SchedulerDB.getSchedulerDB(); + + try{ + + rfdb.connectDB(); + sdb.connectDB(); + + + Map old_data=rfdb.getRFunction(function_id); + + + String old_filename=(String)old_data.get("script_file"); + String old_function_name=(String)old_data.get("function_name"); + + String sub_folder=""; + try{ + sub_folder=rfdb.getFolderName((Integer)old_data.get("folder_id")); + }catch(Exception e){} + + //deleting physical file + String oldfile=folder+((sub_folder!=null)?sub_folder+File.separator:"")+old_filename; + + File file=new File(oldfile); + if(file.exists()){ + file.delete(); + } + + String svnuser=Config.getString("svn_user_r"); + String svnpwd=Config.getString("svn_pwd_r"); + + + Map new_data=rfdb.getRFunction(function_id); + int folder_id=(Integer)new_data.get("folder_id"); + List function_inFolder=rfdb.listAllRScriptNames(folder_id); + + + String user=getAuthenticatedUser(); + Map u=sdb.getSVNUser4WikiUser(user); + + if(u!=null && u.get("svn_username")!=null && u.get("svn_password")!=null){ + svnuser=(String)u.get("svn_username"); + svnpwd=(String)u.get("svn_password"); + } + + SVNSync4RFunction sync=new SVNSync4RFunction(svnuser,svnpwd); + sync.deleteFile(old_function_name); + + + rfdb.connectDB(); + rfdb.deleteFunction(function_id); + + //removing from source + removeLineInSource(sub_folder,old_filename); + + + return true; + + + }catch(Exception e){ + //e.printStackTrace(); + ClientError.reportError(e,null); + throw e; + }finally{ + sdb.closeDB(); + rfdb.closeDB(); + } + } + + private void updateSource4Moved(String old_subfolder, String new_subfolder, String fname, List functions_newfolder,Number is_class) throws Exception { + + String rfolder=Config.getString("r_auto_source_inc_folder"); + String filepref=Config.getString("r_auto_source_inc_prefix"); + if(rfolder!=null && filepref!=null){ + try{ + removeLineInSource(old_subfolder,fname); + addLineInSource(new_subfolder,fname,functions_newfolder,is_class); + }catch(Exception e){ + log.error("ERROR:"+e.getMessage()); + ClientError.reportError(e, null); + } + //adding into new source folder. + } + + } + + private void removeLineInSource(String subfolder,String fname) throws Exception{ + + String rfolder=Config.getString("r_auto_source_inc_folder"); + String filepref=Config.getString("r_auto_source_inc_prefix"); + + File old_s=new File(rfolder+subfolder+RFUNCTION_FILE_EXTENSION); + if(old_s.exists() && old_s.isFile()){ + ArrayList lst=new ArrayList(); + List lines=FileUtils.readLines(old_s); + String tline=filepref+subfolder+"/"+fname; + for(Iterator i=lines.iterator();i.hasNext();){ + String line=(String)i.next(); + String data="source(universalPath(\""+tline+"\")"; + if(line.toUpperCase().indexOf(data.toUpperCase())>=0){ } + else if(!line.trim().equals("")){ + lst.add(line); + } + } + FileUtils.writeLines(old_s, lst); + } + } + + + public List getUnPackagedSources(HashMap subfolders) throws Exception{ + String rfolder=Config.getString("r_auto_source_inc_folder"); + String filepref=Config.getString("r_auto_source_inc_prefix"); + + ArrayList list=new ArrayList(); + for(String subfolder: subfolders.keySet()){ + File old_s=new File(rfolder+subfolder+RFUNCTION_FILE_EXTENSION); + int count=0; + if(old_s.exists() && old_s.isFile()){ + ArrayList lst=new ArrayList(); + List lines=FileUtils.readLines(old_s); + //String tline=filepref+subfolder+"/"+fname; + for(Iterator i=lines.iterator();i.hasNext();){ + String line=(String)i.next(); + String pref= filepref+subfolder+"/"; + if(line.contains(pref ) && line.indexOf(RFUNCTION_FILE_EXTENSION)>0 ){ + //System.out.println("RFunctionMgmt.getUnpackagedSources() Indexof:prf:"+line+" idx:"+ line.indexOf(pref)); + String rFile=line.substring(line.indexOf(pref)+pref.length(),line.indexOf(RFUNCTION_FILE_EXTENSION)); + list.add(rFile); + count++; + } + } + } + subfolders.put(subfolder,new Integer(count)); + } + return list; + } + + + + + + + + + + + + + private void addLineInSource( String subfolder, String fname, List functionnames_order,Number is_class) throws Exception { + + String rfolder=Config.getString("r_auto_source_inc_folder"); + String filepref=Config.getString("r_auto_source_inc_prefix"); + + int pass=0; + if(rfolder!=null && filepref!=null){ + File sfile=new File(rfolder+subfolder+RFUNCTION_FILE_EXTENSION); + //boolean lastempty=true; + boolean src_included=false; + try{ + + List lines=null; + + // log.debug("lines:"+lines); + if(sfile.exists() && sfile.isFile()) { + lines=FileUtils.readLines(sfile); + } + + pass=1; + if(lines!=null && fname!=null){ + + String tline=filepref+subfolder+"/"+fname; + String data="source(universalPath(\""+tline+"\")"; + //if(lines.contains(data)){ + if(checkExist(lines,data)!=null){ + src_included=true; + } + pass=2; + } + pass=3; + if(subfolder.equalsIgnoreCase("Trash")) src_included=true; //omits + if(!src_included){ + String tline1=filepref+subfolder+"/"+fname; + //String tline2= "source(\""+tline1+"\")"; + + String tline2=(is_class!=null && is_class.intValue()>=1?"#":"")+"source(universalPath(\""+tline1+"\")"; + + lines.add(tline2); + + } + pass=6; + //ignores the trash folder... + if(!subfolder.equalsIgnoreCase("Trash")){ + BufferedWriter out = new BufferedWriter(new FileWriter(sfile,false)); + out.write("#updated this file on "+new Date().toString()+"\n"); + for(Iterator i=functionnames_order.iterator();i.hasNext();){ + String cf=(String)i.next(); + String tline1=filepref+subfolder+"/"+cf; + + String tline2= "tryCatch({source(universalPath(\""+tline1+"\"))}, error = function(e) Say(\"Check source file "+cf+"\"))"; + String tline3= "source(universalPath(\""+tline1+"\")"; + //if(lines.contains(tline3)){ + String exist=checkExist(lines,tline3); + if(exist!=null){ + if(exist.trim().startsWith("#")) out.append("#"+tline2+"\n"); + else out.append(tline2+"\n"); + } + pass=7; + } + out.flush(); + out.close(); + sfile.setWritable(true); + pass=8; + } + if(getLockCache().get(cache_packaged_key)!=null){ + getLockCache().remove(cache_packaged_key); + } + + }catch(Exception e){ + log.error("ERROR:"+e.getMessage()); + ClientError.reportError(e, "subfolder:"+subfolder+" fname:"+fname+" pass:"+pass+" is_class:"+is_class); + } + } + } + + private String checkExist(List lines,String source) throws Exception { + String found=null; + for(Iterator i=lines.iterator();i.hasNext();){ + String line=i.next(); + if(line.contains(source)) found=line; + } + return found; + } + + private void syncFoldersWith4ESource(RFunctionDB rfdb) throws Exception { + String rfolder=Config.getString("r_auto_source_inc_folder"); + String filepref=Config.getString("r_auto_source_inc_prefix"); + + + String fe_source_filename=Config.getString("r_source_4e_function_file"); + + String inc_prefix=Config.getString("r_source_4e_function_file_path_perfix"); + + File sfile=null; + //log.debug("4e source file:"+fe_source_filename+" file exist:"+new File(fe_source_filename).isFile()); + + if(fe_source_filename!=null && new File(fe_source_filename).isFile()){ + //List lines=FileUtils.readLines(new File(fe_source_filename)); + + + ArrayList lines=new ArrayList(); + List folders=rfdb.listOfFolders(); + for(Iterator i=folders.iterator();i.hasNext();){ + Map folder=(Map)i.next(); + String subfolder=(String)folder.get("folder_name"); + File sfile1=new File(rfolder+subfolder+RFUNCTION_FILE_EXTENSION); + if(sfile1.isFile()){ + String fileline=inc_prefix+subfolder+RFUNCTION_FILE_EXTENSION; + lines.add(fileline); + log.debug("file_line:"+fileline); + }else{ + log.debug("file note found:"+sfile1.getPath()); + } + } + sfile=new File(fe_source_filename); + if(lines.size()>0){ + BufferedWriter out = new BufferedWriter(new FileWriter(sfile,false)); + out.write("#updated this file on "+new Date().toString()+"\n"); + for(Iterator i=lines.iterator();i.hasNext();){ + String cf=(String)i.next(); + String r_file=""; + try{ + StringTokenizer st=new StringTokenizer(cf,"\\"); + if(st.countTokens()>1){ + while(st.hasMoreTokens()) r_file=st.nextToken(); + } + if(!r_file.toLowerCase().endsWith(".r")){ + r_file=cf; + } + }catch(Exception e){ + + } + out.append("tryCatch({source(universalPath(\""+cf+"\"))}, error = function(e) Say(\"Check source file for "+r_file+"\"))"+"\n"); + } + out.flush(); + out.close(); + sfile.setWritable(true); + + + } + }else{ + log.error("currently main source file configured to:"+fe_source_filename); + log.error("main source file that contains folder named source R not found or invlid, check :r_source_4e_function_file key on config file"); + + } + + } + + + + + private Map getSourcedFunctions() throws Exception { + String rfolder=Config.getString("r_auto_source_inc_folder"); + String filepref=Config.getString("r_auto_source_inc_prefix"); + RFunctionDB rfdb=RFunctionDB.getRFunctionDB(); + String cache_packaged_key="packaged_functions"; + if(getLockCache().get(cache_packaged_key)!=null){ + Map rtn=(Map)getLockCache().get(cache_packaged_key); + return rtn; + }else{ + + HashMap rtn=new HashMap(); + try{ + rfdb.connectDB(); + List folders=rfdb.listOfFolders(); + for(Iterator i=folders.iterator();i.hasNext();){ + Map folder=(Map)i.next(); + + Integer f_id=(Integer)folder.get("id"); + String subfolder=(String)folder.get("folder_name"); + File sfile=new File(rfolder+subfolder+RFUNCTION_FILE_EXTENSION); + if(sfile.exists() && sfile.isFile()){ + List lines=FileUtils.readLines(sfile); + List functions=rfdb.listOfFunctions(f_id); + for(Iterator it=functions.iterator();it.hasNext();){ + Map data=it.next(); + + String tline1=filepref+subfolder+"/"+data.get("script_file"); + String tline2= "source(universalPath(\""+tline1+"\")"; + if(lines.contains(tline2)){ + rtn.put(data.get("id"),true); + } + + } + + } + } + IElementAttributes att= getLockCache().getDefaultElementAttributes(); + att.setMaxLifeSeconds(6000); //every 60 minutes + getLockCache().put(cache_packaged_key,rtn,att); + return rtn; + + }catch(Exception e){ + ClientError.reportError(e, null); + throw e; + }finally{ + rfdb.closeDB(); + } + }//end if + + } + + private void addLineInSource_old( String subfolder, String fname) throws Exception { + String rfolder=Config.getString("r_auto_source_inc_folder"); + String filepref=Config.getString("r_auto_source_inc_prefix"); + + if(rfolder!=null && filepref!=null){ + File sfile=new File(rfolder+subfolder+RFUNCTION_FILE_EXTENSION); + boolean lastempty=true; + boolean src_included=false; + try{ + String tline=filepref+subfolder+"/"+fname; + if(sfile.exists() && sfile.isFile()){ + List lines=FileUtils.readLines(sfile); + for(Iterator i=lines.iterator();i.hasNext();){ + String line=(String)i.next(); + String data="source(universalPath(\""+tline+"\")"; + if(line.toUpperCase().indexOf(data.toUpperCase())>=0){ + src_included=true; + } + lastempty=line.equals("")?true:false; + } + } + if(subfolder.equalsIgnoreCase("Trash")) src_included=true; //omits + if(!src_included){ + String data=(lastempty?"":System.getProperty("line.separator"))+"source(\""+tline+"\")"; + //FileUtils.writeStringToFile(sfile,data ); + BufferedWriter out = new BufferedWriter(new FileWriter(sfile,true)); + out.append(data); + out.flush(); + out.close(); + sfile.setWritable(true); + + } + + }catch(Exception e){ + log.error("ERROR:"+e.getMessage()); + ClientError.reportError(e, "subfolder:"+subfolder+" fname:"+fname); + } + + } + + } + + private void createContent(String fullpath, String script, String subfolder, String fname, List function_names,Number is_class) throws Exception { + + Writer output = null; + File file = new File(fullpath); + output = new BufferedWriter(new FileWriter(file)); + output.write(script); + output.close(); + addLineInSource( subfolder, fname,function_names,is_class); + } + + + /** + * returns svn logs for the given R function name + * @param function_name + * @return + * @throws Exception + */ + + public Vector getSVNLogs(String function_name) throws Exception { + SVNSync4RFunction sync=new SVNSync4RFunction(); + TreeMap rtn=new TreeMap(); + Vector d=sync.log(function_name); + if(d!=null && d.size()>0){ + + for(Iterator i=d.iterator();i.hasNext();){ + SVNLogEntry entry=i.next(); + SimpleDateFormat format=new SimpleDateFormat("dd-MMM-yyyy hh:mm a"); + ValueObject vo=new ValueObject(); + HashMap data=new HashMap(); + data.put("author", entry.getAuthor()); + data.put("date", format.format(entry.getDate())); + data.put("message", entry.getMessage()); + data.put("revision", entry.getRevision()); + data.put("function_name", function_name); + + String path=null; + if(entry.getChangedPaths()!=null){ + for(Iterator it=entry.getChangedPaths().keySet().iterator();it.hasNext();){ + //if(entry.getChangedPaths().keySet().size()>0){ + String thispath=(String)it.next(); + thispath=thispath.substring(thispath.lastIndexOf("/")+1); + path=(path==null)?thispath:path+","+thispath; + } + if(path!=null){ + + data.put("path", path); + } + } + rtn.put(entry.getRevision(), data); + //System.out.println(" Rev:"+entry.getRevision()+" Date:"+format.format(entry.getDate())+" User:"+entry.getAuthor()+" Msg:"+entry.getMessage()); + } + return new Vector(rtn.descendingMap().values()); + }else{ + return null; + } + + } + + + /** + * get script Revition. + * @param function_name + * @param revision + * @param flag + * @param path + * @return + * @throws Exception + */ + public String getScriptRev(String function_name, String revision,boolean flag,String path) throws Exception { + try{ + SVNSync4RFunction sync=new SVNSync4RFunction(); + String script=null; + if(flag) + script=sync.getScript(function_name, Long.parseLong(revision),path); + else + //script=sync.diffWC(function_name, Long.parseLong(revision)); + script=sync.getWhatChanged(function_name, Long.parseLong(revision)); + if(script==null){ + script="No changes were made or first revision of this function"; + } + + return script; + }catch(Exception e){ + e.printStackTrace(); + throw e; + } + + } + + /** + * get wiki for the given function name + * @param function_name + * @return + * @throws Exception + */ + public String getFunctionWiki(String function_name) throws Exception { + WikiRFunctionManual wiki=new WikiRFunctionManual(); + //String rtn=wiki.getWikiHTML(function_name); + + String username=Config.getString("wiki.username"); + String password=Config.getString("wiki.password"); + String wikiurl=Config.getString("wiki.wikiurl"); + + String rtn=wiki.getWikiHTML(username, password, wikiurl, function_name); + return rtn; + + } + + /** + * update tags of the functions, usually invoked while drag and drop functions from available box to selected theme box. + * @param function_id + * @param tag_id + * @param isadd + * @return + * @throws Exception + */ + public Map updateTags4Function(int function_id, int tag_id,boolean isadd) throws Exception { + RFunctionDB rfdb=RFunctionDB.getRFunctionDB(); + + try{ + + HashMap rtn=new HashMap(); + rfdb.connectDB(); + if(isadd){ + rfdb.addTagIds4Function(function_id, tag_id); + }else { + rfdb.removeTagIds4Function(function_id, tag_id); + } + + Map rtndata=rfdb.getRFunction(function_id); + Vector v=new Vector(); + v.add(rtndata); + + + rtn.put("rfunctions", v); + return rtn; + + + }catch(Exception e){ + ClientError.reportError(e, null); + throw e; + }finally{ + rfdb.closeDB(); + } + + } + + + + /** + * this function needed at the begining to migration to new features. + * @deprecated + * @param foldernames + * @return + * @throws Exception + * + * + */ + + public boolean migrateFiles (String foldernames) throws Exception { + try{ + + Vector impfolder=new Vector(); + if(foldernames!=null && !foldernames.equals("")){ + StringTokenizer st=new StringTokenizer(foldernames,","); + while(st.hasMoreTokens()){ + impfolder.add(st.nextToken()); + } + } + + String rfolder=Config.getString("r_function_source_folder"); + File[] folders=new File(rfolder).listFiles(); + int count=0; + for(int i=0;i=0? files[ia].getName().substring(0,files[ia].getName().lastIndexOf(".")) :files[ia].getName(); + String file_ext=files[ia].getName().lastIndexOf(".")>=0? files[ia].getName().substring(files[ia].getName().lastIndexOf(".")) :null; + if(folder_id>0){ + + try{ + createRFunction11(folder_id+"", shortfilename, content,file_ext); + //System.out.println("~~Importing:"+shortfilename); + }catch(Exception e){ + log.error(e); + } + } + content=null; + //System.out.println("::::file:::::"+shortfilename); + //count++; + } + } + } + } + return true; + }catch(Exception e){ + e.printStackTrace(); + throw e; + } + } + + + /** + * Search word within all function, and display finder UI with result and occurances + * @param word + * @return + * @throws Exception + */ + public Map getSearchR(String word) throws Exception { + try{ + + Map r_result = FindStringInFiles.search(word, Config.getString("svn_local_path_r"), RFunctionMgmt.RFUNCTION_FILE_EXTENSION); + + String svnLocalPath = Config.getString("svn_local_path"); + + Map scd_result = FindStringInFiles.search(word, svnLocalPath, ".R"); + //Map scd_result=new FindStringInFiles(Config.getString("svn_local_path")).search(word,".R"); + + HashMap h=new HashMap(); + h.put("r", r_result); + h.put("scd", scd_result); + return h; + + }catch(Exception e){ + e.printStackTrace(); + throw e; + } + } + + + /** + * this is equivalent to find4E R function + * @param filename + * @return + * @throws Exception + */ + public Map getSearchScript(String filename) throws Exception { + + + try{ + + HashMap rtn=new HashMap(); + + + String folder=null; + if(filename.contains("script_")){ + + folder = Config.getString("svn_local_path"); + + Pattern p=Pattern.compile("^(script_)([0-9]+)(.*)$", Pattern.DOTALL); + Matcher m=p.matcher(filename); + if(m.find() && m.groupCount()>2){ + SchedulerDB sdb=SchedulerDB.getSchedulerDB(); + sdb.connectDB(); + try{ + int scd_id=Integer.parseInt(m.group(2)); + Map d=sdb.getScheduler(scd_id); + rtn.put("item_id", scd_id); + rtn.put("item_type", "scheduler"); + if(d.get("deleted")!=null && ((Number)d.get("deleted")).intValue()==1){ + rtn.put("deleted", true); + } + + }catch(Exception e){ + log.error("Error while getSearchScript() scd, e:"+e.getMessage()); + }finally{ + sdb.closeDB(); + } + } + }else{ + folder=Config.getString("svn_local_path_r"); + + RFunctionDB rfdb=RFunctionDB.getRFunctionDB(); + + try{ + + + rfdb.connectDB(); + Map func=rfdb.getRFunctionForScriptFile(filename); + rtn.put("item_type", "function"); + if(func!=null && func.keySet().size()>0){ + + rtn.put("item_id", func.get("function_name")); + if(func.get("folder_name")!=null && func.get("folder_name").toString().equalsIgnoreCase("trash")){ + rtn.put("deleted", true); + } + } + + }catch(Exception e){ + log.error("Error while getSearchScript() scd, e:"+e.getMessage()); + }finally{ + rfdb.closeDB(); + } + + } + + + String ffile=folder.endsWith(File.separator)? folder : folder+File.separator; + ffile+=filename; + StringWriter stringWriter = new StringWriter(); + IOUtils.copy(new FileInputStream(new File(ffile)), stringWriter); + rtn.put("script",stringWriter.toString()); + + return rtn; + + }catch(Exception e){ + e.printStackTrace(); + throw e; + } + } + + + /** + * get package info + * this will be called on clicking of folder's package info button + * @param pack + * @return + * @throws Exception + */ + public Map getPackageInfo(String pack) throws Exception { + + + HashMap h=new HashMap(); + RFunctionDB rfdb=RFunctionDB.getRFunctionDB(); + SchedulerDB sdb=SchedulerDB.getSchedulerDB(); + try{ + + + String unixp=Config.getString("package.repository.unix"); + String winp=Config.getString("package.repository.win"); + + Map ud=new HashMap(),wd=new HashMap(); + try{ + ud=getPackageDescInfo(unixp,pack); + }catch(Exception e){log.error("Error collecint unix package info e:"+e.getMessage());} + try{ + wd=getPackageDescInfo(winp,pack); + }catch(Exception e){log.error("Error collecint windows package info e:"+e.getMessage());} + + String sourceloader=""; + try{ + String rfolder=Config.getString("r_auto_source_inc_folder"); + File sf=new File(rfolder+pack+RFUNCTION_FILE_EXTENSION); + sourceloader=(sf.exists())?FileUtils.readFileToString(sf):""; + }catch(Exception e) { + log.error("error while loading source file e:"+e.getMessage()); + } + + rfdb.connectDB(); + sdb.connectDB(); + + + + Map pinfo=rfdb.getPackageInfo(pack); + + int pid=rfdb.getFolderID( pack); + + + h.put("package_id",pid ); + h.put("folders", rfdb.listOfFolders()); + h.put("required_packages", rfdb.listRelatedFolderIds(pid)); + h.put("pinfo", pinfo); + h.put("unix",ud); + h.put("win", wd); + h.put("sourceloader", sourceloader); + h.put("access", getPackageAccessPrivilege(pid,rfdb)); + + + return h; + + }catch(Exception e){ + ClientError.reportError(e, null); + throw e; + }finally{ + rfdb.closeDB(); + sdb.closeDB(); + } + } + + + + /** + * reset package hirarchial boxes, this info is used to build packages + * @param packname + * @return + * @throws Exception + */ + public List resetPackageHirDep(String packname) throws Exception { + + RFunctionDB rfdb=RFunctionDB.getRFunctionDB(); + try{ + rfdb.connectDB(); + List h=rfdb.getDefaultHierarchyDependsIds(packname); + int pid=rfdb.getFolderID(packname); + rfdb.updateRelatedFolderIds(pid, h); + + return h; + + }catch(Exception e){ + ClientError.reportError(e, null); + throw e; + }finally{ + rfdb.closeDB(); + } + + } + + + /** + * syncPackageHirDep + * + * + * @deprecated + * @param folder_id + * @param ids + * @return + * @throws Exception + */ + public boolean syncPackageHirDep(int folder_id, List ids) throws Exception { + + + + RFunctionDB rfdb=RFunctionDB.getRFunctionDB(); + + try{ + rfdb.connectDB(); + + rfdb.updateRelatedFolderIds(folder_id, ids); + + return true; + + }catch(Exception e){ + ClientError.reportError(e, null); + throw e; + }finally{ + rfdb.closeDB(); + } + + } + + /** + * get package member info + * @param pack_id + * @return + * @throws Exception + */ + public Map getPackageMembersInfo(int pack_id) throws Exception { + RFunctionDB rfdb=RFunctionDB.getRFunctionDB(); + try { + HashMap rtn=new HashMap(); + rfdb.connectDB(); + Vector tags = rfdb.getTags(); + rtn.put("tags", tags); + rtn.put("theme_tags", rfdb.getTagIds4Folder(pack_id, "folder_tags")); + rtn.put("notification_tags", rfdb.getTagIds4Folder(pack_id, "folder_followtags")); + rtn.put("access", getPackageAccessPrivilege(pack_id,rfdb)); + + return rtn; + } catch (Exception e) { + + ClientError.reportError(e, null); + throw e; + } finally { + rfdb.closeDB(); + } + + + } + + + /** + * update package themes + * @param pack_id + * @param tagids + * @param tblname + * @return + * @throws Exception + */ + public boolean updatePackageThemes(int pack_id, List tagids, String tblname) throws Exception { + RFunctionDB rfdb=RFunctionDB.getRFunctionDB(); + try { + rfdb.connectDB(); + rfdb.updateTagIds4Folder(pack_id, tagids, SchedulerDB.REMOVE_BEFORE_UPDATE, tblname); + return true; + } catch (Exception e) { + + ClientError.reportError(e, null); + throw e; + } finally { + rfdb.closeDB(); + } + + + } + + + + private static String PACKAGE_ACTION_OVERWRITEOWNER="overwrite_owner"; + private static String PACKAGE_ACTION_ADDTAG="add_tag"; + private static String PACKAGE_ACTION_REMOVETAG="remove_tag"; + + + private static String PACKAGE_ACTION_ADDNOTIFICATION="add_noti"; + private static String PACKAGE_ACTION_REMOVENOTIFICATION="remove_noti"; + + + + /** + * package memeber action + * @param folder_id + * @param tag_id + * @param function_ids + * @param action + * @return + * @throws Exception + */ + public boolean packageMemberAction(int folder_id, int tag_id, int function_ids[], String action) throws Exception { + + + RFunctionDB rfdb=RFunctionDB.getRFunctionDB(); + try { + boolean rtn=false; + boolean make_owner_also=true; + boolean add_also=true; + + rfdb.connectDB(); + + log.debug("packageMemberAction() folder_id:"+folder_id+" tag_id:"+tag_id+" action:"+action); + + if(action.equals(PACKAGE_ACTION_OVERWRITEOWNER)) { + rfdb.addTag4Package(tag_id, function_ids,make_owner_also); + rtn=true; + } + if(action.equals(PACKAGE_ACTION_ADDTAG)) { + rfdb.addTag4Package(tag_id,function_ids, !make_owner_also); + rtn=true; + } + if(action.equals(PACKAGE_ACTION_REMOVETAG)) { + rfdb.removeTag4Package(tag_id,function_ids); + rtn=true; + } + + if(action.equals(PACKAGE_ACTION_ADDNOTIFICATION)) { + + rfdb.addNotificationTag4Package(tag_id, function_ids, add_also); + rtn=true; + } + if(action.equals(PACKAGE_ACTION_REMOVENOTIFICATION)) { + + rfdb.addNotificationTag4Package(tag_id, function_ids, !add_also); + rtn=true; + } + + + return rtn; + } catch (Exception e) { + + ClientError.reportError(e, null); + throw e; + } finally { + rfdb.closeDB(); + } + } + + + /** + * package desc info + * + * @param path + * @param packagename + * @return + * @throws Exception + */ + + private Map getPackageDescInfo(String path, final String packagename) throws Exception { + + try { + + File[] files = new File(path).listFiles(); + + //sort file names + Arrays.sort(files, new Comparator(){ + public int compare(File f1, File f2) + { + return Long.valueOf(f2.lastModified()).compareTo(f1.lastModified()); + } }); + + + String rtn=null; + + //filter files within the package + FilenameFilter filter = new FilenameFilter() { + public boolean accept(File directory, String fileName) { + return fileName.startsWith(packagename); + } + }; + File[] pfile=files[0].listFiles(filter); + String ex=""; + if(pfile.length>0){ + + + FileInputStream fin = new FileInputStream(pfile[0]); + + if(pfile[0].getPath().endsWith(".tar.gz")){ + + ex="_nix"; + TarArchiveInputStream tai=new TarArchiveInputStream( new GZIPInputStream(fin)); + TarArchiveEntry ze1 = null; + while ((ze1 = tai.getNextTarEntry()) != null) { + if(ze1.getName().equals(packagename+"/DESCRIPTION")){ + byte[] bytes= new byte[(int)ze1.getSize()]; + tai.read(bytes, 0, bytes.length); + rtn= new String( bytes ); + // System.out.println("strUnzipped:"+rtn); + + } + } + tai.close(); + fin.close(); + }else{ + ex="_win"; + ZipInputStream zin; + zin = new ZipInputStream(fin); + ZipEntry ze = null; + while ((ze = zin.getNextEntry()) != null) { + + if(ze.getName().equals(packagename+"/DESCRIPTION")){ + + byte[] bytes= new byte[(int)ze.getSize()]; + zin.read(bytes, 0, bytes.length); + rtn= new String( bytes); + + } + } + + fin.close(); + zin.close(); + + } + + + } + LinkedHashMap h=null; + + File file = new File("c:\\rnd\\temp\\rtn"+ex+".txt"); + FileUtils.writeStringToFile(file, rtn); + + if(rtn!=null){ + h=new LinkedHashMap(); + + Pattern p1 = Pattern.compile("([A-Za-z]+)(:)((\\W)*(\\S)*(.)*)"); + + + + final Matcher matcher = p1.matcher(rtn); + + while(matcher.find()){ + if(matcher.groupCount()>=3){ + final String key = matcher.group(1); + final String value = matcher.group(3); + + h.put(key,value); + } + + } + } + + + return h; + + } catch (Exception e) { + throw e; + } + } + + private String getPackageAccessPrivilege(int folder_id, RFunctionDB rfdb) throws Exception { + String rtn=""; + UserThemeAccessPermission user=getAuthenticatedUserObj(rfdb); + if(user!=null){ + List themes=rfdb.getThemeTags4Folder(folder_id); + for(String ttag:themes){ + if(user.getRwx().contains(ttag)) rtn=ACCESS_PRIVILEGE_RWX; + } + if(themes.size()==0) rtn=ACCESS_PRIVILEGE_RWX; + + } + log.debug("user:"+user); + + 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; + } + + + /** + * Get folder tagname by R function id + * @param r_function_id r function id + * @return folder name + * @throws Exception + */ + public String getFolderThemeByRFunctionId(int r_function_id) throws Exception { + String result = ""; + RFunctionDB rfdb=RFunctionDB.getRFunctionDB(); + try { + rfdb.connectDB(); + result = rfdb.getFolderThemeByRFunctionId(r_function_id); + } catch (Exception e) { + throw e; + } + return result; + } + + + + +} diff --git a/project/bldb/src/main/java/com/fe/client/SchedulerAPIServlet.java b/project/bldb/src/main/java/com/fe/client/SchedulerAPIServlet.java new file mode 100644 index 0000000..47be675 --- /dev/null +++ b/project/bldb/src/main/java/com/fe/client/SchedulerAPIServlet.java @@ -0,0 +1,1041 @@ +/****************************************************************************** +* +* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. +* All rights reserved. +* +******************************************************************************/ + +package com.fe.client; + +import java.io.IOException; +import java.io.PrintWriter; +import java.text.SimpleDateFormat; +import java.util.Collection; +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.StringTokenizer; +import java.util.Timer; +import java.util.TimerTask; +import java.util.Vector; + +import javax.servlet.ServletException; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import net.jxta.peergroup.PeerGroup; +import net.jxta.pipe.PipeService; +import net.jxta.protocol.PipeAdvertisement; + +import org.apache.jcs.JCS; +import org.apache.jcs.engine.behavior.IElementAttributes; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.jfree.util.Log; + +import com.fe.common.Constant; +import com.fe.scheduler.balance.LoadBalancingLinkedQueue; +import com.fe.util.ProxifyURL; +import com.fe.util.RestartTomcat; +import com.fourelementscapital.db.SchedulerDB; +import com.fourelementscapital.scheduler.SchedulerEngine; +import com.fourelementscapital.scheduler.balance.ExecutingQueueCleaner; +import com.fourelementscapital.scheduler.balance.LoadBalancingQueue; +import com.fourelementscapital.scheduler.balance.LoadBalancingQueueItem; +import com.fourelementscapital.scheduler.error.ClientError; +import com.fourelementscapital.scheduler.p2p.MessageBean; +import com.fourelementscapital.scheduler.p2p.P2PAdvertisement; +import com.fourelementscapital.scheduler.p2p.P2PService; +import com.fourelementscapital.scheduler.p2p.listener.IncomingMessage; +import com.fourelementscapital.scheduler.p2p.listener.OutgoingMessage; +import com.fourelementscapital.scheduler.p2p.listener.P2PTransportMessage; +import com.fourelementscapital.scheduler.rscript.RScript; +import com.fourelementscapital.scheduler.rscript.RScriptAsyncListenerImpl; + +/** + * + * SchedulerAPI + * + * + * + * + * + * + * + * + *

run

+ * Executes scheduled tasks + *
+ * Parameters:ids={id1,id2...}
+ * Returns:  
+ * {@code
+ * 
+ * done
+ * }
+ * Example: http://server-ip-or-name/bldb/schedulerAPI?method=run&ids=21,22
+ * 
+ *
+ * + * + * + *

livepeers

+ * This method lists out all live peers + *
+ * Parameters:Nil
+ * Returns:  
+ * {@code
+ * 
+ * 
+ * 	4ECAPSVSG1
+ * 	4ecappcsg11
+ * 	4ecappcsg12
+ * 	4ecappcsg14
+ * 	4ecappcsg7
+ * 	4ecapsvsg5
+ * 	dev-server
+ * 
+ * }
+ * Example: http://server-ip-or-name/bldb/schedulerAPI?method=livepeers
+ * 
+ *
+ * + * + *

queue

+ * This method lists all tasks that currently executing by peers and ready to execute by peer in any moment. + *
+ * Parameters:Nil
+ * Returns:  
+ * {@code
+ * 
+ * 
+ * 	
+ * 	
+ * 
+ * }
+ * Example: http://server-ip-or-name/bldb/schedulerAPI?method=queue
+ * 
+ *
+ * + * + * + * + * + *

syncContract

+ * This method synchronize bloomberg data with Market Contracts those mapped with reference database. + * Accepts optional parameter.
+ * Parameter contract synchronizes only specified contract
+ * Nil parameter synchronizes all new contracts that haven't synchronized so far + *
+ * Parameters:[contract={@code }] if no parameter specified, this synchronizes all new contracts those haven't synchronized yet.
+ * Returns:  
+ * {@code
+ * 
+ * true/false
+ * }
+ * Example: 
+ * http://server-ip-or-name/bldb/schedulerAPI?method=syncContract&contract=CL010
+ * http://server-ip-or-name/bldb/schedulerAPI?method=syncContract 
+ * 
+ *
+ * + * + * + * + * + * + * + *

syncSecurity

+ * This method synchronize bloomberg data with Securities those mapped with reference database. + * Accepts optional parameter.
+ * Parameter security synchronizes only specified contract
+ * Nil parameter synchronizes all new securities that haven't synchronized so far + *
+ * Parameters:[security={@code }] if no parameter specified, this synchronizes all new securities those haven't synchronized yet.
+ * Returns:  
+ * {@code
+ * 
+ * true/false
+ * }
+ * Example: 
+ * http://server-ip-or-name/bldb/schedulerAPI?method=syncSecurity&contract=AIGC
+ * http://server-ip-or-name/bldb/schedulerAPI?method=syncSecurity 
+ * 
+ *
+ * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * @author Rams Kannan + * + */ +public class SchedulerAPIServlet extends HttpServlet { + + + private static String EXECUTETASKS="run"; + private static String GETLIVE="livepeers"; + private static String QUEUE="queue"; + private static String SYNC_REF_CONTRACT="syncContract"; + private static String EXECUTE_R_QUEUEINFO="xRQInfo"; + private static String EXECUTE_R_QUEUE="executeR_queue"; + + private static String PEER_PACKAGES="getPeerPackages"; + private static String PEER_INFO="getPeerInfo"; + private static String PEER_QUEUE="getPeerQueue"; + + private static String SERVER_DATE_TIME="getCurrentDateTime"; + private static String SYNC_REF_SECURITY="syncSecurity"; + + private static String SCHEDULER_QUEUEGROUP_ADD="addGroupIntoQueue"; + private static String SCHEDULER_QUEUEGROUP_REMOVE="removeGroupFromQueue"; + + private static String RESTART_PEER="restartPeer"; + private static String RESTART_PEER_LATER="restartAfterDone"; + private static String RESTART_SERVER="restartServer"; + + private static String PRIORITY_QUEUE="priorityQueue"; + private static String REMOVE_EXECUTING_TASK="removeExecutingTask"; + private static String STRATEGY_XML="strategyXML"; + + private static String EXECUTE_SSCRIPT_P2P="execSyncScript"; + private static String EXECUTE_ASCRIPT_P2P="execAsyncScript"; + private static String EXECUTE_ASCRIPT_P2P_RESULT="getScriptResult"; + + private static int EXPIRY_FOR_UNRETRIEVED=30; //seconds + private static int EXPIRY_FOR_RETRIEVED=2; + + private static String AUTHENTICATION_CHECK="check_auth"; + + + private String msg=null; + + private Logger log = LogManager.getLogger(SchedulerAPIServlet.class.getName()); + + private static JCS cache=null; + private static JCS getCache() throws Exception { + if(cache==null){ + cache=JCS.getInstance("direct_script"); + } + return cache; + } + + public static Map getAllTokens() throws Exception { + + Map rtn=getCache().getMatching("^[A-Za-z0-9\\-]+$"); + return (rtn!=null)?rtn:new HashMap(); //all alpha numeric keys. + + } + + + + public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String method = request.getParameter("method"); + + if(method!=null && !method.equals("") && (method.equals(SchedulerAPIServlet.EXECUTE_SSCRIPT_P2P) || method.equals(SchedulerAPIServlet.EXECUTE_ASCRIPT_P2P))){ + log.debug("debug doPost: method:"+method); + try{ + String script=request.getParameter("script"); + RScript rs=new RScript(); + + + //set unique name from the parameter + //rs.setUniquename("[no-unique-name]"); + + rs.setUniquename(rs.getUid()); + if(request.getParameter("uniquename")!=null && !request.getParameter("uniquename").trim().equals("")){ + rs.setUniquename(request.getParameter("uniquename")); + } + + //set remote host from getRemoteHost + if(request.getRemoteHost()!=null){ + rs.setRequesthost(request.getRemoteHost()); + } + + rs.setTaskuid("direct_script"); + if(request.getParameter("engine")!=null && request.getParameter("engine").equalsIgnoreCase("rserve")){ + rs.setTaskuid("direct_script_unix"); + } + + if(request.getParameter("executeAt")!=null && !request.getParameter("executeAt").equals("")){ + rs.setExecuteAt(request.getParameter("executeAt")); + } + rs.setScript(script); + String id=rs.getUid(); + + IElementAttributes att= getCache().getDefaultElementAttributes(); + att.setMaxLifeSeconds(EXPIRY_FOR_UNRETRIEVED); + + RScriptAsyncListenerImpl liten=new RScriptAsyncListenerImpl(getCache(),att); + getCache().put(id, RScriptAsyncListenerImpl.ALIVE,att); //listener + + LoadBalancingQueue.getExecuteRScriptDefault().addExecuteR(rs,liten); + + boolean finished=false; + String rtn=null; + + if(method.equals(SchedulerAPIServlet.EXECUTE_SSCRIPT_P2P)){ + while(!finished){ + + String ri=(String)getCache().get(id); + if(ri!=null && !ri.equalsIgnoreCase(RScriptAsyncListenerImpl.ALIVE)){ + getCache().remove(id); + rtn=ri; + finished=true; + } + if(ri==null){ + finished=true; + } + try{ + Thread.sleep(10); + }catch(Exception e){ + e.printStackTrace(); + } + } + String resptype=(request.getParameter("responseType")!=null)?request.getParameter("responseType"):"text/xml"; + response.setContentType(resptype); + }else{ + response.setContentType("text/plain"); + rtn=id; + } + PrintWriter out = response.getWriter(); + out.println(rtn); + out.flush(); + + }catch(Exception e){ + e.printStackTrace(); + } + + + }else{ + //report error saying no method parameter found; + } + } + + + public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + String method = request.getParameter("method"); + this.msg="Error in current request, Method value required"; + boolean noerror=false; + if(method!=null && !method.equals("")){ + + + /** + * executing tasks, accepts parameter ids= + * + * expression with code injection can be like the following + * [23:inj] your_code_here [inj][2454:inj] your_code_here [inj] + */ + if(method.equals(SchedulerAPIServlet.EXECUTETASKS) && request.getParameter("ids")!=null && !request.getParameter("ids").equals("") && ( request.getParameter("delay")==null || (request.getParameter("delay")!=null && request.getParameter("delay").equals(""))) ){ + noerror=executeTask(request,response); + }else if(method.equals(SchedulerAPIServlet.EXECUTETASKS) && request.getParameter("ids")!=null && !request.getParameter("ids").equals("") + &&(request.getParameter("delay")!=null && !request.getParameter("delay").equals("")) + ){ + int delay=0; + boolean validdelay=false; + try{ + delay=Integer.parseInt(request.getParameter("delay")); + validdelay=true; + }catch(Exception e){ + validdelay=false; + + } + if(validdelay){ + if(delay==0) noerror=executeTask(request,response); + if(delay>0){ + noerror=executeTaskDelayed(request,response); + } + }else{ + this.msg="Integer value expected for delay"; + noerror=false; + + } + + }else{ + this.msg="Error in current request, Method value required"; + noerror=true; + } + + + /** + * Get peer packages, this request will be forwarded to all online peer and wait few seconds to receive all the responses and returns to UI. + * The responses will includes installed packages and version nos + */ + if(method.equals(SchedulerAPIServlet.PEER_PACKAGES) ){ + String status=null; + try{ + status=getPeerInfo("rpackages"); + }catch(Exception e){ + status=""+e.getMessage()+""; + } + generateResult(response,status); + } + + + /** + * This returns info about all online peers and the response will includes peer started time, executing tasks, JRI version, OS and R version + */ + if(method.equals(SchedulerAPIServlet.PEER_INFO) ){ + String status=null; + try{ + status=getPeerInfo("statistics"); + + }catch(Exception e){ + status=""+e.getMessage()+""; + } + generateResult(response,status); + } + + + /** + * This will return live queue data, such as queued,executing and finished tasks with its trigger time. + */ + if(method.equals(SchedulerAPIServlet.EXECUTE_R_QUEUEINFO) ){ + String status=null; + try{ + ExecuteRMgmt erm=new ExecuteRMgmt(request); + status=erm.getQueueXML(); + + }catch(Exception e){ + status=""+e.getMessage()+""; + } + generateResult(response,status); + } + + + /** + * This will return executeR queue, including number of scripts queued, executing and successfull. + * ExecuteR server is not the same as scheduler server, the request is proxyfied to executeR server + */ + if(method.equals(SchedulerAPIServlet.EXECUTE_R_QUEUE) && request.getParameter("server_ip")!=null ){ + + ProxifyURL purl=new ProxifyURL(request,response); + purl.proxifyGet("http://"+request.getParameter("server_ip")+":8080/bldb/schedulerAPI","method="+SchedulerAPIServlet.EXECUTE_R_QUEUEINFO); + //System.out.println("SchedulerAPIServerlet.class: proxyfy"); + + } + + + + /** + * peer queue information + * recommended not to use this, as there are live stream connection between peers and server available and + * eventually the queue will be maitained at the server level. + * @deprecated + */ + if(method.equals(SchedulerAPIServlet.PEER_QUEUE) ){ + String status=null; + try{ + status=getPeerInfo("peerqueue"); + }catch(Exception e){ + status=""+e.getMessage()+""; + } + generateResult(response,status); + } + + + /** + * This will be used in other applications to check the current session is authenticated + * For example: Tools done in .NET and PHP. + */ + if(method.equals(SchedulerAPIServlet.AUTHENTICATION_CHECK)){ + try{ + authenticationCheck(request,response); + }catch(Exception e){ + generateResult(response,"Error:"+e.getMessage()+""); + } + + } + + /** + * @deprecated + * recommended not to use. + */ + if(method.equalsIgnoreCase(SchedulerAPIServlet.STRATEGY_XML) && request.getParameter("strategy")!=null && request.getParameter("contract")!=null){ + + response.setContentType("text/xml"); + PrintWriter out = response.getWriter(); + String contract=request.getParameter("contract"); + if(contract==null){ + contract=""; + } + try{ + IExecMgmt iemgmt=new IExecMgmt(request,false); + + out.write(iemgmt.getMyParsedXML(request.getParameter("strategy"),contract )); + }catch(Exception e){ + out.write("Error:"+e.getMessage()); + e.printStackTrace(); + } + out.flush(); + + } + + + /** + * Get the result of previously executed executeR script in asynchronous mode, using the returned token + */ + if(method.equals(SchedulerAPIServlet.EXECUTE_ASCRIPT_P2P_RESULT) && request.getParameter("token")!=null && !request.getParameter("token").equals("") ){ + + String ri=null; + String token=null; + try{ + + ri=(String)getCache().get(request.getParameter("token").trim()); + token=request.getParameter("token").trim(); + }catch(Exception e){ + Log.error("Error:"+e.getMessage()); + } + //if(ri!=null && ri.isFinished()){ + if(ri!=null && !ri.equalsIgnoreCase(RScriptAsyncListenerImpl.ALIVE)){ + String resptype=(request.getParameter("responseType")!=null)?request.getParameter("responseType"):"text/xml"; + response.setContentType(resptype); + PrintWriter out = response.getWriter(); + out.print(ri); + out.flush(); + + //remove the result from cache and set the storance once it is retrieved. + try{ + IElementAttributes att= getCache().getDefaultElementAttributes(); + att.setMaxLifeSeconds(EXPIRY_FOR_RETRIEVED); + getCache().remove(token); + getCache().put(token,ri,att); + }catch(Exception e){ + log.error("Error while resetting token in cache"); + } + + }else if(ri!=null && ri.equalsIgnoreCase(RScriptAsyncListenerImpl.ALIVE)) { + //generateResult(response,"not ready"); + //response.setContentType("text/xml"); + String resptype=(request.getParameter("responseType")!=null)?request.getParameter("responseType"):"text/xml"; + response.setContentType(resptype); + PrintWriter out = response.getWriter(); + out.println(""); + out.println("not ready"); + out.flush(); + + }else { + String resptype=(request.getParameter("responseType")!=null)?request.getParameter("responseType"):"text/xml"; + response.setContentType(resptype); + PrintWriter out = response.getWriter(); + out.println(""); + out.println("No script associated to this token, Result for the token expires in "+EXPIRY_FOR_UNRETRIEVED+" seconds. "); + out.flush(); + } + } + + + if(method.equals(SchedulerAPIServlet.SCHEDULER_QUEUEGROUP_ADD) && request.getParameter("taskuid")!=null && !request.getParameter("taskuid").equals("") ){ + String status=addRemoveGroup2Queue(request, response, method); + generateResult(response,""+status+""); + } + + + if(method.equals(SchedulerAPIServlet.REMOVE_EXECUTING_TASK) + && request.getParameter("task_id")!=null && !request.getParameter("task_id").equals("") + && request.getParameter("trigger_time")!=null && !request.getParameter("trigger_time").equals("") + ){ + String status=removeTaskID(request, response); + generateResult(response,""+status+""); + } + + + /** + * adding and removing scheduler group, i.e Bloomberg download, + */ + if(method.equals(SchedulerAPIServlet.SCHEDULER_QUEUEGROUP_REMOVE) && request.getParameter("taskuid")!=null && !request.getParameter("taskuid").equals("") ){ + String status=addRemoveGroup2Queue(request, response, method); + generateResult(response,""+status+""); + } + + /** + * Restart peer, this method is not very reliable as it uses OS dependent tools to kill the running process and it works only on windows peers and still not reliable and consistent. + */ + if((method.equals(SchedulerAPIServlet.RESTART_PEER) || method.equals(SchedulerAPIServlet.RESTART_PEER_LATER)) + && request.getParameter("peer")!=null && !request.getParameter("peer").equals("") + ){ + String status=restartPeer(method,request); + generateResult(response,""+status+""); + } + + + + if(method.equals(SchedulerAPIServlet.SERVER_DATE_TIME)){ + SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z"); + Date d=new Date(); + generateResult(response,"