package com.celements.rights;

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.rights.CelementsRightServiceImpl.PubUnpub;
import com.celements.web.service.IWebUtilsService;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.user.api.XWikiRightService;
import com.xpn.xwiki.web.Utils;

@Component("celementsright")
public class CelementsRightScriptService implements ScriptService {
  
  private static Log LOGGER = LogFactory.getFactory().getInstance(
      CelementsRightScriptService.class);
  
  @Requirement
  Execution execution;
  
  public boolean publicationActivated(DocumentReference forDoc) {
    XWikiRightService rightService = getContext().getWiki().getRightService();
    if(rightService instanceof CelementsRightServiceImpl) {
      return ((CelementsRightServiceImpl)rightService).isPublishActive(forDoc, 
          getContext());
    } else {
      LOGGER.warn("Needs CelementsRightServiceImpl for publish / unpublish to work");
      return false;
    }
  }
  
  public boolean hasAccessLevelPublished(String right, String username, 
      DocumentReference docname) {
    return hasAccessLevel(right, username, getWebUtils().getRefLocalSerializer(
        ).serialize(docname), CelementsRightServiceImpl.PubUnpub.PUBLISHED);
  }
  
  public boolean hasAccessLevelUnpublished(String right, String username, 
      DocumentReference docname) {
    return hasAccessLevel(right, username, getWebUtils().getRefLocalSerializer(
        ).serialize(docname), CelementsRightServiceImpl.PubUnpub.UNPUBLISHED);
  }
  
  boolean hasAccessLevel(String right, String username, String docname, 
      PubUnpub unpublished) {
    XWikiRightService rightService = getContext().getWiki().getRightService();
    if(rightService instanceof CelementsRightServiceImpl) {
      getContext().put("overridePubCheck", unpublished);
    } else {
      LOGGER.warn("Needs CelementsRightServiceImpl for publish / unpublish to work");
    }
    try {
      return rightService.hasAccessLevel(right, username, docname, getContext());
    } catch (XWikiException xwe) {
      LOGGER.error("hasAccessLevelPublished: Exception while checking access level for " +
          "right=" + right + ", username=" + username + ", docname=" + docname, xwe);
      return false;
    }
  }

  private XWikiContext getContext() {
    return (XWikiContext)execution.getContext().getProperty("xwikicontext");
  }
  
  private IWebUtilsService getWebUtils() {
    return (IWebUtilsService)Utils.getComponent(IWebUtilsService.class);
  }
}

@@ -377,34 +377,83 @@
 hts.
-CelementsRightServiceImpl.
+access.RightsAccessScriptService;%0Aimport com.celements.rights.publication.E
 PubU
@@ -663,16 +663,77 @@
 Utils;%0A%0A
+/**%0A * @deprecated use RightsAccessScriptService instead%0A */%0A
 @Compone
@@ -753,16 +753,28 @@
 right%22)%0A
+@Deprecated%0A
 public c
@@ -948,16 +948,94 @@
 ss);%0A  %0A
+  @Requirement(RightsAccessScriptService.NAME)%0A  ScriptService rightsAccess;%0A%0A
   @Requi
@@ -1068,98 +1068,102 @@
 on;%0A
-  
 %0A  p
-ublic boolean publicationActivated(DocumentReference forDoc) %7B%0A    XWikiRigh
+rivate RightsAccessScriptService getRightsAccess() %7B%0A    return (RightsAccessScrip
 tService
  rig
@@ -1162,128 +1162,192 @@
 vice
+)
  right
-Service = getContext().getWiki().getRightService();%0A    if(rightService instanceof CelementsRightServiceImpl
+sAccess;%0A  %7D%0A%0A  /**%0A   * @deprecated use RightsAccessScriptService.isPublishActive instead%0A   */%0A  @Deprecated%0A  public boolean publicationActivated(DocumentReference forDoc
 ) %7B%0A
-  
     
@@ -1357,231 +1357,153 @@
 urn 
-((CelementsRightServiceImpl)rightService).isPublishActive(forDoc, %0A          getContext());%0A    %7D else %7B%0A      LOGGER.warn(%22Needs CelementsRightServiceImpl for publish / unpublish to work%22);%0A      return false;%0A    %7D%0A  %7D%0A  
+getRightsAccess().isPublishActive(forDoc);%0A  %7D%0A  %0A  /**%0A   * @deprecated use RightsAccessScriptService.hasAccessLevel instead%0A   */%0A  @Deprecated
 %0A  p
@@ -1721,34 +1721,9 @@
 e), 
-CelementsRightServiceImpl.
+E
 PubU
@@ -1746,16 +1746,112 @@
 %0A  %7D%0A  %0A
+  /**%0A   * @deprecated use RightsAccessScriptService.hasAccessLevel instead%0A   */%0A  @Deprecated%0A
   public
@@ -2066,34 +2066,9 @@
 e), 
-CelementsRightServiceImpl.
+E
 PubU
@@ -2093,16 +2093,112 @@
 %0A  %7D%0A  %0A
+  /**%0A   * @deprecated use RightsAccessScriptService.hasAccessLevel instead%0A   */%0A  @Deprecated%0A
   boolea
@@ -2268,16 +2268,17 @@
  %0A      
+E
 PubUnpub
@@ -2428,24 +2428,89 @@
 viceImpl) %7B%0A
+      //TODO replace with overridePubUnpub on PublicationService%0A
       getCon

move hasAccessLevel api method to RightsAccesScriptService