public class ViewTopicAction extends GenericTemplateAction
An action for displaying something about a single topic using a template. This extends GenericTemplateAction and adds some features related to topics. For the most part, however, it works exactly like GenericTemplateAction.
The topic this action uses can be specified in the HTTP request. The request parameter used for this is topic, but it can be changed using the initialisation parameter topicRequestKey. The request parameter value can be a number of things. It can be prefixed with one of "si:", "sl:" or "bn:" standing for subject identifier, subject locator and base name, respectively. The prefix is then followed by the value of the chosen identifying field. If you do not use any prefix, a topic is looked for trying all the identifying fields in that order until a topic is found. That is, the value is first assumed to be a subject identifier, then a subject locator and finally a base name. If no suitable topic is found, whether a prefix is used or not, the action will fail.
You may set the initialisation parameter noTopic to true to disable automatic topic resolution. In this case you should resolve the topic in your template. You will also need to forward the relevant request parameter to the template, which is best done using GenericTemplateAction's forwardRequestParameters.
Unless noTopic is set to true, the topic will be automatically added to the template context. By default this is done using template parameter name "topic", but this can be changed using the initialisation parameter topicContextKey. Similarly the topic map is added to the context using parameter name "topicmap" and this can be changed with the initialisation parameter topicMapContextKey. The exact value of the HTTP request parameter is also added in the context with parameter name "topicRequest". This will include the possible prefix of the value, as described above.
You can set the default topic and default language using initialisation parameters defaultTopic and defaultLang, respectively. If either is left undefined in the HTTP request, the defaults will be used instead.
Note that the getTopic and getTopics methods are static and can, and should, be used in other actions which need to resolve topics based on request parameters.
CachedAction.OutputProvider
Modifier and Type | Field and Description |
---|---|
protected java.lang.String |
defaultLang |
protected java.lang.String |
defaultTopic |
protected boolean |
noTopic |
protected TopicMapManager |
tmManager |
protected java.lang.String |
topicContextKey |
protected java.lang.String |
topicMapContextKey |
protected java.lang.String |
topicRequestKey |
defaultRequestParameters, forwardRequestParameters, requestContextKey, staticContext, templateKey, templateManager
cache, caching, threadPool
actionParamKey, handledActions, httpHeaders, isDefaultAction, replacements, replacementsInitialized, servletModule
engine, initScript, persistentObjects, scriptManager, startScript, stopScript
autoStart, isInitialized, isRunning, logging, loggingModule, moduleManager
Constructor and Description |
---|
ViewTopicAction() |
Modifier and Type | Method and Description |
---|---|
protected java.lang.String |
getCacheKey(javax.servlet.http.HttpServletRequest req,
ModulesServlet.HttpMethod method,
java.lang.String action)
Should return a string that uniquely identifies this request.
|
protected java.util.LinkedHashMap<java.lang.String,java.lang.String> |
getCacheKeyParams(javax.servlet.http.HttpServletRequest req,
ModulesServlet.HttpMethod method,
java.lang.String action)
Returns the parameters used in building the cache key.
|
java.util.Collection<Module> |
getDependencies(ModuleManager manager)
Returns all the modules this module depends on.
|
protected java.util.HashMap<java.lang.String,java.lang.Object> |
getStaticTemplateContext()
Gets the static context to for templates.
|
protected java.util.HashMap<java.lang.String,java.lang.Object> |
getTemplateContext(Template template,
javax.servlet.http.HttpServletRequest req,
ModulesServlet.HttpMethod method,
java.lang.String action,
User user)
Gets the template context used for a request.
|
static Topic |
getTopic(java.lang.String query,
TopicMap tm)
Same as getTopics, but returns only the first result of what
getTopics would return.
|
static java.util.ArrayList<Topic> |
getTopics(java.lang.String query,
TopicMap tm,
int max)
Returns topics based on a simple query.
|
boolean |
handleAction(javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse resp,
ModulesServlet.HttpMethod method,
java.lang.String action,
User user)
Handles this action.
|
void |
init(ModuleManager manager,
java.util.HashMap<java.lang.String,java.lang.Object> settings)
Initialises the module.
|
void |
start(ModuleManager manager)
Starts the module.
|
void |
stop(ModuleManager manager)
Stops the module.
|
doOutput, getForwardedParameters, getTemplate, putStaticContext, returnOutput
buildCacheKey, readMetadata, writeMetadata
addHandledAction, doReplacements, handleRequest, isHandleAction, setActionParamKey, setHttpHeaders
getScriptEngine
isInitialized, isRunning, requireLogging, toString
protected java.lang.String topicRequestKey
protected java.lang.String topicMapContextKey
protected java.lang.String topicContextKey
protected boolean noTopic
protected java.lang.String defaultLang
protected java.lang.String defaultTopic
protected TopicMapManager tmManager
public java.util.Collection<Module> getDependencies(ModuleManager manager) throws ModuleException
Module
getDependencies
in interface Module
getDependencies
in class GenericTemplateAction
manager
- The module manager handling this module.ModuleException
public void init(ModuleManager manager, java.util.HashMap<java.lang.String,java.lang.Object> settings) throws ModuleException
Module
Initialises the module. After constructor, this is the first method called in the life cycle of a module. It should not perform anything time consuming or anything with notable outside side effects. It should only read the parameters and initialise the module so that it can later be started. Note that a module being initialised doesn't mean that it necessarily will ever be started.
A ModuleException may be thrown if something vital is missing from the parameters or they are not sensible. In some cases you may not want to throw an exception even if vital initialisation information is missing. If, for example, it is possible that the module is initialised in some other way between the init and the start method calls. A ModuleException may also be thrown at the start method if the module is still not initialised.
init
in interface Module
init
in class GenericTemplateAction
manager
- The module manager handling this module. You may keep a
reference to it if needed.ModuleException
public void start(ModuleManager manager) throws ModuleException
Module
start
in interface Module
start
in class GenericTemplateAction
manager
- The module manager handling this module.ModuleException
public void stop(ModuleManager manager)
Module
stop
in interface Module
stop
in class GenericTemplateAction
manager
- The module manager handling this module.protected java.util.LinkedHashMap<java.lang.String,java.lang.String> getCacheKeyParams(javax.servlet.http.HttpServletRequest req, ModulesServlet.HttpMethod method, java.lang.String action)
GenericTemplateAction
getCacheKeyParams
in class GenericTemplateAction
req
- The HTTP request for which cache key parameters are resolved.method
- The method of the HTTP request.action
- The parsed action parameter from the request.protected java.lang.String getCacheKey(javax.servlet.http.HttpServletRequest req, ModulesServlet.HttpMethod method, java.lang.String action)
CachedAction
getCacheKey
in class GenericTemplateAction
req
- The HTTP request which, result of which is to be cached.method
- The method of the HTTP request.action
- The parsed action parameter from the request.public static java.util.ArrayList<Topic> getTopics(java.lang.String query, TopicMap tm, int max) throws TopicMapException
Returns topics based on a simple query. Currently this will only ever return a single topic at most, in the future more supported query types might be added which could return more topics.
At the moment, the query can be prefixed with one of "si:", "sl:" or "bn:" which stand for subject identifier, subject locator and base name, respectively. After the prefix, add the value of the chosen identifying field. That topic will then be returned, or an empty collection if it doesn't exist. If you don't use any prefix, the value is first assumed to be a subject identifier, if that doesn't correspond to an existing topic, then subject locator is tried, and finally base name. If none work, then an empty collection is returned. Otherwise the first match is returned.
query
- The topic query.tm
- The topic map where to execute the query.max
- Maximum number of results to return.TopicMapException
public static Topic getTopic(java.lang.String query, TopicMap tm) throws TopicMapException
query
- The topic query.tm
- The topic map where to execute the query.TopicMapException
protected java.util.HashMap<java.lang.String,java.lang.Object> getStaticTemplateContext()
GenericTemplateAction
getStaticTemplateContext
in class GenericTemplateAction
protected java.util.HashMap<java.lang.String,java.lang.Object> getTemplateContext(Template template, javax.servlet.http.HttpServletRequest req, ModulesServlet.HttpMethod method, java.lang.String action, User user) throws ActionException
GenericTemplateAction
Gets the template context used for a request. Contains the static context as well forwarded request parameters. The static context contains both action specific context items and the template manager default context. In addition, the logged in user, if applicable, is added with the variable name user. Usually you don't want anything user specific cached so as a safety measure, the user variable is not added if caching is turned on. If you for some reason do, you will have to override this method.
This should be the main overriding point for extending classes. You can perform any tasks needed and then add things in the context. Often you don't need to override anything else besides this and the basic Module methods.
getTemplateContext
in class GenericTemplateAction
template
- The template to be used.req
- The HTTP request.method
- The method of the HTTP request.action
- The action parameter parsed from the HTTP request.user
- The logged in user, or null if not logged in.ActionException
public boolean handleAction(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp, ModulesServlet.HttpMethod method, java.lang.String action, User user) throws javax.servlet.ServletException, java.io.IOException, ActionException
AbstractAction
handleAction
in class CachedAction
req
- The HTTP Request.resp
- The HTTP response.method
- The HTTP method of the request.action
- The parsed action id.user
- The logged in user, or null if not applicable.javax.servlet.ServletException
java.io.IOException
ActionException
Copyright 2004-2015 Wandora Team