BrightSide Workbench Full Report + Source Code
DossierEntityInfo.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.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.dossier.db.DossierPU;
35 import org.turro.dossier.dossier.DossierWrapper;
36 import org.turro.dossier.entity.Dossier;
37 import org.turro.dossier.entity.DossierType;
38 import org.turro.dossier.entity.Issue;
39 import org.turro.dossier.issue.IssueWrapper;
40 import org.turro.elephant.context.Application;
41 import org.turro.elephant.context.ElephantContext;
42 import org.turro.entities.Entities;
43 import org.turro.entities.IElephantEntity;
44 import org.turro.path.Path;
45 import org.turro.plugin.contacts.IContact;
46 import org.turro.www.describeit.DescribeItCtrl;
47 
52 @EntityInfo
53 public class DossierEntityInfo implements IEntityInfo {
54 
55  private Path path;
56 
57  @Override
58  public boolean writeEntityInfo(Writer out, Path path, EntityInfoType type, LinkType link) {
59  this.path = path;
60  if(null != path.getRoot()) switch (path.getRoot()) {
61  case "dossier":
62  try {
63  Dossier dossier = new DossierPU().find(Dossier.class, Long.valueOf(path.getNode(1)));
65  dic.setEntityPath(path.getPath());
66  HashMap<String, Object> args = new HashMap<>();
67  args.put("dic", dic);
68  args.put("entityPath", path.getPath());
69  args.put("resolver", this);
70  out.write(EntitiesInfo.getString(dossier, new DossierWrapper(dossier), "dossier", type, link, args));
71  return true;
72  } catch (IOException ex) {
73  Logger.getLogger(DossierEntityInfo.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
74  }
75  case "issue":
76  try {
77  Issue issue = new DossierPU().find(Issue.class, Long.valueOf(path.getNode(1)));
79  dic.setEntityPath(path.getPath());
80  HashMap<String, Object> args = new HashMap<>();
81  args.put("dic", dic);
82  args.put("entityPath", path.getPath());
83  args.put("resolver", this);
84  out.write(EntitiesInfo.getString(issue, new IssueWrapper(issue), "issue", type, link, args));
85  return true;
86  } catch (IOException ex) {
87  Logger.getLogger(DossierEntityInfo.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
88  }
89  }
90  return false;
91  }
92 
93  @Override
94  public String resolveLink(String type) {
95  LinkType link = LinkType.valueOf(type);
97  return linkForPath(path, contact, link);
98  }
99 
100  public static String linkForPath(Path path, IContact contact, LinkType type) {
101  if(LinkType.INTERNAL.equals(type)) {
102  if(contact.isWebapp()) {
103  return "/app/frame?" + Interceptors.parameters(path.getRoot(), path.getNode(1)).encoded();
104  } else {
105  return "/user/my" + checkPath(path) + "s?item=" + path.getNode(1);
106  }
107  } else if(LinkType.WEB_INTERNAL.equals(type)) {
108  return "/user/my" + checkPath(path) + "s?item=" + path.getNode(1);
109  } else if(LinkType.WEB.equals(type)) {
110  String context = ElephantContext.getEntityWebContext(path);
111  return !Strings.isBlank(context) ? context + "?item=" + path.getNode(1) : linkForPath(path, contact, LinkType.WEB_INTERNAL);
112  }
113  return null;
114  }
115 
116  private static String checkPath(Path path) {
117  String root = path.getRoot();
118  if("dossier".equals(root)) {
119  IElephantEntity iee = Entities.getController(path.getPath());
120  if(iee.getEntity() instanceof Dossier) {
121  Dossier dossier = (Dossier) iee.getEntity();
122  if(DossierType.TYPE_PROJECT.equals(dossier.getType())) {
123  return "project";
124  }
125  }
126  }
127  return root;
128  }
129 
130 }
static String getString(Object entity, Object wrapper, String label, EntityInfoType type, LinkType link)
static Parameters parameters(String root)
static String linkForPath(Path path, IContact contact, LinkType type)
boolean writeEntityInfo(Writer out, Path path, EntityInfoType type, LinkType link)
static String getEntityWebContext(String path)
static IElephantEntity getController(String path)
Definition: Entities.java:78