19 package org.turro.contacts.service;
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;
52 public Collection<ICalendarEvent>
execute(Date start, Date end, HashMap<String, String> args) {
53 return getEvents(start, end, args);
56 private Collection<ICalendarEvent> getEvents(Date start, Date end, HashMap<String, String> args) {
57 ArrayList<ICalendarEvent> events =
new ArrayList<>();
59 checkService(events, service, args);
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)");
71 wc.addClause(
"(c.creation >= :start and c.creation <= :end)");
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 +
"%");
81 return new ContactsPU().getResultList(ContactService.class, wc);
83 return Collections.EMPTY_LIST;
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()))) {
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());
104 calendarEvent.setEndDate(calendarEvent.getBeginDate());
106 calendarEvent.setMotive(I_.get(
"Start date"));
108 calendarEvent.setBeginDate(service.getCreation());
109 calendarEvent.setEndDate(service.getCreation());
110 calendarEvent.setMotive(I_.get(
"Creation"));
112 calendarEvent.setPath(ContactsPU.getObjectPath(service));
113 calendarEvent.setTitle(service.getTitle());
114 calendarEvent.setContent(getContent(service));
115 events.add(calendarEvent);
119 private String getContent(ContactService service) {
120 return EntitiesInfo.getString(service,
new ContactServiceWrapper(service),
"service", EntityInfoType.SUMMARY, LinkType.WEB);