PageRenderTime 15ms CodeModel.GetById 2ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 1ms

/src/mpv5/db/objects/WSContactsMapping.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 186 lines | 107 code | 18 blank | 61 comment | 6 complexity | 82cf92ea0f61b97861b6326de32a2f43 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 java.util.Collections;
 20import java.util.List;
 21import java.util.Vector;
 22import java.util.logging.Level;
 23import java.util.logging.Logger;
 24import javax.swing.JComponent;
 25import mpv5.db.common.Context;
 26import mpv5.db.common.DatabaseObject;
 27import mpv5.db.common.NodataFoundException;
 28import mpv5.db.common.QueryCriteria;
 29import mpv5.db.common.QueryHandler;
 30import mpv5.db.common.ReturnValue;
 31import mpv5.logging.Log;
 32import mpv5.usermanagement.MPSecurityManager;
 33import mpv5.utils.images.MPIcon;
 34
 35/**
 36 * Maps Contacts to WebShop Contacts
 37 */
 38public class WSContactsMapping extends DatabaseObject {
 39
 40    /**
 41     * Fetches a mapping from db
 42     * @param webShopID
 43     * @param contactsids
 44     * @return
 45     * @throws NodataFoundException 
 46     */
 47    public static WSContactsMapping getMapping(int webShopID, int contactsids) throws NodataFoundException {
 48        QueryCriteria qs = new QueryCriteria("webshopid", webShopID);
 49        qs.addAndCondition("contactsids", contactsids);
 50        List old = DatabaseObject.getObjects(Context.getWebShopContactMapping(), qs);
 51        return (WSContactsMapping) old.get(0);
 52    }
 53
 54    /**
 55     * Fetches all contacts from the db which:
 56     * <li>Have no current mapping to the given webshop
 57     * <li>Are in the same group as the webshop (ignored if shop is in "All" group)
 58     * @param webShop
 59     * @return A list of Contacts
 60     */
 61    public static List<Contact> getUnmappedContacts(WebShop webShop) {
 62        String query = Context.getContact().prepareSQLString("SELECT contacts.ids FROM contacts WHERE contacts.ids NOT IN " +
 63                "(SELECT contactsids FROM wscontactsmapping WHERE webshopsids = " + webShop.__getIDS() + ") ");
 64        if (webShop.__getGroupsids() > 1) {
 65            query += " AND groupsids = " + webShop.__getGroupsids();
 66        }
 67        ReturnValue ads = QueryHandler.instanceOf().clone(Context.getContact()).freeSelectQuery(query, MPSecurityManager.VIEW, null);
 68        Object[][] data = ads.getData();
 69        List<Contact> l = new Vector<Contact>();
 70        for (int i = 0; i < data.length; i++) {
 71            int ig = Integer.valueOf(data[i][0].toString());
 72            try {
 73                l.add((Contact) DatabaseObject.getObject(Context.getContact(), ig));
 74            } catch (NodataFoundException ex) {
 75                Log.Debug(WSContactsMapping.class, ex.getMessage());
 76            }
 77        }
 78
 79        Log.Debug(WSContactsMapping.class, "Found unmapped contacts: " + l);
 80        return l;
 81    }
 82
 83    /**
 84     * Tries to find the highest WS id, in the following order:
 85     * <li>If the IDs are {@link Number} values, returns the highest number
 86     * <li>If the values are non-number values, returns the highest values such as Collections.sort(List<String>, String.CASE_INSENSITIVE_ORDER) would return as last value
 87     * @param webShop 
 88     * @return The String representation of the highest found number
 89     */
 90    public static String getLastWsID(WebShop webShop) {
 91        String query = Context.getWebShopContactMapping().prepareSQLString("SELECT wscontact FROM wscontactsmapping WHERE webshopsids = " + webShop.__getIDS());
 92        ReturnValue ads = QueryHandler.instanceOf().clone(Context.getWebShopContactMapping()).freeSelectQuery(query, MPSecurityManager.VIEW, null);
 93
 94        Object[][] data = ads.getData();
 95        List<String> l = new Vector<String>();
 96        boolean stringvals = false;
 97        Integer hn = 0;
 98        for (int i = 0; i < data.length; i++) {
 99            try {
100                Integer d = Integer.valueOf(data[i][0].toString());
101                if (d > hn) {
102                    hn = d;
103                }
104            } catch (NumberFormatException numberFormatException) {
105                stringvals = true;
106            }
107            l.add(String.valueOf(data[i][0].toString()));
108        }
109
110        if (stringvals) {
111            Collections.sort(l, String.CASE_INSENSITIVE_ORDER);
112            Log.Debug(WSContactsMapping.class, "Last String id: " + l.get(l.size()));
113            return String.valueOf(l.get(l.size()));
114        } else {
115            Log.Debug(WSContactsMapping.class, "Last Number id: " + hn);
116            return String.valueOf(hn);
117        }
118    }
119    private int webshopsids;
120    private int contactsids;
121    private String wscontact = "";
122
123    /**
124     * Create a new mapping
125     */
126    public WSContactsMapping() {
127        setContext(Context.getWebShopContactMapping());
128    }
129
130    @Override
131    public JComponent getView() {
132        return null;
133    }
134
135    @Override
136    public MPIcon getIcon() {
137        return null;
138    }
139
140    /**
141     * @return the webshopsids
142     */
143    public int __getWebshopsids() {
144        return webshopsids;
145    }
146
147    /**
148     * @param webshopsids the webshopsids to set
149     */
150    public void setWebshopsids(int webshopsids) {
151        this.webshopsids = webshopsids;
152    }
153
154    /**
155     * @return the contactsids
156     */
157    public int __getContactsids() {
158        return contactsids;
159    }
160
161    /**
162     * @param contactsids the contactsids to set
163     */
164    public void setContactsids(int contactsids) {
165        this.contactsids = contactsids;
166    }
167
168    /**
169     * @return the wscontact
170     */
171    public String __getWscontact() {
172        return wscontact;
173    }
174
175    /**
176     * @param wscontact the wscontact to set
177     */
178    public void setWscontact(String wscontact) {
179        this.wscontact = wscontact;
180    }
181
182    @Override
183    public boolean save() {
184        return super.save(true);
185    }
186}