KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > registries > JahiaFieldDefinitionsRegistry


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12

13 //
14
// EV 25.11.2000
15
//
16

17 package org.jahia.registries;
18
19 import java.util.Enumeration JavaDoc;
20 import java.util.Vector JavaDoc;
21
22 import org.apache.log4j.Logger;
23 import org.jahia.data.fields.JahiaFieldDefinition;
24 import org.jahia.exceptions.JahiaException;
25 import org.jahia.services.cache.Cache;
26 import org.jahia.services.cache.CacheFactory;
27 import org.jahia.services.cache.CacheListener;
28
29 public class JahiaFieldDefinitionsRegistry implements CacheListener {
30
31     private static Logger logger = Logger.getLogger(JahiaFieldDefinitionsRegistry.class);
32
33     private static JahiaFieldDefinitionsRegistry instance = null;
34
35     private static final String JavaDoc KEY_SEPARATOR = "###";
36
37     public static final String JavaDoc FIELD_DEFINITION_BY_ID_CACHE = "FieldDefinitionsByID";
38     public static final String JavaDoc FIELD_DEFINITION_BY_SITE_AND_NAME_CACHE =
39             "FieldDefinitionsBySiteAndName";
40     private Cache fieldDefTable;
41     private Cache fieldDefSiteAndNameTable;
42
43     private JahiaFieldDefinitionsRegistry () {
44         logger.debug("Starting FieldDefinitions Registry");
45         try {
46             fieldDefTable = CacheFactory.createCache (FIELD_DEFINITION_BY_ID_CACHE);
47             fieldDefTable.registerListener(this);
48
49             fieldDefSiteAndNameTable = CacheFactory.createCache (FIELD_DEFINITION_BY_SITE_AND_NAME_CACHE);
50             fieldDefSiteAndNameTable.registerListener(this);
51
52         } catch (JahiaException je) {
53             logger.error(
54                 "Error while creating caches for JahiaFieldDefinition registry.", je);
55         }
56     }
57
58     public static synchronized JahiaFieldDefinitionsRegistry getInstance () {
59         if (instance == null) {
60             instance = new JahiaFieldDefinitionsRegistry();
61         }
62         return instance;
63     }
64
65     public void init () throws JahiaException {
66         loadAllDefinitions();
67     }
68
69     private void loadAllDefinitions ()
70         throws JahiaException {
71         Vector JavaDoc ids = ServicesRegistry.getInstance().getJahiaFieldService().
72                      getAllFieldDefinitionIDs();
73         Enumeration JavaDoc fieldDefIDEnum = ids.elements();
74         while (fieldDefIDEnum.hasMoreElements()) {
75             Integer JavaDoc currentID = ( (Integer JavaDoc) fieldDefIDEnum.nextElement());
76             JahiaFieldDefinition curDefinition = ServicesRegistry.getInstance().
77                                                  getJahiaFieldService().
78                                                  loadFieldDefinition(currentID.
79                 intValue());
80             addToCache(curDefinition);
81         }
82     }
83
84     private JahiaFieldDefinition loadDefinitionByID (int defID)
85         throws JahiaException {
86         JahiaFieldDefinition curDefinition = ServicesRegistry.getInstance().
87                                              getJahiaFieldService().
88                                              loadFieldDefinition(defID);
89         if (curDefinition != null) {
90             addToCache(curDefinition);
91         }
92         return curDefinition;
93     }
94     
95     private JahiaFieldDefinition loadDefinitionBySiteIDAndName (int siteID, String JavaDoc definitionName)
96         throws JahiaException {
97         JahiaFieldDefinition curDefinition = ServicesRegistry.getInstance().
98                                              getJahiaFieldService().
99                                              loadFieldDefinition(siteID, definitionName);
100         if (curDefinition != null) {
101             addToCache(curDefinition);
102         }
103         return curDefinition;
104     }
105
106     private synchronized void addToCache(JahiaFieldDefinition curDefinition) {
107         fieldDefTable.put(new Integer JavaDoc(curDefinition.getID()), curDefinition);
108         fieldDefSiteAndNameTable.put(buildCacheKey(curDefinition.getName(),
109             curDefinition.getJahiaID()), curDefinition);
110     }
111     
112     public JahiaFieldDefinition getDefinition (int defID)
113         throws JahiaException {
114         JahiaFieldDefinition result = (JahiaFieldDefinition) fieldDefTable.get(new
115             Integer JavaDoc(defID));
116         if (result == null) {
117             result = loadDefinitionByID(defID);
118         }
119
120         if (result != null) {
121             return result;
122         }
123         String JavaDoc errorMsg = "Could not find the definition ID " + defID;
124         logger.error(errorMsg + " -> BAILING OUT");
125         throw new JahiaException("Database synchronisation error",
126                                  errorMsg, JahiaException.REGISTRY_ERROR,
127                                  JahiaException.CRITICAL_SEVERITY);
128     } // end getDefinition
129

130
131     // 11.07.2001 update to multi site by adding siteID
132
public JahiaFieldDefinition getDefinition (int siteID, String JavaDoc fieldName)
133         throws JahiaException {
134         JahiaFieldDefinition result = (JahiaFieldDefinition)
135                                       fieldDefSiteAndNameTable.get(
136             buildCacheKey(fieldName, siteID));
137         if (result == null) {
138             result = loadDefinitionBySiteIDAndName(siteID, fieldName);
139         }
140         if (result == null) {
141             logger.debug("Couldn't find field definition for siteID=" + siteID + " and name=" + fieldName);
142         }
143         return result;
144     } // end getDefinition
145

146     public void setDefinition (JahiaFieldDefinition theFieldDef)
147         throws JahiaException {
148         
149         JahiaFieldDefinition aFieldDef = getDefinition(theFieldDef.getJahiaID(),
150             theFieldDef.getName());
151         if (aFieldDef != null) {
152             // field definition already exists, just have to update
153
// We ensure to perform an update
154
theFieldDef.setID(aFieldDef.getID());
155         } else {
156             // field definition doesn't exist, need to add to registry
157
//We ensure to create a new one
158
theFieldDef.setID(0);
159         }
160         ServicesRegistry.getInstance().getJahiaFieldService().
161             saveFieldDefinition(theFieldDef);
162         addToCache(theFieldDef);
163     } // end setDefinition
164

165     public void removeFieldDefinition (int fieldDefID)
166         throws JahiaException {
167         JahiaFieldDefinition fieldDef = getDefinition(fieldDefID);
168         if (fieldDef != null) {
169             synchronized (this){
170             fieldDefTable.remove(new Integer JavaDoc(fieldDefID));
171             fieldDefSiteAndNameTable.remove(buildCacheKey(fieldDef.getName(),
172                 fieldDef.getJahiaID()));
173             }
174         }
175
176     } // end setDefinition
177

178     private String JavaDoc buildCacheKey (String JavaDoc fieldDefinitionName, int siteID) {
179         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
180         result.append(fieldDefinitionName);
181         result.append(KEY_SEPARATOR);
182         result.append(siteID);
183         return result.toString();
184     }
185
186     /**
187      * This method is called each time the cache flushes its items.
188      *
189      * @param cacheName the name of the cache which flushed its items.
190      */

191     public void onCacheFlush(String JavaDoc cacheName) {
192         if (FIELD_DEFINITION_BY_ID_CACHE.equals(cacheName)) {
193             fieldDefSiteAndNameTable.flush (false);
194
195         } else if (FIELD_DEFINITION_BY_SITE_AND_NAME_CACHE.equals(cacheName)) {
196              fieldDefTable.flush (false);
197         }
198
199         try {
200             loadAllDefinitions();
201
202         } catch (JahiaException e) {
203             logger.warn("Could not reload the Field Definitions.", e);
204         }
205     }
206
207     public void onCachePut(String JavaDoc cacheName, Object JavaDoc entryKey) {
208         // do nothing;
209
}
210
211 }
212
Popular Tags