Show ThinkcapSession.java syntax highlighted
package org.integratedmodelling.thinkcap;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import javax.servlet.http.HttpSession;
import org.apache.velocity.VelocityContext;
import org.integratedmodelling.thinkcap.exceptions.ThinkcapException;
import org.integratedmodelling.thinkcap.portfolio.ThinkcapPortfolio;
import org.integratedmodelling.thinkcap.widget.IThinkcapWidget;
import org.integratedmodelling.thinkcap.widget.ThinkcapWidgetManager;
import org.integratedmodelling.thinklab.KnowledgeManager;
import org.integratedmodelling.thinklab.exception.ThinklabException;
import org.integratedmodelling.thinklab.exception.ThinklabMalformedSemanticTypeException;
import org.integratedmodelling.thinklab.exception.ThinklabNoKMException;
import org.integratedmodelling.thinklab.impl.Session;
import org.integratedmodelling.thinklab.interfaces.IConcept;
import org.integratedmodelling.thinklab.interfaces.IOntology;
import org.integratedmodelling.thinklab.interfaces.IPlugin;
import org.integratedmodelling.thinklab.interfaces.IProperty;
import org.integratedmodelling.thinklab.interfaces.IResource;
import org.integratedmodelling.utils.NameGenerator;
public class ThinkcapSession extends Session {
HttpSession httpSession = null;
VelocityContext velocityContext = null;
private ThinkcapPortfolio portfolio = new ThinkcapPortfolio();
private String userName = "anonymous";
public ThinkcapSession() throws ThinklabException {
super();
}
public void initialize(HttpSession session, VelocityContext context) {
httpSession = session;
velocityContext = context;
}
HttpSession getHttpSession() {
return httpSession;
}
VelocityContext getContext() {
return velocityContext;
}
public ThinkcapManager getKnowledgeManager() {
return ThinkcapManager.getThinkcapManager();
}
public ThinkCap getThinkcap() {
return ThinkcapManager.getThinkcapManager().getThinkcap();
}
public IPlugin getPlugin(String string) {
return getKnowledgeManager().getPluginRegistry().retrievePlugin(string);
}
public String getUsername() {
return userName;
}
/**
* Get the full URL for the passed command, with any additional parameters (pass parameter name
* and value in sequence).
* @param command
* @param parameters
* @return
*/
public URL getURLForCommand(String command, String ... parameters) {
String path = ((StringBuffer)velocityContext.get("requestURL")).toString();
if (path == null)
return null;
path =
path.substring(0, path.lastIndexOf('/') + 1) +
command +
".cmd";
if (parameters != null) {
for (int i = 0; i < parameters.length; i++) {
path += (i == 0 ? "?" : "&");
path += parameters[i] + "=" + parameters[++i];
}
}
URL ret = null;
try {
ret = new URL(path);
} catch (MalformedURLException e) {
/* really, should not happen. */
}
return ret;
}
/**
* Utility for use inside templates
* @param concept
* @return
* @throws IMANoKMException
* @throws IMAMalformedSemanticTypeException
*/
public IConcept getConcept(String concept) throws ThinklabMalformedSemanticTypeException, ThinklabNoKMException {
return KnowledgeManager.KM().retrieveConcept(concept);
}
/**
* Utility for use inside templates
* @param concept
* @return
* @throws IMANoKMException
* @throws IMAMalformedSemanticTypeException
*/
public IProperty getProperty(String property) throws ThinklabMalformedSemanticTypeException, ThinklabNoKMException {
return KnowledgeManager.KM().retrieveProperty(property);
}
public Collection<IProperty> getObjectProperties(String concept) throws ThinklabException {
ArrayList<IProperty> ret = new ArrayList<IProperty>();
IConcept c = getConcept(concept);
for (IProperty p : c.getProperties()) {
if (p.isObjectProperty())
ret.add(p);
}
return ret;
}
public Collection<IProperty> getClassificationProperties(String concept) throws ThinklabException {
ArrayList<IProperty> ret = new ArrayList<IProperty>();
IConcept c = getConcept(concept);
for (IProperty p : c.getProperties()) {
if (p.isClassification())
ret.add(p);
}
return ret;
}
public Collection<IProperty> getLiteralProperties(String concept) throws ThinklabException {
ArrayList<IProperty> ret = new ArrayList<IProperty>();
IConcept c = getConcept(concept);
for (IProperty p : c.getProperties()) {
if (p.isLiteralProperty())
ret.add(p);
}
return ret;
}
public ThinkcapPortfolio getPortfolio() {
return this.portfolio;
}
public String getLabelForOntology(String ontology) {
String ret = "";
IOntology ont =
getKnowledgeManager().getKnowledgeRepository().retrieveOntology(ontology);
if (ont != null) {
ret = ont.getLabel();
}
if (ret == null || ret.equals(""))
ret = "Concept space: " + ontology;
return ret;
}
public String getLabelForConcept(String concept) throws ThinklabException {
IConcept c = getConcept(concept);
String ret = c.getLabel();
if (ret == null || ret.equals("")) {
ret = c.getLocalName();
/* reparse camel into sentence */
ret = checkUpperCamel(ret);
}
return ret;
}
public String getLabelForProperty(IProperty property) throws ThinklabException {
String ret = property.getLabel();
if (ret == null || ret.equals("")) {
ret = property.getLocalName();
/* reparse camel into sentence */
ret = checkLowerCamel(ret);
}
return ret;
}
public String getDescription(IResource property) {
String ret = property.getDescription();
if (ret == null || ret.equals(""))
ret = "No description given";
return ret;
}
public String getPropertyRangeLinks(IProperty property) throws ThinklabException {
String ret = "";
Collection<IConcept> range = property.getRange();
if (range == null)
return "object";
int ncs = range.size();
int idc = 0;
for (IConcept c : range) {
if (idc > 0) {
ret += idc < (ncs - 1) ? ", " : " or ";
}
ret +=
"<a href=\"Dictionary.cmd?concept=" +
c +
"\">" +
getLabelForConcept(c.toString()) +
"</a>";
}
return ret.equals("") ? "object" : ret;
}
public String getPropertyCardinalityDescription(IProperty property) {
String ret = "any number of";
// FIXME need to adapt to final shape of cardinality methods
// if (property.getMinimumCardinality() == 1 &&
// property.getMaximumCardinality() == 1)
// ret = "one";
// else if (property.getMinimumCardinality() == 1 &&
// property.getMaximumCardinality() == 0)
// ret = "one or more";
// else if (property.getMinimumCardinality() == 0 &&
// property.getMaximumCardinality() == 0)
// ret = "zero or more";
return "ask me later";
}
/**
* Return the resource ID of an image that can be used to illustrate the i-th result. Cache
* images. Use plug-ins to produce images. May return null, meaning there's no image.
* @param i
* @return
*/
public String getImage(IConcept c, boolean isLarge) {
return getThinkcap().getTypeIcon(c, isLarge);
}
/* quite idiotic */
public static String checkUpperCamel(String ret) {
if (ret.length() > 2 && Character.isUpperCase(ret.charAt(0)) && Character.isLowerCase(ret.charAt(1))) {
ret = ret.replaceAll("([A-Z])", " $1");
}
return ret;
}
/* quite idiotic */
public static String checkLowerCamel(String ret) {
/* FIXME
* check should be: starts with a lowercase and contains uppercase in
* positions different from last.
*/
if (ret.length() > 2 && Character.isLowerCase(ret.charAt(0)) && Character.isLowerCase(ret.charAt(1))) {
ret = ret.replaceAll("([A-Z])", " $1");
}
return ret;
}
/**
* Return the widget manager for this session and page context, creating it if necessary.
* @return
*/
public ThinkcapWidgetManager getWidgetManager() {
ThinkcapWidgetManager ret = (ThinkcapWidgetManager) velocityContext.get("WidgetManager");
if (ret == null) {
ret = new ThinkcapWidgetManager(velocityContext);
velocityContext.put("WidgetManager", ret);
}
return ret;
}
/**
* Return the page manager for this session. By rendering the standard header
* after the template, we make it possible to use it in macros.
*
* @return
*/
public PageSpecs getPageManager() {
return (PageSpecs) velocityContext.get("pageSpecs");
}
/**
* Generates a widget and sets it in the context. Uses the widget registration
* mechanism, so it only works for widgets that have been registered with
* Thinkcap. Mainly intended for use in templates and macros, which will use the
* createWidget ugly family below.
* @param name
* @param wclass
* @param arguments
* @throws ThinkcapException
*/
public void generateWidget(String name, String wclass, Object ... arguments) throws ThinkcapException {
IThinkcapWidget w = ThinkcapWidgetManager.constructWidget(wclass, name + "_code", this, arguments);
velocityContext.put(name, w);
}
public String outputWidget(String name, String wclass) throws ThinkcapException {
return renderWidget(name, wclass);
}
public String outputWidget1(String name, String wclass, Object p1) throws ThinkcapException {
return renderWidget(name, wclass, p1);
}
public String outputWidget2(String name, String wclass, Object p1, Object p2) throws ThinkcapException {
return renderWidget(name, wclass, p1, p2);
}
public String outputWidget3(String name, String wclass, Object p1, Object p2, Object p3) throws ThinkcapException {
return renderWidget(name, wclass, p1, p2, p3);
}
public String outputWidget4(String name, String wclass, Object p1, Object p2, Object p3, Object p4) throws ThinkcapException {
return renderWidget(name, wclass, p1, p2, p3, p4);
}
public String outputWidget5(String name, String wclass, Object p1, Object p2, Object p3, Object p4, Object p5) throws ThinkcapException {
return renderWidget(name, wclass, p1, p2, p3, p4, p5);
}
public String outputWidget6(String name, String wclass, Object p1, Object p2, Object p3, Object p4, Object p5, Object p6) throws ThinkcapException {
return renderWidget(name, wclass, p1, p2, p3, p4, p5, p6);
}
public void createWidget(String name, String wclass) throws ThinkcapException {
generateWidget(name, wclass);
}
public void createWidget1(String name, String wclass, Object p1) throws ThinkcapException {
generateWidget(name, wclass, p1);
}
public void createWidget2(String name, String wclass, Object p1, Object p2) throws ThinkcapException {
generateWidget(name, wclass, p1, p2);
}
public void createWidget3(String name, String wclass, Object p1, Object p2, Object p3) throws ThinkcapException {
generateWidget(name, wclass, p1, p2, p3);
}
public void createWidget4(String name, String wclass, Object p1, Object p2, Object p3, Object p4) throws ThinkcapException {
generateWidget(name, wclass, p1, p2, p3, p4);
}
public void createWidget5(String name, String wclass, Object p1, Object p2, Object p3, Object p4, Object p5) throws ThinkcapException {
generateWidget(name, wclass, p1, p2, p3, p4, p5);
}
public void createWidget6(String name, String wclass, Object p1, Object p2, Object p3, Object p4, Object p5, Object p6) throws ThinkcapException {
generateWidget(name, wclass, p1, p2, p3, p4, p5, p6);
}
/**
* Generates a widget and outputs it in the context. Uses the widget registration
* mechanism, so it only works for widgets that have been registered with
* Thinkcap. Mainly intended for use in templates and macros.
* @param name
* @param wclass
* @param arguments
* @throws ThinkcapException
*/
public String renderWidget(String name, String wclass, Object ... arguments) throws ThinkcapException {
String ret = "";
if (name == null || name.equals(""))
name = NameGenerator.newName("TKW");
IThinkcapWidget widget =
ThinkcapWidgetManager.constructWidget(wclass, name, this, arguments);
if (widget != null)
ret = widget.outputWidget(getPageManager());
return ret;
}
}
See more files for this project here