/**
 * 
 */
package org.wyona.yanel.servlet.menu.impl;

import org.apache.log4j.Logger;
import org.wyona.yanel.core.Resource;
import org.wyona.yanel.core.api.attributes.WorkflowableV1;
import org.wyona.yanel.core.attributes.versionable.RevisionInformation;
import org.wyona.yanel.core.workflow.WorkflowException;
import org.wyona.yanel.servlet.menu.ITransitionMenuContent;
import org.wyona.yanel.servlet.menu.RevisionInformationMenuContent;
import org.wyona.yanel.servlet.menu.RevisionTransitions;
import org.wyona.yanel.servlet.menu.RevisionTransitionsMenuContent;
import org.wyona.yanel.servlet.menu.TransitionMenuContentImpl;

/**
 * Representation of a Revision item for a revision menu. The representation
 * contains an html <li> containing the revision number, and then an html
 * representation of the transitions on that revision.
 * (@see org.wyona.yanel.servlet.menu.RevisionTransitions).
 * TODO this class really belongs to YanelWebsite, so move it into that realm.
 *
 */
public class RevisionInformationMenuItem implements RevisionInformationMenuContent {
    
    private static Logger log = Logger.getLogger(RevisionInformationMenuItem.class);
    
    private Resource resource;
    private RevisionInformation revision;
    private String language;

    /**
     * ctor.
     * @param resource the resource on which the representation is to be based.
     * @param revn the revision of the resource on which the representation is to be based.
     * @param lang the desired language of the menu.
     */
    public RevisionInformationMenuItem(Resource resource, RevisionInformation revn, String lang) {
        this.resource = resource;
        this.revision = revn;
        this.language = lang;
    }

    private String getContent() {
        String value = "";
        
        try {
            WorkflowableV1 workflowable = (WorkflowableV1) this.resource;
            String state = workflowable.getWorkflowState(revision.getName());
            value = "<li class=\"haschild\">"
                  + this.revision.getName()
                  + " (" + formatDate(this.revision.getDate()) + ", " + state + ")"
                  + "&#160;&#160;&#160;";
            
            ITransitionMenuContent x =
                new TransitionMenuContentImpl(getResource(), state, getRevisionInfo().getName(), getMenuLanguageCode());
            RevisionTransitionsMenuContent rt = new RevisionTransitions(getResource(), getRevisionInfo().getName(), getMenuLanguageCode(), x);
            value += rt.toHTML();
            value += "</li>";
        } catch (WorkflowException e) {
            value = "";
            log.error("Could not get workflow.", e);
        }

        return value;
    }

    /* (non-Javadoc)
     * @see org.wyona.yanel.servlet.menu.impl.RevisionInformationMenuContent#toHTML()
     */
    public String toHTML() {
        return getContent();
    }

    /* (non-Javadoc)
     * @see org.wyona.yanel.servlet.menu.impl.RevisionInformationMenuContent#getMenuLanguageCode()
     */
    public String getMenuLanguageCode() {
        return this.language;
    }

    /* (non-Javadoc)
     * @see org.wyona.yanel.servlet.menu.impl.RevisionInformationMenuContent#getResource()
     */
    public Resource getResource() {
        return this.resource;
    }

    /* (non-Javadoc)
     * @see org.wyona.yanel.servlet.menu.impl.RevisionInformationMenuContent#getRevisionInfo()
     */
    public RevisionInformation getRevisionInfo() {
        return this.revision;
    }

    /**
     *
     */
    private String formatDate(java.util.Date date) {
        java.text.DateFormat df = new java.text.SimpleDateFormat();
        return df.format(date);
    }
}

@@ -282,34 +282,145 @@
 ore.
-workflow.WorkflowException
+util.ResourceAttributeHelper;%0Aimport org.wyona.yanel.core.workflow.WorkflowException;%0Aimport org.wyona.yanel.core.workflow.WorkflowHelper
 ;%0Aim
@@ -1911,16 +1911,158 @@
   try %7B%0A
+            if (ResourceAttributeHelper.hasAttributeImplemented(resource, %22Workflowable%22, %221%22) && WorkflowHelper.hasWorkflow(resource)) %7B%0A    
         
@@ -2119,32 +2119,36 @@
  this.resource;%0A
+    
             Stri
@@ -2213,32 +2213,36 @@
 ));%0A            
+  
+  
 value = %22%3Cli cla
@@ -2277,16 +2277,20 @@
         
+    
 + this.r
@@ -2325,16 +2325,20 @@
         
+ 
+   
 + %22 (%22 +
@@ -2395,16 +2395,20 @@
 e + %22)%22%0A
+    
         
@@ -2466,16 +2466,20 @@
         
+    
 ITransit
@@ -2513,16 +2513,20 @@
         
+   
+ 
 new Tran
@@ -2618,24 +2618,28 @@
 ageCode());%0A
+    
             
@@ -2773,32 +2773,36 @@
 x);%0A            
+  
+  
 value += rt.toHT
@@ -2811,32 +2811,36 @@
 ();%0A            
+    
 value += %22%3C/li%3E%22
@@ -2841,16 +2841,283 @@
 %3C/li%3E%22;%0A
+            %7D else %7B%0A                value = %22%3Cli%3E%22%0A                      + this.revision.getName()%0A                      + %22 (%22 + formatDate(this.revision.getDate()) + %22)%22%0A                      + %22&#160;&#160;&#160;%22;%0A                value += %22%3C/li%3E%22;%0A            %7D%0A
         

check if resource has workflow if resource is workflowable