summaryrefslogtreecommitdiffstats
path: root/tags/cpp-stable-20060304/sca/runtime/core/test/MyValue/MyValueImpl.cpp
blob: 8d45eac6e9192aad98fcddc33ca333b0aa5dfdfc (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 *
 *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file 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.
 */

/* $Rev$ $Date: 2005/12/22 11:33:21 $ */

#include "MyValueImpl.h"
#include "CustomerInfo.h"
#include "StockQuoteService.h"
#include <iostream>
using namespace std;

#include "osoa/sca/sca.h"
#include "commonj/sdo/SDO.h"
using namespace osoa::sca;
using namespace commonj::sdo;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

MyValueImpl::MyValueImpl()
{
    cout << "Construct MyValueImpl" << endl;

}

MyValueImpl::~MyValueImpl()
{
    cout << "Destroy MyValueImpl" << endl;
}

//////////////////////////////////////////////////////////////////////
// Other methods
//////////////////////////////////////////////////////////////////////
float MyValueImpl::getMyValue(const char *customerID) 
{
    cout << "In getMyValue with customerid: " << customerID << endl;


    ComponentContext myContext = ComponentContext::getCurrent();
    CustomerInfo* customerInfoService = (CustomerInfo*)myContext.getService("customerInfo");

    if (customerInfoService == 0)
    {
        cout << "unable to find customer info service" << endl;

        return 0.0f;
    }

    const char * stock;
    try
    {
        stock = customerInfoService->getCustomerInformation(customerID);
    }
    catch(const char* x)
    {
        cout << "Exception caught: " << x <<endl;
        throw;
    }

    ServiceList serviceList = myContext.getServices("customerInfo");

    for (unsigned int i=0; i<serviceList.size();i++)
    {
        CustomerInfo* service = (CustomerInfo*)(serviceList[i]);
        stock = service->getCustomerInformation(customerID);
        cout << "Stock from getServices " << i << " : " << stock <<endl;

    }

    // Use a second SCA reference to get the stock price
    StockQuoteService* stockQuoteService = (StockQuoteService*)myContext.getService("stockQuote");

    if (stockQuoteService == 0)
    {
        cout << "unable to find stock quote service" << endl;

        return 0.1f;
    }

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

    try {
        DataFactoryPtr factory = myContext.getDataFactory();
        requestDO = factory->create("http://swanandmokashi.com", "GetQuotes");
        requestDO->setCString("QuoteTicker", stock);
    } catch (SDORuntimeException e)
    {
        cout << e;
    }

    // Invoke the service
    DataObjectPtr result = stockQuoteService->GetStockQuotes(requestDO);

    float stockPrice = 0.2f;

    try {
        float stockPrice = result->getDataObject("GetQuotesResult")->getList("Quote")[0]->getFloat("StockQuote");
        //dataobjectlist& dlist = result->getdataobject("getquotesresult")->getlist("quote");
        //DataObjectList& dlist = result->getDataObject("GetQuotesResult")->getList((unsigned int)0);
        //float stockPrice = result->getDataObject("GetQuotesResult")->getList(0u)[0]->getFloat("StockQuote");
        cout << "My stock price is " << stockPrice << endl;
        //float stockPrice=((DataObject)result.getDataObject("GetQuotesResult").getList(0).get(0)).getFloat("StockQuote");
        //        System.out.println("    [getMyValue] stock quote = "+stockPrice);
        } catch (SDORuntimeException e)
    {
        cout << e;
    }

    return stockPrice;

    /*
    // Dummy method

    if (!strcmp(stock, "IBM"))
        return 99.999f;
    else
        return 31.05f;
    */

}

float MyValueImpl::getMyValueS(const string& customerID) 
{
    cout << "In getMyValueS with customerid: " << customerID.c_str() << endl;
    return 87.35f;
}

string MyValueImpl::getCustname(string& customerID) 
{
    cout << "In getCustname with customerid: " << customerID.c_str() << endl;
    customerID = "FRED";
    cout << "setting customer name to " << customerID.c_str() <<endl;
    return "done";
}

static string fred("FEREDDEYY");
const string& MyValueImpl::getCustnamecs(string customerID) 
{
    cout << "In getCustname with customerid: " << customerID.c_str() << endl;
    customerID = "FRED";
    cout << "setting customer name to " << customerID.c_str() <<endl;
    return fred;
}