/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package com.celements.cells;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xwiki.component.annotation.Component;
import org.xwiki.component.annotation.Requirement;
import org.xwiki.context.Execution;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.script.service.ScriptService;

import com.celements.cells.cmd.PageDependentDocumentReferenceCommand;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.api.Document;

@Component("cells")
public class CellsScriptService implements ScriptService {

  private static Log mLogger = LogFactory.getFactory().getInstance(
      CellsScriptService.class);

  @Requirement
  Execution execution;

  private PageDependentDocumentReferenceCommand injected_pageDepDocRefCmd;

  public DocumentReference getPageDependentDocRef(DocumentReference cellDocRef) {
    return getPageDepDocRefCmd().getDocumentReference(getContext().getDoc(), cellDocRef,
        getContext());
  }

  public DocumentReference getPageDependentDocRef(DocumentReference currentPageRef,
      DocumentReference cellDocRef) {
    try {
      return getPageDepDocRefCmd().getDocumentReference(getContext().getWiki(
          ).getDocument(currentPageRef, getContext()), cellDocRef, getContext());
    } catch (XWikiException exp) {
      mLogger.error("Failed to get xwiki document for [" + currentPageRef + "].", exp);
    }
    return currentPageRef;
  }

  public DocumentReference getPageDependentDocRef(DocumentReference currentPageRef,
      DocumentReference cellDocRef, boolean isInheritable) {
    try {
      return getPageDepDocRefCmd().getDocumentReference(getContext().getWiki(
          ).getDocument(currentPageRef, getContext()), cellDocRef, isInheritable,
          getContext());
    } catch (XWikiException exp) {
      mLogger.error("Failed to get xwiki document for [" + currentPageRef + "].", exp);
    }
    return currentPageRef;
  }

  public Document getPageDependentTranslatedDocument(Document currentDoc,
      DocumentReference cellDocRef) {
    try {
      return getPageDepDocRefCmd().getTranslatedDocument(getContext().getWiki(
        ).getDocument(currentDoc.getDocumentReference(), getContext()), cellDocRef,
         getContext()).newDocument(getContext());
    } catch (XWikiException exp) {
      mLogger.error("Failed to get xwiki document for ["
          + currentDoc.getDocumentReference() + "].", exp);
    }
    return currentDoc;
  }

  public boolean isInheritable(DocumentReference cellDocRef) {
    return getPageDepDocRefCmd().isInheritable(cellDocRef, getContext());
  }

  private XWikiContext getContext() {
    return (XWikiContext)execution.getContext().getProperty("xwikicontext");
  }
  
  PageDependentDocumentReferenceCommand getPageDepDocRefCmd() {
    if (injected_pageDepDocRefCmd != null) {
      return injected_pageDepDocRefCmd;
    }
    return new PageDependentDocumentReferenceCommand();
  }

  void inject_pageDepDocRefCmd(PageDependentDocumentReferenceCommand mockPageDepDocRefCmd
      ) {
    injected_pageDepDocRefCmd = mockPageDepDocRefCmd;
  }

}

@@ -1414,16 +1414,56 @@
 ocument;
+%0Aimport com.xpn.xwiki.doc.XWikiDocument;
 %0A%0A@Compo
@@ -3093,131 +3093,56 @@
 getC
-ontext().getWiki(%0A        ).getDocument(currentDoc.getDocumentReference(), getContext()), cellDocRef,%0A         getContext()
+urrentXWikiDoc(currentDoc),%0A          cellDocRef
 ).ne
@@ -3316,32 +3316,32 @@
 .%22, exp);%0A    %7D%0A
-
     return curre
@@ -3348,24 +3348,434 @@
 ntDoc;%0A  %7D%0A%0A
+  private XWikiDocument getCurrentXWikiDoc(Document currentDoc) throws XWikiException %7B%0A    return getCurrentXWikiDocDef(currentDoc).getTranslatedDocument(%0A        currentDoc.getLanguage(), getContext());%0A  %7D%0A%0A  private XWikiDocument getCurrentXWikiDocDef(Document currentDoc) throws XWikiException %7B%0A    return getContext().getWiki().getDocument(currentDoc.getDocumentReference(),%0A        getContext());%0A  %7D%0A%0A
   public boo

fix getting XWikiDocument for getPageDependentTranslatedDocument in CellsScriptService