boolean isBI = (context.getDesignContext().getTag() != null ? context.getDesignContext().getTag().isBrowserInterface() : false);
// Retrieve context by content key or unid
if (contextFunction.equals("docid") || contextFunction.equals("content")) {
WGContent content = null;
WGContentKey tmpKey;
try {
tmpKey = WGContentKey.parse(contextExpression, context.getdocument().getDatabase());
}
catch (WGAPIException e) {
context.setLastError("Error parsing contextExpression. Exception: " + e.getClass().getName() + " message: " + e.getMessage());
return errorReturnContext;
}
if (tmpKey != null) {
content = context.content().getDatabase().getContentByKey(tmpKey);
if (content != null) {
return context.getTMLContextForDocument(content);
}
}
content =
WGPDispatcher.getContentByAnyKey(
contextExpression,
context.content().getDatabase(),
_languageChooser,
context.isbrowserinterface());
if (content != null) {
return context.getTMLContextForDocument(navigator.chooseRelevantContent(content, mainContent));
}
else {
context.setLastError("docid could not be resolved: " + contextExpression);
return errorReturnContext;
}
}
else if (contextFunction.equals("name")) {
WGContent content = _languageChooser.selectContentForName(context.content().getDatabase(), contextExpression, context.isbrowserinterface());
if (content != null) {
return context.getTMLContextForDocument(navigator.chooseRelevantContent(content, mainContent));
}
context.setLastError("Could not retrieve content for name: " + contextExpression);
return errorReturnContext;
}
// Retrieve the context of another tag
else if (contextFunction.equals("tag")) {
if (context.getDesignContext().getTag() == null) {
context.setLastError("Cannot retrieve tag because this script does not run on a WebTML page");
return errorReturnContext;
}
BaseTagStatus refTag = context.getDesignContext().getTag().getTagStatusById(contextExpression);
if (refTag != null) {
TMLContext tagContext = refTag.tmlContext;
if (tagContext != null) {
return tagContext;
}
else {
context.setLastError("Context of this Tag could not be retrieved: " + contextExpression);
return errorReturnContext;
}
}
else {
context.setLastError("Tag could not be retrieved: " + contextExpression);
return errorReturnContext;
}
}
else if (contextFunction.equals("db") || contextFunction.equals("plugin")) {
if (contextFunction.equals("plugin")) {
WGAPlugin plugin = context.getwgacore().getPluginSet().getPluginByUniqueName(contextExpression);
if (plugin != null) {
contextExpression = plugin.buildDatabaseKey();
}
else {
context.setLastError("Unknown plugin unique name: " + contextExpression);
return errorReturnContext;
}
}
WGDatabase dbTarget = null;
try {
dbTarget = context.db(context.resolveDBKey(contextExpression));
}
catch (WGUnavailableException e1) {
context.setLastError("Database '" + contextExpression + "' is currently unavailable");
return errorReturnContext;
}
catch (WGException e) {
context.setLastError("Unable to open database '" + contextExpression + "'. Exception: " + e.getClass().getName() + " message: " + e.getMessage());
return errorReturnContext;
}
if (dbTarget == null) {
context.setLastError("No database with key " + contextExpression);
return errorReturnContext;
}
if (dbTarget.isSessionOpen() == false) {
context.setLastError("User cannot open database '" + contextExpression + "'");
return errorReturnContext;
}
if (dbTarget.getSessionContext().getAccessLevel() <= WGDatabase.ACCESSLEVEL_NOACCESS) {
context.setLastError("User has no access to database '" + contextExpression + "'");
return errorReturnContext;
}
TMLContext dbContext = context.dbContext(dbTarget);
if (dbContext == null) {
context.setLastError("Target database " + contextExpression + " does not support db context changes");
return errorReturnContext;
}
else {
return dbContext;
}
}
else if (contextFunction.equals("area")) {
WGArea area = db.getArea(contextExpression);
if (area == null) {
context.setLastError("No area of name '" + contextExpression + "'");
return errorReturnContext;
}
WGContent content = navigator.getRootContent(area);
if (content != null) {
return context.getTMLContextForDocument(content);
}
else {
context.setLastError("No root content in area '" + contextExpression + "'");
return errorReturnContext;
}
}
else if (contextFunction.equals("query")) {
if (context.getDesignContext().getTag() == null) {
context.setLastError("Cannot retrieve tag because this script does not run on a WebTML page");
return errorReturnContext;
}
BaseTagStatus tag = context.getDesignContext().getTag().getTagStatusById(contextExpression);
if (tag != null && tag instanceof Query.Status) {
Query.Status queryTag = (Query.Status) tag;
WGContent content = queryTag.getFirstContent();
if (content != null) {
return context.getTMLContextForDocument(queryTag.getFirstContent());
}
else {
context.setLastError(
"Could not retrieve context by query tag \"" + contextExpression + "\". Query had no result.");
return errorReturnContext;
}
}
else {
context.setLastError("No query tag with id: " + contextExpression);
return errorReturnContext;
}
}
else if (contextFunction.equals("role")) {
setRole(contextExpression);
return context;
}
else if (contextFunction.equals("relation")) {
WGContent relationContent = context.content().getRelation(contextExpression);
if (relationContent != null) {
return context.getTMLContextForDocument(relationContent);
}
else {
context.setLastError("Content " + context.meta("KEY") + " has no relation named '" + contextExpression + "'");
return errorReturnContext;
}
}
else if (contextFunction.equals("level")) {
int level;
try {
level = Integer.parseInt(contextExpression);
}
catch (NumberFormatException e) {
context.setLastError("Cannot be parsed as level number: " + contextExpression);
return errorReturnContext;
}
WGContent con = context.content();
if (!con.hasCompleteRelationships()) {
context.setLastError("Current content does not belong to a page hierarchy");
return errorReturnContext;
}
WGStructEntry struct = con.getStructEntry();
while (struct != null && struct.getLevel() > level) {
struct = struct.getParentEntry();
}
if (struct != null) {
WGContent targetCon = _languageChooser.selectContentForPage(struct, isBI);
if (targetCon != null) {
return context.getTMLContextForDocument(targetCon);
}
else {
context.setLastError("Page on level " + level + " has no appropriate content document");
return errorReturnContext;
}
}
else {
context.setLastError("Failed to go to page level " + level);
return errorReturnContext;
}
}
else if (contextFunction.equals("np")) {
String namePart = UniqueNamePartFormatter.INSTANCE.format(contextExpression);
String uname = context.content().getUniqueName();
if (WGUtils.isEmpty(uname)) {
uname = context.content().getStructEntry().getUniqueName();
}
String fullname;
if (!WGUtils.isEmpty(uname)) {
fullname = uname + "." + namePart;
}
else {
fullname = namePart;
}
WGContent content = _languageChooser.selectContentForName(context.content().getDatabase(), fullname, context.isbrowserinterface());
if (content != null) {
return context.getTMLContextForDocument(navigator.chooseRelevantContent(content, mainContent));
}
context.setLastError("Could not retrieve content for hdb unique name " + fullname);