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.PartialStash;
021import io.stallion.dataAccess.db.DB;
022import io.stallion.dataAccess.db.Schema;
023import io.stallion.dataAccess.filtering.FilterCache;
024import io.stallion.settings.Settings;
025
026import java.io.File;
027import java.util.List;
028
029import static io.stallion.utils.Literals.*;
030import static io.stallion.Context.*;
031
032
033public class UserPartialStash<T extends IUser> extends PartialStash<T> {
034    private PredefinedUsersPersister<T> predefinedUserPersister;
035
036    @Override
037    public void loadAll() {
038        for(T obj: DB.instance().query(getPersister().getModelClass(), getInitialLoadSql())) {
039            loadItem(obj);
040        }
041        loadPredefinedUsers();
042    }
043
044    public String getInitialLoadSql() {
045        Schema schema = DB.instance().getSchema(getPersister().getModelClass());
046        String sql = "SELECT * FROM " + schema.getName() + " ORDER BY row_updated_at DESC LIMIT " + Settings.instance().getUsers().getLimitSyncUsersToMemoryToCount();
047        return sql;
048    }
049
050
051
052
053    @Override
054    public void loadForId(Long id)  {
055        T existing = forId(id);
056        T item = null;
057        if (isPredefinedUser(existing)) {
058            item = (T)predefinedUserPersister.fetchOne(id);
059        }
060        if (item == null) {
061            item = (T) getPersister().fetchOne(id);
062        }
063        loadItem(item);
064        FilterCache.clearBucket(getBucket());
065    }
066
067
068    @Override
069    public void save(T obj) {
070        if (isPredefinedUser(obj)) {
071            predefinedUserPersister.persist(obj);
072            return;
073        }
074        super.save(obj);
075    }
076
077
078    public boolean isPredefinedUser(T user) {
079        if (user == null) {
080            return false;
081        }
082        return user.isPredefined();
083    }
084
085
086
087    /**
088     * Users are defined one of two ways:
089     * - dynamically via the application, and stored either in the production database or the production app-date folder
090     * - as a json file in target-path/users folder. These users are created at the command line and are version controlled
091     *   and deployed with the app. These mainly should be for admins and super users, for small sites.
092     */
093    public void loadPredefinedUsers() {
094        predefinedUserPersister = new PredefinedUsersPersister();
095        predefinedUserPersister.init(getController());
096        File folder = new File(predefinedUserPersister.getBucketFolderPath());
097        if (folder.isDirectory()) {
098            List items = predefinedUserPersister.fetchAll();
099            for(Object item: items) {
100                T displayable = (T)item;
101                displayable.setApproved(true);
102                loadItem(displayable);
103            }
104        } else {
105            folder.mkdirs();
106        }
107        predefinedUserPersister.attachWatcher();
108    }
109
110}