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);
}
}
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);
}
}
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);
}
}
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);
}
}
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();
}
}
Aggregations