Exemple #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;
  }
Exemple #2
0
  /** Preload all lanes and vehicles once. */
  @PostConstruct
  public void init() {
    System.out.println("Initializing MovementsDAOImpl");
    Query laneQuery = em.createQuery("select x from Lane x");
    lanes = laneQuery.getResultList();

    Query vehicleQuery = em.createQuery("select x from Vehicle x");
    vehicles = vehicleQuery.getResultList();

    generateLicensePlates(LICENSES);
  }