1 6 package com.nightlabs.ipanema.person; 7 8 import java.io.File ; 9 import java.io.FileInputStream ; 10 import java.io.FileOutputStream ; 11 import java.io.ObjectInputStream ; 12 import java.io.ObjectOutputStream ; 13 import java.util.ArrayList ; 14 import java.util.Collections ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Map ; 18 19 import org.apache.log4j.Logger; 20 21 import com.nightlabs.ipanema.base.app.IpanemaApplication; 22 import com.nightlabs.ipanema.base.login.Login; 23 import com.nightlabs.ipanema.person.id.PersonStructBlockID; 24 import com.nightlabs.ipanema.person.preferences.PersonStructOrderConfigModule; 25 26 29 public class PersonStructCache { 30 private static final Logger LOGGER = Logger.getLogger(PersonStructCache.class); 31 32 public PersonStructCache() { 33 } 34 35 private PersonManager personManager; 36 37 public PersonManager getPersonManager() { 38 if (personManager == null) { 39 try { 40 Login login = Login.getLogin(); 41 PersonManagerHome home = null; 42 try { 43 home = PersonManagerUtil.getHome(login.getInitialContextProperties()); 44 } catch (Exception e) { 45 LOGGER.error("Error getting PersonManagerHome.",e); 46 } 47 try { 48 personManager = home.create(); 49 } catch (Exception e) { 50 LOGGER.error("Error creating PersonManager.",e); 51 } 52 } catch (Throwable e) { 53 throw new RuntimeException (e); 54 } 55 } 56 return personManager; 57 } 58 59 60 public void setPersonManager(PersonManager personManager) { 61 this.personManager = personManager; 62 } 63 64 private PersonStruct personStructure; 65 66 70 public PersonStruct getCachedPersonStructure() { 71 if (personStructure == null) { 72 File cache = new File (getCacheDir()+File.separatorChar+"PersonStruct.res"); 73 74 if (cache.exists()) { 75 try { 78 ObjectInputStream ois = new ObjectInputStream (new FileInputStream (cache)); 79 personStructure = (PersonStruct)ois.readObject(); 80 } catch (Throwable t) { 81 LOGGER.error("Error reading person structure cache.",t); 83 personStructure = null; 84 } 85 } 86 87 if (personStructure == null) { 88 try { 90 personStructure = getPersonManager().getFullPersonStructure(); 91 } catch (Throwable t) { 92 LOGGER.error("Error fetching person structure",t); 93 personStructure = null; 94 } 95 96 if (personStructure != null) { 97 try { 99 File _cacheDir = new File (getCacheDir()); 101 _cacheDir.mkdirs(); 102 cache.createNewFile(); 103 ObjectOutputStream oos = new ObjectOutputStream (new FileOutputStream (cache)); 104 oos.writeObject(personStructure); 105 } catch (Throwable t) { 106 LOGGER.error("Error writing person structure cache.",t); 108 cache.delete(); 109 } 110 } 111 } 112 } 113 if (personStructure == null) 114 throw new IllegalStateException ("personStructure should not be null at this point!"); 115 return personStructure; 116 } 117 118 private static PersonStructCache sharedInstance; 119 120 protected static PersonStructCache getSharedInstance() { 121 if (sharedInstance == null) 122 sharedInstance = new PersonStructCache(); 123 return sharedInstance; 124 } 125 126 private static boolean initialized = false; 127 128 131 public PersonStruct getPersonStruct() { 132 return getCachedPersonStructure(); 133 } 134 135 public static PersonStruct getPersonStructure() { 136 return getSharedInstance().getPersonStruct(); 137 } 138 139 private String cacheDir; 140 141 public String getCacheDir() { 142 return IpanemaApplication.getRootDir()+File.separatorChar+"personstruct.cache"; 143 } 144 145 146 private static List orderedPersonStructBlocks; 147 156 public static List getOrderedPersonStructBlocks(boolean refresh) { 157 if ((orderedPersonStructBlocks != null) && (!refresh)) 158 return orderedPersonStructBlocks; 159 160 orderedPersonStructBlocks = new ArrayList (); 161 int allStructBlockCount = PersonStructCache.getPersonStructure().getPersonStructBlocks().size(); 162 int unmentionedCount = 0; 163 164 Map structBlockOrder = PersonStructOrderConfigModule.getSharedInstance().structBlockDisplayOrder(); 165 166 for (Iterator it = PersonStructCache.getPersonStructure().getPersonStructBlocks().iterator(); it.hasNext(); ) { 167 PersonStructBlock structBlock = (PersonStructBlock)it.next(); 169 170 if (structBlockOrder.containsKey(structBlock.getPrimaryKey())) { 171 Integer index = (Integer )structBlockOrder.get(structBlock.getPrimaryKey()); 173 structBlock.setPriority(index.intValue()); 174 } 175 else { 176 structBlock.setPriority(allStructBlockCount + (unmentionedCount++)); 177 } 178 179 orderedPersonStructBlocks.add(structBlock); 180 } 181 Collections.sort(orderedPersonStructBlocks); 182 183 return orderedPersonStructBlocks; 184 } 185 186 public List getOrderedPersonStructFields(PersonStructBlockID structBlockID) { 187 return null; 189 } 190 191 } 192
| Popular Tags
|