1 23 24 25 package org.apache.slide.search.basic; 26 import java.util.Date; 27 import java.util.Enumeration; 28 import java.util.HashSet; 29 import java.util.Iterator; 30 import java.util.Set; 31 import org.apache.slide.common.Domain; 32 import org.apache.slide.common.PropertyName; 33 import org.apache.slide.common.SlideException; 34 import org.apache.slide.common.SlideToken; 35 import org.apache.slide.common.UriPath; 36 import org.apache.slide.content.Content; 37 import org.apache.slide.content.NodeProperty; 38 import org.apache.slide.content.NodeProperty.NamespaceCache; 39 import org.apache.slide.content.NodeRevisionContent; 40 import org.apache.slide.content.NodeRevisionDescriptor; 41 import org.apache.slide.content.NodeRevisionDescriptors; 42 import org.apache.slide.search.CompareHint; 43 import org.apache.slide.search.PropertyProvider; 44 import org.apache.slide.search.QueryScope; 45 import org.apache.slide.search.SearchToken; 46 import org.apache.slide.search.SlideUri; 47 import org.apache.slide.search.basic.Literals; 48 import org.apache.slide.security.AccessDeniedException; 49 import org.apache.slide.structure.ObjectNode; 50 51 57 public class ComparableResourceImpl implements ComparableResource { 58 59 61 62 63 private ObjectNode objectNode; 64 65 66 protected NodeRevisionDescriptor revisionDescriptor; 67 68 private Content contentHelper; 69 private NodeRevisionDescriptors revisionDescriptors; 70 private SlideToken slideToken; 71 private SearchToken searchToken; 72 73 76 protected PropertyProvider propertyProvider = null; 77 78 81 protected QueryScope scope = null; 82 83 84 85 static final private Set containsForbiddenOn = new HashSet(); 86 static { 87 containsForbiddenOn.add ("image/gif"); 88 containsForbiddenOn.add ("image/jpeg"); 89 containsForbiddenOn.add ("image/pjpeg"); 90 containsForbiddenOn.add ("image/jpg"); 91 containsForbiddenOn.add ("audio/mpg"); 92 containsForbiddenOn.add ("audio/mpeg"); 93 }; 94 95 private static final int EQ = 1; 96 private static final int GT = 2; 97 private static final int LT = 3; 98 private static final int GTE = 4; 99 private static final int LTE = 5; 100 101 102 103 114 public ComparableResourceImpl (ObjectNode objectNode, SearchToken searchToken, QueryScope scope, PropertyProvider propertyProvider) 115 throws SlideException 116 { 117 this (objectNode, searchToken.getSlideToken(), searchToken.getContentHelper(), scope, propertyProvider); 118 this.searchToken = searchToken; 119 } 120 121 122 124 136 public ComparableResourceImpl (ObjectNode objectNode, SlideToken slideToken, Content contentHelper, QueryScope scope, PropertyProvider propertyProvider) throws SlideException { 137 138 this.objectNode = objectNode; 139 this.contentHelper = contentHelper; 140 this.slideToken = slideToken; 141 this.propertyProvider = propertyProvider; 142 this.scope = scope; 143 144 try { 145 revisionDescriptors = 146 contentHelper.retrieve (slideToken, objectNode.getUri()); 147 148 try { 149 revisionDescriptor = 150 contentHelper.retrieve (slideToken, revisionDescriptors); 151 152 } 153 catch (org.apache.slide.content.RevisionDescriptorNotFoundException e) { 154 revisionDescriptor = new NodeRevisionDescriptor(0); 158 revisionDescriptor.setName(new UriPath(objectNode.getUri()).lastSegment()); 159 } 160 } 161 catch (AccessDeniedException e) { 162 e.fillInStackTrace(); 163 throw e; 164 } 165 166 catch (Exception e) { 167 e.printStackTrace(); 168 throw new SlideException (e.getMessage()); } 170 } 171 protected ComparableResourceImpl () {} 172 173 183 public int greaterThan (String propName, String propNamespace, String literal) { 184 return compare (propName, propNamespace, literal, GT); 185 } 186 187 public int greaterThanEquals (String propName, String propNamespace, String literal) { 188 return compare (propName, propNamespace, literal, GTE); 189 } 190 191 public int lowerThan (String propName, String propNamespace, String literal) { 192 return compare (propName, propNamespace, literal, LT); 193 } 194 195 public int lowerThanEquals (String propName, String propNamespace, String literal) { 196 return compare (propName, propNamespace, literal, LTE); 197 } 198 199 209 public int equals (String propName, String propNamespace, String literal) { 210 return compare (propName, propNamespace, literal, EQ); 211 } 212 213 222 public int propContains (String propName, String propNamespace, String literal) { 223 Object o = getThisValue (propName, propNamespace); 224 225 if (o instanceof String == false) 226 return Literals.UNKNOWN; 227 228 String thisValue = (String)o; 229 if (thisValue.indexOf(literal) == -1) 230 return Literals.FALSE; 231 232 return Literals.TRUE; 233 } 234 235 240 public String getInternalHref() { 241 return objectNode.getUri(); 242 } 243 244 250 public String getExternalHref () { 251 SlideUri slideContext = searchToken.getSlideContext(); 252 return slideContext.getContextPath (objectNode.getUri()); 253 } 254 255 256 263 public Object getThisValue (String propName, String propNamespace) { 264 Object result = null; 265 267 NodeProperty property = null; 269 try { 270 property = getProperty(propName, propNamespace); 271 } 272 catch (SlideException e) { 273 Domain.error("ComparableResourceImpl.getThisValue()", e); 274 } 275 result = (property == null) ? null : property.getValue(); 276 277 return result; 278 } 279 280 289 protected NodeProperty getProperty(String propName) { 290 NodeProperty property = null; 291 try { 292 property = getProperty(propName, NamespaceCache.DEFAULT_URI); 293 } 294 catch (SlideException e) { 295 Domain.error("ComparableResourceImpl.getProperty()", e); 296 } 297 return property; 298 } 299 300 301 306 public String getUri() { 307 return getInternalHref(); 308 } 309 310 319 public NodeProperty getProperty(PropertyName propertyName) throws SlideException { 320 return getProperty(propertyName.getName(), propertyName.getNamespace()); 321 } 322 323 335 public NodeProperty getProperty(String name, String namespace) throws SlideException { 336 337 NodeProperty property = null; 338 if ( (propertyProvider != null) && propertyProvider.isSupportedProperty(getUri(), name, namespace) ) { 339 property = propertyProvider.getProperty(getUri(), name, namespace); 340 } 341 else { 342 property = revisionDescriptor.getProperty (name, namespace); 343 } 344 return property; 345 } 346 347 354 public Iterator getAllPropertiesNames() throws SlideException { 355 356 Set propertyNamesSet = new HashSet(); 357 Enumeration enum = revisionDescriptor.enumerateProperties(); 358 NodeProperty property = null; 359 if (enum != null) { 360 while (enum.hasMoreElements()) { 361 property = (NodeProperty)enum.nextElement(); 362 propertyNamesSet.add(new PropertyName(property.getName(), 363 property.getNamespace())); 364 } 365 } 366 367 Iterator iterator = propertyProvider.getSupportedPropertiesNames(getUri()); 368 while (iterator.hasNext()) { 369 propertyNamesSet.add(iterator.next()); 370 } 371 372 return propertyNamesSet.iterator(); 373 } 374 375 382 public Iterator getAllProperties() throws SlideException { 383 384 Set propertySet = new HashSet(); 385 Enumeration enum = revisionDescriptor.enumerateProperties(); 386 if (enum != null) { 387 while (enum.hasMoreElements()) { 388 propertySet.add(enum.nextElement()); 389 } 390 } 391 392 if (propertyProvider != null) { 393 Iterator iterator = propertyProvider.getSupportedProperties(getUri()); 394 while (iterator.hasNext()) { 395 propertySet.add(iterator.next()); 396 } 397 } 398 399 return propertySet.iterator(); 400 } 401 402 411 public boolean isCollection (NodeRevisionDescriptor revisionDescriptor) { 412 boolean result = false; 413 414 if (revisionDescriptor == null) 415 return true; 416 417 NodeProperty property = revisionDescriptor.getProperty("resourcetype"); 418 419 if ((property != null) 420 && (property.getValue().equals(NodeRevisionDescriptor.COLLECTION_TYPE))) { 421 result = true; 422 } 423 424 return result; 425 } 426 427 428 439 public int compareTo (ComparableResource otherResource, CompareHint hint) { 440 int result = 0; 441 442 Comparable otherValue = (Comparable) otherResource.getThisValue (hint.getPropName(), hint.getPropNamespace()); 443 Comparable thisValue = (Comparable) getThisValue (hint.getPropName(), hint.getPropNamespace()); 444 445 if (getInternalHref().equals (otherResource.getInternalHref())) 446 result = 0; 447 448 else if (thisValue != null && otherValue != null) { 449 result = thisValue.compareTo(otherValue); 450 } 453 else if (thisValue == null) 454 result = -1; 455 456 else if (otherValue == null) 457 result = 1; 458 459 if (hint.isAscending() == false) 460 result = result * -1; 461 462 464 return result; 465 } 466 467 475 public boolean isDefined (String propName, String propNamespace) { 476 return getThisValue (propName, propNamespace) == null ? false : true; 477 } 478 479 487 public int contains (String literal) { 488 int result = Literals.UNKNOWN; 489 490 NodeProperty property = getProperty("getcontenttype"); 491 492 if (property != null) { 493 String contentType = (String) property.getValue(); 494 if (!containsForbiddenOn.contains (contentType)) { 495 496 try { 497 NodeRevisionContent revisionContent = contentHelper.retrieve (slideToken, revisionDescriptors, revisionDescriptor); 498 499 501 String content = new String (revisionContent.getContentBytes()); 502 503 if (content.indexOf (literal) > -1) 504 result = Literals.TRUE; 505 else 506 result = Literals.FALSE; 507 } 508 catch (org.apache.slide.common.SlideException e) { 509 } 511 512 } 513 } 514 515 return result; 516 } 517 518 519 533 private int compareTo (String propName, String propNamespace, String literal) 534 throws NumberFormatException, UnknownException 535 { 536 int result = 0; 537 538 Object thisValue = getThisValue (propName, propNamespace); 539 if (thisValue == null) 540 throw new UnknownException (); 541 542 Object otherValue = null; 543 544 otherValue = getOtherValue (thisValue, literal); 545 result = ((Comparable)thisValue).compareTo((Comparable)otherValue); 546 return result; 547 } 548 549 559 private Object getOtherValue (Object thisValue, String literal) 560 throws NumberFormatException 561 { 562 Object otherValue = null; 563 if (thisValue instanceof Boolean) 564 otherValue = new Boolean (literal); 565 566 else if (thisValue instanceof Date) { 567 otherValue = new Date (); } 570 571 else if (thisValue instanceof Float) 572 otherValue = new Float (literal); 573 574 else if (thisValue instanceof Integer) 575 otherValue = new Integer (literal); 576 577 else if (thisValue instanceof Long) 578 otherValue = new Long (literal); 579 580 else 581 otherValue = literal; 582 return otherValue; 583 } 584 585 private int compare (String propName, String propNamespace, String literal, int op) { 586 int result = Literals.FALSE; 587 588 Object thisValue = getThisValue (propName, propNamespace); 589 if (thisValue == null) 590 return Literals.UNKNOWN; 591 592 Object otherValue = null; 593 594 try { 595 otherValue = getOtherValue (thisValue, literal); 596 597 switch (op) 598 { 599 case EQ: 600 if (compareTo (propName, propNamespace, literal) == 0) 601 result = Literals.TRUE; 602 break; 603 604 case GT: 605 if (compareTo (propName, propNamespace, literal) > 0) 606 result = Literals.TRUE; 607 break; 608 609 case LT: 610 if (compareTo (propName, propNamespace, literal) < 0) 611 result = Literals.TRUE; 612 break; 613 614 case GTE: 615 if (compareTo (propName, propNamespace, literal) >= 0) 616 result = Literals.TRUE; 617 break; 618 619 case LTE: 620 if (compareTo (propName, propNamespace, literal) <= 0) 621 result = Literals.TRUE; 622 break; 623 default: 624 break; 625 } 626 } 627 catch (NumberFormatException e) { 628 result = Literals.UNKNOWN; 629 } 630 catch (UnknownException e) { 631 result = Literals.UNKNOWN; 632 } 633 return result; 634 } 635 636 public boolean equals (Object otherObject) { 637 if (!(otherObject instanceof ComparableResource)) 638 return false; 639 640 String otherUri = null; 641 try { 642 otherUri = ((ComparableResource)otherObject).getUri(); 643 } catch (SlideException e) {} 644 645 if (getUri().equals (otherUri)) 646 return true; 647 else 648 return false; 649 } 650 651 public int hashCode () { 652 return getUri().hashCode(); 653 } 654 655 656 659 private class UnknownException extends Exception { 660 } 661 } 662 663 | Popular Tags |