BrightSide Workbench Full Report + Source Code
ServiceEventInfo.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 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.contacts.service;
20 
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.Date;
25 import java.util.HashMap;
26 import java.util.List;
27 import org.amic.util.date.CheckDate;
28 import org.turro.string.Strings;
29 import org.turro.action.EntitiesInfo;
30 import org.turro.action.EntityInfoType;
31 import org.turro.action.LinkType;
32 import org.turro.annotation.ExternalEvent;
33 import org.turro.auth.Authentication;
34 import org.turro.contacts.ContactService;
35 import org.turro.contacts.db.ContactsPU;
36 import org.turro.elephant.context.Application;
37 import org.turro.elephant.db.WhereClause;
38 import org.turro.i18n.I_;
39 import org.turro.plugin.calendar.CalendarColor;
40 import org.turro.plugin.calendar.DefaultCalendarEvent;
41 import org.turro.plugin.calendar.ICalendarEvent;
42 import org.turro.plugin.calendar.ICalendarInfo;
43 
48 @ExternalEvent
49 public class ServiceEventInfo implements ICalendarInfo {
50 
51  @Override
52  public Collection<ICalendarEvent> execute(Date start, Date end, HashMap<String, String> args) {
53  return getEvents(start, end, args);
54  }
55 
56  private Collection<ICalendarEvent> getEvents(Date start, Date end, HashMap<String, String> args) {
57  ArrayList<ICalendarEvent> events = new ArrayList<>();
58  for(ContactService service : getServices(start, end, args)) {
59  checkService(events, service, args);
60  }
61  return events;
62  }
63 
64  private List<ContactService> getServices(Date start, Date end, HashMap<String, String> args) {
65  String serviceTypes = args.get("serviceTypes");
66  WhereClause wc = new WhereClause();
67  wc.addClause("select c from ContactService c");
68  wc.addClause("where (");
69  wc.addClause("(c.startDate >= :start and c.startDate <= :end)");
70  wc.addClause("or");
71  wc.addClause("(c.creation >= :start and c.creation <= :end)");
72  wc.addClause(")");
73  wc.addNamedValue("start", start);
74  wc.addNamedValue("end", end);
75  wc.addClause("and c.timesSent > 0");
76  if(!Strings.isBlank(serviceTypes)) {
77  if(!"all".equals(serviceTypes)) {
78  wc.addClause("and c.entityPath like :path");
79  wc.addNamedValue("path", serviceTypes + "%");
80  }
81  return new ContactsPU().getResultList(ContactService.class, wc);
82  } else {
83  return Collections.EMPTY_LIST;
84  }
85  }
86 
87  private void checkService(ArrayList<ICalendarEvent> events, ContactService service, HashMap<String, String> args) {
88  String target = args.get("target");
89  if("self".equals(target) && !(Application.getApplication().isInRole("contact:all") ||
90  service.isInBusiness(Authentication.getIContact()))) {
91  return;
92  }
93  if(service.getStartDate() != null || service.getCreation() != null) {
94  DefaultCalendarEvent calendarEvent = new DefaultCalendarEvent();
95  calendarEvent.setLocked(false);
96  calendarEvent.setDone(false);
97  calendarEvent.setHeaderColor(CalendarColor.KHAKI.getHeader());
98  calendarEvent.setContentColor(CalendarColor.KHAKI.getContent());
99  if(service.getStartDate() != null) {
100  calendarEvent.setBeginDate(service.getStartDate());
101  if(service.getEndDate() != null && new CheckDate(service.getStartDate()).sameDay(service.getEndDate())) {
102  calendarEvent.setEndDate(service.getEndDate());
103  } else {
104  calendarEvent.setEndDate(calendarEvent.getBeginDate());
105  }
106  calendarEvent.setMotive(I_.get("Start date"));
107  } else {
108  calendarEvent.setBeginDate(service.getCreation());
109  calendarEvent.setEndDate(service.getCreation());
110  calendarEvent.setMotive(I_.get("Creation"));
111  }
112  calendarEvent.setPath(ContactsPU.getObjectPath(service));
113  calendarEvent.setTitle(service.getTitle());
114  calendarEvent.setContent(getContent(service));
115  events.add(calendarEvent);
116  }
117  }
118 
119  private String getContent(ContactService service) {
120  return EntitiesInfo.getString(service, new ContactServiceWrapper(service), "service", EntityInfoType.SUMMARY, LinkType.WEB);
121  }
122 
123 }
Collection< ICalendarEvent > execute(Date start, Date end, HashMap< String, String > args)