use of com.thinkbiganalytics.metadata.modeshape.common.JcrObject 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) {
Node node = ((JcrObject) value).getNode();
return factory.createValue(JcrUtil.dereference(node), 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.common.JcrObject 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.common.JcrObject in project kylo by Teradata.
the class JcrPropertyTest method testFeed.
/**
* Creates a new Category Creates a new Feed Updates the Feed Get Feed and its versions Validates Feed is versioned along with its properties and can successfully return a version
*/
@Test
public void testFeed() {
String categorySystemName = "my_category";
Category category = metadata.commit(() -> {
JcrCategory cat = (JcrCategory) categoryProvider.ensureCategory(categorySystemName);
cat.setDescription("my category desc");
cat.setTitle("my category");
categoryProvider.update(cat);
return cat;
}, MetadataAccess.SERVICE);
final JcrFeed.FeedId createdFeedId = metadata.commit(() -> {
String sysName = "my_category";
JcrCategory cat = (JcrCategory) categoryProvider.ensureCategory(sysName);
cat.setDescription("my category desc");
cat.setTitle("my category");
categoryProvider.update(cat);
JcrDerivedDatasource datasource1 = (JcrDerivedDatasource) datasourceProvider.ensureDerivedDatasource("HiveDatasource", "mysql.table1", "mysql.table1", "mysql table source 1", null);
JcrDerivedDatasource datasource2 = (JcrDerivedDatasource) datasourceProvider.ensureDerivedDatasource("HiveDatasource", "mysql.table2", "mysql.table2", "mysql table source 2", null);
JcrDerivedDatasource emptySource1 = (JcrDerivedDatasource) datasourceProvider.ensureDerivedDatasource("HDFSDatasource", "", "", "empty hdfs source", null);
JcrDerivedDatasource emptySource2 = (JcrDerivedDatasource) datasourceProvider.ensureDerivedDatasource("HDFSDatasource", "", "", "empty hdfs source", null);
Assert.assertEquals(emptySource1.getId(), emptySource2.getId());
JcrDerivedDatasource emptySource3 = (JcrDerivedDatasource) datasourceProvider.ensureDerivedDatasource("HDFSDatasource", null, null, "empty hdfs source", null);
JcrDerivedDatasource emptySource4 = (JcrDerivedDatasource) datasourceProvider.ensureDerivedDatasource("HDFSDatasource", null, null, "empty hdfs source", null);
Assert.assertEquals(emptySource3.getId(), emptySource4.getId());
JcrDerivedDatasource datasource3 = (JcrDerivedDatasource) datasourceProvider.ensureDerivedDatasource("HDFSDatasource", "/etl/customers/swert/012320342", "/etl/customers/swert/012320342", "mysql hdfs source", null);
JcrDerivedDatasource datasource4 = (JcrDerivedDatasource) datasourceProvider.ensureDerivedDatasource("HDFSDatasource", "/etl/customers/swert/012320342", "/etl/customers/swert/012320342", "mysql hdfs source", null);
Assert.assertEquals(datasource3.getId(), datasource4.getId());
String feedSystemName = "my_feed_" + UUID.randomUUID();
// JcrFeed feed = (JcrFeed) feedProvider.ensureFeed(categorySystemName, feedSystemName, "my feed desc", datasource1.getId(), null);
JcrFeed feed = (JcrFeed) feedProvider.ensureFeed(sysName, feedSystemName, "my feed desc");
feedProvider.ensureFeedSource(feed.getId(), datasource1.getId());
feedProvider.ensureFeedSource(feed.getId(), datasource2.getId());
feed.setTitle("my feed");
feed.addTag("my tag");
feed.addTag("my second tag");
feed.addTag("feedTag");
Map<String, Object> otherProperties = new HashMap<String, Object>();
otherProperties.put("prop1", "my prop1");
otherProperties.put("prop2", "my prop2");
feed.setProperties(otherProperties);
return feed.getId();
}, MetadataAccess.SERVICE);
// read and find feed verisons and ensure props
JcrFeed.FeedId readFeedId = metadata.read(() -> {
Session s = null;
JcrFeed f = (JcrFeed) ((JcrFeedProvider) feedProvider).findById(createdFeedId);
// TODO: Feed vesioning disabled for Kylo v0.5.0
// int versions = printVersions(f);
// Assert.assertTrue(versions > 1, "Expecting more than 1 version: jcr:rootVersion, 1.0");
@SuppressWarnings("unchecked") List<? extends FeedSource> sources = f.getSources();
Assert.assertTrue(sources.size() > 0);
if (sources != null) {
for (FeedSource source : sources) {
Map<String, Object> dataSourceProperties = ((JcrDatasource) source.getDatasource().get()).getAllProperties();
String type = (String) dataSourceProperties.get(JcrDerivedDatasource.TYPE_NAME);
Assert.assertEquals(type, "HiveDatasource");
}
}
List<JcrObject> taggedObjects = tagProvider.findByTag("my tag");
// assert we got 1 feed back
Assert.assertTrue(taggedObjects.size() >= 1);
return f.getId();
}, MetadataAccess.SERVICE);
// update the feed again
JcrFeed.FeedId updatedFeed = metadata.commit(() -> {
JcrFeed f = (JcrFeed) ((JcrFeedProvider) feedProvider).findById(createdFeedId);
f.setDescription("My Feed Updated Description");
Map<String, Object> otherProperties = new HashMap<String, Object>();
otherProperties.put("prop1", "my updated prop1");
f.setProperties(otherProperties);
((JcrFeedProvider) feedProvider).update(f);
return f.getId();
}, MetadataAccess.SERVICE);
// read it again and find the versions
readFeedId = metadata.read(() -> {
JcrFeed f = (JcrFeed) ((JcrFeedProvider) feedProvider).findById(updatedFeed);
// Assert.assertEquals(v1Id, baseId);
return f.getId();
}, MetadataAccess.SERVICE);
}
use of com.thinkbiganalytics.metadata.modeshape.common.JcrObject in project kylo by Teradata.
the class AbstractMetadataCriteria method select.
@SuppressWarnings("unchecked")
public <E, J extends JcrObject> List<E> select(Session session, String typeName, Class<E> type, Class<J> jcrClass) {
HashMap<String, Object> params = new HashMap<>();
StringBuilder queryStr = new StringBuilder("select e.* from [" + typeName + "] as e ");
applyFilter(queryStr, params);
applyLimit(queryStr);
Map<String, String> bindParams = new HashMap<>();
for (Map.Entry<String, Object> entry : params.entrySet()) {
bindParams.put(entry.getKey(), entry.getValue() != null ? entry.getValue().toString() : null);
}
QueryResult result = null;
try {
result = JcrQueryUtil.query(session, queryStr.toString(), bindParams);
return (List<E>) JcrQueryUtil.queryResultToList(result, jcrClass);
} catch (RepositoryException e) {
throw new MetadataRepositoryException("Unable to execute Criteria Query for " + type + ". Query is: " + queryStr.toString(), e);
}
}
use of com.thinkbiganalytics.metadata.modeshape.common.JcrObject in project kylo by Teradata.
the class BaseJcrProvider method delete.
@Override
public void delete(T t) {
if (t != null) {
if (t instanceof JcrObject) {
JcrObject jcrObject = (JcrObject) t;
jcrObject.remove();
} else {
throw new UnsupportedOperationException();
}
}
}
Aggregations