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).

1  
!7
2  
3  
lib 1400446 // caldav4j
4  
5  
import net.fortuna.ical4j.model.Calendar;
6  
import net.fortuna.ical4j.model.Component;
7  
import net.fortuna.ical4j.model.ComponentList;
8  
import net.fortuna.ical4j.model.component.VEvent;
9  
import net.fortuna.ical4j.model.Date;
10  
import net.fortuna.ical4j.util.UidGenerator;
11  
import org.apache.commons.httpclient.HostConfiguration;
12  
import org.apache.commons.httpclient.UsernamePasswordCredentials;
13  
import org.apache.commons.httpclient.auth.AuthScope;
14  
import org.osaf.caldav4j.CalDAVCollection;
15  
import org.osaf.caldav4j.CalDAVConstants;
16  
import org.osaf.caldav4j.exceptions.CalDAV4JException;
17  
import org.osaf.caldav4j.methods.CalDAV4JMethodFactory;
18  
import org.osaf.caldav4j.methods.HttpClient;
19  
import org.osaf.caldav4j.model.request.CalendarQuery;
20  
import org.osaf.caldav4j.util.GenerateQuery;
21  
22  
cprint {
23  
  transient Calendar mainCalendar;
24  
  transient CalDAVCollection collection;
25  
  transient HttpClient httpClient;
26  
  
27  
  double calcDuration(VEvent ve) {
28  
    return (    ve.getEndDate().getDate().getTime()
29  
            - ve.getStartDate().getDate().getTime())
30  
           / (1000. * 60. * 60.);
31  
  }
32  
33  
  start-thread {
34  
    httpClient = new HttpClient;
35  
    httpClient.getHostConfiguration().setHost("localhost", 5232, "http");
36  
    String username = "stefan";
37  
    UsernamePasswordCredentials httpCredentials = new(username, "bla");
38  
    httpClient.getState().setCredentials(AuthScope.ANY, httpCredentials);
39  
    httpClient.getParams().setAuthenticationPreemptive(true);
40  
41  
    collection = new CalDAVCollection(
42  
        //"/dav/"+ username +"/Calendar",
43  
        "/stefan/cbedc050-528e-3102-6f59-2bda1287cb87/",
44  
        (HostConfiguration) httpClient.getHostConfiguration().clone(),
45  
        new CalDAV4JMethodFactory(),
46  
        CalDAVConstants.PROC_ID_DEFAULT
47  
        );
48  
49  
    new GenerateQuery gq;
50  
    // filter by date
51  
    gq.setFilter("VEVENT [20201001T000000Z;20211010T000000Z] : STATUS!=CANCELLED");
52  
    // Get the raw caldav query
53  
    // System.out.println("Query: "+ gq.prettyPrint());
54  
    CalendarQuery calendarQuery = gq.generate();
55  
    L<Calendar> calendars = collection.queryCalendars(httpClient, calendarQuery);
56  
    print("Have " + n2(calendars, "calendar"));
57  
    mainCalendar = first(calendars);
58  
59  
    for (Calendar calendar : calendars) {
60  
      ComponentList componentList = calendar.getComponents().getComponents(Component.VEVENT);
61  
      Iterator<VEvent> eventIterator = componentList.iterator();
62  
      while (eventIterator.hasNext()) {
63  
        VEvent ve = eventIterator.next();
64  
        print("Event: "+ ve.toString());
65  
        print("Duration (h): "+ String.format("%.2f", calcDuration(ve)));
66  
        print("\n\n");
67  
      }
68  
    }
69  
    
70  
    print("done");
71  
  }
72  
  
73  
  void createChristmas ctex {
74  
    java.util.Calendar calendar = java.util.Calendar.getInstance();
75  
    calendar.set(java.util.Calendar.MONTH, java.util.Calendar.DECEMBER);
76  
    calendar.set(java.util.Calendar.DAY_OF_MONTH, 25);
77  
    
78  
    // initialise as an all-day event..
79  
    VEvent christmas = new VEvent(new Date(calendar.getTime()), "Christmas Day");
80  
    
81  
    // Generate a UID for the event..
82  
    UidGenerator ug = new("1");
83  
    christmas.getProperties().add(ug.generateUid());
84  
    
85  
    mainCalendar.getComponents().add(christmas);
86  
    print("Christmas added: " + christmas);
87  
  }
88  
  
89  
  void createCalendar ctex {
90  
    collection.createCalendar(httpClient);
91  
  }
92  
}

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: 399 / 524
Version history: 10 change(s)
Referenced in: [show references]