Skip to content

Commit

Permalink
Make sure nestedJobWhiteboard is processed in order
Browse files Browse the repository at this point in the history
There was a problem where depending on the order of the hashmap
insertion, nested folders would not show up in the GUI. The issue
was caused by using childrenMap.entrySet which doesn't concern
about order.

To solve this issue the map values were encapsulated in a object
that implements the right sorting based on level, making sure
children paths are processed before their parents.
  • Loading branch information
DiegoTavares authored and roulaoregan-spi committed Aug 24, 2021
1 parent be251e2 commit 00ffecb
Showing 1 changed file with 75 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
Expand Down Expand Up @@ -157,10 +157,56 @@ public CachedJobWhiteboardMapper(NestedJobWhiteboardMapper result) {
"AND " +
"folder.pk_dept = dept.pk_dept ";

class NestedJobWhiteboardMapper implements RowMapper<NestedGroup> {
private class ChildrenEntry {
String key;
int level;
List<String> children;
String name;

public ChildrenEntry(String key, int level, String name) {
this.key = key;
this.level = level;
this.children = new ArrayList<>();
this.name = name;
}

public List<String> getChildren() {
return children;
}

public void addChild(String child) {
children.add(child);
}

public String getKey() {
return key;
}

public String getName() {
return name;
}

public int compareTo(ChildrenEntry o) {
// Invert order
return Integer.compare(o.level, this.level);
}

public Map<String, NestedGroup> groups = new HashMap<String,NestedGroup>(50);
public Map<String, List<String>> childrenMap = new HashMap<String, List<String>>();
@Override
public String toString() {
StringBuilder out = new StringBuilder();
String spacing = " ".repeat(Math.max(0, this.level + 1));
out.append(spacing);
out.append(key + "(c " + name + ")");
for (String id : children) {
out.append("\n " + spacing + id.substring(0, 4));
}
return out.toString();
}
}

class NestedJobWhiteboardMapper implements RowMapper<NestedGroup> {
public Map<String, NestedGroup> groups = new HashMap<String, NestedGroup>(50);
public Map<String, ChildrenEntry> childrenMap = new HashMap<String, ChildrenEntry>();
public String rootGroupID;

@Override
Expand All @@ -186,12 +232,16 @@ public NestedGroup mapRow(ResultSet rs, int rowNum) throws SQLException {

String parentGroupId = rs.getString("pk_parent_folder");
if (parentGroupId != null) {
List<String> children = childrenMap.get(parentGroupId);
if (children == null) {
children = new ArrayList<>();
childrenMap.put(parentGroupId, children);
ChildrenEntry childrenEntry = childrenMap.get(parentGroupId);
if (childrenEntry == null) {
childrenEntry = new ChildrenEntry(
parentGroupId, group.getLevel() - 1, rs.getString("group_name"));
childrenEntry.addChild(groupId);
childrenMap.put(parentGroupId, childrenEntry);
}
else {
childrenEntry.addChild(groupId);
}
children.add(groupId);
}
else {
rootGroupID = rs.getString("pk_folder");
Expand Down Expand Up @@ -223,19 +273,22 @@ public NestedGroup mapRow(ResultSet rs, int rowNum) throws SQLException {
}

private NestedJobWhiteboardMapper updateConnections(NestedJobWhiteboardMapper mapper) {
for (Map.Entry<String, List<String>> entry : mapper.childrenMap.entrySet()) {
NestedGroup group = mapper.groups.get(entry.getKey());
NestedGroupSeq.Builder childrenBuilder = NestedGroupSeq.newBuilder();
for (String childId : entry.getValue()) {
NestedGroup child = mapper.groups.get(childId);
child = child.toBuilder().setParent(group).build();
childrenBuilder.addNestedGroups(child);
mapper.groups.put(childId, child);
}
group = group.toBuilder()
.setGroups(childrenBuilder.build())
.build();
mapper.groups.put(entry.getKey(), group);
ArrayList<ChildrenEntry> orderedChildren = new ArrayList<>(mapper.childrenMap.values());
orderedChildren.sort(ChildrenEntry::compareTo);

for (ChildrenEntry entry : orderedChildren) {
NestedGroup group = mapper.groups.get(entry.getKey());
NestedGroupSeq.Builder childrenBuilder = NestedGroupSeq.newBuilder();
for (String childId : entry.getChildren()) {
NestedGroup child = mapper.groups.get(childId);
child = child.toBuilder().setParent(group).build();
childrenBuilder.addNestedGroups(child);
mapper.groups.put(childId, child);
}
group = group.toBuilder()
.setGroups(childrenBuilder.build())
.build();
mapper.groups.put(entry.getKey(), group);
}
return mapper;
}
Expand Down

0 comments on commit 00ffecb

Please sign in to comment.