PageRenderTime 24ms CodeModel.GetById 2ms app.highlight 16ms RepoModel.GetById 2ms app.codeStats 0ms

/src/mpv5/db/objects/Favourite.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 152 lines | 73 code | 25 blank | 54 comment | 3 complexity | 22f87696ae98440a083d622ca91c0fe7 MD5 | raw file
  1/*
  2 *  This file is part of YaBS.
  3 *
  4 *      YaBS is free software: you can redistribute it and/or modify
  5 *      it under the terms of the GNU General Public License as published by
  6 *      the Free Software Foundation, either version 3 of the License, or
  7 *      (at your option) any later version.
  8 *
  9 *      YaBS is distributed in the hope that it will be useful,
 10 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 11 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12 *      GNU General Public License for more details.
 13 *
 14 *      You should have received a copy of the GNU General Public License
 15 *      along with YaBS.  If not, see <http://www.gnu.org/licenses/>.
 16 */
 17package mpv5.db.objects;
 18
 19import javax.swing.JComponent;
 20import mpv5.db.common.Context;
 21import mpv5.db.common.DatabaseObject;
 22import mpv5.globals.Messages;
 23import mpv5.ui.frames.MPView;
 24import mpv5.db.objects.User;
 25
 26/**
 27 *
 28 *  
 29 */
 30public class Favourite extends DatabaseObject {
 31
 32
 33    private int usersids;
 34    private int itemsids;
 35
 36    public Favourite() {
 37        setContext(Context.getFavourite());
 38    }
 39
 40    public Favourite(DatabaseObject dato) {
 41        setContext(Context.getFavourite());
 42        this.setUsersids(mpv5.db.objects.User.getCurrentUser().__getIDS());
 43        setCname(dato.getDbIdentity());
 44        setItemsids(dato.__getIDS());
 45    }
 46
 47
 48
 49    /**
 50     * @return the userid
 51     */
 52    public int __getUsersids() {
 53        return usersids;
 54    }
 55
 56    /**
 57     * @param userid the userid to set
 58     */
 59    public void setUsersids(int userid) {
 60        this.usersids = userid;
 61    }
 62
 63    /**
 64     * @return the itemid
 65     */
 66    public int __getItemsids() {
 67        return itemsids;
 68    }
 69
 70    /**
 71     * @param itemid the itemid to set
 72     */
 73    public void setItemsids(int itemid) {
 74        this.itemsids = itemid;
 75    }
 76
 77    /**
 78     * Checks if the given do is in the favourites list of the current logged in user
 79     * @param dato
 80     * @return
 81     */
 82    public static boolean isFavourite(DatabaseObject dato) {
 83        if (!mpv5.db.common.QueryHandler.instanceOf().clone(Context.getFavourite()).
 84                checkConstraint(new String[]{"cname", "usersids", "itemsids"},
 85                    new Object[]{dato.getDbIdentity(), mpv5.db.objects.User.getCurrentUser().__getIDS(),dato.__getIDS()})) {
 86            return true;
 87        } else {
 88            return false;
 89        }
 90    }
 91
 92    /**
 93     * Checks if the given do is in the favourites list of the current logged in user
 94     * and removes it
 95     * @param dato
 96     */
 97    public static void removeFavourite(DatabaseObject dato) {
 98        
 99            mpv5.db.common.QueryHandler.instanceOf().clone(Context.getFavourite()).
100                    delete(new String[]{"cname", "usersids", "itemsids"},
101                    new Object[]{dato.getDbIdentity(), mpv5.db.objects.User.getCurrentUser().__getIDS(),dato.__getIDS()}, Messages.DONE.toString());
102        
103    }
104
105    /**
106     * 
107     * @return The favourites list of the current logged in user
108     */
109    public static Favourite[] getUserFavourites() {
110
111        Object[][] data = mpv5.db.common.QueryHandler.instanceOf().clone(Context.getFavourite()).select("cname, usersids, itemsids", new String[]{"usersids", mpv5.db.objects.User.getCurrentUser().__getIDS().toString(), ""});
112        Favourite[] favs = new Favourite[data.length];
113
114        for (int i = 0; i < favs.length; i++) {
115            Favourite favi = new Favourite();
116            favi.setCname(String.valueOf(data[i][0]));
117            favi.setUsersids(Integer.valueOf(data[i][1].toString()));
118            favi.setItemsids(Integer.valueOf(data[i][2].toString()));
119            favs[i] = favi;
120        }
121
122        return favs;
123    }
124
125
126    /**
127     * Returns the context of the do represented by this fav
128     * @return
129     */
130    public Context getFavContext() {
131       return Context.getMatchingContext(__getCname());
132    }
133
134
135    /**
136     * Flushes the users favourites
137     * @param user
138     */
139    public static void flush(User user) {
140       mpv5.db.common.QueryHandler.instanceOf().clone(Context.getFavourite()).delete(new String[][]{{"usersids",user.__getIDS().toString(),""}},
141               Messages.DONE.toString());
142    }
143
144    @Override
145    public JComponent getView() {
146        throw new UnsupportedOperationException("Not supported yet.");
147    }
148     @Override
149    public mpv5.utils.images.MPIcon getIcon() {
150        return null;
151    }
152}