package woko.facets.builtin.all;

import net.sourceforge.jfacets.annotations.FacetKey;
import woko.Woko;
import woko.facets.BaseFragmentFacet;
import woko.facets.WokoFacetContext;
import woko.facets.builtin.RenderLinks;
import woko.facets.builtin.WokoFacets;
import woko.persistence.ObjectStore;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@FacetKey(name=WokoFacets.renderLinksEdit, profileId="all")
public class RenderLinksEditImpl extends BaseFragmentFacet implements RenderLinks {

    public static final String FRAGMENT_PATH = "/WEB-INF/woko/jsp/all/renderLinksEdit.jsp";

    public String getPath() {
        return FRAGMENT_PATH;
    }

  public List<Link> getLinks() {
    List<Link> links = new ArrayList<Link>();
    WokoFacetContext  facetContext = getFacetContext();
    Woko woko = getFacetContext().getWoko();
    Object o = facetContext.getTargetObject();
    Class<?> oc = o.getClass();
    HttpServletRequest request = getRequest();
    ObjectStore store = woko.getObjectStore();

    // display view link if object can be displayed
    Object viewFacet = woko.getFacet(WokoFacets.view, request, o, oc);
    if (viewFacet!=null) {
      String className = store.getClassMapping(oc);
      String key = store.getKey(o);
      if (key!=null) {
        links.add(new Link(WokoFacets.view + "/" + className + "/" + key, "Close editing").setCssClass("close"));
      }
    }

    Object deleteFacet = woko.getFacet(WokoFacets.delete, request, o, oc);
    if (deleteFacet!=null) {
      String className = store.getClassMapping(oc);
      String key = store.getKey(o);
      if (key!=null) {
        links.add(new Link(WokoFacets.delete + "/" + className + "/" + key, "Delete").setCssClass("delete"));
      }
    }
    return links;
  }

}

@@ -200,27 +200,95 @@
 builtin.
-RenderLinks
+Delete;%0Aimport woko.facets.builtin.RenderLinks;%0Aimport woko.facets.builtin.View
 ;%0Aimport
@@ -470,17 +470,19 @@
 Key(name
-=
+ = 
 WokoFace
@@ -510,17 +510,19 @@
 rofileId
-=
+ = 
 %22all%22)%0Ap
@@ -765,16 +765,18 @@
 %0A    %7D%0A%0A
+  
   public
@@ -796,24 +796,28 @@
 etLinks() %7B%0A
+    
     List%3CLin
@@ -846,24 +846,28 @@
 st%3CLink%3E();%0A
+    
     WokoFace
@@ -875,17 +875,16 @@
 Context 
- 
 facetCon
@@ -905,24 +905,28 @@
 tContext();%0A
+    
     Woko wok
@@ -958,24 +958,28 @@
 Woko();%0A    
+  
+  
 Object o = f
@@ -1013,16 +1013,20 @@
 ();%0A    
+   
+ 
 Class%3C?%3E
@@ -1041,24 +1041,28 @@
 getClass();%0A
+    
     HttpServ
@@ -1096,24 +1096,28 @@
 uest();%0A    
+ 
+   
 ObjectStore 
@@ -1152,16 +1152,20 @@
 );%0A%0A    
+  
+  
 // displ
@@ -1204,16 +1204,20 @@
 splayed%0A
+    
     Obje
@@ -1271,32 +1271,36 @@
 equest, o, oc);%0A
+    
     if (viewFace
@@ -1296,30 +1296,40 @@
 f (viewFacet
-!=null
+ instanceof View
 ) %7B%0A      St
@@ -1318,32 +1318,38 @@
 f View) %7B%0A      
+   
+   
 String className
@@ -1370,32 +1370,38 @@
 assMapping(oc);%0A
+      
       String key
@@ -1422,33 +1422,41 @@
 y(o);%0A      
+  
+    
 if (key
+ 
 !=
+ 
 null) %7B%0A    
@@ -1451,32 +1451,40 @@
 null) %7B%0A        
+    
+    
 links.add(new Li
@@ -1569,24 +1569,30 @@
 (%22close%22));%0A
+      
       %7D%0A    
@@ -1587,27 +1587,35 @@
       %7D%0A    
+ 
-%7D%0A%0A
+   %7D%0A%0A    
     Object d
@@ -1673,24 +1673,28 @@
 st, o, oc);%0A
+    
     if (dele
@@ -1700,22 +1700,34 @@
 eteFacet
-!=null
+ instanceof Delete
 ) %7B%0A    
@@ -1724,24 +1724,30 @@
 te) %7B%0A      
+ 
+     
 String class
@@ -1776,24 +1776,30 @@
 apping(oc);%0A
+      
       String
@@ -1818,24 +1818,30 @@
 .getKey(o);%0A
+      
       if (ke
@@ -1841,18 +1841,20 @@
  if (key
+ 
 !=
+ 
 null) %7B%0A
@@ -1853,32 +1853,40 @@
 null) %7B%0A        
+   
+     
 links.add(new Li
@@ -1981,16 +1981,30 @@
     
+      %7D%0A        
 %7D%0A    
-%7D%0A
     
@@ -2017,15 +2017,17 @@
  links;%0A
+  
   %7D%0A%0A%7D%0A

fix bug in renderLinksEdit (didn't check for logir required facet)