Search in sources :

Example 6 with Path

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());
}
Also used : Path(com.github.drinkjava2.jsqlbox.entitynet.Path) TreeNode(com.github.drinkjava2.functionstest.entitynet.entities.TreeNode) EntityNet(com.github.drinkjava2.jsqlbox.entitynet.EntityNet) Test(org.junit.Test)

Example 7 with Path

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();
}
Also used : ColumnModel(com.github.drinkjava2.jdialects.model.ColumnModel) TableModel(com.github.drinkjava2.jdialects.model.TableModel)

Example 8 with Path

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);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Entry(java.util.Map.Entry) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TableModel(com.github.drinkjava2.jdialects.model.TableModel)

Example 9 with Path

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());
}
Also used : UserRole(com.github.drinkjava2.functionstest.entitynet.entities.UserRole) Role(com.github.drinkjava2.functionstest.entitynet.entities.Role) Path(com.github.drinkjava2.jsqlbox.entitynet.Path) Email(com.github.drinkjava2.functionstest.entitynet.entities.Email) User(com.github.drinkjava2.functionstest.entitynet.entities.User) EntityNet(com.github.drinkjava2.jsqlbox.entitynet.EntityNet) RolePrivilege(com.github.drinkjava2.functionstest.entitynet.entities.RolePrivilege) Privilege(com.github.drinkjava2.functionstest.entitynet.entities.Privilege) Test(org.junit.Test)

Example 10 with Path

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());
}
Also used : Path(com.github.drinkjava2.jsqlbox.entitynet.Path) User(com.github.drinkjava2.functionstest.entitynet.entities.User) Email(com.github.drinkjava2.functionstest.entitynet.entities.Email) EntityNet(com.github.drinkjava2.jsqlbox.entitynet.EntityNet) EntityNetHandler(com.github.drinkjava2.jsqlbox.handler.EntityNetHandler) Test(org.junit.Test)

Aggregations

EntityNet (com.github.drinkjava2.jsqlbox.entitynet.EntityNet)15 Path (com.github.drinkjava2.jsqlbox.entitynet.Path)15 Test (org.junit.Test)15 User (com.github.drinkjava2.functionstest.entitynet.entities.User)12 Email (com.github.drinkjava2.functionstest.entitynet.entities.Email)8 Set (java.util.Set)6 Privilege (com.github.drinkjava2.functionstest.entitynet.entities.Privilege)4 Role (com.github.drinkjava2.functionstest.entitynet.entities.Role)4 RolePrivilege (com.github.drinkjava2.functionstest.entitynet.entities.RolePrivilege)4 UserRole (com.github.drinkjava2.functionstest.entitynet.entities.UserRole)4 TableModel (com.github.drinkjava2.jdialects.model.TableModel)4 TreeNode (com.github.drinkjava2.functionstest.entitynet.entities.TreeNode)3 FKeyModel (com.github.drinkjava2.jdialects.model.FKeyModel)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 ColumnModel (com.github.drinkjava2.jdialects.model.ColumnModel)1 SqlBoxException (com.github.drinkjava2.jsqlbox.SqlBoxException)1 EntityNetHandler (com.github.drinkjava2.jsqlbox.handler.EntityNetHandler)1 IOException (java.io.IOException)1