001 package com.khubla.pragmatach.examples.jcrexample.html;
002
003 import java.util.HashSet;
004 import java.util.Set;
005
006 import javax.jcr.Node;
007 import javax.jcr.NodeIterator;
008 import javax.jcr.Property;
009 import javax.jcr.PropertyIterator;
010 import javax.jcr.RepositoryException;
011 import javax.jcr.Session;
012
013 import com.khubla.pragmatach.framework.annotation.Controller;
014 import com.khubla.pragmatach.framework.annotation.Route;
015 import com.khubla.pragmatach.framework.annotation.View;
016 import com.khubla.pragmatach.framework.api.PragmatachException;
017 import com.khubla.pragmatach.framework.api.Response;
018 import com.khubla.pragmatach.plugin.freemarker.FreemarkerController;
019 import com.khubla.pragmatach.plugin.jcr.JCRSessionFactory;
020
021 /**
022 * @author tome
023 */
024 @Controller(name = "HTMLNodeChildrenController")
025 @View(view = "jcrnode.html")
026 public class HTMLNodeController extends FreemarkerController {
027 /***
028 * JCRSessionFactory
029 */
030 protected final JCRSessionFactory jcrSessionFactory = new JCRSessionFactory();
031 /**
032 * the node
033 */
034 private Node node;
035 /**
036 * subnodes
037 */
038 private Set<Node> subnodes;
039 /**
040 * current node path
041 */
042 private String jcrPath = "";
043
044 public String getJcrPath() {
045 return jcrPath;
046 }
047
048 public Node getNode() {
049 return node;
050 }
051
052 public Set<Node> getSubnodes() {
053 return subnodes;
054 }
055
056 public Set<Property> nodeProperties(Node node) throws RepositoryException {
057 final Set<Property> ret = new HashSet<Property>();
058 final PropertyIterator propertyIterator = node.getProperties();
059 while (propertyIterator.hasNext()) {
060 /*
061 * property
062 */
063 ret.add(propertyIterator.nextProperty());
064 }
065 return ret;
066 }
067
068 private void readSubNodes(Node node) throws RepositoryException {
069 subnodes = new HashSet<Node>();
070 final NodeIterator nodeIterator = node.getNodes();
071 while (nodeIterator.hasNext()) {
072 /*
073 * node
074 */
075 subnodes.add(nodeIterator.nextNode());
076 }
077 }
078
079 @Route(uri = "example/html/")
080 public Response render() throws PragmatachException {
081 try {
082 final Session session = jcrSessionFactory.getSession();
083 node = session.getRootNode();
084 readSubNodes(node);
085 jcrPath = "";
086 return super.render();
087 } catch (final Exception e) {
088 throw new PragmatachException("Exception in render", e);
089 }
090 }
091
092 @Route(uri = "example/html/*")
093 public Response render(String[] nodeName) throws PragmatachException {
094 try {
095 final Session session = jcrSessionFactory.getSession();
096 /*
097 * get the node path and remove the leading slashie
098 */
099 jcrPath = (getJcrPath() + buildWildcardResourceURI(nodeName)).substring(1) + "/";
100 node = session.getRootNode().getNode(jcrPath);
101 readSubNodes(node);
102 return super.render();
103 } catch (final Exception e) {
104 throw new PragmatachException("Exception in render", e);
105 }
106 }
107
108 public void setJcrPath(String jcrPath) {
109 this.jcrPath = jcrPath;
110 }
111
112 public void setNode(Node node) {
113 this.node = node;
114 }
115
116 public void setSubnodes(Set<Node> subnodes) {
117 this.subnodes = subnodes;
118 }
119 }