package com.celements.filebase;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xwiki.component.annotation.Component;
import org.xwiki.component.annotation.Requirement;
import org.xwiki.context.Execution;
import org.xwiki.model.reference.AttachmentReference;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.script.service.ScriptService;

import com.celements.web.plugin.cmd.TokenBasedUploadCommand;
import com.celements.web.service.IWebUtilsService;
import com.xpn.xwiki.XWikiException;

@Component("filebase")
public class FileBaseScriptService implements ScriptService {
  
  @Requirement
  IWebUtilsService webUtilsService;
  
  private static Logger _LOGGER  = LoggerFactory.getLogger(FileBaseScriptService.class);
  
  @Requirement
  private Execution execution;

  @Requirement
  IAttachmentServiceRole attachmentService;

  public String clearFileName(String fileName) {
    return attachmentService.clearFileName(fileName);
  }

  public int tokenBasedUpload(DocumentReference attachToDocRef, String fieldName, 
      String userToken) {
    return tokenBasedUpload(attachToDocRef, fieldName, userToken, false);
  }
  
  public int tokenBasedUpload(DocumentReference attachToDocRef, String fieldName, 
      String userToken, Boolean createIfNotExists) {
    try {
      return new TokenBasedUploadCommand().tokenBasedUploadDocRef(attachToDocRef,
          fieldName, userToken, createIfNotExists);
    } catch (XWikiException exp) {
      _LOGGER.error("token based attachment upload failed: ", exp);
    }
    return 0;
  }
  
  public int deleteAttachmentList(List<AttachmentReference> attachmentRefList) {
    return attachmentService.deleteAttachmentList(attachmentRefList);
  }
}

@@ -22,24 +22,52 @@
 .filebase;%0A%0A
+import java.util.ArrayList;%0A
 import java.
@@ -77,16 +77,16 @@
 l.List;%0A
-
 %0Aimport 
@@ -421,24 +421,211 @@
 ptService;%0A%0A
+import com.celements.filebase.exceptions.FileBaseLoadException;%0Aimport com.celements.filebase.exceptions.FileNotExistsException;%0Aimport com.celements.filebase.matcher.IAttachmentMatcher;%0A
 import com.c
@@ -720,24 +720,59 @@
 ilsService;%0A
+import com.xpn.xwiki.XWikiContext;%0A
 import com.x
@@ -795,16 +795,130 @@
 ception;
+%0Aimport com.xpn.xwiki.api.Attachment;%0Aimport com.xpn.xwiki.api.Document;%0Aimport com.xpn.xwiki.doc.XWikiAttachment;
 %0A%0A@Compo
@@ -1252,16 +1252,118 @@
 rvice;%0A%0A
+  @Requirement(SingleDocFileBaseService.FILEBASE_SINGLE_DOC)%0A  IFileBaseServiceRole filebaseService;%0A%0A
   public
@@ -2139,24 +2139,24 @@
 tRefList) %7B%0A
-
     return a
@@ -2216,11 +2216,1199 @@
 st);%0A  %7D
+%0A%0A  public boolean existsFileNameEqual(String filename) throws FileBaseLoadException %7B%0A    return filebaseService.existsFileNameEqual(filename);%0A  %7D%0A%0A  public Attachment getFileNameEqual(String filename) throws FileBaseLoadException %7B%0A    try %7B%0A      XWikiAttachment xwikiAtt = filebaseService.getFileNameEqual(filename);%0A      Document fbDoc = xwikiAtt.getDoc().newDocument(getContext());%0A      return new Attachment(fbDoc, xwikiAtt, getContext());%0A    %7D catch (FileNotExistsException e) %7B%0A      _LOGGER.trace(%22Filebase could not find file %5B%22 + filename + %22%5D%22);%0A      return null;%0A    %7D%0A  %7D%0A%0A  public List%3CAttachment%3E getFilesNameMatch(IAttachmentMatcher attMatcher%0A      ) throws FileBaseLoadException %7B%0A    List%3CXWikiAttachment%3E xwikiAttList = filebaseService.getFilesNameMatch(attMatcher);%0A    List%3CAttachment%3E attList = new ArrayList%3CAttachment%3E();%0A    for(XWikiAttachment xwikiAtt : xwikiAttList) %7B%0A      Document fbDoc = xwikiAtt.getDoc().newDocument(getContext());%0A      attList.add(new Attachment(fbDoc, xwikiAtt, getContext()));%0A    %7D%0A    return attList;%0A  %7D%0A%0A  private XWikiContext getContext() %7B%0A    return (XWikiContext)execution.getContext().getProperty(%22xwikicontext%22);%0A  %7D
 %0A%7D%0A

add several FileBaseService methods to script service