BrightSide Workbench Full Report + Source Code
CalendarExport.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2013 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.calendar.ical;
20 
21 import java.io.IOException;
22 import java.util.Collection;
23 import java.util.Date;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import javax.servlet.ServletContext;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 import org.amic.util.date.CheckDate;
30 import org.turro.string.Strings;
31 import org.turro.action.Actions;
32 import org.turro.action.Contacts;
33 import org.turro.action.Interceptors;
34 import org.turro.action.LinkType;
35 import org.turro.elephant.context.Application;
36 import org.turro.elephant.context.ElephantApplication;
37 import org.turro.elephant.context.ElephantContext;
38 import org.turro.elephant.direct.DirectContent;
39 import org.turro.elephant.direct.DirectContents;
40 import org.turro.elephant.direct.IDirectContent;
41 import org.turro.elephant.security.IUser;
42 import org.turro.i18n.I_;
43 import org.turro.ical.Calendar;
44 import org.turro.path.Path;
45 import org.turro.plugin.calendar.ICalendarEvent;
46 import org.turro.plugin.command.CalendarPlugin;
47 import org.turro.plugin.contacts.IContact;
48 
53 @DirectContent(identifier="calendar")
54 public class CalendarExport implements IDirectContent {
55 
56  public static String getIdentifier() {
57  return CalendarExport.class.getAnnotation(DirectContent.class).identifier();
58  }
59 
60  @Override
61  public boolean itsMe(String id) {
62  return getIdentifier().equals(id);
63  }
64 
65  @Override
66  public boolean myTurn(HttpServletRequest request) {
67  return DirectContents.isYourTurn(request, getIdentifier());
68  }
69 
70  @Override
71  public void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response) {
72  String contactId = request.getParameter("ses");
73  IContact contact = Contacts.getContactById(Strings.reveal(contactId));
74  if(contact.isValid()) {
75  Application app = ElephantApplication.getCurrent(request, response);
76  Date now = new Date();
77  Collection events = CalendarPlugin.getCalendarFor(
78  contact,
79  new CheckDate(now).addMonths(-3).getDate(),
80  new CheckDate(now).addMonths(1).getDate());
81  Calendar cal = new Calendar(contact.getName());
82  for(Object o : events) {
83  if(o instanceof ICalendarEvent) {
84  ICalendarEvent event = (ICalendarEvent) o;
85  String content = addLinks(event.getContent(), new Path(event.getPath()), contact.getConnector(IUser.CONNECTOR_EMAIL));
86  cal.addEvent(event.getBeginDate(), event.getEndDate(),
87  event.getTitle(),
88  content);
89  if(event.isDone()) {
90  cal.setCompleted();
91  } else if(event.isCancelled()) {
92  cal.setCompleted();
93  cal.setCancelled();
94  }
95  IContact organizer = event.getOrganizer() == null ? contact : event.getOrganizer();
96  if(organizer != null) {
97  cal.setOrganizer(organizer.getName(), organizer.getConnector(IUser.CONNECTOR_EMAIL));
98  }
99  for(IContact c : event.getAttendees()) {
100  cal.addAttendee(c.getName(), c.getConnector(IUser.CONNECTOR_EMAIL));
101  }
102  cal.addLocation(event.getAddress());
103  }
104  }
105  if(!events.isEmpty()) {
106  try {
107  response.setContentType("text/calendar");
108  response.setCharacterEncoding("UTF-8");
109  cal.output(response.getOutputStream());
110  } catch (IOException ex) {
111  Logger.getLogger(CalendarExport.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
112  }
113  }
114  }
115  }
116 
117  private String addLinks(String content, Path path, String email) {
118  String url = null;
119  content += "\n";
120  url = resolveLink("WEB", path, email);
121  if(url != null) {
122  content += "\n" + I_.get("See on the web") + " - " + url;
123  }
124  url = resolveLink("WEB_INTERNAL", path, email);
125  if(url != null) {
126  content += "\n" + I_.get("See on member area") + " - " + url;
127  }
128  url = resolveLink("INTERNAL", path, email);
129  if(url != null) {
130  content += "\n" + I_.get("See on the application") + " - " + url;
131  }
132  return content;
133  }
134 
135  public String resolveLink(String type, Path path, String email) {
136  LinkType link = LinkType.valueOf(type);
137  if(path.isRoot()) {
138  if(LinkType.INTERNAL.equals(link)) {
139  try {
140  return ElephantContext.getServerUrl("http") + "?" +
141  Actions.createAction(email,
142  "/app/frame?" + Interceptors.parameters(path.getRoot()).raw());
143  } catch (Exception ex) {
144  Logger.getLogger(CalendarExport.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
145  }
146  } else if(LinkType.WEB_INTERNAL.equals(link)) {
147  try {
148  return ElephantContext.getServerUrl("http") + "?" +
149  Actions.createAction(email,
150  "/user/" + path.getRoot());
151  } catch (Exception ex) {
152  Logger.getLogger(CalendarExport.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
153  }
154  } else if(LinkType.WEB.equals(link)) {
155  String context = ElephantContext.getEntityWebContext(path);
156  return !Strings.isBlank(context) ? ElephantContext.getServerUrl("http") +
157  context : null;
158  }
159  } else {
160  if(LinkType.INTERNAL.equals(link)) {
161  try {
162  return ElephantContext.getServerUrl("http") + "?" +
163  Actions.createAction(email,
164  "/app/frame?" + Interceptors.parameters(path.getRoot(), path.getNode(1)).raw());
165  } catch (Exception ex) {
166  Logger.getLogger(CalendarExport.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
167  }
168  } else if(LinkType.WEB_INTERNAL.equals(link)) {
169  try {
170  return ElephantContext.getServerUrl("http") + "?" +
171  Actions.createAction(email,
172  "/user/my" + path.getRoot() + "s?item=" + path.getNode(1));
173  } catch (Exception ex) {
174  Logger.getLogger(CalendarExport.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
175  }
176  } else if(LinkType.WEB.equals(link)) {
177  String context = ElephantContext.getEntityWebContext(path);
178  return !Strings.isBlank(context) ? ElephantContext.getServerUrl("http") +
179  context + "?item=" + path.getNode(1) : null;
180  }
181  }
182  return null;
183  }
184 
185 }
186 
static String createAction(String email, String redir)
Definition: Actions.java:90
static IContact getContactById(String id)
Definition: Contacts.java:72
static Parameters parameters(String root)
void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response)
boolean myTurn(HttpServletRequest request)
String resolveLink(String type, Path path, String email)
static Application getCurrent(HttpServletRequest request, HttpServletResponse response)
static String getServerUrl(String scheme)
static String getEntityWebContext(String path)
static boolean isYourTurn(HttpServletRequest request, String path)
static String get(String msg)
Definition: I_.java:41
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
void addAttendee(String name, String email)
Definition: Calendar.java:103
void setOrganizer(String name, String email)
Definition: Calendar.java:83
static Collection< Object > getCalendarFor(IContact contact, Date from, Date to)
static final String CONNECTOR_EMAIL
Definition: IUser.java:27