use of javax.jcr.nodetype.NodeTypeExistsException in project jackrabbit by apache.
the class NodeTypeManagerImpl method registerNodeTypes.
/**
* @see NodeTypeManager#registerNodeTypes(javax.jcr.nodetype.NodeTypeDefinition[], boolean)
*/
public NodeTypeIterator registerNodeTypes(NodeTypeDefinition[] ntds, boolean allowUpdate) throws RepositoryException {
List<QNodeTypeDefinition> defs = new ArrayList<QNodeTypeDefinition>(ntds.length);
for (NodeTypeDefinition definition : ntds) {
QNodeTypeDefinition qdef = new QNodeTypeDefinitionImpl(definition, getNamePathResolver(), mgrProvider.getQValueFactory());
if (!allowUpdate && hasNodeType(qdef.getName())) {
throw new NodeTypeExistsException("NodeType " + definition.getName() + " already exists.");
}
defs.add(qdef);
}
getNodeTypeRegistry().registerNodeTypes(defs, allowUpdate);
List<NodeType> nts = new ArrayList<NodeType>();
for (QNodeTypeDefinition def : defs) {
nts.add(getNodeType(def.getName()));
}
return new NodeTypeIteratorAdapter(nts);
}
use of javax.jcr.nodetype.NodeTypeExistsException in project jackrabbit-oak by apache.
the class NodeTypeTemplateImpl method writeTo.
/**
* Writes this node type as an {@code nt:nodeType} child of the given
* parent node. An exception is thrown if the child node already exists,
* unless the {@code allowUpdate} flag is set, in which case the existing
* node is overwritten.
*
* @param parent parent node under which to write this node type
* @param allowUpdate whether to overwrite an existing type
* @return The node type tree.
* @throws RepositoryException if this type could not be written
*/
Tree writeTo(@Nonnull Tree parent, boolean allowUpdate) throws RepositoryException {
String oakName = getOakName();
if (oakName == null) {
throw new RepositoryException("Cannot register node type: name is missing.");
}
Tree type = parent.getChild(oakName);
if (!type.exists()) {
type = parent.addChild(oakName);
type.setProperty(JCR_PRIMARYTYPE, NT_NODETYPE, Type.NAME);
} else if (!allowUpdate) {
throw new NodeTypeExistsException("Node type " + getName() + " already exists");
}
type.setProperty(JCR_NODETYPENAME, oakName, Type.NAME);
if (superTypeOakNames.length > 0) {
type.setProperty(JCR_SUPERTYPES, Arrays.asList(superTypeOakNames), Type.NAMES);
} else {
type.removeProperty(JCR_SUPERTYPES);
}
type.setProperty(JCR_IS_ABSTRACT, isAbstract);
type.setProperty(JCR_IS_QUERYABLE, queryable);
type.setProperty(JCR_ISMIXIN, isMixin);
// TODO fail (in validator?) if not orderable but a supertype is orderable
// See 3.7.6.7 Node Type Attribute Subtyping Rules (OAK-411)
type.setProperty(JCR_HASORDERABLECHILDNODES, isOrderable);
// See 3.7.6.7 Node Type Attribute Subtyping Rules (OAK-411)
if (primaryItemOakName != null) {
type.setProperty(JCR_PRIMARYITEMNAME, primaryItemOakName, Type.NAME);
} else {
type.removeProperty(JCR_PRIMARYITEMNAME);
}
// TODO fail (in validator?) on invalid item definitions
// See 3.7.6.8 Item Definitions in Subtypes (OAK-411)
writeItemDefinitions(type, propertyDefinitionTemplates, JCR_PROPERTYDEFINITION, NT_PROPERTYDEFINITION);
writeItemDefinitions(type, nodeDefinitionTemplates, JCR_CHILDNODEDEFINITION, NT_CHILDNODEDEFINITION);
return type;
}
use of javax.jcr.nodetype.NodeTypeExistsException 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.NodeTypeExistsException in project jackrabbit by apache.
the class NodeTypeCreationTest method testRegisterNodeType.
public void testRegisterNodeType() throws Exception {
NodeTypeTemplate ntt = ntm.createNodeTypeTemplate();
ntt.setName("mix:foo");
ntt.setAbstract(false);
ntt.setMixin(true);
ntt.setOrderableChildNodes(false);
ntt.setQueryable(false);
PropertyDefinitionTemplate pdt = ntm.createPropertyDefinitionTemplate();
pdt.setAutoCreated(false);
pdt.setName("foo");
pdt.setMultiple(false);
pdt.setRequiredType(PropertyType.STRING);
List pdefs = ntt.getPropertyDefinitionTemplates();
pdefs.add(pdt);
ntm.registerNodeType(ntt, true);
try {
ntm.registerNodeType(ntt, false);
fail("NodeTypeExistsException expected.");
} catch (NodeTypeExistsException e) {
// success
}
}
use of javax.jcr.nodetype.NodeTypeExistsException in project jackrabbit by apache.
the class NodeTypeCreationTest method testRegisterNodeTypes.
public void testRegisterNodeTypes() throws Exception {
NodeTypeDefinition[] defs = new NodeTypeDefinition[5];
for (int i = 0; i < defs.length; i++) {
NodeTypeTemplate ntt = ntm.createNodeTypeTemplate();
ntt.setName("mix:foo" + i);
ntt.setAbstract(false);
ntt.setMixin(true);
ntt.setOrderableChildNodes(false);
ntt.setQueryable(false);
PropertyDefinitionTemplate pdt = ntm.createPropertyDefinitionTemplate();
pdt.setAutoCreated(false);
pdt.setName("foo" + i);
pdt.setMultiple(false);
pdt.setRequiredType(PropertyType.STRING);
List pdefs = ntt.getPropertyDefinitionTemplates();
pdefs.add(pdt);
defs[i] = ntt;
}
ntm.registerNodeTypes(defs, true);
try {
ntm.registerNodeTypes(defs, false);
fail("NodeTypeExistsException expected.");
} catch (NodeTypeExistsException e) {
// success
}
}
Aggregations