use of org.apache.jackrabbit.spi.QPropertyDefinition in project jackrabbit by apache.
the class NodeTypeImpl method canSetProperty.
/**
* @see javax.jcr.nodetype.NodeType#canSetProperty(String, Value)
*/
public boolean canSetProperty(String propertyName, Value value) {
if (value == null) {
// setting a property to null is equivalent of removing it
return canRemoveItem(propertyName);
}
try {
Name name = resolver().getQName(propertyName);
QPropertyDefinition def;
try {
// try to get definition that matches the given value type
def = getApplicablePropDef(name, value.getType(), false);
} catch (ConstraintViolationException cve) {
// fallback: ignore type
def = getApplicablePropDef(name, PropertyType.UNDEFINED, false);
}
if (def.isProtected()) {
return false;
}
if (def.isMultiple()) {
return false;
}
Value v;
if (def.getRequiredType() != PropertyType.UNDEFINED && def.getRequiredType() != value.getType()) {
// type conversion required
v = ValueHelper.convert(value, def.getRequiredType(), mgrProvider.getJcrValueFactory());
} else {
// no type conversion required
v = value;
}
// create QValue from Value
QValue qValue = ValueFormat.getQValue(v, resolver(), mgrProvider.getQValueFactory());
checkSetPropertyValueConstraints(def, new QValue[] { qValue });
return true;
} catch (NameException re) {
// fall through
} catch (RepositoryException e) {
// fall through
}
return false;
}
use of org.apache.jackrabbit.spi.QPropertyDefinition in project jackrabbit by apache.
the class NodeTypeImpl method canSetProperty.
/**
* @see javax.jcr.nodetype.NodeType#canSetProperty(String, Value[])
*/
public boolean canSetProperty(String propertyName, Value[] values) {
if (values == null) {
// setting a property to null is equivalent of removing it
return canRemoveItem(propertyName);
}
try {
Name name = resolver().getQName(propertyName);
// determine type of values
int type = PropertyType.UNDEFINED;
for (int i = 0; i < values.length; i++) {
if (values[i] == null) {
// skip null values as those would be purged
continue;
}
if (type == PropertyType.UNDEFINED) {
type = values[i].getType();
} else if (type != values[i].getType()) {
// inhomogeneous types
return false;
}
}
QPropertyDefinition def;
try {
// try to get definition that matches the given value type
def = getApplicablePropDef(name, type, true);
} catch (ConstraintViolationException cve) {
// fallback: ignore type
def = getApplicablePropDef(name, PropertyType.UNDEFINED, true);
}
if (def.isProtected()) {
return false;
}
if (!def.isMultiple()) {
return false;
}
// determine target type
int targetType;
if (def.getRequiredType() != PropertyType.UNDEFINED && def.getRequiredType() != type) {
// type conversion required
targetType = def.getRequiredType();
} else {
// no type conversion required
targetType = type;
}
ArrayList<QValue> list = new ArrayList<QValue>();
// convert values and compact array (purge null entries)
for (int i = 0; i < values.length; i++) {
if (values[i] != null) {
// create QValue from Value and perform
// type conversion as necessary
Value v = ValueHelper.convert(values[i], targetType, mgrProvider.getJcrValueFactory());
QValue qValue = ValueFormat.getQValue(v, resolver(), mgrProvider.getQValueFactory());
list.add(qValue);
}
}
QValue[] internalValues = list.toArray(new QValue[list.size()]);
checkSetPropertyValueConstraints(def, internalValues);
return true;
} catch (NameException be) {
// implementation specific exception, fall through
} catch (RepositoryException re) {
// fall through
}
return false;
}
use of org.apache.jackrabbit.spi.QPropertyDefinition in project jackrabbit by apache.
the class NodeTypeImpl method getPropertyDefinitions.
/**
* @see javax.jcr.nodetype.NodeType#getPropertyDefinitions()
*/
public PropertyDefinition[] getPropertyDefinitions() {
QPropertyDefinition[] pda = ent.getAllQPropertyDefinitions();
PropertyDefinition[] propDefs = new PropertyDefinition[pda.length];
for (int i = 0; i < pda.length; i++) {
propDefs[i] = ntMgr.getPropertyDefinition(pda[i]);
}
return propDefs;
}
use of org.apache.jackrabbit.spi.QPropertyDefinition in project jackrabbit by apache.
the class NodeEntryImpl method complete.
private void complete(AddProperty operation) throws RepositoryException {
if (operation.getParentState().getHierarchyEntry() != this) {
throw new IllegalArgumentException();
}
PropertyEntry pe = getPropertyEntry(operation.getPropertyName());
if (pe != null && pe.getStatus() == Status.NEW) {
switch(operation.getStatus()) {
case Operation.STATUS_PERSISTED:
// for autocreated/protected props, mark to be reloaded
// upon next access.
PropertyState addedState = (PropertyState) ((PropertyEntryImpl) pe).internalGetItemState();
addedState.setStatus(Status.EXISTING);
QPropertyDefinition pd = addedState.getDefinition();
if (pd.isAutoCreated() || pd.isProtected()) {
pe.invalidate(true);
}
// else: assume added property is up to date.
break;
case Operation.STATUS_UNDO:
pe.revert();
break;
// ignore
default:
}
}
// else: no such prop entry or entry has already been persisted
// e.g due to external modifications merged into this NodeEntry.
}
use of org.apache.jackrabbit.spi.QPropertyDefinition in project jackrabbit by apache.
the class SessionImporter method importNode.
/**
*
* @param nodeInfo
* @param parent
* @return
* @throws ConstraintViolationException
* @throws ItemNotFoundException
* @throws RepositoryException
*/
private NodeState importNode(NodeInfo nodeInfo, NodeState parent) throws ConstraintViolationException, ItemNotFoundException, RepositoryException {
Name[] parentNtNames = parent.getAllNodeTypeNames();
if (parent.hasPropertyName(nodeInfo.getName())) {
/**
* a property with the same name already exists; if this property
* has been imported as well (e.g. through document view import
* where an element can have the same name as one of the attributes
* of its parent element) we have to rename the conflicting property;
*
* see http://issues.apache.org/jira/browse/JCR-61
*/
PropertyState conflicting = parent.getPropertyState(nodeInfo.getName());
if (conflicting.getStatus() == Status.NEW) {
// assume this property has been imported as well;
// rename conflicting property
// TODO: use better reversible escaping scheme to create unique name
Name newName = session.getNameFactory().create(nodeInfo.getName().getNamespaceURI(), nodeInfo.getName().getLocalName() + "_");
if (parent.hasPropertyName(newName)) {
newName = session.getNameFactory().create(newName.getNamespaceURI(), newName.getLocalName() + "_");
}
// since name changes, need to find new applicable definition
QPropertyDefinition propDef;
if (conflicting.getValues().length == 1) {
// could be single- or multi-valued (n == 1)
try {
// try single-valued
propDef = session.getItemDefinitionProvider().getQPropertyDefinition(parentNtNames, newName, conflicting.getType(), false);
} catch (ConstraintViolationException cve) {
// try multi-valued
propDef = session.getItemDefinitionProvider().getQPropertyDefinition(parentNtNames, newName, conflicting.getType(), true);
}
} else {
// can only be multi-valued (n == 0 || n > 1)
propDef = session.getItemDefinitionProvider().getQPropertyDefinition(parentNtNames, newName, conflicting.getType(), true);
}
Operation ap = AddProperty.create(parent, newName, conflicting.getType(), propDef, conflicting.getValues());
stateMgr.execute(ap);
Operation rm = Remove.create(conflicting);
stateMgr.execute(rm);
}
}
// do create new nodeState
QNodeDefinition def = session.getItemDefinitionProvider().getQNodeDefinition(parentNtNames, nodeInfo.getName(), nodeInfo.getNodeTypeName());
if (def.isProtected()) {
log.debug("Skipping protected nodeState (" + nodeInfo.getName() + ")");
return null;
} else {
Name ntName = nodeInfo.getNodeTypeName();
if (ntName == null) {
// use default node type
ntName = def.getDefaultPrimaryType();
}
Operation an = AddNode.create(parent, nodeInfo.getName(), ntName, nodeInfo.getUUID());
stateMgr.execute(an);
// retrieve id of state that has been created during execution of AddNode
NodeState childState = (NodeState) ((AddNode) an).getAddedStates().get(0);
// and set mixin types
Name[] mixinNames = nodeInfo.getMixinNames();
if (mixinNames != null && mixinNames.length > 0) {
Operation sm = SetMixin.create(childState, nodeInfo.getMixinNames());
stateMgr.execute(sm);
}
return childState;
}
}
Aggregations