/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.test.jbossnet.external.server;

import java.rmi.RemoteException;

import javax.ejb.SessionBean;

import org.jboss.test.util.ejb.SessionSupport;
import org.jboss.test.jbossnet.external.google.GoogleSearchPort;
import org.jboss.test.jbossnet.external.google.GoogleSearchService;
import org.jboss.test.jbossnet.external.google.GoogleSearchResult;

import org.jboss.test.jbossnet.external.babelfish.BabelFishPortType;
import org.jboss.test.jbossnet.external.babelfish.BabelFishService;
import org.jboss.logging.Logger;

import javax.naming.InitialContext;

/**
 * implementation of a federated ejb service making
 * use of external web services bound in the JNDI tree.
 * @version 	1.0
 * @author cgjung
 * @ejb.bean name="FederatedService"
 *           display-name="Federated Service"
 *           type="Stateless"
 *           view-type="both"
 *           jndi-name="FederatedService"
 *           local-jndi-name="FederatedServiceLocal"
 * @ejb.transaction type="Required"
 * @jboss-net.web-service urn="FederatedService"
 * @ejb.interface remote-class="org.jboss.test.jbossnet.external.FederatedService" local-class="org.jboss.test.jbossnet.external.server.FederatedServiceLocal" extends="javax.ejb.EJBObject" local-extends="javax.ejb.EJBLocalObject"
 * @ejb.home remote-class="org.jboss.test.jbossnet.external.FederatedServiceHome" local-class="org.jboss.test.jbossnet.external.server.FederatedServiceHomeLocal" extends="javax.ejb.EJBHome" local-extends="javax.ejb.EJBLocalHome"
 */

public class FederatedServiceBean extends SessionSupport implements SessionBean
{
   private static Logger log = Logger.getLogger(FederatedServiceBean.class);

   /** 
    * first accesses google to produce a set of
    * search results and then connects to babelfish
    * to translate the content of the title of 
    * one of them
    * @jboss-net.web-method
    * @ejb.interface-method view-type="both"
    */

   public String findAndTranslate(String searchTerm) throws Exception
   {
      // look into JNDI
      InitialContext initContext = new InitialContext();

      // find external references there
      GoogleSearchService googleService =
         (GoogleSearchService) initContext.lookup("Google");
      GoogleSearchPort google = googleService.getGoogleSearchPort();

      String licenseKey =
         System.getProperty(
            "google.license",
            "Wr5iTf5QFHJKmmnJn+61lt9jaMuWMKCj");

      GoogleSearchResult searchResult =
         google.doGoogleSearch(
            licenseKey,
            searchTerm,
            0,
            10,
            true,
            "",
            false,
            "",
            "latin1",
            "latin1");

      log.debug("Query for: '" + searchTerm + "' returned: " + searchResult);

      BabelFishService babelFishService =
         (BabelFishService) initContext.lookup("BabelFish");

      BabelFishPortType babelFish = babelFishService.getBabelFishPort();

      // and call them
      try
      {
         String translationResult = babelFish.babelFish("en_de", searchTerm);
         log.debug(
            "Translation of: '"
               + searchTerm
               + "' returned: "
               + translationResult);
         return translationResult;
      } catch (RemoteException e)
      {
         // sometimes babelfish is down
         return "This service is currently disabled.";
      }

   }

}
