alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*******************************************************************************
 * Copyright (c) 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package model;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.ui.examples.rcp.adventure.Account;
import org.eclipse.ui.examples.rcp.adventure.Activity;
import org.eclipse.ui.examples.rcp.adventure.Adventure;
import org.eclipse.ui.examples.rcp.adventure.AdventureFactory;
import org.eclipse.ui.examples.rcp.adventure.Catalog;
import org.eclipse.ui.examples.rcp.adventure.Category;
import org.eclipse.ui.examples.rcp.adventure.Lodging;
import org.eclipse.ui.examples.rcp.adventure.Signon;
import org.eclipse.ui.examples.rcp.adventure.Transportation;

import dk.jaoo.abwsclient.catalog.ABPackage;
import dk.jaoo.abwsclient.catalog.ActivityList;

/**
 * Utility class for converting between the web Catalog objects and the EMF
 * Catalog model.
 */
public final class CatalogUtil {

	private CatalogUtil() {
		// prevent instantiation
	}

	/**
	 * Converts a Catalog from the Catalog web service to the corresponding EMF
	 * model.
	 * 
	 * @param webCatalog
	 *            the web Catalog
	 * @return the EMF Catalog
	 */
	public static Catalog convertCatalog(
			dk.jaoo.abwsclient.catalog.Catalog webCatalog) {
		Catalog catalog = AdventureFactory.eINSTANCE.createCatalog();
		catalog.setLastModified(webCatalog.getLastModified().getTimeInMillis());
		Map categoryMap = convertCategories(catalog, webCatalog.getCategories());
		Map lodgingMap = convertLodgings(catalog, webCatalog.getLodgings());
		Map activityMap = convertActivities(catalog, webCatalog.getActivities());
		convertTransportations(catalog, webCatalog.getTransportation());
		Map adventureMap = convertAdventures(webCatalog.getPackages(),
				categoryMap, lodgingMap);
		assignDefaultActivities(webCatalog.getActivitylists(), activityMap,
				adventureMap);
		convertSignons(catalog, webCatalog.getSignons());
		convertAccounts(catalog, webCatalog.getAccounts());
		return catalog;
	}

	private static Map convertCategories(Catalog catalog,
			dk.jaoo.abwsclient.catalog.Category[] webCategories) {
		Map categoryMap = new HashMap(webCategories.length);
		for (int i = 0; i < webCategories.length; i++) {
			Category category = convert(catalog, webCategories[i]);
			categoryMap.put(category.getId(), category);
		}
		return categoryMap;
	}

	private static Category convert(Catalog catalog,
			dk.jaoo.abwsclient.catalog.Category webCategory) {
		Category category = AdventureFactory.eINSTANCE.createCategory();
		category.setId(webCategory.getId().trim());
		category.setName(webCategory.getName());
		catalog.getCategories().add(category);
		return category;
	}

	private static Map convertActivities(Catalog catalog,
			dk.jaoo.abwsclient.catalog.Activity[] webActivities) {
		Map activityMap = new HashMap(webActivities.length);
		for (int i = 0; i < webActivities.length; i++) {
			Activity activity = convert(catalog, webActivities[i]);
			activityMap.put(activity.getId(), activity);
		}
		return activityMap;
	}

	private static Activity convert(Catalog catalog,
			dk.jaoo.abwsclient.catalog.Activity webActivity) {
		Activity activity = AdventureFactory.eINSTANCE.createActivity();
		activity.setId(webActivity.getId().trim());
		activity.setName(webActivity.getName());
		activity.setDescription(webActivity.getDescription());
		activity.setLocation(webActivity.getLocation());
		activity.setPrice(webActivity.getPrice());
		catalog.getActivities().add(activity);
		return activity;
	}

	private static Map convertLodgings(Catalog catalog,
			dk.jaoo.abwsclient.catalog.Lodging[] webLodgings) {
		Map lodgingMap = new HashMap(webLodgings.length);
		for (int i = 0; i < webLodgings.length; i++) {
			Lodging lodging = convert(catalog, webLodgings[i]);
			lodgingMap.put(lodging.getId(), lodging);
		}
		return lodgingMap;
	}

	private static Lodging convert(Catalog catalog,
			dk.jaoo.abwsclient.catalog.Lodging webLodging) {
		Lodging lodging = AdventureFactory.eINSTANCE.createLodging();
		lodging.setId(webLodging.getId().trim());
		lodging.setName(webLodging.getName());
		lodging.setDescription(webLodging.getDescription());
		lodging.setLocation(webLodging.getLocation());
		lodging.setPrice(webLodging.getPrice());
		catalog.getLodgings().add(lodging);
		return lodging;
	}

	private static void convertTransportations(Catalog catalog,
			dk.jaoo.abwsclient.catalog.Transportation[] webTransportations) {
		for (int i = 0; i < webTransportations.length; i++) {
			convert(catalog, webTransportations[i]);
		}
	}

	private static Transportation convert(Catalog catalog, dk.jaoo.abwsclient.catalog.Transportation webTP) {
		Transportation tp = AdventureFactory.eINSTANCE.createTransportation();
		tp.setArrivalTime(webTP.getArrivaltime());
		tp.setCarrier(webTP.getCarrier());
		tp.setDepartureTime(webTP.getDeparturetime());
		tp.setDescription(webTP.getDescription());
		tp.setDestination(webTP.getDestination());
		tp.setId(webTP.getId().trim());
		tp.setName(webTP.getName());
		tp.setOrigin(webTP.getOrigin());
		tp.setPrice(webTP.getPrice());
		tp.setTransportationClass(webTP.getTransportationClass());
		catalog.getTransportations().add(tp);
		return tp;
	}

	private static Map convertAdventures(ABPackage[] webPackages,
			Map categoryMap, Map lodgingMap) {
		Map adventureMap = new HashMap(webPackages.length);
		for (int i = 0; i < webPackages.length; i++) {
			Adventure adventure = convert(webPackages[i], categoryMap,
					lodgingMap);
			adventureMap.put(adventure.getId(), adventure);
		}
		return adventureMap;
	}

	private static Adventure convert(ABPackage pkg, Map categoryMap,
			Map lodgingMap) {
		Adventure adventure = AdventureFactory.eINSTANCE.createAdventure();
		String pkgId = pkg.getId().trim();
		adventure.setId(pkgId);
		adventure.setName(pkg.getName());
		adventure.setDescription(pkg.getDescription());
		adventure.setLocation(pkg.getLocation());
		adventure.setPrice(pkg.getPrice());

		// Add adventure to resolved category
		String lodgingId = pkg.getLodgingId().trim();
		Lodging lodging = (Lodging) lodgingMap.get(lodgingId);
		if (lodging == null) {
			// TODO: Improve error handling
			System.out.println("Unresolved lodging '" + lodgingId
					+ "' for adventure '" + pkgId + "'");
		} else {
			adventure.setDefaultLodging(lodging);
		}

		// Add adventure to resolved category
		String categoryId = pkg.getCategoryId().trim();
		Category category = (Category) categoryMap.get(categoryId);
		if (category == null) {
			// TODO: Improve error handling
			System.out.println("Unresolved category '" + categoryId
					+ "' for adventure '" + pkgId + "'");
		} else {
			category.getAdventures().add(adventure);
		}

		return adventure;
	}

	private static void assignDefaultActivities(ActivityList[] activitylists,
			Map activityMap, Map adventureMap) {
		for (int i = 0; i < activitylists.length; i++) {
			ActivityList entry = activitylists[i];
			String pkgId = entry.getPackageid().trim();
			String activityId = entry.getActivityid().trim();

			Adventure adventure = (Adventure) adventureMap.get(pkgId);
			if (adventure == null) {
				// TODO: Improve error handling
				System.out.println("Unresolved adventure '" + pkgId
						+ "' for activity '" + activityId + "'");
				continue;
			}

			Activity activity = (Activity) activityMap.get(activityId);
			if (activity == null) {
				// TODO: Improve error handling
				System.out.println("Unresolved activity '" + activityId
						+ "' for adventure '" + pkgId + "'");
				continue;
			}

			adventure.getDefaultActivities().add(activity);
		}
	}

	private static void convertSignons(Catalog catalog,
			dk.jaoo.abwsclient.catalog.Signon[] webSignons) {
		for (int i = 0; i < webSignons.length; i++) {
			convert(catalog, webSignons[i]);
		}
	}

	private static Signon convert(Catalog catalog,
			dk.jaoo.abwsclient.catalog.Signon webSignon) {
		Signon signon = AdventureFactory.eINSTANCE.createSignon();
		signon.setUserName(webSignon.getUsername());
		signon.setPassword(webSignon.getPassword());
		catalog.getSignons().add(signon);
		return signon;
	}

	private static void convertAccounts(Catalog catalog,
			dk.jaoo.abwsclient.catalog.Account[] webAccounts) {
		for (int i = 0; i < webAccounts.length; i++) {
			convert(catalog, webAccounts[i]);
		}
	}

	private static Account convert(Catalog catalog,
			dk.jaoo.abwsclient.catalog.Account webAccount) {
		Account account = AdventureFactory.eINSTANCE.createAccount();
		account.setId(webAccount.getId());
		account.setAddress1(webAccount.getAddress1());
		account.setAddress2(webAccount.getAddress2());
		account.setCity(webAccount.getCity());
		account.setCountry(webAccount.getCountry());
		account.setEmail(webAccount.getEmail());
		account.setFirstName(webAccount.getFirstname());
		account.setLastName(webAccount.getLastname());
		account.setPhone(webAccount.getPhone());
		account.setState(webAccount.getState());
		account.setZip(webAccount.getZip());
		catalog.getAccounts().add(account);
		return account;
	}

}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.