Search in sources :

Example 6 with MetadataRepositoryException

use of com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException in project kylo by Teradata.

the class JcrPropertyUtil method addToSetProperty.

public static boolean addToSetProperty(Node node, String name, Object value, boolean weakReference) {
    try {
        if (node == null) {
            throw new IllegalArgumentException("Cannot set a property on a null-node!");
        }
        if (name == null) {
            throw new IllegalArgumentException("Cannot set a property without a provided name");
        }
        Set<Value> values = null;
        if (node.hasProperty(name)) {
            values = Arrays.stream(node.getProperty(name).getValues()).map(v -> {
                if (PropertyType.REFERENCE == v.getType() && weakReference) {
                    try {
                        Node n = JcrPropertyUtil.asValue(v, node.getSession());
                        return n.getSession().getValueFactory().createValue(n, true);
                    } catch (AccessDeniedException e) {
                        log.debug("Access denied", e);
                        throw new AccessControlException(e.getMessage());
                    } catch (RepositoryException e) {
                        throw new MetadataRepositoryException("Failed to add to set property: " + name + "->" + value, e);
                    }
                } else {
                    return v;
                }
            }).collect(Collectors.toSet());
        } else {
            values = new HashSet<>();
        }
        Value newVal = createValue(node.getSession(), value, weakReference);
        boolean result = values.add(newVal);
        if (weakReference) {
            Property property = node.setProperty(name, (Value[]) values.stream().toArray(size -> new Value[size]), PropertyType.WEAKREFERENCE);
        } else {
            Property property = node.setProperty(name, (Value[]) values.stream().toArray(size -> new Value[size]));
        }
        return result;
    } catch (AccessDeniedException e) {
        log.debug("Access denied", e);
        throw new AccessControlException(e.getMessage());
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException("Failed to add to set property: " + name + "->" + value, e);
    }
}
Also used : MetadataRepositoryException(com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException) AccessDeniedException(javax.jcr.AccessDeniedException) Node(javax.jcr.Node) Value(javax.jcr.Value) AccessControlException(java.security.AccessControlException) MetadataRepositoryException(com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException) RepositoryException(javax.jcr.RepositoryException) Property(javax.jcr.Property)

Example 7 with MetadataRepositoryException

use of com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException in project kylo by Teradata.

the class JcrPropertyUtil method createValue.

public static Value createValue(Session session, Object value, boolean weakRef) {
    try {
        ValueFactory factory = session.getValueFactory();
        if (value == null) {
            throw new IllegalArgumentException("Cannot create a value from null");
        } else if (value instanceof Enum) {
            return factory.createValue(((Enum) value).name());
        } else if (value instanceof JcrObject) {
            return factory.createValue(((JcrObject) value).getNode(), weakRef);
        // return factory.createValue(((JcrObject) value).getNode().getIdentifier(), weakRef ? PropertyType.WEAKREFERENCE : PropertyType.REFERENCE);
        } else if (value instanceof Value) {
            return (Value) value;
        } else if (value instanceof Node) {
            // return factory.createValue((Node) value, weakRef);
            return factory.createValue(((Node) value).getIdentifier(), weakRef ? PropertyType.WEAKREFERENCE : PropertyType.REFERENCE);
        } else if (value instanceof Binary) {
            return factory.createValue((Binary) value);
        } else if (value instanceof Calendar) {
            return factory.createValue((Calendar) value);
        } else if (value instanceof DateTime) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(((DateTime) value).toDate());
            return factory.createValue(cal);
        } else if (value instanceof Date) {
            Calendar cal = Calendar.getInstance();
            cal.setTime((Date) value);
            return factory.createValue(cal);
        } else if (value instanceof BigDecimal) {
            return factory.createValue((BigDecimal) value);
        } else if (value instanceof Long) {
            return factory.createValue(((Long) value).longValue());
        } else if (value instanceof Double) {
            return factory.createValue((Double) value);
        } else if (value instanceof Boolean) {
            return factory.createValue((Boolean) value);
        } else if (value instanceof InputStream) {
            return factory.createValue((InputStream) value);
        // } else if (value instanceof Collection) {
        // String[] list = new String[((Collection<Object>) value).size()];
        // int pos = 0;
        // for (Object cal : (Collection<Object>) value) {
        // list[pos] = cal.toString();
        // pos += 1;
        // }
        // return factory.createValue(list);
        } else {
            return factory.createValue(value.toString());
        }
    } catch (AccessDeniedException e) {
        log.debug("Access denied", e);
        throw new AccessControlException(e.getMessage());
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException("Failed to create value frpm: " + value, e);
    }
}
Also used : MetadataRepositoryException(com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException) AccessDeniedException(javax.jcr.AccessDeniedException) InputStream(java.io.InputStream) Node(javax.jcr.Node) Calendar(java.util.Calendar) AccessControlException(java.security.AccessControlException) MetadataRepositoryException(com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException) RepositoryException(javax.jcr.RepositoryException) ValueFactory(javax.jcr.ValueFactory) DateTime(org.joda.time.DateTime) Date(java.util.Date) BigDecimal(java.math.BigDecimal) JcrObject(com.thinkbiganalytics.metadata.modeshape.common.JcrObject) Value(javax.jcr.Value) Binary(javax.jcr.Binary)

Example 8 with MetadataRepositoryException

use of com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException in project kylo by Teradata.

the class JcrPropertyUtil method setProperty.

public static void setProperty(Node node, String name, Object value) {
    try {
        if (node == null) {
            throw new IllegalArgumentException("Cannot set a property on a null-node!");
        }
        if (name == null) {
            throw new IllegalArgumentException("Cannot set a property without a provided name");
        }
        if (value == null) {
            node.setProperty(name, (Value) null);
        } else if (value instanceof Enum) {
            node.setProperty(name, ((Enum) value).name());
        } else if (value instanceof JcrObject) {
            node.setProperty(name, ((JcrObject) value).getNode());
        } else if (value instanceof Value) {
            node.setProperty(name, (Value) value);
        } else if (value instanceof Node) {
            node.setProperty(name, (Node) value);
        } else if (value instanceof Binary) {
            node.setProperty(name, (Binary) value);
        } else if (value instanceof Calendar) {
            node.setProperty(name, (Calendar) value);
        } else if (value instanceof DateTime) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(((DateTime) value).toDate());
            node.setProperty(name, cal);
        } else if (value instanceof Date) {
            Calendar cal = Calendar.getInstance();
            cal.setTime((Date) value);
            node.setProperty(name, cal);
        } else if (value instanceof BigDecimal) {
            node.setProperty(name, (BigDecimal) value);
        } else if (value instanceof Long) {
            node.setProperty(name, ((Long) value).longValue());
        } else if (value instanceof Double) {
            node.setProperty(name, (Double) value);
        } else if (value instanceof Boolean) {
            node.setProperty(name, (Boolean) value);
        } else if (value instanceof InputStream) {
            node.setProperty(name, (InputStream) value);
        } else if (value instanceof Collection) {
            String[] list = new String[((Collection<Object>) value).size()];
            int pos = 0;
            for (Object cal : (Collection<Object>) value) {
                list[pos] = cal.toString();
                pos += 1;
            }
            node.setProperty(name, list);
        } else {
            node.setProperty(name, value.toString());
        }
    } catch (AccessDeniedException e) {
        log.debug("Access denied", e);
        throw new AccessControlException(e.getMessage());
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException("Failed to set property value: " + name + "=" + value, e);
    }
}
Also used : MetadataRepositoryException(com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException) AccessDeniedException(javax.jcr.AccessDeniedException) InputStream(java.io.InputStream) Node(javax.jcr.Node) Calendar(java.util.Calendar) AccessControlException(java.security.AccessControlException) MetadataRepositoryException(com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException) RepositoryException(javax.jcr.RepositoryException) DateTime(org.joda.time.DateTime) Date(java.util.Date) BigDecimal(java.math.BigDecimal) JcrObject(com.thinkbiganalytics.metadata.modeshape.common.JcrObject) Value(javax.jcr.Value) Collection(java.util.Collection) JcrExtensiblePropertyCollection(com.thinkbiganalytics.metadata.modeshape.extension.JcrExtensiblePropertyCollection) JcrObject(com.thinkbiganalytics.metadata.modeshape.common.JcrObject) Binary(javax.jcr.Binary)

Example 9 with MetadataRepositoryException

use of com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException in project kylo by Teradata.

the class JcrQueryUtil method findFirst.

public static <T extends Object> T findFirst(Session session, String query, Map<String, String> bindParams, Class<T> type) {
    JcrTools tools = new JcrTools();
    try {
        QueryResult result = query(session, query, bindParams);
        List<T> list = queryResultToList(result, 1, type);
        if (list != null && list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException("Unable to findFirst for query : " + query, e);
    }
}
Also used : MetadataRepositoryException(com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException) QueryResult(javax.jcr.query.QueryResult) MetadataRepositoryException(com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException) RepositoryException(javax.jcr.RepositoryException) JcrTools(org.modeshape.jcr.api.JcrTools)

Example 10 with MetadataRepositoryException

use of com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException in project kylo by Teradata.

the class JcrQueryUtil method queryResultStream.

public static <T extends Object> Stream<T> queryResultStream(QueryResult result, Integer fetchSize, Class<T> type, Object... args) {
    if (result != null) {
        try {
            @SuppressWarnings("unchecked") Iterator<Node> nodeItr = (Iterator<Node>) result.getNodes();
            Stream<T> stream = StreamSupport.stream(Spliterators.spliteratorUnknownSize(nodeItr, 0), false).map(node -> JcrUtil.constructNodeObject(node, type, args));
            if (fetchSize != null) {
                return stream.limit(fetchSize);
            } else {
                return stream;
            }
        } catch (RepositoryException e) {
            throw new MetadataRepositoryException("Unable to parse QueryResult to stream for type  : " + type, e);
        }
    } else {
        return Stream.empty();
    }
}
Also used : MetadataRepositoryException(com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException) Node(javax.jcr.Node) Iterator(java.util.Iterator) RowIterator(javax.jcr.query.RowIterator) NodeIterator(javax.jcr.NodeIterator) MetadataRepositoryException(com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException) RepositoryException(javax.jcr.RepositoryException)

Aggregations

MetadataRepositoryException (com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException)83 RepositoryException (javax.jcr.RepositoryException)79 Node (javax.jcr.Node)54 AccessDeniedException (javax.jcr.AccessDeniedException)29 AccessControlException (java.security.AccessControlException)28 Session (javax.jcr.Session)25 ArrayList (java.util.ArrayList)16 HashMap (java.util.HashMap)14 HashSet (java.util.HashSet)12 NodeIterator (javax.jcr.NodeIterator)12 Nonnull (javax.annotation.Nonnull)10 Value (javax.jcr.Value)10 Map (java.util.Map)9 Property (javax.jcr.Property)8 ItemNotFoundException (javax.jcr.ItemNotFoundException)7 QueryResult (javax.jcr.query.QueryResult)7 JcrObject (com.thinkbiganalytics.metadata.modeshape.common.JcrObject)6 AccessControlManager (javax.jcr.security.AccessControlManager)6 UserFieldDescriptor (com.thinkbiganalytics.metadata.api.extension.UserFieldDescriptor)5 List (java.util.List)5