BrightSide Workbench Full Report + Source Code
IssueCalendar.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2013 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.Collection;
22 import java.util.Date;
23 import java.util.EnumMap;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.Map;
27 import org.amic.util.date.CheckDate;
28 import org.turro.string.ObjectString;
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.ElephantPlugin;
34 import org.turro.dossier.db.DossierPU;
35 import org.turro.dossier.entity.IssueParticipant;
36 import org.turro.dossier.entity.IssueParticipantRole;
37 import org.turro.dossier.issue.IssueWrapper;
38 import org.turro.dossier.search.IssueResults;
39 import org.turro.dossier.zul.issue.IssueStage;
40 import org.turro.elephant.context.Application;
41 import org.turro.elephant.db.WhereClause;
42 import org.turro.i18n.I_;
43 import org.turro.path.Path;
44 import org.turro.plugin.calendar.CalendarColor;
45 import org.turro.plugin.calendar.DefaultCalendarEvent;
46 import org.turro.plugin.command.CalendarPlugin;
47 import org.turro.www.describeit.DescribeItCtrl;
48 
53 @ElephantPlugin
54 public class IssueCalendar extends CalendarPlugin {
55 
56  private Map<IssueStage, Integer> map;
57  private Collection<IssueWrapper> iwl;
58  //private RangeDate range;
59 
60  @Override
61  protected void executePlugin() {
62  //range = new RangeDate(getFrom(), getTo(), new Date());
63  pendingIssues();
64  int pending = map.get(IssueStage.ISSUE_STAGE_TO_DO) != null ?
65  map.get(IssueStage.ISSUE_STAGE_TO_DO) : 0;
66  pending += map.get(IssueStage.ISSUE_STAGE_DATE_SURPASSED) != null ?
68  if(pending > 0) {
69  DefaultCalendarEvent calendarEvent = new DefaultCalendarEvent();
70  calendarEvent.setLocked(true);
71  calendarEvent.setDone(false);
72  calendarEvent.setHeaderColor(CalendarColor.RED.getHeader());
73  calendarEvent.setContentColor(CalendarColor.RED.getContent());
74  calendarEvent.setBeginDate(new CheckDate(new Date()).setHour(8).setMinute(0).getDate());
75  calendarEvent.setEndDate(calendarEvent.getBeginDate());
76  calendarEvent.setPath("/myissues");
77  calendarEvent.setTitle(I_.get("My issues") + ": " + pending);
78  calendarEvent.setContent(getMyIssuesContent());
79  calendarEvent.setEventId("myissues");
80  addResult(calendarEvent.getEventId(), calendarEvent);
81  }
82  // cal afegir temes amb data
83  }
84 
85  private void pendingIssues() {
86  map = new EnumMap<>(IssueStage.class);
87  IssueResults ir = new IssueResults() {
88  @Override
89  protected void addClause(WhereClause wc) {
90  wc.addClause("and (");
91  wc.addClause("(issue.startDate >= :start and issue.startDate <= :end)");
92  wc.addClause("or (issue.controlDate >= :start and issue.controlDate <= :end)");
93  wc.addClause("or (issue.delivery >= :start and issue.delivery <= :end)");
94  wc.addClause("or (issue.solvedDate >= :start and issue.solvedDate <= :end)");
95  wc.addClause("or (issue.issueDate >= :start and issue.issueDate <= :end)");
96  wc.addClause("or (issue.modification >= :start and issue.modification <= :end)");
97  wc.addClause(")");
98  wc.addNamedValue("start", new CheckDate().addMonths(-3).getDate());
99  wc.addNamedValue("end", new CheckDate().addMonths(1).getDate());
100  }
101  };
103  iwl = ir.getIssueList();
104  for(IssueWrapper iw : iwl) {
105  IssueStage is = getStage(iw);
106  Integer i = map.get(is);
107  if(i == null) {
108  map.put(is, 1);
109  } else {
110  map.put(is, i + 1);
111  }
112  checkIssue(iw);
113  }
114  }
115 
116  //TODO: move to IssueWrapper. Also from IssueGrid.
117  private IssueStage getStage(IssueWrapper wissue) {
118 
119  if(wissue.isUnrelated()) {
120  return IssueStage.ISSUE_STAGE_UNRELATED;
121  }
122 
123  if(wissue.hasPassedNow()) {
124  return IssueStage.ISSUE_STAGE_DATE_SURPASSED;
125  }
126 
127  if(!wissue.hasPassedStartDate() || !wissue.canStartBySources()) {
128  return IssueStage.ISSUE_STAGE_TO_COME;
129  }
130 
131  int stage = wissue.getRelevanceOrderByContact();
132 
133  switch(stage) {
134  case 1: return IssueStage.ISSUE_STAGE_TO_DO;
135  case 2: return IssueStage.ISSUE_STAGE_REUNION;
136  case 5: return IssueStage.ISSUE_STAGE_FROZEN;
137  }
138 
139  return IssueStage.ISSUE_STAGE_TO_MONITOR;
140  }
141 
142  private void checkIssue(IssueWrapper iw) {
143  //if(range.inRange(iw.getBeginDate(), iw.getEndDate())) {}
144  HashSet<Date> dates = new HashSet<>();
145  if(iw.getBeginDate()!= null) {
146  DefaultCalendarEvent calendarEvent = new DefaultCalendarEvent();
147  calendarEvent.setLocked(false);
148  calendarEvent.setDone(iw.getIssue().getStatus().isFinished());
149  calendarEvent.setHeaderColor(CalendarColor.RED.getHeader());
150  calendarEvent.setContentColor(CalendarColor.RED.getContent());
151  calendarEvent.setBeginDate(iw.getBeginDate());
152  dates.add(calendarEvent.getBeginDate());
153  if(new CheckDate(iw.getBeginDate()).sameDay(iw.getEndDate())) {
154  calendarEvent.setEndDate(iw.getEndDate());
155  dates.add(iw.getEndDate());
156  } else {
157  calendarEvent.setEndDate(calendarEvent.getBeginDate());
158  }
159  calendarEvent.setPath(DossierPU.getObjectPath(iw.getIssue()));
160  calendarEvent.setTitle(iw.getIssue().getDescription());
161  calendarEvent.setContent(getContent(iw));
162  calendarEvent.setMotive(I_.get("Start date"));
163  addAttandees(calendarEvent, iw);
164  calendarEvent.setEventId(Strings.identifier("ib" + iw.getId() +
165  ObjectString.formatObject(calendarEvent.getBeginDate(), ObjectString.COMPRESSED_DATE_PATTERN, true)));
166  addResult(calendarEvent.getEventId(), calendarEvent);
167  }
168  if(iw.getEndDate()!= null && !dates.contains(iw.getEndDate())) {
169  DefaultCalendarEvent calendarEvent = new DefaultCalendarEvent();
170  calendarEvent.setLocked(false);
171  calendarEvent.setDone(iw.getIssue().getStatus().isFinished());
172  calendarEvent.setHeaderColor(CalendarColor.RED.getHeader());
173  calendarEvent.setContentColor(CalendarColor.RED.getContent());
174  calendarEvent.setBeginDate(iw.getEndDate());
175  dates.add(calendarEvent.getBeginDate());
176  calendarEvent.setEndDate(calendarEvent.getBeginDate());
177  calendarEvent.setPath(DossierPU.getObjectPath(iw.getIssue()));
178  calendarEvent.setTitle(iw.getIssue().getDescription());
179  calendarEvent.setContent(getContent(iw));
180  calendarEvent.setMotive(I_.get("End date"));
181  addAttandees(calendarEvent, iw);
182  calendarEvent.setEventId(Strings.identifier("ie" + iw.getId() +
183  ObjectString.formatObject(calendarEvent.getBeginDate(), ObjectString.COMPRESSED_DATE_PATTERN, true)));
184  addResult(calendarEvent.getEventId(), calendarEvent);
185  }
186  if(iw.getControlDate() != null && !dates.contains(iw.getControlDate())) {
187  DefaultCalendarEvent calendarEvent = new DefaultCalendarEvent();
188  calendarEvent.setLocked(false);
189  calendarEvent.setDone(iw.getIssue().getStatus().isFinished());
190  calendarEvent.setHeaderColor(CalendarColor.RED.getHeader());
191  calendarEvent.setContentColor(CalendarColor.RED.getContent());
192  calendarEvent.setBeginDate(iw.getControlDate());
193  dates.add(calendarEvent.getBeginDate());
194  calendarEvent.setEndDate(calendarEvent.getBeginDate());
195  calendarEvent.setPath(DossierPU.getObjectPath(iw.getIssue()));
196  calendarEvent.setTitle(iw.getIssue().getDescription());
197  calendarEvent.setContent(getContent(iw));
198  calendarEvent.setMotive(I_.get("Control"));
199  addAttandees(calendarEvent, iw);
200  calendarEvent.setEventId(Strings.identifier("ic" + iw.getId() +
201  ObjectString.formatObject(calendarEvent.getBeginDate(), ObjectString.COMPRESSED_DATE_PATTERN, true)));
202  addResult(calendarEvent.getEventId(), calendarEvent);
203  }
204  if(iw.getIssue().getSolvedDate() != null && !dates.contains(iw.getIssue().getSolvedDate())) {
205  DefaultCalendarEvent calendarEvent = new DefaultCalendarEvent();
206  calendarEvent.setLocked(false);
207  calendarEvent.setDone(iw.getIssue().getStatus().isFinished());
208  calendarEvent.setHeaderColor(CalendarColor.RED.getHeader());
209  calendarEvent.setContentColor(CalendarColor.RED.getContent());
210  calendarEvent.setBeginDate(iw.getIssue().getSolvedDate());
211  dates.add(calendarEvent.getBeginDate());
212  calendarEvent.setEndDate(calendarEvent.getBeginDate());
213  calendarEvent.setPath(DossierPU.getObjectPath(iw.getIssue()));
214  calendarEvent.setTitle(iw.getIssue().getDescription());
215  calendarEvent.setContent(getContent(iw));
216  calendarEvent.setMotive(I_.get("Closing"));
217  addAttandees(calendarEvent, iw);
218  calendarEvent.setEventId(Strings.identifier("ix" + iw.getId() +
219  ObjectString.formatObject(calendarEvent.getBeginDate(), ObjectString.COMPRESSED_DATE_PATTERN, true)));
220  addResult(calendarEvent.getEventId(), calendarEvent);
221  }
222  if(iw.getIssue().getIssueDate() != null && !dates.contains(iw.getIssue().getIssueDate())) {
223  DefaultCalendarEvent calendarEvent = new DefaultCalendarEvent();
224  calendarEvent.setLocked(false);
225  calendarEvent.setDone(iw.getIssue().getStatus().isFinished());
226  calendarEvent.setHeaderColor(CalendarColor.RED.getHeader());
227  calendarEvent.setContentColor(CalendarColor.RED.getContent());
228  calendarEvent.setBeginDate(iw.getIssue().getIssueDate());
229  dates.add(calendarEvent.getBeginDate());
230  calendarEvent.setEndDate(calendarEvent.getBeginDate());
231  calendarEvent.setPath(DossierPU.getObjectPath(iw.getIssue()));
232  calendarEvent.setTitle(iw.getIssue().getDescription());
233  calendarEvent.setContent(getContent(iw));
234  calendarEvent.setMotive(I_.get("Creation"));
235  addAttandees(calendarEvent, iw);
236  calendarEvent.setEventId(Strings.identifier("ir" + iw.getId() +
237  ObjectString.formatObject(calendarEvent.getBeginDate(), ObjectString.COMPRESSED_DATE_PATTERN, true)));
238  addResult(calendarEvent.getEventId(), calendarEvent);
239  }
240  if(iw.getIssue().getModification()!= null && !dates.contains(iw.getIssue().getModification())) {
241  DefaultCalendarEvent calendarEvent = new DefaultCalendarEvent();
242  calendarEvent.setLocked(false);
243  calendarEvent.setDone(iw.getIssue().getStatus().isFinished());
244  calendarEvent.setHeaderColor(CalendarColor.RED.getHeader());
245  calendarEvent.setContentColor(CalendarColor.RED.getContent());
246  calendarEvent.setBeginDate(iw.getIssue().getModification());
247  dates.add(calendarEvent.getBeginDate());
248  calendarEvent.setEndDate(calendarEvent.getBeginDate());
249  calendarEvent.setPath(DossierPU.getObjectPath(iw.getIssue()));
250  calendarEvent.setTitle(iw.getIssue().getDescription());
251  calendarEvent.setContent(getContent(iw));
252  calendarEvent.setMotive(I_.get("Modification"));
253  addAttandees(calendarEvent, iw);
254  calendarEvent.setEventId(Strings.identifier("im" + iw.getId() +
255  ObjectString.formatObject(calendarEvent.getBeginDate(), ObjectString.COMPRESSED_DATE_PATTERN, true)));
256  addResult(calendarEvent.getEventId(), calendarEvent);
257  }
258  }
259 
260  private String getContent(IssueWrapper iw) {
261  Path path = new Path(DossierPU.getObjectPath(iw.getIssue()));
262  DescribeItCtrl dic = new DescribeItCtrl(Application.getApplication().getConstructor());
263  dic.setEntityPath(path.getPath());
264  HashMap<String, Object> args = new HashMap<>();
265  args.put("dic", dic);
266  return EntitiesInfo.getTextString(iw.getIssue(), iw, "issue", EntityInfoType.SUMMARY, LinkType.NONE, args);
267  }
268 
269  private void addAttandees(DefaultCalendarEvent calendarEvent, IssueWrapper iw) {
270  for(IssueParticipant ip : iw.getIssue().getParticipants()) {
271  if(calendarEvent.getOrganizer() == null && ip.getRole().equals(IssueParticipantRole.ISSUE_REPORTER)) {
272  calendarEvent.setOrganizer(ip.getIContact());
273  } else {
274  calendarEvent.addAttendee(ip.getIContact());
275  }
276  }
277  }
278 
279  private String getMyIssuesContent() {
280  StringBuilder sb = new StringBuilder();
281  for(IssueWrapper iw : iwl) {
282  sb.append(iw.getIssue().getDescription())
283  .append(" (")
284  .append(I_.byKey(getStage(iw).toString()))
285  .append(")\n");
286  }
287  return sb.toString();
288  }
289 
290 }
Object addResult(String key, Object value)
java.util.List< IssueWrapper > getIssueList()
void setByParticipant(IContact byParticipant)
void addNamedValue(String name, Object value)
static String get(String msg)
Definition: I_.java:41