BrightSide Workbench Full Report + Source Code
ConvocationEventInfo.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2017 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.command;
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.turro.string.Strings;
28 import org.turro.action.EntitiesInfo;
29 import org.turro.action.EntityInfoType;
30 import org.turro.action.LinkType;
31 import org.turro.annotation.ExternalEvent;
32 import org.turro.auth.Authentication;
33 import org.turro.contacts.Convocation;
34 import org.turro.contacts.db.ContactsPU;
35 import org.turro.elephant.context.Application;
36 import org.turro.elephant.db.WhereClause;
37 import org.turro.plugin.calendar.CalendarColor;
38 import org.turro.plugin.calendar.DefaultCalendarEvent;
39 import org.turro.plugin.calendar.ICalendarEvent;
40 import org.turro.plugin.calendar.ICalendarInfo;
41 import org.turro.zul.convocation.ConvocationWrapper;
42 
47 @ExternalEvent
48 public class ConvocationEventInfo implements ICalendarInfo {
49 
50  @Override
51  public Collection<ICalendarEvent> execute(Date start, Date end, HashMap<String, String> args) {
52  return getEvents(start, end, args);
53  }
54 
55  private Collection<ICalendarEvent> getEvents(Date start, Date end, HashMap<String, String> args) {
56  ArrayList<ICalendarEvent> events = new ArrayList<>();
57  for(Convocation convocation : getConvocations(start, end, args)) {
58  checkConvocation(events, convocation, args);
59  }
60  return events;
61  }
62 
63  private List<Convocation> getConvocations(Date start, Date end, HashMap<String, String> args) {
64  String convocationPath = args.get("convocationPaths");
65  WhereClause wc = new WhereClause();
66  wc.addClause("select c from Convocation c");
67  wc.addClause("where c.callDate >= :start");
68  wc.addClause("and c.callDate <= :end");
69  wc.addNamedValue("start", start);
70  wc.addNamedValue("end", end);
71  if(!Strings.isBlank(convocationPath)) {
72  if(!"all".equals(convocationPath)) {
73  wc.addClause("and c.entityPath like :path");
74  wc.addNamedValue("path", convocationPath + "%");
75  }
76  return new ContactsPU().getResultList(Convocation.class, wc);
77  } else {
78  return Collections.EMPTY_LIST;
79  }
80  }
81 
82  private void checkConvocation(ArrayList<ICalendarEvent> events, Convocation convocation, HashMap<String, String> args) {
83  String target = args.get("target");
84  if(!convocation.isPublishable() && "self".equals(target) &&
85  !(Application.getApplication().isInRole("convocation:all") || convocation.isAssistant(Authentication.getIContact()))) {
86  return;
87  }
88  if(convocation.getCallDate()!= null) {
89  DefaultCalendarEvent calendarEvent = new DefaultCalendarEvent();
90  calendarEvent.setLocked(false);
91  calendarEvent.setDone(false);
92  calendarEvent.setHeaderColor(CalendarColor.PURPLE.getHeader());
93  calendarEvent.setContentColor(CalendarColor.PURPLE.getContent());
94  calendarEvent.setBeginDate(convocation.getCallDate());
95  calendarEvent.setEndDate(convocation.getEndDate());
96  calendarEvent.setPath(ContactsPU.getObjectPath(convocation));
97  calendarEvent.setTitle(convocation.getName());
98  calendarEvent.setContent(getContent(convocation));
99  events.add(calendarEvent);
100  }
101  }
102 
103  private String getContent(Convocation convocation) {
104  return EntitiesInfo.getString(convocation, new ConvocationWrapper(convocation), "convocation", EntityInfoType.SUMMARY, LinkType.NONE);
105  }
106 
107 }
Collection< ICalendarEvent > execute(Date start, Date end, HashMap< String, String > args)