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.users;
019
020import io.stallion.dataAccess.LocalMemoryStash;
021import io.stallion.dataAccess.filtering.FilterCache;
022import io.stallion.services.Log;
023
024import java.io.File;
025import java.util.List;
026
027import static io.stallion.utils.Literals.list;
028
029
030public class UserMemoryStash<T extends IUser> extends LocalMemoryStash<T> {
031    private PredefinedUsersPersister<T> predefinedUserPersister;
032
033
034
035    @Override
036    public void loadForId(Long id)  {
037        T existing = forId(id);
038        T item = null;
039        if (isPredefinedUser(existing)) {
040            item = (T)predefinedUserPersister.fetchOne(id);
041        }
042        if (item == null) {
043            item = (T) getPersister().fetchOne(id);
044        }
045        loadItem(item);
046        FilterCache.clearBucket(getBucket());
047    }
048
049
050    @Override
051    public void save(T obj) {
052        if (isPredefinedUser(obj)) {
053            predefinedUserPersister.persist(obj);
054            return;
055        }
056        super.save(obj);
057    }
058
059
060    public boolean isPredefinedUser(T user) {
061        if (user == null) {
062            return false;
063        }
064        return user.isPredefined();
065    }
066
067
068    @Override
069    public void loadAll()  {
070        Log.fine("Load all from {0}. ", getBucket());
071        List items = this.getPersister().fetchAll();
072        for(Object item: items) {
073            T displayable = (T)item;
074            loadItem(displayable);
075        }
076        loadPredefinedUsers();
077    }
078
079
080    public boolean loadItem(T item)  {
081        //Log.fine("Pojo item: {0}:{1}", item.getClass().getName(), item.getId());
082        boolean hasChanges = false;
083        if (item.getId() == null) {
084            Log.warn("Loading a pojo item with a null ID! bucket: {0} class:{1}", getBucket(), item.getClass().getName());
085        }
086
087        T original = itemByPrimaryKey.getOrDefault(item.getId(), null);
088        if (original != null) {
089            hasChanges = cloneInto(item, original, null, true, list());
090        } else {
091            registerItem(item);
092            hasChanges = true;
093        }
094        getController().onPostLoadItem(item);
095        registerKeys(item);
096        return hasChanges;
097    }
098
099
100    /**
101     * Users are defined one of two ways:
102     * - dynamically via the application, and stored either in the production database or the production app-date folder
103     * - as a json file in target-path/users folder. These users are created at the command line and are version controlled
104     *   and deployed with the app. These mainly should be for admins and super users, for small sites.
105     */
106    public void loadPredefinedUsers() {
107        predefinedUserPersister = new PredefinedUsersPersister();
108        predefinedUserPersister.init(getController());
109        File folder = new File(predefinedUserPersister.getBucketFolderPath());
110        if (folder.isDirectory()) {
111            List items = predefinedUserPersister.fetchAll();
112            for(Object item: items) {
113                T displayable = (T)item;
114                displayable.setApproved(true);
115                loadItem(displayable);
116            }
117        } else {
118            folder.mkdirs();
119        }
120        predefinedUserPersister.attachWatcher();
121    }
122
123}