Reading various information about a resource
(Difference between revisions)
(New page: This is an example scriptlet that illustrates how to retrieve some basic info about a resource programmatically. It is a fairly large file, containing many examples. See the source code co...) |
|||
Line 183: | Line 183: | ||
out.println("<tr><td>Released</td><td>" + (r.isReleased(System.currentTimeMillis()) ? "yes" : "no") + "</td></tr>"); | out.println("<tr><td>Released</td><td>" + (r.isReleased(System.currentTimeMillis()) ? "yes" : "no") + "</td></tr>"); | ||
out.println("<tr><td>Expired</td><td>" + (r.isExpired(System.currentTimeMillis()) ? "yes" : "no") + "</td></tr>"); | out.println("<tr><td>Expired</td><td>" + (r.isExpired(System.currentTimeMillis()) ? "yes" : "no") + "</td></tr>"); | ||
− | |||
out.println("</table>"); | out.println("</table>"); | ||
− | /** | + | /** Get the resource type */ |
I_CmsResourceType resType = OpenCms.getResourceManager().getResourceType(r.getTypeId()); | I_CmsResourceType resType = OpenCms.getResourceManager().getResourceType(r.getTypeId()); | ||
− | if (resType instanceof CmsResourceTypeXmlContent) { | + | |
+ | /** If the resource is of type structured content (a.k.a. xmlcontent) */ | ||
+ | if (resType instanceof CmsResourceTypeXmlContent) { | ||
out.println("<h3>This file is of type " + resType.getTypeName() + " - an XML file, instance of CmsResourceTypeXmlContent</h3>"); | out.println("<h3>This file is of type " + resType.getTypeName() + " - an XML file, instance of CmsResourceTypeXmlContent</h3>"); | ||
out.println("<h4>Available paths in the XML file</h4>"); | out.println("<h4>Available paths in the XML file</h4>"); | ||
+ | /** Read the structured content (a.k.a. xmlcontent) file programmatically */ | ||
CmsXmlContent resourceDocument = CmsXmlContentFactory.unmarshal(cmso, cmso.readFile(r)); | CmsXmlContent resourceDocument = CmsXmlContentFactory.unmarshal(cmso, cmso.readFile(r)); | ||
+ | /** Get all element names for the given locale */ | ||
List elementNames = resourceDocument.getNames(locale); | List elementNames = resourceDocument.getNames(locale); | ||
Iterator elementNamesItr = elementNames.iterator(); | Iterator elementNamesItr = elementNames.iterator(); | ||
Line 200: | Line 203: | ||
out.println("<ul>"); | out.println("<ul>"); | ||
+ | /** Print all the retrieved element names and their values */ | ||
while (elementNamesItr.hasNext()) { | while (elementNamesItr.hasNext()) { | ||
out.print("<li>"); | out.print("<li>"); | ||
Line 214: | Line 218: | ||
out.println("</ul>"); | out.println("</ul>"); | ||
} | } | ||
+ | |||
+ | /** If the resource is not of type structured content */ | ||
else { | else { | ||
out.println("<h3>This file is of type " + resType + "</h3>"); | out.println("<h3>This file is of type " + resType + "</h3>"); |
Revision as of 12:51, 7 April 2010
This is an example scriptlet that illustrates how to retrieve some basic info about a resource programmatically. It is a fairly large file, containing many examples. See the source code comments for detailed information.
To run a test, you can either assign the JSP as "template" or "template-elements" (for template driven resource types) or run the JSP itself, then append a VFS path parameter to the URL: "?uri=/path/to/resource.html".
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ page contentType="text/html" pageEncoding="UTF-8"%> <%@ page import="java.util.*" %> <%@ page import="java.text.SimpleDateFormat" %> <%@ page import="org.opencms.file.CmsResource" %> <%@ page import="org.opencms.file.CmsObject" %> <%@ page import="org.opencms.util.CmsUUID" %> <%@ page import="org.opencms.util.CmsStringUtil" %> <%@ page import="org.opencms.main.*" %> <%@ page import="org.opencms.jsp.*" %> <%@ page import="org.opencms.file.*" %> <%@ page import="org.opencms.file.types.*" %> <%@ page import="org.opencms.xml.*" %> <%@ page import="org.opencms.xml.content.*" %> <%@ page import="org.opencms.xml.types.*" %> <%@ page import="org.opencms.lock.*" %> <%@ page import="org.opencms.db.CmsResourceState" %> <%@ page import="org.opencms.xml.content.CmsXmlContent" %> <%! /** * Converts a date from numeric to human readable format. The dateFormat * argument is a String that is used to define the date format. * For more information on time format syntax, see the documentation on * java.text.SimpleDateFormat. * @param timestamp The timestamp, a long parsed as a String. * @param dateFormat The output time format. If null, a default time format is applied. * @return The date, formatted in compliance to the specified dateFormat. * @throws javax.servlet.ServletException If the formatting fails (probably due to syntax error in dateFormat). * @see java.text.SimpleDateFormat */ public String formatDate(String timestamp, String dateFormat) throws ServletException { Date date = new Date(); // Create a date (representing the moment in time it is created). This object is changed below. float millis = Float.parseFloat(timestamp); // Get the float value of the timestamp date.setTime((long)millis); // Change the date object so that it represents the time kept in "timestamp" if (dateFormat == null) dateFormat = "EEE MMM dd yyyy HH:mm:ss zzz"; // Format: Tue Nov 04 2003 21:53:43 EST // Create the output format SimpleDateFormat outputFormat = new SimpleDateFormat(dateFormat); String dateString = null; try { dateString = outputFormat.format(date); } catch (Exception e) { e.printStackTrace(); throw new ServletException("An error was encountered while trying to process the date-time " + date.toString() + ". Please check the format of the string and correct the text."); } return dateString; } %> <% /** Create an action element */ CmsJspActionElement cms = new CmsJspActionElement(pageContext, request, response); /** Get the initialized CmsObject */ CmsObject cmso = cms.getCmsObject(); /** Get the locale */ Locale locale = cms.getRequestContext().getLocale(); /** Get the resource path: get the path from the URL parameter if a parameter "uri" is set, or use the requesting file's URI if no such parameter is set */ String uri = cms.getRequest().getParameter("uri") == null ? cms.getRequestContext().getUri() : cms.getRequest().getParameter("uri"); %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Resource information: <%= cmso.readPropertyObject(uri, "Title", false).getValue("NO TITLE") %></title> </head> <body> <h3>Resource information</h3> <h4>This view is generated from template JSP '<%= cms.info("opencms.uri") %>'</h4> <% /** The number of resource states used here */ final int NUM_RESOURCE_STATES = 5; /** Map corresponding string values to the different resource states */ HashMap resourceStates = new HashMap(NUM_RESOURCE_STATES); /** See org.opencms.db.CmsResourceState for explanations of the different resource states */ resourceStates.put(String.valueOf(CmsResourceState.STATE_CHANGED.getState()), "STATE_CHANGED"); resourceStates.put(String.valueOf(CmsResourceState.STATE_DELETED.getState()), "STATE_DELETED"); resourceStates.put(String.valueOf(CmsResourceState.STATE_KEEP.getState()), "STATE_KEEP"); resourceStates.put(String.valueOf(CmsResourceState.STATE_NEW.getState()), "STATE_NEW"); resourceStates.put(String.valueOf(CmsResourceState.STATE_UNCHANGED.getState()), "STATE_UNCHANGED"); /** Just a list */ List list = null; /** Just an iterator */ Iterator i = null; /* Read resource via the site path */ CmsResource resourceViaPath = cmso.readResource(uri); /* Get the resource ID */ CmsUUID rid = resourceViaPath.getResourceId(); /* Get the structure ID */ CmsUUID sid = resourceViaPath.getStructureId(); /* Get the structure ID String value */ String sidString = sid.getStringValue(); /* Construct a new CmsUUID using the String value */ CmsUUID sidReconstructed = CmsUUID.valueOf(sidString); /* "Recreate" the resource: get the CmsResource by using its structure ID */ CmsResource resourceViaId = cmso.readResource(sidReconstructed); /* Use a copy (just to save me from typing the long name :-) */ CmsResource r = resourceViaId; /* Get the ID of the user who created the resource */ CmsUUID userCreated = r.getUserCreated(); /* Get the ID of the user who did the last modification to the resource */ CmsUUID userModified = r.getUserLastModified(); /* Get the lock applied to the resource */ CmsLock lock = cmso.getLock(r); /* Get the resource's site path */ String sitePath = cmso.getSitePath(r); /* Print out resource info */ out.println("<table border=\"1\" cellpadding=\"3\" cellspacing=\"0\">"); out.println("<tr><td>Name</td><td>" + r.getName() + "</td></tr>"); out.println("<tr><td>Title</td><td>" + cmso.readPropertyObject(r, "Title", false).getValue() + "</td></tr>"); out.println("<tr><td>Version</td><td>" + r.getVersion() + "</td></tr>"); out.println("<tr><td>Path (site path)</td><td>" + cmso.getSitePath(resourceViaId) + "</td></tr>"); out.println("<tr><td>Root path</td><td>" + r.getRootPath() + "</td></tr>"); out.println("<tr><td>Link path</td><td>" + cms.link(sitePath) + "</td></tr>"); out.println("<tr><td>Parent folder</td><td>" + CmsResource.getParentFolder(sitePath) + "</td></tr>"); out.println("<tr><td>Locale</td><td>" + cmso.readPropertyObject(r, "locale", false).getValue() + "</td></tr>"); out.println("<tr><td>Siblings</td><td><table border=\"1\" cellpadding=\"3\" cellspacing=\"0\">"); out.println("<tr><th>" + cmso.readSiblings(sitePath, CmsResourceFilter.ALL).size() + " occurences</th></tr>"); list = cmso.readSiblings(sitePath, CmsResourceFilter.ALL); i = list.iterator(); CmsResource sibling = null; while (i.hasNext()) { sibling = (CmsResource)i.next(); out.println("<tr><td> " + cmso.getSitePath(sibling) + " </td></tr>"); } out.println("</table></td></tr>"); out.println("<tr><td>Resource type name</td><td>" + OpenCms.getResourceManager().getResourceType(r).getTypeName() + "</td></tr>"); out.println("<tr><td>Resource type ID</td><td>" + r.getTypeId() + "</td></tr>"); out.println("<tr><td>Resource ID</td><td>" + r.getResourceId() + "</td></tr>"); out.println("<tr><td>Structure ID</td><td>" + r.getStructureId().getStringValue() + "</td></tr>"); out.println("<tr><td>Created</td><td>" + formatDate(String.valueOf(r.getDateCreated()), null) + "</td></tr>"); out.println("<tr><td>Created by</td><td>" + cmso.readUser(userCreated).getFullName() + "</td></tr>"); out.println("<tr><td>Last modified</td><td>" + formatDate(String.valueOf(r.getDateLastModified()), null) + "</td></tr>"); out.println("<tr><td>Last modified by</td><td>" + cmso.readUser(userModified).getFullName() + "</td></tr>"); out.println("<tr><td>Lock</td><td>"); if (lock.isUnlocked()) out.println("Not locked"); else{ out.println("Locked, " + (lock.isInherited() ? ((lock.isDirectlyInherited() ? "directly " : "") + "inherited lock") : "directly locked (not inherited)")); out.println("<br/>Locked by " + (cmso.readUser(lock.getUserId()).getFullName())); } out.println("</td></tr>"); out.println("<tr><td>Properties</td><td><table border=\"1\" cellpadding=\"3\" cellspacing=\"0\">"); out.println("<tr><th colspan=\"2\">" + cmso.readPropertyObjects(r, true).size() + " occurences</th></tr>"); list = cmso.readPropertyObjects(r, true); i = list.iterator(); CmsProperty prop = null; while (i.hasNext()) { prop = (CmsProperty)i.next(); out.println("<tr><td> " + prop.getName() + " </td><td> " + prop.getValue() + " </td></tr>"); } out.println("</table></td></tr>"); out.println("<tr><td>Resource state</td><td>" + (resourceStates.get(String.valueOf(r.getState().getState())) + " [" + r.getState().getState()) + "]</td></tr>"); out.println("<tr><td>Touched</td><td>" + (r.isTouched() ? "yes" : "no") + "</td></tr>"); out.println("<tr><td>Internal</td><td>" + (r.isInternal() ? "yes" : "no") + "</td></tr>"); out.println("<tr><td>Released</td><td>" + (r.isReleased(System.currentTimeMillis()) ? "yes" : "no") + "</td></tr>"); out.println("<tr><td>Expired</td><td>" + (r.isExpired(System.currentTimeMillis()) ? "yes" : "no") + "</td></tr>"); out.println("</table>"); /** Get the resource type */ I_CmsResourceType resType = OpenCms.getResourceManager().getResourceType(r.getTypeId()); /** If the resource is of type structured content (a.k.a. xmlcontent) */ if (resType instanceof CmsResourceTypeXmlContent) { out.println("<h3>This file is of type " + resType.getTypeName() + " - an XML file, instance of CmsResourceTypeXmlContent</h3>"); out.println("<h4>Available paths in the XML file</h4>"); /** Read the structured content (a.k.a. xmlcontent) file programmatically */ CmsXmlContent resourceDocument = CmsXmlContentFactory.unmarshal(cmso, cmso.readFile(r)); /** Get all element names for the given locale */ List elementNames = resourceDocument.getNames(locale); Iterator elementNamesItr = elementNames.iterator(); String elementName = null; out.println("<ul>"); /** Print all the retrieved element names and their values */ while (elementNamesItr.hasNext()) { out.print("<li>"); elementName = (String)elementNamesItr.next(); out.print(elementName); try { I_CmsXmlContentValue elementValue = resourceDocument.getValue(elementName, locale); out.println("\t- Value:\t" + CmsStringUtil.escapeHtml(elementValue.getStringValue(cmso))); } catch (Exception e) { out.print("\t- No value:\t" + e.getMessage().substring(0, 40)); } out.print("</li>"); } out.println("</ul>"); } /** If the resource is not of type structured content */ else { out.println("<h3>This file is of type " + resType + "</h3>"); } %> </body> </html>