Skip to content

Instantly share code, notes, and snippets.

@chrismrgn chrismrgn/Sitemap.java

Last active Jul 14, 2016
Embed
What would you like to do?
Sitemap interface and classes for DD4T 2 Java
public interface Sitemap {
//Always one root Folder
Folder getFolder();
void setFolder(Folder folder);
//Example
List<BaseItem> getTopNavigation();
}
@JsonIgnoreProperties(ignoreUnknown = true)
public abstract class BaseSitemap implements Sitemap {
private Folder folder;
@Override
public Folder getFolder() {
return folder;
}
@Override
public void setFolder(Folder folder) {
this.folder = folder;
}
}
public class SitemapImpl extends BaseSitemap {
@Override
public List<BaseItem> getTopNavigation()
{
List<BaseItem> topNavigationItems = getTopItemsRecursive(this.getFolder(), new ArrayList<>());
return topNavigationItems;
}
//TODO: Method to be refined
private <T extends BaseItem> List<BaseItem> getTopItemsRecursive(T currentItem, List<BaseItem> allItems)
{
if(null == allItems)
allItems = new ArrayList<>();
if(currentItem.isTopnav())
allItems.add(currentItem);
if(currentItem.getClass().equals(Folder.class))
{
Folder folder = (Folder) currentItem;
for(Folder subFolder : folder.getFolders()) {
getTopItemsRecursive(subFolder, allItems);
}
for(Page page : folder.getPages()) {
getTopItemsRecursive(page, allItems);
}
}
return allItems;
}
}
public abstract class BaseItem {
private String id;
private String title;
private boolean topnav;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public boolean isTopnav() {
return topnav;
}
public void setTopnav(boolean topnav) {
this.topnav = topnav;
}
}
public class Folder extends BaseItem {
private int level;
private ArrayList<Folder> folders;
private ArrayList<Page> pages;
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public ArrayList<Page> getPages() {
if (null==pages) {
return new ArrayList<>();
}
return pages;
}
public void setPages(ArrayList<Page> pages) {
this.pages = pages;
}
public ArrayList<Folder> getFolders() {
if (null==folders) {
return new ArrayList<>();
}
return folders;
}
public void setFolders(ArrayList<Folder> folders) {
this.folders = folders;
}
}
public class Page extends BaseItem {
@JsonProperty("default")
private boolean isDefault;
public boolean isDefault() {
return isDefault;
}
public void setDefault(boolean aDefault) {
isDefault = aDefault;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.