use of javax.jcr.nodetype.NodeTypeDefinition in project jackrabbit-oak by apache.
the class ReadWriteNodeTypeManager method registerNodeTypes.
@Override
public final NodeTypeIterator registerNodeTypes(NodeTypeDefinition[] ntds, boolean allowUpdate) throws RepositoryException {
Root root = getWriteRoot();
try {
Tree tree = getOrCreateNodeTypes(root);
for (NodeTypeDefinition ntd : ntds) {
NodeTypeTemplateImpl template;
if (ntd instanceof NodeTypeTemplateImpl) {
template = (NodeTypeTemplateImpl) ntd;
} else {
// some external template implementation, copy before proceeding
template = new NodeTypeTemplateImpl(getNamePathMapper(), ntd);
}
template.writeTo(tree, allowUpdate);
}
root.commit();
refresh();
List<NodeType> types = new ArrayList<NodeType>(ntds.length);
for (NodeTypeDefinition ntd : ntds) {
types.add(getNodeType(ntd.getName()));
}
return new NodeTypeIteratorAdapter(types);
} catch (CommitFailedException e) {
String message = "Failed to register node types.";
throw e.asRepositoryException(message);
}
}
use of javax.jcr.nodetype.NodeTypeDefinition in project jackrabbit-oak by apache.
the class NodeTypeTest method updateNodeType.
@Test
public void updateNodeType() throws Exception {
Session session = getAdminSession();
Node root = session.getRootNode();
ValueFactory vf = session.getValueFactory();
NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
Node n = root.addNode("q1", "nt:query");
n.setProperty("jcr:statement", vf.createValue("statement"));
session.save();
NodeTypeDefinition ntd = manager.getNodeType("nt:query");
NodeTypeTemplate ntt = manager.createNodeTypeTemplate(ntd);
try {
manager.registerNodeType(ntt, true);
// no changes to the type, so the registration should be a no-op
} catch (ConstraintViolationException unexpected) {
fail();
}
// make the (still missing) jcr:language property mandatory
@SuppressWarnings("unchecked") List<PropertyDefinitionTemplate> pdts = ntt.getPropertyDefinitionTemplates();
for (PropertyDefinitionTemplate pdt : pdts) {
if ("jcr:language".equals(pdt.getName())) {
pdt.setMandatory(true);
}
}
try {
manager.registerNodeType(ntt, true);
fail();
} catch (ConstraintViolationException expected) {
// the registration fails because of the would-be invalid content
}
// add the jcr:language property so it can be made mandatory
n.setProperty("jcr:language", vf.createValue("language"));
session.save();
try {
manager.registerNodeType(ntt, true);
// now the mandatory property exists, so the type change is OK
} catch (ConstraintViolationException unexpected) {
fail();
}
}
use of javax.jcr.nodetype.NodeTypeDefinition in project jackrabbit-oak by apache.
the class NodeTypeUtils method createNodeType.
/**
* Creates a node type with the given properties.
*
* @param session the session
* @param name the name
* @param properties the properties
* @param superTypes the super types
* @param childrenTypes the children types
* @param baseType the base type
* @param isMixin the is mixin
* @return the string
* @throws RepositoryException the repository exception
*/
@SuppressWarnings("unchecked")
public static String createNodeType(Session session, String name, String[] properties, int[] propTypes, String[] superTypes, String[] childrenTypes, String baseType, boolean isMixin) throws RepositoryException {
NodeTypeManager ntm = session.getWorkspace().getNodeTypeManager();
NodeTypeTemplate ntt = ntm.createNodeTypeTemplate();
if (baseType != null) {
NodeTypeDefinition ntd = ntm.getNodeType(baseType);
ntt = ntm.createNodeTypeTemplate(ntd);
}
if ((superTypes != null) && (superTypes.length != 0)) {
ntt.setDeclaredSuperTypeNames(superTypes);
}
ntt.setOrderableChildNodes(false);
ntt.setName(name);
if (properties != null) {
for (int count = 0; count < properties.length; count++) {
ntt.getPropertyDefinitionTemplates().add(createPropertyDefTemplate(ntm, properties[count], propTypes[count]));
}
}
if (childrenTypes != null) {
ntt.getNodeDefinitionTemplates().add(createNodeDefTemplate(ntm, childrenTypes));
}
ntt.setMixin(isMixin);
ntm.registerNodeType(ntt, true);
return ntt.getName();
}
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 nsMapping the mapping from prefix to namespace URI.
* @param out output writer
* @throws java.io.IOException if an I/O error occurs
*/
public static void write(Collection<NodeTypeDefinition> defs, NamespaceMapping nsMapping, Writer out) throws IOException {
CompactNodeTypeDefWriter w = new CompactNodeTypeDefWriter(out, nsMapping, true);
for (NodeTypeDefinition def : defs) {
w.write(def);
}
w.close();
}
use of javax.jcr.nodetype.NodeTypeDefinition 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);
}
Aggregations