BrightSide Workbench Full Report + Source Code
PublicationEntityInfo.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.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.elephant.context.ElephantContext;
35 import org.turro.path.Path;
36 import org.turro.plugin.contacts.IContact;
37 import org.turro.publication.db.PublicationPU;
38 import org.turro.publication.entity.Publication;
39 import org.turro.publication.util.PublicationWrapper;
40 
46 public class PublicationEntityInfo implements IEntityInfo {
47 
48  private Path path;
49 
50  @Override
51  public boolean writeEntityInfo(Writer out, Path path, EntityInfoType type, LinkType link) {
52  this.path = path;
53  if("publication".equals(path.getRoot())) {
54  try {
55  Publication publication = new PublicationPU().find(Publication.class, Long.valueOf(path.getNode(1)));
56  HashMap<String, Object> args = new HashMap<>();
57  args.put("entityPath", path.getPath());
58  args.put("resolver", this);
59  out.write(EntitiesInfo.getString(publication, new PublicationWrapper(publication), "publication", type, link, args));
60  return true;
61  } catch (IOException ex) {
62  Logger.getLogger(PublicationEntityInfo.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
63  }
64  }
65  return false;
66  }
67 
68  @Override
69  public String resolveLink(String type) {
70  LinkType link = LinkType.valueOf(type);
72  return linkForPath(path, contact, link);
73  }
74 
75  public static String linkForPath(Path path, IContact contact, LinkType type) {
76  if(LinkType.INTERNAL.equals(type) && contact.isWebapp()) {
77  return "/app/frame?" + Interceptors.parameters(path.getRoot(), path.getNode(1)).encoded();
78  } else if(LinkType.WEB.equals(type)) {
79  String context = ElephantContext.getEntityWebContext(path);
80  return !Strings.isBlank(context) ? context + "?item=" + path.getNode(1) : null;
81  }
82  return null;
83  }
84 
85 }
static String getString(Object entity, Object wrapper, String label, EntityInfoType type, LinkType link)
static Parameters parameters(String root)
static String getEntityWebContext(String path)
static String linkForPath(Path path, IContact contact, LinkType type)
boolean writeEntityInfo(Writer out, Path path, EntityInfoType type, LinkType link)