0



06Feb2011

A javascript interface to CatalogManager

FatPhone installer requires  creation of elements, as well as assets. But XMLPost does not create those elements, at least for Templates, so the installer has to create those elements calling directly CatalogManager .

I wrote a Javascript class that does just this:  interfaces with CatalogManager for creating and querying rows. Since this class is useful by itself, I am writing this  post as documentation for general usage.

Code is available in the FatPhone github public repository:  the file is named  catalogmanager.js.  It is released under the GPL. You can download and use in your project freely. There is also a test html page to test and call it interactively.

Now, 2 important warnings:

  • You cannot use it locally. You have to copy the code in the static area of your FatWire installation site, and invoke the javascript with a browser. Security restrictions prevent executing remote http calls from locally loaded files.
  • It is not 100% complete: does not offer access to every feature of CatalogManager.  Nonetheless, in the current state,  it is useful to create, update and delete rows, perform simple queries and  check if a row exists.

It can also already perform generic queries, but the returned result  is just the HTML of the response. I should parse the HTML and return the answer in json format to be more useful.

To summarize the available functions:

  1. Create a new Catalog Manager object.
  2. Login into content server.
  3. Select a row (returning the result in)
  4. Check for the existence of a row
  5. Add a row
  6. Edit a row
  7. Delete a row

Creating a new Catalog Manager object

var cm = new CatalogManager(url)

where url is the Fatwire ContentServer base url (tipically http://www.yoursite.com/cs/),

Login into FatWire

cm.login(username, password, loginFunction, errorFunction)

where username and password are your FatWire credentials.  If login is ok, loginFunction is called, otherwise errorFunction is called.

Select a Row

Once you are logged on , you can execute calls. The simplest call is:

cm.selectRow(tablename, where, what, callbackFunction)

this will perform a query on the given tablename, specifying a where condition  and what (fields) to search for. The callbackFunction will be called with the result.

For example,

 cm.selectRow(Page, "id", "name='Home'", alert)

will show in an alert box the result of searching the Page table for records with name Home, and will return his id. A simple regular expression can extract the id from the returned result. I do exactly this in the installer, you can check the source code of install.js to see an example.

In an ideal world,  I should parse the returned html in a structured javascript object (an array of maps, for example). If you want to improve the code before I will do it... your help is appreciated.

Check for existence of a row

cm.checkExist(tablename, selwhere, found, notFound)

This function perform a limited parsing of the result to see if the row in tablename, specified in the selwhere, was found or not. The appropriate callback function is called according to the result.

Add a row

cm.addRow(tablename, data, nextStep)

tablename is a string, the name of a row, while data must be a map. For example, this call create a new Element in the ElementCatalog (assuming that elementName, fileBody, fileName and fileFolder are defined, of course):

cm.addRow("ElementCatalog", {
 "elementname" : elementName,
 "url" : fileBody,
 "url_file" : fileName,
 "url_folder" : fileFolder
 }, nextStep)

The function nextStep is called with a status object  passed as the first parameter. The status is a map that has at least 3 keys:

  • ok is a boolean telling if the result is successful or no not
  • answer is the unparsed answer
  • parsed is a boolean telling if the answer was parsed. If true there are also the following fields defined (parsed from the CatalogManager returned status)
    • result
    • reason
    • err
    • command

Edit a row

cm.editRow(tablename, data, nextStep)

tablename is a string, the name of a row, while data must be a map and nextStep is a function called with the status of the result.

Both data and the return status have the same format as in addRow, except that the data must specify the id field to make the call work.

Delete a row

cm.deleteRow(tablename, data, nextStep)

tablename is a string, the name of a row, while data must be a map and nextStep is a function called with the status of the result.

Both data and the return status have the same format as in addRow, except that the data must specify an id field to make the call work successfully. Actually, in the delete the id is the only required field, any other field in this call is just ignored

Posted by msciab
06Feb2011