BrightSide Workbench Full Report + Source Code
Calendar.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2012 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.ical;
20 
21 import java.io.IOException;
22 import java.io.OutputStream;
23 import java.net.SocketException;
24 import java.net.URI;
25 import java.util.Date;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 import net.fortuna.ical4j.data.CalendarOutputter;
29 import net.fortuna.ical4j.model.DateTime;
30 import net.fortuna.ical4j.model.component.VEvent;
31 import net.fortuna.ical4j.model.parameter.Cn;
32 import net.fortuna.ical4j.model.parameter.Role;
33 import net.fortuna.ical4j.model.property.Attendee;
34 import net.fortuna.ical4j.model.property.CalScale;
35 import net.fortuna.ical4j.model.property.Completed;
36 import net.fortuna.ical4j.model.property.Description;
37 import net.fortuna.ical4j.model.property.Location;
38 import net.fortuna.ical4j.model.property.Organizer;
39 import net.fortuna.ical4j.model.property.ProdId;
40 import net.fortuna.ical4j.model.property.Status;
41 import net.fortuna.ical4j.model.property.Uid;
42 import net.fortuna.ical4j.model.property.Version;
43 import net.fortuna.ical4j.util.FixedUidGenerator;
44 import net.fortuna.ical4j.util.UidGenerator;
45 import org.turro.string.Strings;
46 import org.turro.action.Contacts;
47 import org.turro.elephant.context.ElephantContext;
48 
53 public class Calendar {
54 
55  private net.fortuna.ical4j.model.Calendar icsCalendar;
56  private VEvent currentEvent;
57  private Date endDate;
58 
59  public Calendar(String name) {
60  icsCalendar = new net.fortuna.ical4j.model.Calendar();
61  icsCalendar.getProperties().add(new ProdId(name));
62  icsCalendar.getProperties().add(Version.VERSION_2_0);
63  icsCalendar.getProperties().add(CalScale.GREGORIAN);
64  }
65 
66  public void addEvent(Date start, Date end, String name, String description) {
67  try {
68  currentEvent = new VEvent(new DateTime(start), new DateTime(end == null ? start : end), name);
69  currentEvent.getProperties().add(new Description(description));
70 
71  endDate = end;
72 
73  UidGenerator ug = new FixedUidGenerator("uidGen");
74  Uid uid = ug.generateUid();
75  currentEvent.getProperties().add(uid);
76 
77  icsCalendar.getComponents().add(currentEvent);
78  } catch (SocketException ex) {
79  Logger.getLogger(Calendar.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
80  }
81  }
82 
83  public void setOrganizer(String name, String email) {
84  URI umail;
85  try {
86  umail = URI.create("mailto:" + email);
87  } catch(Exception ex) {
88  umail = URI.create("mailto:" + Contacts.getNoEmail());
89  }
90  Organizer organizer = new Organizer(umail);
91  organizer.getParameters().add(new Cn(name));
92  currentEvent.getProperties().add(organizer);
93  }
94 
95  public void setCompleted() {
96  currentEvent.getProperties().add(new Completed(new DateTime(endDate)));
97  }
98 
99  public void setCancelled() {
100  currentEvent.getProperties().add(new Status("CANCELLED"));
101  }
102 
103  public void addAttendee(String name, String email) {
104  URI umail;
105  try {
106  umail = URI.create("mailto:" + email);
107  } catch(Exception ex) {
108  umail = URI.create("mailto:" + Contacts.getNoEmail());
109  }
110  Attendee attendee = new Attendee(umail);
111  attendee.getParameters().add(Role.REQ_PARTICIPANT);
112  attendee.getParameters().add(new Cn(name));
113  currentEvent.getProperties().add(attendee);
114  }
115 
116  public void addLocation(String address) {
117  if(!Strings.isBlank(address)) {
118  Location location = new Location(address);
119  currentEvent.getProperties().add(location);
120  }
121  }
122 
123  public void output(OutputStream out) {
124  try {
125  CalendarOutputter outputter = new CalendarOutputter();
126  outputter.output(icsCalendar, out);
127  } catch (IOException ex) {
128  Logger.getLogger(Calendar.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
129  }
130  }
131 
132 }
static String getNoEmail()
Definition: Contacts.java:104
void addEvent(Date start, Date end, String name, String description)
Definition: Calendar.java:66
void addLocation(String address)
Definition: Calendar.java:116
void output(OutputStream out)
Definition: Calendar.java:123
Calendar(String name)
Definition: Calendar.java:59
void addAttendee(String name, String email)
Definition: Calendar.java:103
void setOrganizer(String name, String email)
Definition: Calendar.java:83