Using the import servlet: Difference between revisions

From Open-Xchange
(New page: = An example for using the module to import or export contact data as CSV == Importing and exporting is done by the servlet ImportExport.java in the package com.openexchange.ajax. The pat...)
 
(No difference)

Revision as of 15:35, 19 March 2007

An example for using the module to import or export contact data as CSV =

Importing and exporting is done by the servlet ImportExport.java in the package com.openexchange.ajax. The path to this servlet is stored in servletmapping.properties, usually it is [serveraddress]/ajax/importexport.

Whether you do an import or an export is decided quite simply: If you send a PUT request, you're importing (because you are sending data), if you send a GET request, you're exporting (because you are requesting data). So no parameter is needed to indicate what you're doing. This has the additional advantage that you can use the same kind of URL to import and export (which also means: I only have to describe this only once ;-)). So, let's construct an URL:

  StringBuilder bob = new StringBuilder();
  bob.append("http://");
  bob.append(getHostName()); 

you probably know your hostname better than I do. ;-)

  bob.append("/ajax/importexport?session=");
  bob.append(getSessionId());  

how you get the session depends on whether use write another servlet or work from the GUI.

  addParam(bob, ImportExport.PARAMETER_FOLDERID, folderId ) ; 

folderID = the ID of the folder you want to export from or import into.

  addParam(bob, ImportExport.AJAX_TYPE, type);

type = the type of folder (Contact, Task, Appointment or whatever), as defined in com.openexchange.groupware.Types. Now, if you are stuck with another number (yeah, the OX design is a bit incongruent there...), you might want to take a look at com.openexchange.groupware.importexport.ModuleTypeTranslator, which was written to solve this problem.

  addParam(bob, ImportExport.PARAMETER_CONTENT_TYPE, format.getMimeType());

format is usually of type com.openexchange.groupware.importexport.Format - you can use a selfmade MIME-type if you're sure you have a converter for that.

  addParam(bob, ImportExport.PARAMETER_COLUMNS, ContactField.GIVEN_NAME.getNumber());
  addParam(bob, ImportExport.PARAMETER_COLUMNS, ContactField.EMAIL1.getNumber());

This parameter is optional. If you have a format that allows for a variable number of data fields to be exported or imported, this is the way to pass them to the servlet. It currently is used only for CSV Contacts. The parameter contains an array (as in the example, you can use this parameter more than one to create an array). Now you just need the numbers for the columns, right? For Contacts, the way to identify a field is to use the enum ContactField of the package com.openexchange.groupware.contact.helpers.

If you want to import data, you must add your data as InputStream to your request.

Now send this URL to the servlet.

See also: HTTP API