<?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>ScriptLicensingPush1</Rule_Name>
    <AXRID>axcprule:705892a8-4d76-4de6-867e-e69e11b8df36</AXRID>
    <Rule_Version></Rule_Version>
    <Rule_Type>AXCP</Rule_Type>
    <Software_Name></Software_Name>
    <Version_of_software></Version_of_software>
    <Date_of_production>2006-10-23</Date_of_production>
    <Author></Author>
    <Affiliation></Affiliation>
    <URL></URL>
    <Comment></Comment>
    <Last_Modifications>2006-10-25</Last_Modifications>
    <Terminal_ID></Terminal_ID>
    <Cost></Cost>
    <Work_Item_ID></Work_Item_ID>
  </Header>
  <Schedule>
    <Run>
      <Date>2006-10-23</Date>
      <Time>19:09:18</Time>
      <Periodicity Unit="Day">0</Periodicity>
      <Expiration_Date>2006-10-23</Expiration_Date>
      <Expiration_Time>19:09:18</Expiration_Time>
    </Run>
    <Status>Inactive</Status>
  </Schedule>
  <Definition>
    <AXCP_Rule>
      <Arguments>
        <Parameter Name="axuids_file" Type="String">x:\BCert.txt</Parameter>
        <Parameter Name="axoids_file_for_production" Type="String">x:\production.txt</Parameter>
        <Parameter Name="axoids_file_for_production2" Type="String">x:\production2.txt</Parameter>
        <Parameter Name="usecasenum" Type="Integer">1</Parameter>
        <Parameter Name="AXDID" Type="String">URN:AXMEDIS:00001:BUS:3A2F35F1-5E91-314C-A51A-D07D75C216C7</Parameter>
        <Parameter Name="LicType" Type="String">A</Parameter>
      
      </Arguments>
      <Rule_Body>
        <JS_Script name="main"><![CDATA[function ProduceLicense(TYPE,objId,distID,userID)
{ 
    var lic;
    var id = objId.replace("\n","");
    if(TYPE=="A")
    {
        var grant = createAGrant(id,"mx:play");
        lic = createBxLicense(distID,userID,grant)
    }
    else if(TYPE=="B1")
    {
        var grant = createB1Grant(id,"mx:play","1.00","iso:EUR");
        lic = createBxLicense(distID,userID,grant)
    }
    else if(TYPE=="B3")
    {
        var grant = createB3Grant(id,"mx:play","2007-10-23T00:00:00","2006-10-23T00:00:00",10);
        lic = createBxLicense(distID,userID,grant)
    }
    return lic; //"license for " + TYPE + " - " + obj + " - " + AXDID + " - " + user;
}

function PostLic(lic)
{
    //"192.168.0.108"
    print("Posting License on PMS");
    var PMSClientEndpoint="https://192.168.0.108:8502/PMS/";
    var PMSClientCertificate="client.pem";
    var PMSClientCertificatePsw="axmedis";
    var PMSClientCA="cacert.pem";
    var pms = new PMSClient(PMSClientEndpoint,PMSClientCertificate,PMSClientCertificatePsw,PMSClientCA);
    pms.sendLicense(lic.getXMLLicense());
    print ("License Posted on PMS");
}
  
function SaveLicOnDisk(lic,objId)
{
  var id = objId;
  lic.StoreLicenseToFile(licFolder+"\\license_"+id+".xml");
  
  /*var path = licFolder+"\\license_"+id+".xml";
  writeToFile(path,lic.getXMLLicense());*/
  print("License Saved On Disk");
} 

function LicensingPush(AXDID,TYPE,USERGROUP, usecasenum)
{

  // read user identifiers
  var str = readFromFile(USERGROUP);
  print("str="+str);  
  var usrs_array = str.split("\n");
  var usr_num=0;
  while (usr_num < usrs_array.length)
  {
      print(usr_num+"--"+usrs_array[usr_num]);
      usr_num+=1;
  }
  
  // read objects identifiers
  if (usecasenum==1)
 {
    str = readFromFile(axoids_file_for_production);
    print ("------ PRODUCTION ------");
  }
  else
  {
    str = readFromFile(axoids_file_for_production2);
    print ("------ PRODUCTION 2 ------");
  }
  print("str="+str);  
  var objs_array = str.split("\n");
  var objs_num=0;
  while (objs_num < objs_array.length)
  {
      print(objs_num+"--"+objs_array[objs_num]);
      objs_num+=1;
  }
  
  // for each user, for each object produce license, post it and save it on disk
  var i = 0;
  for (user in usrs_array)
  {
    for (obj in objs_array)
    { 
      if(objs_array[obj].length>0)
      {
          var p =  usrs_array[user]
          var lic = ProduceLicense(TYPE,objs_array[obj],AXDID,usrs_array[user]);
          print ("license is "+lic);
          PostLic(lic);
          SaveLicOnDisk(lic,i);
          i++;
      }
    }
  }
  
  return true;
}

var licFolder = "C:\\factory";
a = LicensingPush(AXDID,LicType,axuids_file,usecasenum);]]>
        </JS_Script>
        <JS_Script name="licenseGenerator">
          
          <![CDATA[/*******************************************************
* A License Generation functions from MB License *
********************************************************/
// Completed - it works
function createALicense(creatorId,resId,userId)
{
    return createMotherLicense(creatorId,resId,userId,"mx:play",null);
}
/***********************************************************
*   Grant: function for creating grant for A license    *
************************************************************/
function createAGrant(resId, right)
{ 
     var grant = new Grant();
     var rights = new Right();
     rights.setRight(right);
     var res = new Resource();
     res.resourceId = resId;
     res.diType = 1;
     grant.setResource(res);
     grant.setRight(rights);
     return grant;
}
/*******************************************************
* B License Generation functions from MB License *
********************************************************/
//it works but conditions check is missing
function createB1FromMB1License(lic,userId)
{
   var b1Lic = new License();
   var grant = lic.grantgroup.grants[0];
   var principal = grant.getPrincipal();
   var subgrant = grant.resource.ggResource.grants[0];
   var rIssuer = new Issuer();
   rIssuer.setIssuer(principal.name);
   var rGrantGroup=new GrantGroup();
   var rGrant = new Grant()
   rGrant.setPrincipal(grant.principal);
   rGrant.setResource(subgrant.resource);
   rGrant.setRight(subgrant.right);
   rGrantGroup.addGrant(rGrant);
   b1Lic.setGrantGroup(rGrantGroup);
   b1Lic.setIssuer(rIssuer);
   return b1Lic;
}
// Missing the Interval or the way to set the "validityIntervalFloating"
function createB2FromMB2License(lic,userId)
{
   var b2Lic = new License();
   var grant = lic.grantgroup.grants[0];
   var principal = grant.getPrincipal();
   var subgrant = grant.resource.ggResource.grants[0];
   
   var rIssuer = new Issuer();
   rIssuer.setIssuer(principal.name);
   var rGrantGroup=new GrantGroup();
   var rGrant = new Grant()
   rGrant.setPrincipal(grant.principal);
   rGrant.setResource(subgrant.resource);
   rGrant.setRight(subgrant.right);
   rGrantGroup.addGrant(rGrant);
   b2Lic.setGrantGroup(rGrantGroup);
   b2Lic.setIssuer(rIssuer);
   return b2Lic;
}
// Completed - it works
function createB3FromMB3License(lic,userId)
{
   var b3Lic = new License();
   var grant = lic.grantgroup.grants[0];
   var principal = grant.getPrincipal();
   var subgrant = grant.resource.ggResource.grants[0];
   
   var rIssuer = new Issuer();
   rIssuer.setIssuer(principal.name);
   var rGrantGroup=new GrantGroup();
   var rGrant = new Grant()
   rGrant.setPrincipal(grant.principal);
   rGrant.setResource(subgrant.resource);
   rGrant.setRight(subgrant.right);
   rGrantGroup.addGrant(rGrant);
   b3Lic.setGrantGroup(rGrantGroup);
   b3Lic.setIssuer(rIssuer);
   return b3Lic;
}
/****************************************************
*           Bx License Generation functions         *
*****************************************************/
function createB3Grant(resId, right, notAfter, notBefore, count)
{
        var grant = new Grant();
        var rights = new Right();
        var timecondition = new Interval();                                    
        var limit = new LNumber();
        var res = new Resource();
        res.resourceId = resId;
        res.diType = 1;    
        limit.count = count;
        rights.setRight(right)
        timecondition.notBefore = notBefore;
        timecondition.notAfter = notAfter;
        grant.addCondition(timecondition);
        grant.addCondition(limit);
        grant.setRight(rights);
        grant.setResource(res);
        return grant;
}
function createB2Grant(resId, right, time)
{
        var grant = new Grant();
        var rights = new Right();
        var res = new Resource();
        res.resourceId = resId;
        res.diType = 1;        
        rights.setRight(right);
        grant.setResource(res);
        grant.addCondition(timecondition);
        grant.addCondition(limit);
        grant.setRight(right);
        return grant;
}
function createB1Grant(resId, right, amount, currency)
{ 
     var grant = new Grant();
     var fee = new Fee();
     var rights = new Right();
     rights.setRight(right);
     fee.amount = parseFloat(amount);
     fee.currency = "iso:EUR";
     fee.tipoFee = 1;
     var res = new Resource();
     res.resourceId = resId;
     res.diType = 1;
     grant.setResource(res);
     grant.addCondition(fee);
     grant.setRight(rights);
     return grant;
}
/****************************************************
*     Bx License Generation function from grant     *
*****************************************************/
function createBxLicense(distributorId,userId,grant)
{
    var license = new License();
    var grantGroup = new GrantGroup();
    var issuer = new Issuer();
    var principal = new Principal();
       
    principal.setName(userId);
    grant.setPrincipal(principal);
    grantGroup.addGrant(grant);
    issuer.setIssuer(distributorId);
    license.setGrantGroup(grantGroup);
    license.setIssuer(issuer);
    return license;
}
/****************************************************
*           Mother License Generation function      *
*****************************************************/
function createMotherLicense(creatorId,userId,grant)
{
    var license = new License();
    var grantGroup = new Array(new GrantGroup(),new GrantGroup());
    var grant0 = new Grant();
    var issuer = new Issuer();
    var rights = new Right();
    var principal = new Principal();
    var res = new Array (new Resource(),new Resource());
    
    //level 1
    grantGroup[1].addGrant(grant);
      
    //level 0 - grant Group
    res[0].setGrantGroup(grantGroup[1]);
    rights.setRight("r:issue");
    principal.setName(userId);
    grant0.setPrincipal(principal);
    grant0.setResource(res[0]);
    grant0.setRight(rights);
    grantGroup[0].addGrant(grant0);
    //level 0 - issuer
    issuer.setIssuer(creatorId);
    license.setGrantGroup(grantGroup[0]);
    license.setIssuer(issuer);
    return license;
}]]>
        </JS_Script>
      </Rule_Body>
      <Dependencies/>
    </AXCP_Rule>
  </Definition>
</Rule>

