BrightSide Workbench Full Report + Source Code
CrmCalendar.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.crm.command;
20 
21 import java.util.Date;
22 import java.util.List;
23 import org.amic.util.date.CheckDate;
24 import org.turro.string.ObjectString;
25 import org.turro.string.Strings;
26 import org.turro.annotation.ElephantPlugin;
27 import org.turro.crm.db.CrmPU;
28 import org.turro.crm.entity.SaleAction;
29 import org.turro.crm.entity.Vendor;
30 import org.turro.crm.entity.VendorProspect;
31 import org.turro.jpa.Dao;
32 import org.turro.plugin.calendar.CalendarColor;
33 import org.turro.plugin.calendar.DefaultCalendarEvent;
34 import org.turro.plugin.command.CalendarPlugin;
35 import org.turro.plugin.contacts.IContact;
36 import org.turro.util.Chars;
37 import org.turro.util.PhraseBuilder;
38 
44 public class CrmCalendar extends CalendarPlugin {
45 
46  @Override
47  protected void executePlugin() {
48  Dao dao = new CrmPU();
49  Vendor vendor = (Vendor) dao.getSingleResultOrNull(
50  "select v from Vendor v where v.idContact = ?",
51  new Object[] { getContact().getId() });
52  if(vendor != null) {
53  Date newFrom = new CheckDate(getFrom()).addDays(-7).getDate(),
54  newTo = new CheckDate(getTo()).addDays(7).getDate();
55  List<SaleAction> list = dao.getResultList(
56  "select sa from SaleAction sa " +
57  "where sa.vendorProspect.vendor = ? " +
58  "and (" +
59  "(sa.actionDate >= ? and sa.actionDate <= ?) " +
60  "or (sa.finalDate >= ? and sa.finalDate <= ?) " +
61  "or (sa.actionDate < ? and sa.finalDate > ?) " +
62  ")",
63  new Object[] { vendor,
64  newFrom, newTo,
65  newFrom, newTo,
66  getFrom(), getTo()
67  });
68  for(SaleAction sa : list) {
69  DefaultCalendarEvent calendarEvent = new DefaultCalendarEvent();
70  calendarEvent.setLocked(false);
71  calendarEvent.setDone(sa.getStatus() > 0);
72  if(sa.getStatus() > SaleAction.SA_PENDING) {
73  calendarEvent.setHeaderColor(CalendarColor.SILVER.getHeader());
74  calendarEvent.setContentColor(CalendarColor.SILVER.getContent());
75  } else {
76  calendarEvent.setHeaderColor(CalendarColor.GREEN.getHeader());
77  calendarEvent.setContentColor(CalendarColor.GREEN.getContent());
78  }
79  calendarEvent.setBeginDate(sa.getActionDate());
80  calendarEvent.setEndDate(sa.getFinalDate());
81  calendarEvent.setPath(CrmPU.getObjectPath(sa));
82  calendarEvent.setOrganizer(sa.getVendorProspect().getVendor().getIContact());
83  PhraseBuilder pb = new PhraseBuilder();
84  VendorProspect vp = sa.getVendorProspect();
85  pb.addWord(vp.getSaleProspect().getCustomer().getName());
86  pb.addPendingSeparator(Chars.forward().toString());
87  pb.addWord(vp.getSaleProspect().getDescription());
88  calendarEvent.setTitle(pb.toString());
89  pb.addPendingSeparator(Chars.nl().repeat(2).toString());
90  pb.addWord(sa.getComment());
91  pb.addPendingSeparator(Chars.nl().repeat(2).toString());
92  for(IContact contact : sa.getIAttendees()) {
93  pb.addWord(contact.getName());
94  pb.addPendingSeparator(", ");
95  calendarEvent.addAttendee(contact);
96  }
97  calendarEvent.setContent(pb.toString());
98  calendarEvent.setEventId(Strings.identifier("sa" + sa.getId() +
99  ObjectString.formatObject(calendarEvent.getBeginDate(), ObjectString.COMPRESSED_DATE_PATTERN, true)));
100  addResult(calendarEvent.getEventId(), calendarEvent);
101  }
102  }
103  }
104 
105 }
Object addResult(String key, Object value)
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419