BrightSide Workbench Full Report + Source Code
ContactsEntityInfo.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.io.IOException;
22 import java.io.Writer;
23 import java.util.HashMap;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import org.turro.string.Strings;
27 import org.turro.action.EntitiesInfo;
28 import org.turro.action.EntityInfoType;
29 import org.turro.action.IEntityInfo;
30 import org.turro.action.Interceptors;
31 import org.turro.action.LinkType;
32 import org.turro.annotation.EntityInfo;
33 import org.turro.auth.Authentication;
34 import org.turro.contacts.Contact;
35 import org.turro.contacts.ContactService;
36 import org.turro.contacts.Convocation;
37 import org.turro.contacts.db.ContactsPU;
38 import org.turro.contacts.form.ContactWrapper;
39 import org.turro.contacts.service.ContactServiceWrapper;
40 import org.turro.elephant.context.ElephantContext;
41 import org.turro.path.Path;
42 import org.turro.plugin.contacts.IContact;
43 import org.turro.zul.convocation.ConvocationWrapper;
44 
50 public class ContactsEntityInfo implements IEntityInfo {
51 
52  private Path path;
53 
54  @Override
55  public boolean writeEntityInfo(Writer out, Path path, EntityInfoType type, LinkType link) {
56  this.path = path;
57  if("convocation".equals(path.getRoot())) {
58  try {
59  Convocation convocation = new ContactsPU().find(Convocation.class, path.getNode(1));
60  HashMap<String, Object> args = new HashMap<>();
61  args.put("entityPath", path.getPath());
62  args.put("resolver", this);
63  out.write(EntitiesInfo.getString(convocation, new ConvocationWrapper(convocation), "convocation", type, link, args));
64  return true;
65  } catch (IOException ex) {
66  Logger.getLogger(ContactsEntityInfo.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
67  }
68  } else if("service".equals(path.getRoot())) {
69  try {
70  ContactService service = new ContactsPU().find(ContactService.class, path.getNode(1));
71  HashMap<String, Object> args = new HashMap<>();
72  args.put("entityPath", path.getPath());
73  args.put("resolver", this);
74  out.write(EntitiesInfo.getString(service, new ContactServiceWrapper(service), "service", type, link, args));
75  return true;
76  } catch (IOException ex) {
77  Logger.getLogger(ContactsEntityInfo.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
78  }
79  } else if("contact".equals(path.getRoot())) {
80  try {
81  Contact contact = new ContactsPU().find(Contact.class, path.getNode(1));
82  HashMap<String, Object> args = new HashMap<>();
83  args.put("entityPath", path.getPath());
84  args.put("resolver", this);
85  out.write(EntitiesInfo.getString(contact, new ContactWrapper(contact), "contact", type, link, args));
86  return true;
87  } catch (IOException ex) {
88  Logger.getLogger(ContactsEntityInfo.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
89  }
90  }
91  return false;
92  }
93 
94  @Override
95  public String resolveLink(String type) {
96  LinkType link = LinkType.valueOf(type);
98  return linkForPath(path, contact, link);
99  }
100 
101  public static String linkForPath(Path path, IContact contact, LinkType type) {
102  if(LinkType.INTERNAL.equals(type) && contact.isWebapp()) {
103  return "/app/frame?" + Interceptors.parameters(path.getRoot(), path.getNode(1)).encoded();
104  } else if(LinkType.WEB_INTERNAL.equals(type)) {
105  return "/user/services?item=" + path.getNode(1);
106  } else if(LinkType.WEB.equals(type)) {
107  String context = ElephantContext.getEntityWebContext(path);
108  return !Strings.isBlank(context) ? context + "?item=" + path.getNode(1) : null;
109  }
110  return null;
111  }
112 
113 }
static String getString(Object entity, Object wrapper, String label, EntityInfoType type, LinkType link)
static Parameters parameters(String root)
boolean writeEntityInfo(Writer out, Path path, EntityInfoType type, LinkType link)
static String linkForPath(Path path, IContact contact, LinkType type)
static String getEntityWebContext(String path)