Skip to content

Commit

Permalink
RS-6567: AddChild method is not working if child is hibernate proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
edu-de committed Dec 7, 2022
1 parent eaf9128 commit 243aa31
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
10 changes: 7 additions & 3 deletions src/net/datenwerke/treedb/service/treedb/AbstractNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -332,14 +332,18 @@ final public void addChild(N child) {
addChild(child, children.size() + 1);
}

final public void addChild(N child, int position) {
final public void addChild(N childObj, int position) {
N child = childObj;
if (childObj instanceof HibernateProxy)
childObj = (N) ((HibernateProxy) childObj).getHibernateLazyInitializer().getImplementation();

/* make sure child is not denied */
boolean foundInListOfDeniedChildren = false;
for (Class<?> potentialChild : getDeniedChildren()) {
try {
potentialChild.cast(child);
if (foundInListOfDeniedChildren)
throw new UnsupportedChildException(this.getClass(), child.getClass());
throw new UnsupportedChildException(this, child);
} catch (ClassCastException e) {
}
}
Expand All @@ -355,7 +359,7 @@ final public void addChild(N child, int position) {
}
}
if (!foundInListOfAllowedChildren)
throw new UnsupportedChildException(this.getClass(), child.getClass());
throw new UnsupportedChildException(this, child);

if (child.getParent() != null)
child.getParent().removeChild(child);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,23 @@ public class UnsupportedChildException extends TreeDBRuntimeException {
*/
private static final long serialVersionUID = -5119598342580086538L;

public UnsupportedChildException(Class<? extends AbstractNode> parent, Class<? extends AbstractNode> child) {
super(TreeDbMessages.INSTANCE.exceptionUnsupportedChild(child.getSimpleName(), parent.getSimpleName()));
public UnsupportedChildException(AbstractNode<?> parent, AbstractNode<?> child) {
super(TreeDbMessages.INSTANCE.exceptionUnsupportedChild(getNodeInfo(child),
getNodeInfo(parent)));
}

private static String getNodeInfo(AbstractNode<?> node) {
StringBuilder sb = new StringBuilder();
sb.append("(class: ")
.append(node.getClass().getSimpleName())
.append(", ")
.append("id: ")
.append(node.getId())
.append(", ")
.append("name: ")
.append(node.getNodeName())
.append(")");
return sb.toString();
}

}

0 comments on commit 243aa31

Please sign in to comment.