use of com.github.drinkjava2.jsqlbox.entitynet.Path in project jSqlBox by drinkjava2.
the class EntityNetTreeTest method testSearchTreeChild.
@Test
public void testSearchTreeChild() {
EntityNet net = ctx.netLoad(TreeNode.class);
Set<TreeNode> TreeNodes = net.findEntitySet(TreeNode.class, new Path("S+", TreeNode.class).where("id=? or id=?", "B", "D").nextPath("C*", TreeNode.class, "pid"));
for (TreeNode node : TreeNodes) System.out.print(node.getId() + " ");
Assert.assertEquals(9, TreeNodes.size());
}
use of com.github.drinkjava2.jsqlbox.entitynet.Path in project jSqlBox by drinkjava2.
the class PathUtils method calculateRefColumns.
/**
* Calculate path's Ref Columns and join it to one String
*/
static String calculateRefColumns(Path path) {
TableModel childOrParentModelOrSelf = path.targetModel;
if ("P".equalsIgnoreCase(path.getType().substring(0, 1)))
childOrParentModelOrSelf = path.fatherPath.targetModel;
// compare tableModel to determine refs are field names or column names
StringBuilder sb = new StringBuilder();
for (String ref : path.refs) {
boolean found = false;
for (ColumnModel col : childOrParentModelOrSelf.getColumns()) {
if (ref != null && ref.equalsIgnoreCase(col.getEntityField())) {
if (found)
throw new EntityNetException("Can't judge '" + ref + "' is a field or a column name.");
found = true;
if (sb.length() > 0)
sb.append(EntityNet.COMPOUND_COLUMNNAME_SEPARATOR);
sb.append(col.getColumnName());
} else if (ref != null && ref.equalsIgnoreCase(col.getColumnName())) {
if (ref.equalsIgnoreCase(col.getEntityField())) {
if (found)
throw new EntityNetException("Can't judge '" + ref + "' is a field or a column name.");
found = true;
if (sb.length() > 0)
sb.append(EntityNet.COMPOUND_COLUMNNAME_SEPARATOR);
sb.append(ref);
}
}
}
if (!found)
throw new EntityNetException("Can't find reference column name for '" + ref + "' ");
}
return sb.toString();
}
use of com.github.drinkjava2.jsqlbox.entitynet.Path in project jSqlBox by drinkjava2.
the class EntityNet method findNodeSetforNodes.
/**
* According given path and input Node Set, find related node set
*
* @param level
* search level, start from 0
* @param path
* The Path
* @param input
* The input node collection
* @param output
* The output node collection
* @return Related node set
*/
private void findNodeSetforNodes(Integer level, Path path, Collection<Node> input, Map<Class<?>, Set<Node>> result) {
if (level > 1000)
throw new EntityNetException("Search level beyond 1000, this may caused by a circular reference path chain.");
TableModel model = path.getTargetModel();
String type0 = path.getType().substring(0, 1);
String type1 = path.getType().substring(1, 2);
Class<?> targetClass = model.getEntityClass();
Set<Node> selected = new LinkedHashSet<Node>();
String pathUniqueString = path.getUniqueIdString();
Integer pathId = pathIdCache.get(pathUniqueString);
// Start
if ("S".equalsIgnoreCase(type0)) {
if (level != 0)
throw new EntityNetException("'S' type can only be used on path start");
// Check if cached
Map<Integer, Set<Node>> rootCache = queryCache.get("ROOT");
if (this.allowQueryCache && path.getCacheable() && pathId != null && (rootCache != null)) {
Set<Node> cachedNodes = rootCache.get(pathId);
if (cachedNodes != null)
selected = cachedNodes;
} else {
// check all targetClass
Collection<Node> nodesToCheck = getAllNodeSet(targetClass);
validateSelected(level, path, selected, nodesToCheck);
// cache it if allow cache
if (this.allowQueryCache && path.getCacheable() && !StrUtils.isEmpty(pathUniqueString)) {
cacheSelected("ROOT", pathUniqueString, selected);
}
}
} else // Child
if ("C".equalsIgnoreCase(type0) && input != null && !input.isEmpty()) {
for (Node inputNode : input) {
Map<Integer, Set<Node>> childCache = queryCache.get(inputNode.getId());
if (this.allowQueryCache && path.getCacheable() && pathId != null && childCache != null) {
Set<Node> cachedNodes = childCache.get(pathId);
if (cachedNodes != null)
selected.addAll(cachedNodes);
} else {
// Find childNodes meat class/columns/id condition
Set<Node> nodesToCheck = new LinkedHashSet<Node>();
for (Entry<String, Node> cNode : body.get(targetClass).entrySet()) {
List<ParentRelation> prs = cNode.getValue().getParentRelations();
if (prs != null)
for (ParentRelation pr : prs) {
if (inputNode.getId().equals(pr.getParentId()) && pr.getRefColumns().equalsIgnoreCase(path.getRefColumns())) {
nodesToCheck.add(cNode.getValue());
break;
}
}
}
validateSelected(level, path, selected, nodesToCheck);
// now cached childNodes on parentNode
if (this.allowQueryCache && path.getCacheable() && !StrUtils.isEmpty(pathUniqueString)) {
cacheSelected(inputNode.getId(), pathUniqueString, selected);
}
}
}
} else // Parent
if ("P".equalsIgnoreCase(type0) && input != null && !input.isEmpty()) {
String targetTableName = model.getTableName();
EntityNetException.assureNotEmpty(targetTableName, "targetTableName can not be null");
for (Node inputNode : input) {
// Find parent nodes meat tableName/refColumns/nodeId condition
Set<Node> nodesToCheck = new LinkedHashSet<Node>();
List<ParentRelation> prs = inputNode.getParentRelations();
if (prs != null)
for (ParentRelation pr : prs) {
if (targetTableName.equalsIgnoreCase(pr.getParentTable()) && path.getRefColumns().equalsIgnoreCase(pr.getRefColumns())) {
Node node = this.getOneNode(targetClass, pr.getParentId());
if (node != null)
nodesToCheck.add(node);
}
}
validateSelected(level, path, selected, nodesToCheck);
// now cached childNodes on parentNode
if (this.allowQueryCache && path.getCacheable() && !StrUtils.isEmpty(pathUniqueString)) {
cacheSelected(inputNode.getId(), pathUniqueString, selected);
}
}
}
Set<Node> nodes = result.get(targetClass);
if (nodes == null) {
nodes = new LinkedHashSet<Node>();
result.put(targetClass, nodes);
}
if ("+".equals(type1) || "*".equals(type1))
nodes.addAll(selected);
if (!(path.getCacheable() && StrUtils.isEmpty(path.getUniqueIdString()))) {
if (selected.size() > 100000)
throw new EntityNetException("Query result return more than 100000 records to cache in memory, this may caused by careless programming.");
}
if (level > 10000)
throw new EntityNetException("Search depth >10000, this may caused by careless programming.");
if ("*".equals(type1) && !selected.isEmpty())
findNodeSetforNodes(level + 1, path, selected, result);
if (path.getNextPath() != null) {
findNodeSetforNodes(level + 1, path.getNextPath(), selected, result);
}
}
use of com.github.drinkjava2.jsqlbox.entitynet.Path in project jSqlBox by drinkjava2.
the class EntityNetDemoTest method testAutoPath2.
@Test
public void testAutoPath2() {
insertDemoData();
EntityNet net = ctx.netLoad(new Email(), new User(), new Role(), Privilege.class, UserRole.class, RolePrivilege.class);
Set<Privilege> privileges = net.findEntitySet(Privilege.class, new Path(Email.class).setValidator(new EmailValidator()).autoPath(Privilege.class));
for (Privilege privilege : privileges) System.out.println(privilege.getId());
Assert.assertEquals(1, privileges.size());
}
use of com.github.drinkjava2.jsqlbox.entitynet.Path in project jSqlBox by drinkjava2.
the class EntityNetDemoTest method testEntityNetQuery.
@Test
public void testEntityNetQuery() {
insertDemoData();
EntityNet net = ctx.nQuery(new EntityNetHandler(User.class, Email.class), "select u.**, e.** from usertb u, emailtb e where u.id=e.userId");
Assert.assertEquals(8, net.size());
Set<Email> emails = net.findEntitySet(Email.class, new Path(User.class).where("id='u1' or id='u2'").autoPath(Email.class));
Assert.assertEquals(4, emails.size());
}
Aggregations