BrightSide Workbench Full Report + Source Code
StudentsEntityInfo.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2019 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.students.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.auth.Authentication;
33 import org.turro.elephant.context.Application;
34 import org.turro.elephant.context.ElephantContext;
35 import org.turro.path.Path;
36 import org.turro.plugin.contacts.IContact;
37 import org.turro.students.db.StudentsPU;
38 import org.turro.students.entities.Challenge;
39 import org.turro.students.wrappers.ChallengeWrapper;
40 import org.turro.www.describeit.DescribeItCtrl;
41 
46 public class StudentsEntityInfo 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(null != path.getRoot()) switch (path.getRoot()) {
54  case "challenge":
55  try {
56  Challenge challenge = new StudentsPU().find(Challenge.class, Long.valueOf(path.getNode(1)));
58  dic.setEntityPath(path.getPath());
59  HashMap<String, Object> args = new HashMap<>();
60  args.put("dic", dic);
61  args.put("entityPath", path.getPath());
62  args.put("resolver", this);
63  out.write(EntitiesInfo.getString(challenge, new ChallengeWrapper(challenge), "challenge", type, link, args));
64  return true;
65  } catch (IOException ex) {
66  Logger.getLogger(StudentsEntityInfo.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
67  }
68 
69  }
70  return false;
71  }
72 
73  @Override
74  public String resolveLink(String type) {
75  LinkType link = LinkType.valueOf(type);
77  return linkForPath(path, contact, link);
78  }
79 
80  public static String linkForPath(Path path, IContact contact, LinkType type) {
81  if(LinkType.INTERNAL.equals(type)) {
82  if(contact.isWebapp()) {
83  return "/app/frame?" + Interceptors.parameters(path.getRoot(), path.getNode(1)).encoded();
84  } else {
85  return "/user/" + path.getRoot() + "s?item=" + path.getNode(1);
86  }
87  } else if(LinkType.WEB_INTERNAL.equals(type)) {
88  return "/user/" + path.getRoot() + "s?item=" + path.getNode(1);
89  } else if(LinkType.WEB.equals(type)) {
90  String context = ElephantContext.getEntityWebContext(path);
91  return !Strings.isBlank(context) ? context + "?item=" + path.getNode(1) : linkForPath(path, contact, LinkType.WEB_INTERNAL);
92  }
93  return null;
94  }
95 
96 }
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)