KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > ipanema > person > PersonStructCache


1 /*
2  * Created on Dec 1, 2004
3  * by alex
4  *
5  */

6 package com.nightlabs.ipanema.person;
7
8 import java.io.File JavaDoc;
9 import java.io.FileInputStream JavaDoc;
10 import java.io.FileOutputStream JavaDoc;
11 import java.io.ObjectInputStream JavaDoc;
12 import java.io.ObjectOutputStream JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
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 /**
27  * @author Alexander Bieber <alex[AT]nightlabs[DOT]de>
28  */

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 JavaDoc e) {
45                 LOGGER.error("Error getting PersonManagerHome.",e);
46             }
47             try {
48               personManager = home.create();
49             } catch (Exception JavaDoc e) {
50                 LOGGER.error("Error creating PersonManager.",e);
51             }
52             } catch (Throwable JavaDoc e) {
53                 throw new RuntimeException JavaDoc(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     /**
67      * Returns and caches a {@link PersonStruct} per session;
68      * @return
69      */

70     public PersonStruct getCachedPersonStructure() {
71         if (personStructure == null) {
72             File JavaDoc cache = new File JavaDoc(getCacheDir()+File.separatorChar+"PersonStruct.res");
73             
74             if (cache.exists()) {
75                 // have locally stored version
76
// TODO: Check validity of PersonStructCache
77
try {
78                     ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(new FileInputStream JavaDoc(cache));
79                     personStructure = (PersonStruct)ois.readObject();
80                 } catch (Throwable JavaDoc t) {
81                     // something, whatever went wrong ..
82
LOGGER.error("Error reading person structure cache.",t);
83                     personStructure = null;
84                 }
85             }
86             
87             if (personStructure == null) {
88                 // not found locally
89
try {
90                     personStructure = getPersonManager().getFullPersonStructure();
91                 } catch (Throwable JavaDoc t) {
92                     LOGGER.error("Error fetching person structure",t);
93                     personStructure = null;
94                 }
95                 
96                 if (personStructure != null) {
97                     // store person structure for later use
98
try {
99 // cache.mkdirs();
100
File JavaDoc _cacheDir = new File JavaDoc(getCacheDir());
101                         _cacheDir.mkdirs();
102                         cache.createNewFile();
103                         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(new FileOutputStream JavaDoc(cache));
104                         oos.writeObject(personStructure);
105                     } catch (Throwable JavaDoc t) {
106                         // something, whatever went wrong ..
107
LOGGER.error("Error writing person structure cache.",t);
108                         cache.delete();
109                     }
110                 }
111             }
112         }
113         if (personStructure == null)
114             throw new IllegalStateException JavaDoc("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     /**
129      * @see com.nightlabs.ipanema.person.PersonStructFactory#getPersonStruct()
130      */

131     public PersonStruct getPersonStruct() {
132         return getCachedPersonStructure();
133     }
134     
135     public static PersonStruct getPersonStructure() {
136         return getSharedInstance().getPersonStruct();
137     }
138     
139     private String JavaDoc cacheDir;
140     
141     public String JavaDoc getCacheDir() {
142         return IpanemaApplication.getRootDir()+File.separatorChar+"personstruct.cache";
143     }
144     
145     
146     private static List JavaDoc orderedPersonStructBlocks;
147     /**
148      * Returns a ordered list of the StructBlocks for the
149      * current person structure.
150      * If refresh is true the list will be resorted according to
151      * the current order in {@link PersonStructOrderConfigModule}.
152      *
153      * @param refresh
154      * @return
155      */

156     public static List JavaDoc getOrderedPersonStructBlocks(boolean refresh) {
157         if ((orderedPersonStructBlocks != null) && (!refresh))
158             return orderedPersonStructBlocks;
159         
160         orderedPersonStructBlocks = new ArrayList JavaDoc();
161         int allStructBlockCount = PersonStructCache.getPersonStructure().getPersonStructBlocks().size();
162         int unmentionedCount = 0;
163         
164         Map JavaDoc structBlockOrder = PersonStructOrderConfigModule.getSharedInstance().structBlockDisplayOrder();
165         
166         for (Iterator JavaDoc it = PersonStructCache.getPersonStructure().getPersonStructBlocks().iterator(); it.hasNext(); ) {
167             // all blocks
168
PersonStructBlock structBlock = (PersonStructBlock)it.next();
169             
170             if (structBlockOrder.containsKey(structBlock.getPrimaryKey())) {
171                 // block mentioned in structBlockOrder
172
Integer JavaDoc index = (Integer JavaDoc)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 JavaDoc getOrderedPersonStructFields(PersonStructBlockID structBlockID) {
187         // TODO: Implement getOrderedPersonStructFields
188
return null;
189     }
190     
191 }
192
Popular Tags