Search in sources :

Example 26 with QValue

use of org.apache.jackrabbit.spi.QValue 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();
}
Also used : QValue(org.apache.jackrabbit.spi.QValue) ValueConstraint(org.apache.jackrabbit.spi.commons.nodetype.constraint.ValueConstraint) QValueConstraint(org.apache.jackrabbit.spi.QValueConstraint) QValueConstraint(org.apache.jackrabbit.spi.QValueConstraint) ValueConstraint(org.apache.jackrabbit.spi.commons.nodetype.constraint.ValueConstraint) QValueConstraint(org.apache.jackrabbit.spi.QValueConstraint)

Example 27 with QValue

use of org.apache.jackrabbit.spi.QValue in project jackrabbit by apache.

the class SessionImporter method buildQValue.

/**
     *
     * @param tv
     * @param targetType
     * @param resolver The name/path resolver used to build a <code>QValue</code>.
     * @return
     * @throws RepositoryException
     */
private QValue buildQValue(TextValue tv, int targetType, NamePathResolver resolver) throws RepositoryException {
    QValue iv;
    try {
        switch(targetType) {
            case PropertyType.BINARY:
                // base64 encoded BINARY type
                if (tv.length() < 0x10000) {
                    // < 65kb: deserialize BINARY type in memory
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    Base64.decode(tv.retrieve(), baos);
                    // no need to close ByteArrayOutputStream
                    //baos.close();
                    iv = session.getQValueFactory().create(baos.toByteArray());
                } else {
                    // >= 65kb: deserialize BINARY type
                    // using Reader and temporary file
                    TransientFileFactory fileFactory = TransientFileFactory.getInstance();
                    File tmpFile = fileFactory.createTransientFile("bin", null, null);
                    FileOutputStream out = new FileOutputStream(tmpFile);
                    Reader reader = tv.reader();
                    try {
                        Base64.decode(reader, out);
                    } finally {
                        reader.close();
                        out.close();
                    }
                    iv = session.getQValueFactory().create(tmpFile);
                }
                break;
            default:
                // build iv using namespace context of xml document
                Value v = ValueHelper.convert(tv.retrieve(), targetType, session.getValueFactory());
                iv = ValueFormat.getQValue(v, resolver, session.getQValueFactory());
                break;
        }
        return iv;
    } catch (IOException e) {
        String msg = "failed to retrieve serialized value";
        log.debug(msg, e);
        throw new RepositoryException(msg, e);
    }
}
Also used : QValue(org.apache.jackrabbit.spi.QValue) TransientFileFactory(org.apache.jackrabbit.util.TransientFileFactory) FileOutputStream(java.io.FileOutputStream) SetPropertyValue(org.apache.jackrabbit.jcr2spi.operation.SetPropertyValue) QValue(org.apache.jackrabbit.spi.QValue) Value(javax.jcr.Value) Reader(java.io.Reader) RepositoryException(javax.jcr.RepositoryException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) File(java.io.File)

Example 28 with QValue

use of org.apache.jackrabbit.spi.QValue in project jackrabbit by apache.

the class BinaryTest method testBinaryTwiceModifiedProperty.

public void testBinaryTwiceModifiedProperty() throws Exception {
    Node test = testRootNode.addNode("test");
    Property p = test.setProperty("prop", generateValue());
    superuser.save();
    // modify twice
    test.setProperty("prop", generateValue());
    QValue qv1 = getQValue(p);
    test.setProperty("prop", generateValue());
    QValue qv2 = getQValue(p);
    assertFalse(qv1.equals(qv2));
    superuser.save();
    assertEquals(qv2, getQValue(p));
    assertDisposed(qv1);
}
Also used : QValue(org.apache.jackrabbit.spi.QValue) Node(javax.jcr.Node) Property(javax.jcr.Property)

Example 29 with QValue

use of org.apache.jackrabbit.spi.QValue in project jackrabbit by apache.

the class BinaryTest method testBinaryTwiceIntermediateSave.

public void testBinaryTwiceIntermediateSave() throws Exception {
    Node test = testRootNode.addNode("test");
    Property p = test.setProperty("prop", generateValue());
    QValue qv1 = getQValue(p);
    superuser.save();
    test.setProperty("prop", generateValue());
    QValue qv2 = getQValue(p);
    assertFalse(qv1.equals(qv2));
    superuser.save();
    assertEquals(qv2, getQValue(p));
    assertDisposed(qv1);
}
Also used : QValue(org.apache.jackrabbit.spi.QValue) Node(javax.jcr.Node) Property(javax.jcr.Property)

Example 30 with QValue

use of org.apache.jackrabbit.spi.QValue in project jackrabbit by apache.

the class QValueTest method testReadBooleanAsLong.

public void testReadBooleanAsLong() throws RepositoryException {
    try {
        QValue v = factory.create(true);
        v.getLong();
    } catch (ValueFormatException e) {
        // ok
        return;
    }
    assertTrue("Cannot convert value to long", false);
}
Also used : QValue(org.apache.jackrabbit.spi.QValue) ValueFormatException(javax.jcr.ValueFormatException)

Aggregations

QValue (org.apache.jackrabbit.spi.QValue)125 Name (org.apache.jackrabbit.spi.Name)40 RepositoryException (javax.jcr.RepositoryException)23 Value (javax.jcr.Value)21 NodeId (org.apache.jackrabbit.spi.NodeId)21 Batch (org.apache.jackrabbit.spi.Batch)19 PropertyInfo (org.apache.jackrabbit.spi.PropertyInfo)18 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)12 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 ValueFormatException (javax.jcr.ValueFormatException)8 Path (org.apache.jackrabbit.spi.Path)8 QValueConstraint (org.apache.jackrabbit.spi.QValueConstraint)8 HashMap (java.util.HashMap)7 ValueConstraint (org.apache.jackrabbit.spi.commons.nodetype.constraint.ValueConstraint)7 InputStream (java.io.InputStream)5 Node (javax.jcr.Node)5 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)5 PropertyId (org.apache.jackrabbit.spi.PropertyId)5