Wednesday, October 3, 2012

BOE: Using Query Builder API to get User Info

As I've been learning Grails, I've also been reading up on the BOE API. The Business Objects query builder returns results in HTML format, which isn't friendly data. So I've been using Grails to run BOE queries and returning the results as .csv files.

The first query I ran and output wasn't too complicated. The second query was a bit more complicated.
These are some notes on what I did.

Every quarter or so, we need to verify that the BOE users are still active and no one is missing. The query builder allows you to pull the users with this query:

SELECT SI_NAME, SI_USERFULLNAME,SI_EMAIL_ADDRESS FROM CI_SYSTEMOBJECTS WHERE SI_KIND = 'User'

Which results in an HTML page like this for every user.


I wanted to pull this info into a .csv file.  (Email, Userfullname, Name). With a user on each line.

I have some code in Grails that connects to BOE using a token to login. Then I have a function that takes my query above as a string. Then creates and returns a IInfoObjects using the query.

public IInfoObjects getUserReport(String myquery, String token) {
        IInfoStore iStore = getInfoStore(token);  //Creates session using my login token
        IInfoObjects reports = iStore.query(myquery);       
       
        return reports;
}


In my controller, I call the getUserReport function to get the object with my query data.

IInfoObjects reportinfo = BoeService.getUserReport(query,tok);
        recordcount = reportinfo.getResultSize();    //grab the number of records    


I have an array of IInfoObject (not to be confused with IInfoObjects) for each row of data from the query.


        IInfoObject[] report = new IInfoObject[recordcount];
       
        for ( x = 0; x < recordcount; x = x+1 ){
       
            report[x] = (IInfoObject) reportinfo.get(x);   //Set new object with a row of data
           
        }     
       
//Strings to hold data for each column
        String user;
        String email;
        String fullname;
        String line;         

//Create .csv file to export data to      
        File uf = new File(u.exportfile)       //original file
        String newfile=null
        newfile = u.exportfile
               
        BufferedWriter out = new BufferedWriter(new FileWriter(newfile))
                  
//Each row in object gets written to a string
for ( y = 0; y < recordcount; y = y+1 )    {
       
   user = report[y].properties().getProperty(CePropertyID.SI_NAME).getValue();
   email = report[y].properties().getProperty(CePropertyID.SI_EMAIL_ADDRESS).getValue();
   fullname = report[y].properties().getProperty(CePropertyID.SI_USERFULLNAME).getValue();
         
          //Set NULL variables to something else
          if ( email == '') { email = 'NoEmail'}
          if ( fullname == '') { fullname = 'NoFullName'}
          if ( user == '') { user = 'NoUser'}
         
//Write data to the .csv file
          line = email + ',' + fullname + ',' + user + '\n'
          out.write(line);
}
      
out.close()  //close writer

No comments:

Post a Comment