Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure nestedJobWhiteboard is processed in order #973

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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