01 package com.bigllc.retsiq.examples;
02
03 import java.io.FileOutputStream;
04 import java.io.IOException;
05 import java.net.MalformedURLException;
06
07 import com.bigllc.retsiq.simpleclient.DelimitedSearchResponseHandler;
08 import com.bigllc.retsiq.simpleclient.RETSClientException;
09 import com.bigllc.retsiq.simpleclient.RETSConnection;
10 import com.bigllc.retsiq.simpleclient.RETSUserSession;
11 import com.bigllc.retsiq.simpleclient.SearchResponseHandler;
12
13 /**
14 * Example to demonstrate how to search for records and output to
15 * a file in a comma delimited format.
16 *
17 * @author Marc G. Smith
18 */
19 public class SearchToCSV
20 {
21 public static void main(String[] args) throws MalformedURLException
22 {
23 String loginurl = "http://rets.server.com/rets/login";
24 String username = "username";
25 String password = "password";
26 String path = "/Agents/Agents";
27 String query = "(UIDOFFICE=OFFICE01)";
28 String outputFile = "/tmp/agents.csv";
29
30 // Create the connection class
31 RETSConnection connection = new RETSConnection(loginurl);
32 RETSUserSession session = null;
33 FileOutputStream out = null;
34
35 try {
36 // Authenticate the user and get session
37 session = connection.getSession(username, password);
38
39 // Create the output stream for the results
40 out = new FileOutputStream(outputFile);
41
42 // Create the output handler. This is a handler that
43 // writes the results out in a comma delimited format
44 SearchResponseHandler handler =
45 new DelimitedSearchResponseHandler(out);
46
47 // Execute the search
48 session.search(path, query, null, handler);
49 }
50 catch(IOException e) {
51 System.err.println(e.getMessage());
52 }
53 catch(RETSClientException e) {
54 System.err.println(e.getMessage());
55 }
56 finally {
57 if(session != null) {
58 try {
59 session.logout();
60 }
61 catch(RETSClientException e) {
62 System.err.println(e.getMessage());
63 }
64 }
65
66 if(out != null) {
67 try {
68 out.close();
69 }
70 catch(IOException e) {
71 System.err.println(e.getMessage());
72 }
73 }
74 }
75 }
76 }
|