Esempio n. 1
0
  /**
   * Helper function to load an annotation object from an xwiki object.
   *
   * @param object the xwiki object to load an annotation from
   * @param deprecatedContext XWikiContext to make operations on xwiki data
   * @return the Annotation instance for the annotation stored in BaseObject
   */
  protected Annotation loadAnnotationFromObject(BaseObject object, XWikiContext deprecatedContext) {
    // load the annotation with its ID, special handling of the state since it needs
    // deserialization, special
    // handling of the original selection which shouldn't be set if it's empty
    Annotation annotation = new Annotation(object.getNumber() + "");
    annotation.setState(AnnotationState.valueOf(object.getStringValue(Annotation.STATE_FIELD)));
    String originalSelection = object.getStringValue(Annotation.ORIGINAL_SELECTION_FIELD);
    if (originalSelection != null && originalSelection.length() > 0) {
      annotation.setOriginalSelection(originalSelection);
    }

    Collection<String> skippedFields =
        Arrays.asList(new String[] {Annotation.ORIGINAL_SELECTION_FIELD, Annotation.STATE_FIELD});
    // go through all props and load them in the annotation, except for the ones already loaded
    // get all the props, filter those that need to be skipped and save the rest
    for (String propName : object.getPropertyNames()) {
      if (!skippedFields.contains(propName)) {
        try {
          annotation.set(propName, ((BaseProperty) object.get(propName)).getValue());
        } catch (XWikiException e) {
          this.logger.warn(
              "Unable to get property "
                  + propName
                  + " from object "
                  + object.getClassName()
                  + "["
                  + object.getNumber()
                  + "]. Will not be saved in the annotation.",
              e);
        }
      }
    }
    return annotation;
  }
  private static void fillObjectSummary(
      ObjectSummary objectSummary,
      ObjectFactory objectFactory,
      URI baseUri,
      Document doc,
      BaseObject xwikiObject,
      XWiki xwikiApi,
      Boolean withPrettyNames)
      throws XWikiException {

    objectSummary.setId(String.format("%s:%s", doc.getPrefixedFullName(), xwikiObject.getGuid()));
    objectSummary.setGuid(xwikiObject.getGuid());
    objectSummary.setPageId(doc.getPrefixedFullName());
    objectSummary.setPageVersion(doc.getVersion());
    objectSummary.setPageAuthor(doc.getAuthor());
    if (withPrettyNames) {
      objectSummary.setPageAuthorName(xwikiApi.getUserName(doc.getAuthor(), false));
    }
    objectSummary.setWiki(doc.getWiki());
    objectSummary.setSpace(doc.getSpace());
    objectSummary.setPageName(doc.getName());
    objectSummary.setClassName(xwikiObject.getClassName());
    objectSummary.setNumber(xwikiObject.getNumber());

    String[] propertyNames = xwikiObject.getPropertyNames();
    if (propertyNames.length > 0) {
      objectSummary.setHeadline(xwikiObject.get(propertyNames[0]).toFormString());
    }
  }