BrightSide Workbench Full Report + Source Code
PublicationEventInfo.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2015 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.publication.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.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.elephant.context.Application;
34 import org.turro.elephant.db.WhereClause;
35 import org.turro.plugin.calendar.CalendarColor;
36 import org.turro.plugin.calendar.DefaultCalendarEvent;
37 import org.turro.plugin.calendar.ICalendarEvent;
38 import org.turro.plugin.calendar.ICalendarInfo;
39 import org.turro.publication.db.PublicationPU;
40 import org.turro.publication.entity.Publication;
41 import org.turro.publication.util.PublicationWrapper;
42 
47 @ExternalEvent
48 public class PublicationEventInfo implements ICalendarInfo {
49 
50  /*
51  publicationCategories=1,...,n
52  publicationGroups=1,...,n
53  */
54  @Override
55  public Collection<ICalendarEvent> execute(Date start, Date end, HashMap<String, String> args) {
56  return getEvents(start, end, args);
57  }
58 
59  private Collection<ICalendarEvent> getEvents(Date start, Date end, HashMap<String, String> args) {
60  ArrayList<ICalendarEvent> events = new ArrayList<>();
61  for(Publication publication : getPublications(start, end, args)) {
62  checkPublication(events, publication, args);
63  }
64  return events;
65  }
66 
67  private List<Publication> getPublications(Date start, Date end, HashMap<String, String> args) {
68  String publicationCategories = args.get("publicationCategories");
69  String publicationGroups = args.get("publicationGroups");
70  WhereClause wc = new WhereClause();
71  wc.addClause("select p from Publication as p");
72  wc.addClause("where p.accepted = TRUE");
73  wc.addClause("and (p.date >= :start and p.date <= :end)");
74  wc.addNamedValue("start", start);
75  wc.addNamedValue("end", end);
76  boolean hasFilter = false;
77  if(!Strings.isBlank(publicationCategories)) {
78  if(!"all".equals(publicationCategories)) {
79  wc.addClause("and p.publicationCategory in (" + publicationCategories + ")");
80  }
81  hasFilter = true;
82  }
83  if(!Strings.isBlank(publicationGroups)) {
84  if(!"all".equals(publicationGroups)) {
85  wc.addClause("and p.publicationGroup in (" + publicationGroups + ")");
86  }
87  hasFilter = true;
88  }
89  if(hasFilter) {
90  return new PublicationPU().getResultList(Publication.class, wc);
91  } else {
92  return Collections.EMPTY_LIST;
93  }
94  }
95 
96  private void checkPublication(ArrayList<ICalendarEvent> events, Publication publication, HashMap<String, String> args) {
97  String target = args.get("target");
98  PublicationWrapper pw = new PublicationWrapper(publication);
99  if("self".equals(target) && !(Application.getApplication().isInRole("publication:all") || pw.isAuthor())) {
100  return;
101  }
102  if(publication.getDate() != null) {
103  DefaultCalendarEvent calendarEvent = new DefaultCalendarEvent();
104  calendarEvent.setLocked(false);
105  calendarEvent.setDone(false);
106  calendarEvent.setHeaderColor(CalendarColor.BLUE.getHeader());
107  calendarEvent.setContentColor(CalendarColor.BLUE.getContent());
108  calendarEvent.setBeginDate(new CheckDate(publication.getDate()).setHour(8).setMinute(0).getDate());
109  calendarEvent.setEndDate(calendarEvent.getBeginDate());
110  calendarEvent.setPath(PublicationPU.getObjectPath(publication));
111  calendarEvent.setTitle(publication.getTitle());
112  calendarEvent.setContent(getContent(publication));
113  events.add(calendarEvent);
114  }
115  }
116 
117  private String getContent(Publication publication) {
118  return EntitiesInfo.getString(publication, new PublicationWrapper(publication), "publication", EntityInfoType.SUMMARY, LinkType.NONE);
119  }
120 
121 }
Collection< ICalendarEvent > execute(Date start, Date end, HashMap< String, String > args)