PageRenderTime 122ms CodeModel.GetById 13ms app.highlight 97ms RepoModel.GetById 2ms app.codeStats 0ms

/src/mpv5/db/common/Context.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 1786 lines | 1302 code | 187 blank | 297 comment | 105 complexity | f3af97773d126fe701ae97555a0d949a MD5 | raw file
   1package mpv5.db.common;
   2
   3import java.io.Serializable;
   4import java.util.ArrayList;
   5import java.util.Arrays;
   6import java.util.HashMap;
   7import java.util.List;
   8import java.util.Map;
   9import mpv5.db.objects.Account;
  10import mpv5.db.objects.ActivityList;
  11import mpv5.db.objects.ActivityListSubItem;
  12import mpv5.db.objects.Address;
  13import mpv5.db.objects.Company;
  14import mpv5.db.objects.Contact;
  15import mpv5.db.objects.Conversation;
  16import mpv5.db.objects.Expense;
  17import mpv5.db.objects.Favourite;
  18import mpv5.db.objects.FileToContact;
  19import mpv5.db.objects.Group;
  20import mpv5.db.objects.HistoryItem;
  21import mpv5.db.objects.UserProperty;
  22import mpv5.db.objects.Schedule;
  23import mpv5.db.objects.FileToItem;
  24import mpv5.db.objects.FileToProduct;
  25import mpv5.db.objects.Item;
  26import mpv5.db.objects.MailMessage;
  27import mpv5.db.objects.MassprintRules;
  28import mpv5.db.objects.Product;
  29import mpv5.db.objects.ProductGroup;
  30import mpv5.db.objects.ProductList;
  31import mpv5.db.objects.ProductOrder;
  32import mpv5.db.objects.ProductOrderSubItem;
  33import mpv5.db.objects.ProductPrice;
  34import mpv5.db.objects.ProductlistSubItem;
  35import mpv5.db.objects.ProductsToSuppliers;
  36import mpv5.db.objects.Reminder;
  37import mpv5.db.objects.Revenue;
  38import mpv5.db.objects.ScheduleTypes;
  39import mpv5.db.objects.Stage;
  40import mpv5.db.objects.SubItem;
  41import mpv5.db.objects.Tax;
  42import mpv5.db.objects.Template;
  43import mpv5.pluginhandling.Plugin;
  44import mpv5.db.objects.User;
  45import mpv5.db.objects.ValueProperty;
  46import mpv5.db.objects.WSContactsMapping;
  47import mpv5.db.objects.WSItemsMapping;
  48import mpv5.db.objects.WebShop;
  49import mpv5.logging.Log;
  50import mpv5.pluginhandling.UserPlugin;
  51import mpv5.usermanagement.MPSecurityManager;
  52import mpv5.utils.text.RandomText;
  53import mpv5.utils.xml.XMLReader;
  54
  55/**
  56 *
  57 * Context controls Database Queries for all native MP {@link DatabaseObject}s
  58 */
  59public class Context implements Serializable {
  60
  61    //********** tablenames ****************************************************
  62    public static String IDENTITY_CONTACTS = "contacts";
  63    public static String IDENTITY_USERS = "users";
  64    public static String IDENTITY_ITEMS = "items";
  65    public static String IDENTITY_SUBITEMS = "subitems";
  66    public static String IDENTITY_LANGUAGES = "languages";
  67    public static String IDENTITY_COUNTRIES = "countries";
  68    public static String IDENTITY_PRODUCTS = "products";
  69    public static String IDENTITY_FILES = "files";
  70    public static String IDENTITY_LOCK = "tablelock";
  71    public static String IDENTITY_FAVS = "favourites";
  72    public static String IDENTITY_ADDRESS = "addresses";
  73    public static String IDENTITY_GROUPS = "groups";
  74    public static String IDENTITY_PGROUPS = "productgroups";
  75    public static String IDENTITY_SCHEDULE = "schedule";
  76    public static String IDENTITY_SCHEDULE_TYPES = "scheduletypes";
  77    public static String IDENTITY_HISTORY = "history";
  78    public static String IDENTITY_FILES_TO_CONTACTS = "filestocontacts";
  79    public static String IDENTITY_FILES_TO_PRODUCTS = "filestoproducts";
  80    public static String IDENTITY_SEARCHINDEX = "searchindex";
  81    public static String IDENTITY_PLUGINS_TO_USERS = "pluginstousers";
  82    public static String IDENTITY_TEMPLATES_TO_USERS = "templatestousers";
  83    public static String IDENTITY_PLUGINS = "plugins";
  84    public static String IDENTITY_PROPERTIES_TO_USERS = "userproperties";
  85    public static String IDENTITY_ACCOUNTS = "accounts";
  86    public static String IDENTITY_ITEMS_TO_ACCOUNTS = "itemstoaccounts";
  87    public static String IDENTITY_PRODUCTSLISTITEMS = "productlistitems";
  88    public static String IDENTITY_PRODUCTSLIST = "productlists";
  89    public static String IDENTITY_FORMATS_T_USERS = "formatstousers";
  90    public static String IDENTITY_FILES_TO_ITEMS = "filestoitems";
  91    public static String IDENTITY_MAIL = "mails";
  92    public static String IDENTITY_TAX = "tax";
  93    public static String IDENTITY_COMPANIES = "comps";
  94    public static String IDENTITY_GLOBALSETTINGS = "globalsettings";
  95    public static String IDENTITY_WEBSHOPS = "webshops";
  96    public static String IDENTITY_WSMAPPING = "wscontactsmapping";
  97    public static String IDENTITY_WSIMAPPING = "wsitemsmapping";
  98    public static String IDENTITY_TEMPLATES = "templates";
  99    public static String IDENTITY_REMINDERS = "reminders";
 100    public static String IDENTITY_STAGES = "stages";
 101    public static String IDENTITY_REVENUE = "revenues";
 102    public static String IDENTITY_EXPENSE = "expenses";
 103    public static String IDENTITY_PRODUCTS_TO_SUPPLIERS = "productstosuppliers";
 104    public static String IDENTITY_VALUE_PROPERTIES = "valueproperties";
 105    public static String IDENTITY_CONVERSATION = "conversations";
 106    public static String IDENTITY_PRODUCT_PRICES = "productprices";
 107    public static String IDENTITY_ACTIVITYLISTITEMS = "activitylistitems";
 108    public static String IDENTITY_ACTIVITYLIST = "activitylists";
 109    public static String IDENTITY_MASSPRINT = "massprintrules";
 110    public static String IDENTITY_PRODUCTORDER = "productorders";
 111    public static String IDENTITY_PRODUCTORDERSUBITEM = "productordersubitems";
 112    //********** identity classes **********************************************
 113    private static Class<Contact> IDENTITY_CONTACTS_CLASS = Contact.class;
 114    private static Class<Address> IDENTITY_ADDRESS_CLASS = Address.class;
 115    private static Class<User> IDENTITY_USERS_CLASS = User.class;
 116    private static Class<Item> IDENTITY_ITEMS_CLASS = Item.class;
 117    private static Class<FileToContact> IDENTITY_CONTACTS_FILES_CLASS = FileToContact.class;
 118    private static Class<FileToItem> IDENTITY_ITEM_FILES_CLASS = FileToItem.class;
 119    private static Class<HistoryItem> IDENTITY_HISTORY_ITEMS_CLASS = HistoryItem.class;
 120    private static Class<SubItem> IDENTITY_SUBITEMS_CLASS = SubItem.class;
 121    private static Class<UserPlugin> IDENTITY_USER_PLUGINS_CLASS = UserPlugin.class;
 122    private static Class<Plugin> IDENTITY_PLUGINS_CLASS = Plugin.class;
 123    private static Class<UserProperty> IDENTITY_PROPERTIES_CLASS = UserProperty.class;
 124    private static Class<Account> IDENTITY_ACCOUNTS_CLASS = Account.class;
 125    private static Class<ProductlistSubItem> IDENTITY_ITEMSLIST_CLASS = ProductlistSubItem.class;
 126    private static Class<MailMessage> IDENTITY_MAILS_CLASS = MailMessage.class;
 127    private static Class<Product> IDENTITY_PRODUCTS_CLASS = Product.class;
 128    private static Class<Group> IDENTITY_GROUPS_CLASS = Group.class;
 129    private static Class<Company> IDENTITY_COMPANY_CLASS = Company.class;
 130    private static Class<ProductGroup> IDENTITY_PGROUPS_CLASS = ProductGroup.class;
 131    private static Class<FileToProduct> IDENTITY_PRODUCTS_FILES_CLASS = FileToProduct.class;
 132    private static Class<WebShop> IDENTITY_WEBSHOP_CLASS = WebShop.class;
 133    private static Class<Template> IDENTITY_TEMPLATE_CLASS = Template.class;
 134    private static Class<Reminder> IDENTITY_REMINDER_CLASS = Reminder.class;
 135    private static Class<Stage> IDENTITY_STAGE_CLASS = Stage.class;
 136    private static Class<ValueProperty> IDENTITY_VALUEPROPERTY_CLASS = ValueProperty.class;
 137    private static Class<ProductPrice> IDENTITY_PRODUCTPRICE_CLASS = ProductPrice.class;
 138    private static Class<Favourite> IDENTITY_FAVOURITE_CLASS = Favourite.class;
 139    private static Class<Tax> IDENTITY_TAX_CLASS = Tax.class;
 140    private static Class<WSContactsMapping> IDENTITY_WSCONTACTSMAPPING_CLASS = WSContactsMapping.class;
 141    private static Class<WSItemsMapping> IDENTITY_WSITEMSMAPPING_CLASS = WSItemsMapping.class;
 142    private static Class<Expense> IDENTITY_EXPENSE_CLASS = Expense.class;
 143    private static Class<ProductList> IDENTITY_PRODUCTLIST_CLASS = ProductList.class;
 144    private static Class<ProductsToSuppliers> IDENTITY_PRODUCTSTOSUPPLIERS_CLASS = ProductsToSuppliers.class;
 145    private static Class<Conversation> IDENTITY_CONVERSATION_CLASS = Conversation.class;
 146    private static Class<Schedule> IDENTITY_SCHEDULE_CLASS = Schedule.class;
 147    private static Class<ScheduleTypes> IDENTITY_SCHEDULETYPES_CLASS = ScheduleTypes.class;
 148    private static Class<ActivityListSubItem> IDENTITY_ACTIVITYITEMSLIST_CLASS = ActivityListSubItem.class;
 149    private static Class<ActivityList> IDENTITY_ACTIVITYLIST_CLASS = ActivityList.class;
 150    private static Class<MassprintRules> IDENTITY_MASSPRINT_CLASS = MassprintRules.class;
 151    private static Class<ProductOrder> IDENTITY_PRODUCTORDER_CLASS = ProductOrder.class;
 152    private static Class<ProductOrderSubItem> IDENTITY_PRODUCTORDERSUBITEM_CLASS = ProductOrderSubItem.class;
 153    
 154    //********** unique constraints *******************************************
 155    public static String UNIQUECOLUMNS_USER = "cname";
 156    public static String UNIQUECOLUMNS_ITEMS = "cname";
 157    public static String UNIQUECOLUMNS_GROUPS = "cname";
 158    public static String UNIQUECOLUMNS_DEFAULT = "cname";
 159    public static String DETAIL_CONTACT_SEARCH = "prename,cname,street,city,country,notes";
 160    private static final long serialVersionUID = 1L;
 161
 162    //********** conditions ****************************************************
 163    private boolean isCompany = false;
 164    private boolean isCustomer = false;
 165    private boolean isManufacturer = false;
 166    private boolean isSupplier = false;
 167    private Integer itemStatus = null;
 168    private Integer itemType = null;
 169    public static final String CONDITION_DEFAULT = "%%tablename%%" + "." + "IDS>0";
 170    public static final String CONDITION_CONTACTS_COMPANY = IDENTITY_CONTACTS + "." + "iscompany";
 171    public static final String CONDITION_CONTACTS_CUSTOMER = IDENTITY_CONTACTS + "." + "iscustomer";
 172    public static final String CONDITION_CONTACTS_MANUFACTURER = IDENTITY_CONTACTS + "." + "ismanufacturer";
 173    public static final String CONDITION_CONTACTS_SUPPLIER = IDENTITY_CONTACTS + "." + "issupplier";
 174    public static final String CONDITION_ITEMS_TYPE = IDENTITY_ITEMS + "." + "inttype";
 175    public static final String CONDITION_ITEMS_STATUS = IDENTITY_ITEMS + "." + "intstatus";
 176    //********** searchfields **************************************************
 177    public static final String SEARCH_NAME = "cname";
 178//    public static final String SEARCH_CONTACT_NUMBER = "cnumber";
 179//    public static final String SEARCH_CONTACT_CITY = "city";
 180    //********** defaults ******************************************************
 181    public static String DEFAULT_SUBID = "ids, cname";
 182    public static String DEFAULT_CONTACT_SEARCH = "ids, cnumber, cname, city, street";
 183    public static String DEFAULT_USER_SEARCH = "ids, cname, mail, lastlogdate";
 184    public static String DEFAULT_ITEM_SEARCH = "ids, cname, dateadded, netvalue";
 185    public static String DEFAULT_PRODUCT_SEARCH = "ids, cnumber, cname, description";
 186    public static String DEFAULT_ACTIVITYLIST_SEARCH = "DISTINCT ids,cname";
 187    public static String DEFAULT_PRODUCTSLIST_SEARCH = "DISTINCT ids,cname,description";
 188    //********** table fields ********************************************************
 189    public static String DETAILS_CONTACTS = IDENTITY_CONTACTS + "." + "IDS," + IDENTITY_CONTACTS + "." + "CNUMBER,"
 190            + IDENTITY_CONTACTS + "." + "TITLE," + IDENTITY_CONTACTS + "." + "PRENAME," + IDENTITY_CONTACTS + "." + "CNAME,"
 191            + IDENTITY_CONTACTS + "." + "STREET," + IDENTITY_CONTACTS + "." + "ZIP," + IDENTITY_CONTACTS + "." + "CITY,"
 192            + IDENTITY_CONTACTS + "." + "MAINPHONE," + IDENTITY_CONTACTS + "." + "FAX," + IDENTITY_CONTACTS + "." + "MOBILEPHONE,"
 193            + IDENTITY_CONTACTS + "." + "WORKPHONE," + IDENTITY_CONTACTS + "." + "COMPANY," + IDENTITY_CONTACTS + "." + "MAILADDRESS,"
 194            + IDENTITY_CONTACTS + "." + "WEBSITE," + IDENTITY_CONTACTS + "." + "NOTES,"
 195            + IDENTITY_CONTACTS + "." + "TAXNUMBER";
 196    public static String DETAILS_PRODUCTS =
 197            IDENTITY_PRODUCTS + "." + "ids,"
 198            + IDENTITY_PRODUCTS + "." + "cnumber,"
 199            + IDENTITY_PRODUCTS + "." + "cname,"
 200            + IDENTITY_PRODUCTS + "." + "ean,"
 201            + IDENTITY_PRODUCTS + "." + "reference,"
 202            + IDENTITY_CONTACTS + "." + "cname,"
 203            + IDENTITY_GROUPS + "." + "cname,"
 204            + IDENTITY_PRODUCTS + "." + "internalnetvalue,"
 205            + IDENTITY_PRODUCTS + "." + "externalnetvalue,"
 206            + IDENTITY_PRODUCTS + "." + "stockvalue";
 207    public static String DETAILS_USERS = IDENTITY_USERS + "." + "IDS," + IDENTITY_USERS + "." + "CNAME,"
 208            + IDENTITY_USERS + "." + "fullname,"
 209            + IDENTITY_USERS + "." + "mail,"
 210            + IDENTITY_USERS + "." + "isenabled,"
 211            + IDENTITY_USERS + "." + "isloggedin";
 212    public static String DETAILS_ITEMS =
 213            IDENTITY_ITEMS + "." + "IDS,"
 214            + IDENTITY_ITEMS + "." + "CNAME,"
 215            + IDENTITY_ITEMS + "." + "dateadded,"
 216            + IDENTITY_ITEMS + "." + "netvalue,"
 217            + IDENTITY_ITEMS + "." + "taxvalue, "
 218            + IDENTITY_ITEMS + "." + "datetodo";
 219    public static String DETAILS_JOURNAL =  Context.IDENTITY_ITEMS   + "." + "IDS," + Context.IDENTITY_CONTACTS + "." + "cname," +    Context.IDENTITY_CONTACTS + "." + "prename,"  + Context.IDENTITY_CONTACTS + "." + "street,"   + Context.IDENTITY_CONTACTS + "." + "city,"     + Context.IDENTITY_CONTACTS + "." + "country,"  + Context.IDENTITY_ITEMS   + "." + "{date},"    + Context.IDENTITY_GROUPS + "." + "CNAME," + Context.IDENTITY_ACCOUNTS + "." + "cname," + Context.IDENTITY_ITEMS   + "." + "CNAME," + Context.IDENTITY_ITEMS   + "." + "inttype," + Context.IDENTITY_ITEMS   + "." + "intstatus," + Context.IDENTITY_ITEMS   + "." + "netvalue,"   + Context.IDENTITY_ITEMS   + "." + "taxvalue, ("      + Context.IDENTITY_ITEMS   + "." + "netvalue +" + Context.IDENTITY_ITEMS   + "." + "taxvalue)";
 220    public static String DETAILS_JOURNAL2 = Context.IDENTITY_REVENUE + "." + "IDS," + Context.IDENTITY_GROUPS +   "." + "reserve2," + Context.IDENTITY_GROUPS + "."   + "reserve2," + Context.IDENTITY_GROUPS + "."   + "reserve2," + Context.IDENTITY_GROUPS   + "." + "reserve2," + Context.IDENTITY_GROUPS   + "." + "reserve2," + Context.IDENTITY_REVENUE + "." + "dateadded," + Context.IDENTITY_GROUPS + "." + "CNAME," + Context.IDENTITY_ACCOUNTS + "." + "cname," + Context.IDENTITY_REVENUE + "." + "CNAME," + Context.IDENTITY_REVENUE + "." + "ids,"     + Context.IDENTITY_REVENUE + "." + "ispaid,"    + Context.IDENTITY_REVENUE + "." + "netvalue, (" + Context.IDENTITY_REVENUE + "." + "brutvalue-" + Context.IDENTITY_REVENUE + "." + "netvalue)," + Context.IDENTITY_REVENUE + "." + "brutvalue";
 221    public static String DETAILS_JOURNAL3 = Context.IDENTITY_EXPENSE + "." + "IDS," + Context.IDENTITY_GROUPS +   "." + "reserve2," + Context.IDENTITY_GROUPS + "."   + "reserve2," + Context.IDENTITY_GROUPS + "."   + "reserve2," + Context.IDENTITY_GROUPS   + "." + "reserve2," + Context.IDENTITY_GROUPS   + "." + "reserve2," + Context.IDENTITY_EXPENSE + "." + "{date},"    + Context.IDENTITY_GROUPS + "." + "CNAME," + Context.IDENTITY_ACCOUNTS + "." + "cname," + Context.IDENTITY_EXPENSE + "." + "CNAME," + Context.IDENTITY_EXPENSE + "." + "ids,"     + Context.IDENTITY_EXPENSE + "." + "ispaid,"    + Context.IDENTITY_EXPENSE + "." + "netvalue, (" + Context.IDENTITY_EXPENSE + "." + "brutvalue-" + Context.IDENTITY_EXPENSE + "." + "netvalue)," + Context.IDENTITY_EXPENSE + "." + "brutvalue";
 222    public static String DETAILS_HISTORY = getHistory().getDbIdentity() + ".ids, " + getHistory().getDbIdentity() + ".cname, " + getHistory().getDbIdentity() + ".username, " + Context.getGroup().getDbIdentity() + ".cname," + Context.getHistory().getDbIdentity() + ".dateadded";
 223    public static String DETAILS_FILES_TO_CONTACTS = Context.getFiles().getDbIdentity() + ".cname," + getFilesToContacts().getDbIdentity() + ".cname, " + Context.getFiles().getDbIdentity() + ".dateadded," + Context.getFilesToContacts().getDbIdentity() + ".description," + Context.getFilesToContacts().getDbIdentity() + ".intsize," + Context.getFilesToContacts().getDbIdentity() + ".mimetype";
 224    public static String DETAILS_FILES_TO_ITEMS = Context.getFiles().getDbIdentity() + ".cname," + getFilesToItems().getDbIdentity() + ".cname, " + Context.getFiles().getDbIdentity() + ".dateadded," + Context.getFilesToItems().getDbIdentity() + ".description," + Context.getFilesToItems().getDbIdentity() + ".intsize," + Context.getFilesToItems().getDbIdentity() + ".mimetype";
 225    public static String DETAILS_FILES_TO_PRODUCTS = Context.getFiles().getDbIdentity() + ".cname," + getFilesToProducts().getDbIdentity() + ".cname, " + Context.getFiles().getDbIdentity() + ".dateadded," + Context.getFilesToProducts().getDbIdentity() + ".description," + Context.getFilesToProducts().getDbIdentity() + ".intsize," + Context.getFilesToProducts().getDbIdentity() + ".mimetype";
 226    public static String DETAILS_MASSPRINT =
 227            Context.IDENTITY_MASSPRINT + "." + "IDS,"
 228            + Context.IDENTITY_MASSPRINT + "." + "cname,"
 229            + Context.IDENTITY_MASSPRINT + "." + "inttype,"
 230            + Context.IDENTITY_MASSPRINT + "." + "dateadded";
 231//    public static String DETAILS_FILES_TO_TEMPLATES = Context.getTemplate().getDbIdentity() + ".ids," + getTemplate().getDbIdentity() + ".cname, " + Context.getTemplate().getDbIdentity() + ".dateadded," + Context.getTemplate().getDbIdentity() + ".intsize," + Context.getTemplate().getDbIdentity() + ".mimetype";
 232//    public static String DETAILS_TEMPLATES = Context.getTemplate().getDbIdentity() + ".ids," + getTemplate().getDbIdentity() + ".cname, " + Context.getTemplate().getDbIdentity() + ".mimetype," + " groups0.cname";
 233    private static final Map<String, String> FOREIGN_KEY_ALIASES = new HashMap<String, String>();
 234
 235    /**
 236     * Find an alias (eg originalproducstids is an alias for productsids in table subitems)
 237     * @param context
 238     * @param exct
 239     * @return 
 240     */
 241    public static String getAliasFor(Context context, Context exct) {
 242        if (FOREIGN_KEY_ALIASES.containsKey(context + "@" + exct)) {
 243            return FOREIGN_KEY_ALIASES.get(context + "@" + exct);
 244        } else {
 245            return context.getDbIdentity();
 246        }
 247    }
 248    //**************************************************************************
 249
 250    /**
 251     * Contexts which can have an export template
 252     * @return
 253     */
 254    public static ArrayList<Context> getTemplateableContexts() {
 255        ArrayList<Context> list = new ArrayList<Context>();
 256        list.add(getItem(null, null));
 257        list.add(getInvoice());
 258        list.add(getOrder());
 259        list.add(getOffer());
 260        list.add(getProduct());
 261        list.add(getReminder());
 262        list.add(getActivityList());
 263        return list;
 264    }
 265
 266    /**
 267     * Contexts which are protected by the Securitymanager
 268     * @return
 269     */
 270    public static ArrayList<Context> getSecuredContexts() {
 271        ArrayList<Context> list = new ArrayList<Context>();
 272        list.add(getContactsCompanies());
 273//        list.add(getUser()); Needs to be non-secure, to update user details on close
 274        list.add(getCustomer());
 275        list.add(getManufacturer());
 276        list.add(getSupplier());
 277        list.add(getAddress());
 278        list.add(getItem(null, null));
 279        list.add(getInvoice());
 280        list.add(getOrder());
 281        list.add(getOffer());
 282        list.add(getSchedule());
 283        list.add(getCountries());
 284        list.add(getContact());
 285        list.add(getProduct());
 286        list.add(getAccounts());
 287        list.add(getCompany());
 288        list.add(getWebShop());
 289        list.add(getTemplate());
 290        list.add(getReminder());
 291        list.add(getSubItem());
 292        list.add(getProductOrderSubitem());
 293        list.add(getProductOrder());
 294        list.add(getProductPrice());
 295
 296        return list;
 297    }
 298
 299    /**
 300     * Contexts which can get cached
 301     * @return
 302     */
 303    public static ArrayList<Context> getCacheableContexts() {
 304        return cacheableContexts;
 305    }
 306    private static ArrayList<Context> cacheableContexts = new ArrayList<Context>(Arrays.asList(new Context[]{
 307                getContact(),
 308                getFavourite(),
 309                getUser(),
 310                getAddress(),
 311                getItem(null, null),
 312                getSubItem(),
 313                getGroup(),
 314                getSchedule(),
 315                getFilesToContacts(),
 316                getFilesToItems(),
 317                getFilesToProducts(),
 318                getProduct(),
 319                getAccounts(),
 320                getMessage(),
 321                getProductListItems(),
 322                getCompany(),
 323                getWebShop(),
 324                getTemplate(),
 325                getReminder(),
 326                getStage(),
 327                getTaxes(),
 328                getProductGroup(),
 329                getProductPrice(),
 330                getActivityListItems(),
 331                getProductOrderSubitem(),
 332                getProductOrder()
 333            }));
 334
 335    /**
 336     * Contexts which are groupable
 337     * @return
 338     */
 339    public static ArrayList<Context> getGroupableContexts() {
 340        ArrayList<Context> list = new ArrayList<Context>();
 341        list.add(getContactsCompanies());
 342        list.add(getCustomer());
 343        list.add(getManufacturer());
 344        list.add(getSupplier());
 345        list.add(getItem(null, null));
 346        list.add(getInvoice());
 347        list.add(getOrder());
 348        list.add(getOffer());
 349        list.add(getSchedule());
 350        list.add(getContact());
 351        list.add(getProduct());
 352        list.add(getAccounts());
 353        list.add(getMessage());
 354        list.add(getProductListItems());
 355        list.add(getSearchIndex());
 356        list.add(getGlobalSettings());
 357        list.add(getCompany());
 358        list.add(getWebShop());
 359        list.add(getTemplate());
 360        list.add(getReminder());
 361        list.add(getStage());
 362        list.add(getExpense());
 363        list.add(getRevenue());
 364        list.add(getTaxes());
 365        list.add(getProductPrice());
 366        list.add(getActivityListItems());
 367        list.add(getProductOrderSubitem());
 368        list.add(getProductOrder());
 369        return list;
 370    }
 371
 372    /**
 373     * Contexts which can be moved to trash rather than delete
 374     * @return
 375     */
 376    public static ArrayList<Context> getTrashableContexts() {
 377        ArrayList<Context> list = new ArrayList<Context>();
 378        list.add(getContactsCompanies());
 379        list.add(getCustomer());
 380        list.add(getManufacturer());
 381        list.add(getSupplier());
 382        list.add(getItem(null, null));
 383        list.add(getInvoice());
 384        list.add(getOrder());
 385        list.add(getOffer());
 386        list.add(getSubItem());
 387        list.add(getContact());
 388        list.add(getProduct());
 389        list.add(getFiles());
 390        list.add(getExpense());
 391        list.add(getRevenue());
 392        list.add(getActivityList());
 393        list.add(getProductOrderSubitem());
 394        list.add(getProductOrder());
 395        return list;
 396    }
 397
 398    /**
 399     * Contexts which are monitored by the History
 400     * @return
 401     */
 402    public static ArrayList<Context> getArchivableContexts() {
 403        return getSecuredContexts();
 404    }
 405
 406    /**
 407     * Contexts which can be used in a user's Search
 408     * @return
 409     */
 410    public static ArrayList<Context> getSearchableContexts() {
 411        ArrayList<Context> list = new ArrayList<Context>();
 412        list.add(getSubItem());
 413        list.add(getItem());
 414        list.add(getSchedule());
 415        list.add(getContact());
 416        list.add(getProduct());
 417        list.add(getProductOrderSubitem());
 418        list.add(getProductOrder());
 419////        list.add(getAccounts());
 420        return list;
 421    }
 422
 423    /**
 424     * LOckable Contexts
 425     * @return
 426     */
 427    public static ArrayList<Context> getLockableContexts() {
 428        ArrayList<Context> list = new ArrayList<Context>();
 429//        list.add(getUser());
 430        list.add(getItem(null, null));
 431        list.add(getSchedule());
 432        list.add(getContact());
 433        list.add(getProduct());
 434        list.add(getContactsCompanies());
 435        list.add(getCustomer());
 436        list.add(getSupplier());
 437        list.add(getManufacturer());
 438        list.add(getWebShop());
 439        return list;
 440    }
 441
 442    /**
 443     * Importable Contexts
 444     * @return
 445     */
 446    public static ArrayList<Context> getImportableContexts() {
 447        ArrayList<Context> list = new ArrayList<Context>();
 448        list.add(getItem(null, null));
 449        list.add(getMessage());
 450        list.add(getContact());
 451        list.add(getProduct());
 452        list.add(getAccounts());
 453        list.add(getGroup());
 454        return list;
 455    }
 456
 457    /**
 458     *
 459     * @return All availbale contexts
 460     */
 461    public static ArrayList<Context> getContexts() {
 462        return allContexts;
 463    }
 464    /**
 465     * A list of all available contexts
 466     */
 467    private static ArrayList<Context> allContexts = new ArrayList<Context>(Arrays.asList(new Context[]{
 468                getContactsCompanies(),
 469                getContact(),
 470                getCustomer(),
 471                getFavourite(),
 472                getFiles(),
 473                getLanguage(),
 474                getLock(),
 475                getManufacturer(),
 476                getSupplier(),
 477                getUser(),
 478                getAddress(),
 479                getItem(null, null),
 480                getInvoice(),
 481                getOrder(),
 482                getOffer(),
 483                getSubItem(),
 484                getGroup(),
 485                getSchedule(),
 486                getFilesToContacts(),
 487                getHistory(),
 488                getCountries(),
 489                getProduct(),
 490                getPlugins(),
 491                getPluginsToUsers(),
 492                getTemplatesToUsers(),
 493                getUserProperties(),
 494                getAccounts(),
 495                getItemsToAccounts(),
 496                getMessage(),
 497                getProductListItems(),
 498                getFormats(),
 499                getTaxes(),
 500                getGlobalSettings(),
 501                getCompany(),
 502                getWebShop(),
 503                getWebShopItemMapping(),
 504                getWebShopContactMapping(),
 505                getTemplate(),
 506                getReminder(),
 507                getStage(),
 508                getProductlist(),
 509                getExpense(),
 510                getRevenue(),
 511                getProductsToSuppliers(),
 512                getValueProperties(),
 513                getProductPrice(),
 514                getMassprint(),
 515                getProductOrderSubitem(),
 516                getProductOrder()
 517            }));
 518//    private String[] searchHeaders;
 519    private volatile ArrayList<String[]> references = new ArrayList<String[]>();
 520    private boolean exclusiveConditionsAvailable = false;
 521    private volatile String exclusiveCondition;
 522    private volatile String uniqueColumns;
 523    private volatile int id = -1;
 524
 525    /**
 526     * Constructor now private
 527     */
 528    private Context() {
 529    }
 530
 531    /**
 532     * Constructs a new Context with a random ID
 533     * @param tablename The name of the table to use
 534     * @param targetObjectClass The class of the {@link DatabaseObject} child to be used with this Context
 535     * @param cacheable If true, the {@link DatabaseObject}s related to this context will be cached
 536     * @param secured If true, the {@link DatabaseObject}s related to this context will be protected by the {@link MPSecurityManager}
 537     * @param importable If true, the {@link DatabaseObject}s related to this context will be importable by the {@link XMLReader}
 538     * @param lockable If true, the {@link DatabaseObject}s related to this context will be loackable to avoid concurrent access
 539     * @param groupable If true, the {@link DatabaseObject}s related to this context will apply to a users group restriction
 540     * @param trashable If true, the {@link DatabaseObject}s related to this context will be moved to the trashbin on delete
 541     * @param archivable If true, actions on the {@link DatabaseObject}s related to this context will be monitored
 542     */
 543    public Context(String tablename, Class targetObjectClass, boolean cacheable,
 544            boolean secured, boolean importable, boolean lockable, boolean groupable,
 545            boolean trashable, boolean archivable) {
 546        setId(Integer.valueOf(RandomText.getNumberText()));
 547        this.setSubID(DEFAULT_SUBID);
 548        this.setDbIdentity(tablename);
 549        this.setIdentityClass(targetObjectClass);
 550        if (archivable) {
 551            getArchivableContexts().add(this);
 552        }
 553        if (cacheable) {
 554            getCacheableContexts().add(this);
 555        }
 556        if (secured) {
 557            getSecuredContexts().add(this);
 558        }
 559        if (importable) {
 560            getImportableContexts().add(this);
 561        }
 562        if (lockable) {
 563            getLockableContexts().add(this);
 564        }
 565        if (groupable) {
 566            getGroupableContexts().add(this);
 567        }
 568        if (trashable) {
 569            getTrashableContexts().add(this);
 570        }
 571        if (archivable) {
 572            getArchivableContexts().add(this);
 573        }
 574    }
 575
 576    /**
 577     * Constructs a new Context with a random ID
 578     * @param tablename The name of the table to use
 579     * @param targetObjectClass The class of the {@link DatabaseObject} child to be used with this Context
 580     */
 581    public Context(String tablename, Class targetObjectClass) {
 582        setId(Integer.valueOf(RandomText.getNumberText()));
 583        this.setSubID(DEFAULT_SUBID);
 584        this.setDbIdentity(tablename);
 585        this.setIdentityClass(targetObjectClass);
 586    }
 587
 588    /**
 589     * Constructs a new Context with a random ID
 590     * @param targetObjectClass The tablename will be assumed from the Class' simple name
 591     */
 592    public Context(Class targetObjectClass) {
 593        this(targetObjectClass.getSimpleName(), targetObjectClass);
 594    }
 595
 596    /**
 597     * Constructs a new Context with a random ID.
 598     * The tablename will be assumed from the targetObject's Class simple name
 599     * @param targetObject
 600     */
 601    public Context(DatabaseObject targetObject) {
 602        setId(Integer.valueOf(RandomText.getNumberText()));
 603        this.setSubID(DEFAULT_SUBID);
 604        this.setDbIdentity(targetObject.getClass().getSimpleName());
 605        this.setIdentityClass(targetObject.getClass());
 606    }
 607    /**
 608     * A default, empty Context without id
 609     */
 610    public static Context DEFAULT = new Context();
 611    private Class identityClass = null;
 612    /*
 613     * The DB Identity name - usually the table
 614     */
 615    private String dbIdentity = null;
 616
 617    /*
 618     * The DB Sub Identity name - maybe a column name
 619     */
 620    private String subID = null;
 621
 622    /*
 623     * The search default result columns
 624     */
 625    private String defResultFields = null;
 626
 627    /**
 628     *
 629     * @return The unique constraints
 630     */
 631    public String getUniqueColumns() {
 632        return uniqueColumns;
 633    }
 634
 635    /**
 636     *
 637     * @param customer
 638     * @param supplier
 639     * @param manufacturer
 640     * @param company
 641     */
 642    public void setContactConditions(boolean customer, boolean supplier, boolean manufacturer, boolean company) {
 643        setCustomer(customer);
 644        setSupplier(supplier);
 645        setManufacturer(manufacturer);
 646        setCompany(company);
 647    }
 648
 649    /**
 650     * Set conditions to get exclusive data (e.g. customer = false results in all data without any customer)
 651     * @param customer
 652     * @param supplier
 653     * @param manufacturer
 654     * @param company
 655     */
 656    public void setExclusiveContactConditions(boolean customer, boolean supplier, boolean manufacturer, boolean company) {
 657
 658        String cond = "    ";
 659        boolean first = true;
 660
 661        if (customer) {
 662            if (first) {
 663                cond += "WHERE ";
 664            }
 665            first = false;
 666            cond += " " + CONDITION_CONTACTS_CUSTOMER + "=1 AND ";
 667        } else {
 668            if (first) {
 669                cond += "WHERE ";
 670            }
 671            first = false;
 672            cond += " " + CONDITION_CONTACTS_CUSTOMER + "=0 AND ";
 673        }
 674        if (supplier) {
 675            if (first) {
 676                cond += "WHERE ";
 677            }
 678            first = false;
 679            cond += " " + CONDITION_CONTACTS_SUPPLIER + "=1 AND ";
 680        } else {
 681            if (first) {
 682                cond += "WHERE ";
 683            }
 684            first = false;
 685            cond += " " + CONDITION_CONTACTS_SUPPLIER + "=0 AND ";
 686        }
 687        if (manufacturer) {
 688            if (first) {
 689                cond += "WHERE ";
 690            }
 691            first = false;
 692            cond += " " + CONDITION_CONTACTS_MANUFACTURER + "=1 AND ";
 693        } else {
 694            if (first) {
 695                cond += "WHERE ";
 696            }
 697            first = false;
 698            cond += " " + CONDITION_CONTACTS_MANUFACTURER + "=0 AND ";
 699        }
 700        if (company) {
 701            if (first) {
 702                cond += "WHERE ";
 703            }
 704            first = false;
 705            cond += " " + CONDITION_CONTACTS_COMPANY + "=1 AND ";
 706        } else {
 707            if (first) {
 708                cond += "WHERE ";
 709            }
 710            first = false;
 711            cond += " " + CONDITION_CONTACTS_COMPANY + "=0 AND ";
 712        }
 713
 714        if (!first) {
 715            cond = cond.substring(4, cond.length() - 4);
 716            if (mpv5.db.objects.User.getCurrentUser().isGroupRestricted() && getGroupableContexts().contains(this)) {
 717                cond += "AND   (" + dbIdentity + "." + "GROUPSIDS = " + mpv5.db.objects.User.getCurrentUser().__getGroupsids() + " OR " + dbIdentity + "." + "GROUPSIDS = 1)";
 718            }
 719        } else {
 720
 721            if (mpv5.db.objects.User.getCurrentUser().isGroupRestricted() && getGroupableContexts().contains(this)) {
 722                cond = "WHERE (" + dbIdentity + "." + "GROUPSIDS = " + mpv5.db.objects.User.getCurrentUser().__getGroupsids() + " OR " + dbIdentity + "." + "GROUPSIDS = 1)";
 723            } else {
 724                cond = "WHERE " + CONDITION_DEFAULT;
 725            }
 726        }
 727
 728        if (getTrashableContexts().contains(this)) {
 729            cond += " AND " + getDbIdentity() + ".invisible = 0 ";
 730        }
 731
 732        exclusiveCondition = cond;
 733    }
 734
 735    /**
 736     *
 737     * @return DB condition string
 738     */
 739    public String getConditions(boolean withInvisible) {
 740        if (!exclusiveConditionsAvailable) {
 741            String cond = "    ";
 742            boolean first = true;
 743            if (isCompany()) {
 744                if (first) {
 745                    cond += "(";
 746                }
 747                first = false;
 748                cond += " " + CONDITION_CONTACTS_COMPANY + "=1 OR ";
 749            }
 750            if (isCustomer()) {
 751                if (first) {
 752                    cond += "(";
 753                }
 754                first = false;
 755                cond += " " + CONDITION_CONTACTS_CUSTOMER + "=1 OR ";
 756            }
 757            if (isManufacturer()) {
 758                if (first) {
 759                    cond += "(";
 760                }
 761                first = false;
 762                cond += " " + CONDITION_CONTACTS_MANUFACTURER + "=1 OR ";
 763            }
 764            if (isSupplier()) {
 765                if (first) {
 766                    cond += "(";
 767                }
 768                first = false;
 769                cond += " " + CONDITION_CONTACTS_SUPPLIER + "=1 OR ";
 770            }
 771            if (itemType != null) {
 772                if (first) {
 773                    cond += "(";
 774
 775                }
 776                first = false;
 777                cond += " " + CONDITION_ITEMS_TYPE + "=" + getItemType() + " OR ";
 778            }
 779            if (itemStatus != null) {
 780                if (first) {
 781                    cond += "(";
 782
 783                }
 784                first = false;
 785                cond += " " + CONDITION_ITEMS_STATUS + "=" + getItemStatus() + " OR ";
 786            }
 787            if (!first) {
 788                cond = "WHERE" + cond.substring(0, cond.length() - 3) + ")";
 789                if (mpv5.db.objects.User.getCurrentUser().isGroupRestricted() && getGroupableContexts().contains(this)) {
 790                    cond += " AND (" + dbIdentity + "." + "GROUPSIDS = " + mpv5.db.objects.User.getCurrentUser().__getGroupsids() + " OR " + dbIdentity + "." + "GROUPSIDS = 1)";
 791                }
 792            } else {
 793                if (mpv5.db.objects.User.getCurrentUser().isGroupRestricted() && getGroupableContexts().contains(this)) {
 794                    cond = "WHERE (" + dbIdentity + "." + "GROUPSIDS = " + mpv5.db.objects.User.getCurrentUser().__getGroupsids() + " OR " + dbIdentity + "." + "GROUPSIDS = 1)";
 795                } else {
 796                    cond = "WHERE " + CONDITION_DEFAULT;
 797                }
 798            }
 799            if (!withInvisible && getTrashableContexts().contains(this)) {
 800                cond += " AND " + dbIdentity + ".invisible = 0 ";
 801            }
 802//            if (!QueryHandler.isMatchingBraces(cond)) {
 803//                cond += ")";
 804//            }
 805            return cond;
 806        } else {
 807            return exclusiveCondition.toString();
 808        }
 809    }
 810
 811    /**
 812     * Generates a SQL String (WHERE clause) which can be used to implement multi-client capability.<br/>
 813     * <br/>
 814     * <b>If the current Context does not support grouping, or the current user is not Group restricted, this will return NULL.</b>
 815     * @return
 816     */
 817    public synchronized String getGroupRestrictionSQLString() {
 818        if (mpv5.db.objects.User.getCurrentUser().isGroupRestricted() && getGroupableContexts().contains(this)) {
 819            return " (" + dbIdentity + "." + "GROUPSIDS = " + mpv5.db.objects.User.getCurrentUser().__getGroupsids() + " OR " + dbIdentity + "." + "GROUPSIDS = 1)";
 820        } else {
 821            return null;
 822        }
 823    }
 824
 825    /**
 826     * Generates a SQL String (WHERE clause) which can be used to avoid having already trashed elements in the resulting data.
 827     *
 828     * @return  " invisible = 0 " or NULL if Context is not trashable
 829     */
 830    public synchronized String getNoTrashSQLString() {
 831        if (getTrashableContexts().contains(this)) {
 832            return " " + dbIdentity + ".invisible = 0 ";
 833        } else {
 834            return null;
 835        }
 836    }
 837
 838    /**
 839     * Generates a SQL String (WHERE clause) which can be used to implement multi-client capability.<br/>
 840     * <br/>
 841     * <b>If the current Context does not support grouping, or the current user is not Group restricted, this will return NULL.</b>
 842     * @param tableName
 843     * @return
 844     */
 845    public synchronized String getGroupRestrictionSQLString(String tableName) {
 846        if (mpv5.db.objects.User.getCurrentUser().isGroupRestricted() && getGroupableContexts().contains(this)) {
 847            return " (" + tableName + "." + "GROUPSIDS = " + mpv5.db.objects.User.getCurrentUser().__getGroupsids() + " OR " + tableName + "." + "GROUPSIDS = 1)";
 848        } else {
 849            return null;
 850        }
 851    }
 852
 853    /**
 854     * Generates a SQL String (WHERE clause) which can be used to avoid having already trashed elements in the resulting data.
 855     *
 856     * @param tableName
 857     * @return  " invisible = 0 " or NULL if Context is not trashable
 858     */
 859    public synchronized String getNoTrashSQLString(String tableName) {
 860        if (getTrashableContexts().contains(this)) {
 861            return " " + tableName + ".invisible = 0 ";
 862        } else {
 863            return null;
 864        }
 865    }
 866
 867    /**
 868     * Add MP specific conditions to a sql query
 869     * @param query
 870     * @return The query
 871     */
 872    public synchronized String prepareSQLString(String query) {
 873
 874        if (getGroupRestrictionSQLString() != null) {
 875            if (query.toUpperCase().contains("WHERE")) {
 876                query = query + " AND" + getGroupRestrictionSQLString();
 877            } else {
 878                query = query + " WHERE" + getGroupRestrictionSQLString();
 879            }
 880        }
 881        if (getNoTrashSQLString() != null) {
 882            if (query.toUpperCase().contains("WHERE")) {
 883                query = query + " AND" + getNoTrashSQLString();
 884            } else {
 885                query = query + " WHERE" + getNoTrashSQLString();
 886            }
 887        }
 888        return query;
 889    }
 890
 891    /**
 892     * Add MP specific conditions to a sql query
 893     * @param query
 894     * @param tableName
 895     * @return The query
 896     */
 897    public synchronized String prepareSQLString(String query, String tableName) {
 898
 899        if (getGroupRestrictionSQLString(tableName) != null) {
 900            if (query.toUpperCase().contains("WHERE")) {
 901                query = query + " AND" + getGroupRestrictionSQLString(tableName);
 902            } else {
 903                query = query + " WHERE" + getGroupRestrictionSQLString(tableName);
 904            }
 905        }
 906        if (getNoTrashSQLString(tableName) != null) {
 907            if (query.toUpperCase().contains("WHERE")) {
 908                query = query + " AND" + getNoTrashSQLString(tableName);
 909            } else {
 910                query = query + " WHERE" + getNoTrashSQLString(tableName);
 911            }
 912        }
 913        return query;
 914    }
 915
 916//    /**
 917//     * Define the owner of this Context
 918//     * @param parentobject
 919//     */
 920//    public void setOwner(DatabaseObject parentobject) {
 921//        this.parent = parentobject;
 922//    }
 923    /**
 924     * Remove all exclusive conditions or reuse them
 925     * @param bool
 926     */
 927    public void useExclusiveConditions(boolean bool) {
 928        exclusiveConditionsAvailable = bool;
 929    }
 930
 931//    /**
 932//     * Add a self-table reference to this context
 933//     * @param referencekey
 934//     * @param referenceidkey
 935//     */
 936//    public void addReference(String referencekey, String referenceidkey) {
 937//        String alias = this.getDbIdentity();
 938//        references.add(new String[]{this.getDbIdentity(), referencekey, referenceidkey, alias});
 939//    }
 940    /**
 941     * Add a foreign table reference to this context
 942     * @param c
 943     */
 944    public void addReference(Context c) {
 945        if (!refs.contains(c)) {
 946            String alias = c.getDbIdentity();
 947            refs.add(c);
 948            references.add(new String[]{alias, "ids", alias + "ids", alias, this.getDbIdentity()});
 949        }
 950    }
 951    List<Context> refs = new ArrayList<Context>();
 952
 953    /**
 954     * Add a foreign table reference to this context<br/><br/>
 955     *  Context c= Context.getFilesToContacts();<br/>
 956     *  c.addReference(Context.getFiles().getDbIdentity(), "cname", "filename");<br/>
 957     * @param referencetable The table which will be joined
 958     * @param referencekey The key column of the joined table
 959     * @param referenceidkey They key column in the original table
 960     */
 961    public void addReference(String referencetable, String referencekey, String referenceidkey) {
 962        synchronized (this) {
 963            String alias = referencetable;
 964            references.add(new String[]{referencetable, referencekey, referenceidkey, alias, getDbIdentity()});
 965        }
 966    }
 967
 968    /**
 969     * @return the dbIdentity
 970     */
 971    public String getDbIdentity() {
 972        return dbIdentity;
 973    }
 974
 975    public String getSearchFields() {
 976        return defResultFields;
 977    }
 978//
 979//    public String[] getSearchHeaders() {
 980//        return searchHeaders;
 981//    }
 982//
 983//    public DatabaseObject getParent() {
 984//        return parent;
 985//    }
 986
 987    /**
 988     * @param dbIdentity the dbIdentity to set
 989     */
 990    public final void setDbIdentity(String dbIdentity) {
 991        this.dbIdentity = dbIdentity;
 992    }
 993
 994    /**
 995     * @return the subID
 996     */
 997    public String getSubID() {
 998        return subID;
 999    }
1000
1001    /**
1002     * @return the IDENTITY CLASS
1003     */
1004    public Class getIdentityClass() {
1005        return identityClass;
1006    }
1007
1008    /**
1009     * @param subID the subID to set
1010     */
1011    public final void setSubID(String subID) {
1012        this.subID = subID;
1013    }
1014
1015    public String getReferences() {
1016        String cond = "";
1017        if (references.size() > 0) {
1018            for (int i = 0; i < references.size(); i++) {
1019                if (references.get(i).length == 4) {
1020                    cond += " LEFT OUTER JOIN " + references.get(i)[0] + " ON " + references.get(i)[3] + "." + references.get(i)[1] + " = " + references.get(i)[3] + "." + references.get(i)[2];
1021                } else if (references.get(i).length == 5) {
1022                    cond += " LEFT OUTER JOIN " + references.get(i)[0] + " ON " + references.get(i)[3] + "." + references.get(i)[1] + " = " + references.get(i)[4] + "." + references.get(i)[2];
1023                }
1024            }
1025        }
1026        return cond;
1027    }
1028
1029    /**
1030     * @return the Company
1031     */
1032    public boolean isCompany() {
1033        return isCompany;
1034    }
1035
1036    /**
1037     * @param Company the Company to set
1038     */
1039    private void setCompany(boolean Company) {
1040        this.isCompany = Company;
1041    }
1042
1043    /**
1044     * @return the Customer
1045     */
1046    private boolean isCustomer() {
1047        return isCustomer;
1048    }
1049
1050    /**
1051     * @param Customer the Customer to set
1052     */
1053    private void setCustomer(boolean Customer) {
1054        this.isCustomer = Customer;
1055    }
1056
1057    /**
1058     * @return the Manufacturer
1059     */
1060    private boolean isManufacturer() {
1061        return isManufacturer;
1062    }
1063
1064    private void setId(int id) {
1065        this.id = id;
1066    }
1067
1068    /**
1069     * @param Manufacturer the Manufacturer to set
1070     */
1071    private void setManufacturer(boolean Manufacturer) {
1072        this.isManufacturer = Manufacturer;
1073    }
1074
1075    /**
1076     * @return the Supplier
1077     */
1078    private boolean isSupplier() {
1079        return isSupplier;
1080    }
1081
1082    /**
1083     * @param Supplier the Supplier to set
1084     */
1085    private void setSupplier(boolean Supplier) {
1086        this.isSupplier = Supplier;
1087    }
1088
1089    /**
1090     * @return the itemStatus
1091     */
1092    public int getItemStatus() {
1093        return itemStatus;
1094    }
1095
1096    /**
1097     * @param itemStatus the itemStatus to set
1098     */
1099    public void setItemStatus(int itemStatus) {
1100        this.itemStatus = itemStatus;
1101    }
1102
1103    /**
1104     * @return the itemType
1105     */
1106    public int getItemType() {
1107        return itemType;
1108    }
1109
1110    /**
1111     * @param itemType the itemType to set
1112     */
1113    public void setItemType(int itemType) {
1114        this.itemType = itemType;
1115    }
1116
1117    /**
1118     * Returns an empty "sample" Object of the context type
1119     * @param <T> 
1120     * @return An empty {@link DatabaseObject}
1121     */
1122    @SuppressWarnings("unchecked")
1123    public <T extends DatabaseObject> T getSampleObject() {
1124        return (T) DatabaseObject.getObject(this);
1125    }
1126////////////////////////////////////////////////////////////////////////////////
1127//generate a new Context Object on each call here, callers may alter them to variants
1128
1129    public static synchronized Context getContactsCompanies() {
1130        Context c = new Context();
1131        c.setCompany(true);
1132        c.setSubID(DEFAULT_SUBID);
1133        c.setDbIdentity(IDENTITY_CONTACTS);
1134        c.setSearchFields(DEFAULT_CONTACT_SEARCH);
1135//        c.setSearchHeaders(Headers.CONTACT_DEFAULT.getValue());
1136        c.setIdentityClass(IDENTITY_CONTACTS_CLASS);
1137        c.setId(0);
1138
1139        return c;
1140    }
1141
1142    /**
1143     * 
1144     * @param type 
1145     * @param status
1146     * @return
1147     */
1148    public static synchronized Context getItem(Integer type, Integer status) {
1149        Context c = new Context();
1150        c.setSubID(DEFAULT_SUBID);
1151        c.setDbIdentity(IDENTITY_ITEMS);
1152        c.setSearchFields(DEFAULT_ITEM_SEARCH);
1153//        c.setSearchHeaders(Headers.ITEM_DEFAULT.getValue());
1154        c.setIdentityClass(IDENTITY_ITEMS_CLASS);
1155        if (status != null) {
1156            c.setItemStatus(status);
1157        }
1158        if (type != null) {
1159            c.setItemType(type);
1160        }
1161        c.setId(1);
1162
1163        return c;
1164    }
1165
1166    public static synchronized Context getItem() {
1167        return getItem(null, null);
1168    }
1169
1170    public static synchronized Context getSubItem() {
1171        Context subitem = new Context();
1172        subitem.setSubID(DEFAULT_SUBID);
1173        subitem.setDbIdentity(IDENTITY_SUBITEMS);
1174        subitem.setIdentityClass(IDENTITY_SUBITEMS_CLASS);
1175        subitem.setId(2);
1176
1177        return subitem;
1178    }
1179
1180    public static synchronized Context getCustomer() {
1181        Context c = new Context();
1182        c.setCustomer(true);
1183        c.setSubID(DEFAULT_SUBID);
1184        c.setDbIdentity(IDENTITY_CONTACTS);
1185        c.setSearchFields(DEFAULT_CONTACT_SEARCH);
1186//        c.setSearchHeaders(Headers.CONTACT_DEFAULT.getValue());
1187        c.setIdentityClass(IDENTITY_CONTACTS_CLASS);
1188        c.setId(3);
1189
1190        return c;
1191    }
1192
1193    public static synchronized Context getManufacturer() {
1194        Context c = new Context();
1195        c.setManufacturer(true);
1196        c.setSubID(DEFAULT_SUBID);
1197        c.setDbIdentity(IDENTITY_CONTACTS);
1198        c.setSearchFields(DEFAULT_CONTACT_SEARCH);
1199//        c.setSearchHeaders(Headers.CONTACT_DEFAULT.getValue());
1200        c.setIdentityClass(IDENTITY_CONTACTS_CLASS);
1201        c.setId(4);
1202
1203        return c;
1204    }
1205
1206    public static synchronized Context getSupplier() {
1207        Context c = new Context();
1208        c.setSupplier(true);
1209        c.setSubID(DEFAULT_SUBID);
1210        c.setDbIdentity(IDENTITY_CONTACTS);
1211        c.setSearchFields(DEFAULT_CONTACT_SEARCH);
1212//        c.setSearchHeaders(Headers.CONTACT_DEFAULT.getValue());
1213        c.setIdentityClass(IDENTITY_CONTACTS_CLASS);
1214        c.setId(5);
1215
1216        return c;
1217    }
1218
1219    public static synchronized Context getContact() {
1220        Context c = new Context();
1221        c.setSubID(DEFAULT_SUBID);
1222        c.setDbIdentity(IDENTITY_CONTACTS);
1223        c.setSearchFields(DEFAULT_CONTACT_SEARCH);
1224//        c.setSearchHeaders(Headers.CONTACT_DEFAULT.getValue());
1225        c.setIdentityClass(IDENTITY_CONTACTS_CLASS);
1226        c.setId(6);
1227
1228        return c;
1229    }
1230
1231    public static synchronized Context getUser() {
1232        Context c = new Context();
1233        c.setSubID(DEFAULT_SUBID);
1234        c.setDbIdentity(IDENTITY_USERS);
1235        c.setSearchFields(DEFAULT_USER_SEARCH);
1236//        c.setSearchHeaders(Headers.USER_DEFAULT.getValue());
1237        c.setIdentityClass(IDENTITY_USERS_CLASS);
1238        c.uniqueColumns = UNIQUECOLUMNS_USER;
1239        c.setId(7);
1240
1241        return c;
1242    }
1243
1244    public static synchronized Context getSchedule() {
1245        Context c = new Context();
1246        c.setSubID(DEFAULT_SUBID);
1247        c.setDbIdentity(IDENTITY_SCHEDULE);
1248        c.setIdentityClass(IDENTITY_SCHEDULE_CLASS);
1249        c.setId(8);
1250
1251        return c;
1252    }
1253
1254    public static synchronized Context getLanguage() {
1255        Context c = new Context();
1256        c.setSubID(DEFAULT_SUBID);
1257        c.setDbIdentity(IDENTITY_LANGUAGES);
1258        c.setId(9);
1259
1260        return c;
1261    }
1262
1263    public static synchronized Context getFiles() {
1264        Context c = new Context();
1265        c.setSubID(DEFAULT_SUBID);
1266        c.setDbIdentity(IDENTITY_FILES);
1267        c.setId(10);
1268
1269        return c;
1270    }
1271
1272    public static synchronized Context getLock() {
1273        Context c = new Context();
1274        c.setSubID(DEFAULT_SUBID);
1275        c.setDbIdentity(IDENTITY_LOCK);
1276        c.setId(11);
1277
1278        return c;
1279    }
1280
1281    public static synchronized Context getGroup() {
1282        Context c = new Context();
1283        c.setSubID(DEFAULT_SUBID);
1284        c.setDbIdentity(IDENTITY_GROUPS);
1285        c.setIdentityClass(IDENTITY_GROUPS_CLASS);
1286        c.uniqueColumns = UNIQUECOLUMNS_GROUPS;
1287        c.setId(14);
1288
1289        return c;
1290    }
1291
1292    public static synchronized Context getFavourite() {
1293        Context c = new Context();
1294        c.setSubID(DEFAULT_SUBID);
1295        c.setDbIdentity(IDENTITY_FAVS);
1296        c.setIdentityClass(IDENTITY_FAVOURITE_CLASS);
1297        c.setId(15);
1298
1299        return c;
1300    }
1301
1302    public static synchronized Context getAddress() {
1303        Context c = new Context();
1304        c.setSubID(DEFAULT_SUBID);
1305        c.setDbIdentity(IDENTITY_ADDRESS);
1306        c.setIdentityClass(IDENTITY_ADDRESS_CLASS);
1307        c.setId(16);
1308
1309        return c;
1310    }
1311
1312    public static synchronized Context getInvoice() {
1313        return getItem(Item.TYPE_BILL, Item.STATUS_IN_PROGRESS);
1314    }
1315
1316    public static synchronized Context getOrder() {
1317        Context c = new Context();
1318        c.setSubID(DEFAULT_SUBID);
1319        c.setDbIdentity(IDENTITY_ITEMS);
1320        c.setSearchFields(DEFAULT_ITEM_SEARCH);
1321//        c.setSearchHeaders(Headers.ITEM_DEFAULT.getValue());
1322        c.setIdentityClass(IDENTITY_ITEMS_CLASS);
1323        c.setItemType(Item.TYPE_ORDER);
1324        c.setId(18);
1325
1326        return c;
1327    }
1328
1329    public static synchronized Context getOffer() {
1330        Context c = new Context();
1331        c.setSubID(DEFAULT_SUBID);
1332        c.setDbIdentity(IDENTITY_ITEMS);
1333        c.setSearchFields(DEFAULT_ITEM_SEARCH);
1334//        c.setSearchHeaders(Headers.ITEM_DEFAULT.getValue());
1335        c.setIdentityClass(IDENTITY_ITEMS_CLASS);
1336        c.setItemType(Item.TYPE_OFFER);
1337        c.setId(19);
1338
1339        return c;
1340    }
1341
1342    public static synchronized Context getFilesToContacts() {
1343        Context c = new Context();
1344        c.setSubID(DEFAULT_SUBID);
1345        c.setDbIdentity(IDENTITY_FILES_TO_CONTACTS);
1346        c.setIdentityClass(IDENTITY_CONTACTS_FILES_CLASS);
1347        c.setId(20);
1348
1349        return c;
1350    }
1351
1352    public static synchronized Context getHistory() {
1353        Context c = new Context();
1354        c.setSubID(DEFAULT_SUBID);
1355        c.setDbIdentity(IDENTITY_HISTORY);
1356        c.setIdentityClass(IDENTITY_HISTORY_ITEMS_CLASS);
1357        c.setId(21);
1358
1359        return c;
1360    }
1361
1362    public static synchronized Context getCountries() {
1363        Context c = new Context();
1364        c.setSubID(DEFAULT_SUBID);
1365        c.setDbIdentity(IDENTITY_COUNTRIES);
1366        c.setId(22);
1367
1368        return c;
1369    }
1370
1371    public static synchronized Context getProduct() {
1372        Context c = new Context();
1373        c.setSubID(DEFAULT_SUBID);
1374        c.setDbIdentity(IDENTITY_PRODUCTS);
1375        c.setIdentityClass(IDENTITY_PRODUCTS_CLASS);
1376        c.setSearchFields(DEFAULT_PRODUCT_SEARCH);
1377        c.setId(23);
1378        FOREIGN_KEY_ALIASES.put(c + "@" + getSubItem(), "originalproducts");
1379
1380        return c;
1381    }
1382
1383    public static synchronized Context getSearchIndex() {
1384        Context c = new Context();
1385        c.setSubID(DEFAULT_SUBID);
1386        c.setDbIdentity(IDENTITY_SEARCHINDEX);
1387        c.setId(24);
1388
1389        return c;
1390    }
1391
1392    public static synchronized Context getPluginsToUsers() {
1393        Context c = new Context();
1394        c.setSubID(DEFAULT_SUBID);
1395        c.setDbIdentity(IDENTITY_PLUGINS_TO_USERS);
1396        c.setIdentityClass(IDENTITY_USER_PLUGINS_CLASS);
1397        c.setId(25);
1398
1399        return c;
1400    }
1401
1402    public static synchronized Context getPlugins() {
1403        Context c = new Context();
1404        c.setSubID(DEFAULT_SUBID);
1405        c.setDbIdentity(IDENTITY_PLUGINS);
1406        c.setIdentityClass(IDENTITY_PLUGINS_CLASS);
1407        c.setId(26);
1408
1409        return c;
1410    }
1411
1412    public static synchronized Context getUserProperties() {
1413        Context c = new Context();
1414        c.setSubID(DEFAULT_SUBID);
1415        c.setDbIdentity(IDENTITY_PROPERTIES_TO_USERS);
1416        c.setIdentityClass(IDENTITY_PROPERTIES_CLASS);
1417        c.setId(27);
1418
1419        return c;
1420    }
1421
1422    public static synchronized Context getAccounts() {
1423        Context c = new Context();
1424        c.setSubID(DEFAULT_SUBID);
1425        c.setDbIdentity(IDENTITY_ACCOUNTS);
1426        c.setIdentityClass(IDENTITY_ACCOUNTS_CLASS);
1427        c.setId(28);
1428        c.uniqueColumns = UNIQUECOLUMNS_DEFAULT;
1429
1430        return c;
1431    }
1432
1433    public static synchronized Context getItemsToAccounts() {
1434        Context c = new Context();
1435        c.setSubID(DEFAULT_SUBID);
1436        c.setDbIdentity(IDENTITY_ITEMS_TO_ACCOUNTS);
1437        c.setId(29);
1438
1439        return c;
1440    }
1441
1442    public static synchronized Context getMessage() {
1443        Context c = new Context();
1444        c.setSubID(DEFAULT_SUBID);
1445        c.setDbIdentity(IDENTITY_MAIL);
1446        c.setIdentityClass(IDENTITY_MAILS_CLASS);
1447        c.setId(30);
1448        c.uniqueColumns = UNIQUECOLUMNS_DEFAULT;
1449
1450        return c;
1451    }
1452
1453    public static synchronized Context getProductListItems() {
1454        Context c = new Context();
1455        c.setSubID(DEFAULT_SUBID);
1456        c.setDbIdentity(IDENTITY_PRODUCTSLISTITEMS);
1457        c.setIdentityClass(IDENTITY_ITEMSLIST_CLASS);
1458        c.setId(32);
1459
1460        return c;
1461    }
1462
1463    public static synchronized Context getFormats() {
1464        Context c = new Context();
1465        c.setSubID(DEFAULT_SUBID);
1466        c.setDbIdentity(IDENTITY_FORMATS_T_USERS);
1467        c.setId(33);
1468
1469        return c;
1470    }
1471
1472    public static synchronized Context getFilesToItems() {
1473        Context c = new Context();
1474        c.setSubID(DEFAULT_SUBID);
1475        c.setDbIdentity(IDENTITY_FILES_TO_ITEMS);
1476        c.setIdentityClass(IDENTITY_ITEM_FILES_CLASS);
1477        c.setId(34);
1478
1479        return c;
1480    }
1481
1482    public static synchronized Context getTaxes() {
1483        Context c = new Context();
1484        c.setSubID(DEFAULT_SUBID);
1485        c.setDbIdentity(IDENTITY_TAX);
1486        c.setIdentityClass(IDENTITY_TAX_CLASS);
1487        c.setId(36);
1488
1489        return c;
1490    }
1491
1492    public static synchronized Context getGlobalSettings() {
1493        Context c = new Context();
1494        c.setSubID(DEFAULT_SUBID);
1495        c.setDbIdentity(IDENTITY_GLOBALSETTINGS);
1496        c.setId(37);
1497
1498        return c;
1499    }
1500
1501    public static synchronized Context getProductGroup() {
1502        Context c = new Context();
1503        c.setSubID(DEFAULT_SUBID);
1504        c.setDbIdentity(IDENTITY_PGROUPS);
1505        c.setIdentityClass(IDENTITY_PGROUPS_CLASS);
1506        c.uniqueColumns = UNIQUECOLUMNS_GROUPS;
1507        c.setId(38);
1508
1509        return c;
1510    }
1511
1512    public static synchronized Context getFilesToProducts() {
1513        Context c = new Context();
1514        c.setSubID(DEFAULT_SUBID);
1515        c.setDbIdentity(IDENTITY_FILES_TO_PRODUCTS);
1516        c.setIdentityClass(IDENTITY_PRODUCTS_FILES_CLASS);
1517        c.setId(20);
1518
1519        return c;
1520    }
1521
1522    public static synchronized Context getCompany() {
1523        Context c = new Context();
1524        c.setSubID(DEFAULT_SUBID);
1525        c.setDbIdentity(IDENTITY_COMPANIES);
1526        c.setIdentityClass(IDENTITY_COMPANY_CLASS);
1527        c.setId(39);
1528
1529        return c;
1530    }
1531
1532    public static synchronized Context getWebShop() {
1533        Context c = new Context();
1534        c.setSubID(DEFAULT_SUBID);
1535        c.setDbIdentity(IDENTITY_WEBSHOPS);
1536        c.setIdentityClass(IDENTITY_WEBSHOP_CLASS);
1537        c.setId(40);
1538
1539        return c;
1540    }
1541
1542    public static synchronized Context getWebShopContactMapping() {
1543        Context c = new Context();
1544        c.setSubID(DEFAULT_SUBID);
1545        c.setDbIdentity(IDENTITY_WSMAPPING);
1546        c.setIdentityClass(IDENTITY_WSCONTACTSMAPPING_CLASS);
1547        c.setId(41);
1548
1549        return c;
1550    }
1551
1552    public static synchronized Context getWebShopItemMapping() {
1553        Context c = new Context();
1554        c.setSubID(DEFAULT_SUBID);
1555        c.setDbIdentity(IDENTITY_WSIMAPPING);
1556        c.setIdentityClass(IDENTITY_WSITEMSMAPPING_CLASS);
1557        c.setId(42);
1558
1559        return c;
1560    }
1561
1562    public static synchronized Context getTemplate() {
1563        Context c = new Context();
1564        c.setSubID(DEFAULT_SUBID);
1565        c.setDbIdentity(IDENTITY_TEMPLATES);
1566        c.setIdentityClass(IDENTITY_TEMPLATE_CLASS);
1567        c.setId(43);
1568
1569        return c;
1570    }
1571
1572    public static synchronized Context getTemplatesToUsers() {
1573        Context c = new Context();
1574        c.setSubID(DEFAULT_SUBID);
1575        c.setDbIdentity(IDENTITY_TEMPLATES_TO_USERS);
1576        c.setId(44);
1577
1578        return c;
1579    }
1580
1581    public static synchronized Context getReminder() {
1582        Context c = new Context();
1583        c.setSubID(DEFAULT_SUBID);
1584        c.setDbIdentity(IDENTITY_REMINDERS);
1585        c.setIdentityClass(IDENTITY_REMINDER_CLASS);
1586        c.setId(45);
1587
1588        return c;
1589    }
1590
1591    public static synchronized Context getStage() {
1592        Context c = new Context();
1593        c.setSubID(DEFAULT_SUBID);
1594        c.setDbIdentity(IDENTITY_STAGES);
1595        c.setIdentityClass(IDENTITY_STAGE_CLASS);
1596        c.setId(46);
1597
1598        return c;
1599    }
1600
1601    public static synchronized Context getRevenue() {
1602        Context c = new Context();
1603        c.setSubID(DEFAULT_SUBID);
1604        c.setDbIdentity(IDENTITY_REVENUE);
1605        c.setIdentityClass(Revenue.class);
1606        c.setId(47);
1607
1608        return c;
1609    }
1610
1611    public static synchronized Context getExpense() {
1612        Context c = new Context();
1613        c.setSubID(DEFAULT_SUBID);
1614        c.setDbIdentity(IDENTITY_EXPENSE);
1615        c.setIdentityClass(IDENTITY_EXPENSE_CLASS);
1616        c.setId(48);
1617
1618        return c;
1619    }
1620
1621    public static synchronized Context getProductlist() {
1622        Context c = new Context();
1623        c.setSubID(DEFAULT_SUBID);
1624        c.setDbIdentity(IDENTITY_PRODUCTSLIST);
1625        c.setSearchFields(DEFAULT_PRODUCTSLIST_SEARCH);
1626        c.setIdentityClass(IDENTITY_PRODUCTLIST_CLASS);
1627        c.setId(48);
1628
1629        return c;
1630    }
1631
1632    public static synchronized Context getProductsToSuppliers() {
1633        Context c = new Context();
1634        c.setSubID(DEFAULT_SUBID);
1635        c.setDbIdentity(IDENTITY_PRODUCTS_TO_SUPPLIERS);
1636        c.setIdentityClass(IDENTITY_PRODUCTSTOSUPPLIERS_CLASS);
1637        c.setId(49);
1638
1639        return c;
1640    }
1641
1642    public static synchronized Context getValueProperties() {
1643        Context c = new Context();
1644        c.setSubID(DEFAULT_SUBID);
1645        c.setDbIdentity(IDENTITY_VALUE_PROPERTIES);
1646        c.setIdentityClass(IDENTITY_VALUEPROPERTY_CLASS);
1647        c.setId(50);
1648
1649        return c;
1650    }
1651
1652    public static synchronized Context getScheduleTypes() {
1653        Context c = new Context();
1654        c.setSubID(DEFAULT_SUBID);
1655        c.setDbIdentity(IDENTITY_SCHEDULE_TYPES);
1656        c.setIdentityClass(IDENTITY_SCHEDULETYPES_CLASS);
1657        c.setId(51);
1658
1659        return c;
1660    }
1661
1662    public static synchronized Context getConversation() {
1663        Context c = new Context();
1664        c.setSubID(DEFAULT_SUBID);
1665        c.setDbIdentity(IDENTITY_CONVERSATION);
1666        c.setIdentityClass(IDENTITY_CONVERSATION_CLASS);
1667        c.setSearchFields(DEFAULT_SUBID);
1668        c.setId(52);
1669
1670        return c;
1671    }
1672
1673    public static synchronized Context getProductPrice() {
1674        Context c = new Context();
1675        c.setSubID(DEFAULT_SUBID);
1676        c.setDbIdentity(IDENTITY_PRODUCT_PRICES);
1677        c.setIdentityClass(IDENTITY_PRODUCTPRICE_CLASS);
1678        c.setId(53);
1679
1680        return c;
1681    }
1682
1683    public static synchronized Context getActivityList() {
1684        Context c = new Context();
1685        c.setSubID(DEFAULT_SUBID);
1686        c.setDbIdentity(IDENTITY_ACTIVITYLIST);
1687        c.setSearchFields(DEFAULT_ACTIVITYLIST_SEARCH);
1688        c.setIdentityClass(IDENTITY_ACTIVITYLIST_CLASS);
1689        c.setId(54);
1690
1691        return c;
1692    }
1693
1694    public static synchronized Context getActivityListItems() {
1695        Context c = new Context();
1696        c.setSubID(DEFAULT_SUBID);
1697        c.setDbIdentity(IDENTITY_ACTIVITYLISTITEMS);
1698        c.setIdentityClass(IDENTITY_ACTIVITYITEMSLIST_CLASS);
1699        c.setId(55);
1700
1701        return c;
1702    }
1703
1704    public static Context getMassprint() {
1705        Context c = new Context();
1706        c.setSubID(DEFAULT_SUBID);
1707        c.setDbIdentity(IDENTITY_MASSPRINT);
1708        c.setIdentityClass(IDENTITY_MASSPRINT_CLASS);
1709        c.setId(56);
1710
1711        return c;
1712    }
1713    
1714    public static Context getProductOrder() {
1715        Context c = new Context();
1716        c.setSubID(DEFAULT_SUBID);
1717        c.setDbIdentity(IDENTITY_PRODUCTORDER);
1718        c.setIdentityClass(IDENTITY_PRODUCTORDER_CLASS);
1719        c.setId(57);
1720
1721        return c;
1722    }
1723     
1724    public static Context getProductOrderSubitem() {
1725        Context c = new Context();
1726        c.setSubID(DEFAULT_SUBID);
1727        c.setDbIdentity(IDENTITY_PRODUCTORDERSUBITEM);
1728        c.setIdentityClass(IDENTITY_PRODUCTORDERSUBITEM_CLASS);
1729        c.setId(58);
1730
1731        return c;
1732    }
1733
1734    /////////////////////////////////////////////////////////////////////////////////////////////////
1735    /**
1736     *
1737     * @param contextdbidentity
1738     * @return The matching context or null if not existing
1739     */
1740    public static synchronized Context getMatchingContext(String contextdbidentity) {
1741        if (contextdbidentity.toLowerCase().endsWith("ids")) {
1742            contextdbidentity = contextdbidentity.replace("ids", "");
1743        }
1744        for (int i = 0; i < allContexts.size(); i++) {
1745            Context context = allContexts.get(i);
1746            if (context.getDbIdentity().equalsIgnoreCase(contextdbidentity)) {
1747                return context;
1748            }
1749        }
1750        Log.Debug(Context.class, "Context not found for name: " + contextdbidentity);
1751        return null;
1752    }
1753
1754    /**
1755     *
1756     * @param contextId
1757     * @return The matching context or null if not existing
1758     */
1759    public static synchronized Context getMatchingContext(int contextId) {
1760        for (int i = 0; i < allContexts.size(); i++) {
1761            Context context = allContexts.get(i);
1762            if (context.getId() == contextId) {
1763                return context;
1764            }
1765        }
1766        Log.Debug(Context.class, "Context not found for id: " + contextId);
1767        return null;
1768    }
1769
1770    public void setSearchFields(String fields) {
1771        defResultFields = fields;
1772    }
1773
1774    /**
1775     * @param identityClass the identityClass to set
1776     */
1777    public final void setIdentityClass(Class identityClass) {
1778        this.identityClass = identityClass;
1779    }
1780
1781    @Override
1782    public String toString() {
1783        if (true/*getIdentityClass() == null*/) {
1784            return String.valueOf(dbIdentity).toUpperCase() + " [" + id + "]";
1785        } else {
1786            return "<html>"+String.valueOf(dbIdentity).toUpperCase() + " [" + id + "] (<b>"