use of javax.jcr.nodetype.NodeTypeDefinition in project jackrabbit by apache.
the class CompactNodeTypeDefWriter method write.
/**
* Writes the given list of QNodeTypeDefinition to the output writer including the
* used namespaces.
*
* @param defs collection of definitions
* @param session session
* @param out output writer
* @throws java.io.IOException if an I/O error occurs
*/
public static void write(Collection<NodeTypeDefinition> defs, Session session, Writer out) throws IOException {
CompactNodeTypeDefWriter w = new CompactNodeTypeDefWriter(out, session, true);
for (NodeTypeDefinition def : defs) {
w.write(def);
}
w.close();
}
use of javax.jcr.nodetype.NodeTypeDefinition in project jackrabbit by apache.
the class CndImporter method ensureNtBase.
private static void ensureNtBase(NodeTypeTemplate ntt, Map<String, NodeTypeTemplate> templates, NodeTypeManager nodeTypeManager) throws RepositoryException {
if (!ntt.isMixin() && !NT_BASE.equals(ntt.getName())) {
String[] supertypes = ntt.getDeclaredSupertypeNames();
if (supertypes.length == 0) {
ntt.setDeclaredSuperTypeNames(new String[] { NT_BASE });
} else {
// Check whether we need to add the implicit "nt:base" supertype
boolean needsNtBase = true;
for (String name : supertypes) {
NodeTypeDefinition std = templates.get(name);
if (std == null) {
std = nodeTypeManager.getNodeType(name);
}
if (std != null && !std.isMixin()) {
needsNtBase = false;
}
}
if (needsNtBase) {
String[] withNtBase = new String[supertypes.length + 1];
withNtBase[0] = NT_BASE;
System.arraycopy(supertypes, 0, withNtBase, 1, supertypes.length);
ntt.setDeclaredSuperTypeNames(withNtBase);
}
}
}
}
use of javax.jcr.nodetype.NodeTypeDefinition in project jackrabbit by apache.
the class NodeTypeManagerImpl method registerNodeTypes.
//--------------------------------------------------< new JSR 283 methods >
/**
* Registers or updates the specified <code>Collection</code> of
* <code>NodeTypeDefinition</code> objects. This method is used to register
* or update a set of node types with mutual dependencies. Returns an
* iterator over the resulting <code>NodeType</code> objects.
* <p>
* The effect of the method is "all or nothing"; if an error occurs, no node
* types are registered or updated.
* <p>
* Throws an <code>InvalidNodeTypeDefinitionException</code> if a
* <code>NodeTypeDefinition</code> within the <code>Collection</code> is
* invalid or if the <code>Collection</code> contains an object of a type
* other than <code>NodeTypeDefinition</code>.
* <p>
* Throws a <code>NodeTypeExistsException</code> if <code>allowUpdate</code>
* is <code>false</code> and a <code>NodeTypeDefinition</code> within the
* <code>Collection</code> specifies a node type name that is already
* registered.
* <p>
* Throws an <code>UnsupportedRepositoryOperationException</code> if this
* implementation does not support node type registration.
*
* @param definitions a collection of <code>NodeTypeDefinition</code>s
* @param allowUpdate a boolean
* @return the registered node types.
* @throws InvalidNodeTypeDefinitionException if a
* <code>NodeTypeDefinition</code> within the <code>Collection</code> is
* invalid or if the <code>Collection</code> contains an object of a type
* other than <code>NodeTypeDefinition</code>.
* @throws NodeTypeExistsException if <code>allowUpdate</code> is
* <code>false</code> and a <code>NodeTypeDefinition</code> within the
* <code>Collection</code> specifies a node type name that is already
* registered.
* @throws UnsupportedRepositoryOperationException if this implementation
* does not support node type registration.
* @throws RepositoryException if another error occurs.
* @since JCR 2.0
*/
public NodeTypeIterator registerNodeTypes(NodeTypeDefinition[] definitions, boolean allowUpdate) throws InvalidNodeTypeDefinitionException, NodeTypeExistsException, UnsupportedRepositoryOperationException, RepositoryException {
// make sure the editing session is allowed to register node types.
context.getAccessManager().checkRepositoryPermission(Permission.NODE_TYPE_DEF_MNGMT);
NodeTypeRegistry registry = context.getNodeTypeRegistry();
// split the node types into new and already registered node types.
// this way we can register new node types together with already
// registered node types which make circular dependencies possible
List<QNodeTypeDefinition> addedDefs = new ArrayList<QNodeTypeDefinition>();
List<QNodeTypeDefinition> modifiedDefs = new ArrayList<QNodeTypeDefinition>();
for (NodeTypeDefinition definition : definitions) {
// convert to QNodeTypeDefinition
QNodeTypeDefinition def = toNodeTypeDef(definition);
if (registry.isRegistered(def.getName())) {
if (allowUpdate) {
modifiedDefs.add(def);
} else {
throw new NodeTypeExistsException(definition.getName());
}
} else {
addedDefs.add(def);
}
}
try {
ArrayList<NodeType> result = new ArrayList<NodeType>();
// register new node types
result.addAll(registerNodeTypes(addedDefs));
// re-register already existing node types
for (QNodeTypeDefinition nodeTypeDef : modifiedDefs) {
registry.reregisterNodeType(nodeTypeDef);
result.add(getNodeType(nodeTypeDef.getName()));
}
return new NodeTypeIteratorAdapter(result);
} catch (InvalidNodeTypeDefException e) {
throw new InvalidNodeTypeDefinitionException(e.getMessage(), e);
}
}
use of javax.jcr.nodetype.NodeTypeDefinition in project jackrabbit-oak by apache.
the class NodeTypeTest method trivialUpdates.
@Test
public void trivialUpdates() throws Exception {
// test various trivial updates that should not trigger repository scans
// whether or not the repository scan happens can not be checked directly;
// it requires inspecting the INFO level log
String[] types = new String[] { "trivial1", "trivial2" };
ArrayList<NodeTypeTemplate> ntt = new ArrayList<NodeTypeTemplate>();
// adding node types
Session session = getAdminSession();
NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
for (String t : types) {
NodeTypeTemplate nt = manager.createNodeTypeTemplate();
nt.setName(t);
ntt.add(nt);
}
manager.registerNodeTypes(ntt.toArray(new NodeTypeTemplate[0]), false);
// adding an optional property
ntt = new ArrayList<NodeTypeTemplate>();
for (String t : types) {
NodeTypeDefinition ntd = manager.getNodeType(t);
PropertyDefinitionTemplate opt = manager.createPropertyDefinitionTemplate();
opt.setMandatory(false);
opt.setName("optional");
opt.setRequiredType(PropertyType.STRING);
PropertyDefinitionTemplate opts = manager.createPropertyDefinitionTemplate();
opts.setMandatory(false);
opts.setMultiple(true);
opts.setName("optionals");
opts.setRequiredType(PropertyType.STRING);
NodeTypeTemplate nt = manager.createNodeTypeTemplate(ntd);
List pdt = nt.getPropertyDefinitionTemplates();
pdt.add(opt);
pdt.add(opts);
ntt.add(nt);
}
manager.registerNodeTypes(ntt.toArray(new NodeTypeTemplate[0]), true);
// make one optional property mandatory
ntt = new ArrayList<NodeTypeTemplate>();
for (String t : types) {
NodeTypeDefinition ntd = manager.getNodeType(t);
PropertyDefinitionTemplate opt = manager.createPropertyDefinitionTemplate();
opt.setMandatory("trivial2".equals(t));
opt.setName("optional");
opt.setRequiredType(PropertyType.STRING);
PropertyDefinitionTemplate opts = manager.createPropertyDefinitionTemplate();
opts.setMandatory("trivial2".equals(t));
opts.setMultiple(true);
opts.setName("optionals");
opts.setRequiredType(PropertyType.STRING);
NodeTypeTemplate nt = manager.createNodeTypeTemplate(ntd);
List pdt = nt.getPropertyDefinitionTemplates();
pdt.add(opt);
pdt.add(opts);
ntt.add(nt);
}
// but update both node types
manager.registerNodeTypes(ntt.toArray(new NodeTypeTemplate[0]), true);
}
use of javax.jcr.nodetype.NodeTypeDefinition in project jackrabbit-oak by apache.
the class NodeTypeTest method removeMandatoryPropertyFlag.
@Test
public void removeMandatoryPropertyFlag() throws Exception {
Session session = getAdminSession();
Node root = session.getRootNode();
NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
String cnd = "<'test'='http://www.apache.org/jackrabbit/test'>\n" + "[test:MyType] > nt:unstructured\n" + " - test:mandatory (string) mandatory";
CndImporter.registerNodeTypes(new StringReader(cnd), session);
Node n = root.addNode("test", "test:MyType");
n.setProperty("test:mandatory", "value");
session.save();
try {
n.getProperty("test:mandatory").remove();
session.save();
fail("Must fail with ConstraintViolationException");
} catch (ConstraintViolationException e) {
// expected
session.refresh(false);
}
// remove the mandatory property flag
cnd = "<'test'='http://www.apache.org/jackrabbit/test'>\n" + "[test:MyType] > nt:unstructured\n" + " - test:mandatory (string)";
CndImporter.registerNodeTypes(new StringReader(cnd), session, true);
// check node type
NodeTypeDefinition ntd = manager.getNodeType("test:MyType");
assertEquals(1, ntd.getDeclaredPropertyDefinitions().length);
assertFalse(ntd.getDeclaredPropertyDefinitions()[0].isMandatory());
// now we should be able to remove the property
n.getProperty("test:mandatory").remove();
session.save();
}
Aggregations