PageRenderTime 262ms CodeModel.GetById 80ms app.highlight 7ms RepoModel.GetById 114ms app.codeStats 0ms

/src/mpv5/handler/TrashHandler.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 89 lines | 45 code | 9 blank | 35 comment | 1 complexity | e51183cd232f753b77e72f47a361b8eb 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.handler;
18
19import javax.swing.SwingUtilities;
20import mpv5.db.common.Context;
21import mpv5.db.common.DatabaseObject;
22import mpv5.db.common.NodataFoundException;
23import mpv5.db.common.QueryCriteria;
24import mpv5.db.common.QueryData;
25import mpv5.db.common.QueryHandler;
26import mpv5.globals.Messages;
27import mpv5.logging.Log;
28import mpv5.ui.frames.MPView;
29
30/**
31 *
32 */
33public class TrashHandler {
34
35    /**
36     * Finally delete an item
37     * @param type
38     * @param id
39     * @param message
40     */
41    public static void delete(final String type, final int id, final String message) {
42        QueryHandler.instanceOf().clone(type).delete(new String[][]{{"ids", String.valueOf(id), ""}}, message);
43        Runnable runnable = new Runnable() {
44
45            public void run() {
46                QueryHandler.instanceOf().clone(Context.getHistory()).insertHistoryItem(message, mpv5.db.objects.User.getCurrentUser().__getCname(), type, id, mpv5.db.objects.User.getCurrentUser().__getGroupsids());
47            }
48        };
49        SwingUtilities.invokeLater(runnable);
50    }
51
52    /**
53     * Fetches the trashbin data
54     * @return
55     */
56    public static Object[][] getData() {
57        Object[][] data = QueryHandler.instanceOf().clone("trashbin").select("cname, rowid, description", new String[]{"deleteme", "1", ""});
58        for (int i = 0; i < data.length; i++) {
59            data[i][0] = data[i][0].toString().toUpperCase();
60        }
61        return data;
62    }
63
64    /**
65     * Restores the given item
66     * @param type
67     * @param id
68     * @param message
69     */
70    public static void restore(final String type, final int id, final String message) {
71
72        Context context = Context.getMatchingContext(type);
73        try {
74            DatabaseObject dbo = DatabaseObject.getObject(context, id, true);
75            Log.Debug(TrashHandler.class, "Restoring: " + dbo);
76            dbo.undelete();
77        } catch (NodataFoundException ex) {
78            Log.Debug(ex);
79        }
80
81        Runnable runnable = new Runnable() {
82
83            public void run() {
84                QueryHandler.instanceOf().clone(Context.getHistory()).insertHistoryItem(message, mpv5.db.objects.User.getCurrentUser().__getCname(), type, id, mpv5.db.objects.User.getCurrentUser().__getGroupsids());
85            }
86        };
87        SwingUtilities.invokeLater(runnable);
88    }
89}