Skip to content

Commit

Permalink
Comment
Browse files Browse the repository at this point in the history
  • Loading branch information
OndraZizka committed Jun 21, 2014
1 parent 68a3680 commit 4ae058e
Showing 1 changed file with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package org.jboss.windup.graph.dao.impl;

import java.util.Iterator;

import javax.inject.Inject;

import org.jboss.windup.graph.GraphContext;
import org.jboss.windup.graph.dao.BaseDao;
import org.jboss.windup.graph.dao.exception.NonUniqueResultException;

import com.thinkaurelius.titan.core.TitanTransaction;
import com.thinkaurelius.titan.core.attribute.Text;
import com.thinkaurelius.titan.util.datastructures.IterablesUtil;
Expand All @@ -16,7 +8,16 @@
import com.tinkerpop.frames.VertexFrame;
import com.tinkerpop.frames.modules.typedgraph.TypeValue;
import com.tinkerpop.gremlin.java.GremlinPipeline;
import java.util.Iterator;
import javax.inject.Inject;
import org.jboss.windup.graph.GraphContext;
import org.jboss.windup.graph.dao.BaseDao;
import org.jboss.windup.graph.dao.exception.NonUniqueResultException;


/**
* Base DAO implementing the basic operations generically.
*/
public class BaseDaoImpl<T extends VertexFrame> implements BaseDao<T>
{

Expand All @@ -31,35 +32,35 @@ public GraphContext getContext()
return context;
}


public BaseDaoImpl(Class<T> type)
{
this.type = type;

TypeValue typeValue = type.getAnnotation(TypeValue.class);
if (typeValue == null)
{
throw new IllegalArgumentException("Must contain annotation 'TypeValue'");
}
this.typeValueForSearch = typeValue.value();
}


public void delete(T obj)
{
context.getFramed().removeVertex(obj.asVertex());
}


public Iterable<T> getByProperty(String key, Object value)
{
return context.getFramed().getVertices(key, value, type);
}


public Iterable<T> findValueMatchingRegex(String key, String... regex)
{
// build regex
if (regex.length == 0)
{
return IterablesUtil.emptyIterable();
}

final String regexFinal;
if (regex.length == 1)
Expand All @@ -74,9 +75,7 @@ public Iterable<T> findValueMatchingRegex(String key, String... regex)
for (String value : regex)
{
if (i > 0)
{
builder.append("|");
}
builder.append(value);
i++;
}
Expand All @@ -88,6 +87,7 @@ public Iterable<T> findValueMatchingRegex(String key, String... regex)
.has(key, Text.REGEX, regexFinal).vertices(type);
}


public Iterable<T> hasAllProperties(String[] keys, String[] vals)
{
FramedGraphQuery fgq = context.getFramed().query().has("type", Text.CONTAINS, this.typeValueForSearch);
Expand All @@ -103,37 +103,44 @@ public Iterable<T> hasAllProperties(String[] keys, String[] vals)
return fgq.vertices(type);
}


public T create(Object id)
{
return context.getFramed().addVertex(id, type);
}


public T create()
{
return context.getFramed().addVertex(null, type);
}


public Iterable<T> getAll()
{
return context.getFramed().query().has("type", Text.CONTAINS, typeValueForSearch).vertices(type);
}


public TitanTransaction newTransaction()
{
return context.getGraph().newTransaction();
}


public long count(Iterable<?> obj)
{
GremlinPipeline pipe = new GremlinPipeline();
return pipe.start(obj).count();
}


public T getById(Object id)
{
return context.getFramed().getVertex(id, type);
}


public T getByUniqueProperty(String property, Object value) throws NonUniqueResultException
{
Iterable<T> results = getByProperty(property, value);
Expand All @@ -154,6 +161,7 @@ public T getByUniqueProperty(String property, Object value) throws NonUniqueResu
return result;
}


public T castToType(VertexFrame v)
{
Vertex vertex = v.asVertex();
Expand All @@ -162,16 +170,19 @@ public T castToType(VertexFrame v)
return context.getFramed().frame(vertex, type);
}


public void commit()
{
this.context.getGraph().commit();
}


protected Class<T> getType()
{
return type;
}


protected String getTypeValueForSearch()
{
return typeValueForSearch;
Expand Down

0 comments on commit 4ae058e

Please sign in to comment.