KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > content > ContentFieldXRefManager


1 package org.jahia.content;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.Set JavaDoc;
5 import java.util.TreeSet JavaDoc;
6
7 import org.jahia.exceptions.JahiaException;
8 import org.jahia.utils.JahiaConsole;
9
10 /**
11  * Title: Jahia
12  * Description: The purpose of this class is to provide a cross reference
13  * system between fields and pages, in order to know which pages reference
14  * a field. This is necessary since currently this information is defined
15  * only by template developpers and is useful for the HTML cache system when a
16  * field modification is done. This class allows lookups such as : "which
17  * pages access this field via the absolute field reference system?"
18  * Copyright: Copyright (c) 2002
19  * Company: Jahia Ltd
20  * @author Serge Huber
21  * @version 1.0
22  */

23
24 public class ContentFieldXRefManager {
25
26     private static ContentFieldXRefManager theObject = null;
27
28     protected ContentFieldXRefManager() {
29         JahiaConsole.println("FieldXRefManager",
30                              "Initializing...");
31     }
32
33     /**
34      * Returns a singleton instance of this service
35      * Note : this is a synchronized access method so it might affect performance
36      * if a lot of threads must access it simultaneously.
37      * @returns the singleton instance.
38      */

39     public static synchronized ContentFieldXRefManager getInstance()
40     {
41         if (theObject == null) {
42             theObject = new ContentFieldXRefManager();
43         }
44         return theObject;
45     } // end getInstance
46

47     /**
48      * Retrieves a list of pageIDs that contain references to a specific
49      * absolute field.
50      * @param fieldID the field identifier
51      * @returns a Set of Integers containing pageIDs
52      * @throws JahiaException is thrown in case there was a problem
53      * communicating with the database that stores the link persistently
54      */

55     public Set JavaDoc getAbsoluteFieldPageIDs(int fieldID)
56         throws JahiaException {
57
58         ContentFieldKey fieldKey = new ContentFieldKey(fieldID);
59
60         Set JavaDoc objectRefs = CrossReferenceManager.getInstance().getObjectXRefs(fieldKey);
61         Set JavaDoc pageIDs = new TreeSet JavaDoc();
62         if (objectRefs == null) {
63             return pageIDs;
64         }
65         Iterator JavaDoc objectRefIter = objectRefs.iterator();
66         while (objectRefIter.hasNext()) {
67             Object JavaDoc ref = objectRefIter.next();
68             if (ref instanceof ObjectKey) {
69                 ObjectKey refKey = (ObjectKey) ref;
70                 if (ContentPageKey.PAGE_TYPE.equals(refKey.getType())) {
71                     ContentPageKey pageRefKey = (ContentPageKey) refKey;
72                     Integer JavaDoc pageID = new Integer JavaDoc(pageRefKey.getPageID());
73                     pageIDs.add(pageID);
74                 } else {
75                     JahiaConsole.println("FieldXRefManager.getAbsoluteFieldPageIDs",
76                                          "Expected page type in cross reference list, ignoring value... ");
77                 }
78             } else {
79                 JahiaConsole.println("FieldXRefManager.getAbsoluteFieldPageIDs",
80                                      "Invalid key object in cross reference list, ignoring... ");
81             }
82         }
83         return pageIDs;
84     }
85
86     /**
87      * Retrieves a set of field keys that represent all the absolute fields
88      * used on a page.
89      * @param pageID the identifier of the page
90      * @return a Set of FieldKey objects that contain identifier to absolute
91      * fields used in the page.
92      * @throws JahiaException in case there was a problem retrieving the
93      * references from the database
94      */

95     public Set JavaDoc getAbsoluteFieldsFromPageID(int pageID)
96         throws JahiaException {
97
98         ContentPageKey pageKey = new ContentPageKey(pageID);
99
100         Set JavaDoc objectRefs = CrossReferenceManager.getInstance().getReverseObjectXRefs(pageKey);
101         Set JavaDoc fieldKeys = new TreeSet JavaDoc();
102         if (objectRefs == null) {
103             return fieldKeys;
104         }
105         Iterator JavaDoc objectRefIter = objectRefs.iterator();
106         while (objectRefIter.hasNext()) {
107             Object JavaDoc source = objectRefIter.next();
108             if (source instanceof ObjectKey) {
109                 ObjectKey sourceKey = (ObjectKey) source;
110                 if (ContentFieldKey.FIELD_TYPE.equals(sourceKey.getType())) {
111                     ContentFieldKey fieldKey = (ContentFieldKey) sourceKey;
112                     fieldKeys.add(fieldKey);
113                 } else {
114                     //JahiaConsole.println("FieldXRefManager.getAbsoluteFieldPageIDs",
115
// "Expected page type in cross reference list, ignoring value... ");
116
}
117             } else {
118                 JahiaConsole.println("FieldXRefManager.getAbsoluteFieldPageIDs",
119                                      "Invalid key object in cross reference list, ignoring... ");
120             }
121         }
122         return fieldKeys;
123
124     }
125
126     /**
127      * Adds or updates a cross reference between an absolute field and
128      * a page
129      * @param fieldID the identifier for the field
130      * @param referencePageID the page identifier for the page that references
131      * the field
132      * @throws JahiaException is thrown in case there was a problem
133      * communicating with the database that stores the link persistently
134      */

135     public void setAbsoluteFieldPageID(int fieldID,
136                                        int referencePageID)
137         throws JahiaException {
138         ContentFieldKey fieldKey = new ContentFieldKey(fieldID);
139         ContentPageKey pageKey = new ContentPageKey(referencePageID);
140         CrossReferenceManager.getInstance().setObjectXRef(fieldKey, pageKey);
141     }
142
143     /**
144      * Removes all the cross references for a specified absolute field
145      * @param fieldID the identifier for the field
146      * @throws JahiaException is thrown in case there was a problem
147      * communicating with the database that stores the link persistently
148      */

149     public void removeAbsoluteField(int fieldID)
150         throws JahiaException {
151         ContentFieldKey fieldKey = new ContentFieldKey(fieldID);
152         CrossReferenceManager.getInstance().removeObjectXRefs(fieldKey);
153     }
154
155     /**
156      * Removes a specific page cross reference for an absolute field
157      * @param fieldID the identifier for the field addressed absolutely
158      * @param referencePageID the page identifier that is referencing the
159      * field absolutely
160      * @throws JahiaException is thrown in case there was a problem
161      * communicating with the database that stores the link persistently
162      */

163     public void removeAbsoluteFieldPageID(int fieldID, int referencePageID)
164         throws JahiaException {
165         ContentFieldKey fieldKey = new ContentFieldKey(fieldID);
166         ContentPageKey pageKey = new ContentPageKey(referencePageID);
167         CrossReferenceManager.getInstance().removeObjectXRef(fieldKey, pageKey);
168     }
169
170 }
Popular Tags