use of javax.jcr.nodetype.NodeTypeManager in project jackrabbit by apache.
the class NodeSetPrimaryTypeTest method getPrimaryTypeName.
private static String getPrimaryTypeName(Session session, Node node) throws RepositoryException {
NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
NodeTypeIterator nts = manager.getPrimaryNodeTypes();
while (nts.hasNext()) {
String name = nts.nextNodeType().getName();
if (!name.equals(node.getPrimaryNodeType().getName())) {
return name;
}
}
return null;
}
use of javax.jcr.nodetype.NodeTypeManager in project jackrabbit by apache.
the class TreeComparator method createComplexTree.
/**
* Creates a complex example tree that uses as many features of the
* repository as possible
*
* @param save Save the tree to the workspace. If false, the tree remains in
* the session.
*/
public void createComplexTree(boolean save) {
Node rootNode = null;
Node nt = null;
Node pt = null;
Node mvp = null;
Node referenceable = null;
try {
Node src = (Node) session.getItem(sourceFolder);
rootNode = src.addNode(sc.rootNodeName);
nt = rootNode.addNode(sc.nodeTypesTestNode);
rootNode.addNode(sc.mixinTypeTestNode);
pt = rootNode.addNode(sc.propertyTypesTestNode);
rootNode.addNode(sc.sameNameChildrenTestNode);
mvp = rootNode.addNode(sc.multiValuePropertiesTestNode);
referenceable = rootNode.addNode(sc.referenceableNodeTestNode);
rootNode.addNode(sc.orderChildrenTestNode);
rootNode.addNode(sc.namespaceTestNode);
} catch (RepositoryException e) {
sc.log("Error while creating example tree: " + e.getMessage());
}
// Add nodes with mixin types
NodeTypeManager ntmgr = null;
NodeTypeIterator types = null;
try {
ntmgr = workspace.getNodeTypeManager();
types = ntmgr.getMixinNodeTypes();
} catch (RepositoryException e) {
fail("Cannot access NodeType iterator: " + e);
}
while (types.hasNext()) {
NodeType t = (NodeType) types.next();
String name = t.getName();
name = "Node_" + name.replaceAll(":", "_");
Node n = null;
try {
n = nt.addNode(name);
n.addMixin(t.getName());
// try saving, because some exceptions are thrown only at save time
session.save();
} catch (RepositoryException e) {
sc.log("Cannot create node with mixin node type: " + e);
// if saving failed for a node, then remove it again (or else the next save will fail on it)
try {
if (n != null) {
n.remove();
}
} catch (RepositoryException e1) {
sc.log("Could not remove node: " + e);
}
}
}
// Create all property types
try {
// String
pt.setProperty(sc.stringTestProperty, "This is a string.");
// Binary
byte[] byteArray = { (byte) 0, (byte) 255, (byte) 167, (byte) 100, (byte) 21, (byte) 6, (byte) 19, (byte) 71, (byte) 221 };
pt.setProperty(sc.binaryTestProperty, new ByteArrayInputStream(byteArray));
// Date
Calendar c = Calendar.getInstance();
c.set(2005, 6, 21, 13, 30, 5);
pt.setProperty(sc.dateTestProperty, c);
// Long
pt.setProperty(sc.longTestProperty, (long) (1 / 3));
// Double
pt.setProperty(sc.doubleTestProperty, (double) Math.PI);
// Boolean
pt.setProperty(sc.booleanTestProperty, true);
// Name
pt.setProperty(sc.nameTestProperty, session.getValueFactory().createValue(sc.jcrPrimaryType, PropertyType.NAME));
// Path
pt.setProperty(sc.pathTestProperty, session.getValueFactory().createValue("paths/dont/have/to/point/anywhere", PropertyType.PATH));
// Reference: Note that I only check if the node exists. We do not specify what happens with
// the UUID during serialization.
sc.ensureMixinType(referenceable, sc.mixReferenceable);
// some implementations may require a save after addMixin()
session.save();
pt.setProperty(sc.referenceTestProperty, referenceable);
// Create a boolean property on the root node, so I can test noRecurse and skipBinary at the same time
rootNode.setProperty(sc.binaryTestProperty, new ByteArrayInputStream(byteArray));
session.save();
} catch (Exception e) {
fail("Could not add property: " + e);
}
// multi value properties
String[] s = { "one", "two", "three" };
try {
mvp.setProperty(sc.multiValueTestProperty, s);
session.save();
} catch (RepositoryException e) {
sc.log("Could not create multi-value property: " + e);
}
// Save to the workspace. Note that export is from session anyway.
if (save) {
try {
session.save();
} catch (RepositoryException e) {
fail("Cannot save the example tree to the repository: " + e);
}
}
}
use of javax.jcr.nodetype.NodeTypeManager in project jackrabbit-oak by apache.
the class RepositorySidegradeTest method verifyCustomNodeTypes.
@Test
public void verifyCustomNodeTypes() throws Exception {
Session session = createAdminSession();
try {
NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
assertTrue(manager.hasNodeType("test:unstructured"));
NodeType type = manager.getNodeType("test:unstructured");
assertFalse(type.isMixin());
assertTrue(type.isNodeType("nt:unstructured"));
boolean foundDefaultString = false;
boolean foundDefaultPath = false;
for (PropertyDefinition pDef : type.getPropertyDefinitions()) {
if ("defaultString".equals(pDef.getName())) {
assertEquals(PropertyType.STRING, pDef.getRequiredType());
assertNotNull(pDef.getDefaultValues());
assertEquals(1, pDef.getDefaultValues().length);
assertEquals("stringValue", pDef.getDefaultValues()[0].getString());
foundDefaultString = true;
} else if ("defaultPath".equals(pDef.getName())) {
assertEquals(PropertyType.PATH, pDef.getRequiredType());
assertNotNull(pDef.getDefaultValues());
assertEquals(1, pDef.getDefaultValues().length);
assertEquals("/jcr:path/nt:value", pDef.getDefaultValues()[0].getString());
foundDefaultPath = true;
}
}
assertTrue("Expected property definition with name \"defaultString\"", foundDefaultString);
assertTrue("Expected property definition with name \"defaultPath\"", foundDefaultPath);
} finally {
session.logout();
}
}
use of javax.jcr.nodetype.NodeTypeManager in project jackrabbit-oak by apache.
the class RepositorySidegradeTest method createSourceContent.
@SuppressWarnings("unchecked")
protected NodeStore createSourceContent() throws Exception {
NodeStore source = SegmentNodeStoreBuilders.builder(new MemoryStore()).build();
Repository repository = new Jcr(new Oak(source)).createRepository();
Session session = repository.login(CREDENTIALS);
try {
JackrabbitWorkspace workspace = (JackrabbitWorkspace) session.getWorkspace();
NamespaceRegistry registry = workspace.getNamespaceRegistry();
registry.registerNamespace("test", "http://www.example.org/");
NodeTypeManager nodeTypeManager = workspace.getNodeTypeManager();
NodeTypeTemplate template = nodeTypeManager.createNodeTypeTemplate();
template.setName("test:unstructured");
template.setDeclaredSuperTypeNames(new String[] { "nt:unstructured" });
PropertyDefinitionTemplate pDef1 = nodeTypeManager.createPropertyDefinitionTemplate();
pDef1.setName("defaultString");
pDef1.setRequiredType(PropertyType.STRING);
Value stringValue = session.getValueFactory().createValue("stringValue");
pDef1.setDefaultValues(new Value[] { stringValue });
template.getPropertyDefinitionTemplates().add(pDef1);
PropertyDefinitionTemplate pDef2 = nodeTypeManager.createPropertyDefinitionTemplate();
pDef2.setName("defaultPath");
pDef2.setRequiredType(PropertyType.PATH);
Value pathValue = session.getValueFactory().createValue("/jcr:path/nt:value", PropertyType.PATH);
pDef2.setDefaultValues(new Value[] { pathValue });
template.getPropertyDefinitionTemplates().add(pDef2);
nodeTypeManager.registerNodeType(template, false);
template = nodeTypeManager.createNodeTypeTemplate();
template.setName("test:referenceable");
template.setDeclaredSuperTypeNames(new String[] { "nt:unstructured", "mix:referenceable" });
nodeTypeManager.registerNodeType(template, false);
Node root = session.getRootNode();
Node referenceable = root.addNode("referenceable", "test:unstructured");
referenceable.addMixin(NodeType.MIX_REFERENCEABLE);
Node versionable = root.addNode("versionable", NT_UNSTRUCTURED);
versionable.addMixin(MIX_VERSIONABLE);
Node child = versionable.addNode("child", "test:referenceable");
child.addNode("child2", NT_UNSTRUCTURED);
session.save();
session.getWorkspace().getVersionManager().checkin("/versionable");
Node properties = root.addNode("properties", "test:unstructured");
properties.setProperty("boolean", true);
Binary binary = session.getValueFactory().createBinary(new ByteArrayInputStream(BINARY));
try {
properties.setProperty("binary", binary);
} finally {
binary.dispose();
}
properties.setProperty("date", DATE);
properties.setProperty("decimal", new BigDecimal(123));
properties.setProperty("double", Math.PI);
properties.setProperty("long", 9876543210L);
properties.setProperty("reference", referenceable);
properties.setProperty("weak_reference", session.getValueFactory().createValue(referenceable, true));
properties.setProperty("mv_reference", new Value[] { session.getValueFactory().createValue(versionable, false) });
properties.setProperty("mv_weak_reference", new Value[] { session.getValueFactory().createValue(versionable, true) });
properties.setProperty("string", "test");
properties.setProperty("multiple", "a,b,c".split(","));
session.save();
binary = properties.getProperty("binary").getBinary();
try {
InputStream stream = binary.getStream();
try {
for (byte aBINARY : BINARY) {
assertEquals(aBINARY, (byte) stream.read());
}
assertEquals(-1, stream.read());
} finally {
stream.close();
}
} finally {
binary.dispose();
}
return source;
} finally {
session.logout();
}
}
use of javax.jcr.nodetype.NodeTypeManager in project jackrabbit-oak by apache.
the class RepositoryUpgradeTest method verifyReplacedBuiltinNodeTypes.
@Test
public void verifyReplacedBuiltinNodeTypes() throws Exception {
Session session = createAdminSession();
try {
NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
NodeType nt = manager.getNodeType(UserConstants.NT_REP_GROUP);
assertTrue("Migrated repository must have new nodetype definitions", nt.isNodeType(UserConstants.NT_REP_MEMBER_REFERENCES));
} finally {
session.logout();
}
}
Aggregations