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 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 alreadyLoadedValues = Boolean.FALSE; 33 34 int id; 35 int jahiaID; 36 int pageID; 37 int ctnID; 38 int type; 39 int right; 40 String fieldName = ""; 41 int versionID; 42 int workflowState; 43 String languageCode; 44 Object [] values; 45 46 51 56 57 66 public AddedField (JahiaField theField, int workflowState, 67 String keyFieldName, String 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 getFieldName () { 109 return fieldName; 110 } 111 112 public Object [] 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 getLanguageCode () { 125 return languageCode; 126 } 127 128 public void setLanguageCode (String 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 [] values) { 140 this.values = values; 141 } 142 143 149 public Hashtable getFields () { 150 prepareFieldValue (); 151 return super.getFields (); 152 } 153 154 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 [] values = null; 172 if (this.getType () != FieldTypes.FILE) { 173 Object [] objs = this.getValues (); 174 List list = Arrays.asList (objs); 175 values = new String [0]; 176 values = (String []) list.toArray (values); 177 } else { 178 try { 179 Object [] vals = this.getValues (); 181 if (vals != null && vals.length > 0) { 182 JahiaFileField fField = (JahiaFileField) vals[0]; 183 String 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 contentType = fField.getType (); 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 charSet = null; 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 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 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 [1]; 231 values[0] = strVal; 232 } 233 } 234 } catch (Throwable 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 key = null; 247 String [] vals = null; 248 while ( iterator.hasNext() ){ 249 key = (String )iterator.next(); 250 vals = (String [])extDoc.getPropertiesAsStrings(key); 251 if ( vals != null ){ 252 this.setField(JahiaSearchConstant.FILE_PROPERTY_PREFIX + key,vals); 253 } 254 } 255 } 256 257 } 258 259 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 t) { 272 } 273 274 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 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 t) { 309 logger.debug ("Error accessing field's categories", t); 310 } 311 312 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 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 t) { 348 logger.debug ("Error accessing field's categories", t); 349 } 350 } 351 352 this.setField (this.getKeyFieldName() , this.getKey ()); 353 } 354 355 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 377 public void doScheduledLoad(){ 378 this.getFields(); 379 } 380 381 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 |