Author: ssolsagl
Date: 2006-11-22 16:29:18 +0100 (Wed, 22 Nov 2006)
New Revision: 4006

Added:
   
trunk/core-api/src/main/java/no/schibstedsok/searchportal/view/velocity/ShareHoldersDirective.java
Log:

Parse and show shareholders 



Added: 
trunk/core-api/src/main/java/no/schibstedsok/searchportal/view/velocity/ShareHoldersDirective.java
===================================================================
--- 
trunk/core-api/src/main/java/no/schibstedsok/searchportal/view/velocity/ShareHoldersDirective.java
                          (rev 0)
+++ 
trunk/core-api/src/main/java/no/schibstedsok/searchportal/view/velocity/ShareHoldersDirective.java
  2006-11-22 15:29:18 UTC (rev 4006)
@@ -0,0 +1,300 @@
+package no.schibstedsok.searchportal.view.velocity;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+import org.apache.velocity.exception.MethodInvocationException;
+import org.apache.velocity.exception.ResourceNotFoundException;
+import org.apache.velocity.runtime.directive.Directive;
+import org.apache.velocity.runtime.parser.node.Node;
+import org.apache.velocity.context.InternalContextAdapter;
+import org.apache.velocity.exception.ParseErrorException;
+import org.dom4j.Document;
+import org.dom4j.DocumentHelper;
+import org.dom4j.Element;
+
+/**
+ * Directive to display share holder information in the yellowInfopage.vm. The
+ * layout is controlled by the css.
+ * 
+ * The layout can be controlled by the following css elements:
+ * 
+ * tabsheader - The table header rolestable - defining the overall gui for the
+ * table sh_left - Gui left Column sh_middle - Gui Middle Column sh_right - Gui
+ * Rigth
+ * 
+ * hover_on - The gray color witch is every second row
+ * 
+ * The way to use this would be: #shareHolder($company.getInfo(yproller)
+ * 
+ * The format of shareholders is defines as : #aksjonaer0#
+ * #bold#Navn#sep#Eierandel i %#sep#Antall aksjer#sepnl#
+ * 
+ * This class is not thread safe,,, must it be ?
+ * 
+ * @See JiraIssue https://jira.sesam.no/jira/browse/CAT-497
+ * @author olas <[EMAIL PROTECTED]>
+ * 
+ */
+public class ShareHoldersDirective extends Directive {
+       /** Logger */
+       private static Logger log = 
Logger.getLogger(ShareHoldersDirective.class);
+
+       // Constants -----------------------------------------------------
+       /** Title Name */
+       static final String MSG_NAME_SHAREHOLDER_TITLE = "Aksjonærer";
+
+       /** Th Name */
+       static final String MSG_NAME = "Navn";
+
+       /** Th number of shares */
+       static final String MSG_NUMB_OF_SHARES = "Antall aksjer";
+
+       /** Th shares in percent */
+       static final String MSG_SHARES_IN_PERCENT = "Andel i %";
+
+       
+       // Attributes -------------------------------------------------------
+       /** Table Background colors, switch between white and gray */
+       public enum TableBgColor {
+               GRAY, WHITE
+       }
+
+       /** The first row in the shareholders table is color white */
+       private TableBgColor tableBgColor = TableBgColor.WHITE;
+
+       /** Html elements used for building up the table */
+       static final String HTML_DIV = "div";
+
+       /** Table Tag */
+       static final String HTML_TABLE = "table";
+
+       /** Tr Tag */
+       static final String HTML_TR = "tr";
+
+       /** Th Tag */
+       static final String HTML_TH = "th";
+
+       /** Td Tag */
+       static final String HTML_TD = "td";
+
+       /** Html Class */
+       static final String HTML_CLASS = "class";
+
+       /** Not supported by IE ? Equivalent to the css attribute 
border-spacing */
+       static final String HTML_CELLSPACING = "cellspacing";
+
+       /**
+        * This is the information we get from the field 'yproller' aboute
+        * shareholders.
+        */
+       private class ShareHolder 
+       {
+               /** Name of the shareHolder field #1 */
+               String name;
+               /** Id so we can link to WhitePages */
+               String id; 
+               /** Stocks in percent, field #2 */
+               String sharesInPercent;
+               /** Number of stocks, field #2 */
+               String numberOfShares;
+       }
+
+    // Public --------------------------------------------------------
+
+       /**
+        * Name of Directive
+        * 
+        * @return directive name
+        */
+       public String getName() 
+       {
+               return "shareHolders";
+       }
+
+       /**
+        * Type of Directive
+        * 
+        * @return type as int value
+        */
+       public int getType() 
+       {
+               return LINE;
+       }
+
+       /**
+        * Render content (non-Javadoc)
+        * 
+        * @see 
org.apache.velocity.runtime.directive.Directive#render(org.apache.velocity.context.InternalContextAdapter,
+        *      java.io.Writer, org.apache.velocity.runtime.parser.node.Node)
+        */
+       public boolean render(InternalContextAdapter ica, Writer writer, Node 
node)
+                       throws IOException, ResourceNotFoundException, 
ParseErrorException,
+                       MethodInvocationException 
+       {
+        if (node.jjtGetNumChildren() != 1) 
+        {
+            rsvc.error("#" + getName() + " - wrong number of arguments");
+            return false;
+        }
+        // The text string from datafield which all the roledata is stored
+        String ypRoles = node.jjtGetChild(0).value(ica).toString();
+
+        /*
+        ypRoles = "#aksjonaer0##sepnl#\n " +  
+        "#bold#Navn#sep#Eierandel i %#sep#Antall aksjer#sepnl#\n" +
+        "STENSENTERET AS#id#2703596#sep#50#sep#100#sepnl#\n" +  
+        "ARILD C. GUSTAVSEN#id##sep#25#sep#50#sepnl#\n" +
+        "INGER A. O. GUSTAVSEN#id##sep#25#sep#50#sepnl#";
+               */
+
+        List<ShareHolder> shareHolders = parse(ypRoles);
+
+        if(shareHolders.size() == 0 )
+        {
+                       return true;
+        }
+               Document content = internalRender(parse(ypRoles));
+
+               // If we found shareHolders then print header.
+               Document root = DocumentHelper.createDocument();
+               Element header = 
root.addElement(HTML_DIV).addAttribute(HTML_CLASS,
+                               "tabs_header");
+               header.addText(MSG_NAME_SHAREHOLDER_TITLE);
+               writer.write(stripXmlHeader(header.asXML()));
+               writer.write("\n");
+               writer.write(stripXmlHeader(content.asXML()));
+               return true;
+       }
+
+       /*
+        * internal render content
+        */
+       private Document internalRender(List<ShareHolder> shareHolders) {
+
+               Document document = DocumentHelper.createDocument();
+               Element div = document.addElement(HTML_DIV);
+               Element table = div.addElement(HTML_TABLE);
+               table.addAttribute(HTML_CLASS, "roletable").
+                     addAttribute("cellspacing", "1");
+
+               Element tr1 = table.addElement(HTML_TR);
+               tr1.addElement(HTML_TH).addAttribute(HTML_CLASS, "sh_left").
+                  addText(MSG_NAME);
+               tr1.addElement(HTML_TH).addAttribute(HTML_CLASS, "sh_middle").
+                  addText(MSG_NUMB_OF_SHARES);
+               tr1.addElement(HTML_TH).addAttribute(HTML_CLASS, "sh_right").
+                  addText(MSG_SHARES_IN_PERCENT);
+
+               // Body goes here
+               table.addAttribute(HTML_CLASS, "roletable").
+                     addAttribute("cellspacing","1");
+
+               for (ShareHolder sh : shareHolders) 
+               {
+                       Element tr = table.addElement(HTML_TR);
+                       addTd(tr, "sh_left").addText(sh.name);
+                       addTd(tr, "sh_middle").addText(sh.numberOfShares);
+                       addTd(tr, "sh_right").addText(sh.sharesInPercent);
+
+                       switchBgColor();
+               }
+               return document;
+       }
+
+       /** 
+        * Strip away the xml header generated by dom4j
+        * 
+        * @param xml to be striped
+        * @return xml without header
+        */
+       public String stripXmlHeader(String xml) 
+       {
+               return xml.replace("<?xml version=\"1.0\" 
encoding=\"UTF-8\"?>", "");
+       }
+
+       /**
+        * Switch color
+        */
+       private void switchBgColor() 
+       {
+               if (tableBgColor == TableBgColor.WHITE) 
+               {
+                       tableBgColor = TableBgColor.GRAY;
+               }
+               else 
+               {
+                       tableBgColor = TableBgColor.WHITE;
+               }
+       }
+
+       /*
+        * Add a HTML_TD element to a HTML_TR element and add td attributes.
+        */
+       private Element addTd(Element tableRow, String cssClass) 
+       {
+
+               if (tableBgColor == TableBgColor.WHITE) 
+               {
+                       return 
tableRow.addElement(HTML_TD).addAttribute(HTML_CLASS,
+                                       cssClass);
+               }
+               else
+               {
+                       return 
tableRow.addElement(HTML_TD).addAttribute(HTML_CLASS,
+                                       cssClass + " hover_on");
+               }
+       }
+       
+       List<ShareHolder> parse(String content) {
+       
+               List<ShareHolder> shareHolders = new ArrayList();
+               boolean isShareHoldersStarted = false;
+               boolean isShareHolderRow = false;
+
+                       // Check input
+               if(content == null) {
+                       return shareHolders;
+               }
+               if("".equals(content.trim())) {
+                       return shareHolders;
+               }
+               
+               String row[] = content.split("#sepnl#");
+
+               for(String s : content.split("#sepnl#")) 
+               {
+                       s= s.trim();
+                       if(isShareHolderRow) {
+                               String tmp[] = s.split("#sep#");
+                       
+                               if(tmp.length == 3) {
+                                       ShareHolder sh = new ShareHolder();
+                                       String tmp2[] = tmp[0].split("#id#");
+                                       sh.name = tmp2[0];
+                                       if(tmp2.length==2){
+                                               sh.id = tmp2[1];
+                                       }
+                                       sh.sharesInPercent = tmp[1];
+                                       sh.numberOfShares = tmp[2];
+                                       shareHolders.add(sh);
+                               }
+                       }
+                       if(s.startsWith("#aksjonaer")) 
+                       {
+                               isShareHoldersStarted = true;
+                       }
+                       if(s.startsWith("#bold#Navn#sep") && 
isShareHoldersStarted) 
+                       {
+                               isShareHolderRow = true;
+                       }
+               }
+               return shareHolders;
+       }
+}

_______________________________________________
Kernel-commits mailing list
[email protected]
http://sesat.no/mailman/listinfo/kernel-commits

Reply via email to