001/* 002 * Copyright 2010 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.workspaces.repository.jcr; 017 018import java.io.IOException; 019 020import javax.jcr.Node; 021import javax.jcr.Session; 022import javax.jcr.query.Query; 023import javax.jcr.query.QueryManager; 024import javax.jcr.query.QueryResult; 025import javax.jcr.query.Row; 026import javax.jcr.query.RowIterator; 027 028import org.apache.cocoon.ProcessingException; 029import org.apache.cocoon.environment.ObjectModelHelper; 030import org.apache.cocoon.environment.Request; 031import org.apache.cocoon.xml.AttributesImpl; 032import org.apache.cocoon.xml.XMLUtils; 033import org.apache.commons.lang3.StringUtils; 034import org.xml.sax.SAXException; 035 036/** 037 * Generate a list of path from an XPath query 038 */ 039public class QueryResultGenerator extends AbstractRepositoryGenerator 040{ 041 @SuppressWarnings("deprecation") 042 public void generate() throws IOException, SAXException, ProcessingException 043 { 044 Request request = ObjectModelHelper.getRequest(objectModel); 045 046 String workspaceName = parameters.getParameter("workspace", "default"); 047 048 contentHandler.startDocument(); 049 050 if (StringUtils.isNotBlank(workspaceName)) 051 { 052 _getRepository(request, workspaceName); 053 054 if (getLogger().isDebugEnabled()) 055 { 056 getLogger().debug("Trying to generate results for a XPath query"); 057 } 058 059 XMLUtils.startElement(contentHandler, "resultList"); 060 061 String query = parameters.getParameter("query", ""); 062 String language = parameters.getParameter("language", Query.XPATH); 063 064 if (StringUtils.isNotBlank(query)) 065 { 066 _populateQueryResult(_session, query, language); 067 } 068 069 XMLUtils.endElement(contentHandler, "resultList"); 070 } 071 072 contentHandler.endDocument(); 073 } 074 075 076 private void _populateQueryResult(Session session, String query, String language) throws SAXException 077 { 078 try 079 { 080 QueryManager queryManager = session.getWorkspace().getQueryManager(); 081 QueryResult queryResult = queryManager.createQuery(query, language).execute(); 082 String[] selectorNames = queryResult.getSelectorNames(); 083 String selector = selectorNames.length > 0 ? selectorNames[0] : null; 084 085 RowIterator rows = queryResult.getRows(); 086 087 while (rows.hasNext()) 088 { 089 Row row = rows.nextRow(); 090 Node node = selector == null ? row.getNode() : row.getNode(selector); 091 092 // Node can be null when querying with outer joins. 093 if (node != null) 094 { 095 AttributesImpl attr = new AttributesImpl(); 096 attr.addCDATAAttribute("path", node.getPath()); 097 attr.addCDATAAttribute("pathWithGroups", NodeGroupHelper.getPathWithGroups(node)); 098 attr.addCDATAAttribute("name", node.getName()); 099 XMLUtils.createElement(contentHandler, "result", attr); 100 } 101 } 102 } 103 catch (Exception e) 104 { 105 getLogger().error("Unable to execute query '" + query + "'", e); 106 XMLUtils.createElement(contentHandler, "error", e.toString()); 107 } 108 } 109 110}