summaryrefslogtreecommitdiffstats
path: root/tags/cpp-0.1.incubating-M1-RC3b/sca/samples/BigBank/Accounts/StockQuoteServiceImpl.cpp
blob: 8ce6d410c292efa0d31a5c22cb11faa05a683acd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#if defined(WIN32)  || defined (_WINDOWS)
#pragma warning(disable: 4786)
#endif

#include "StockQuoteServiceImpl.h"
#include "StockQuoteExternalService.h"

#include "osoa/sca/sca.h"
#include "commonj/sdo/SDO.h"

using namespace osoa::sca;
using namespace commonj::sdo;

namespace com{
    namespace bigbank{
        namespace stockquote {


            float StockQuoteServiceImpl::getQuote(const char *symbol)
            {
                ComponentContext myContext = ComponentContext::getCurrent();

                // Use an SCA reference to get the stock price
                StockQuoteExternalService* stockQuoteExternalService = 
                    (StockQuoteExternalService*)myContext.
                    getService("StockQuoteExternal");

                if (stockQuoteExternalService == 0)
                {
                    std::cout << "unable to find external stock quote service" << std::endl;
                    return 0.1f;
                }

                // Create a data object representing the requests (use dynamic API until static is available)
                DataObjectPtr requestDO;

                // Invoke the service
                const char* result = stockQuoteExternalService->GetQuote(symbol);

                //std::cout << result<< std::endl;
    
                float stockPrice = 0.0f;

                try
                {
    	            XMLHelperPtr xmlHelper = HelperProvider::getXMLHelper(myContext.getDataFactory());
     	            XMLDocumentPtr stockDoc = xmlHelper->load(result);
                    if (stockDoc->getRootDataObject())
                    {
                        stockPrice=stockDoc->getRootDataObject()->getFloat("Stock.0/Last");
                    }
                }
                catch (SDORuntimeException e)
                {
                    std::cout << e << std::endl;
                    return 0.0f;
                }
                return stockPrice;
            }
        }
    }
}