JSP Codes

From VYRE

Jump to: navigation, search

JSP allows users to write custom server side code in VYRE. The article is intended to reference for some of the common JSP code that are used in the page template and content template.

Note that when using some of these code snippets within page templates, although not shown in the examples, some methods will need to be wrapped in try catch statements. For instance, if you are trying to access the current item, the code will need to use a try/catch to prevent errors on the page template level (There is no current item to get hold of).


Contents

Path related

Getting the context path of the current web application

<% request.setAttribute("contextPath", vyre.publishing.portal.PortalAttributes.getInstance().getPortalContextPath()); %>

Getting the current path (url) of the page

<%!
    String getCurrentPath() {
        String currentPath = "";
        try {
            currentPath = vyre.publishing.portal.PortalAttributes.getInstance().getPage().getBase().getPath();
        } catch( Exception e ) { }
        return currentPath; 
    }
%>

Page related

Getting ID of the current site

NOTE: below will be empty string if on a template.

   Site site = (Site) request.getAttribute(vyre.publishing.Keys.SITE);
   long siteId = site.getId();

Getting title of the current page

   Page page = (Page) request.getAttribute(vyre.publishing.Keys.PAGE);
   String pageTitle;
   if (page != null) {  
     pageTitle = page.getBase().getTitle();
   } else {
     // most likely on a page template or AJAX
   }

Item related

Getting the ID of the currently display item

<%
    Item currentItem = (Item) request.getAttribute(vyre.publishing.Keys.ITEM);
    String currentItemId;
    if (currentItem != null) {
       currentItemId = currentItem.getId();
    } else {
       // current item is not set
    }
%>

Getting item attribute value, before Unify 4.8 (since deprecated)

<%
    Attribute attribute = item.findFirstAttributeByDefinitionName(attributeName);
    String attributeValue;
    if (attribute != null) {
       attributeValue = attribute.getStringValue();
    } else {
       // attribute value is not set for this item
    }
%>

getting item attribute value, since Unify 4.8

<%
    boolean isContent = ...;   // either true if content attribute is required, or false if metadata
    String attributeValue;
    if (isContent) {
       attributeValue = item.getContent().getAttributeValue("<attribute_definition_id>");
    } else {
       attributeValue = item.getMetadata().getAttributeValue("<attribute_definition_id>");
    }
    if (attributeValue == null) {
       // handle attribute value absence
    }
%>

getting non-string attribute value, since Unify 4.8

    boolean isContent = ...;   // either true if content attribute is required, or false if metadata
    Integer attributeValue;
    // possible classes: Integer, String, Boolean, Double, Date. Attribute definition should be of relevant type
    if (isContent) {
       // replace Integer.class with relevant type your attribute definition is
       attributeValue = item.getContent().getAttributeValue("<attribute_definition_id>", Integer.class);
    } else {
       // replace Integer.class with relevant type your attribute definition is
       attributeValue = item.getMetadata().getAttributeValue("<attribute_definition_id>", Integer.class);
    }
    if (attributeValue == null) {
       // handle attribute value absence
    }

Setting item attribute value, up to Unify 4.8, since deprecated

    if (item.getMetadata().findFirstAttribute(attId) == null) {
        Attribute attribute = new Attribute(this.attributeDefDAO.load(attId));
        attribute.setValue(val);
        item.addAttribute(attribute);
    } else {
        item.getMetadata().findFirstAttribute(attId).setValue(val);
    }

Creating or modifying attribute, from Unify 4.8

    Item item = ...;    // item must be checked out or not yet persisted
    AttributeDefinition definition = ...;
    Attribute attribute = new Attribute(definition);
    attribute.setValue("new value");  // should be String or Date for date types
    item.setAttribute(attribute);

Removing attribute, from Unify 4.8

    Item item = ...;      // item must be checked out or not yet persisted
    AttributeDefinition definition = ...;
    item.removeAttribute(definition);

Getting the user id of the creator of the item

<%
vyre.content.items.Item item = vyre.publishing.portal.PortalAttributes.getInstance().getItem();
if (item != null) {
String itemCreatorUserId = item.getBase().getCreatorProfile().getUserId(); 
request.setAttribute("itemCreatorUserId", itemCreatorUserId );
}
%>

User related

  • groupIds : a comma separated string containing the IDs of groups which the current user is member of
 
<%
 // get user groups
 String groupIds = ",";
 for ( vyre.realms.Group g : vyre.publishing.portal.PortalAttributes.getInstance().getUser().getGroups() ) { groupIds += g.getId() + ","; }
 request.setAttribute("groupIds", groupIds );
%>
  • currentUserProfileId : the profile ID of the user which is logged in
 
<% request.setAttribute("currentUserProfileId", vyre.publishing.portal.PortalAttributes.getInstance().getUser().getProfile().getId()); %>
  • current_user_fullname : the full name of the current user
 
<% request.setAttribute("current_user_fullname", vyre.publishing.portal.PortalAttributes.getInstance().getUser().getFullName()); %>
  • current_user_email : the email of the current user
 
<% request.setAttribute("current_user_email", vyre.publishing.portal.PortalAttributes.getInstance().getUser().getEmailAddress()); %>
  • current_user_name : the username of the current user
 
<% request.setAttribute("current_user_name", vyre.publishing.portal.PortalAttributes.getInstance().getUser().getUsername()); %>
  • current_user_name : the current user
 
<% request.setAttribute("currentUser", vyre.realms.UserSessionMapper.getUser(session)); %>
  • current_user_<custom_profile_property_name> : a custom profile property of the current user
<% request.setAttribute("current_user_<custom_profile_property_name>", vyre.publishing.portal.PortalAttributes.getInstance().getUser().getProfile().getProperty("<custom_profile_property_name>")); %>

e.g. getting a user's agency

<% request.setAttribute("agency", vyre.publishing.portal.PortalAttributes.getInstance().getUser().getProfile().getProperty("agency")); %>

Others

  • renderAttributes - access to renderAttributes map
<%
  vyre.publishing.portal.PortalAttributes portalAtts =  vyre.publishing.portal.PortalAttributes.getInstance();
  java.util.Map<String,Object> renderAttributes = portalAtts.getRenderAttributes();
  request.setAttribute( vyre.publishing.Variables.RENDER_ATTRIBUTES_KEYWORD, renderAttributes );
%>
  • renderAttributes values - access to renderAttributes value
<%
String getRenderAttributeVaue(String key) {
 
    vyre.publishing.portal.PortalAttributes portalAtts =  vyre.publishing.portal.PortalAttributes.getInstance();
 
    String value = "";
    try {
    	java.util.Map<String,Object> renderAttributes = portalAtts.getRenderAttributes();
        value= renderAttributes.get(key);
    } catch( Exception e ) {}
    return value;
}
%>

Date related

  • current_date - returns the date
<%
  java.util.Date date = new java.util.Date();
  request.setAttribute("current_date", date ); 
%>
  • current_year - returns the year
<%
  java.util.Calendar cal = java.util.Calendar.getInstance();
  request.setAttribute("current_year", cal.get(java.util.Calendar.YEAR)); 
%>

JSP Content Template Code Usage

Example 1: Do not display the delete button if the item does not exist

<c:if test ="${item!=null}">
	<a href = "javascript: remove();">delete</a>
</c:if>

Example 2: If an value of an certain attribute is true execute a set of code

<vyre-item:attribute-value id="32" var="workflow"/>
<c:if test ="${workflow == 'waiting approval'}">....</c:if>

Example 3: setMaxClauseCount sets the maximum number of clauses permitted per BooleanQuery.

<%org.apache.lucene.search.BooleanQuery.setMaxClauseCount(10000);%>

Example 4: Accessing parameters from the url
Parameter can be appended to the end of a url (eg. http://www.eftar.com/item123?age=23) starting a '?' and name=value

<% String value = request.getParameter("age"); %>

Normalise Namespace

Generic Method of accesing javascript methods generated by unify that have portlet id attached to end of the function name.

 <%--portletId--%>
 <input type="hidden" value="<portlet:namespace />" id="namespace"/>
 
<script type="text/javascript">
 
	/**
		Normalise namespacing for linking api
	*/
	var normalisedMethodNames = [
	 "addSelectedLink","addSelectedUserLink",
	 "removeLink", "removeUserLink",
	 "updateLinks", "updateUserLinks"
	]
 
 
	var portletId ="${namespace}";
	function normaliseMethods(){
		for(var i =0; i<normalisedMethodNames.length; i++) {
			var methodName = normalisedMethodNames[i];
			window[methodName] = window[methodName+portletId];
		}
	}
	normaliseMethods();
</script>
Personal tools