labs-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simo...@apache.org
Subject svn commit: r880983 - in /labs/magma/trunk/website-beansview/src: main/java/org/apache/magma/website/beansview/ test/java/org/apache/magma/website/beansview/
Date Mon, 16 Nov 2009 21:52:15 GMT
Author: simoneg
Date: Mon Nov 16 21:52:14 2009
New Revision: 880983

URL: http://svn.apache.org/viewvc?rev=880983&view=rev
Log:
LABS-221 : use LinkHelper for FormAction and list links

Modified:
    labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/FormAction.java
    labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/ShowList.java
    labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/SmartForm.java
    labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/SmartList.java
    labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/SmartListProducer.java
    labs/magma/trunk/website-beansview/src/test/java/org/apache/magma/website/beansview/ShowListTest.java

Modified: labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/FormAction.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/FormAction.java?rev=880983&r1=880982&r2=880983&view=diff
==============================================================================
--- labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/FormAction.java
(original)
+++ labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/FormAction.java
Mon Nov 16 21:52:14 2009
@@ -16,10 +16,14 @@
  */
 package org.apache.magma.website.beansview;
 
+import java.lang.reflect.Method;
+
 import org.apache.magma.basics.MagmaException;
 import org.apache.magma.beans.MagmaBeanSupport;
 import org.apache.magma.conversion.Converter;
 import org.apache.magma.conversion.Converters;
+import org.apache.magma.website.WebHandler;
+import org.apache.magma.website.utils.LinkHelper;
 
 
 public class FormAction {
@@ -28,12 +32,13 @@
 	private String method;
 	private boolean parametrized = true;
 	private boolean validate;
+	
+	private LinkHelper linkHelper = null;
 
 	
 	public FormAction(String label, String method) {
 		this.label = label;
 		this.method = method;
-		checkMethod();
 	}
 	
 	
@@ -80,20 +85,29 @@
 		this.validate = validate;
 		return this;
 	}
+
+	protected void createHelper(Class<?> beanClass) {
+		if (linkHelper == null) {
+			if (this.parametrized && beanClass != null) {
+				linkHelper = new LinkHelper(this.method, beanClass);
+			} else {
+				linkHelper = new LinkHelper(this.method);
+			}
+		}		
+	}
+	
+	public void validate(Class<? extends WebHandler> against, Class<?> beanClass)
{
+		createHelper(beanClass);
+		linkHelper.checkAgainst(against);
+	}
+	
+	public Method getMethod(Class<? extends WebHandler> on, MagmaBeanSupport bean) {
+		createHelper(bean.getClass());
+		return linkHelper.findMethod(on);
+	}
 	
 	public String getMethodUrl(MagmaBeanSupport bean) {
-		String meth = getMethod();
-		if (meth.startsWith("do")) {
-			meth = meth.substring(2);
-		} else if (meth.startsWith("handle")) {
-			meth = meth.substring(6);
-		}
-		if (bean != null && this.parametrized) {
-			Converter conv = Converters.getConverterFor(bean.getClass());
-			if (conv == null) throw new MagmaException("Cannot find a converter for the form action
on bean {0}", bean.getClass());
-			meth += "!";
-			meth += conv.to(bean);
-		}
-		return meth;
+		createHelper(bean.getClass());
+		return linkHelper.makeLink(bean);
 	}
  }

Modified: labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/ShowList.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/ShowList.java?rev=880983&r1=880982&r2=880983&view=diff
==============================================================================
--- labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/ShowList.java
(original)
+++ labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/ShowList.java
Mon Nov 16 21:52:14 2009
@@ -35,6 +35,7 @@
 import org.apache.magma.website.Head;
 import org.apache.magma.website.HtmlProducer;
 import org.apache.magma.website.WebHandler;
+import org.apache.magma.website.utils.LinkHelper;
 import org.apache.velocity.VelocityContext;
 import org.apache.velocity.app.Velocity;
 
@@ -55,15 +56,15 @@
 	
 	protected Collection<?> list;
 	protected String link = null;
-	protected String linkUrl = null;
 	protected Class<? extends MagmaBeanSupport> myclass;
-	protected Converter converter;
 	protected String caption;
 	protected String summary;
 	protected String gototitle;
 	protected String[] layers = LIST;
 	protected ListStyle listStyle;
 	protected List<FormAction> actions = null;
+	protected LinkHelper linkHelper;
+	protected WebHandler referringHandler = null;
 	
 	
 	public <T extends MagmaBeanSupport> ShowList(Class<T> myclass, Collection<?
extends T> list, String link, String... layers) {
@@ -94,32 +95,22 @@
 	
 	@Override
 	public void produce(Writer tables) throws IOException {
-		this.gototitle = new LocalizableString("Show element").toString();		
+		this.gototitle = new LocalizableString("Show element").toString();
+		
+		if (referringHandler == null) referringHandler = getCreatingHandler();
+		
+		// Check link and actions validity
 		if (this.link != null) {
-			GenericClass gc = GenericClass.forClass(getCreatingHandler().getClass());
-			List<MethodDef> methods = gc.findMethods(this.link, new Class<?>[] { this.myclass
});
-			if (methods.size() == 0) {
-				methods = gc.findMethods(this.link, new Class<?>[] { null });
-				if (methods.size() == 0) {				
-					throw new MagmaException("Cannot find method {0}.{1}({2}) to make the link", getCreatingHandler(),
this.link, this.myclass);
-				} else if (methods.size() > 1) {
-					throw new MagmaException("There is more than one method {0}.{1}(), and no one is accepting
explicitly {2} as a parameter. Cannot not make the link", getCreatingHandler(), this.link,
this.myclass);					
-				}
-			}
-			this.linkUrl = this.link;
-			if (linkUrl.startsWith("do")) {
-				this.linkUrl = this.linkUrl.substring(2);
-			} else if (linkUrl.startsWith("handle")){
-				this.linkUrl = this.linkUrl.substring(6);				
-			} else {
-				throw new MagmaException("The link {0} is not valid : it should refer a doMethod or handleMethod",
this.link);
-			}
+			linkHelper = new LinkHelper(this.link, this.myclass);
+			linkHelper.checkAgainst(referringHandler.getClass());
 		}
-		if (this.linkUrl != null) {
-			this.converter = Converters.getConverterFor(this.myclass);
-			if (this.converter == null) throw new MagmaException("Cannot find converter for {0}, cannot
make link!", this.myclass);			
+		if (this.actions != null) {
+			for (FormAction action : this.actions) {
+				action.validate(referringHandler.getClass(), this.myclass);
+			}
 		}
-				
+		
+		// build the template
 		RootNode root = BeanData.getFor(myclass).getViewTree(this.layers);
 		// TODO optimize this
 		if (root.getAllNodes().size() == 1) throw new MagmaException("Cannot create list for {0},
seems like no @Listed elements are present or they are not correctly connected to each other
to form a proper list", myclass);
@@ -129,7 +120,7 @@
 		}
 		listStyle.setMyclass(myclass);
 		listStyle.setRoot(root);
-		listStyle.setWithLink(this.linkUrl != null);
+		listStyle.setWithLink(this.link != null);
 		listStyle.setWithAdditional(this.actions != null && this.actions.size() > 0);
 		String tplkey = listStyle.getClass().getName() + ":" + listStyle.getStyleKey();
 		
@@ -143,7 +134,8 @@
 				}
 			}
 		}
-		
+
+		// Output the list
 		VelocityContext ctx = new VelocityContext();
 		ctx.put("list", this);
 		Velocity.evaluate(ctx, tables, "", new StringReader(tplval));
@@ -201,7 +193,7 @@
 	}
 		
 	public String computeLinkURL(MagmaBeanSupport bean, String nodeid) {
-		return this.linkUrl + WebHandler.paramSeparator + this.converter.to(bean);
+		return this.linkHelper.makeLink(bean);
 	}
 	
 	public String computeLinkTitle(MagmaBeanSupport bean, String nodeid) {
@@ -281,5 +273,4 @@
 		return this;
 	}
 	
-	
 }

Modified: labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/SmartForm.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/SmartForm.java?rev=880983&r1=880982&r2=880983&view=diff
==============================================================================
--- labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/SmartForm.java
(original)
+++ labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/SmartForm.java
Mon Nov 16 21:52:14 2009
@@ -114,6 +114,12 @@
 	}
 	
 	public HtmlProducer doShow() {
+		// Check validity of actions
+		if (this.actions != null) {
+			for (FormAction action : this.actions) {
+				action.validate(getCreatingHandler().getClass(), bean.getClass());
+			}
+		}
 		parametrize(prodHeader);
 		parametrize(prodFooter);
 		BeanFormProducer producer = new BeanFormProducer(this.bean, errors, getMyCode(), this.layers);
@@ -164,21 +170,7 @@
 
 	protected HtmlProducer goToAction(FormAction action) {
 		WebHandler parent = getCreatingHandler();
-		GenericClass gc = GenericClass.forClass(parent.getClass());
-		List<MethodDef> methods = null;
-		if (action.isParametrized()) {
-			methods = gc.findMethods(action.getMethod(), new Class<?>[] { this.bean.getClass()
});
-			if (methods.size() == 0) {
-				throw new MagmaException("Cannot find method {0}.{1}({2}) to go after the form", getCreatingHandler(),
action.getMethod(), this.bean.getClass());					
-			}
-		} else {
-			methods = gc.findMethods(action.getMethod(), new Class<?>[] {});
-			if (methods.size() == 0) {
-				throw new MagmaException("Cannot find method {0}.{1}() to go after the form", getCreatingHandler(),
action.getMethod());					
-			}			
-		}
-
-		Method method = methods.get(0).getBaseMethod();
+		Method method = action.getMethod(parent.getClass(), bean);
 		try {
 			if (action.isParametrized()) {
 				return (HtmlProducer) method.invoke(parent, this.bean);

Modified: labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/SmartList.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/SmartList.java?rev=880983&r1=880982&r2=880983&view=diff
==============================================================================
--- labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/SmartList.java
(original)
+++ labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/SmartList.java
Mon Nov 16 21:52:14 2009
@@ -39,6 +39,7 @@
 import org.apache.magma.website.HtmlProducer;
 import org.apache.magma.website.WebHandler;
 import org.apache.magma.website.producers.ParametrizableProducer;
+import org.apache.magma.website.utils.LinkHelper;
 
 public class SmartList extends WebHandler {
 
@@ -64,7 +65,6 @@
 	private boolean defaultDesc = false;
 	
 	private String link;
-	private String linkUrl;
 	private Class<? extends MagmaBeanSupport> myclass;
 
 	// Delegate fields
@@ -83,29 +83,7 @@
 	}
 	
 	public HtmlProducer doShow() {
-		if (this.link != null) {
-			GenericClass gc = GenericClass.forClass(getCreatingHandler().getClass());
-			List<MethodDef> methods = gc.findMethods(this.link, new Class<?>[] { this.myclass
});
-			if (methods.size() == 0) {
-				methods = gc.findMethods(this.link, new Class<?>[] { null });
-				if (methods.size() == 0) {				
-					throw new MagmaException("Cannot find method {0}.{1}({2}) to make the link", getCreatingHandler(),
this.link, this.myclass);
-				} else if (methods.size() > 1) {
-					throw new MagmaException("There is more than one method {0}.{1}(), and no one is accepting
explicitly {2} as a parameter. Cannot not make the link", getCreatingHandler(), this.link,
this.myclass);					
-				}
-			}
-			this.linkUrl = this.link;
-			if (linkUrl.startsWith("do")) {
-				this.linkUrl = this.linkUrl.substring(2);
-			} else if (linkUrl.startsWith("handle")){
-				this.linkUrl = this.linkUrl.substring(6);				
-			} else {
-				throw new MagmaException("The link {0} is not valid : it should refer a doMethod or handleMethod",
this.link);
-			}
-		}
-		
 		checkSortables();
-		
 		Database db = new Database();
 		int count = -1;
 		StringBuilder countquery = new StringBuilder(this.query); 
@@ -162,7 +140,8 @@
 		producer.setSummary(summary);
 		producer.setListStyle(listStyle);
 		producer.setLayers(layers);
-		producer.setLink("../" + this.linkUrl);
+		producer.setLink(this.link);
+		producer.setReferringHandler(getCreatingHandler());
 		producer.compoundWith(prodHeader, CompoundType.BEFORE);
 		producer.compoundWith(prodFooter, CompoundType.AFTER);
 		return producer;

Modified: labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/SmartListProducer.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/SmartListProducer.java?rev=880983&r1=880982&r2=880983&view=diff
==============================================================================
--- labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/SmartListProducer.java
(original)
+++ labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/SmartListProducer.java
Mon Nov 16 21:52:14 2009
@@ -248,12 +248,19 @@
 		this.count = count;
 	}
 
-	public String getLink() {
-		return linkUrl;
+	public void setReferringHandler(WebHandler handler) {
+		this.referringHandler = handler;
 	}
-
-	public void setLink(String next) {
-		this.linkUrl = next;
+	
+	
+	public void setLink(String link) {
+		this.link = link;
 	}
-
+	
+	@Override
+	public String computeLinkURL(MagmaBeanSupport bean, String nodeid) {
+		// TODO this is ugly
+		return "../" + super.computeLinkURL(bean, nodeid);
+	}
+	
 }

Modified: labs/magma/trunk/website-beansview/src/test/java/org/apache/magma/website/beansview/ShowListTest.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/website-beansview/src/test/java/org/apache/magma/website/beansview/ShowListTest.java?rev=880983&r1=880982&r2=880983&view=diff
==============================================================================
--- labs/magma/trunk/website-beansview/src/test/java/org/apache/magma/website/beansview/ShowListTest.java
(original)
+++ labs/magma/trunk/website-beansview/src/test/java/org/apache/magma/website/beansview/ShowListTest.java
Mon Nov 16 21:52:14 2009
@@ -17,6 +17,8 @@
 package org.apache.magma.website.beansview;
 
 import org.apache.magma.beans.BeanData;
+import org.apache.magma.website.HtmlProducer;
+import org.apache.magma.website.WebHandler;
 import org.apache.magma.website.beansview.stuff.Person;
 
 import java.io.ByteArrayOutputStream;
@@ -28,7 +30,7 @@
 import static org.junit.Assert.*;
 
 
-public class ShowListTest {
+public class ShowListTest extends WebHandler {
 
 	@Test
 	public void listTemplateTest() throws Exception {
@@ -99,6 +101,7 @@
 		lst.add(p);
 		
 		ShowList sbp = new ShowList(Person.class, lst);
+		sbp.referringHandler = this;
 		sbp.addAction(new FormAction("Delete", "doDelete"));
 		sbp.addAction(new FormAction("Promote", "doPromote"));
 		
@@ -109,10 +112,16 @@
 
 		assertStructure(result, 
 				"<table","<tr",
-				"<td", "Simone", "</td", "<td", "Gianni", "<td", "additionals", "<a",
"href=\"Delete",
+				"<td", "Simone", "</td", "<td", "Gianni", "<td", "additionals", 
+				"<a", "href=\"delete!converted",
+				"<a", "href=\"promote!converted",
 				"</table");
 		System.out.println(result);
 	}
 	
+	// Cehcked for actions in actionsTest 
+	public HtmlProducer doDelete(Person p) { return null; };
+	public HtmlProducer doPromote(Person p) { return null; };
+	
 	
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org


Mime
View raw message