Search in sources :

Example 1 with BookingBean

use of com.github.drbookings.core.datamodel.impl.BookingBean in project drbookings by DrBookings.

the class RunnableImportGoogleCalendar method process.

@Override
protected List<Booking> process(final IProgressMonitor monitor) throws Exception {
    try {
        if (logger.isInfoEnabled()) {
            logger.info("Adding from " + url);
        }
        // initialize the transport
        httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        // initialize the data store factory
        dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
        // authorization
        final Credential credential = authorize();
        // set up global Calendar instance
        final Calendar client = new com.google.api.services.calendar.Calendar.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName("drbookings").build();
        final CalendarList list = client.calendarList().list().execute();
        String id = null;
        for (final CalendarListEntry item : list.getItems()) {
            if (item.getSummary().contains("airbnb")) {
                id = item.getId();
            }
        }
        final Events feed = client.events().list(id).execute();
        final List<Booking> result = new ArrayList<>();
        for (final Event item : feed.getItems()) {
            System.out.println(item);
            result.add(new BookingBean(item.getSummary(), LocalDate.parse(item.getStart().getDate().toString()), LocalDate.parse(item.getEnd().getDate().toString())));
        }
        return result;
    } finally {
        monitor.done();
    }
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) Calendar(com.google.api.services.calendar.Calendar) ArrayList(java.util.ArrayList) Booking(com.github.drbookings.core.datamodel.api.Booking) BookingBean(com.github.drbookings.core.datamodel.impl.BookingBean) FileDataStoreFactory(com.google.api.client.util.store.FileDataStoreFactory) CalendarListEntry(com.google.api.services.calendar.model.CalendarListEntry) Events(com.google.api.services.calendar.model.Events) Event(com.google.api.services.calendar.model.Event) CalendarList(com.google.api.services.calendar.model.CalendarList)

Example 2 with BookingBean

use of com.github.drbookings.core.datamodel.impl.BookingBean in project drbookings by DrBookings.

the class RunnableImportCSVBooking method process.

@Override
protected List<BookingBean> process(final IProgressMonitor monitor) throws Exception {
    try {
        if (logger.isInfoEnabled()) {
            logger.info("Reading " + file);
        }
        final FileInputStream stream = new FileInputStream(file);
        final HSSFWorkbook workbook = new HSSFWorkbook(stream);
        final HSSFSheet sheet = workbook.getSheetAt(0);
        if (logger.isInfoEnabled()) {
            logger.info("Processing sheet " + sheet.getSheetName());
        }
        final int indexBookingNumber = getColumnIndexBookingNumber(sheet.getRow(0));
        final int indexClientName = getColumnIndexClientName(sheet.getRow(0));
        final int indexBookingCheckIn = getColumnIndexCheckIn(sheet.getRow(0));
        final int indexBookingCheckOut = getColumnIndexCheckOut(sheet.getRow(0));
        final int indexStatus = getColumnIndexStatus(sheet.getRow(0));
        final List<Integer> bookingNumbers = new ArrayList<>();
        final List<String> guestNames = new ArrayList<>();
        final List<String> stati = new ArrayList<>();
        final List<LocalDate> bookingCheckIn = new ArrayList<>();
        final List<LocalDate> bookingCheckOut = new ArrayList<>();
        for (final Row r : sheet) {
            // skip first row
            if (r.getRowNum() == 0) {
                continue;
            }
            bookingNumbers.add(getBookingNumber(r.getCell(indexBookingNumber)));
            guestNames.add(getString(r.getCell(indexClientName)));
            bookingCheckIn.add(getDate(r.getCell(indexBookingCheckIn)));
            bookingCheckOut.add(getDate(r.getCell(indexBookingCheckOut)));
            stati.add(getString(r.getCell(indexStatus)));
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Booking numbers: " + bookingNumbers);
            logger.debug("Guest names: " + guestNames);
            logger.debug("Check-in dates: " + bookingCheckIn);
            logger.debug("Check-out dates: " + bookingCheckOut);
        }
        if (logger.isInfoEnabled()) {
            logger.info("Building bookings.. ");
        }
        final List<BookingBean> bookings = new ArrayList<>();
        for (int i = 0; i < bookingNumbers.size(); i++) {
            final int number = bookingNumbers.get(i);
            final LocalDate checkIn = bookingCheckIn.get(i);
            final LocalDate checkOut = bookingCheckOut.get(i);
            final String names = guestNames.get(i);
            final String status = stati.get(i);
            bookings.add(new BookingBean(Integer.toString(number), checkIn, checkOut, Arrays.asList(names.split(","))).setStatus(status));
        }
        return bookings;
    } finally {
        monitor.done();
    }
}
Also used : ArrayList(java.util.ArrayList) LocalDate(java.time.LocalDate) FileInputStream(java.io.FileInputStream) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) BookingBean(com.github.drbookings.core.datamodel.impl.BookingBean) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) Row(org.apache.poi.ss.usermodel.Row)

Aggregations

BookingBean (com.github.drbookings.core.datamodel.impl.BookingBean)2 ArrayList (java.util.ArrayList)2 Booking (com.github.drbookings.core.datamodel.api.Booking)1 Credential (com.google.api.client.auth.oauth2.Credential)1 FileDataStoreFactory (com.google.api.client.util.store.FileDataStoreFactory)1 Calendar (com.google.api.services.calendar.Calendar)1 CalendarList (com.google.api.services.calendar.model.CalendarList)1 CalendarListEntry (com.google.api.services.calendar.model.CalendarListEntry)1 Event (com.google.api.services.calendar.model.Event)1 Events (com.google.api.services.calendar.model.Events)1 FileInputStream (java.io.FileInputStream)1 LocalDate (java.time.LocalDate)1 HSSFRow (org.apache.poi.hssf.usermodel.HSSFRow)1 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)1 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)1 Row (org.apache.poi.ss.usermodel.Row)1