use of org.apache.jackrabbit.spi.QValueConstraint in project jackrabbit by apache.
the class VirtualNodeTypeStateProvider method createPropertyDefState.
/**
* creates a node state for the given property def
*
* @param parent
* @param propDef
* @return
* @throws RepositoryException
*/
private VirtualNodeState createPropertyDefState(VirtualNodeState parent, QPropertyDefinition propDef, QNodeTypeDefinition ntDef, int n) throws RepositoryException {
NodeId id = calculateStableId(ntDef.getName().toString() + "/" + NameConstants.JCR_PROPERTYDEFINITION.toString() + "/" + n);
VirtualNodeState pState = createNodeState(parent, NameConstants.JCR_PROPERTYDEFINITION, id, NameConstants.NT_PROPERTYDEFINITION);
// add properties
if (!propDef.definesResidual()) {
pState.setPropertyValue(NameConstants.JCR_NAME, InternalValue.create(propDef.getName()));
}
pState.setPropertyValue(NameConstants.JCR_AUTOCREATED, InternalValue.create(propDef.isAutoCreated()));
pState.setPropertyValue(NameConstants.JCR_MANDATORY, InternalValue.create(propDef.isMandatory()));
pState.setPropertyValue(NameConstants.JCR_ONPARENTVERSION, InternalValue.create(OnParentVersionAction.nameFromValue(propDef.getOnParentVersion())));
pState.setPropertyValue(NameConstants.JCR_PROTECTED, InternalValue.create(propDef.isProtected()));
pState.setPropertyValue(NameConstants.JCR_MULTIPLE, InternalValue.create(propDef.isMultiple()));
pState.setPropertyValue(NameConstants.JCR_REQUIREDTYPE, InternalValue.create(PropertyType.nameFromValue(propDef.getRequiredType()).toUpperCase()));
InternalValue[] defVals = InternalValue.create(propDef.getDefaultValues());
// retrieve the property type from the first default value present with
// the property definition. in case no default values are defined,
// fallback to PropertyType.STRING in order to avoid creating a property
// with type UNDEFINED which is illegal.
int defValsType = PropertyType.STRING;
if (defVals != null && defVals.length > 0) {
defValsType = defVals[0].getType();
}
if (defVals != null) {
pState.setPropertyValues(NameConstants.JCR_DEFAULTVALUES, defValsType, defVals);
}
QValueConstraint[] vc = propDef.getValueConstraints();
InternalValue[] vals = new InternalValue[vc.length];
for (int i = 0; i < vc.length; i++) {
vals[i] = InternalValue.create(vc[i].getString());
}
pState.setPropertyValues(NameConstants.JCR_VALUECONSTRAINTS, PropertyType.STRING, vals);
return pState;
}
use of org.apache.jackrabbit.spi.QValueConstraint in project jackrabbit by apache.
the class NodeTypeWriter method addPropDef.
/**
* Builds a property definition element under the current element.
*
* @param def property definition
* @throws RepositoryException if the default values cannot
* be serialized
* @throws NamespaceException if the property definition contains
* invalid namespace references
*/
private void addPropDef(QPropertyDefinition def) throws NamespaceException, RepositoryException {
builder.startElement(Constants.PROPERTYDEFINITION_ELEMENT);
// simple attributes
builder.setAttribute(Constants.NAME_ATTRIBUTE, resolver.getJCRName(def.getName()));
builder.setAttribute(Constants.AUTOCREATED_ATTRIBUTE, def.isAutoCreated());
builder.setAttribute(Constants.MANDATORY_ATTRIBUTE, def.isMandatory());
builder.setAttribute(Constants.PROTECTED_ATTRIBUTE, def.isProtected());
builder.setAttribute(Constants.ONPARENTVERSION_ATTRIBUTE, OnParentVersionAction.nameFromValue(def.getOnParentVersion()));
builder.setAttribute(Constants.MULTIPLE_ATTRIBUTE, def.isMultiple());
builder.setAttribute(Constants.ISFULLTEXTSEARCHABLE_ATTRIBUTE, def.isFullTextSearchable());
builder.setAttribute(Constants.ISQUERYORDERABLE_ATTRIBUTE, def.isQueryOrderable());
// TODO do properly...
String[] qops = def.getAvailableQueryOperators();
if (qops != null && qops.length > 0) {
List<String> ops = Arrays.asList(qops);
List<String> defaultOps = Arrays.asList(Operator.getAllQueryOperators());
if (!ops.containsAll(defaultOps)) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < qops.length; i++) {
if (i > 0) {
sb.append(' ');
}
if (qops[i].equals(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO)) {
sb.append(Constants.EQ_ENTITY);
} else if (qops[i].equals(QueryObjectModelConstants.JCR_OPERATOR_NOT_EQUAL_TO)) {
sb.append(Constants.NE_ENTITY);
} else if (qops[i].equals(QueryObjectModelConstants.JCR_OPERATOR_GREATER_THAN)) {
sb.append(Constants.GT_ENTITY);
} else if (qops[i].equals(QueryObjectModelConstants.JCR_OPERATOR_GREATER_THAN_OR_EQUAL_TO)) {
sb.append(Constants.GE_ENTITY);
} else if (qops[i].equals(QueryObjectModelConstants.JCR_OPERATOR_LESS_THAN)) {
sb.append(Constants.LT_ENTITY);
} else if (qops[i].equals(QueryObjectModelConstants.JCR_OPERATOR_LESS_THAN_OR_EQUAL_TO)) {
sb.append(Constants.LE_ENTITY);
} else if (qops[i].equals(QueryObjectModelConstants.JCR_OPERATOR_LIKE)) {
sb.append(Constants.LIKE_ENTITY);
}
}
builder.setAttribute(Constants.AVAILABLEQUERYOPERATORS_ATTRIBUTE, sb.toString());
}
}
builder.setAttribute(Constants.REQUIREDTYPE_ATTRIBUTE, PropertyType.nameFromValue(def.getRequiredType()));
// value constraints
QValueConstraint[] constraints = def.getValueConstraints();
if (constraints != null && constraints.length > 0) {
builder.startElement(Constants.VALUECONSTRAINTS_ELEMENT);
for (QValueConstraint constraint : constraints) {
ValueConstraint vc = ValueConstraint.create(def.getRequiredType(), constraint.getString());
builder.addContentElement(Constants.VALUECONSTRAINT_ELEMENT, vc.getDefinition(resolver));
}
builder.endElement();
}
// default values
QValue[] defaults = def.getDefaultValues();
if (defaults != null && defaults.length > 0) {
builder.startElement(Constants.DEFAULTVALUES_ELEMENT);
for (QValue v : defaults) {
builder.addContentElement(Constants.DEFAULTVALUE_ELEMENT, factory.createValue(v).getString());
}
builder.endElement();
}
builder.endElement();
}
use of org.apache.jackrabbit.spi.QValueConstraint in project jackrabbit by apache.
the class NodeTypeRegistry method toString.
// --------------------------------------------------------------< Object >
/**
* {@inheritDoc}
*/
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("NodeTypeRegistry (" + super.toString() + ")\n");
builder.append("Registered NodeTypes:\n");
for (QNodeTypeDefinition ntd : registeredNTDefs.values()) {
builder.append(ntd.getName());
builder.append("\n");
builder.append("\tSupertypes: " + Arrays.toString(ntd.getSupertypes()) + "\n");
builder.append("\tMixin\t" + ntd.isMixin() + "\n");
builder.append("\tOrderableChildNodes\t" + ntd.hasOrderableChildNodes() + "\n");
builder.append("\tPrimaryItemName\t" + (ntd.getPrimaryItemName() == null ? "<null>" : ntd.getPrimaryItemName().toString()) + "\n");
QPropertyDefinition[] pd = ntd.getPropertyDefs();
for (QPropertyDefinition aPd : pd) {
builder.append("\tPropertyDefinition\n");
builder.append(" (declared in " + aPd.getDeclaringNodeType() + ")\n");
builder.append("\t\tName\t\t" + (aPd.definesResidual() ? "*" : aPd.getName().toString()) + "\n");
String type = aPd.getRequiredType() == 0 ? "null" : PropertyType.nameFromValue(aPd.getRequiredType());
builder.append("\t\tRequiredType\t" + type + "\n");
QValueConstraint[] vca = aPd.getValueConstraints();
StringBuilder constraints = new StringBuilder();
if (vca == null) {
constraints.append("<null>");
} else {
for (QValueConstraint aVca : vca) {
if (constraints.length() > 0) {
constraints.append(", ");
}
constraints.append(aVca.getString());
}
}
builder.append("\t\tValueConstraints\t" + constraints + "\n");
QValue[] defVals = aPd.getDefaultValues();
StringBuilder defaultValues = new StringBuilder();
if (defVals == null) {
defaultValues.append("<null>");
} else {
for (QValue defVal : defVals) {
if (defaultValues.length() > 0) {
defaultValues.append(", ");
}
defaultValues.append(defVal.toString());
}
}
builder.append("\t\tDefaultValue\t" + defaultValues + "\n");
builder.append("\t\tAutoCreated\t" + aPd.isAutoCreated() + "\n");
builder.append("\t\tMandatory\t" + aPd.isMandatory() + "\n");
builder.append("\t\tOnVersion\t" + OnParentVersionAction.nameFromValue(aPd.getOnParentVersion()) + "\n");
builder.append("\t\tProtected\t" + aPd.isProtected() + "\n");
builder.append("\t\tMultiple\t" + aPd.isMultiple() + "\n");
}
QNodeDefinition[] nd = ntd.getChildNodeDefs();
for (QNodeDefinition aNd : nd) {
builder.append("\tNodeDefinition\\n");
builder.append(" (declared in " + aNd.getDeclaringNodeType() + ")\\n");
builder.append("\t\tName\t\t" + (aNd.definesResidual() ? "*" : aNd.getName().toString()) + "\n");
Name[] reqPrimaryTypes = aNd.getRequiredPrimaryTypes();
if (reqPrimaryTypes != null && reqPrimaryTypes.length > 0) {
for (Name reqPrimaryType : reqPrimaryTypes) {
builder.append("\t\tRequiredPrimaryType\t" + reqPrimaryType + "\n");
}
}
Name defPrimaryType = aNd.getDefaultPrimaryType();
if (defPrimaryType != null) {
builder.append("\n\t\tDefaultPrimaryType\t" + defPrimaryType + "\n");
}
builder.append("\n\t\tAutoCreated\t" + aNd.isAutoCreated() + "\n");
builder.append("\t\tMandatory\t" + aNd.isMandatory() + "\n");
builder.append("\t\tOnVersion\t" + OnParentVersionAction.nameFromValue(aNd.getOnParentVersion()) + "\n");
builder.append("\t\tProtected\t" + aNd.isProtected() + "\n");
builder.append("\t\tAllowsSameNameSiblings\t" + aNd.allowsSameNameSiblings() + "\n");
}
}
builder.append(entCache);
return builder.toString();
}
use of org.apache.jackrabbit.spi.QValueConstraint in project jackrabbit by apache.
the class EffectiveNodeType method checkSetPropertyValueConstraints.
/**
* Tests if the value constraints defined in the property definition
* <code>pd</code> are satisfied by the the specified <code>values</code>.
* <p>
* Note that the <i>protected</i> flag is not checked. Also note that no
* type conversions are attempted if the type of the given values does not
* match the required type as specified in the given definition.
*
* @param pd The definiton of the property
* @param values An array of <code>InternalValue</code> objects.
* @throws ConstraintViolationException if the value constraints defined in
* the property definition are satisfied
* by the the specified values
* @throws RepositoryException if another error occurs
*/
public static void checkSetPropertyValueConstraints(QPropertyDefinition pd, InternalValue[] values) throws ConstraintViolationException, RepositoryException {
// check multi-value flag
if (!pd.isMultiple() && values != null && values.length > 1) {
throw new ConstraintViolationException("the property is not multi-valued");
}
QValueConstraint[] constraints = pd.getValueConstraints();
if (constraints == null || constraints.length == 0) {
// no constraints to check
return;
}
if (values != null && values.length > 0) {
// check value constraints on every value
for (InternalValue value : values) {
// constraints are OR-ed together
boolean satisfied = false;
ConstraintViolationException cve = null;
for (QValueConstraint constraint : constraints) {
try {
constraint.check(value);
satisfied = true;
break;
} catch (ConstraintViolationException e) {
cve = e;
}
}
if (!satisfied) {
// re-throw last exception we encountered
throw cve;
}
}
}
}
use of org.apache.jackrabbit.spi.QValueConstraint in project jackrabbit by apache.
the class QNodeTypeDefinitionImpl method createQPropertyDefinitions.
private static QPropertyDefinition[] createQPropertyDefinitions(Name declName, PropertyDefinition[] pds, NamePathResolver resolver, QValueFactory qValueFactory) throws RepositoryException {
if (pds == null || pds.length == 0) {
return QPropertyDefinition.EMPTY_ARRAY;
}
QPropertyDefinition[] declaredPropDefs = new QPropertyDefinition[pds.length];
for (int i = 0; i < pds.length; i++) {
PropertyDefinition propDef = pds[i];
Name name = propDef.getName().equals(NameConstants.ANY_NAME.getLocalName()) ? NameConstants.ANY_NAME : resolver.getQName(propDef.getName());
// check if propDef provides declaring node type and if it matches 'this' one.
if (propDef.getDeclaringNodeType() != null) {
if (!declName.equals(resolver.getQName(propDef.getDeclaringNodeType().getName()))) {
throw new RepositoryException("Property definition specified invalid declaring nodetype: " + propDef.getDeclaringNodeType().getName() + ", but should be " + declName);
}
}
QValue[] defVls = propDef.getDefaultValues() == null ? QValue.EMPTY_ARRAY : ValueFormat.getQValues(propDef.getDefaultValues(), resolver, qValueFactory);
String[] jcrConstraints = propDef.getValueConstraints();
QValueConstraint[] constraints = QValueConstraint.EMPTY_ARRAY;
if (jcrConstraints != null && jcrConstraints.length > 0) {
constraints = new QValueConstraint[jcrConstraints.length];
for (int j = 0; j < constraints.length; j++) {
constraints[j] = ValueConstraint.create(propDef.getRequiredType(), jcrConstraints[j], resolver);
}
}
declaredPropDefs[i] = new QPropertyDefinitionImpl(name, declName, propDef.isAutoCreated(), propDef.isMandatory(), propDef.getOnParentVersion(), propDef.isProtected(), defVls, propDef.isMultiple(), propDef.getRequiredType(), constraints, propDef.getAvailableQueryOperators(), propDef.isFullTextSearchable(), propDef.isQueryOrderable());
}
return declaredPropDefs;
}
Aggregations