Revision: 18478
Author: wko
Date: 2009-06-11 13:11:29 +0200 (Thu, 11 Jun 2009)
Log Message:
-----------
HSTTWO-627: Adding basic search functionality
Modified Paths:
--------------
ecm/site-toolkit/trunk/testsuite/site/src/main/java/org/hippoecm/hst/wicketexamples/WicketContentBrowserApplication.java
ecm/site-toolkit/trunk/testsuite/site/src/main/java/org/hippoecm/hst/wicketexamples/WicketContentBrowserPage.java
ecm/site-toolkit/trunk/testsuite/site/src/main/resources/org/hippoecm/hst/wicketexamples/WicketContentBrowserPage.html
ecm/site-toolkit/trunk/testsuite/site/src/main/webapp/WEB-INF/pa-web.xml
ecm/site-toolkit/trunk/testsuite/site/src/main/webapp/WEB-INF/web.xml
Modified:
ecm/site-toolkit/trunk/testsuite/site/src/main/java/org/hippoecm/hst/wicketexamples/WicketContentBrowserApplication.java
===================================================================
---
ecm/site-toolkit/trunk/testsuite/site/src/main/java/org/hippoecm/hst/wicketexamples/WicketContentBrowserApplication.java
2009-06-11 10:23:01 UTC (rev 18477)
+++
ecm/site-toolkit/trunk/testsuite/site/src/main/java/org/hippoecm/hst/wicketexamples/WicketContentBrowserApplication.java
2009-06-11 11:11:29 UTC (rev 18478)
@@ -17,7 +17,6 @@
import javax.jcr.Credentials;
import javax.jcr.Repository;
-import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import javax.naming.Context;
import javax.naming.InitialContext;
@@ -30,7 +29,6 @@
private Repository repository;
private Credentials credentials;
private boolean credentialsConfigured = true;
- private String basePath;
@Override
public Class getHomePage() {
@@ -68,16 +66,4 @@
return credentials;
}
- public String getBasePath() {
- if (basePath == null) {
- basePath = getInitParameter("base-path");
-
- if (basePath == null) {
- basePath = "/jcr:root";
- }
- }
-
- return basePath;
- }
-
}
Modified:
ecm/site-toolkit/trunk/testsuite/site/src/main/java/org/hippoecm/hst/wicketexamples/WicketContentBrowserPage.java
===================================================================
---
ecm/site-toolkit/trunk/testsuite/site/src/main/java/org/hippoecm/hst/wicketexamples/WicketContentBrowserPage.java
2009-06-11 10:23:01 UTC (rev 18477)
+++
ecm/site-toolkit/trunk/testsuite/site/src/main/java/org/hippoecm/hst/wicketexamples/WicketContentBrowserPage.java
2009-06-11 11:11:29 UTC (rev 18478)
@@ -19,21 +19,28 @@
import java.util.List;
import javax.jcr.Credentials;
+import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Repository;
import javax.jcr.Session;
import javax.jcr.query.Query;
+import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
+import org.apache.wicket.markup.html.form.Button;
+import org.apache.wicket.markup.html.form.Form;
+import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
+import org.apache.wicket.markup.repeater.data.IDataProvider;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.apache.wicket.model.PropertyModel;
+import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.request.target.basic.RedirectRequestTarget;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -42,11 +49,23 @@
private Logger logger =
LoggerFactory.getLogger(WicketContentBrowserPage.class);
- protected String currentRelativePath = "";
+ protected String basePath;
+ protected int itemsPerPage = 10;
+ protected String currentPath;
+ protected String currentRelativePath;
protected List<ItemBean> currentItemBeans = new ArrayList<ItemBean>();
+ protected List<ItemBean> searchedItemBeans = new ArrayList<ItemBean>();
+
+ protected String searchQuery;
public WicketContentBrowserPage() {
+ basePath = ((WebApplication)
getApplication()).getInitParameter("basePath");
+
+ if (basePath == null) {
+ basePath = "/";
+ }
+
refreshCurrentPathItemBeans();
Label currentRelativePathLabel = new Label("currentRelativePath", new
PropertyModel(this, "currentRelativePath"));
@@ -56,11 +75,16 @@
private static final long serialVersionUID = 1L;
public void onClick() {
- if (currentRelativePath != null &&
!"".equals(currentRelativePath)) {
- int offset = currentRelativePath.lastIndexOf('/');
+ if (!"/".equals(currentPath)) {
+ int offset = currentPath.lastIndexOf('/');
if (offset >= 0) {
- currentRelativePath = currentRelativePath.substring(0,
offset);
+ currentPath = currentPath.substring(0, offset);
+
+ if ("".equals(currentPath)) {
+ currentPath = "/";
+ }
+
refreshCurrentPathItemBeans();
}
}
@@ -69,79 +93,65 @@
add(parentLink);
- final DataView itemView = new DataView("itemView", new
ListDataProvider(currentItemBeans)) {
+ final DataView itemView = new ItemBeansDataView("itemView", new
ListDataProvider(currentItemBeans)) {
private static final long serialVersionUID = 1L;
@Override
- protected void populateItem(Item item) {
- final ItemBean itemBean = (ItemBean) item.getModelObject();
-
- final String name = itemBean.getName();
- final String path = itemBean.getPath();
- String primaryNodeTypeName = null;
- String uuid = null;
- String content = null;
-
- if (itemBean instanceof NodeBean) {
- NodeBean nodeBean = (NodeBean) itemBean;
- primaryNodeTypeName = nodeBean.getPrimaryNodeTypeName();
- uuid = nodeBean.getUuid();
-
- if ("hippostd:html".equals(primaryNodeTypeName)) {
- Object [] props =
nodeBean.getProperty("hippostd:content");
-
- if (props != null && props.length > 0) {
- content = (String) props[0];
- }
- }
- }
-
- Link nameLink = null;
-
- if ("hippo:resource".equals(primaryNodeTypeName)) {
- // download link
- nameLink = new Link("nameLink") {
- private static final long serialVersionUID = 1L;
-
- public void onClick() {
- currentRelativePath += ("/" + name);
- getRequestCycle().setRequestTarget(new
RedirectRequestTarget("/binaries" + path));
- }
- };
- } else {
- // navigational link
- nameLink = new Link("nameLink") {
- private static final long serialVersionUID = 1L;
-
- public void onClick() {
- currentRelativePath += ("/" + name);
- refreshCurrentPathItemBeans();
- }
- };
- }
-
- nameLink.add(new Label("name", name));
- item.add(nameLink);
- item.add(new Label("primaryNodeTypeName",
primaryNodeTypeName));
- item.add(new Label("uuid", uuid));
- item.add(new Label("content",
content).setEscapeModelStrings(false));
+ protected void navigateToPath(String path) {
+ currentPath = path;
+ refreshCurrentPathItemBeans();
}
+
};
+
+ String param = ((WebApplication)
getApplication()).getInitParameter("itemsPerPage");
- itemView.setItemsPerPage(10);
+ if (param != null) {
+ itemsPerPage = Math.max(1, Integer.parseInt(param.trim()));
+ }
+
+ itemView.setItemsPerPage(itemsPerPage);
add(itemView);
add(new PagingNavigator("itemNavigator", itemView));
FeedbackPanel feedback = new FeedbackPanel("feedback");
feedback.setEscapeModelStrings(false);
add(feedback);
+ Form form = new Form("searchForm") {
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ public void onSubmit() {
+ if (searchQuery != null && !"".equals(searchQuery.trim())) {
+ searchItems();
+ }
+ }
+ };
+
+ form.add(new TextField("searchQuery", new PropertyModel(this,
"searchQuery")));
+ form.add(new Button("search"));
+ add(form);
+
+ final DataView searchedItemView = new
ItemBeansDataView("searchedItemView", new ListDataProvider(searchedItemBeans));
+
+ searchedItemView.setItemsPerPage(itemsPerPage);
+ add(searchedItemView);
+ add(new PagingNavigator("searchedItemNavigator", itemView));
}
public String getCurrentRelativePath() {
return currentRelativePath;
}
+ public void setSearchQuery(String searchQuery) {
+ this.searchQuery = searchQuery;
+ }
+
+ public String getSearchQuery() {
+ return searchQuery;
+ }
+
protected void refreshCurrentPathItemBeans() {
Repository repository = null;
Session session = null;
@@ -150,23 +160,36 @@
WicketContentBrowserApplication app =
(WicketContentBrowserApplication) getApplication();
repository = app.getDefaultRepository();
Credentials credentials = app.getDefaultCredentials();
- String basePath = app.getBasePath();
session = (credentials == null ? repository.login() :
repository.login(credentials));
- String statement = basePath + currentRelativePath +
(currentRelativePath.endsWith("/") ? "*" : "/*");
- Query query =
session.getWorkspace().getQueryManager().createQuery(statement, "xpath");
- QueryResult result = query.execute();
+ if (currentPath == null) {
+ currentPath = basePath;
+ }
+ currentRelativePath = currentPath;
+
+ if (!"/".equals(basePath) && currentPath.startsWith(basePath)) {
+ currentRelativePath = currentPath.substring(basePath.length());
+ }
+
+ Node rootNode = session.getRootNode();
+ Node currentNode = ("/".equals(currentPath) ? rootNode :
rootNode.getNode(currentPath.startsWith("/") ? currentPath.substring(1) :
currentPath));
+
+ NodeIterator nodeIt = currentNode.getNodes();
+
currentItemBeans.clear();
- for (NodeIterator it = result.getNodes(); it.hasNext(); ) {
-
currentItemBeans.add(ItemBeanFactory.createItemBean(it.nextNode()));
+ for ( ; nodeIt.hasNext(); ) {
+
currentItemBeans.add(ItemBeanFactory.createItemBean(nodeIt.nextNode()));
}
} catch (Exception e) {
if (logger.isWarnEnabled()) {
- logger.warn("Failed to query.", e);
+ logger.warn("Unable to retrieve nodes on " +
currentRelativePath, e);
}
+
+ FeedbackPanel feed = (FeedbackPanel) getPage().get("feedback");
+ feed.error("Unable to retrieve nodes on " + currentRelativePath +
": " + e.getMessage());
} finally {
if (session != null) {
try {
@@ -177,4 +200,109 @@
}
}
+ protected void searchItems() {
+ Repository repository = null;
+ Session session = null;
+
+ try {
+ WicketContentBrowserApplication app =
(WicketContentBrowserApplication) getApplication();
+ repository = app.getDefaultRepository();
+ Credentials credentials = app.getDefaultCredentials();
+
+ session = (credentials == null ? repository.login() :
repository.login(credentials));
+
+ QueryManager queryManager =
session.getWorkspace().getQueryManager();
+ String statement = "//element(*,
hippostd:html)[jcr:like(@hippostd:content, '%" + searchQuery + "%')]";
+ Query query = queryManager.createQuery(statement, "xpath");
+ QueryResult result = query.execute();
+ NodeIterator nodeIt = result.getNodes();
+
+ searchedItemBeans.clear();
+
+ for ( ; nodeIt.hasNext(); ) {
+
searchedItemBeans.add(ItemBeanFactory.createItemBean(nodeIt.nextNode()));
+ }
+ } catch (Exception e) {
+ if (logger.isWarnEnabled()) {
+ logger.warn("Unable to search nodes by " + searchQuery, e);
+ }
+
+ FeedbackPanel feed = (FeedbackPanel) getPage().get("feedback");
+ feed.error("Unable to search nodes by " + searchQuery + ": " +
e.getMessage());
+ } finally {
+ if (session != null) {
+ try {
+ session.logout();
+ } catch (Exception ce) {
+ }
+ }
+ }
+ }
+
+ protected class ItemBeansDataView extends DataView {
+
+ private static final long serialVersionUID = 1L;
+
+ protected ItemBeansDataView(String id, IDataProvider dataProvider) {
+ super(id, dataProvider);
+ }
+
+ @Override
+ protected void populateItem(Item item) {
+ final ItemBean itemBean = (ItemBean) item.getModelObject();
+
+ final String name = itemBean.getName();
+ final String path = itemBean.getPath();
+ String primaryNodeTypeName = null;
+ String uuid = null;
+ String content = null;
+
+ if (itemBean instanceof NodeBean) {
+ NodeBean nodeBean = (NodeBean) itemBean;
+ primaryNodeTypeName = nodeBean.getPrimaryNodeTypeName();
+ uuid = nodeBean.getUuid();
+
+ if ("hippostd:html".equals(primaryNodeTypeName)) {
+ Object [] props = nodeBean.getProperty("hippostd:content");
+
+ if (props != null && props.length > 0) {
+ content = (String) props[0];
+ }
+ }
+ }
+
+ Link nameLink = null;
+
+ if ("hippo:resource".equals(primaryNodeTypeName)) {
+ // download link
+ nameLink = new Link("nameLink") {
+ private static final long serialVersionUID = 1L;
+
+ public void onClick() {
+ getRequestCycle().setRequestTarget(new
RedirectRequestTarget("/binaries" + path));
+ }
+ };
+ } else {
+ // navigational link
+ nameLink = new Link("nameLink") {
+ private static final long serialVersionUID = 1L;
+
+ public void onClick() {
+ navigateToPath(path);
+ }
+ };
+ }
+
+ nameLink.add(new Label("name", name));
+ item.add(nameLink);
+ item.add(new Label("primaryNodeTypeName", primaryNodeTypeName));
+ item.add(new Label("uuid", uuid));
+ item.add(new Label("content",
content).setEscapeModelStrings(false));
+ }
+
+ protected void navigateToPath(String path) {
+ }
+
+ };
+
}
Modified:
ecm/site-toolkit/trunk/testsuite/site/src/main/resources/org/hippoecm/hst/wicketexamples/WicketContentBrowserPage.html
===================================================================
---
ecm/site-toolkit/trunk/testsuite/site/src/main/resources/org/hippoecm/hst/wicketexamples/WicketContentBrowserPage.html
2009-06-11 10:23:01 UTC (rev 18477)
+++
ecm/site-toolkit/trunk/testsuite/site/src/main/resources/org/hippoecm/hst/wicketexamples/WicketContentBrowserPage.html
2009-06-11 11:11:29 UTC (rev 18478)
@@ -63,6 +63,31 @@
<hr/>
+<h2>Search Contents</h2>
+<form wicket:id="searchForm">
+ <input type="text" wicket:id="searchQuery"/><input type="submit"
wicket:id="search" value="Search"/>
+</form>
+
+<table class="content-browser">
+ <tr class="content-browser">
+ <th class="content-browser">Name</th>
+ <th class="content-browser">Primary Type</th>
+ <th class="content-browser">UUID</th>
+ <th class="content-browser">Content</th>
+ </tr>
+ <tr wicket:id="searchedItemView" class="content-browser">
+ <td class="content-browser"><a href='#' wicket:id='nameLink'><span
wicket:id="name" class="content-browser">name</span></a></td>
+ <td class="content-browser"><span wicket:id="primaryNodeTypeName"
class="content-browser">primaryNodeTypeName</span></td>
+ <td class="content-browser"><span wicket:id="uuid"
class="content-browser">UUID</span></td>
+ <td class="content-browser"><span wicket:id="content"
class="content-browser">Content</span></td>
+ </tr>
+ <tr class="content-browser">
+ <th class="content-browser" colspan="4"><span
wicket:id="searchedItemNavigator" class="content-browser">Navigator</span></th>
+ </tr>
+</table>
+
+<hr/>
+
<em>Wicket Content Browser Example</em>
</body>
Modified:
ecm/site-toolkit/trunk/testsuite/site/src/main/webapp/WEB-INF/pa-web.xml
===================================================================
--- ecm/site-toolkit/trunk/testsuite/site/src/main/webapp/WEB-INF/pa-web.xml
2009-06-11 10:23:01 UTC (rev 18477)
+++ ecm/site-toolkit/trunk/testsuite/site/src/main/webapp/WEB-INF/pa-web.xml
2009-06-11 11:11:29 UTC (rev 18478)
@@ -59,6 +59,14 @@
<param-name>applicationClassName</param-name>
<param-value>org.hippoecm.hst.wicketexamples.WicketContentBrowserApplication</param-value>
</init-param>
+ <init-param>
+ <param-name>basePath</param-name>
+ <param-value>/</param-value>
+ </init-param>
+ <init-param>
+ <param-name>itemsPerPage</param-name>
+ <param-value>10</param-value>
+ </init-param>
</filter>
<filter-mapping>
Modified: ecm/site-toolkit/trunk/testsuite/site/src/main/webapp/WEB-INF/web.xml
===================================================================
--- ecm/site-toolkit/trunk/testsuite/site/src/main/webapp/WEB-INF/web.xml
2009-06-11 10:23:01 UTC (rev 18477)
+++ ecm/site-toolkit/trunk/testsuite/site/src/main/webapp/WEB-INF/web.xml
2009-06-11 11:11:29 UTC (rev 18478)
@@ -58,6 +58,14 @@
<param-name>applicationClassName</param-name>
<param-value>org.hippoecm.hst.wicketexamples.WicketContentBrowserApplication</param-value>
</init-param>
+ <init-param>
+ <param-name>basePath</param-name>
+ <param-value>/</param-value>
+ </init-param>
+ <init-param>
+ <param-name>itemsPerPage</param-name>
+ <param-value>10</param-value>
+ </init-param>
</filter>
<filter-mapping>
_______________________________________________
Hippocms-svn mailing list
[email protected]
http://lists.hippo.nl/mailman/listinfo/hippocms-svn