<commit_before>package com.ligadata.MetadataAPI.Utility

import java.io.File

import com.ligadata.MetadataAPI.MetadataAPIImpl

import scala.io.Source
import org.apache.log4j._
/**
 * Created by dhaval on 8/12/15.
 */
object FunctionService {
  private val userid: Option[String] = Some("metadataapi")
  val loggerName = this.getClass.getName
  lazy val logger = Logger.getLogger(loggerName)

  def addFunction(input: String): String ={
    var response = ""
    var functionFileDir: String = ""
    //val gitMsgFile = "https://raw.githubusercontent.com/ligadata-dhaval/Kamanja/master/HelloWorld_Msg_Def.json"
    if (input == "") {
      functionFileDir = MetadataAPIImpl.GetMetadataAPIConfig.getProperty("FUNCTION_FILES_DIR")
      if (functionFileDir == null) {
        response = "FUNCTION_FILES_DIR property missing in the metadata API configuration"
      } else {
        //verify the directory where messages can be present
        IsValidDir(functionFileDir) match {
          case true => {
            //get all files with json extension
            val types: Array[File] = new java.io.File(functionFileDir).listFiles.filter(_.getName.endsWith(".json"))
            types.length match {
              case 0 => {
                println("Functions not found at " + functionFileDir)
                "Functions not found at " + functionFileDir
              }
              case option => {
                val functionDefs = getUserInputFromMainMenu(types)
                for (functionDef <- functionDefs) {
                  response += MetadataAPIImpl.AddFunctions(functionDef.toString, "JSON", userid)
                }
              }
            }
          }
          case false => {
            //println("Message directory is invalid.")
            response = "Message directory is invalid."
          }
        }
      }
    } else {
      //input provided
      var function = new File(input.toString)
      if(function.exists()){
        val functionDef = Source.fromFile(function).mkString
        response = MetadataAPIImpl.AddFunctions(functionDef.toString, "JSON", userid)
      }else{
        response="File does not exist"
      }
    }
    response
  }
  def getFunction(param: String = ""): String ={
    var response=""
    try {
      if (param.length > 0) {
        val(ns, name, ver) = com.ligadata.kamanja.metadata.Utils.parseNameToken(param)
        try {
          return MetadataAPIImpl.GetFunctionDef(ns, name,"JSON", userid)
        } catch {
          case e: Exception => e.printStackTrace()
        }
      }
      val functionKeys = MetadataAPIImpl.GetAllFunctionsFromCache(true, None)
      if (functionKeys.length == 0) {
        val errorMsg="Sorry, No functions available, in the Metadata, to display!"
        response=errorMsg
      }
      else{
        println("\nPick the type to be displayed from the following list: ")
        var srno = 0
        for(functionKey <- functionKeys){
          srno+=1
          println("["+srno+"] "+functionKey)
        }
        println("Enter your choice: ")
        val choice: Int = readInt()

        if (choice < 1 || choice > functionKeys.length) {
          val errormsg="Invalid choice " + choice + ". Start with the main menu."
          response=errormsg
        }
        val functionKey = functionKeys(choice - 1)
        val functionKeyTokens = functionKey.split("\\.")
        val functionNameSpace = functionKeyTokens(0)
        val functionName = functionKeyTokens(1)
        val functionVersion = functionKeyTokens(2)
        response = MetadataAPIImpl.GetFunctionDef(functionNameSpace, functionName,"JSON", userid).toString
      }

    } catch {
      case e: Exception => {
        response=e.getStackTrace.toString
      }
    }
    response
  }
  def removeFunction(param: String = ""): String ={
    var response=""
    try {
      if (param.length > 0) {
        val(ns, name, ver) = com.ligadata.kamanja.metadata.Utils.parseNameToken(param)
        try {
          return MetadataAPIImpl.RemoveFunction(ns, name,ver.toInt, userid)
        } catch {
          case e: Exception => e.printStackTrace()
        }
      }

      val functionKeys =MetadataAPIImpl.GetAllFunctionsFromCache(true, None)
      if (functionKeys.length == 0) {
        val errorMsg="Sorry, No functions available, in the Metadata, to delete!"
        //println(errorMsg)
        response=errorMsg
      }
      else{
        println("\nPick the function to be deleted from the following list: ")
        var srno = 0
        for(functionKey <- functionKeys){
          srno+=1
          println("["+srno+"] "+functionKey)
        }
        println("Enter your choice: ")
        val choice: Int = readInt()

        if (choice < 1 || choice > functionKeys.length) {
          val errormsg="Invalid choice " + choice + ". Start with the main menu."
          //println(errormsg)
          response=errormsg
        }
        val fcnKey = functionKeys(choice - 1)

        val(fcnNameSpace, fcnName, fcnVersion) = com.ligadata.kamanja.metadata.Utils.parseNameToken(fcnKey)

        response=MetadataAPIImpl.RemoveFunction(fcnNameSpace, fcnName, fcnVersion.toLong, userid)
      }
    } catch {
      case e: Exception => {
        //e.printStackTrace
        response=e.getStackTrace.toString
      }
    }
    response
  }
  def updateFunction(input: String): String ={
    var response = ""
    var functionFileDir: String = ""
    //val gitMsgFile = "https://raw.githubusercontent.com/ligadata-dhaval/Kamanja/master/HelloWorld_Msg_Def.json"
    if (input == "") {
      functionFileDir = MetadataAPIImpl.GetMetadataAPIConfig.getProperty("FUNCTION_FILES_DIR")
      if (functionFileDir == null) {
        response = "FUNCTION_FILES_DIR property missing in the metadata API configuration"
      } else {
        //verify the directory where messages can be present
        IsValidDir(functionFileDir) match {
          case true => {
            //get all files with json extension
            val types: Array[File] = new java.io.File(functionFileDir).listFiles.filter(_.getName.endsWith(".json"))
            types.length match {
              case 0 => {
                println("Functions not found at " + functionFileDir)
                "Functions not found at " + functionFileDir
              }
              case option => {
                val functionDefs = getUserInputFromMainMenu(types)
                for (functionDef <- functionDefs) {
                  response += MetadataAPIImpl.UpdateFunctions(functionDef.toString, "JSON", userid)
                }
              }
            }
          }
          case false => {
            //println("Message directory is invalid.")
            response = "Message directory is invalid."
          }
        }
      }
    } else {
      //input provided
      var function = new File(input.toString)
      if(function.exists()){
        val functionDef = Source.fromFile(function).mkString
        response = MetadataAPIImpl.UpdateFunctions(functionDef.toString, "JSON", userid)

      }else{
        response="File does not exist"
      }
    }
    response<commit_msg>Minor changes in the structure<commit_after>package com.ligadata.MetadataAPI.Utility

import java.io.File

import com.ligadata.MetadataAPI.MetadataAPIImpl

import scala.io.Source
import org.apache.log4j._
/**
 * Created by dhaval on 8/12/15.
 */
object FunctionService {
  private val userid: Option[String] = Some("metadataapi")
  val loggerName = this.getClass.getName
  lazy val logger = Logger.getLogger(loggerName)

  def addFunction(input: String): String ={
    var response = ""
    var functionFileDir: String = ""
    //val gitMsgFile = "https://raw.githubusercontent.com/ligadata-dhaval/Kamanja/master/HelloWorld_Msg_Def.json"
    if (input == "") {
      functionFileDir = MetadataAPIImpl.GetMetadataAPIConfig.getProperty("FUNCTION_FILES_DIR")
      if (functionFileDir == null) {
        response = "FUNCTION_FILES_DIR property missing in the metadata API configuration"
      } else {
        //verify the directory where messages can be present
        IsValidDir(functionFileDir) match {
          case true => {
            //get all files with json extension
            val types: Array[File] = new java.io.File(functionFileDir).listFiles.filter(_.getName.endsWith(".json"))
            types.length match {
              case 0 => {
                println("Functions not found at " + functionFileDir)
                "Functions not found at " + functionFileDir
              }
              case option => {
                val functionDefs = getUserInputFromMainMenu(types)
                for (functionDef <- functionDefs) {
                  response += MetadataAPIImpl.AddFunctions(functionDef.toString, "JSON", userid)
                }
              }
            }
          }
          case false => {
            //println("Message directory is invalid.")
            response = "Message directory is invalid."
          }
        }
      }
    } else {
      //input provided
      var function = new File(input.toString)
      if(function.exists()){
        val functionDef = Source.fromFile(function).mkString
        response = MetadataAPIImpl.AddFunctions(functionDef.toString, "JSON", userid)
      }else{
        response="File does not exist"
      }
    }
    response
  }
  def getFunction(param: String = ""): String ={
    var response=""
    try {
      if (param.length > 0) {
        val(ns, name, ver) = com.ligadata.kamanja.metadata.Utils.parseNameToken(param)
        try {
          return MetadataAPIImpl.GetFunctionDef(ns, name,"JSON", userid)
        } catch {
          case e: Exception => e.printStackTrace()
        }
      }
      val functionKeys = MetadataAPIImpl.GetAllFunctionsFromCache(true, None)
      if (functionKeys.length == 0) {
        val errorMsg="Sorry, No functions available, in the Metadata, to display!"
        response=errorMsg
      }
      else{
        println("\nPick the type to be displayed from the following list: ")
        var srno = 0
        for(functionKey <- functionKeys){
          srno+=1
          println("["+srno+"] "+functionKey)
        }
        println("Enter your choice: ")
        val choice: Int = readInt()

        if (choice < 1 || choice > functionKeys.length) {
          val errormsg="Invalid choice " + choice + ". Start with the main menu."
          response=errormsg
        }
        val functionKey = functionKeys(choice - 1)
        val functionKeyTokens = functionKey.split("\\.")
        val functionNameSpace = functionKeyTokens(0)
        val functionName = functionKeyTokens(1)
        val functionVersion = functionKeyTokens(2)
        response = MetadataAPIImpl.GetFunctionDef(functionNameSpace, functionName,"JSON", userid).toString
      }

    } catch {
      case e: Exception => {
        response=e.getStackTrace.toString
      }
    }
    response
  }
  def removeFunction(param: String = ""): String ={
    var response=""
    try {
      if (param.length > 0) {
        val(ns, name, ver) = com.ligadata.kamanja.metadata.Utils.parseNameToken(param)
        try {
          return MetadataAPIImpl.RemoveFunction(ns, name,ver.toInt, userid)
        } catch {
          case e: Exception => e.printStackTrace()
        }
      }

      val functionKeys =MetadataAPIImpl.GetAllFunctionsFromCache(true, None)
      if (functionKeys.length == 0) {
        val errorMsg="Sorry, No functions available, in the Metadata, to delete!"
        //println(errorMsg)
        response=errorMsg
      }
      else{
        println("\nPick the function to be deleted from the following list: ")
        var srno = 0
        for(functionKey <- functionKeys){
          srno+=1
          println("["+srno+"] "+functionKey)
        }
        println("Enter your choice: ")
        val choice: Int = readInt()

        if (choice < 1 || choice > functionKeys.length) {
          val errormsg="Invalid choice " + choice + ". Start with the main menu."
          //println(errormsg)
          response=errormsg
        }
        val fcnKey = functionKeys(choice - 1)

        val(fcnNameSpace, fcnName, fcnVersion) = com.ligadata.kamanja.metadata.Utils.parseNameToken(fcnKey)

        response=MetadataAPIImpl.RemoveFunction(fcnNameSpace, fcnName, fcnVersion.toLong, userid)
      }
    } catch {
      case e: Exception => {
        //e.printStackTrace
        response=e.getStackTrace.toString
      }
    }
    response
  }
  def updateFunction(input: String): String ={
    var response = ""
    var functionFileDir: String = ""
    //val gitMsgFile = "https://raw.githubusercontent.com/ligadata-dhaval/Kamanja/master/HelloWorld_Msg_Def.json"
    if (input == "") {
      functionFileDir = MetadataAPIImpl.GetMetadataAPIConfig.getProperty("FUNCTION_FILES_DIR")
      if (functionFileDir == null) {
        response = "FUNCTION_FILES_DIR property missing in the metadata API configuration"
      } else {
        //verify the directory where messages can be present
        IsValidDir(functionFileDir) match {
          case true => {
            //get all files with json extension
            val types: Array[File] = new java.io.File(functionFileDir).listFiles.filter(_.getName.endsWith(".json"))
            types.length match {
              case 0 => {
                println("Functions not found at " + functionFileDir)
                "Functions not found at " + functionFileDir
              }
              case option => {
                val functionDefs = getUserInputFromMainMenu(types)
                for (functionDef <- functionDefs) {
                  response += MetadataAPIImpl.UpdateFunctions(functionDef.toString, "JSON", userid)
                }
              }
            }
          }
          case false => {
            //println("Message directory is invalid.")
            response = "Message directory is invalid."
          }
        }
      }
    } else {
      //input provided
      var function = new File(input.toString)
      if(function.exists()){
        val functionDef = Source.fromFile(function).mkString
        response = MetadataAPIImpl.UpdateFunctions(functionDef.toString, "JSON", userid)
      }else{
        response="File does not exist"
      }
    }
    response
{'fraction_non_alphanumeric': 0.10216110019646366, 'fraction_numerical': 0.0032276171765366264, 'mean_word_length': 2.143581826202029, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 35, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3114297', 'n_tokens_mistral': 3903, 'n_tokens_neox': 3833, 'n_words': 927}