Managing license keys

This example consists of two CSP pages. Create GetLicenseKeys.csp, and ManageLicenseKey.csp. Copy the corresponding code from below into the CSP pages. Start the example with GetLicenseKeys.csp.

Note:    Before running the example see "To use sample files" in Examples (JavaScript).

GetLicenseKeys.csp


<% @language=JavaScript %>

<link REL=STYLESHEET TYPE='text/css'>

<!-- #include file=helper_js.csp -->

<!-- #include file=ceconstjs.inc -->

<%

// This page is the first page in the example. It provides a form

// that shows all license keys in the APS. The user can

// then select a license key to display, remove or add.

%>

<html>

<body>

<%

    

function GetLicenses(IStore)

// Retrieve a list of all licenses on the APS. Return the list

// in an HTML string that will draw a drop down box.

// Parameters:

// IStore - The InfoStore object.

{

    try{

        // Query for all licenses.

        var Licenses = IStore.Query

        ("SELECT SI_ID, SI_LICENSE_KEY FROM CI_SYSTEMOBJECTS WHERE SI_PROGID='CrystalEnterprise.LicenseKey'");

        if (Licenses.Count == 0)

        {    

            return "There are no licenses to display.";

        }

        var k;

        var License;

        var HTMLTable="<select name=LicenseKeyId>";

        // Iterate through all licenses and build the drop down box.

        for (k=1;k<=Licenses.Count;k++)

        {

            License = Licenses.Item(k).PluginInterface("");

            HTMLTable = HTMLTable + "<option value='"+Licenses.Item(k).Id+"'>"+License.LicenseKey;

        }

        HTMLTable= HTMLTable + "</select>";

        return HTMLTable;

    }

    catch (e)

    {

        Response.Write("The creation of the File event failed.");

        return;

    };    

}



function Main()

{

    //Retrieve the InfoStore object.

    var IStore = GetSession("IStore");


    // If the infostore object is not found then display an error message.

    if (IStore == null)

    {

    Response.Write ("Example failed: try logging on first!");

    };

    

    Response.Write ("<H2>License Key Management</H2>");

    Response.Write ("<form action='ManageLicenseKey.csp' method=POST>");

    Response.Write ("<B><FONT SIZE=+1>Add a new license key</FONT></B>");


    Response.Write ("<P>Enter the license code and click Add:<BR>");

    Response.Write("<input type=text name='LicenseKey' size=25 maxlength=21>");

    Response.Write ("<BR><input type=submit name='Add' value='Add'><HR>");


    Response.Write ("<B><FONT SIZE=+1>Display or remove a license key</FONT></B><P>");

    Response.Write (GetLicenses(IStore));

    

    Response.Write ("<P>Select the license key and click Display to view the properties:");

    Response.Write ("<BR><input type=submit name='Display' value='Display'> ");


    Response.Write ("<P>Select the license key and click Remove to delete:");

    Response.Write ("<BR><input type=submit name='Remove' value='Remove'>");


    Response.Write ("</FORM>");

}


Main();

    

%>

</body>

</html>


ManageLicenseKey.csp


<% @language=JavaScript %>

<HTML>

<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">

<!-- #include file=helper_js.csp -->

</HTML>

<%


// This page is called from GetLicenseKeys.csp. Depending on the user's choice from

// the previous page:

//  - The license key will be added to the APS database.

//  - The license key will be removed from the APS database.

//  - The selected license key properties will be displayed.


//================================================


function AddLicenseKey (IStore, LicenseKey)

// This function adds a license key to the APS database.

// Parameters:

// IStore - The InfoStore object.

// LicenseKey - The license key you want to add to the APS database.

// Returns: None.

{

    try

    {

        var PluginMgr = IStore.PluginManager;

        // Get the LicenseKey plugin.

        var LicenseKeyPlugin = PluginMgr.PluginInfo("CrystalEnterprise.LicenseKey");

        // Create a new InfoObjects collection. This will be used to store the new object

        // that will be commmitted later.

        var NewInfoObjects  = IStore.NewInfoObjectCollection();

        // Add the result of the plugin. This will create a new InfoObject that

        // represents a LicenseKey.

        var NewLicenseKey = NewInfoObjects.Add (LicenseKeyPlugin);

        // Get the LicenseKey plugin interface.

        var CELicenseKey = NewLicenseKey.PluginInterface("");

        // Set the new license key.

        Response.Write("Adding: " + LicenseKey);

        CELicenseKey.LicenseKey = LicenseKey;

        // Commit the collection, in which the new license key is stored, to the APS.

        IStore.Commit (NewInfoObjects);

        

    }

    catch (e)

    {

        Response.Write ("<BR>Error ");

        return;

    };

};


function DeleteLicenseKey (IStore, LicenseKeyId)

// This function deletes the selected license key from the APS database.

// Parameters:

// IStore - The InfoStore object.

// LicenseKeyId - The InfoObject ID of the selected license key.

// Returns: None.

{

    try

    {

        // Query for the selected license key.

        var LicenseKeys = IStore.Query

        ("Select SI_ID, SI_LICENSEKEY From CI_SYSTEMOBJECTS Where SI_ID=" + LicenseKeyId);

        

        // Get the LicenseKey.

        var CELicenseKey = LicenseKeys.Item(1);

    

        // Delete the License Key by passing the LicenseKey's InfoObject ID.

        LicenseKeys.Delete(CELicenseKey);

    

        // Commit the information to the APS.

        IStore.Commit(LicenseKeys);

    }

    catch (e)

    {

        Response.Write ("<BR>Error ");

        return;

    };

};


function DisplayLicenseKey (IStore, LicenseKeyId)

// This function displays the selected license key's properties.

// Parameters:

// IStore - The InfoStore object.

// LicenseKeyId - The InfoObject ID of the selected license key.

// Returns: The table displaying the license key properties.

{

    try

    {

        // Query for the selected license key.

        var LicenseKeys = IStore.Query

        ("Select Top 1*    From CI_SYSTEMOBJECTS Where SI_ID=" + LicenseKeyId);

        

        // Get the LicenseKey plugin interface.

        var LicenseKey = LicenseKeys.Item(1).PluginInterface("");

        

        // Create a table to display the license key properties.

        var HTMLTable = "<Table BORDER=0 CELLPADDING=3 CELLSPACING=3>";


        HTMLTable = HTMLTable + "<TD><B>Product level:</B></TD>";

        HTMLTable = HTMLTable + "<TD>" + LicenseKey.ProductLevel+ "</TD>";

        HTMLTable = HTMLTable + "<TD><B>Product name:</B></TD>";

        HTMLTable = HTMLTable + "<TD>" + LicenseKey.ProductName + "</TD>";

        HTMLTable = HTMLTable + "<TD><B>Product version:</B></TD>";

        HTMLTable = HTMLTable + "<TD>" + LicenseKey.ProductVersion + "</TD></TR>";

        HTMLTable = HTMLTable + "<TR><TD><B>Expiry date:</B></TD>";

        HTMLTable = HTMLTable + "<TD>" + LicenseKey.ExpiryDate + "</TD>";

        HTMLTable = HTMLTable + "<TD><B>Language:</B></TD>";

        HTMLTable = HTMLTable + "<TD>" + LicenseKey.Language + "</TD></TR>";

        HTMLTable = HTMLTable + "<TR><TD><B>License count:</B></TD>";

        HTMLTable = HTMLTable + "<TD>" + LicenseKey.LicenseCount + "</TD>" ;

        HTMLTable = HTMLTable + "<TD><B>License type:</B></TD>";

        HTMLTable = HTMLTable + "<TD>" + LicenseKey.LicenseType + "</TD></TR>";


        HTMLTable = HTMLTable + "</Table>";

        Response.Write(HTMLTable);

    

    }

    catch (e)

    {

        Response.Write ("<BR>Error ");

        return;

    };

};


//================================================


function Main()

{

   //Retrieve the InfoStore object.

   var IStore = GetSession("IStore");


   // If the infostore object is not found then display an error message.

   if (IStore == null)

   {

      Response.Write ("Example failed: try logging on first!");

   };

    LicenseKeyId = Request.Form.Item("LicenseKeyId");


    if(Request.Form.Item("Add")=="Add")

    {

        var LicenseKey = Request.Form.Item("LicenseKey");

        AddLicenseKey (IStore, LicenseKey);

        Response.ReDirect("GetLicenseKeys.csp");

    };


    if (Request.Form.Item("Remove")=="Remove")

    {

        

        DeleteLicenseKey (IStore, LicenseKeyId);

        Response.ReDirect("GetLicenseKeys.csp");

    };    


    if(Request.Form.Item("Display")=="Display")

    {

        Response.Write ("<form action='GetLicenseKeys.csp' method=POST>");

        DisplayLicenseKey (IStore, LicenseKeyId);

        Response.Write ("<P><input type=submit name='Back' value='Back'>");

    };

  

    Response.Write ("</FORM>");

}


Main ();

%>




Crystal Decisions, Inc.
http://www.crystaldecisions.com
Support services:
http://support.crystaldecisions.com