BrightSide Workbench Full Report + Source Code
IssueEventInfo.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.dossier.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.HashSet;
27 import java.util.List;
28 import org.amic.util.date.CheckDate;
29 import org.turro.string.Strings;
30 import org.turro.action.EntitiesInfo;
31 import org.turro.action.EntityInfoType;
32 import org.turro.action.LinkType;
33 import org.turro.annotation.ExternalEvent;
34 import org.turro.dossier.db.DossierPU;
35 import org.turro.dossier.entity.Issue;
36 import org.turro.dossier.issue.IssueWrapper;
37 import org.turro.dossier.search.CategoryResults;
38 import org.turro.elephant.context.Application;
39 import org.turro.elephant.db.WhereClause;
40 import org.turro.i18n.I_;
41 import org.turro.plugin.calendar.CalendarColor;
42 import org.turro.plugin.calendar.DefaultCalendarEvent;
43 import org.turro.plugin.calendar.ICalendarEvent;
44 import org.turro.plugin.calendar.ICalendarInfo;
45 import org.turro.www.describeit.DescribeItCtrl;
46 
51 @ExternalEvent
52 public class IssueEventInfo implements ICalendarInfo {
53 
54  /*
55  dossierCategories=1,...,n | all
56  required=(S) start, (C) control, (D) delivery, (R) resolution, (I) creation, TODO: (M) modification
57  */
58  @Override
59  public Collection<ICalendarEvent> execute(Date start, Date end, HashMap<String, String> args) {
60  return getEvents(start, end, args);
61  }
62 
63  private Collection<ICalendarEvent> getEvents(Date start, Date end, HashMap<String, String> args) {
64  ArrayList<ICalendarEvent> events = new ArrayList<>();
65  for(Issue issue : getIssues(start, end, args)) {
66  checkIssue(events, new IssueWrapper(issue), args);
67  }
68  return events;
69  }
70 
71  private List<Issue> getIssues(Date start, Date end, HashMap<String, String> args) {
72  String dossierCategories = args.get("dossierCategories");
73  String required = args.get("required");
74  WhereClause wc = new WhereClause();
75  wc.addClause("select distinct i from Issue as i");
76  wc.addClause("where i.publishable = TRUE");
77  wc.addClause("and (");
78  String join = "";
79  if(isRequired("S", required)) {
80  wc.addClause(join + " (i.startDate >= :start and i.startDate <= :end)");
81  join = "or";
82  }
83  if(isRequired("C", required)) {
84  wc.addClause(join + " (i.controlDate >= :start and i.controlDate <= :end)");
85  join = "or";
86  }
87  if(isRequired("D", required)) {
88  wc.addClause(join + " (i.delivery >= :start and i.delivery <= :end)");
89  join = "or";
90  }
91  if(isRequired("R", required)) {
92  wc.addClause(join + " (i.solvedDate >= :start and i.solvedDate <= :end)");
93  join = "or";
94  }
95  if(isRequired("I", required)) {
96  wc.addClause(join + " (i.issueDate >= :start and i.issueDate <= :end)");
97  join = "or";
98  }
99  if(isRequired("M", required)) {
100  wc.addClause(join + " (i.modification >= :start and i.modification <= :end)");
101  join = "or";
102  }
103  wc.addClause(")");
104  wc.addNamedValue("start", start);
105  wc.addNamedValue("end", end);
106  if(!Strings.isBlank(dossierCategories)) {
107  if(!"all".equals(dossierCategories)) {
108  CategoryResults.addExistsCategoryAffiliance(wc, dossierCategories, "i.dossier");
109  }
110  return new DossierPU().getResultList(Issue.class, wc);
111  } else {
112  return Collections.EMPTY_LIST;
113  }
114  }
115 
116  private void checkIssue(ArrayList<ICalendarEvent> events, IssueWrapper iw, HashMap<String, String> args) {
117  String required = args.get("required");
118  String target = args.get("target");
119  if("self".equals(target) && !(Application.getApplication().isInRole("issue:all") || iw.isFullParticipant())) {
120  return;
121  }
122  HashSet<Date> dates = new HashSet<>();
123  if(iw.getBeginDate()!= null && isRequired("S", required)) {
124  DefaultCalendarEvent calendarEvent = new DefaultCalendarEvent();
125  calendarEvent.setLocked(false);
126  calendarEvent.setDone(iw.getIssue().getStatus().isFinished());
127  calendarEvent.setHeaderColor(CalendarColor.RED.getHeader());
128  calendarEvent.setContentColor(CalendarColor.RED.getContent());
129  calendarEvent.setBeginDate(iw.getBeginDate());
130  dates.add(calendarEvent.getBeginDate());
131  if(isRequired("D", required) && new CheckDate(iw.getBeginDate()).sameDay(iw.getEndDate())) {
132  calendarEvent.setEndDate(iw.getEndDate());
133  dates.add(iw.getEndDate());
134  } else {
135  calendarEvent.setEndDate(calendarEvent.getBeginDate());
136  }
137  calendarEvent.setPath(DossierPU.getObjectPath(iw.getIssue()));
138  calendarEvent.setTitle(iw.getIssue().getDescription());
139  calendarEvent.setContent(getContent(iw));
140  calendarEvent.setMotive(I_.get("Start date"));
141  events.add(calendarEvent);
142  }
143  if(iw.getEndDate()!= null && !dates.contains(iw.getEndDate()) && isRequired("D", required)) {
144  DefaultCalendarEvent calendarEvent = new DefaultCalendarEvent();
145  calendarEvent.setLocked(false);
146  calendarEvent.setDone(iw.getIssue().getStatus().isFinished());
147  calendarEvent.setHeaderColor(CalendarColor.RED.getHeader());
148  calendarEvent.setContentColor(CalendarColor.RED.getContent());
149  calendarEvent.setBeginDate(iw.getEndDate());
150  dates.add(calendarEvent.getBeginDate());
151  calendarEvent.setEndDate(calendarEvent.getBeginDate());
152  calendarEvent.setPath(DossierPU.getObjectPath(iw.getIssue()));
153  calendarEvent.setTitle(iw.getIssue().getDescription());
154  calendarEvent.setContent(getContent(iw));
155  calendarEvent.setMotive(I_.get("End date"));
156  events.add(calendarEvent);
157  }
158  if(iw.getControlDate() != null && !dates.contains(iw.getControlDate()) && isRequired("C", required)) {
159  DefaultCalendarEvent calendarEvent = new DefaultCalendarEvent();
160  calendarEvent.setLocked(false);
161  calendarEvent.setDone(iw.getIssue().getStatus().isFinished());
162  calendarEvent.setHeaderColor(CalendarColor.RED.getHeader());
163  calendarEvent.setContentColor(CalendarColor.RED.getContent());
164  calendarEvent.setBeginDate(iw.getControlDate());
165  dates.add(calendarEvent.getBeginDate());
166  calendarEvent.setEndDate(calendarEvent.getBeginDate());
167  calendarEvent.setPath(DossierPU.getObjectPath(iw.getIssue()));
168  calendarEvent.setTitle(iw.getIssue().getDescription());
169  calendarEvent.setContent(getContent(iw));
170  calendarEvent.setMotive(I_.get("Control"));
171  events.add(calendarEvent);
172  }
173  if(iw.getIssue().getSolvedDate() != null && !dates.contains(iw.getIssue().getSolvedDate()) && isRequired("R", required)) {
174  DefaultCalendarEvent calendarEvent = new DefaultCalendarEvent();
175  calendarEvent.setLocked(false);
176  calendarEvent.setDone(iw.getIssue().getStatus().isFinished());
177  calendarEvent.setHeaderColor(CalendarColor.RED.getHeader());
178  calendarEvent.setContentColor(CalendarColor.RED.getContent());
179  calendarEvent.setBeginDate(iw.getIssue().getSolvedDate());
180  dates.add(calendarEvent.getBeginDate());
181  calendarEvent.setEndDate(calendarEvent.getBeginDate());
182  calendarEvent.setPath(DossierPU.getObjectPath(iw.getIssue()));
183  calendarEvent.setTitle(iw.getIssue().getDescription());
184  calendarEvent.setContent(getContent(iw));
185  calendarEvent.setMotive(I_.get("Closing"));
186  events.add(calendarEvent);
187  }
188  if(iw.getIssue().getIssueDate() != null && !dates.contains(iw.getIssue().getIssueDate()) && isRequired("I", required)) {
189  DefaultCalendarEvent calendarEvent = new DefaultCalendarEvent();
190  calendarEvent.setLocked(false);
191  calendarEvent.setDone(iw.getIssue().getStatus().isFinished());
192  calendarEvent.setHeaderColor(CalendarColor.RED.getHeader());
193  calendarEvent.setContentColor(CalendarColor.RED.getContent());
194  calendarEvent.setBeginDate(iw.getIssue().getIssueDate());
195  dates.add(calendarEvent.getBeginDate());
196  calendarEvent.setEndDate(calendarEvent.getBeginDate());
197  calendarEvent.setPath(DossierPU.getObjectPath(iw.getIssue()));
198  calendarEvent.setTitle(iw.getIssue().getDescription());
199  calendarEvent.setContent(getContent(iw));
200  calendarEvent.setMotive(I_.get("Creation"));
201  events.add(calendarEvent);
202  }
203  if(iw.getIssue().getModification() != null && !dates.contains(iw.getIssue().getModification()) && isRequired("M", required)) {
204  DefaultCalendarEvent calendarEvent = new DefaultCalendarEvent();
205  calendarEvent.setLocked(false);
206  calendarEvent.setDone(iw.getIssue().getStatus().isFinished());
207  calendarEvent.setHeaderColor(CalendarColor.RED.getHeader());
208  calendarEvent.setContentColor(CalendarColor.RED.getContent());
209  calendarEvent.setBeginDate(iw.getIssue().getModification());
210  dates.add(calendarEvent.getBeginDate());
211  calendarEvent.setEndDate(calendarEvent.getBeginDate());
212  calendarEvent.setPath(DossierPU.getObjectPath(iw.getIssue()));
213  calendarEvent.setTitle(iw.getIssue().getDescription());
214  calendarEvent.setContent(getContent(iw));
215  calendarEvent.setMotive(I_.get("Modification"));
216  events.add(calendarEvent);
217  }
218  }
219 
220  private String getContent(IssueWrapper iw) {
221  DescribeItCtrl dic = new DescribeItCtrl(Application.getApplication().getConstructor());
222  dic.setEntityPath(DossierPU.getObjectPath(iw.getIssue()));
223  HashMap<String, Object> args = new HashMap<>();
224  args.put("dic", dic);
225  return EntitiesInfo.getString(iw.getIssue(), iw, "issue", EntityInfoType.SUMMARY, LinkType.NONE, args);
226  }
227 
228  private boolean isRequired(String value, String param) {
229  if(!Strings.isBlank(value) && !Strings.isBlank(param)) {
230  return param.contains(value);
231  }
232  return true;
233  }
234 
235 }
Collection< ICalendarEvent > execute(Date start, Date end, HashMap< String, String > args)