use of org.apache.jackrabbit.spi.QValue 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.QValue in project jackrabbit by apache.
the class SessionImporter method importProperty.
/**
*
* @param pi
* @param parentState
* @param resolver
* @throws RepositoryException
* @throws ConstraintViolationException
*/
private void importProperty(PropInfo pi, NodeState parentState, NamePathResolver resolver) throws RepositoryException, ConstraintViolationException {
Name propName = pi.getName();
TextValue[] tva = pi.getValues();
int infoType = pi.getType();
PropertyState propState = null;
QPropertyDefinition def = null;
NodeEntry parentEntry = (NodeEntry) parentState.getHierarchyEntry();
PropertyEntry pEntry = parentEntry.getPropertyEntry(propName);
if (pEntry != null) {
// a property with that name already exists...
try {
PropertyState existing = pEntry.getPropertyState();
def = existing.getDefinition();
if (def.isProtected()) {
// skip protected property
log.debug("skipping protected property " + LogUtil.safeGetJCRPath(existing, session.getPathResolver()));
return;
}
if (def.isAutoCreated() && (existing.getType() == infoType || infoType == PropertyType.UNDEFINED) && def.isMultiple() == existing.isMultiValued()) {
// this property has already been auto-created, no need to create it
propState = existing;
} else {
throw new ItemExistsException(LogUtil.safeGetJCRPath(existing, session.getPathResolver()));
}
} catch (ItemNotFoundException e) {
// property doesn't exist any more
// -> ignore
}
}
Name[] parentNtNames = parentState.getAllNodeTypeNames();
if (def == null) {
// there's no property with that name, find applicable definition
if (tva.length == 1) {
// could be single- or multi-valued (n == 1)
def = session.getItemDefinitionProvider().getQPropertyDefinition(parentNtNames, propName, infoType);
} else {
// can only be multi-valued (n == 0 || n > 1)
def = session.getItemDefinitionProvider().getQPropertyDefinition(parentNtNames, propName, infoType, true);
}
if (def.isProtected()) {
// skip protected property
log.debug("skipping protected property " + propName);
return;
}
}
// retrieve the target property type needed for creation of QValue(s)
// including an eventual conversion. the targetType is then needed for
// setting/updating the type of the property-state.
int targetType = def.getRequiredType();
if (targetType == PropertyType.UNDEFINED) {
if (infoType == PropertyType.UNDEFINED) {
targetType = PropertyType.STRING;
} else {
targetType = infoType;
}
}
QValue[] values = getPropertyValues(pi, targetType, def.isMultiple(), resolver);
if (propState == null) {
// create new property
Operation ap = AddProperty.create(parentState, propName, targetType, def, values);
stateMgr.execute(ap);
propState = parentEntry.getPropertyEntry(propName).getPropertyState();
} else {
// modify value of existing property
Operation sp = SetPropertyValue.create(propState, values, targetType);
stateMgr.execute(sp);
}
// store reference for later resolution
if (propState.getType() == PropertyType.REFERENCE) {
refTracker.processedReference(propState);
}
}
use of org.apache.jackrabbit.spi.QValue in project jackrabbit by apache.
the class ReferenceChangeTracker method getMappedReference.
/**
* Returns the new UUID to which <code>oldUUID</code> has been mapped
* or <code>null</code> if no such mapping exists.
*
* @param oldReference old uuid represented by the given <code>QValue</code>.
* @param factory
* @return mapped new QValue of the reference value or <code>null</code> if no such mapping exists
* @see #mappedUUIDs(String,String)
*/
public QValue getMappedReference(QValue oldReference, QValueFactory factory) {
QValue remapped = null;
if (oldReference.getType() == PropertyType.REFERENCE) {
try {
String oldValue = oldReference.getString();
if (uuidMap.containsKey(oldValue)) {
String newValue = uuidMap.get(oldValue);
remapped = factory.create(newValue, PropertyType.REFERENCE);
}
} catch (RepositoryException e) {
log.error("Unexpected error while creating internal value.", e);
}
}
return remapped;
}
use of org.apache.jackrabbit.spi.QValue in project jackrabbit by apache.
the class AbstractJCR2SPITest method getDescriptors.
protected Map<String, QValue[]> getDescriptors() throws RepositoryException {
Map<String, QValue[]> descriptorKeys = new HashMap<String, QValue[]>();
QValueFactory qvf = QValueFactoryImpl.getInstance();
descriptorKeys.put(Repository.REP_NAME_DESC, new QValue[] { qvf.create("Mock Repository", PropertyType.STRING) });
descriptorKeys.put(Repository.REP_VENDOR_DESC, new QValue[] { qvf.create("Apache Software Foundation", PropertyType.STRING) });
descriptorKeys.put(Repository.REP_VENDOR_URL_DESC, new QValue[] { qvf.create("http://www.apache.org/", PropertyType.STRING) });
descriptorKeys.put(Repository.REP_VERSION_DESC, new QValue[] { qvf.create("2.0", PropertyType.STRING) });
descriptorKeys.put(Repository.SPEC_NAME_DESC, new QValue[] { qvf.create("Content Repository API for Java(TM) Technology Specification", PropertyType.STRING) });
descriptorKeys.put(Repository.SPEC_VERSION_DESC, new QValue[] { qvf.create("2.0", PropertyType.STRING) });
return descriptorKeys;
}
use of org.apache.jackrabbit.spi.QValue in project jackrabbit by apache.
the class SessionItemStateManager method visit.
/**
* @see OperationVisitor#visit(SetMixin)
*/
public void visit(SetMixin operation) throws ConstraintViolationException, AccessDeniedException, NoSuchNodeTypeException, UnsupportedRepositoryOperationException, VersionException, RepositoryException {
// NOTE: nodestate is only modified upon save of the changes!
Name[] mixinNames = operation.getMixinNames();
NodeState nState = operation.getNodeState();
NodeEntry nEntry = nState.getNodeEntry();
// assert the existence of the property entry and set the array of
// mixinNames to be set on the corresponding property state
PropertyEntry mixinEntry = nEntry.getPropertyEntry(NameConstants.JCR_MIXINTYPES);
if (mixinNames.length > 0) {
// update/create corresponding property state
if (mixinEntry != null) {
// execute value of existing property
PropertyState pState = mixinEntry.getPropertyState();
setPropertyStateValue(pState, getQValues(mixinNames, qValueFactory), PropertyType.NAME, operation.getOptions());
} else {
// create new jcr:mixinTypes property
ItemDefinitionProvider defProvider = mgrProvider.getItemDefinitionProvider();
QPropertyDefinition pd = defProvider.getQPropertyDefinition(nState.getAllNodeTypeNames(), NameConstants.JCR_MIXINTYPES, PropertyType.NAME, true);
QValue[] mixinValue = getQValues(mixinNames, qValueFactory);
addPropertyState(nState, pd.getName(), pd.getRequiredType(), mixinValue, pd, operation.getOptions());
}
nState.markModified();
transientStateMgr.addOperation(operation);
} else if (mixinEntry != null) {
// remove the jcr:mixinTypes property state if already present
PropertyState pState = mixinEntry.getPropertyState();
removeItemState(pState, operation.getOptions());
nState.markModified();
transientStateMgr.addOperation(operation);
}
// else: empty Name array and no mixin-prop-entry (should not occur)
}
Aggregations