KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > cache > FieldsCache


1 package com.dotmarketing.cache;
2
3 import java.io.FileInputStream JavaDoc;
4 import java.io.FileNotFoundException JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Properties JavaDoc;
8
9 import com.dotmarketing.portlets.structure.model.Field;
10 import com.dotmarketing.portlets.structure.model.Structure;
11 import com.dotmarketing.util.Logger;
12 import com.dotmarketing.util.UtilMethods;
13
14 /**
15  * @author David
16  */

17 public class FieldsCache {
18
19     //Cache to store the data
20
private static DotCache cache;
21
22     //Name of the variable with the provider name
23
private static String JavaDoc providerEntry = "CACHE_PROVIDER";
24
25     //Name of the variable with the properties path
26
private static String JavaDoc propertiesEntry = "CACHE_PROPERTIES_FILE";
27
28     //region's name for the cache
29
private static String JavaDoc regionName = "FieldsCache";
30
31     static
32     {
33         init();
34     }
35     
36     public static void addFields(Structure st, List JavaDoc<Field> fields)
37     {
38         // we use the identifier uri for our mappings.
39
long inode = st.getInode();
40         String JavaDoc structureName = st.getName();
41         cache.put(inode, fields);
42         cache.put(structureName, fields);
43     }
44     
45     public static List JavaDoc<Field> getFieldsByStructureInode(long inode)
46     {
47         List JavaDoc<Field> fields = (List JavaDoc<Field>) cache.get(inode);
48         if (fields == null) {
49             Structure st = StructureCache.getStructureByInode(inode);
50             fields = st.getFields();
51             addFields(st, fields);
52         }
53         return fields;
54     }
55
56     public static List JavaDoc<Field> getFieldsByStructureInode(String JavaDoc inode)
57     {
58         return getFieldsByStructureInode(Long.parseLong(inode));
59     }
60
61     /**
62      * This methods retrieves the fields from the cache based in the
63      * structure name.
64      *
65      * This methods tries to retrive the fields from the cache, if the
66      * structure were not found in the cache, it would try to find it in database
67      * and store the fields in cache.
68      *
69      * <b>NOTE:</b> This method runs the same code than getFieldsByStructureType
70      * the name and the type of a structure are synomyns
71      *
72      * @param name Name of the structure
73      * @return The fields of the structure
74      */

75     public static List JavaDoc<Field> getFieldsByStructureName(String JavaDoc name)
76     {
77         List JavaDoc<Field> fields = (List JavaDoc<Field>) cache.get(name);
78         if (fields == null) {
79             Structure st = StructureCache.getStructureByName(name);
80             addFields(st, st.getFields());
81         }
82         return fields;
83     }
84
85     /**
86      * @see getFieldsByStructureName(String)
87      *
88      * @param type Type of the structure
89      * @return The fields of the structure
90      */

91     public static List JavaDoc<Field> getFieldsByStructureType(String JavaDoc type)
92     {
93         return getFieldsByStructureName(type);
94     }
95
96     public boolean hasFieldsByStructureType (String JavaDoc name) {
97         return getFieldsByStructureType(name) != null;
98     }
99     
100     public boolean hasFieldsByStructureName (String JavaDoc name) {
101         return getFieldsByStructureName(name) != null;
102     }
103     
104     public boolean hasFieldsByStructureInode (String JavaDoc inode) {
105         return getFieldsByStructureInode(inode) != null;
106     }
107     
108     public boolean hasFieldsByStructureInode (long inode) {
109         return getFieldsByStructureInode(inode) != null;
110     }
111     
112     public static void removeFields(Structure st)
113     {
114         long inode = st.getInode();
115         String JavaDoc structureName = st.getName();
116         cache.remove(inode);
117         cache.remove(structureName);
118     }
119
120     public static void clearCache()
121     {
122         //clear the cache
123
cache.clear();
124     }
125     
126     private static void init()
127     {
128         try
129         {
130             Properties JavaDoc WorkingProperties = new Properties JavaDoc();
131             String JavaDoc cacheProviderClassName = com.dotmarketing.util.Config.getStringProperty(providerEntry);
132             try {
133                 String JavaDoc propertyFilePath = com.dotmarketing.util.Config.getStringProperty(propertiesEntry);
134                 if (UtilMethods.isSet(propertyFilePath)) {
135                     FileInputStream JavaDoc fileInputStream = new FileInputStream JavaDoc(propertyFilePath);
136                     WorkingProperties.load(fileInputStream);
137                 }
138             } catch (FileNotFoundException JavaDoc ex) {
139                 
140                 String JavaDoc propertyFileNotFound = "The property file has been no found \n";
141                 Logger.debug(FieldsCache.class, propertyFileNotFound + ex.getMessage());
142             } catch (IOException JavaDoc ex) {
143                 Logger.debug(FieldsCache.class, ex.getMessage());
144             }
145             finally
146             {
147                 cache = new DotCache(cacheProviderClassName, regionName,WorkingProperties);
148             }
149         }
150         catch(Exception JavaDoc ex)
151         {
152             Logger.error(FieldsCache.class,ex.toString());
153         }
154     }
155 }
156
Popular Tags