PageRenderTime 83ms CodeModel.GetById 40ms app.highlight 14ms RepoModel.GetById 26ms app.codeStats 1ms

/src/mpv5/webshopinterface/WSIManager.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 191 lines | 108 code | 36 blank | 47 comment | 8 complexity | 1cf5831fc9c82825d4b5113bb20acd06 MD5 | raw file
  1
  2/*
  3*  This file is part of YaBS.
  4*
  5*      YaBS is free software: you can redistribute it and/or modify
  6*      it under the terms of the GNU General Public License as published by
  7*      the Free Software Foundation, either version 3 of the License, or
  8*      (at your option) any later version.
  9*
 10*      YaBS is distributed in the hope that it will be useful,
 11*      but WITHOUT ANY WARRANTY; without even the implied warranty of
 12*      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13*      GNU General Public License for more details.
 14*
 15*      You should have received a copy of the GNU General Public License
 16*      along with YaBS.  If not, see <http://www.gnu.org/licenses/>.
 17 */
 18package mpv5.webshopinterface;
 19
 20//~--- non-JDK imports --------------------------------------------------------
 21
 22import mpv5.db.common.Context;
 23import mpv5.db.common.DatabaseObject;
 24import mpv5.db.common.NodataFoundException;
 25import mpv5.db.objects.WebShop;
 26
 27import mpv5.logging.Log;
 28
 29import mpv5.ui.dialogs.Popup;
 30
 31import mpv5.webshopinterface.wsdjobs.addContactJob;
 32import mpv5.webshopinterface.wsdjobs.newContactsJob;
 33import mpv5.webshopinterface.wsdjobs.newOrdersJob;
 34import mpv5.webshopinterface.wsdjobs.newSystemMessages;
 35import mpv5.webshopinterface.wsdjobs.updatedContactsJob;
 36import mpv5.webshopinterface.wsdjobs.updatedOrdersJob;
 37
 38//~--- JDK imports ------------------------------------------------------------
 39
 40import java.net.URL;
 41
 42import java.util.ArrayList;
 43import java.util.Collection;
 44import java.util.HashMap;
 45import java.util.Iterator;
 46import java.util.List;
 47import java.util.Set;
 48import java.util.Vector;
 49import java.util.logging.Level;
 50import java.util.logging.Logger;
 51
 52import javax.naming.OperationNotSupportedException;
 53
 54/**
 55 * Manages the web shops and related tasks
 56 */
 57public class WSIManager {
 58    private static WSIManager          instance;
 59    private ArrayList<WebShop>         shops       = new ArrayList<WebShop>();
 60    private HashMap<WebShop, WSDaemon> shopDeamons = new HashMap<WebShop, WSDaemon>();
 61
 62    private WSIManager() {
 63        reset();
 64    }
 65
 66    /**
 67     * Get the one and only WSI Manager instance
 68     * @return
 69     */
 70    public static WSIManager instanceOf() {
 71        return (instance == null)
 72               ? instance = new WSIManager()
 73               : instance;
 74    }
 75
 76    /**
 77     * Starts the WSI Manager
 78     */
 79    public void start() {
 80        Log.Debug(this, "Initiating WebShop clients..");
 81
 82        for (int i = 0; i < shops.size(); i++) {
 83            WebShop webShop = shops.get(i);
 84
 85            try {
 86                WSDaemon d = new WSDaemon(webShop);
 87
 88                d.addJob(new newContactsJob(d));
 89                d.addJob(new newOrdersJob(d));
 90                d.addJob(new newSystemMessages(d));
 91                d.addJob(new updatedContactsJob(d));
 92                d.addJob(new updatedOrdersJob(d));
 93                d.addJob(new addContactJob(d));
 94                d.start();
 95                shopDeamons.put(webShop, d);
 96                Log.Debug(this, "WebShop client: " + d + " started.");
 97            } catch (Exception ex) {
 98                Log.Debug(ex);
 99            }
100        }
101
102        Log.Debug(this, "Done with initiating WebShop clients.");
103    }
104
105    /**
106     * @return the shops
107     */
108    public ArrayList<WebShop> getShops() {
109        return shops;
110    }
111
112    /**
113     * Creates objects from the given map/list. Assumes data in the format [int id, String key, String value].
114     * @param <T>
115     * @param data
116     * @param template
117     * @return
118     */
119    @SuppressWarnings("unchecked")
120    public static <T extends DatabaseObject> List<T> createObjects(Object data, T template) {
121        Vector<T>      list    = new Vector<T>();
122        List<String[]> rawlist = new Vector<String[]>();
123
124        if (data instanceof HashMap) {
125            HashMap             m = (HashMap) data;
126            Collection<HashMap> d = m.values();
127
128            for (Iterator<HashMap> it = d.iterator(); it.hasNext(); ) {
129                HashMap hashMap = it.next();
130
131                rawlist.add(new String[] { String.valueOf(hashMap.get("id")), String.valueOf(hashMap.get("key")),
132                                           String.valueOf(hashMap.get("value")) });
133            }
134        } else    // else if (data instanceof List) {
135
136        // List n = (List) data;
137        // for (int i = 0; i < n.size(); i++) {
138        // rawlist.add(new String[]{String.valueOf(((Object[]) n.get(i))[0]), String.valueOf(((Object[]) n.get(i))[1]), String.valueOf(((Object[]) n.get(i))[2])});
139        // }
140        // } else
141        {
142            throw new IllegalArgumentException("Only HashMap is supported here! You provided: " + data.getClass());
143        }
144
145        HashMap<String, T> result = new HashMap<String, T>();
146
147        for (int i = 0; i < rawlist.size(); i++) {
148            String[] strings = rawlist.get(i);
149            String   id      = strings[0];
150            String   key     = strings[1];
151            String   value   = strings[2];
152
153            if (!result.containsKey(id)) {
154                result.put(id, (T) DatabaseObject.getObject(template.getContext()));
155            }
156
157            try {
158                result.get(id).parse(key, value);
159            } catch (Exception ex) {
160                Log.Debug(ex);
161            }
162        }
163
164        list.addAll(result.values());
165        Log.Debug(WSIManager.class, "Found sets: " + list.size());
166
167        return list;
168    }
169
170    /**
171     * Reset. Call start() afterwards.
172     */
173    public void reset() {
174        try {
175            shops.clear();
176
177            Set<WebShop> k = shopDeamons.keySet();
178
179            for (Iterator i = k.iterator(); i.hasNext(); ) {
180                shopDeamons.get((WebShop) i.next()).kill();
181            }
182
183            shops.addAll(DatabaseObject.toObjectList(DatabaseObject.getObjects(Context.getWebShop()), new WebShop()));
184        } catch (NodataFoundException ex) {
185            Log.Debug(this, ex.getMessage());
186        }
187    }
188}
189
190
191//~ Formatted by Jindent --- http://www.jindent.com