
com.squeakysand.jcr.SimpleNode Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2010-2011 Craig S. Dickson (http://craigsdickson.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.squeakysand.jcr;
import java.util.*;
import javax.jcr.AccessDeniedException;
import javax.jcr.ItemNotFoundException;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.PathNotFoundException;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SimpleNode extends DelegatingNode implements Node {
private static final String SLING_RESOURCE_TYPE = "sling:resourceType";
private static final Logger LOG = LoggerFactory.getLogger(SimpleNode.class);
public static SimpleNode asSimpleNode(Node node) {
SimpleNode result = null;
if (node != null) {
if (node instanceof SimpleNode) {
result = (SimpleNode) node;
} else {
result = new SimpleNode(node);
}
}
return result;
}
public SimpleNode(Node node) {
super(node);
}
public List getAllChildNodes() {
return getChildNodes(ItemFilter.ACCEPT_ALL);
}
public List getAllProperties() {
return getProperties(ItemFilter.ACCEPT_ALL);
}
public SimpleNode getAncestorByPropertyValue(String propertyName, Value value) {
SimpleNode result = null;
SimpleNode ancestor = getSimpleParent();
while (ancestor != null) {
try {
if (ancestor.hasProperty(propertyName)) {
Property ancestorProperty = ancestor.getProperty(propertyName);
Value ancestorPropertyValue = ancestorProperty.getValue();
if (value == null && ancestorPropertyValue == null) {
result = ancestor;
break;
} else if (ancestorPropertyValue != null && ancestorPropertyValue.equals(value)) {
result = ancestor;
break;
}
}
} catch (RepositoryException e) {
LOG.error(e.getMessage(), e);
}
ancestor = ancestor.getSimpleParent();
}
return result;
}
public boolean getBooleanProperty(String propertyName) {
boolean result = Boolean.FALSE;
try {
if (hasProperty(propertyName)) {
result = getProperty(propertyName).getBoolean();
}
} catch (PathNotFoundException e) {
LOG.trace(e.getMessage(), e);
} catch (RepositoryException e) {
LOG.error(e.getMessage(), e);
}
return result;
}
public List getChildNodes(ItemFilter filter) {
List result = null;
if (filter == null) {
result = getAllChildNodes();
} else {
try {
NodeIterator nodeIterator = getNodes();
result = new ArrayList();
while (nodeIterator.hasNext()) {
Node node = nodeIterator.nextNode();
if (filter.filter(node)) {
result.add(new SimpleNode(node));
}
}
} catch (RepositoryException e) {
LOG.error(e.getMessage(), e);
}
}
if (result == null) {
result = Collections.emptyList();
}
return result;
}
public String getDescription() {
return getStringProperty(Property.JCR_DESCRIPTION);
}
public Long getLongProperty(String propertyName) {
Long result = null;
try {
Property property = getProperty(propertyName);
result = property.getLong();
} catch (PathNotFoundException e) {
LOG.trace(e.getMessage(), e);
} catch (RepositoryException e) {
LOG.error(e.getMessage(), e);
}
return result;
}
public List getMultiValuedStringProperty(String propertyName) {
List result = new ArrayList();
try {
Property property = getProperty(propertyName);
if (property.isMultiple()) {
Value[] values = property.getValues();
for (Value value : values) {
result.add(value.getString());
}
} else {
result.add(property.getString());
}
} catch (PathNotFoundException e) {
LOG.trace(e.getMessage(), e);
} catch (RepositoryException e) {
LOG.error(e.getMessage(), e);
}
return result;
}
public List getProperties(ItemFilter filter) {
List result = null;
if (filter == null) {
result = getAllProperties();
} else {
try {
PropertyIterator propertyIterator = getProperties();
result = new ArrayList();
while (propertyIterator.hasNext()) {
Property property = propertyIterator.nextProperty();
if (filter.filter(property)) {
result.add(property);
}
}
} catch (RepositoryException e) {
LOG.error(e.getMessage(), e);
}
}
if (result == null) {
result = Collections.emptyList();
}
return result;
}
public String getStringProperty(String propertyName) {
String result = null;
String simplePropertyName = null;
try {
simplePropertyName = new Name(propertyName, getSession().getWorkspace().getNamespaceRegistry()).getSimpleName();
Property property = getProperty(simplePropertyName);
result = property.getString();
} catch (PathNotFoundException e) {
LOG.trace(e.getMessage(), e);
} catch (RepositoryException e) {
LOG.error(e.getMessage(), e);
}
if (LOG.isDebugEnabled()) {
LOG.debug("property {} with simple name {} on node {} was {}", new Object[] {propertyName, simplePropertyName, this, result});
}
return result;
}
public List getStringPropertyValues(String propertyName) {
throw new RuntimeException();
}
public String getTitle() {
return getStringProperty(Property.JCR_TITLE);
}
@Override
public String toString() {
try {
return "SimpleNode{" + getPath() + '}';
} catch (RepositoryException e) {
LOG.error(e.getMessage(), e);
return e.getMessage();
}
}
public SimpleNode getSimpleParent() {
Node parent = null;
try {
parent = getParent();
} catch (ItemNotFoundException e) {
LOG.error(e.getMessage(), e);
} catch (AccessDeniedException e) {
LOG.error(e.getMessage(), e);
} catch (RepositoryException e) {
LOG.error(e.getMessage(), e);
}
return asSimpleNode(parent);
}
public boolean isCreatedBefore(Node node) {
throw new RuntimeException();
}
public boolean isCreatedBefore(Date date) {
throw new RuntimeException();
}
public boolean isCreatedAfter(Node node) {
throw new RuntimeException();
}
public boolean isCreatedAfter(Date date) {
throw new RuntimeException();
}
public boolean isCreatedInRange(Date start, Date end) {
throw new RuntimeException();
}
public boolean isModifiedBefore(Node node) {
throw new RuntimeException();
}
public boolean isModifiedBefore(Date date) {
throw new RuntimeException();
}
public boolean isModifiedAfter(Node node) {
throw new RuntimeException();
}
public boolean isModifiedAfter(Date date) {
throw new RuntimeException();
}
public boolean isModifiedInRange(Date start, Date end) {
throw new RuntimeException();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy