Пример #1
0
 /**
  * Generate a relative path based on provided document for the directory where the document should
  * be stored.
  *
  * @param doc the document to export
  * @return the corresponding path
  */
 public String getDirectoryForDocument(XWikiDocument doc) {
   StringBuilder path = new StringBuilder();
   for (SpaceReference space : doc.getDocumentReference().getSpaceReferences()) {
     path.append(space.getName()).append('/');
   }
   return path.toString();
 }
  @Override
  public EntityReference getDefaultReference(EntityType type) {
    EntityReference result = null;

    XWikiContext xcontext = this.xcontextProvider.get();
    if (xcontext != null) {
      if (type == EntityType.WIKI) {
        result = xcontext.getWikiReference();
      } else if (type == EntityType.SPACE) {
        XWikiDocument currentDoc = xcontext.getDoc();
        if (currentDoc != null) {
          SpaceReference spaceReference = currentDoc.getDocumentReference().getLastSpaceReference();
          // Keep only the spaces part
          result = spaceReference.removeParent(spaceReference.getWikiReference());
        }
      } else if (type == EntityType.DOCUMENT) {
        XWikiDocument currentDoc = xcontext.getDoc();
        if (currentDoc != null) {
          DocumentReference documentReference = currentDoc.getDocumentReference();
          // Keep only the document part
          result = documentReference.removeParent(documentReference.getLastSpaceReference());
        }
      }
    }

    if (result == null) {
      result = super.getDefaultReference(type);
    }

    return result;
  }
  @Override
  public List<DocumentReference> getDocumentReferences(SpaceReference spaceReference)
      throws FilterException {
    List<String> documentNames;
    try {
      Query query =
          this.queryManager.createQuery(
              "select distinct doc.name from Document doc where doc.space = :space order by doc.name asc",
              Query.XWQL);
      query.bindValue("space", localSerializer.serialize(spaceReference));
      query.setWiki(spaceReference.getWikiReference().getName());

      documentNames = query.execute();
    } catch (QueryException e) {
      throw new FilterException(
          String.format("Failed to get the list of documents in space [%s]", spaceReference), e);
    }

    List<DocumentReference> documentReferences = new ArrayList<>(documentNames.size());
    for (String documentName : documentNames) {
      documentReferences.add(new DocumentReference(documentName, spaceReference));
    }

    return documentReferences;
  }
Пример #4
0
  @Test
  public void getSpacePreference() throws Exception {
    this.mocker.registerMockComponent(ConfigurationSource.class, "wiki");
    ConfigurationSource spaceConfiguration =
        this.mocker.registerMockComponent(ConfigurationSource.class, "space");

    when(this.xwikiCfgConfigurationSource.getProperty(anyString(), anyString()))
        .then(
            new Answer<String>() {
              @Override
              public String answer(InvocationOnMock invocation) throws Throwable {
                return invocation.getArgumentAt(1, String.class);
              }
            });

    WikiReference wikiReference = new WikiReference("wiki");
    SpaceReference space1Reference = new SpaceReference("space1", wikiReference);
    SpaceReference space2Reference = new SpaceReference("space2", space1Reference);

    // Without preferences and current doc

    assertEquals("", this.xwiki.getSpacePreference("pref", this.context));
    assertEquals(
        "defaultvalue", this.xwiki.getSpacePreference("pref", "defaultvalue", this.context));
    assertEquals("", this.xwiki.getSpacePreference("pref", space2Reference, this.context));
    assertEquals(
        "defaultvalue",
        this.xwiki.getSpacePreference("pref", space2Reference, "defaultvalue", this.context));

    // Without preferences but with current doc

    this.context.setDoc(new XWikiDocument(new DocumentReference("document", space2Reference)));

    assertEquals("", this.xwiki.getSpacePreference("pref", this.context));
    assertEquals(
        "defaultvalue", this.xwiki.getSpacePreference("pref", "defaultvalue", this.context));
    assertEquals("", this.xwiki.getSpacePreference("pref", space2Reference, this.context));
    assertEquals(
        "defaultvalue",
        this.xwiki.getSpacePreference("pref", space2Reference, "defaultvalue", this.context));

    // With preferences

    final Map<String, Map<String, String>> spacesPreferences = new HashMap<>();
    Map<String, String> space1Preferences = new HashMap<>();
    space1Preferences.put("pref", "prefvalue1");
    space1Preferences.put("pref1", "pref1value1");
    Map<String, String> space2Preferences = new HashMap<>();
    space2Preferences.put("pref", "prefvalue2");
    space2Preferences.put("pref2", "pref2value2");
    spacesPreferences.put(space1Reference.getName(), space1Preferences);
    spacesPreferences.put(space2Reference.getName(), space2Preferences);

    when(spaceConfiguration.getProperty(anyString(), same(String.class)))
        .then(
            new Answer<String>() {
              @Override
              public String answer(InvocationOnMock invocation) throws Throwable {
                if (context.getDoc() != null) {
                  Map<String, String> spacePreferences =
                      spacesPreferences.get(
                          context.getDoc().getDocumentReference().getParent().getName());
                  if (spacePreferences != null) {
                    return spacePreferences.get(invocation.getArgumentAt(0, String.class));
                  }
                }

                return null;
              }
            });

    this.context.setDoc(new XWikiDocument(new DocumentReference("document", space1Reference)));
    assertEquals("prefvalue1", this.xwiki.getSpacePreference("pref", this.context));
    assertEquals("prefvalue1", this.xwiki.getSpacePreference("pref", "defaultvalue", this.context));
    assertEquals("pref1value1", this.xwiki.getSpacePreference("pref1", this.context));
    assertEquals("", this.xwiki.getSpacePreference("pref2", this.context));

    this.context.setDoc(new XWikiDocument(new DocumentReference("document", space2Reference)));
    assertEquals("prefvalue2", this.xwiki.getSpacePreference("pref", this.context));
    assertEquals("prefvalue2", this.xwiki.getSpacePreference("pref", "defaultvalue", this.context));
    assertEquals("pref1value1", this.xwiki.getSpacePreference("pref1", this.context));
    assertEquals("pref2value2", this.xwiki.getSpacePreference("pref2", this.context));

    assertEquals("", this.xwiki.getSpacePreference("nopref", space1Reference, this.context));
    assertEquals(
        "defaultvalue",
        this.xwiki.getSpacePreference("nopref", space1Reference, "defaultvalue", this.context));
    assertEquals(
        "prefvalue1", this.xwiki.getSpacePreference("pref", space1Reference, this.context));
    assertEquals(
        "prefvalue1",
        this.xwiki.getSpacePreference("pref", space1Reference, "defaultvalue", this.context));
    assertEquals(
        "pref1value1", this.xwiki.getSpacePreference("pref1", space1Reference, this.context));
    assertEquals("", this.xwiki.getSpacePreference("pref2", space1Reference, this.context));

    assertEquals("", this.xwiki.getSpacePreference("nopref", space2Reference, this.context));
    assertEquals(
        "defaultvalue",
        this.xwiki.getSpacePreference("nopref", space2Reference, "defaultvalue", this.context));
    assertEquals(
        "prefvalue2", this.xwiki.getSpacePreference("pref", space2Reference, this.context));
    assertEquals(
        "prefvalue2",
        this.xwiki.getSpacePreference("pref", space2Reference, "defaultvalue", this.context));
    assertEquals(
        "pref1value1", this.xwiki.getSpacePreference("pref1", space2Reference, this.context));
    assertEquals(
        "pref2value2", this.xwiki.getSpacePreference("pref2", space2Reference, this.context));
  }