12.07.2015 Views

Database.com Metadata API Developer's Guide - Salesforce.com

Database.com Metadata API Developer's Guide - Salesforce.com

Database.com Metadata API Developer's Guide - Salesforce.com

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

File-Based Callsretrieve()private void run() throws ServiceException, Exception {if (login()) {getUserInput("SUCCESSFUL LOGIN! Hit the enter key to continue.");retrieveZip();}}private void retrieveZip() throws RemoteException, Exception{RetrieveRequest retrieveRequest = new RetrieveRequest();retrieveRequest.setApiVersion(<strong>API</strong>_VERSION);setUnpackaged(retrieveRequest);}/**AsyncResult asyncResult = metadatabinding.retrieve(retrieveRequest);// Wait for the retrieve to <strong>com</strong>pleteint poll = 0;long waitTimeMilliSecs = ONE_SECOND;while (!asyncResult.isDone()) {Thread.sleep(waitTimeMilliSecs);// double the wait time for the next iterationwaitTimeMilliSecs *= 2;if (poll++ > MAX_NUM_POLL_REQUESTS) {throw new Exception("Request timed out. If this is a large set " +"of metadata <strong>com</strong>ponents, check that the time allowed " +"by MAX_NUM_POLL_REQUESTS is sufficient.");}asyncResult = metadatabinding.checkStatus(new String[] {asyncResult.getId()})[0];System.out.println("Status is: " + asyncResult.getState());}if (asyncResult.getState() != AsyncRequestState.Completed) {throw new Exception(asyncResult.getStatusCode() + " msg: " +asyncResult.getMessage());}RetrieveResult result = metadatabinding.checkRetrieveStatus(asyncResult.getId());// Print out any warning messagesStringBuilder buf = new StringBuilder();if (result.getMessages() != null) {for (RetrieveMessage rm : result.getMessages()) {buf.append(rm.getFileName() + " - " + rm.getProblem());}}if (buf.length() > 0) {System.out.println("Retrieve warnings:\n" + buf);}// Write the zip to the file systemSystem.out.println("Writing results to zip file");ByteArrayInputStream bais = new ByteArrayInputStream(result.getZipFile());File resultsFile = new File("retrieveResults.zip");FileOutputStream os = new FileOutputStream(resultsFile);try {ReadableByteChannel src = Channels.newChannel(bais);FileChannel dest = os.getChannel();copy(src, dest);System.out.println("Results written to " + resultsFile.getAbsolutePath());}finally {os.close();}36

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!