Sending Files to the ReportCaster Library Using the ReportCaster API

by Gerry Snyder

I’m often asked if there is a way to send files to the ReportCaster Report Library using the ReportCaster API. The answer is yes. Yes that is, if the Distribution Server can access your files either through the file system or FTP. If your files are accessible to the Distribution Server, you can use the ReportCaster API to create a schedule that will distribute your files to the Report Library.

An easy way to do this is to modify one of the sample ReportCaster API Java programs included with ReportCaster and found in the drive:\ibi\ReportCaster76\samples directory. The samples directory contains programs that demonstrate the capabilities of the ReportCaster API. These programs are documented in the SampleInstructions.pdf file located in the tools subdirectory.

The ReportCaster sample Java programs obtain information from the samples.properties file also located in the tools subdirectory. The file contains the host and port number of the Distribution Server and user ID and password that will submit the request to the ReportCater API and be the owner of the schedule that is created. The user ID must be a ReportCaster or Managed Reporting user ID with “schedule” privilege. In the example provided in this article the user ID ‘admin’ with no password is used. 

You can start with the program S01_Add_Library.java. This program creates a schedule which executes a server procedure and distributes the output to the Library. Here is a look at a portion of this program’s code:

String scheduleDescription = arg[0];
String fexFileName = arg[1];
String execId = arg[2];
String execPassword = arg[3];

// Create ScheduleManager
ScheduleManager manager =  Util.createCasterConnection().getScheduleManager();

//create object schedule with default values from config file
Schedule schedule = manager.createScheduleInstanceDefault();

//set description
schedule.setDescription(scheduleDescription);

//set distribution to Library
Distribution distribution = new StorageLibrary();
schedule.setDistribution(distribution);   

//create task
TaskWFServerProcedure task = new TaskWFServerProcedure();
task.setProcedureName(fexFileName);
task.setExecId(execId);
task.setExecPassword(execPassword);
task.setSendFormat(TaskWFServerProcedure.PDF);
schedule.setTaskList(new Task[]{task});
//schedule.setCompressedReport(true);
//subscribe schedule
manager.addSchedule(schedule);

This portion of the code accepts four arguments consisting of the schedule description, the name of the server procedure to be executed, an execution ID and a password. Using the values in the samples.properties file a ScheduleManager object is created that writes the schedule’s data to the ReportCaster repository. A schedule is created with the description provided, the server procedure as the task, the library as the distribution method and default values for all other schedule fields. You can change this program to distribute a file simply by changing the task.

First, change the argument definition section at the beginning of the code sample that defines the arguments passed to this program. A file is being distributed instead of a report, so change the name of the argument fexFileName to localFileName. The value that will be passed into the localFileName argument must be the full path and filename of a file accessible by the Distribution Server.

An execution ID and password are not needed to distribute a file so remove these lines:
String execId = arg[2];
String execPassword = arg[3];


The argument definition section at the beginning of the code sample should now be as follows:
String scheduleDescription = arg[0];
String localFileName = arg[1];

Next in the create task section, change the task type from a server procedure to a file. 
Change the first line in this section from specifying TaskWFServerProcedure to specify TaskFile. Then on the next line set the filename to be distributed to the variable containing the file location information by changing setProcedureName(fexFilename) to setFileName(localFilename).  The last change in this section is to remove the lines pertaining to execution ID, password, send format and the two commented lines that start with //.   The create task section should now be:

//create task
TaskFile task = new TaskFile();
task.setFileName(localFileName);
schedule.setTaskList(new Task[]{task});
manager.addSchedule(schedule);

Following the naming conventions of the sample Java files, save the program as S01_Add_File_to_Library.java. Modify the SamplesComp.bat file in the tools directory to set the values of JAVA_RUN and CLASSPATH variables for the machine you will run this program on. Run the SamplesComp.bat file to compile the S01_Add_files_to_Library program.

Finally, modify Schedule.bat to run the S01_Add_files_to_Library program, again following the existing naming conventions. For example, if your Distribution Server is on Windows and you have set up a shared directory containing a word document named weekly_status.doc, then add to Schedule.bat the following line on a single line: 

%JAVA_RUN% S01_Add_File_to_Library Weekly_Status_File_to_Library
\\your_machine _name\shared_drive\weekly_status.doc admin

Note that the user IS is ‘admin’ but you should use your own ReportCaster user ID with Schedule privilege.

This will create a run-once schedule named Weekly_Status_File_to_Library.  By default this schedule is disabled. So we need to add a second line to Schedule.bat to run the schedule.

%JAVA_RUN% S21_Run admin Weekly_Status_File_to_Library

The S21_Run program accepts the schedule description as an argument and submits a request to ReportCaster to run the specified schedule.  The Weekly_Status_File_to_Library schedule will execute and distribute your file to the Report Library under the default category named default. Also by default the file stored in the Report Library will never expire. Each time you run the schedule, a new version of you file will be sent to the Library.

You may want to assign a category other than default, which you do by adding a line to the set distribution to Library section that calls the setCategory method of the StorageLibrary class. The highlighted code below shows the addition to specify that the file will be assigned to the ‘My Files’ category. 

            //set distribution to Library
Distribution distribution = new StorageLibrary();
((StorageLibrary)distribution).setCategory("My Files");
schedule.setDistribution(distribution);

Similarly you may choose to set an expiration value for the file you are storing in the Report Library, enable the schedule when it is created and even change the task type from a file task to an FTP task if the Distribution Server cannot access your files through the file system. By referring to the ReportCaster Javadoc and the ReportCaster API sample programs, you can find the methods to set the values for a schedule. 

The ReportCaster Javadoc is accessible by selecting the help option within the ReportCaster user interfaces and then select ReportCaster API Help from the tree on the left side of the page. You can also access the ReportCaster Javadoc from a browser by entering the address http://host:port/rcaster/doc/index.html  and changing ‘host:port’ and ‘rcaster’ URL context to values to your site-specific values to access the application server where ReportCaster is deployed.

Hopefully this article not only answers the question about storing files in the Report Library but also introduces you to the sample programs that demonstrate the capabilities of the ReportCaster API.

previous next