use of javax.jcr.nodetype.NodeType in project jackrabbit by apache.
the class LuceneQueryFactory method getComparisonQuery.
protected Query getComparisonQuery(DynamicOperand left, int transform, String operator, StaticOperand rigth, Map<String, NodeType> selectorMap) throws RepositoryException {
if (left instanceof PropertyValue) {
PropertyValue pv = (PropertyValue) left;
String field = npResolver.getJCRName(session.getQName(pv.getPropertyName()));
int type = PropertyType.UNDEFINED;
NodeType nt = selectorMap.get(pv.getSelectorName());
if (nt != null) {
for (PropertyDefinition pd : nt.getPropertyDefinitions()) {
if (pd.getName().equals(pv.getPropertyName())) {
type = pd.getRequiredType();
break;
}
}
}
return getPropertyValueQuery(field, operator, evaluator.getValue(rigth), type, transform);
} else if (left instanceof NodeName) {
return getNodeNameQuery(transform, operator, rigth);
} else if (left instanceof NodeLocalName) {
return getNodeLocalNameQuery(transform, operator, rigth);
} else {
throw new UnsupportedRepositoryOperationException(// FIXME
"Unknown operand type: " + left);
}
}
use of javax.jcr.nodetype.NodeType in project jackrabbit by apache.
the class LuceneQueryBuilder method visit.
public Object visit(NodeTypeQueryNode node, Object data) {
List<Term> terms = new ArrayList<Term>();
try {
String mixinTypesField = resolver.getJCRName(NameConstants.JCR_MIXINTYPES);
String primaryTypeField = resolver.getJCRName(NameConstants.JCR_PRIMARYTYPE);
NodeTypeManager ntMgr = session.getWorkspace().getNodeTypeManager();
NodeType base = ntMgr.getNodeType(session.getJCRName(node.getValue()));
if (base.isMixin()) {
// search for nodes where jcr:mixinTypes is set to this mixin
Term t = new Term(FieldNames.PROPERTIES, FieldNames.createNamedValue(mixinTypesField, resolver.getJCRName(node.getValue())));
terms.add(t);
} else {
// search for nodes where jcr:primaryType is set to this type
Term t = new Term(FieldNames.PROPERTIES, FieldNames.createNamedValue(primaryTypeField, resolver.getJCRName(node.getValue())));
terms.add(t);
}
// now search for all node types that are derived from base
NodeTypeIterator allTypes = ntMgr.getAllNodeTypes();
while (allTypes.hasNext()) {
NodeType nt = allTypes.nextNodeType();
NodeType[] superTypes = nt.getSupertypes();
if (Arrays.asList(superTypes).contains(base)) {
Name n = session.getQName(nt.getName());
String ntName = nsMappings.translateName(n);
Term t;
if (nt.isMixin()) {
// search on jcr:mixinTypes
t = new Term(FieldNames.PROPERTIES, FieldNames.createNamedValue(mixinTypesField, ntName));
} else {
// search on jcr:primaryType
t = new Term(FieldNames.PROPERTIES, FieldNames.createNamedValue(primaryTypeField, ntName));
}
terms.add(t);
}
}
} catch (NameException e) {
exceptions.add(e);
} catch (RepositoryException e) {
exceptions.add(e);
}
if (terms.size() == 0) {
// exception occured
return new BooleanQuery();
} else if (terms.size() == 1) {
return new JackrabbitTermQuery(terms.get(0));
} else {
BooleanQuery b = new BooleanQuery();
for (Term term : terms) {
b.add(new JackrabbitTermQuery(term), Occur.SHOULD);
}
return b;
}
}
use of javax.jcr.nodetype.NodeType in project jackrabbit by apache.
the class NodeAddMixinTest method testAddSuccessfully.
/**
* Tests if <code>Node.addMixin(String mixinName)</code> adds the requested
* mixin and stores it in property <code>jcr:mixinTypes</code>
*/
public void testAddSuccessfully() throws NotExecutableException, RepositoryException {
Session session = testRootNode.getSession();
Node node = testRootNode.addNode(nodeName1, testNodeType);
String mixinName = NodeMixinUtil.getAddableMixinName(session, node);
if (mixinName == null) {
throw new NotExecutableException("No testable mixin node type found");
}
node.addMixin(mixinName);
// test if mixin is written to property jcr:mixinTypes immediately
Value[] mixinValues = node.getProperty(jcrMixinTypes).getValues();
boolean found = false;
for (int i = 0; i < mixinValues.length; i++) {
found |= mixinName.equals(mixinValues[i].getString());
}
if (!found) {
fail("Mixin type must be added to property " + jcrMixinTypes + " immediately.");
}
// it is implementation-specific if a added mixin is available
// before or after save therefore save before further tests
testRootNode.getSession().save();
// test if added mixin is available by node.getMixinNodeTypes()
NodeType[] mixins = node.getMixinNodeTypes();
found = false;
for (int i = 0; i < mixins.length; i++) {
found |= mixinName.equals(mixins[i].getName());
}
if (!found) {
fail("Mixin '" + mixinName + "' type not added.");
}
}
use of javax.jcr.nodetype.NodeType in project jackrabbit by apache.
the class AddNodeTest method testAbstractNodeType.
/**
* Tests if addNode() throws a ConstraintViolationException in case
* of an abstract node type.
*/
public void testAbstractNodeType() throws RepositoryException {
NodeTypeManager ntMgr = superuser.getWorkspace().getNodeTypeManager();
NodeTypeIterator nts = ntMgr.getPrimaryNodeTypes();
while (nts.hasNext()) {
NodeType nt = nts.nextNodeType();
if (nt.isAbstract()) {
try {
testRootNode.addNode(nodeName1, nt.getName());
superuser.save();
fail("Expected ConstraintViolationException.");
} catch (ConstraintViolationException e) {
// correct.
} finally {
superuser.refresh(false);
}
}
}
}
use of javax.jcr.nodetype.NodeType in project jackrabbit by apache.
the class NodeDiscoveringNodeTypesTest method testGetMixinNodeTypes.
/**
* Test if getMixinNodeType returns the node types according to the property
* "jcr:mixinTypes". Therefore a node with mixin types is located recursively
* in the entire repository. A NotExecutableException is thrown when no such
* node is found.
*/
public void testGetMixinNodeTypes() throws NotExecutableException, RepositoryException {
if (childNode == null) {
throw new NotExecutableException("Workspace does not have sufficient content for this test. " + "Root node must have at least one child node.");
}
Node node = locateNodeWithMixinNodeTypes(testRootNode);
if (node == null) {
throw new NotExecutableException("Workspace does not contain a node with mixin node types defined");
}
Value[] names = node.getProperty(jcrMixinTypes).getValues();
NodeType[] types = node.getMixinNodeTypes();
assertEquals("getMixinNodeTypes() does not return the same number of " + "node types as " + "getProperty(\"jcr:mixinTypes\").getValues()", types.length, names.length);
StringBuffer namesString = new StringBuffer();
for (int i = 0; i < names.length; i++) {
namesString.append("|" + names[i].getString() + "|");
}
for (int i = 0; i < types.length; i++) {
String pattern = "|" + types[i].getName() + "|";
assertTrue("getMixinNodeTypes() does not return the same node" + "types as getProperty(\"jcr:mixinTypes\").getValues()", namesString.indexOf(pattern) != -1);
assertTrue("All nodes returned by getMixinNodeTypes() must be" + "mixin", types[i].isMixin());
}
}
Aggregations