package com.celements.rights.access.internal;

import org.apache.commons.lang.RandomStringUtils;
import org.xwiki.component.annotation.Component;
import org.xwiki.component.annotation.Requirement;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.model.reference.SpaceReference;

import com.celements.model.access.IModelAccessFacade;

@Component
public class EntityReferenceRandomCompleter
  implements IEntityReferenceRandomCompleterRole {

  @Requirement
  private IModelAccessFacade modelAccess;

  public EntityReference randomCompleteSpaceRef(EntityReference entityRef) {
    if (entityRef.getType() == EntityType.SPACE) {
      SpaceReference spaceRef = new SpaceReference(entityRef);
      DocumentReference randomDocRef;
      do {
        String randomDocName = RandomStringUtils.randomAlphanumeric(50);
        randomDocRef = new DocumentReference(randomDocName, spaceRef);
      } while (modelAccess.exists(randomDocRef));
      entityRef = randomDocRef;
    }
    return entityRef;
  }

}

@@ -90,16 +90,62 @@
 gUtils;%0A
+import org.xwiki.bridge.DocumentAccessBridge;%0A
 import o
@@ -181,24 +181,24 @@
 .Component;%0A
-
 import org.x
@@ -427,63 +427,8 @@
 e;%0A%0A
-import com.celements.model.access.IModelAccessFacade;%0A%0A
 @Com
@@ -559,38 +559,49 @@
 ate 
-IModelAccessFacade modelAccess
+DocumentAccessBridge documentAccessBridge
 ;%0A%0A 
@@ -1002,19 +1002,28 @@
 le (
-modelAccess
+documentAccessBridge
 .exi

replace modelAccess with documentAccessBridge for exists-check in EntityReferenceRandomCompleter