- Main Page
- Data Structures
- Files

- File List
- Globals

# samples/jsrun_params.c

Go to the documentation of this file.

    00001 /*
    00002  * Copyright (C) 2007 JasperSoft http://www.jaspersoft.com
    00003  *
    00004  * This program is free software; you can redistribute it and/or modify
    00005  * it under the terms of the GNU General Public License as published by
    00006  * the Free Software Foundation; either version 2 of the License, or
    00007  * (at your option) any later version.
    00008  *
    00009  * This program is distributed WITHOUT ANY WARRANTY; and without the
    00010  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00011  * See the GNU General Public License for more details.
    00012  *
    00013  * You should have received a copy of the GNU General Public License
    00014  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
    00015  * or write to:
    00016  *
    00017  * Free Software Foundation, Inc.,
    00018  * 59 Temple Place - Suite 330,
    00019  * Boston, MA  USA  02111-1307
    00020  */
    00021
    00031 #include <jasperserver.h>
    00032 #include <time.h>
    00033
    00034 const char server[] = "http://127.0.0.1:8080/jasperserver/services/repository";
    00035
    00036 #define MAX_INPUT_STRING 512
    00037
    00038
    00039
    00040
    00047 jasperserver_server_t *get_connection()
    00048 {
    00049       jasperserver_server_t *jasperServer = NULL;
    00050       // Create a new structure of type jasperserver_server_t to save the
    00051       // server url and credentials.
    00052       jasperServer = jasperserver_server_new();
    00053       jasperserver_string_cset(jasperServer->url, server);
    00054       jasperserver_string_cset(jasperServer->username, "tomcat");
    00055       jasperserver_string_cset(jasperServer->password, "tomcat");
    00056
    00057       return jasperServer;
    00058 }
    00059
    00068 void print_query_result(jasperserver_resource_descriptor_t *inputcontrol, char *datasourceUri)
    00069 {
    00070      jasperserver_server_t *server = NULL;
    00071      jasperserver_request_t *request = NULL;
    00072      jasperserver_argument_t *arg = NULL;
    00073      jasperserver_resource_descriptor_t *data = NULL;
    00074      jasperserver_resource_property_t *listOfRecords = NULL;
    00075      jasperserver_resource_property_t *record = NULL;
    00076      jasperserver_operation_result_t *result = NULL;
    00077
    00078      jasperserver_resource_descriptor_t *siblingNode = NULL;
    00079
    00080
    00081      request = jasperserver_request_new();
    00082
    00083      // Temporarly remove sibilng node...
    00084      siblingNode = inputcontrol->next;
    00085      inputcontrol->next = NULL;
    00086
    00087      arg = jasperserver_argument_new();
    00088      jasperserver_string_cset(arg->name, JS_ARG_IC_GET_QUERY_DATA );
    00089      if (datasourceUri != NULL)
    00090      {
    00091          jasperserver_string_cset(arg->value, datasourceUri );
    00092      }
    00093
    00094      jasperserver_list_append((jasperserver_list_t **)&request->arguments, (jasperserver_list_t *)arg);
    00095
    00096      request->resource = inputcontrol;
    00097
    00098      printf("\n");
    00099
    00100      server = get_connection();
    00101
    00102      result = jasperserver_ws_get(server, request, NULL);
    00103
    00104      if (result && result->returnCode == 0)
    00105      {
    00106           data = result->resources;
    00107           listOfRecords = jasperserver_get_property(data, JS_PROP_QUERY_DATA);
    00108
    00109           for (record =listOfRecords->properties; record; record = record->next)
    00110           {
    00111               jasperserver_resource_property_t *column = NULL;
    00112               printf("[%s]", JS_UTFSTR( record->value ));
    00113               // Print the rest of columns....
    00114               for (column = record->properties; column; column = column->next)
    00115               {
    00116                   printf("\t%s", JS_UTFSTR( record->value ));
    00117               }
    00118               printf("\n");
    00119           }
    00120      }
    00121
    00122      request->resource = NULL;
    00123      inputcontrol->next = siblingNode; //reset the sibling node...
    00124      jasperserver_request_free(request);
    00125      jasperserver_operation_result_free(result);
    00126      jasperserver_server_free(server);
    00127 }
    00128
    00143 int getDataType(const char *dataTypeResourceUri)
    00144 {
    00145      int dataType = -1;
    00146      jasperserver_server_t *server = NULL;
    00147      jasperserver_request_t *request = NULL;
    00148      jasperserver_resource_descriptor_t *res = NULL;
    00149      jasperserver_operation_result_t *result = NULL;
    00150
    00151      request = jasperserver_request_new();
    00152      res = jasperserver_resource_descriptor_new();
    00153      jasperserver_string_cset(res->uriString, dataTypeResourceUri);
    00154      request->resource = res;
    00155
    00156      server = get_connection();
    00157
    00158      result = jasperserver_ws_get(server, request, NULL);
    00159
    00160      if (result && result->returnCode == 0)
    00161      {
    00162           jasperserver_resource_descriptor_t *data = NULL;
    00163
    00164           data = result->resources;
    00165           if (data)
    00166           {
    00167              dataType = atoi( JS_CSTR( jasperserver_get_property_value(data, JS_PROP_DATATYPE_TYPE) ) );
    00168           }
    00169      }
    00170
    00171      jasperserver_request_free(request);
    00172      jasperserver_operation_result_free(result);
    00173      jasperserver_server_free(server);
    00174
    00175      return dataType;
    00176 }
    00177
    00190 jasperserver_parameter_t *acquire_value_for_input_control( jasperserver_resource_descriptor_t *inputcontrol, char *dsUri )
    00191 {
    00192       jasperserver_string_t *icType = NULL;
    00193       jasperserver_parameter_t *parameter = NULL;
    00194       int icTypeID = -1;
    00195
    00196
    00197
    00198       // Look for the type of inputcontrol....
    00199       icType = jasperserver_get_property_value(inputcontrol, JS_PROP_INPUTCONTROL_TYPE);
    00200       if (icType == NULL) return NULL;
    00201
    00202       parameter =  jasperserver_parameter_new();
    00203       jasperserver_string_set( parameter->name, JS_UTFSTR(inputcontrol->name));
    00204
    00205       icTypeID = atoi( JS_CSTR(icType) );
    00206
    00207       printf("--------------------------------------:\n%s (%s)\n", JS_UTFSTR( inputcontrol->label), JS_UTFSTR( inputcontrol->name) );
    00208
    00209       // Rendering of the control. You can write your own functions to do that.
    00210       switch (icTypeID)
    00211       {
    00212          case JS_IC_TYPE_BOOLEAN:
    00213          {
    00214              char test[MAX_INPUT_STRING] = "";
    00215              printf("Value [Y/N]: ");
    00216              fgets(test, MAX_INPUT_STRING, stdin);
    00217              jasperserver_string_cset( parameter->value, (strcmp(test, "Y") == 0) ? "true" : "false" );
    00218              return parameter;
    00219          }
    00220          case JS_IC_TYPE_SINGLE_VALUE:
    00221          {
    00222              char test[MAX_INPUT_STRING] = "";
    00223              jasperserver_resource_descriptor_t *resDataType = NULL;
    00224              jasperserver_resource_property_t *prop = NULL;
    00225              int intDataType = JS_DT_TYPE_TEXT; // Set a default...
    00226              // Check what type of data type...
    00227              resDataType = inputcontrol->children;
    00228              prop = jasperserver_get_property(resDataType, JS_PROP_REFERENCE_URI);
    00229              if (prop)
    00230              {
    00231                 // the type is linked to another resource...
    00232                 intDataType = getDataType( JS_CSTR(prop->value)  );
    00233              }
    00234              else
    00235              {
    00236                  intDataType = atoi( JS_CSTR(jasperserver_get_property(resDataType, JS_PROP_DATATYPE_TYPE )->value) );
    00237              }
    00238
    00239              switch (intDataType)
    00240              {
    00241                     case JS_DT_TYPE_TEXT:
    00242                         printf("Value (String): ");
    00243                         fgets(test, MAX_INPUT_STRING, stdin);
    00244                         jasperserver_string_cset( parameter->value, test );
    00245                         break;
    00246                     case JS_DT_TYPE_NUMBER:
    00247                         printf("Value (number): ");
    00248                         fgets(test, MAX_INPUT_STRING, stdin);
    00249                         jasperserver_string_cset( parameter->value, test );
    00250                         break;
    00251                     case JS_DT_TYPE_DATE:
    00252                     {
    00253                         // A date must be represented as a number holding the number of MILLISECONDS from Jan 1, 1970.
    00254                         // (java standard).
    00255
    00256                         int year = 0;
    00257                         int month = 0;
    00258                         int day = 0;
    00259                         struct tm time_struct;
    00260                         time_t ttime = 0;
    00261
    00262                         printf("Value (date in the format mm-dd-yyyy): ");
    00263                         fgets(test, MAX_INPUT_STRING, stdin);
    00264                         sscanf(test, "%d-%d-%d", &month, &day, &year);
    00265                         time_struct.tm_year=year-1900;
    00266                         time_struct.tm_mon=month-1;
    00267                         time_struct.tm_mday=day;
    00268                         time_struct.tm_sec=0;
    00269                         time_struct.tm_min=0;
    00270                         time_struct.tm_hour=0;
    00271                         time_struct.tm_isdst=-1;
    00272
    00273                         ttime = mktime(&time_struct);
    00274                         if (ttime != -1)
    00275                         {
    00276                          printf("Date: %d-%d-%d\n",month,day,year);
    00277                         }
    00278                         else
    00279                         {
    00280                             printf("Date not valid, using current time.\n");
    00281                             ttime = time(NULL);
    00282                         }
    00283                         jasperserver_string_format( parameter->value, "%ld000", ttime );
    00284                         fflush(stdout);
    00285                         break;
    00286                     }
    00287              }
    00288
    00289              return parameter;
    00290          }
    00291          case JS_IC_TYPE_SINGLE_SELECT_LIST_OF_VALUES:
    00292          {
    00293              char test[MAX_INPUT_STRING] = "";
    00294
    00295              // get the list of values (it *SHOULD* be the first child...)
    00296              jasperserver_resource_descriptor_t *lov = NULL;
    00297              jasperserver_resource_property_t *lovList = NULL;
    00298              jasperserver_resource_property_t *lovItem = NULL;
    00299
    00300              lov = inputcontrol->children;
    00301
    00302              lovList = jasperserver_get_property( lov, JS_PROP_LOV  );
    00303              printf("\n");
    00304
    00305              // show all the items...
    00306              for ( lovItem = lovList->properties; lovItem; lovItem = lovItem->next)
    00307                  {
    00308                   printf("[%s] %s\n", JS_UTFSTR( lovItem->name), JS_UTFSTR( lovItem->value) );
    00309              }
    00310
    00311              printf("\nSelect a value from the list: ");
    00312
    00313              fgets(test, MAX_INPUT_STRING, stdin);
    00314              jasperserver_string_cset( parameter->value, test );
    00315              return parameter;
    00316          }
    00317          case JS_IC_TYPE_SINGLE_SELECT_QUERY:
    00318          {
    00319                char test[MAX_INPUT_STRING] = "";
    00320
    00321                print_query_result( inputcontrol,dsUri);
    00322                printf("\nSelect a value from the list: ");
    00323
    00324                fgets(test, MAX_INPUT_STRING, stdin);
    00325                jasperserver_string_cset( parameter->value, test );
    00326
    00327                return parameter;
    00328          }
    00329          default:
    00330          {
    00331              printf("Unhandled input control type (type %d)...\n", icTypeID );
    00332          }
    00333       }
    00334
    00335       // No parameter to supply...
    00336       jasperserver_parameter_free( parameter );
    00337       return NULL;
    00338 }
    00339
    00340
    00341 int main(int argc, char **argv)
    00342 {
    00343    jasperserver_server_t *jasperServer = NULL;
    00344
    00345    // Uri of a sample with a lot of input controls...
    00346    char *reportUnitUri = "/reports/samples/SalesByMonth";
    00347
    00348    // File to store the final result...
    00349    char *outfile = "test.pdf";
    00350
    00351    char *datasourceUri = NULL;
    00352    jasperserver_request_t *request = NULL;
    00353    jasperserver_argument_t *argPdfExport = NULL;
    00354    jasperserver_resource_descriptor_t *report = NULL;
    00355    jasperserver_resource_descriptor_t *inputcontrol = NULL;
    00356    jasperserver_resource_descriptor_t *resource = NULL;
    00357    jasperserver_operation_result_t *operationResult = NULL;
    00358
    00359
    00360    // Fill the structure to connect to the server
    00361    jasperServer = get_connection();
    00362
    00363    // Create the request to get informations about the report...
    00364    request = jasperserver_request_new();
    00365
    00366    // Create the resource to get...
    00367    report = jasperserver_resource_descriptor_new();
    00368    jasperserver_string_cset(report->uriString, reportUnitUri);
    00369
    00370    // Add the resource to the request
    00371    request->resource = report;
    00372
    00373    // Get the whole report resource...
    00374    operationResult = jasperserver_ws_get( jasperServer, request, NULL);
    00375
    00376    if (!operationResult || operationResult->returnCode != 0)
    00377    {
    00378        printf("Error getting report information\n");
    00379        exit(0);
    00380    }
    00381
    00382    jasperserver_resource_descriptor_free(report);
    00383    report = NULL;
    00384
    00385    if (!operationResult->resources)
    00386    {
    00387        printf("Resource not found on this server!\n");
    00388        exit(0);
    00389    }
    00390
    00391    report = operationResult->resources;
    00392
    00393    // Void and free this operations result.
    00394    operationResult->resources = NULL; // This avoids to free the memory used for the resource...
    00395    jasperserver_operation_result_free(operationResult);
    00396    operationResult = NULL;
    00397
    00398    // Void and free this request
    00399    request->resource = NULL; // This avoids to free the memory used for the resource...
    00400    jasperserver_request_free(request);
    00401
    00402
    00403    // Find the datasource uri (used only for query based input control that does not have
    00404    // a defined datasource to use...
    00405    for ( resource = report->children; resource; resource = resource->next)
    00406    {
    00407        if ( !strcmp( JS_UTFSTR(resource->wsType), JS_TYPE_DATASOURCE) )
    00408        {
    00409             jasperserver_resource_property_t *prop = NULL;
    00410             prop = jasperserver_get_property(resource, JS_PROP_REFERENCE_URI );
    00411             if (prop && JS_NOTNULL(prop->value) )
    00412             {
    00413              datasourceUri = (char *)JS_CSTR( prop->value);
    00414              break;
    00415             }
    00416        }
    00417        else if (!strcmp( JS_UTFSTR(resource->wsType), JS_TYPE_DATASOURCE_JDBC) ||
    00418                 !strcmp( JS_UTFSTR(resource->wsType), JS_TYPE_DATASOURCE_JNDI) ||
    00419                 !strcmp( JS_UTFSTR(resource->wsType), JS_TYPE_DATASOURCE_BEAN))
    00420        {
    00421                 datasourceUri = (char *)JS_CSTR(resource->uriString);
    00422                 break;
    00423        }
    00424    }
    00425
    00426    printf("Report datasource URI %s\n", datasourceUri);
    00427    fflush(stdout);
    00428
    00429    // Now we need to present to the user the input controls....
    00430    // and fill the request for the report execution...
    00431
    00432    for ( inputcontrol = report->children; inputcontrol; inputcontrol = inputcontrol->next)
    00433    {
    00434          if ( !strcmp( JS_UTFSTR(inputcontrol->wsType), JS_TYPE_INPUT_CONTROL) )
    00435          {
    00436               jasperserver_parameter_t *parameter = NULL;
    00437               // Inputcontrol found!
    00438               // Show the input control and get the parameter...
    00439               parameter = acquire_value_for_input_control( inputcontrol, datasourceUri );
    00440               if (parameter != NULL)
    00441               {
    00442                  parameter->next = NULL;
    00443                  jasperserver_list_append((jasperserver_list_t **)&report->parameters, (jasperserver_list_t *)parameter);
    00444               }
    00445          }
    00446    }
    00447
    00448    printf("--------------------------------------\n");
    00449
    00450    // We have collected all the information to execute the report...
    00451    // Create a new structure of type jasperserver_request_t to store
    00452    // the request information like the resource to list
    00453    request = jasperserver_request_new();
    00454
    00455    // Set PDF as export format...
    00456    argPdfExport = jasperserver_argument_new();
    00457    jasperserver_string_cset(argPdfExport->name, JS_ARG_RUN_OUTPUT_FORMAT);
    00458    jasperserver_string_cset(argPdfExport->value, JS_ARG_RUN_OUTPUT_FORMAT_PDF);
    00459
    00460    request->resource = report;
    00461    jasperserver_list_append((jasperserver_list_t **)&request->arguments, (jasperserver_list_t *)argPdfExport);
    00462
    00463    // Call the web service
    00464    operationResult = jasperserver_ws_runReport(jasperServer, request, outfile);
    00465
    00466    if (operationResult && operationResult->returnCode != 0)
    00467    {
    00468        printf("Error filling the report: %d (%s)", operationResult->returnCode, JS_UTFSTR(operationResult->returnMessage) );
    00469    }
    00470    else
    00471    {
    00472        printf("test.pdf succesfully created.\n\n");
    00473    }
    00474
    00475    // Free the unused resources
    00476    jasperserver_server_free( jasperServer );
    00477    jasperserver_request_free( request );
    00478    jasperserver_operation_result_free( operationResult );
    00479
    00480    return 0;
    00481 }