/**
* PlayTrade API Java Client
* Copyright 2013 PureBuy Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use the this PlayTrade API Java Client except
* in compliance with the License.
*
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.File;
import java.io.IOException;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import uk.co.purebuy.commerce.businessobjects.importexport.playtrade.call.DownloadCallResult;
import uk.co.purebuy.commerce.businessobjects.importexport.playtrade.call.DownloadReportCall;
import uk.co.purebuy.commerce.businessobjects.importexport.playtrade.call.PlayTradeApiCallException;
import uk.co.purebuy.commerce.businessobjects.importexport.playtrade.dom.OrderReport;
import uk.co.purebuy.commerce.businessobjects.importexport.playtrade.dom.OrderReportRow;
import uk.co.purebuy.commerce.businessobjects.importexport.playtrade.dom.PlayTradeRow;
import uk.co.purebuy.commerce.businessobjects.importexport.playtrade.parser.FlatFileListener;
import uk.co.purebuy.commerce.businessobjects.importexport.playtrade.parser.OrdersReportParser;
/**
* This example will download an Orders Report and
* parse the outout into and {@link OrderReport} object
*
* Each order will be turned into an {@link OrderReportRow}
*
* You must implement the {@link FlatFileListener} interface
* to listen for parsed Records from the order report.
*
* @author Dave/James Allcock
* Copyright 2013 PureBuy Ltd
*/
public class DownloadOrdersReport implements FlatFileListener
{
/**
* This is where we will store all the downloaded orders
* for processing later on
*/
private OrderReport m_orderReport = new OrderReport();
/**
* @param args
* @throws PlayTradeApiCallException
* @throws IOException
*/
public static void main(String[] args) throws PlayTradeApiCallException, IOException
{
final String REPORT_ID = "1234567";
System.out.println("Geting the report status of report with ID="+ REPORT_ID);
DownloadReportCall call = new DownloadReportCall(REPORT_ID);
//set the authentication
call.setUsername("user@email.com");
call.setPassword("password");
//make the call to get the report
DownloadCallResult result = call.callDownloadReport();
//write the report to a temporary file
File tempfile = File.createTempFile("playtrade_orders_"+REPORT_ID+"_", ".txt");
String resultbody = result.getResultBody();
FileUtils.writeStringToFile(tempfile, resultbody, "UTF-8");
System.out.println("Wrote PlayTrade report "+ REPORT_ID +" to '" + tempfile.getAbsolutePath() +"'" );
try
{
/* parse the downloaded assuming it is an orders report report */
OrdersReportParser orders_parser = new OrdersReportParser( tempfile );
//parse the file sending each order found(PlayTradeRow row) method
Date youngest_order_date = orders_parser.parseFlatFile( new DownloadOrdersReport() );
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
* (non-Javadoc)
* @see uk.co.purebuy.commerce.businessobjects.importexport.playtrade.parser.FlatFileListener#found(uk.co.purebuy.commerce.businessobjects.importexport.playtrade.dom.PlayTradeRow)
*/
@Override
public void found(PlayTradeRow row) throws Exception
{
if ( row == null )
{
throw new IllegalArgumentException("The OrderReportRow record cannot be null");
}
else if ( row instanceof OrderReportRow == false )
{
throw new IllegalArgumentException("The data must be of type '" + OrderReportRow.class.getName() + "'");
}
OrderReportRow order_row = (OrderReportRow)row;
System.out.println("Processing raw order : " + order_row);
//here we can either process the order, or save it for later
m_orderReport.addRowData( row );
}
}