private Node getOrCreateNode(Identifiable source, final String nodeClassType) { UniqueFactory<Node> factory = new UniqueFactory.UniqueNodeFactory(graph, PROP_INDEX_NODE) { @Override protected void initialize(Node created, Map<String, Object> properties) { created.setProperty(PROP_ID, properties.get(PROP_ID)); created.setProperty(PROP_NODE_CLASS, nodeClassType); } }; return factory.getOrCreate(PROP_ID, source.getId()); }
@Override public T get(String id) { for(T conference: conferences) { if(conference.getId().equals(id)) { return conference; } } return null; }
@Override public T get(String id) { for(T d : data) { if(d.getId().equals(id)) { return d; } } return null; }
@Override public void appendLinks(LinkableRepresentation<?> representation) { Resource source = getResource(representation); if(source == null) { return; } if(representation instanceof Identifiable) { Identifiable sourceObject = (Identifiable)representation; if(sourceObject.getId() != null) { representation.addLink( generateResourceLink( source, sourceObject.getId(), representation.getUriInfo())); } } }
@POST @Consumes({ BASE_JSON_MEDIA_TYPE, BASE_XML_MEDIA_TYPE }) public Response create(REP representation) { DOMAIN entity = getConverter().to(uriInfo, representation); getRepository().store(entity); return Response.created( UriBuilder.fromResource(getResourceClass()).segment("{id}").build(entity.getId())).build(); }
@Override public void remove(Identifiable source, String type, Identifiable target) { for(int i = 0; i < relations.size(); i++) { ExtendedRelation rel = relations.get(i); if(rel.getSourceId().equals(source.getId()) && rel.getTargetId().equals(target.getId()) && rel.getType().equals(type)) { relations.remove(i); i--; } } }
@SuppressWarnings("unchecked") @Override public <T extends Identifiable> List<T> findTargets(Identifiable source, String type, Class<T> targetType) { List<T> result = new ArrayList<T>(); for(ExtendedRelation rel : relations) { if(rel.getSourceId().equals(source.getId()) && rel.getType().equals(type)) { result.add((T)rel.getTarget()); } } return result; }
@Override public Relation add(Identifiable source, final String type, Identifiable target) { Transaction tx = graph.beginTx(); try { Node root =graph.getNodeById(0); String sourceTypeName = source.getClass().getSimpleName(); String targetTypeName = target.getClass().getSimpleName(); Node sourceTypeNode = getOrCreateNodeType(sourceTypeName); Node targetTypeNode = getOrCreateNodeType(targetTypeName); getOrCreateRelationship(root, sourceTypeNode, Named.relation(sourceTypeName)); getOrCreateRelationship(root, targetTypeNode, Named.relation(targetTypeName)); Node sourceNode = getOrCreateNode(source, sourceTypeName); getOrCreateRelationship(sourceTypeNode, sourceNode, Named.relation(REL_TYPE_ALL)); Node targetNode = getOrCreateNode(target, targetTypeName); getOrCreateRelationship(targetTypeNode, targetNode, Named.relation(REL_TYPE_ALL)); getOrCreateRelationship(sourceNode, targetNode, Named.relation(type)); tx.success(); } catch(Exception e) { tx.failure(); throw new RuntimeException( "Could not add relation of type " + type + " between " + source + " and " + target, e); } finally { tx.finish(); } return new Relation(source.getId(), target.getId(), type); }
@Override public void remove(Identifiable source, String type, Identifiable target) { Transaction tx = graph.beginTx(); try { Index<Node> nodeIndex = graph.index().forNodes(PROP_INDEX_NODE); Index<Relationship> relationIndex = graph.index().forRelationships(PROP_INDEX_REL); Node sourceNode = nodeIndex.get(PROP_ID, source.getId()).getSingle(); Node targetNode = nodeIndex.get(PROP_ID, target.getId()).getSingle(); for(Relationship rel : sourceNode.getRelationships(Named.relation(type))) { if(rel.getEndNode().equals(targetNode)) { rel.delete(); relationIndex.remove(rel); } } tx.success(); } catch(Exception e) { tx.failure(); throw new RuntimeException( "Could not add relation of type " + type + " between " + source + " and " + target, e); } finally { tx.finish(); } }
Node node = index.get(PROP_ID, source.getId()).getSingle(); if(node == null) { return targets;
@Test public void shouldUpdateObjectOnCommit() throws Exception { final DOMAIN domain = createNewDomainObject(); commit(Void.class, new Store(domain)); DOMAIN stored = commit(new Get(domain.getId())); stored = updateDomainObject(stored); commit(Void.class, new Store(stored)); DOMAIN updated = commit(new Get(domain.getId())); validateUpdatedDomainObject(updated); }
@Test public void shouldStoreObjectOnCommit() throws Exception { final DOMAIN domain = createNewDomainObject(); commit(Void.class, new Store(domain)); DOMAIN stored = commit(new Get(domain.getId())); Assert.assertNotNull( "Object should be stored when transaction is committed", stored); }
@Test public void shouldSetUpdatedDate() throws Exception { DOMAIN domain = createNewDomainObject(); Assume.assumeTrue(domain instanceof Timestampable); commit(Void.class, new Store(domain)); DOMAIN stored = commit(new Get(domain.getId())); stored = updateDomainObject(stored); stored = commit(new Get(domain.getId())); Timestampable timed = (Timestampable)stored; Assert.assertNotNull(timed.getCreated()); Assert.assertNotSame(timed.getCreated(), timed.getLastModified()); }
@Override public void appendLinks(LinkableRepresentation<?> representation) { if(!(representation instanceof Identifiable)) { return; } Identifiable sourceIdentifiable = (Identifiable)representation; // TODO: Remove need for cast Collection<RelationMatch> matches = service.getMatchingRelations(representation.getSourceType()); for(RelationMatch target: matches) { UriBuilder buidler = representation.getUriInfo().getBaseUriBuilder().segment( "rel", getTypeName(target.getSourceModel()), sourceIdentifiable.getId(), target.getSource().getType(), getTypeName(target.getTargetModel())); // TODO: Dynamically lookup MediaType representation.addLink( new ResourceLink( target.getSource().getName(), buidler.build(), RelationResource.BASE_JSON_MEDIA_TYPE + "; type=" + getMediaTypeName(target.getTargetModel()))); } }
@Test public void shouldNotUpdateObjectOnRollback() throws Exception { final DOMAIN domain = createNewDomainObject(); commit(Void.class, new Store(domain)); DOMAIN stored = commit(new Get(domain.getId())); stored = updateDomainObject(stored); rollback(Void.class, new Store(stored)); DOMAIN updated = commit(new Get(domain.getId())); try { validateUpdatedDomainObject(updated); Assert.fail("Object should not be updated when transaction is rolled back"); } catch(AssertionError error) { // no-op, the updated object should not validate as a updated one } }
@Test public void shouldNotStoreObjectOnRollback() throws Exception { final DOMAIN domain = createNewDomainObject(); rollback(Void.class, new Store(domain)); DOMAIN stored = commit(getDomainClass(), new Get(domain.getId())); Assert.assertNull( "Object should not be stored when transaction is rolled back", stored); }
@Override public void perform() { responseValidation( given(). then(). contentType(getTypedMediaType()) , domain). when(). get(createRootURL() + "/{id}", domain.getId()). body(); } }).inspect(new SetupRepository<DOMAIN>(getDomainClass(), domain));
@Test public void shouldNotRemoveObjectOnRollback() throws Exception { DOMAIN domain = createNewDomainObject(); commit(Void.class, new Store(domain)); rollback(Void.class, new Remove(domain)); DOMAIN stored = commit(new Get(domain.getId())); Assert.assertNotNull( "Object should not be removed when transaction is rolled back", stored); }
@Test public void shouldRemoveObjectOnCommmit() throws Exception { DOMAIN domain = createNewDomainObject(); commit(Void.class, new Store(domain)); commit(Void.class, new Remove(domain)); DOMAIN stored = commit(new Get(domain.getId())); Assert.assertNull( "Object should be removed when transaction is committed", stored); }