Export model to CSV

tomcat-thumbCSV (comma-separated values) file is a very common file format for saving textual data in table structure. Each line in the CSV file is presenting an entry, while fields in each record is speared by commas (or other separator). Data in CSV can be used with any spreadsheet program, or even for exchange of information between applications. In this article we will show you how you can make use of Visual Paradigm Open API to create a plugin which export the use case models in your project into a CSV file.

 

Export use case models into CSV file will have the following steps:

  1. Obtain the use case models
  2. Create StringBuffer to hold the model information
  3. Prompt user to specify the path for the CSV file
  4. Save CSV file to file system

Let’s start by obtain the use case models

Obtain the use case models

We first obtain the use case models form your opening project. To do this:

// Retrieve the project
IProject project = ApplicationManager.instance().getProjectManager().getProject();
// Retrieve all use case model elements into an array
IModelElement[] modelElements = project.toAllLevelModelElementArray(IModelElementFactory.MODEL_TYPE_USE_CASE);

Create StringBuffer to hold the model information

Once we obtained the use case models, we can then translate the model information into the StringBuffer.

// Create a StringBuffer to store the output
StringBuffer sb = new StringBuffer();// Insert header row in our CSV
sb.append("use case id,name,description");
// Insert a new line
sb.append("\n");
// walk through its containing elements one by one
for (int i = 0; i < modelElements.length; i++) {
  IUseCase usecase = (IUseCase) modelElements[i];
  // Insert the use case's user ID and separator
  sb.append(usecase.getUserID());
  sb.append(",");
  // Insert the use case's name and separator
  sb.append(usecase.getName());
  sb.append(",");
  // Insert the use case's description and a new line
  sb.append(usecase.getDescription());
  sb.append("\n");
}

Prompt user to specify the path for the CSV file

Next we create a file chooser to prompt user specify the output path of the CSV file.

// Create File Chooser to let user specify the output path
JFileChooser fileChooser = ApplicationManager.instance().getViewManager().createJFileChooser();fileChooser.setDialogTitle("Export CSV");
// Create a File Filter for CSV file type and set it as default
FileFilter filter = new FileNameExtensionFilter("CSV File", "csv");fileChooser.addChoosableFileFilter(filter);fileChooser.setFileFilter(filter);
// Show up the File Chooser dialog
int returnVal = fileChooser.showSaveDialog(null);

Save CSV file to file system

Finally we can save the StringBuffer to user specified output file.

// If provided a file and press Save button
if (returnVal == fileChooser.APPROVE_OPTION) {
  // Obtain the file path user selected
  File outputFile = fileChooser.getSelectedFile();
  // Add csv extension if user haven't specify it
  if (!outputFile.getAbsolutePath().toLowerCase().endsWith(".csv")) {
    outputFile = new File(outputFile.getAbsolutePath() + ".csv");
  }
  try {
    // Write the StringBuffer to file
    FileOutputStream fout = new FileOutputStream(outputFile);
    fout.write(sb.toString().getBytes());
    fout.close();
  } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return;
  } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return;
  }
  // Print a message in Message Pane to tell user export was completed.
  ApplicationManager.instance().getViewManager().showMessage("CSV exported to " + outputFile.getAbsolutePath());
}

Sample Plugin

The sample plugin attached in this article demonstrate how to export the use case models into a CSV file. After you deploy the plugin into Visual Paradigm you can then click the plugin button in the application toolbar to trigger it.

Trigger sample plugin

Trigger sample plugin

Download Sample Plugin

You can click this link to download the sample plugin.

Related Know-how

Related Links

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply