1 13 package info.magnolia.cms.taglibs; 14 15 import info.magnolia.cms.beans.config.ContentRepository; 16 import info.magnolia.cms.core.ItemType; 17 import info.magnolia.cms.core.search.Query; 18 import info.magnolia.cms.core.search.QueryResult; 19 import info.magnolia.context.MgnlContext; 20 21 import java.util.Collection ; 22 23 import javax.jcr.RepositoryException; 24 import javax.jcr.query.InvalidQueryException; 25 import javax.servlet.jsp.JspException ; 26 import javax.servlet.jsp.tagext.TagSupport ; 27 28 import org.apache.commons.lang.exception.NestableRuntimeException; 29 30 31 36 public class QueryTag extends TagSupport { 37 38 41 private static final long serialVersionUID = 222L; 42 43 46 private String var; 47 48 51 private String query; 52 53 56 private String repository = ContentRepository.WEBSITE; 57 58 61 private String nodeType = ItemType.CONTENT.getSystemName(); 62 63 66 private String type = Query.XPATH; 67 68 72 public void setQuery(String query) { 73 this.query = query; 74 } 75 76 80 public void setType(String type) { 81 this.type = type; 82 } 83 84 88 public void setVar(String var) { 89 this.var = var; 90 } 91 92 96 public void setRepository(String repository) { 97 this.repository = repository; 98 } 99 100 104 public void setNodeType(String nodeType) { 105 this.nodeType = nodeType; 106 } 107 108 111 public int doEndTag() throws JspException { 112 113 Query q; 114 try { 115 q = MgnlContext.getQueryManager(repository).createQuery(query, type); 116 } 117 catch (InvalidQueryException e) { 118 throw new NestableRuntimeException(e); 119 } 120 catch (RepositoryException e) { 121 throw new NestableRuntimeException(e); 122 } 123 124 QueryResult queryResult; 125 try { 126 queryResult = q.execute(); 127 } 128 catch (RepositoryException e) { 129 throw new NestableRuntimeException(e); 130 } 131 Collection result = queryResult.getContent(nodeType); 132 pageContext.setAttribute(var, result); 133 134 return EVAL_PAGE; 135 } 136 137 140 public void release() { 141 super.release(); 142 this.var = null; 143 this.query = null; 144 this.type = Query.XPATH; 145 this.repository = ContentRepository.WEBSITE; 146 this.nodeType = ItemType.CONTENT.getSystemName(); 147 } 148 } 149 | Popular Tags |