MetadataSaveAndLoad.java
001 package com.bigllc.retsiq.examples;
002 
003 import java.io.FileInputStream;
004 import java.io.FileOutputStream;
005 import java.io.IOException;
006 import java.net.MalformedURLException;
007 import java.util.List;
008 
009 import com.bigllc.retsiq.api.RETSConstants.MetadataFormat;
010 import com.bigllc.retsiq.api.RETSConstants.MetadataType;
011 import com.bigllc.retsiq.api.RETSConstants.Version;
012 import com.bigllc.retsiq.metadata.MetadataSystem;
013 import com.bigllc.retsiq.metadata.io.MetadataTransformer;
014 import com.bigllc.retsiq.metadata.io.MetadataTransformerFactory;
015 import com.bigllc.retsiq.metadata.io.XmlMetadataSource;
016 import com.bigllc.retsiq.simpleclient.RETSClientException;
017 import com.bigllc.retsiq.simpleclient.RETSConnection;
018 import com.bigllc.retsiq.simpleclient.RETSUserSession;
019 
020 /**
021  * Example to demonstrate saving and loading metadata to and from
022  * a file. 
023  
024  * This example will require greater memory allocation to hold the 
025  * metadata. This can be set by using the VM arguments -Xms128m and
026  * -Xmx256m.
027  
028  @author Marc G. Smith
029  */
030 public class MetadataSaveAndLoad 
031 {
032   public static void main(String[] argsthrows MalformedURLException 
033   {
034     String loginurl   = "http://rets.server.com/rets/login";
035     String username   = "username";
036     String password   = "password";
037     String metadataFile = "/tmp/metadata.xml";
038     
039     // Create the connection class
040     RETSConnection connection = new RETSConnection(loginurl);
041     RETSUserSession session = null;
042     FileOutputStream out = null;
043     FileInputStream in = null;
044     
045     try {
046       // Authenticate the user and get session
047       session = connection.getSession(username, password);
048 
049       
050       // Get the metadata from the server
051       MetadataSystem system = session.getMetadata();
052 
053       // Create the output stream for the metadata
054       out = new FileOutputStream(metadataFile);
055 
056       // Get a standard xml transformer
057       MetadataTransformer transformer = 
058         MetadataTransformerFactory.newTransformer(
059           MetadataFormat.STANDARD_XML, 
060           Version.toEnum(session.getRetsVersion()),
061           true);
062 
063       System.out.println("[WRITING METADATA]");
064 
065       // Write the complete metadata to file 
066       transformer.transform(
067         system, MetadataType.SYSTEM, 
068         new String[] {"*"}, out);
069       
070       out.close();
071       out = null;
072       
073       System.out.println("[READING METADATA]");
074       
075       // To read the metadata from a file create
076       // an input file stream
077       in = new FileInputStream(metadataFile);
078       
079       // Create a metadata source 
080       XmlMetadataSource source = new XmlMetadataSource(in);
081       
082       // Build the metadata. This returns a list of systems
083       // This list should contain 1 item.
084       List<MetadataSystem> systems = source.build();
085       system = systems.get(0);
086       
087       System.out.println(
088         "SYSTEM: " + system.getName() 
089         " - " + system.getDescription());
090     }
091     catch(IOException e) {
092       System.err.println(e.getMessage());
093     }
094     catch(RETSClientException e) {
095       System.err.println(e.getMessage());
096     }
097     catch(Exception e) {
098       System.err.println(e.getMessage());
099     }
100     finally {
101       if(session != null) { 
102         try {
103           session.logout()
104         
105         catch(RETSClientException e) {
106           System.err.println(e.getMessage());
107         }
108       }
109       
110       if(out != null) { 
111         try {
112           out.close()
113         
114         catch(IOException e) {
115           System.err.println(e.getMessage());
116         }
117       }
118 
119       if(in != null) { 
120         try {
121           in.close()
122         
123         catch(IOException e) {
124           System.err.println(e.getMessage());
125         }
126       }
127       
128     }
129   }
130 }