use of javax.jcr.nodetype.PropertyDefinition in project jackrabbit by apache.
the class AddMixinOperation method perform.
public Object perform(SessionContext context) throws RepositoryException {
int permissions = Permission.NODE_TYPE_MNGMT;
// is required in addition.
if (MIX_VERSIONABLE.equals(mixinName) || MIX_SIMPLE_VERSIONABLE.equals(mixinName)) {
permissions |= Permission.VERSION_MNGMT;
}
context.getItemValidator().checkModify(node, CHECK_LOCK | CHECK_CHECKED_OUT | CHECK_CONSTRAINTS | CHECK_HOLD, permissions);
NodeTypeManagerImpl ntMgr = context.getNodeTypeManager();
NodeTypeImpl mixin = ntMgr.getNodeType(mixinName);
if (!mixin.isMixin()) {
throw new RepositoryException(context.getJCRName(mixinName) + " is not a mixin node type");
}
Name primaryTypeName = node.getNodeState().getNodeTypeName();
NodeTypeImpl primaryType = ntMgr.getNodeType(primaryTypeName);
if (primaryType.isDerivedFrom(mixinName)) {
// new mixin is already included in primary type
return this;
}
// build effective node type of mixin's & primary type in order
// to detect conflicts
NodeTypeRegistry ntReg = context.getNodeTypeRegistry();
EffectiveNodeType entExisting;
try {
// existing mixin's
Set<Name> mixins = new HashSet<Name>(node.getNodeState().getMixinTypeNames());
// build effective node type representing primary type including
// existing mixin's
entExisting = ntReg.getEffectiveNodeType(primaryTypeName, mixins);
if (entExisting.includesNodeType(mixinName)) {
// new mixin is already included in existing mixin type(s)
return this;
}
// add new mixin
mixins.add(mixinName);
// try to build new effective node type (will throw in case
// of conflicts)
ntReg.getEffectiveNodeType(primaryTypeName, mixins);
} catch (NodeTypeConflictException e) {
throw new ConstraintViolationException(e.getMessage(), e);
}
// try to revert the changes in case an exception occurs
try {
// modify the state of this node
NodeState thisState = (NodeState) node.getOrCreateTransientItemState();
// add mixin name
Set<Name> mixins = new HashSet<Name>(thisState.getMixinTypeNames());
mixins.add(mixinName);
thisState.setMixinTypeNames(mixins);
// set jcr:mixinTypes property
node.setMixinTypesProperty(mixins);
// add 'auto-create' properties defined in mixin type
for (PropertyDefinition aPda : mixin.getAutoCreatedPropertyDefinitions()) {
PropertyDefinitionImpl pd = (PropertyDefinitionImpl) aPda;
// make sure that the property is not already defined
// by primary type or existing mixin's
NodeTypeImpl declaringNT = (NodeTypeImpl) pd.getDeclaringNodeType();
if (!entExisting.includesNodeType(declaringNT.getQName())) {
node.createChildProperty(pd.unwrap().getName(), pd.getRequiredType(), pd);
}
}
// recursively add 'auto-create' child nodes defined in mixin type
for (NodeDefinition aNda : mixin.getAutoCreatedNodeDefinitions()) {
NodeDefinitionImpl nd = (NodeDefinitionImpl) aNda;
// make sure that the child node is not already defined
// by primary type or existing mixin's
NodeTypeImpl declaringNT = (NodeTypeImpl) nd.getDeclaringNodeType();
if (!entExisting.includesNodeType(declaringNT.getQName())) {
node.createChildNode(nd.unwrap().getName(), (NodeTypeImpl) nd.getDefaultPrimaryType(), null);
}
}
} catch (RepositoryException re) {
// try to undo the modifications by removing the mixin
try {
node.removeMixin(mixinName);
} catch (RepositoryException re1) {
// silently ignore & fall through
}
throw re;
}
return this;
}
use of javax.jcr.nodetype.PropertyDefinition in project jackrabbit by apache.
the class LuceneQueryFactory method getComparisonQuery.
protected Query getComparisonQuery(DynamicOperand left, int transform, String operator, StaticOperand rigth, Map<String, NodeType> selectorMap) throws RepositoryException {
if (left instanceof PropertyValue) {
PropertyValue pv = (PropertyValue) left;
String field = npResolver.getJCRName(session.getQName(pv.getPropertyName()));
int type = PropertyType.UNDEFINED;
NodeType nt = selectorMap.get(pv.getSelectorName());
if (nt != null) {
for (PropertyDefinition pd : nt.getPropertyDefinitions()) {
if (pd.getName().equals(pv.getPropertyName())) {
type = pd.getRequiredType();
break;
}
}
}
return getPropertyValueQuery(field, operator, evaluator.getValue(rigth), type, transform);
} else if (left instanceof NodeName) {
return getNodeNameQuery(transform, operator, rigth);
} else if (left instanceof NodeLocalName) {
return getNodeLocalNameQuery(transform, operator, rigth);
} else {
throw new UnsupportedRepositoryOperationException(// FIXME
"Unknown operand type: " + left);
}
}
use of javax.jcr.nodetype.PropertyDefinition in project jackrabbit by apache.
the class GetPropertyNamesTest method testGetPropertyNames.
/**
* Check if the property names from the search results match the
* non-residual ones from the base node type
*/
public void testGetPropertyNames() throws RepositoryException {
String queryStatement = "/" + jcrRoot;
// build and execute search query
Query query = superuser.getWorkspace().getQueryManager().createQuery(queryStatement, qsXPATH);
QueryResult result = query.execute();
// Get the node's non-residual properties
PropertyDefinition[] pd = superuser.getWorkspace().getNodeTypeManager().getNodeType(ntBase).getDeclaredPropertyDefinitions();
List<String> singleValPropNames = new ArrayList<String>();
for (int i = 0; i < pd.length; i++) {
// only keep the single-value properties
if (!pd[i].isMultiple()) {
singleValPropNames.add(pd[i].getName());
}
}
// add jcr:path
singleValPropNames.add(jcrPath);
singleValPropNames.add(jcrScore);
String[] foundPropertyNames = result.getColumnNames();
Object[] realPropertyNames = singleValPropNames.toArray();
// sort the 2 arrays before comparing them
Arrays.sort(foundPropertyNames);
Arrays.sort(realPropertyNames);
assertTrue("Property names don't match", Arrays.equals(foundPropertyNames, realPropertyNames));
}
use of javax.jcr.nodetype.PropertyDefinition in project jackrabbit by apache.
the class NodeTypeDefinitionImpl method getDeclaredPropertyDefinitions.
/**
* {@inheritDoc}
*/
public PropertyDefinition[] getDeclaredPropertyDefinitions() {
QPropertyDefinition[] pda = ntd.getPropertyDefs();
PropertyDefinition[] propDefs = new PropertyDefinition[pda.length];
for (int i = 0; i < pda.length; i++) {
propDefs[i] = new PropertyDefinitionImpl(pda[i], null, resolver, valueFactory);
}
return propDefs;
}
use of javax.jcr.nodetype.PropertyDefinition in project jackrabbit-oak by apache.
the class AuthorizablePropertiesImpl method getAuthorizableProperty.
/**
* Returns the valid authorizable property identified by the specified
* property location or {@code null} if that property does not exist or
* isn't a authorizable property because it is protected or outside of the
* scope of the {@code authorizableTree}.
*
* @param authorizableTree The tree of the target authorizable.
* @param propertyLocation Location to be tested.
* @param verifyAncestor If true the property is tested to be a descendant
* of the node of this authorizable; otherwise it is expected that this
* test has been executed by the caller.
* @return a valid authorizable property or {@code null} if no such property
* exists or fi the property is protected or not defined by the rep:authorizable
* node type or one of it's sub-node types.
* @throws RepositoryException If an error occurs.
*/
@CheckForNull
private PropertyState getAuthorizableProperty(@Nonnull Tree authorizableTree, @Nonnull TreeLocation propertyLocation, boolean verifyAncestor) throws RepositoryException {
PropertyState property = propertyLocation.getProperty();
if (property == null) {
return null;
}
String authorizablePath = authorizableTree.getPath();
if (verifyAncestor && !Text.isDescendant(authorizablePath, propertyLocation.getPath())) {
log.debug("Attempt to access property outside of authorizable scope.");
return null;
}
Tree parent = propertyLocation.getParent().getTree();
if (parent == null) {
log.debug("Unable to determine definition of authorizable property at " + propertyLocation.getPath());
return null;
}
ReadOnlyNodeTypeManager nodeTypeManager = authorizable.getUserManager().getNodeTypeManager();
PropertyDefinition def = nodeTypeManager.getDefinition(parent, property, true);
if (def.isProtected() || (authorizablePath.equals(parent.getPath()) && !def.getDeclaringNodeType().isNodeType(UserConstants.NT_REP_AUTHORIZABLE))) {
return null;
}
return property;
}
Aggregations