001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package reversemapping;
020:
021: import java.text.*;
022: import java.util.*;
023: import javax.persistence.*;
024:
025: /**
026: * A simple program that uses the reverse-mapped classes from the airlines
027: * schema to print out a list of schedules flightes and the
028: * projected profits from them.
029: */
030: public class Main {
031:
032: private static void print(String msg) {
033: System.out.println(msg);
034: }
035:
036: @SuppressWarnings("unchecked")
037: public static void main(String[] args) {
038: // Create a new EntityManagerFactory using the System properties.
039: // The "reversemapping" name will be used to configure based on the
040: // corresponding name in the META-INF/persistence.xml file
041: EntityManagerFactory factory = Persistence
042: .createEntityManagerFactory("reversemapping", System
043: .getProperties());
044:
045: // Create a new EntityManager from the EntityManagerFactory. The
046: // EntityManager is the main object in the persistence API, and is
047: // used to create, delete, and query objects, as well as access
048: // the current transaction
049: EntityManager em = factory.createEntityManager();
050:
051: List<Availability> schedule = (List<Availability>) em
052: .createQuery(
053: "select avail from Availability avail "
054: + "join fetch avail.flight "
055: + "order by avail.flightDate asc, avail.flight.departureTime asc")
056: .getResultList();
057: for (Availability avail : schedule) {
058: Flight flight = avail.getFlight();
059:
060: // note that Availability.getFlightDate is just a DATE with no
061: // time component, and Flight.getDepartureTime() is just a TIME
062: // with no date component
063: print(new SimpleDateFormat("MMM dd, yyyy").format(avail
064: .getFlightDate())
065: + " flight "
066: + flight.getFlightId()
067: + " departs "
068: + new SimpleDateFormat("hh:mm aa").format(flight
069: .getDepartureTime())
070: + " from "
071: + flight.getOrigAirport()
072: + " to "
073: + flight.getDestAirport());
074:
075: // look up the Airline reference based on the flight ID
076: Airline airline = em.getReference(Airline.class, flight
077: .getFlightId().substring(0, 2));
078: double ratePerMile = airline.getBasicRate();
079: double rate = flight.getMiles() * ratePerMile;
080:
081: int econTaken = avail.getEconomySeatsTaken();
082: int businessTaken = avail.getBusinessSeatsTaken();
083: int firstclassTaken = avail.getFirstclassSeatsTaken();
084:
085: double income = (econTaken * rate)
086: + (businessTaken * rate)
087: + (businessTaken * rate * airline
088: .getBusinessLevelFactor())
089: + (firstclassTaken * rate)
090: + (firstclassTaken * rate * airline
091: .getFirstclassLevelFactor());
092:
093: int seatsTaken = econTaken + businessTaken
094: + firstclassTaken;
095: int totalSeats = airline.getEconomySeats()
096: + airline.getBusinessSeats()
097: + airline.getFirstclassSeats();
098: double percentFull = (double) seatsTaken
099: / (double) totalSeats;
100:
101: print(" income from flight: "
102: + NumberFormat.getCurrencyInstance().format(income)
103: + " with "
104: + seatsTaken
105: + " seats taken ("
106: + NumberFormat.getPercentInstance().format(
107: percentFull) + " full)");
108:
109: double gallonsPerMile = 2.0d; // approx for a small plane
110: double totalGallons = gallonsPerMile * flight.getMiles();
111: double costPerGallon = 0.50d; // approx 2006 prices
112: double totalFuelCost = totalGallons * costPerGallon;
113: print(" fuel cost of flight over "
114: + NumberFormat.getNumberInstance().format(
115: flight.getMiles())
116: + " miles: "
117: + NumberFormat.getCurrencyInstance().format(
118: totalFuelCost));
119:
120: double totalCost = totalFuelCost;
121:
122: print(" total profit: "
123: + NumberFormat.getCurrencyInstance().format(
124: income - totalCost));
125: }
126:
127: // Again, it is always good to clean up after ourselves
128: em.close();
129:
130: factory.close();
131: }
132: }
|