KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > search > AddedField


1 package org.jahia.services.search;
2
3 import java.io.*;
4 import java.util.*;
5
6 import org.jahia.content.*;
7 import org.jahia.data.containers.*;
8 import org.jahia.data.fields.*;
9 import org.jahia.data.files.*;
10 import org.jahia.registries.*;
11 import org.jahia.services.categories.*;
12 import org.jahia.services.containers.*;
13 import org.jahia.services.sites.*;
14 import org.jahia.services.webdav.*;
15 import org.jahia.services.fileextraction.ExtractedDocument;
16 import org.jahia.utils.fileparsers.*;
17
18 /**
19  * <p>Title: This class represents a field wrapper used by search engine</p>
20  * <p>Description: </p>
21  * <p>Copyright: Copyright (c) 2002</p>
22  * <p>Company: </p>
23  *
24  * @author Khue Nguyen
25  * @version 1.0
26  */

27 class AddedField extends JahiaIndexableDocumentImpl {
28
29     private static org.apache.log4j.Logger logger =
30             org.apache.log4j.Logger.getLogger (AddedField.class);
31
32     private volatile Boolean JavaDoc alreadyLoadedValues = Boolean.FALSE;
33
34     int id;
35     int jahiaID;
36     int pageID;
37     int ctnID;
38     int type;
39     int right;
40     String JavaDoc fieldName = "";
41     int versionID;
42     int workflowState;
43     String JavaDoc languageCode;
44     Object JavaDoc[] values;
45
46     /**
47      * @param siteId
48      * @param key
49      * @param fields
50      */

51     /*
52     public AddedField (int siteId, String key,
53                                        Hashtable fields) {
54         super (siteId, null, key, fields);
55     }*/

56
57     /**
58      * A Field instance to store in the search index
59      *
60      * @param theField
61      * @param workflowState,
62      * the effective workflow state ( not the one returned by theField.getWorkflowState )
63      * @param fieldKeyName, the name of key field in which to store the key
64      * @param key, the value of the key to store in fieldKeyName
65      */

66     public AddedField (JahiaField theField, int workflowState,
67                        String JavaDoc keyFieldName, String JavaDoc key) {
68
69         super (theField.getJahiaID (), keyFieldName, key, null);
70
71         this.id = theField.getID ();
72         this.jahiaID = theField.getJahiaID ();
73         this.pageID = theField.getPageID ();
74         this.ctnID = theField.getctnid ();
75         this.type = theField.getType ();
76         this.right = theField.getAclID ();
77         this.workflowState = workflowState;
78         this.languageCode = theField.getLanguageCode ();
79         this.versionID = theField.getVersionID ();
80
81         this.initFields (theField);
82     }
83
84     public int getID () {
85         return id;
86     }
87
88     public int getJahiaID () {
89         return jahiaID;
90     }
91
92     public int getPageID () {
93         return pageID;
94     }
95
96     public int getctnid () {
97         return ctnID;
98     }
99
100     public int getType () {
101         return type;
102     }
103
104     public int getAclID () {
105         return right;
106     }
107
108     public String JavaDoc getFieldName () {
109         return fieldName;
110     }
111
112     public Object JavaDoc[] getValues () {
113         return values;
114     }
115
116     public int getVersionID () {
117         return versionID;
118     }
119
120     public int getWorkflowState () {
121         return workflowState;
122     }
123
124     public String JavaDoc getLanguageCode () {
125         return languageCode;
126     }
127
128     public void setLanguageCode (String JavaDoc languageCode) {
129         this.languageCode = languageCode;
130         this.setField(JahiaSearchConstant.FIELD_LANGUAGE_CODE,languageCode);
131     }
132
133     public void setWorkflowState (int workflowState) {
134         this.workflowState = workflowState;
135         this.setField(JahiaSearchConstant.FIELD_WORKFLOW_STATE,
136                       String.valueOf(workflowState));
137     }
138
139     public void setValues (Object JavaDoc[] values) {
140         this.values = values;
141     }
142
143     /**
144      * Return an hastable of key/values to store as fields for this object
145      * the key is a String and the values is an array of string values
146      *
147      * @return
148      */

149     public Hashtable getFields () {
150         prepareFieldValue ();
151         return super.getFields ();
152     }
153
154     /**
155      * Used to delay when field content will really be loaded in memory
156      */

157     private void prepareFieldValue () {
158
159         synchronized ( alreadyLoadedValues ) {
160             if (alreadyLoadedValues.booleanValue()) {
161                 return;
162             }
163             alreadyLoadedValues = Boolean.TRUE;
164             if ( this.getValues() == null ){
165                 return;
166             }
167         }
168
169         ExtractedDocument extDoc = null;
170
171         String JavaDoc[] values = null;
172         if (this.getType () != FieldTypes.FILE) {
173             Object JavaDoc[] objs = this.getValues ();
174             List list = Arrays.asList (objs);
175             values = new String JavaDoc[0];
176             values = (String JavaDoc[]) list.toArray (values);
177         } else {
178             try {
179                 // try to get a pdf document
180
Object JavaDoc[] vals = this.getValues ();
181                 if (vals != null && vals.length > 0) {
182                     JahiaFileField fField = (JahiaFileField) vals[0];
183                     String JavaDoc strVal = null;
184                     if (fField != null) {
185                         JahiaSite site = ServicesRegistry.getInstance ()
186                                 .getJahiaSitesService ().getSite (this.getJahiaID ());
187                         DAVFileAccess file = JahiaWebdavBaseService.getInstance ()
188                                 .getDAVFileAccess (site, fField.getRealName ());
189                         file.beginTransaction();
190                         if (file.isValid () && file.hasRevisions () && !file.isCollection ()) {
191                             String JavaDoc contentType = fField.getType (); // (String) file.getProperty("DAV:","getcontenttype");
192
if (contentType != null) {
193                                 if (!file.getPath().equals("#")) {
194
195                                     this.setField (JahiaSearchConstant.FILE_NAME, file.getName());
196
197                                     try {
198                                         InputStream ins = file.downloadFile();
199                                         String JavaDoc charSet = null; // by default open as ascii
200
CharsetDetection charsetDet = new CharsetDetection();
201                                         charsetDet.charsetDetection(ins);
202                                         charSet = charsetDet.getCharset();
203                                         ins.close();
204                                         long lastModifiedDate = System.currentTimeMillis();
205                                         try {
206                                             lastModifiedDate = file.getJahiaFileField()
207                                                 .getLastModifDate();
208                                         } catch ( Throwable JavaDoc t ){
209                                             logger.debug(t);
210                                         }
211
212                                         ins = file.downloadFile();
213                                         extDoc = ServicesRegistry.getInstance().getFileExtractionService()
214                                             .getExtractedDocument(contentType,file.getPath(),lastModifiedDate,
215                                                     true,ins,charSet);
216                                         ins.close();
217                                         if ( extDoc != null ){
218                                             strVal = extDoc.getContentAsString();
219                                         }
220                                     } catch ( Throwable JavaDoc t ){
221                                         logger.debug(t);
222                                     }
223                                 }
224                             }
225                             strVal += " " + file.getName();
226                         }
227                         file.commitTransaction();
228                     }
229                     if (strVal != null) {
230                         values = new String JavaDoc[1];
231                         values[0] = strVal;
232                     }
233                 }
234             } catch (Throwable JavaDoc t) {
235                 logger.warn ("Error parsing the file's content", t);
236             }
237         }
238
239         if (values != null) {
240             this.setField (JahiaSearchConstant.FIELD_VALUE, values);
241             this.unStoreField (JahiaSearchConstant.FIELD_VALUE);
242         }
243
244         if ( extDoc != null && !extDoc.getProperties().isEmpty()){
245             Iterator iterator = extDoc.getProperties().keySet().iterator();
246             String JavaDoc key = null;
247             String JavaDoc[] vals = null;
248             while ( iterator.hasNext() ){
249                 key = (String JavaDoc)iterator.next();
250                 vals = (String JavaDoc[])extDoc.getPropertiesAsStrings(key);
251                 if ( vals != null ){
252                     this.setField(JahiaSearchConstant.FILE_PROPERTY_PREFIX + key,vals);
253                 }
254             }
255         }
256
257     }
258
259     /**
260      * Prepares search index fields with field values
261      *
262      * @param theField
263      */

264     private void initFields (JahiaField theField) {
265
266         try {
267             JahiaFieldDefinition fieldDef = theField.getDefinition ();
268             if (fieldDef != null) {
269                 this.fieldName = fieldDef.getName ();
270             }
271         } catch (Throwable JavaDoc t) {
272         }
273
274         // Add comparable meta-data to index.
275
this.setField (JahiaSearchConstant.FIELD_OBJTYPE,
276                 String.valueOf (JahiaSearchConstant.FIELD_FIELDOBJ));
277         this.setField (JahiaSearchConstant.FIELD_FIELDID,
278                 String.valueOf (this.getID ()));
279         this.setField (JahiaSearchConstant.FIELD_JAHIAID,
280                 String.valueOf (this.getJahiaID ()));
281         this.setField (JahiaSearchConstant.FIELD_PAGEID,
282                 String.valueOf (this.getPageID ()));
283         this.setField (JahiaSearchConstant.FIELD_CTNID,
284                 String.valueOf (this.getctnid ()));
285         this.setField (JahiaSearchConstant.FIELD_FIELDTYPE,
286                 String.valueOf (this.getType ()));
287         this.setField (JahiaSearchConstant.FIELD_RIGHT,
288                 String.valueOf (this.getAclID ()));
289         this.setField (JahiaSearchConstant.FIELD_FIELDNAME,
290                 this.getFieldName ().toLowerCase ());
291         this.setField (JahiaSearchConstant.FIELD_VERSION,
292                 String.valueOf (this.getVersionID ()));
293         this.setField (JahiaSearchConstant.FIELD_WORKFLOW_STATE,
294                 String.valueOf (this.getWorkflowState()));
295         this.setField (JahiaSearchConstant.FIELD_LANGUAGE_CODE,
296                 this.getLanguageCode ());
297
298         // indexing the field categories
299
try {
300             ContentFieldKey fieldKey = new ContentFieldKey (this.getID ());
301             Set categories = ServicesRegistry.getInstance ().getCategoryService ()
302                     .getObjectCategories (fieldKey);
303             Iterator iterator = categories.iterator ();
304             while (iterator.hasNext ()) {
305                 Category category = (Category) iterator.next ();
306                 this.setField (JahiaSearchConstant.CATEGORY_KEY, category.getKey ());
307             }
308         } catch (Throwable JavaDoc t) {
309             logger.debug ("Error accessing field's categories", t);
310         }
311
312         // indexing the parent's container information
313
if (this.ctnID > 0) {
314             ContentContainer container = null;
315             try {
316                 container = ContentContainer
317                         .getContainer (this.ctnID);
318                 if ( container != null ){
319                     int defId = container.getDefinitionID(null);
320                     JahiaContainerDefinition ctnDef =
321                         JahiaContainerDefinitionsRegistry.getInstance()
322                         .getDefinition(defId);
323                     if (ctnDef != null) {
324                         this.setField(JahiaSearchConstant.
325                                       CONTAINER_DEFINITION_NAME,
326                                       ctnDef.getName().toLowerCase());
327                         logger.debug(
328                             "Indexing field container definition name:"
329                             + ctnDef.getName().toLowerCase());
330                     }
331                 }
332             } catch (Throwable JavaDoc t) {
333                 logger.debug ("Error retrieving container definition name for container :"
334                         + this.getctnid (), t);
335             }
336
337             try {
338                 ContentContainerKey ctnKey = new ContentContainerKey (this.ctnID);
339                 Set categories = ServicesRegistry.getInstance ().getCategoryService ()
340                         .getObjectCategories (ctnKey);
341                 Iterator iterator = categories.iterator ();
342                 while (iterator.hasNext ()) {
343                     Category category = (Category) iterator.next ();
344                     this.setField (JahiaSearchConstant.CONTAINER_CATEGORY_KEY,
345                             category.getKey ());
346                 }
347             } catch (Throwable JavaDoc t) {
348                 logger.debug ("Error accessing field's categories", t);
349             }
350         }
351
352         this.setField (this.getKeyFieldName() , this.getKey ());
353     }
354
355     /**
356      * If this document can be scheduled ( indexation can be delayed, i.e in case of File Field Document which can take a lot
357      * of time when parsing pdf file ), it should return true and add itself in the indexationJob
358      *
359      * By default, return false.
360      *
361      * @param indexationJobDetail
362      * @return
363      */

364     public boolean scheduled(IndexationJobDetail indexationJobDetail){
365         if ( this.getType() == FieldTypes.FILE &&
366                 !this.alreadyLoadedValues.booleanValue()
367                 && indexationJobDetail != null ){
368             indexationJobDetail.addDocument(this);
369             return true;
370         }
371         return false;
372     }
373
374     /**
375      * load all datas
376      */

377     public void doScheduledLoad(){
378         this.getFields();
379     }
380
381     /**
382      * Return true if this document is allowed to be indexed by RAMIndexer
383      *
384      * @return
385      */

386     public boolean isCacheableWithRAMIndexer() {
387         if ( this.getType() == FieldTypes.FILE ||
388              !super.isCacheableWithRAMIndexer() ){
389             return false;
390         }
391         return true;
392     }
393
394 }
395
Popular Tags