001/* 002 $Id: SystemItems.java 3207 2009-04-09 06:48:11Z gregory $ 003 004 Copyright (C) 2006 Gregory Vincic, Olle Mansson 005 Copyright (C) 2007 Gregory Vincic 006 007 This file is part of Proteios. 008 Available at http://www.proteios.org/ 009 010 Proteios is free software; you can redistribute it and/or modify it 011 under the terms of the GNU General Public License as published by 012 the Free Software Foundation; either version 2 of the License, or 013 (at your option) any later version. 014 015 Proteios is distributed in the hope that it will be useful, but 016 WITHOUT ANY WARRANTY; without even the implied warranty of 017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 018 General Public License for more details. 019 020 You should have received a copy of the GNU General Public License 021 along with this program; if not, write to the Free Software 022 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 023 02111-1307, USA. 024*/ 025package org.proteios.core; 026 027import org.proteios.core.data.RoleKeyData; 028import org.proteios.core.data.SystemData; 029import java.util.HashMap; 030import java.util.List; 031import java.util.Map; 032 033/** 034 This class is used to map the system id of all {@link 035 org.proteios.core.data.SystemData} items to the numerical internal 036 id in the current installation. This information is initialised 037 at startup and is then kept in memory. 038 039 @author Nicklas 040 @version 2.0 041 @base.modified $Date: 2009-04-09 08:48:11 +0200 (Thu, 09 Apr 2009) $ 042*/ 043public class SystemItems 044{ 045 046 private static boolean isInitialised = false; 047 048 /** 049 A map from system id --> numeric id 050 */ 051 private static Map<String, Integer> systemIds = null; 052 053 /** 054 An array to hold mappings for {@link org.proteios.core.data.RoleKeyData}: 055 RoleKeyData.itemType --> RoleKeyData.id 056 */ 057 private static int[] roleKeys = null; 058 059 /** 060 Initialize the class and load all mappings for item.systemId --> item.id 061 and for RoleKeyData.itemType --> RoleKeyData.id 062 @throws BaseException If there is an error 063 */ 064 static synchronized void init() 065 throws BaseException 066 { 067 if (isInitialised) return; 068 org.hibernate.Session session = null; 069 org.hibernate.Transaction tx = null; 070 071 try 072 { 073 systemIds = new HashMap<String,Integer>(); 074 roleKeys = new int[Item.MAX_VALUE+1]; 075 session = HibernateUtil.newSession(); 076 tx = HibernateUtil.newTransaction(session); 077 List result = HibernateUtil.getPredefinedQuery(session, "LOAD_SYSTEM_ITEMS").list(); 078 /* 079 SELECT item.id, item.systemId 080 FROM org.proteios.core.data.SystemData item 081 WHERE item.systemId NOT IS NULL 082 */ 083 for (int i = 0; i < result.size(); i++) 084 { 085 Object[] item = (Object[])result.get(i); 086 String systemId = (String)item[1]; 087 Integer id = (Integer)item[0]; 088 systemIds.put(systemId.toLowerCase(), id); 089 } 090 091 // Load RoleKeys 092 result = HibernateUtil.getPredefinedQuery(session, "LOAD_ROLE_KEY_IDS").list(); 093 /* 094 SELECT rk.id, rk.itemType 095 FROM RoleKeyData rk 096 */ 097 for (int i = 0; i < result.size(); i++) 098 { 099 Object[] item = (Object[])result.get(i); 100 roleKeys[(Integer)item[1]] = (Integer)item[0]; 101 } 102 isInitialised = true; 103 } 104 finally 105 { 106 if (tx != null) HibernateUtil.commit(tx); 107 if (session != null) HibernateUtil.close(session); 108 } 109 } 110 111 /** 112 Unload all settings. 113 */ 114 static synchronized void unload() 115 { 116 isInitialised = false; 117 if (systemIds != null) systemIds.clear(); 118 systemIds = null; 119 roleKeys = null; 120 } 121 122 /** 123 Register a system item. Used by the {@link Install} class while 124 creating new items. 125 */ 126 static void add(SystemData item) 127 { 128 assert isInitialised : "SystemItems has not been initialised"; 129 systemIds.put(item.getSystemId().toLowerCase(), item.getId()); 130 } 131 132 /** 133 Get the numeric id for an item when you know the system id. 134 @param systemId The system id string 135 @return The numeric id of the item, or 0 if it could not be found 136 */ 137 public static int getId(String systemId) 138 { 139 assert isInitialised : "SystemItems has not been initialised"; 140 if (systemId == null) return 0; 141 Integer id = systemIds.get(systemId.toLowerCase()); 142 return id == null ? 0 : id.intValue(); 143 } 144 145 /** 146 Register a role key. Used by the {@link Install} class while 147 creating new items. 148 */ 149 static void addRoleKey(RoleKeyData roleKey) 150 { 151 assert isInitialised : "SystemItems has not been initialised"; 152 Item itemType = Item.fromValue(roleKey.getItemType()); 153 roleKeys[itemType.getValue()] = roleKey.getId(); 154 } 155 156 /** 157 Get the numeric id for a {@link RoleKey} when you know the {@link Item} 158 type of the RoleKey. 159 @param itemType The item type code as defined by the {@link Item} class 160 @return The numeric id for the RoleKey 161 */ 162 public static int getRoleKeyId(Item itemType) 163 { 164 assert isInitialised : "SystemItems has not been initialised"; 165 assert itemType.getValue() < roleKeys.length : "Bad item type: "+itemType+"; value="+itemType.getValue(); 166 return roleKeys[itemType.getValue()]; 167 } 168 169}