/*
* Copyright 2011 Google Inc. All Rights Reserved.
*
* 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.
*/
package net.helipilot50.stocktrade.server;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import net.helipilot50.stocktrade.client.CustomerSO;
import net.helipilot50.stocktrade.model.Customer;
import net.helipilot50.stocktrade.model.Holding;
import Framework.ServiceObjectRegistry;
import Framework.TextData;
import StockTradeBusinessClasses.Array_Of_Holding;
import StockTradeServices.interfaces.CustomerSO_proxy;
import StockTradeServices.interfaces.ICustomerMgr;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
@SuppressWarnings("serial")
public class CustomerSOImpl extends RemoteServiceServlet implements CustomerSO {
@Override
public void init() throws ServletException {
super.init();
ServiceObjectRegistry.setConfigFileName("net/helipilot50/stocktrade/server/stock-trade-client-config.xml");
}
@Override
public Customer getCustomer(String name) {
ICustomerMgr customerSO = CustomerSO_proxy.getInstance();
StockTradeBusinessClasses.Customer customer = customerSO.getCustomer(new TextData(name));
return toCustomerDTO(customer);
}
public static Customer toCustomerDTO(StockTradeBusinessClasses.Customer customer) {
Customer customerDTO = new Customer(customer.getCustomerName().toString(),
customer.getAddress(),
customer.getPhoneNumber(),
customer.getCashBalance());
List<Holding> holdings = new ArrayList<Holding>();
for (StockTradeBusinessClasses.Holding holding : customer.getHoldingList()){
holdings.add(toHoldingDTO(holding));
}
customerDTO.setHoldingList(holdings);
return customerDTO;
}
public static StockTradeBusinessClasses.Customer fromCustomerDTO(Customer customerDTO){
StockTradeBusinessClasses.Customer customer = new StockTradeBusinessClasses.Customer();
customer.setCustomerName(new TextData(customerDTO.getCustomerName()));
customer.setAddress(customerDTO.getAddress());
customer.setCashBalance(customerDTO.getCashBalance());
customer.setPhoneNumber(customerDTO.getPhoneNumber());
Array_Of_Holding<StockTradeBusinessClasses.Holding> holdings = new Array_Of_Holding<StockTradeBusinessClasses.Holding>();
for (Holding holding : customerDTO.getHoldingList()){
holdings.add(fromHoldingDTO(holding));
}
customer.setHoldingList(holdings);
return customer;
}
public static Holding toHoldingDTO(StockTradeBusinessClasses.Holding holding){
Holding holdingDTO = new Holding(holding.getCustomerName().toString(),
holding.getStockName(),
holding.getQuantity(),
holding.getPrice());
return holdingDTO;
}
public static StockTradeBusinessClasses.Holding fromHoldingDTO(Holding holdingDTO){
StockTradeBusinessClasses.Holding holding = new StockTradeBusinessClasses.Holding();
holding.setCustomerName(new TextData(holdingDTO.getCustomerName()));
holding.setStockName(holdingDTO.getStockName());
holding.setQuantity(holdingDTO.getQuantity());
holding.setPrice(holdingDTO.getPrice());
return holding;
}
}