From 5ac70584f228eec2e0678f5ffb1732fd53554a64 Mon Sep 17 00:00:00 2001
From: Vivian Steller <vivian.steller@lemonize.de>
Date: Thu, 8 May 2014 16:02:32 +0200
Subject: [PATCH] added RestrictToTemplateEventListener that listens for
 events on nodes with a given template

---
 .../commands/RestrictToTemplateEventListener.java  | 122 +++++++++++++++++++++
 1 file changed, 122 insertions(+)
 create mode 100644 src/main/java/info/magnolia/module/observation/commands/RestrictToTemplateEventListener.java

diff --git a/src/main/java/info/magnolia/module/observation/commands/RestrictToTemplateEventListener.java b/src/main/java/info/magnolia/module/observation/commands/RestrictToTemplateEventListener.java
new file mode 100644
index 0000000..3840b9a
--- /dev/null
+++ b/src/main/java/info/magnolia/module/observation/commands/RestrictToTemplateEventListener.java
@@ -0,0 +1,122 @@
+/**
+ * This file Copyright (c) 2012-2013 Magnolia International
+ * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
+ *
+ *
+ * This file is dual-licensed under both the Magnolia
+ * Network Agreement and the GNU General Public License.
+ * You may elect to use one or the other of these licenses.
+ *
+ * This file is distributed in the hope that it will be
+ * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
+ * implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
+ * Redistribution, except as permitted by whichever of the GPL
+ * or MNA you select, is prohibited.
+ *
+ * 1. For the GPL license (GPL), you can redistribute and/or
+ * modify this file under the terms of the GNU General
+ * Public License, Version 3, as published by the Free Software
+ * Foundation.  You should have received a copy of the GNU
+ * General Public License, Version 3 along with this program;
+ * if not, write to the Free Software Foundation, Inc., 51
+ * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * 2. For the Magnolia Network Agreement (MNA), this file
+ * and the accompanying materials are made available under the
+ * terms of the MNA which accompanies this distribution, and
+ * is available at http://www.magnolia-cms.com/mna.html
+ *
+ * Any modifications to this file must keep this entire header
+ * intact.
+ *
+ */
+package info.magnolia.module.observation.commands;
+
+import info.magnolia.context.MgnlContext;
+import info.magnolia.jcr.util.NodeTypes;
+import info.magnolia.module.observation.BaseConfiguration;
+import info.magnolia.module.observation.ObservationConfiguration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.jcr.Node;
+import javax.jcr.PathNotFoundException;
+import javax.jcr.RepositoryException;
+import javax.jcr.observation.Event;
+
+/**
+ * Event listener class that implements the restrict path to template behavior.
+ */
+public class RestrictToTemplateEventListener extends CommandEventListener implements BaseConfiguration {
+    private static final Logger logger = LoggerFactory.getLogger(RestrictToTemplateEventListener.class);
+    // will set the path for the command of the node specified
+    private String template;
+
+    private ObservationConfiguration obsConf;
+
+    // This method returns the path up to the node type specified.
+    @Override
+    protected String getPath(Event event) throws RepositoryException {
+        String path = super.getPath(event);
+
+        switch (event.getType()) {
+        case Event.PROPERTY_ADDED:
+        case Event.PROPERTY_CHANGED:
+        case Event.PROPERTY_REMOVED:
+            path = path.substring(0, path.lastIndexOf("/"));
+            break;
+        }
+
+        if (template == null) {
+            return path;
+        }
+
+        // depends of the type of event
+        Node node;
+        try {
+            node = MgnlContext.getSystemContext().getJCRSession(getConfiguration().getRepository()).getNode(path);
+        } catch (PathNotFoundException e) {
+            if (event.getType() == Event.NODE_REMOVED) {
+                return path;
+            }
+            logger.warn("Path [{}] was not found and Event is not of type NODE_REMOVED. Returning null path", path);
+            return null;
+        }
+
+        while (node != null && !template.equals(NodeTypes.Renderable.getTemplate(node)) && path.length() > 1) {
+            node = node.getParent();
+            // no more parents
+            if (node != null) {
+                path = node.getPath();
+            }
+        }
+        // if after searching we don't find a node of the type we don't
+        // execute the event
+        final String nodeTemplate = NodeTypes.Renderable.getTemplate(node);
+        if (node == null) {
+            logger.warn("Returning null path as node was null.");
+            return null;
+        }
+        return path;
+    }
+
+    public String getTemplate() {
+        return template;
+    }
+
+    public void setTemplate(String template) {
+        this.template = template;
+    }
+
+    @Override
+    public ObservationConfiguration getConfiguration() {
+        return obsConf;
+    }
+
+    @Override
+    public void setConfiguration(ObservationConfiguration obs) {
+        this.obsConf = obs;
+    }
+
+}
-- 
1.7.11.1

