BrightSide Workbench Full Report + Source Code
ContactsParser.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2014 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.parser;
20 
21 import java.io.IOException;
22 import java.util.Arrays;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import org.turro.string.ObjectString;
26 import org.turro.string.Strings;
27 import org.turro.annotation.ExternalParser;
28 import org.turro.auth.Authentication;
29 import org.turro.contacts.Contact;
30 import org.turro.contacts.ContactServiceRole;
31 import org.turro.contacts.content.ContactServiceContentIterator;
32 import org.turro.contacts.content.ConvocationContentIterator;
33 import org.turro.contacts.content.DirectoryContentIterator;
34 import org.turro.contacts.content.OldDirectoryContentIterator;
35 import org.turro.contacts.db.ContactsPU;
36 import org.turro.contacts.mydata.MyDataList;
37 import org.turro.contacts.profile.ProfileCtrl;
38 import org.turro.contacts.www.ContactIterator;
39 import org.turro.contacts.zul.register.SignUpCtrl;
40 import org.turro.elephant.context.ElephantContext;
41 import org.turro.elephant.context.IConstructor;
42 import org.turro.elephant.db.WhereClause;
43 import org.turro.jpa.search.DaoHtmlSearch;
44 import org.turro.plugin.contacts.IContact;
45 import org.turro.www.convocation.ConvocationCheck;
46 import org.turro.www.describeit.DescribeItCtrl;
47 import org.turro.www.groupit.GroupsTree;
48 
53 @ExternalParser
54 public class ContactsParser extends AbstractParser {
55 
56  @Override
57  public String getParserName() {
58  return "Contacts";
59  }
60 
61  @Override
62  protected boolean doExecute() {
63  boolean done = false;
64  if("groups".equals(getMacroName())) {
65  out.write(getGroups(constructor,
66  null,
67  null,
68  getToken(1),
69  getToken(2)));
70  done = true;
71  } else if("contacts".equals(getMacroName()) || "contacts-summary".equals(getMacroName())) {
73  (Integer) ObjectString.parseNativeString(getToken(1), Integer.class, true),
74  getToken(2),
75  getToken(3),
76  getToken(5),
77  getToken(4),
78  "contacts".equals(getMacroName())));
79  done = true;
80  } else if("contacts-by-group".equals(getMacroName()) || "contacts-by-group-summary".equals(getMacroName())) {
81  out.write(getContactByGroup(constructor,
82  (Integer) ObjectString.parseNativeString(getToken(1), Integer.class, true),
83  getToken(2),
84  getToken(3),
85  null,
86  "contacts-by-group".equals(getMacroName())));
87  done = true;
88  } else if("contactcount".equals(getMacroName())) {
89  Long count = getCount(constructor, getToken(1));
90  out.write(count != null ? count + "" : "0");
91  done = true;
92  } else if("contacts-connector".equals(getMacroName()) || "contacts-connector-summary".equals(getMacroName())) {
94  (Integer) ObjectString.parseNativeString(getToken(1), Integer.class, true),
95  getToken(2),
96  getToken(3),
97  getToken(4),
98  getToken(6),
99  getToken(5),
100  "contacts-connector".equals(getMacroName())));
101  done = true;
102  } else if("my-data".equals(getMacroName())) {
103  out.write(checkMyData() ? getToken(1) : getToken(2));
104  done = true;
105  } else if("describe-it".equals(getMacroName())) {
106  if(!Strings.isBlank(getToken(1))) {
108  dic.setEntityPath(getToken(1));
109  dic.renderDescriptions(getToken(2), true);
110  done = true;
111  }
112  } else if("convocation-check".equals(getMacroName())) {
114  if(tokens.length == 2) {
115  cc.setTemplate(getToken(1));
116  }
117  out.write(cc.check());
118  } else if("convocations".equals(getMacroName()) || "convocation-summary".equals(getMacroName()) ||
119  "public-convocations".equals(getMacroName()) || "public-convocation-summary".equals(getMacroName()) ||
120  "myconvocations".equals(getMacroName()) || "myconvocation-summary".equals(getMacroName())) {
123  Strings.isBlank(getToken(2)) ? null : ElephantContext.getRootWebPath() + getToken(2));
124  cci.setMode(getToken(1));
125  cci.setPage((Integer) ObjectString.parseNativeString(getToken(3), Integer.class, true));
126  cci.setAllowAttachments(true);
127  cci.setAllowComments(true);
129  if(getMacroName().contains("summary")) {
130  cci.setNavigatorTop(false);
131  cci.setNavigatorBottom(false);
132  }
133  if(getMacroName().contains("myconvocation")) {
134  cci.setRestricted(true);
135  }
136  if(getMacroName().contains("public-")) {
137  cci.setPublishable(true);
138  }
139  cci.load();
140  cci.render();
141  done = true;
142  } else if("old-directory".equals(getMacroName())) {
145  Strings.isBlank(getToken(1)) ? null : ElephantContext.getRootWebPath() + getToken(1));
146  dci.setPage((Integer) ObjectString.parseNativeString(getToken(2), Integer.class, true));
147  dci.setAllowAttachments(true);
148  dci.setAllowFiles(true);
149  dci.setAllowComments(true);
150  dci.setRestricted(true);
152  dci.load();
153  dci.render();
154  done = true;
155  } else if(getMacroName().startsWith("directory")) {
158  Strings.isBlank(getToken(2)) ? null : ElephantContext.getRootWebPath() + getToken(2));
159  pci.setPage((Integer) ObjectString.parseNativeString(getToken(3), Integer.class, true));
160  pci.setAllowAttachments(true);
161  pci.setAllowFiles(true);
162  pci.setRestricted(false);
163  pci.setTypes(getToken(1));
166  pci.load();
167  pci.render();
168  done = true;
169  } else if(getMacroName().startsWith("syndications")) {
172  Strings.isBlank(getToken(3)) ? null : ElephantContext.getRootWebPath() + getToken(3));
173  pci.setPage((Integer) ObjectString.parseNativeString(getToken(4), Integer.class, true));
174  pci.setAllowAttachments(true);
175  pci.setAllowFiles(true);
176  pci.setRestricted(false);
177  pci.setTypes(getToken(1));
178  pci.setSyndications(getToken(2));
181  pci.load();
182  pci.render();
183  done = true;
184  } else if("services".equals(getMacroName()) || "service-summary".equals(getMacroName()) ||
185  "myservices".equals(getMacroName()) || "myservice-summary".equals(getMacroName()) ||
186  "lastservices".equals(getMacroName()) || "lastservice-summary".equals(getMacroName()) ||
187  "mylastservices".equals(getMacroName()) || "mylastservice-summary".equals(getMacroName())) {
189  Strings.isBlank(getToken(3)) ? null : ElephantContext.getRootWebPath() + getToken(3));
190  String role = getToken(1);
191  csci.setRole("all".equals(role) ? null : ContactServiceRole.valueOf(role));
192  String type = getToken(2);
193  csci.setType("all".equals(type) ? null : type);
194  csci.setPage((Integer) ObjectString.parseNativeString(getToken(4), Integer.class, true));
195  csci.setAllowAttachments(true);
196  csci.setAllowComments(true);
197  csci.setSummaryTemplate(getToken(5));
198  if(getMacroName().contains("summary")) {
199  csci.setNavigatorTop(false);
200  csci.setNavigatorBottom(false);
201  }
202  if(getMacroName().contains("my")) {
203  csci.setRestricted(true);
204  }
205  if(getMacroName().contains("last")) {
206  csci.setLastEntries(true);
207  csci.setForceSummary(true);
208  }
209  csci.load();
210  csci.render();
211  done = true;
212  } else if("register".equals(getMacroName())) {
213  SignUpCtrl ctrl = new SignUpCtrl();
215  ctrl.setTemplate(getToken(1));
216  out.write(ctrl.parse(constructor));
217  } else if("profile".equals(getMacroName())) {
219  pci.setPage(100);
220  pci.setNavigatorTop(false);
221  pci.setNavigatorBottom(false);
222  pci.setAllowAttachments(true);
223  pci.setAllowFiles(true);
224  pci.setRestricted(false);
225  pci.setContactIds(Strings.toList(getToken(1), ";"));
228  pci.load();
229  pci.render();
230  done = true;
231  } else if("myprofile".equals(getMacroName())) {
232  ProfileCtrl ctrl = new ProfileCtrl();
234  ctrl.setTemplate(getToken(1));
235  ctrl.setContact(null); // forces current
236  out.write(ctrl.parse(constructor));
237  }
238  return done;
239  }
240 
241  @Override
242  protected void explainMacros(ParserMacroSet macros) {
243  macros.addMacro("groups")
244  .addParameter("context", false)
245  .addParameter("group-category", false);
246  macros.addMacro("contacts")
247  .addParameter("count", true, "10")
248  .addParameter("syndicationId", true)
249  .addParameter("summaryTemplate", true, "summary")
250  .addParameter("context", true)
251  .addParameter("fullTemplate", true, "full");
252  macros.addMacro("contacts-summary")
253  .copyParametersFrom("contacts", macros);
254  macros.addMacro("contacts-by-group")
255  .addParameter("count", true, "10")
256  .addParameter("groupings", true)
257  .addParameter("summaryTemplate", true, "summary");
258  macros.addMacro("contacts-by-group-summary")
259  .copyParametersFrom("contacts-by-group", macros);
260  macros.addMacro("contactcount")
261  .addParameter("syndicationId", true);
262  macros.addMacro("contacts-connector")
263  .addParameter("count", false, "10")
264  .addParameter("connector", false)
265  .addParameter("value", true, null, true)
266  .addParameter("summaryTemplate", true, "summary")
267  .addParameter("context", true)
268  .addParameter("fullTemplate", true, "full");
269  macros.addMacro("contacts-connector-summary")
270  .copyParametersFrom("contacts-connector", macros);
271  macros.addMacro("my-data")
272  .addParameter("valid", false)
273  .addParameter("not-valid", false);
274  macros.addMacro("describe-it")
275  .addParameter("entityPath", false)
276  .addParameter("id", true, "Default");
277  macros.addMacro("convocation-check")
278  .addParameter("template", true, "check");
279  macros.addMacro("convocations")
280  .addParameter("mode", false)
281  .addPossibleValue("open")
282  .addPossibleValue("closed")
283  .addPossibleValue("all")
284  .addParameter("context", true)
285  .addParameter("count", true, "10")
286  .addParameter("template", true, "summary");
287  macros.addMacro("convocation-summary")
288  .copyParametersFrom("convocations", macros);
289  macros.addMacro("myconvocations")
290  .copyParametersFrom("convocations", macros);
291  macros.addMacro("myconvocation-summary")
292  .copyParametersFrom("convocations", macros);
293  macros.addMacro("public-convocations")
294  .copyParametersFrom("convocations", macros);
295  macros.addMacro("public-convocation-summary")
296  .copyParametersFrom("convocations", macros);
297  macros.addMacro("old-directory")
298  .addParameter("context", true)
299  .addParameter("count", true, "12")
300  .addParameter("template", true, "summary");
301  macros.addMacro("directory")
302  .addParameter("type", true, "all", true)
303  .addPossibleValue("all")
304  .addPossibleValue("typed")
305  .addPossibleValue("companies")
306  .addPossibleValue("centers")
307  .addPossibleValue("professionals")
308  .addPossibleValue("educators")
309  .addPossibleValue("students")
310  .addPossibleValue("singletons")
311  .addParameter("context", true)
312  .addParameter("count", true, "12")
313  .addParameter("template", true, "summary");
314  macros.addDefaultsFor("directory", macros);
315  macros.addMacro("syndications")
316  .addParameter("type", true, "all", true)
317  .addPossibleValue("all")
318  .addPossibleValue("typed")
319  .addPossibleValue("companies")
320  .addPossibleValue("centers")
321  .addPossibleValue("professionals")
322  .addPossibleValue("educators")
323  .addPossibleValue("students")
324  .addPossibleValue("singletons")
325  .addParameter("syndicationId", true)
326  .addParameter("context", true)
327  .addParameter("count", true, "12")
328  .addParameter("template", true, "summary");
329  macros.addDefaultsFor("syndications", macros);
330  macros.addMacro("services")
331  .addParameter("role", false)
332  .addPossibleValue("SERVICE_SUPPLY")
333  .addPossibleValue("SERVICE_DEMAND")
334  .addParameter("type", false)
335  .addParameter("context", true)
336  .addParameter("count", true, "10")
337  .addParameter("template", true, "summary");
338  macros.addMacro("service-summary")
339  .copyParametersFrom("services", macros);
340  macros.addMacro("myservices")
341  .copyParametersFrom("services", macros);
342  macros.addMacro("myservice-summary")
343  .copyParametersFrom("services", macros);
344  macros.addMacro("lastservices")
345  .copyParametersFrom("services", macros);
346  macros.addMacro("lastservice-summary")
347  .copyParametersFrom("services", macros);
348  macros.addMacro("mylastservices")
349  .copyParametersFrom("services", macros);
350  macros.addMacro("mylastservice-summary")
351  .copyParametersFrom("services", macros);
352  macros.addMacro("register")
353  .addParameter("template", true, "signup");
354  macros.addMacro("profile")
355  .addParameter("contactIds", false)
356  .addParameter("template", true, "summary");
357  macros.addMacro("myprofile")
358  .addParameter("template", true, "profile");
359  }
360 
361  public Long getCount(IConstructor constructor, String syndicationId) {
362  WhereClause wc = new WhereClause();
363  wc.addClause("select count(c) from Contact as c");
364  if(!Strings.isBlank(syndicationId)) {
365  wc.addClause("join c.syndications as s");
366  wc.addClause("where s.name = :syndication");
367  wc.addNamedValue("syndication", syndicationId);
368  }
369  return (Long) new ContactsPU().getSingleResultOrNull(wc);
370  }
371 
372  public String getPublished(IConstructor constructor, int count, String syndicationId,
373  String summaryTemplate, String fullTemplate, String context, boolean navigator) {
374  try {
375  if(context == null) {
376  context = constructor.getCurrentContext().getPath();
377  }
381  pi.setNavigatorTop(navigator);
382  pi.setNavigatorBottom(navigator);
383  pi.setFullTemplate(fullTemplate);
384  pi.setSummaryTemplate(summaryTemplate);
385  pi.setPage(count);
386  pi.setSyndicationId(syndicationId);
387  pi.load();
388  pi.render();
389  constructor.setOut(null);
390  return constructor.getOutputString();
391  } catch (IOException ex) {
392  Logger.getLogger(ContactsParser.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
393  }
394  return null;
395  }
396 
397  public String getPublished(IConstructor constructor, int count, String connector, String values,
398  String summaryTemplate, String fullTemplate, String context, boolean navigator) {
399  try {
403  pi.setNavigatorTop(navigator);
404  pi.setNavigatorBottom(navigator);
405  pi.setFullTemplate(fullTemplate);
406  pi.setSummaryTemplate(summaryTemplate);
407  pi.setPage(count);
408  pi.setConnector(connector, values);
409  pi.load();
410  pi.render();
411  constructor.setOut(null);
412  return constructor.getOutputString();
413  } catch (IOException ex) {
414  Logger.getLogger(ContactsParser.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
415  }
416  return null;
417  }
418 
419  private String getContactByGroup(IConstructor constructor, int count, String groupings,
420  String summaryTemplate, String context, boolean navigator) {
421 
422  try {
426  pi.setNavigatorTop(navigator);
427  pi.setNavigatorBottom(navigator);
428  pi.setSummaryTemplate(summaryTemplate);
429  pi.setPage(count);
430  pi.setGroupings(Arrays.asList(groupings.split(",")));
431  pi.load();
432  pi.render();
433  constructor.setOut(null);
434  return constructor.getOutputString();
435  } catch (IOException ex) {
436  Logger.getLogger(ContactsParser.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
437  }
438  return null;
439  }
440 
441  public String getGroups(IConstructor constructor, String summaryTemplate, String fullTemplate, String context, String category) {
442  try {
444  GroupsTree pi = new GroupsTree(constructor, ElephantContext.getRootWebPath() + context, category);
445  pi.fillTree("");
446  pi.render();
447  constructor.setOut(null);
448  return constructor.getOutputString();
449  } catch (IOException ex) {
450  Logger.getLogger(ContactsParser.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
451  }
452  return null;
453  }
454 
455  private boolean checkMyData() {
456  IContact icontact = Authentication.getIContact();
457  if(icontact.isValid()) {
458  Object contact = icontact.getContact();
459  if(contact instanceof Contact) {
460  MyDataList myData = ((Contact) contact).getMyData();
461  return myData.isValid();
462  } else {
463  return false;
464  }
465  }
466  return true;
467  }
468 
469 }
void setSyndicationId(String syndicationId)
void setConnector(String connector, String values)
void addNamedValue(String name, Object value)
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419
void setAllowAttachments(boolean allowAttachments)
void setFullTemplate(String fullTemplate)
void setForceSummary(boolean forceSummary)
void setAllowComments(boolean allowComments)
void setNavigatorBottom(boolean navigatorBottom)
void setSummaryTemplate(String summaryTemplate)
void setNavigatorTop(boolean navigatorTop)
void fillTree(String path)
Definition: DaoTree.java:45
static void renderInstance(IConstructor constructor)
Long getCount(IConstructor constructor, String syndicationId)
String getPublished(IConstructor constructor, int count, String connector, String values, String summaryTemplate, String fullTemplate, String context, boolean navigator)
void explainMacros(ParserMacroSet macros)
String getGroups(IConstructor constructor, String summaryTemplate, String fullTemplate, String context, String category)
String getPublished(IConstructor constructor, int count, String syndicationId, String summaryTemplate, String fullTemplate, String context, boolean navigator)
void addDefaultsFor(String name, ParserMacroSet macros)
ParserMacro addMacro(String macroName)
ParserMacro addPossibleValue(String value)
void copyParametersFrom(String macroName, ParserMacroSet macros)
ParserMacro addParameter(String name, boolean optional)
void renderDescriptions(boolean container)