Beispiel #1
0
  /**
   * Get a vehicle by cartracker id from the cached list, if the vehicle does not exist it will be
   * created.
   *
   * @param cartrackerID
   * @return The found or newly created vehicle.
   */
  @Override
  public Vehicle getOrCreateVehicleById(String cartrackerID) {
    Vehicle returnVehicle = null;

    Iterator<Vehicle> it = vehicles.iterator();
    while (it.hasNext()) {
      Vehicle vehicle = it.next();
      if (cartrackerID.equals(vehicle.getCarTrackerID())) {
        returnVehicle = vehicle;
        break;
      }
    }

    /* Vehicle does not exist, lets create a new one */
    if (returnVehicle == null) {
      try {
        /* Also create a vehicle ownership */
        GregorianCalendar registerdate = new GregorianCalendar();
        registerdate.add(Calendar.YEAR, -3);
        VehicleOwnership vehicleOwnership = new VehicleOwnership();
        vehicleOwnership.setContributeGPSData(true);
        vehicleOwnership.setRegistrationdate((GregorianCalendar) registerdate.clone());
        vehicleOwnership.setRegistrationExperationDate(null);
        try {
          // create new user with incrementing name
          userDAO = new UserDAOImpl(emf);
          loginDAO = new LoginDAOImpl(em);
          UserDto user =
              userDAO.register(
                  "user" + USER_ID++ + "name", "aidas123", "*****@*****.**");
          MovementUser mUser = loginDAO.register(user.getUsername(), user.getEmail());
          vehicleOwnership.setUser(mUser);
        } catch (UserSystemException ex) {
          Logger.getLogger(MovementsDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        // create car
        returnVehicle = new Vehicle(cartrackerID);
        returnVehicle.setLicensePlate(itLicensePlates.next());
        List<VehicleOwnership> owners = new ArrayList();
        owners.add(vehicleOwnership);
        returnVehicle.setVehicleOwners(owners);
        em.persist(returnVehicle);

        vehicleOwnership.setVehicle(returnVehicle);
        em.persist(vehicleOwnership);
        Logger.getLogger(MovementsDAOImpl.class.getName())
            .log(Level.WARNING, "Created new vehicle with ID" + cartrackerID);

        this.vehicles.add(returnVehicle);
      } catch (Exception ex) {
        Logger.getLogger(MovementsDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
      }
    }

    return returnVehicle;
  }
Beispiel #2
0
 /**
  * Get a lane from the cached list.
  *
  * @param laneId
  * @return
  */
 @Override
 public Lane getLaneById(String laneId) {
   Iterator<Lane> it = lanes.iterator();
   while (it.hasNext()) {
     Lane lane = it.next();
     if (laneId.equals(lane.getId())) {
       return lane;
     }
   }
   return null;
 }
  public static void main(String[] args) throws Exception {
    HashMap map = new HashMap();
    // map.put("hibernate.show_sql", "true");
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("titan", map);
    EntityManager entityManager = factory.createEntityManager();
    entityManager.getTransaction().begin();
    try {
      System.out.println("Initialize DB");
      InitializeDB.initialize(entityManager);
      System.out.println();
      System.out.println();

      System.out.println("Find Bill Burke by named parameter");
      Customer cust = findCustomerByNamedParameter(entityManager, "Bill", "Burke");
      System.out.println("Bill Burke's cust id: " + cust.getId());
      System.out.println();
      System.out.println();

      System.out.println("Find Gavin King by indexed parameter");
      cust = findCustomerByIndexedParameter(entityManager, "Gavin", "King");
      System.out.println("Gavin King's cust id: " + cust.getId());
      System.out.println();
      System.out.println();

      System.out.println("Output all customers via paging");
      List results;
      int first = 0;
      int max = 2;
      do {
        results = getCustomers(entityManager, max, first);
        Iterator it = results.iterator();
        while (it.hasNext()) {
          Customer c = (Customer) it.next();
          System.out.println(c.getFirstName() + " " + c.getLastName());
        }
        entityManager.clear();
        first = first + results.size();
      } while (results.size() > 0);
    } finally {
      entityManager.getTransaction().commit();
      entityManager.close();
      factory.close();
    }
  }
  /** Walk through all mapped entity's descriptors verifying that no data exists on the database */
  private static void verifyDatabaseEmpty(EntityManager em) {
    Server server = JpaHelper.getEntityManager(em).getServerSession();

    for (Iterator i = server.getDescriptors().values().iterator(); i.hasNext(); ) {
      RelationalDescriptor descriptor = (RelationalDescriptor) i.next();

      if (!descriptor.isAggregateDescriptor()) {
        ReportQuery rq = new ReportQuery(descriptor.getJavaClass(), new ExpressionBuilder());
        rq.addCount();
        rq.setShouldReturnSingleValue(true);

        int count = -1;
        try {
          count = ((Number) server.executeQuery(rq)).intValue();
        } catch (DatabaseException dbe) {

        }

        System.out.println("COUNT(" + descriptor.getAlias() + "): " + count);
      }
    }
  }
Beispiel #5
0
 /**
  * Goes trough the table auction and checks which auctions are over. If the auction is over, finds
  * all bids for that auction, selects the highest bid and sends winning message, also sends to all
  * other users message that auction is over.
  */
 public static void checkAuctionOutcome() {
   // Declaring list of all auctions.
   List<Auction> auctions = finder.all();
   // Declaring variable that represents current date.
   Date currentDate = new Date();
   // Going trough all auctions.
   for (int i = 0; i < auctions.size(); i++) {
     // Declaring variable that represents auction ending date.
     Date auctionEndingDate = auctions.get(i).endingDate;
     // Checking if the auction is active and if the auction ending date is before current date.
     if (auctions.get(i).isActive && auctionEndingDate.before(currentDate)) {
       // Finding all auction bids.
       List<Bid> bids = Bid.getAuctionBids(auctions.get(i));
       // Declaring variable that represents highest bid.
       Bid highestBid = bids.get(0);
       // Declaring string variable that represents winning message.
       String winningMessage =
           "Congratulations!!! You won bitBay auction for item #"
               + auctions.get(i).product.id
               + " - "
               + auctions.get(i).product.name
               + ".\n\r \n\r To proceed with transaction please contact: "
               + auctions.get(i).product.user.email;
       // Declaring and saving winning message.
       Message message =
           new Message(
               CommonHelpers.serviceUser(), highestBid.user, "Auction Winning", winningMessage);
       message.save();
       // Sending SMS notification to auction winner.
       String sms =
           "Congratulations!!! You won bitBay auction for item #"
               + auctions.get(i).product.id
               + " - "
               + auctions.get(i).product.name
               + ". "
               + "This product has been sent to your cart";
       if (highestBid.user.phoneNumber != null) {
         SmsHelper.sendSms(sms, highestBid.user.phoneNumber);
       }
       // After user won on auction create new cart if user don't have one, and put auction item to
       // his cart
       Cart cart = Cart.findCartByUser(highestBid.user);
       if (cart == null) {
         Cart newCart = new Cart();
         newCart.user = highestBid.user;
         newCart.save();
         CartItem cartItem = new CartItem(auctions.get(i).product, highestBid.user, newCart);
         cartItem.save();
       } else {
         CartItem cartItem = new CartItem(auctions.get(i).product, highestBid.user, cart);
         cartItem.save();
       }
       // Declaring string set that will contain emails of all users that have not had highest bid.
       Set<String> bidUsers = new HashSet<>();
       // Adding all user emails to the set.
       for (int j = 0; j < bids.size(); j++) {
         bidUsers.add(bids.get(j).user.email);
       }
       // Removing email of highest bit user from the set.
       bidUsers.remove(highestBid.user.email);
       // Declaring string variable that represents losing message.
       String losingMessage =
           "Biding for item #"
               + auctions.get(i).product.id
               + " - "
               + auctions.get(i).product.name
               + " is over.\n\r \n\r We are sorry to inform you that your bid was not enough.";
       // Declaring iterator list of all user emails.
       Iterator<String> iter = bidUsers.iterator();
       // Going trough all emails and sending message.
       while (iter.hasNext()) {
         User receiver = User.getUserByEmail(iter.next());
         Message msg =
             new Message(CommonHelpers.serviceUser(), receiver, "Auction results", losingMessage);
         msg.save();
       }
       // Setting auction status to inactive.
       auctions.get(i).isActive = false;
       auctions.get(i).update();
     }
   }
 }