Skip to content

Instantly share code, notes, and snippets.

@vietj
Created June 1, 2011 15:07
Show Gist options
  • Save vietj/1002492 to your computer and use it in GitHub Desktop.
Save vietj/1002492 to your computer and use it in GitHub Desktop.
private void save(NavigationNodeContainerData src, Navigation dst)
{
if (src instanceof NavigationNodeData)
{
NavigationNodeData node = (NavigationNodeData)src;
Workspace workspace = dst.getSite().getWorkspace();
String reference = node.getPageReference();
if (reference != null)
{
String[] pageChunks = split("::", reference);
ObjectType<? extends Site> siteType = parseSiteType(pageChunks[0]);
Site site = workspace.getSite(siteType, pageChunks[1]);
org.gatein.mop.api.workspace.Page target = site.getRootPage().getChild("pages").getChild(pageChunks[2]);
PageLink link = dst.linkTo(ObjectType.PAGE_LINK);
link.setPage(target);
}
else
{
PageLink link = dst.linkTo(ObjectType.PAGE_LINK);
link.setPage(null);
}
//
Described described = dst.adapt(Described.class);
described.setName(node.getLabel());
if(!dst.getName().equals(((NavigationNodeData) src).getName()))
{
dst.setName(((NavigationNodeData) src).getName());
}
//
Visible visible = dst.adapt(Visible.class);
visible.setVisibility(node.getVisibility());
visible.setStartPublicationDate(node.getStartPublicationDate());
visible.setEndPublicationDate(node.getEndPublicationDate());
//
Attributes attrs = dst.getAttributes();
attrs.setValue(MappedAttributes.URI, node.getURI());
attrs.setValue(MappedAttributes.ICON, node.getIcon());
}
else if (src instanceof NavigationData)
{
NavigationData pageNav = (NavigationData)src;
//
Attributes attrs = dst.getAttributes();
attrs.setValue(MappedAttributes.PRIORITY, pageNav.getPriority());
}
else
{
throw new AssertionError();
}
//
final List<String> orders = new ArrayList<String>();
Set<String> savedSet = new HashSet<String>();
for (NavigationNodeData node : src.getNodes())
{
String srcId = node.getStorageId();
Navigation dstChild;
if (srcId != null)
{
dstChild = session.findObjectById(ObjectType.NAVIGATION, srcId);
}
else
{
dstChild = dst.getChild(node.getName());
if (dstChild == null)
{
dstChild = dst.addChild(node.getName());
}
srcId = dstChild.getObjectId();
}
save(node, dstChild);
savedSet.add(srcId);
orders.add(dstChild.getObjectId());
}
for (Iterator<? extends Navigation> i = dst.getChildren().iterator(); i.hasNext();)
{
Navigation dstChild = i.next();
if (!savedSet.contains(dstChild.getObjectId()))
{
Visible visible = dstChild.adapt(Visible.class);
if (visible.getVisibility() != Visibility.SYSTEM)
{
i.remove();
}
}
}
// Now sort children according to the order provided by the container
// need to replace that with Collections.sort once the set(int index, E element) is implemented in Chromattic lists
Navigation[] a = dst.getChildren().toArray(new Navigation[dst.getChildren().size()]);
Arrays.sort(a, new Comparator<Navigation>()
{
public int compare(Navigation o1, Navigation o2)
{
int i1 = orders.indexOf(o1.getObjectId());
int i2 = orders.indexOf(o2.getObjectId());
return i1 - i2;
}
});
for (int j = 0; j < a.length; j++)
{
dst.getChildren().add(j, a[j]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment