001/*
002 * Stallion Core: A Modern Web Framework
003 *
004 * Copyright (C) 2015 - 2016 Stallion Software LLC.
005 *
006 * This program is free software: you can redistribute it and/or modify it under the terms of the
007 * GNU General Public License as published by the Free Software Foundation, either version 2 of
008 * the License, or (at your option) any later version. This program is distributed in the hope that
009 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
011 * License for more details. You should have received a copy of the GNU General Public License
012 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl-2.0.html>.
013 *
014 *
015 *
016 */
017
018package io.stallion.sitemaps;
019
020import io.stallion.dataAccess.Displayable;
021import io.stallion.restfulEndpoints.SlugRegistry;
022import io.stallion.settings.SecondaryDomain;
023
024import java.util.ArrayList;
025import java.util.List;
026
027import static io.stallion.Context.settings;
028import static io.stallion.utils.Literals.empty;
029
030
031public class SiteMapController {
032    private static SiteMapController _instance;
033    public static SiteMapController instance() {
034        return _instance;
035    }
036    public static void load() {
037        _instance = new SiteMapController();
038    }
039    public static void shutdown() {
040        _instance = null;
041    }
042
043    private List<SiteMapItem> extraItems = new ArrayList<>();
044
045    public List<SiteMapItem> getAllItems() {
046        List<SiteMapItem> items = new ArrayList<>();
047        items.addAll(extraItems);
048        for(Displayable item: SlugRegistry.instance().listAll()) {
049           items.add(
050                   new SiteMapItem()
051                           .setPermalink(item.getPermalink())
052           );
053        }
054        return items;
055    }
056
057    /**
058     * Get all items that exist on the given domain, as opposed to being accessible from a secondary
059     * domain.
060     *
061     * @param domain
062     * @return
063     */
064    public List<SiteMapItem> getAllItemsForDomain(String domain) {
065        boolean isDefaultDomain = true;
066        for(SecondaryDomain sd: settings ().getSecondaryDomains()) {
067            if (sd.getDomain().equals(domain)) {
068                isDefaultDomain = false;
069            }
070        }
071        List<SiteMapItem> items = new ArrayList<>();
072        items.addAll(extraItems);
073        for(Displayable item: SlugRegistry.instance().listAll()) {
074            if (isDefaultDomain && !empty(item.getOverrideDomain())) {
075                continue;
076            }
077            if (!isDefaultDomain && !domain.equals(item.getOverrideDomain())) {
078                continue;
079            }
080            items.add(
081                    new SiteMapItem()
082                            .setPermalink(item.getPermalink())
083            );
084        }
085        return items;
086    }
087
088    public SiteMapController addItem(SiteMapItem item) {
089        extraItems.add(item);
090        return this;
091    }
092
093    public SiteMapController addDisplayable(Displayable displayableItem) {
094        extraItems.add(
095                new SiteMapItem()
096                        .setPermalink(displayableItem.getPermalink())
097        );
098        return this;
099    }
100}