package com.sun.j2ee.blueprints.catalog.client;
import java.util.Locale;
import javax.naming.NamingException;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.rmi.PortableRemoteObject;
import javax.naming.InitialContext;
import com.sun.j2ee.blueprints.servicelocator.ejb.ServiceLocator;
import com.sun.j2ee.blueprints.servicelocator.ServiceLocatorException;
import com.sun.j2ee.blueprints.catalog.util.JNDINames;
import com.sun.j2ee.blueprints.catalog.ejb.CatalogLocalHome;
import com.sun.j2ee.blueprints.catalog.ejb.CatalogLocal;
import com.sun.j2ee.blueprints.catalog.dao.CatalogDAO;
import com.sun.j2ee.blueprints.catalog.dao.CatalogDAOFactory;
import com.sun.j2ee.blueprints.catalog.exceptions.CatalogDAOSysException;
import com.sun.j2ee.blueprints.catalog.model.Page;
import com.sun.j2ee.blueprints.catalog.model.Category;
import com.sun.j2ee.blueprints.catalog.model.Product;
import com.sun.j2ee.blueprints.catalog.model.Item;
public class CatalogHelper implements java.io.Serializable {
private CatalogDAO dao;
private boolean useFastLane = false;
private String searchQuery = "";
private String categoryId = "";
private String productId = "";
private String itemId = "";
private Locale locale;
private int count = 2;
private int start = 0;
public CatalogHelper(boolean useFastLane) {
this.useFastLane = useFastLane;
}
public CatalogHelper() {
useFastLane = true;
}
public void setLocale(String localeString) {
locale = getLocaleFromString(localeString);
}
public void setCount(String countString) {
this.count = (new Integer(countString)).intValue();
}
public void setStart(String startString) {
if (startString != null) {
this.start = (new Integer(startString)).intValue();
} else {
start = 0;
}
}
public void setCategoryId(String categoryId) {
this.categoryId = categoryId;
}
public void setProductId(String productId) {
this.productId = productId;
}
public void setItemId(String itemId) {
this.itemId = itemId;
}
public void setSearchQuery(String searchQuery) {
this.searchQuery = searchQuery;
}
public Page getSearchItems()
throws CatalogException {
return useFastLane
? searchItemsFromDAO(searchQuery, start, count, locale)
: searchItemsFromEJB(searchQuery, start, count, locale);
}
public Page searchItems(String searchQuery, int start, int count, String localeString)
throws CatalogException {
Locale locale = getLocaleFromString(localeString);
return useFastLane
? searchItemsFromDAO(searchQuery, start, count, locale)
: searchItemsFromEJB(searchQuery, start, count, locale);
}
private Page searchItemsFromEJB(String searchQuery, int start, int count, Locale locale)
throws CatalogException {
return getCatalogEJB().searchItems(searchQuery, start, count,locale);
}
private Page searchItemsFromDAO(String searchQuery, int start, int count, Locale locale)
throws CatalogException {
try {
if (dao == null)
dao = CatalogDAOFactory.getDAO();
return dao.searchItems(searchQuery, start, count,
locale);
} catch (CatalogDAOSysException se) {
throw new CatalogException(se.getMessage());
}
}
public Page getCategories(int start, int count, String localeString)
throws CatalogException {
Locale locale = getLocaleFromString(localeString);
return useFastLane
? getCategoriesFromDAO(start, count, locale)
: getCategoriesFromEJB(start, count, locale);
}
public Page getCategories()
throws CatalogException {
return useFastLane
? getCategoriesFromDAO(start, count, locale)
: getCategoriesFromEJB(start, count, locale);
}
private Page getCategoriesFromDAO(int start, int count, Locale locale)
throws CatalogException {
try {
if (dao == null)
dao = CatalogDAOFactory.getDAO();
return dao.getCategories(start, count, locale);
} catch (CatalogDAOSysException se) {
throw new CatalogException(se.getMessage());
}
}
private Page getCategoriesFromEJB(int start, int count, Locale locale)
throws CatalogException {
return getCatalogEJB().getCategories(start, count, locale);
}
public Page getProducts()
throws CatalogException {
return useFastLane
? getProductsFromDAO(categoryId, start, count, locale)
: getProductsFromEJB(categoryId, start, count,locale);
}
public Page getProducts(String categoryId, int start, int count, String localeString)
throws CatalogException {
Locale locale = getLocaleFromString(localeString);
return useFastLane
? getProductsFromDAO(categoryId, start, count, locale)
: getProductsFromEJB(categoryId, start, count,locale);
}
private Page getProductsFromEJB(String categoryId, int start, int count, Locale locale)
throws CatalogException {
return getCatalogEJB().getProducts(categoryId, start, count, locale);
}
private Page getProductsFromDAO(String categoryId, int start, int count, Locale locale)
throws CatalogException {
try {
if (dao == null)
dao = CatalogDAOFactory.getDAO();
return dao.getProducts(categoryId, start, count, locale);
} catch (CatalogDAOSysException se) {
throw new CatalogException(se.getMessage());
}
}
public Page getItems()
throws CatalogException {
return useFastLane
? getItemsFromDAO(productId, start, count, locale)
: getItemsFromEJB(productId, start, count, locale);
}
public Page getItems(String productId, int start, int count, String localeString)
throws CatalogException {
Locale locale = getLocaleFromString(localeString);
return useFastLane
? getItemsFromDAO(productId, start, count, locale)
: getItemsFromEJB(productId, start, count, locale);
}
private Page getItemsFromEJB(String productId, int start, int count, Locale locale)
throws CatalogException {
return getCatalogEJB().getItems(productId, start, count, locale);
}
private Page getItemsFromDAO(String productId, int start, int count, Locale locale)
throws CatalogException {
try {
if (dao == null)
dao = CatalogDAOFactory.getDAO();
return dao.getItems(productId, start, count, locale);
} catch (CatalogDAOSysException se) {
throw new CatalogException(se.getMessage());
}
}
public Item getItem() throws CatalogException {
return useFastLane ? getItemFromDAO(itemId,locale) : getItemFromEJB(itemId,locale);
}
public Item getItem(String itemId, Locale locale) throws CatalogException {
return useFastLane ? getItemFromDAO(itemId,locale) : getItemFromEJB(itemId,locale);
}
public Item getItem(String itemId, String localeString) throws CatalogException {
Locale locale = getLocaleFromString(localeString);
return useFastLane ? getItemFromDAO(itemId,locale) : getItemFromEJB(itemId,locale);
}
private Item getItemFromDAO(String itemId, Locale locale) throws CatalogException {
try {
if (dao == null)
dao = CatalogDAOFactory.getDAO();
return dao.getItem(itemId, locale);
} catch (CatalogDAOSysException se) {
throw new CatalogException(se.getMessage());
}
}
private Item getItemFromEJB(String itemId, Locale locale) throws CatalogException {
return getCatalogEJB().getItem(itemId, locale);
}
private CatalogLocal getCatalogEJB() throws CatalogException {
try {
ServiceLocator sl = new ServiceLocator();
CatalogLocalHome home =(CatalogLocalHome)sl.getLocalHome(JNDINames.CATALOG_EJBHOME);
return home.create();
} catch (javax.ejb.CreateException cx) {
throw new CatalogException("CatalogHelper: failed to create CatalogLocal EJB: caught " + cx);
} catch (ServiceLocatorException slx) {
throw new CatalogException("CatalogHelper: failed to look up Catalog Home: caught " + slx);
}
}
private Locale getLocaleFromString(String localeString) {
if (localeString == null) return null;
if (localeString.toLowerCase().equals("default")) return Locale.getDefault();
int languageIndex = localeString.indexOf('_');
if (languageIndex == -1) return null;
int countryIndex = localeString.indexOf('_', languageIndex +1);
String country = null;
if (countryIndex == -1) {
if (localeString.length() > languageIndex) {
country = localeString.substring(languageIndex +1, localeString.length());
} else {
return null;
}
}
int variantIndex = -1;
if (countryIndex != -1) countryIndex = localeString.indexOf('_', countryIndex +1);
String language = localeString.substring(0, languageIndex);
String variant = null;
if (variantIndex != -1) {
variant = localeString.substring(variantIndex +1, localeString.length());
}
if (variant != null) {
return new Locale(language, country, variant);
} else {
return new Locale(language, country);
}
}
}