Advertisement
Guest User

clientImpl

a guest
Nov 27th, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.39 KB | None | 0 0
  1. package it.polito.dp2.FDS.sol3;
  2.  
  3. import static org.junit.Assert.assertNotNull;
  4.  
  5. import java.math.BigInteger;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. import java.text.DateFormat;
  9. import java.text.ParseException;
  10. import java.text.SimpleDateFormat;
  11. import java.util.ArrayList;
  12. import java.util.Calendar;
  13. import java.util.GregorianCalendar;
  14. import java.util.HashSet;
  15. import java.util.List;
  16. import java.util.Set;
  17. import java.util.TimeZone;
  18.  
  19. import javax.xml.ws.Holder;
  20. import javax.xml.ws.WebServiceException;
  21.  
  22. import it.polito.dp2.FDS.lab3.FDSBookingClient;
  23. import it.polito.dp2.FDS.lab3.FDSUnbookingClient;
  24. import it.polito.dp2.FDS.lab3.MissingDataException;
  25. import it.polito.dp2.FDS.lab3.OperationFailException;
  26. import it.polito.dp2.FDS.lab3.UnbookClientImpl;
  27. import it.polito.dp2.FDS.lab3.gen.FDSBooking;
  28. import it.polito.dp2.FDS.lab3.gen.FDSBookingService;
  29. import it.polito.dp2.FDS.lab3.gen.FlightDateType;
  30. import it.polito.dp2.FDS.lab3.gen.InvalidFlightInstanceFault_Exception;
  31. import it.polito.dp2.FDS.lab3.server.FlightDate;
  32. import it.polito.dp2.FDS.lab3.server.FlightInstance;
  33.  
  34.  
  35. public class FDSBookingClientImpl implements FDSBookingClient {
  36.    
  37.     public URL service_url = null;
  38.     WebServiceException e = null;
  39.    
  40.     FDSBooking proxy;
  41.    
  42.     String flightNumber = null;
  43.     FlightDateType departureDate = null;
  44.     boolean partialBookingAllowed = false;
  45.     Holder<List<String>> passenger;
  46.     Holder<Boolean> success;
  47.    
  48.     public FDSBookingClientImpl(){
  49.        
  50.         try{
  51.              success = new Holder<Boolean>(false);
  52.             setServiceURL(new URL(System.getProperty("it.polito.dp2.FDS.sol3.URL")));
  53.              
  54.             FDSBookingService service = new FDSBookingService(service_url);
  55.            
  56.              proxy = service.getFDSBookingServiceSOAPPort();
  57.            
  58.            
  59.         } catch (MalformedURLException ex) {
  60.             e = new WebServiceException(ex);
  61.        
  62.         }catch(Exception e){
  63.            
  64.             System.out.println ("Fatal error: " + e);
  65.         }
  66.     }
  67.  
  68.     public static void main(String[] args)
  69.     {
  70.         FDSBookingClientImpl f = new FDSBookingClientImpl();
  71.         System.out.println("URL :"+f.service_url);
  72.         String stringdate = "05/03/2010 GMT";
  73.        
  74.         DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy z");
  75.         GregorianCalendar gdate = new GregorianCalendar();
  76.         try {
  77.             gdate.setTime(dateFormat.parse(stringdate));
  78.              gdate.setTimeZone(TimeZone.getTimeZone(stringdate.substring(11)));
  79.     } catch (ParseException e) {
  80.             // TODO Auto-generated catch block
  81.             e.printStackTrace();
  82.         }
  83.        
  84.        
  85.        f.setDepartureDate(gdate);
  86.        f.setFlightNumber("FI097");
  87.        
  88.         Set<String> p = new HashSet<String>();
  89.         p.add("Lorella Ferlito");
  90.        
  91.         try
  92.         {
  93.             System.out.println("size before = "+f.getPassengers().size());
  94.             p = f.book(p, true);
  95.             System.out.println("Result of BOOK: ");
  96.             for(String name : p)
  97.                 System.out.println("passenger :"+name);
  98.            
  99.             p = f.getPassengers();
  100.             System.out.println("size after = "+p.size());
  101.             System.out.println("Result of GETPASSENGERS: ");
  102.             for(String name : p)
  103.                 System.out.println("passenger :"+name);
  104.         }
  105.         catch (MissingDataException | OperationFailException e)
  106.         {
  107.             // TODO Auto-generated catch block
  108.            
  109.             e.printStackTrace();
  110.         }
  111.        
  112.     }
  113.  
  114.     @Override
  115.     public void setFlightNumber(String number) {
  116.         // TODO Auto-generated method stub
  117.        
  118.         flightNumber = number;
  119.        
  120.     }
  121.  
  122.     @Override
  123.     public void setDepartureDate(GregorianCalendar gdate) {
  124.        
  125.         departureDate = new FlightDateType();
  126.         departureDate.setDay(gdate.get(Calendar.DAY_OF_MONTH));
  127.         departureDate.setMonth(gdate.get(Calendar.MONTH));
  128.         departureDate.setYear(BigInteger.valueOf(gdate.get(Calendar.YEAR)));
  129.     }
  130.  
  131.     @Override
  132.     public void setServiceURL(URL url) {
  133.         // TODO Auto-generated method stub     
  134.             service_url = url;             
  135.     }
  136.  
  137.     @Override
  138.     public Set<String> book(Set<String> passengerNames,
  139.             boolean partialBookingAllowed) throws MissingDataException,
  140.             OperationFailException {
  141.        
  142.         if(flightNumber==null || departureDate==null || passengerNames==null || passengerNames.size()==0)
  143.             throw new MissingDataException();
  144.        
  145.         List<String> l = new ArrayList<String>();
  146.         l.addAll(passengerNames);
  147.         passenger = new Holder<List<String>>( l );
  148.         try {
  149.             proxy.book(flightNumber, departureDate, partialBookingAllowed, passenger, success);
  150.         } catch (InvalidFlightInstanceFault_Exception e) {
  151.            
  152.             throw new OperationFailException(e);
  153.         }
  154.        
  155.         /*
  156.         if(success.value==true)
  157.         {
  158.             System.out.println("Book operation successfully completed.");
  159.             return new HashSet<String>(passenger.value);
  160.         }
  161.         else {
  162.             System.out.println("Book operation failed.");
  163.             return null;
  164.         }*/
  165.         if(success.value!=null)
  166.         return new HashSet<String>(passenger.value);
  167.         else return null;
  168.        
  169.     }
  170.  
  171.     @Override
  172.     public Set<String> getPassengers() throws MissingDataException,
  173.             OperationFailException {
  174.        
  175.         if(flightNumber==null || departureDate==null)
  176.             throw new MissingDataException();
  177.        
  178.         List<String> l = new ArrayList<String>();
  179.         try {
  180.             l = proxy.getPassengerList(flightNumber, departureDate);
  181.         } catch (InvalidFlightInstanceFault_Exception e) {
  182.            
  183.             throw new OperationFailException();
  184.         }
  185.         return new HashSet<String>(l);
  186.     }
  187.    
  188.      static private GregorianCalendar flightDateToGregorianCalendar(FlightDate date)
  189.         {
  190.             GregorianCalendar cal = new GregorianCalendar(date.getYear().intValue(), date.getMonth(), date.getDay());
  191.             return cal;
  192.         }
  193.  
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement