Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

92
LINES

< > BotCompany Repo | #1030397 // caldav4j test for local Radicale server

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 21956K of libraries. Click here for Pure Java version (2230L/12K).

!7

lib 1400446 // caldav4j

import net.fortuna.ical4j.model.Calendar;
import net.fortuna.ical4j.model.Component;
import net.fortuna.ical4j.model.ComponentList;
import net.fortuna.ical4j.model.component.VEvent;
import net.fortuna.ical4j.model.Date;
import net.fortuna.ical4j.util.UidGenerator;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.osaf.caldav4j.CalDAVCollection;
import org.osaf.caldav4j.CalDAVConstants;
import org.osaf.caldav4j.exceptions.CalDAV4JException;
import org.osaf.caldav4j.methods.CalDAV4JMethodFactory;
import org.osaf.caldav4j.methods.HttpClient;
import org.osaf.caldav4j.model.request.CalendarQuery;
import org.osaf.caldav4j.util.GenerateQuery;

cprint {
  transient Calendar mainCalendar;
  transient CalDAVCollection collection;
  transient HttpClient httpClient;
  
  double calcDuration(VEvent ve) {
    return (    ve.getEndDate().getDate().getTime()
            - ve.getStartDate().getDate().getTime())
           / (1000. * 60. * 60.);
  }

  start-thread {
    httpClient = new HttpClient;
    httpClient.getHostConfiguration().setHost("localhost", 5232, "http");
    String username = "stefan";
    UsernamePasswordCredentials httpCredentials = new(username, "bla");
    httpClient.getState().setCredentials(AuthScope.ANY, httpCredentials);
    httpClient.getParams().setAuthenticationPreemptive(true);

    collection = new CalDAVCollection(
        //"/dav/"+ username +"/Calendar",
        "/stefan/cbedc050-528e-3102-6f59-2bda1287cb87/",
        (HostConfiguration) httpClient.getHostConfiguration().clone(),
        new CalDAV4JMethodFactory(),
        CalDAVConstants.PROC_ID_DEFAULT
        );

    new GenerateQuery gq;
    // filter by date
    gq.setFilter("VEVENT [20201001T000000Z;20211010T000000Z] : STATUS!=CANCELLED");
    // Get the raw caldav query
    // System.out.println("Query: "+ gq.prettyPrint());
    CalendarQuery calendarQuery = gq.generate();
    L<Calendar> calendars = collection.queryCalendars(httpClient, calendarQuery);
    print("Have " + n2(calendars, "calendar"));
    mainCalendar = first(calendars);

    for (Calendar calendar : calendars) {
      ComponentList componentList = calendar.getComponents().getComponents(Component.VEVENT);
      Iterator<VEvent> eventIterator = componentList.iterator();
      while (eventIterator.hasNext()) {
        VEvent ve = eventIterator.next();
        print("Event: "+ ve.toString());
        print("Duration (h): "+ String.format("%.2f", calcDuration(ve)));
        print("\n\n");
      }
    }
    
    print("done");
  }
  
  void createChristmas ctex {
    java.util.Calendar calendar = java.util.Calendar.getInstance();
    calendar.set(java.util.Calendar.MONTH, java.util.Calendar.DECEMBER);
    calendar.set(java.util.Calendar.DAY_OF_MONTH, 25);
    
    // initialise as an all-day event..
    VEvent christmas = new VEvent(new Date(calendar.getTime()), "Christmas Day");
    
    // Generate a UID for the event..
    UidGenerator ug = new("1");
    christmas.getProperties().add(ug.generateUid());
    
    mainCalendar.getComponents().add(christmas);
    print("Christmas added: " + christmas);
  }
  
  void createCalendar ctex {
    collection.createCalendar(httpClient);
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1030397
Snippet name: caldav4j test for local Radicale server
Eternal ID of this version: #1030397/11
Text MD5: 18deea8b4fdcc8f19477827f55abc779
Transpilation MD5: 3281001411859da33224627c6eea0eef
Author: stefan
Category: javax / stefan's os
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-12-11 17:06:16
Source code size: 3432 bytes / 92 lines
Pitched / IR pitched: No / No
Views / Downloads: 397 / 522
Version history: 10 change(s)
Referenced in: [show references]