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.restfulEndpoints;
019
020import io.stallion.dataAccess.DataAccessRegistry;
021import io.stallion.dataAccess.Displayable;
022import io.stallion.dataAccess.ModelController;
023import io.stallion.exceptions.UsageException;
024import io.stallion.services.Log;
025
026import java.util.Collection;
027import java.util.HashMap;
028import java.util.List;
029import java.util.Map;
030
031import static io.stallion.utils.Literals.empty;
032import static io.stallion.utils.Literals.list;
033
034
035public class SlugRegistry {
036    private static SlugRegistry _instance;
037
038    private Map<String, BucketThing> slugMap = new HashMap<>();
039    private Map<String, String> redirectMap = new HashMap<>();
040
041    public SlugRegistry() {
042
043    }
044
045    public static SlugRegistry instance() {
046        if (_instance == null) {
047            throw new UsageException("Must call SlugRegistry.load() before calling instance();");
048        }
049        return _instance;
050    }
051
052    public static SlugRegistry load() {
053        _instance = new SlugRegistry();
054        return _instance;
055    }
056
057    public void shutdown() {
058        _instance = null;
059    }
060
061
062    public Collection<Displayable> listAll() {
063        List<Displayable> allThings = list();
064        for(BucketThing thing: slugMap.values()) {
065            allThings.add(
066                    (Displayable)DataAccessRegistry.instance().get(thing.getBucket()).originalForId(thing.getId())
067            );
068        }
069        return allThings;
070    }
071
072    public SlugRegistry addDisplayable(Displayable item) {
073        if (item.getSlug() == null) {
074            return this;
075        }
076        getSlugMap().put(item.getSlug(), new BucketThing().setBucket(item.getController().getBucket()).setId(item.getId()));
077        return this;
078    }
079
080    public boolean hasUrl(String url) {
081        return getSlugMap().containsKey(url);
082    }
083
084    public Displayable lookup(String url) {
085        BucketThing thing = getSlugMap().get(url);
086        Log.info("URL={0} thingId={1} thingBucket={2}", url, thing.getId(), thing.getBucket());
087        ModelController controller = DataAccessRegistry.instance().get(thing.getBucket());
088        return (Displayable)controller.originalForId(thing.getId());
089    }
090
091
092
093    public Displayable lookup(String url, Displayable defaultObj) {
094        Displayable item = lookup(url);
095        if (item == null) {
096            return defaultObj;
097        }
098        return item;
099    }
100
101    public Map<String, BucketThing> getSlugMap() {
102        return slugMap;
103    }
104
105    public void registerRedirect(String fromUrl, String toUrl) {
106        if (fromUrl == null || toUrl == null) {
107            return;
108        }
109        fromUrl = fromUrl.trim();
110        if (empty(fromUrl)) {
111            return;
112        }
113        redirectMap.put(fromUrl, toUrl);
114    }
115
116    public String returnRedirectIfExists(String fromUrl) {
117        if (redirectMap.containsKey(fromUrl)) {
118            return redirectMap.get(fromUrl);
119        }
120        return null;
121    }
122
123    public static class BucketThing {
124        private String bucket;
125        private Long id;
126
127        public String getBucket() {
128            return bucket;
129        }
130
131        public BucketThing setBucket(String bucket) {
132            this.bucket = bucket;
133            return this;
134        }
135
136        public Long getId() {
137            return id;
138        }
139
140        public BucketThing setId(Long id) {
141            this.id = id;
142            return this;
143        }
144    }
145}