<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<Rule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Rule_Axmedis.xsd">
  <Header>
    <Rule_Name>lobster-access</Rule_Name>
    <AXRID>axcprule:95d38099-04fd-4b82-918c-736c2ed3f0b4</AXRID>
    <Rule_Version></Rule_Version>
    <Rule_Type>AXCP</Rule_Type>
    <Software_Name></Software_Name>
    <Version_of_software></Version_of_software>
    <Date_of_production>2006-09-11</Date_of_production>
    <Author></Author>
    <Affiliation></Affiliation>
    <URL></URL>
    <Comment></Comment>
    <Last_Modifications>2007-03-22</Last_Modifications>
    <Terminal_ID></Terminal_ID>
    <Cost></Cost>
    <Work_Item_ID></Work_Item_ID>
  </Header>
  <Schedule>
    <Run>
      <Date>2006-09-11</Date>
      <Time>18:34:30</Time>
      <Periodicity Unit="Day">0</Periodicity>
      <Expiration_Date>2006-09-11</Expiration_Date>
      <Expiration_Time>18:34:30</Expiration_Time>
    </Run>
    <Status>Inactive</Status>
  </Schedule>
  <Definition>
    <AXCP_Rule>
      <Arguments/>
      <Rule_Body>
        <JS_Script name="main"><![CDATA[//creates the connection with the lobster server
var l=new LobsterConnection("http://axmedis.learnexact.com/LobsterWebService/lobster.wsdl", "axmedis", "bellini", "Bellini", "http")

try
{
    testCMSQuery(l)
    testCMSInsert(l)
    testCMSUpdate(l)
    testCMSDelete(l)
}
catch(e)
{
    print(e)
}
print("Closing connection...")
l.shutdown()

function testCMSQuery(l)
{
    print("TEST QUERY")
    var lomns = new Namespace("http://www.imsglobal.org/xsd/imsmd_rootv1p2p1")
    
    //search for packages containing Paul in the title 
    var manifest=l.findPackage("manifest[metadata/lom/general/title~=\"Paul\"]", 1, 5)

    if(!existsDir("C:\\lobster-test"))
        makeDir("C:\\lobster-test")
    if(!existsDir("C:\\lobster-test\pifs"))
        makeDir("C:\\lobster-test\pifs")
        
    for(var i=0; i<manifest.length(); i++)
    {
       var m=manifest[i]
       var title=m..lomns::title.lomns::langstring.toString()
       var identifier=m.@identifier.toString()
       print("id: " +identifier+ " title:"+title)
       
       l.getPackageFile(identifier, "imsmanifest.xml", "C:/lobster-test/pifs/"+identifier+".xml")
    }
    
    print("the imsmanifest.xml files are in C:\\lobster-test\\pifs")
}

function testCMSInsert(l)
{
    print("TEST INSERT")
    
    if(!existsFile("C:/lobster-test/test.zip"))
    {
        print("C:/lobster-test/test.zip file is missing")
        return;
    }
    //check if the package is already present
    var lomns = new Namespace("http://www.imsglobal.org/xsd/imsmd_rootv1p2p1")
    var manifest=l.findPackage("manifest[@identifier=\"test\"]", 0, 10)
    if(manifest.length()>0)
    {
        print("Package \"test\" already present, cannot insert it")
        return
    }
    
    var result=l.insertPackage("test","Course","C:/lobster-test/test.zip")
    print("upload result:" + result)

    var manifest=l.findPackage("manifest[@identifier=\"test\"]", 0, 10)
    // find returns an array with the manifests
    if(manifest.length()==0)
    {
        print("INSERT FAILED")
        return
    }    
    for(var i=0; i<manifest.length(); i++)
    {
       var m=manifest[i]
       var title=m..lomns::title.lomns::langstring.toString()
       var identifier=m.@identifier.toString()
       print("id: " +identifier+ " title:"+title.getText())
       if(title!="TEST")
          print("INSERT FAILED")
    }    
}

function testCMSUpdate(l)
{
    print("TEST UPDATE")
    if(!existsFile("C:/lobster-test/test-update.zip"))
    {
        print("C:/lobster-test/test-update.zip file is missing")
        return;
    }
    
    var lomns = new Namespace("http://www.imsglobal.org/xsd/imsmd_rootv1p2p1")

    var manifest=l.findPackage("manifest[@identifier=\"test\"]", 0, 10)
    if(manifest.length()==0)
    {
        print("Package \"test\" not present, cannot update it")
        return
    }

    var result = l.updatePackage("test","C:/lobster-test/test-update.zip")
    print("upload result:" + result)
    
    var manifest=l.findPackage("manifest[@identifier=\"test\"]", 0, 10)
    for(var i=0; i<manifest.length(); i++)
    {
       var m=manifest[i]
       var title=m..lomns::title.lomns::langstring.toString()
       var identifier=m.@identifier.toString()
       print("id: " +identifier+ " title:"+title)
       if(title.getText()!="TEST - updated")
          print("UPDATE FAILED")
    }    
}

function testCMSDelete(l)
{
    print("TEST DELETE")

    var manifest=l.findPackage("manifest[@identifier=\"test\"]", 0, 10)
    if(manifest.length()==0)
    {
        print("Package \"test\" not present, cannot delete it")
        return
    }

    l.deletePackage("test")
    
    var manifest=l.findPackage("manifest[@identifier=\"test\"]", 0, 10)
    if(manifest.length()>0)
       print("DELETE FAILED")
}]]>
        </JS_Script>
        <JS_Script name="lobster-4-0">
          
          <![CDATA[// ILABS LOBSTER Connection service
// class LobsterConnection allows to interact with the LOBSTER WS v 4.0
// constructor LobsterConnection(wsdl,domain,user,passw,uploadtype)
//    allows to connect with the lobster WS, it performs the 
//    login and retrieves domain information, the uploadtype can be "ftp"(default) or http",
//    it indicates how to upload files on the server.
//    Example:
//       var l=new LobsterConnection("http://host.org/lobster.wsdl", "domain", "user", "passw")
// methods
//    insertPackage(packid, objectType, pifLocalFilepath)
//       allows to insert the package into the CMS giving the 
//       id of the package, the type of object to be uploaded
//       ("Course" or 0, "LearningObject" or 1) and the local 
//       filename for the package
//       Example:
//           l.insertPackage("test","Course","C:/test.zip")
//    updatePackage(packid, pifLocalFilepath)
//       allows to update a package into the CMS giving the 
//       id of the package, and the local filename for the 
//       updated package.
//       Example:
//           l.updatePackage("test","C:/test-update.zip")
//    result findPackage(XPATHQuery, start, size)
//       allows to perform an XPATH query on the CMS, the start 
//       and size values indicate the results to be returned
//       the return value is an array with the xml parsed results
//       Example:
//          metad=l.findPackage("manifest[@identifier=\"test\"]/metadata", 0, 100)
//          var title=metad[0].childElement("lom").childElement["general"].childElement("title").getText()
//    getPackageFile(packid, remoteFile, localFile)
//       allows to download files of a package from the server, 
//       the file is stored in the localfile
//       Example:
//          l.getPackageFile("test","imsmanifest.xml","C:/packs/imsmanifest.xml")
//          l.getPackageFile("test","res90800.htm","C:/packs/res90800.htm")
//    deletePackage(packid)
//       allows to delete a package
//       Example:
//          l.deletePackage("test")
//    logout(), shutdown()
//       allows to logout from the server, note that no multiple logins 
//       of the same user are allowed.
//       Example:
//          l.logout()
 
function LobsterConnection(wsdl,domain,user,passw,uploadType)
{
    if(uploadType==null)
        uploadType="ftp"
    this.wsdl=wsdl
    this.domain=domain
    this.user=user
    this.passw=passw
    this.uploadType=uploadType
    this.insertPackage=LOBSTER_insert
    this.updatePackage=LOBSTER_update
    this.findPackage=LOBSTER_find
    this.getPackageFile=LOBSTER_getFile
    this.uploadFile=LOBSTER_uploadFile
    this.deletePackage=LOBSTER_delete
    this.logout=LOBSTER_logout
    this.shutdown=LOBSTER_logout
    
    //create the connection with the wsdl
    this.ws=new WebServiceConnection(wsdl)
    
    //request the Lobster version
    this.version=this.ws.GetVersion()[0]
    print("connected with Lobster v."+this.version)
    if(uploadType=="http" && this.version<4.0)
        uploadType="ftp"
    
    //perform the login and obtain the session id
    this.sid=this.ws.Login(domain, user, passw)[0]
    print("SID:  "+this.sid)
        
    //retrieve the domain information and store them in the object
    var domainInfo=this.ws.GetDomainInfo(this.sid)[0]
    var dinfo = domainInfo.substring(domainInfo.indexOf("?>")+2)
    var doc=new XML(dinfo)
    this.domainurl=doc.domainurl.toString()
    this.coursedomainpath=doc.coursedomainpath.toString()
    this.ftpuser=doc.ftpuser.toString()
    this.ftppassw=doc.ftppassword.toString()
    this.ftpport=doc.ftpport.toString()
}
function LOBSTER_insert(packid, objType, pifFilePath)
{
    if(this.sid=="") return "FAIL"
    var oType=0
    
    if(typeof(objType)=="string")
    {
        if(objType.toLowerCase()=="course" )
            oType=0 //course
        else
            oType=1 //learning object
    }
    else if(typeof(objType)=="numeric")
        oType=objType
    
    //make request of update for the package
    var filename=this.ws.InsertPackageRequest(this.sid,packid,oType)[0]
    //upload the file
    var result = this.uploadFile(filename, pifFilePath)
    
    try
    {
        //send the notification of upload
        this.ws.NotifyUploadDone(this.sid)
    }
    catch(e)
    {}
    
    return result
}
function LOBSTER_update(packid, pifFilePath)
{
    if(this.sid=="") return "FAIL"
    //make request of update for the package
    var filename=this.ws.UpdatePackageRequest(this.sid,packid)[0]
        
    //upload the file with the name indicated 
    var result = this.uploadFile(filename, pifFilePath)
    //send the notification of upload
    try
    {
        this.ws.NotifyUploadDone(this.sid)
    }
    catch(e)
    {}
    
    return result
}
function LOBSTER_find(XPATHQuery, start, size)
{
    if(this.sid=="") return "FAIL"
    
    //make a XPATH query
    var result=this.ws.FindPackage(this.sid,XPATHQuery, start, size)
    if(result.length>0)
    {
        var res = result[0].substring(result[0].indexOf("?>")+2) //removes the <?xml...?>
    
        //parse the xml result removing the tamino elements
        // fill an array with the results
        var resDom=new XML(res)
        var imsns = new Namespace("http://www.imsproject.org/xsd/imscp_rootv1p1p2")
        return resDom..imsns::manifest
    }
    return []
}
function LOBSTER_getFile(packid, remoteFile, localFilePath)
{
    //request the folder where the files of the package are
    //var foldername=this.ws.GetPackageFolder(this.sid,packid)
    
    //build the url of the file to get
    //var url=this.domainurl+this.coursedomainpath+foldername+"/"+remoteFile
    var url=this.domainurl+this.coursedomainpath+packid+"/last/"+remoteFile
    //download the file from the server
    var http=new HttpConnection()
    http.getToFile(url,localFilePath);
    return http.getResultMsg()
}
function LOBSTER_uploadFile(remoteFilename,pifFilePath)
{
    var result = ""
    if(this.uploadType=="ftp")
    {
        //obtain the name of the host from the domain url
        var ftpurl=this.domainurl
        if(ftpurl.substr(0,7)=="http://")
            ftpurl=ftpurl.substr(7)
        ftpurl=ftpurl.substring(0,ftpurl.indexOf(":"))
        //upload the file with the name indicated 
        var ftp=new FtpConnection()
        ftp.setLogin(this.ftpuser, this.ftppassw)
        ftp.upload(ftpurl+"/"+remoteFilename,pifFilePath)
        result=ftp.getResultMsg()
    }
    else if(this.uploadType=="http")
    {
        var url = this.domainurl + "/X-Siter/packLight/wsUploadFile.asp"
        var http = new HttpConnection
        var header = [ "LEXUPLOADSID: " + this.sid, "Pragma: no-cache", "Expect:", "Accept:" ]
        http.postFromFile(url, pifFilePath, header)
        result = http.getResultMsg()
    }
    return result;
}
function LOBSTER_delete(packid)
{
    if(this.sid=="") return "FAIL"
    
    //perform the delete request
    try
    {
        this.ws.DeletePackage(this.sid,packid)
    }
    catch(e) 
    {}
}
function LOBSTER_logout()
{
    //perform the shutdown request
    try
    {
        this.ws.Shutdown(this.sid)
    }
    catch(e)
    {}
}]]>
        </JS_Script>
      </Rule_Body>
      <Dependencies/>
    </AXCP_Rule>
  </Definition>
</Rule>

