use of javax.jcr.nodetype.PropertyDefinitionTemplate in project kylo by Teradata.
the class JcrExtensibleTypeProvider method buildType.
/**
* Builds the type defined by the specified builder.
*
* @param builder the builder for the type
* @return the type
* @throws MetadataRepositoryException if the repository is unavailable
* @throws TypeAlreadyExistsException if a type with the same name already exists
*/
@Nonnull
private ExtensibleType buildType(@Nonnull final TypeBuilder builder) {
try {
// Remove existing type and subgraph
if (builder.node != null) {
builder.node.remove();
}
// Get or create the type node
final Session session = getSession();
final Node typeNode;
if (!session.nodeExists(JcrUtil.path(session.getRootNode().getPath(), ExtensionsConstants.TYPES + "/" + builder.name).toString())) {
typeNode = session.getRootNode().addNode(ExtensionsConstants.TYPES + "/" + builder.name, ExtensionsConstants.TYPE_DESCRIPTOR_TYPE);
} else {
throw new TypeAlreadyExistsException(builder.name);
}
// Update type metadata
if (builder.displayName != null) {
typeNode.setProperty(JcrExtensibleType.NAME, builder.displayName);
}
if (builder.description != null) {
typeNode.setProperty(JcrExtensibleType.DESCRIPTION, builder.description);
}
// Update type definition
final NodeTypeManager typeMgr = session.getWorkspace().getNodeTypeManager();
final NodeTypeTemplate nodeTemplate = typeMgr.createNodeTypeTemplate();
nodeTemplate.setName(builder.name);
if (builder.supertype != null) {
final JcrExtensibleType superImpl = (JcrExtensibleType) builder.supertype;
final String supername = superImpl.getJcrName();
nodeTemplate.setDeclaredSuperTypeNames(new String[] { ExtensionsConstants.EXTENSIBLE_ENTITY_TYPE, supername });
} else {
nodeTemplate.setDeclaredSuperTypeNames(new String[] { ExtensionsConstants.EXTENSIBLE_ENTITY_TYPE });
}
// Update field definitions
@SuppressWarnings("unchecked") final List<PropertyDefinitionTemplate> propertyDefinitionTemplates = nodeTemplate.getPropertyDefinitionTemplates();
for (final FieldBuilder bldr : builder.fieldBuilders) {
// Create field
PropertyDefinitionTemplate propDef = typeMgr.createPropertyDefinitionTemplate();
propDef.setName(bldr.name);
propDef.setRequiredType(asCode(bldr.type));
propDef.setMandatory(bldr.required);
propDef.setMultiple(bldr.collection);
propertyDefinitionTemplates.add(propDef);
// Set field metadata
Node fieldNode = typeNode.addNode(bldr.name, ExtensionsConstants.FIELD_DESCRIPTOR_TYPE);
for (final Map.Entry<String, String> entry : bldr.metadata.entrySet()) {
fieldNode.setProperty(entry.getKey(), entry.getValue());
}
}
NodeType nodeType = typeMgr.registerNodeType(nodeTemplate, true);
return new JcrExtensibleType(typeNode, nodeType);
} catch (RepositoryException e) {
throw new MetadataRepositoryException("Failed to create type: " + builder.name, e);
}
}
use of javax.jcr.nodetype.PropertyDefinitionTemplate in project jackrabbit by apache.
the class NodeTypeCreationTest method testNewNodeTypeTemplate.
public void testNewNodeTypeTemplate() throws Exception {
String expandedName = "{" + NS_MIX_URI + "}" + "littlemixin";
String jcrName = superuser.getNamespacePrefix(NS_MIX_URI) + ":littlemixin";
NodeTypeTemplate ntt = ntm.createNodeTypeTemplate();
ntt.setName(expandedName);
assertEquals(jcrName, ntt.getName());
ntt.setName(jcrName);
assertEquals(jcrName, ntt.getName());
ntt.setAbstract(false);
assertFalse(ntt.isAbstract());
try {
ntt.setDeclaredSuperTypeNames(null);
fail("null isn't a valid array of jcr name");
} catch (ConstraintViolationException e) {
// success
}
assertNotNull(ntt.getDeclaredSupertypeNames());
assertEquals(0, ntt.getDeclaredSupertypeNames().length);
ntt.setDeclaredSuperTypeNames(new String[] { mixReferenceable });
assertNotNull(ntt.getDeclaredSupertypeNames());
assertEquals(1, ntt.getDeclaredSupertypeNames().length);
assertEquals(mixReferenceable, ntt.getDeclaredSupertypeNames()[0]);
ntt.setMixin(true);
assertTrue(ntt.isMixin());
ntt.setOrderableChildNodes(true);
assertTrue(ntt.hasOrderableChildNodes());
ntt.setQueryable(false);
assertFalse(ntt.isQueryable());
ntt.setPrimaryItemName(null);
assertNull(ntt.getPrimaryItemName());
ntt.setPrimaryItemName(jcrPrimaryType);
assertEquals(jcrPrimaryType, ntt.getPrimaryItemName());
PropertyDefinitionTemplate pdTemplate = createBooleanPropTemplate();
List pdefs = ntt.getPropertyDefinitionTemplates();
pdefs.add(pdTemplate);
assertNotNull(ntt.getDeclaredPropertyDefinitions());
assertEquals(1, ntt.getDeclaredPropertyDefinitions().length);
assertEquals(pdTemplate, ntt.getDeclaredPropertyDefinitions()[0]);
pdefs = ntt.getPropertyDefinitionTemplates();
assertEquals(1, pdefs.size());
assertEquals(pdTemplate, pdefs.get(0));
NodeDefinitionTemplate ndTemplate = ntm.createNodeDefinitionTemplate();
List ndefs = ntt.getNodeDefinitionTemplates();
ndefs.add(ndTemplate);
assertNotNull(ntt.getDeclaredChildNodeDefinitions());
assertEquals(1, ntt.getDeclaredChildNodeDefinitions().length);
assertEquals(ndTemplate, ntt.getDeclaredChildNodeDefinitions()[0]);
ndefs = ntt.getNodeDefinitionTemplates();
assertEquals(1, ndefs.size());
assertEquals(ndTemplate, ndefs.get(0));
}
use of javax.jcr.nodetype.PropertyDefinitionTemplate 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
}
}
use of javax.jcr.nodetype.PropertyDefinitionTemplate 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.PropertyDefinitionTemplate in project jackrabbit by apache.
the class NodeTypeCreationTest method testResidualNames.
public void testResidualNames() throws Exception {
String residualName = "*";
NodeDefinitionTemplate ndt = ntm.createNodeDefinitionTemplate();
ndt.setName(residualName);
assertEquals(residualName, ndt.getName());
PropertyDefinitionTemplate pdt = ntm.createPropertyDefinitionTemplate();
pdt.setName(residualName);
assertEquals(residualName, pdt.getName());
}
Aggregations