Example #1
0
  public void updateCalendar(Date date, Date fromHour, Date toHour) {
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(date);
    int day = cal.get(Calendar.DAY_OF_WEEK);
    DayEnum dayEnum = null;
    if (Calendar.MONDAY == day) {
      dayEnum = DayEnum.MONDAY;
    }
    if (Calendar.TUESDAY == day) {
      dayEnum = DayEnum.TUESDAY;
    }
    if (Calendar.WEDNESDAY == day) {
      dayEnum = DayEnum.WEDNESDAY;
    }
    if (Calendar.THURSDAY == day) {
      dayEnum = DayEnum.THURSDAY;
    }
    if (Calendar.FRIDAY == day) {
      dayEnum = DayEnum.FRIDAY;
    }
    if (Calendar.SUNDAY == day) {
      dayEnum = DayEnum.SUNDAY;
    }
    if (Calendar.SATURDAY == day) {
      dayEnum = DayEnum.SATURDAY;
    }

    for (DayRange dayRange : calendar) {

      if (dayRange.getDayEnum().equals(dayEnum)) {
        dayRange.setRange(fromHour, toHour);
        dayRange.save();
      }
    }
  }
Example #2
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;
  }