BrightSide Workbench Full Report + Source Code
VendorCalendar.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.vendor;
20 
21 import java.io.IOException;
22 import java.util.List;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import javax.servlet.ServletContext;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28 import org.amic.util.date.CheckDate;
29 import org.turro.contacts.Address;
30 import org.turro.crm.db.CrmPU;
31 import org.turro.crm.entity.SaleAction;
32 import org.turro.crm.entity.Vendor;
33 import org.turro.elephant.context.ElephantContext;
34 import org.turro.elephant.direct.DirectContent;
35 import org.turro.elephant.direct.DirectContents;
36 import org.turro.elephant.direct.IDirectContent;
37 import org.turro.elephant.security.IUser;
38 import org.turro.ical.Calendar;
39 import org.turro.jpa.Dao;
40 import org.turro.plugin.contacts.IContact;
41 
46 @DirectContent(identifier="vendorcal")
47 public class VendorCalendar implements IDirectContent {
48 
49  public static String getIdentifier() {
50  return VendorCalendar.class.getAnnotation(DirectContent.class).identifier();
51  }
52 
53  @Override
54  public boolean itsMe(String id) {
55  return getIdentifier().equals(id);
56  }
57 
58  @Override
59  public boolean myTurn(HttpServletRequest request) {
60  return DirectContents.isYourTurn(request, getIdentifier());
61  }
62 
63  @Override
64  public void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response) {
65  String vendorId = request.getParameter("ses");
66  Dao dao = new CrmPU();
67  Vendor vendor = (Vendor) dao.getSingleResult(
68  "select v from Vendor v where v.idContact = ?",
69  new Object[] { vendorId.substring(3) });
70  Calendar cal = new Calendar(vendor.getName());
71  List<SaleAction> list = dao.getResultList(
72  "select sa from SaleAction sa " +
73  "where sa.vendorProspect.vendor = ? " +
74  "and sa.actionDate >= ?",
75  new Object[] { vendor, new CheckDate().addYears(-1).getDate() });
76  for(SaleAction sa : list) {
77  cal.addEvent(sa.getActionDate(), sa.getFinalDate(),
78  sa.getVendorProspect().getSaleProspect().getCustomer().getName(),
79  sa.getComment());
80  if(sa.getStatus() == SaleAction.SA_DONE) {
81  cal.setCompleted();
82  } else if(sa.getStatus() == SaleAction.SA_CANCELLED) {
83  cal.setCompleted();
84  cal.setCancelled();
85  }
86  cal.setOrganizer(vendor.getName(), vendor.getIContact().getConnector(IUser.CONNECTOR_EMAIL));
87  for(IContact c : sa.getIAttendees()) {
88  cal.addAttendee(c.getName(), c.getConnector(IUser.CONNECTOR_EMAIL));
89  }
90  Address address = sa.getAddress();
91  if(address != null) cal.addLocation(address.getAddressString());
92  }
93  try {
94  response.setContentType("text/calendar");
95  response.setCharacterEncoding("UTF-8");
96  cal.output(response.getOutputStream());
97  } catch (IOException ex) {
98  Logger.getLogger(VendorCalendar.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
99  }
100  }
101 
102 }
static boolean isYourTurn(HttpServletRequest request, String path)
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
Object getSingleResult(WhereClause wc)
Definition: Dao.java:380
void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response)
boolean myTurn(HttpServletRequest request)