Browse Source

Initial support for fortunes

Martin Tzvetanov Grigorov 11 years ago
parent
commit
3a2fdc1eb6

+ 0 - 2
wicket/src/main/java/hellowicket/BasePage.java

@@ -1,7 +1,5 @@
 package hellowicket;
 package hellowicket;
 
 
-import org.apache.wicket.request.mapper.parameter.PageParameters;
-import org.apache.wicket.markup.html.basic.Label;
 import org.apache.wicket.markup.html.WebPage;
 import org.apache.wicket.markup.html.WebPage;
 
 
 public class BasePage extends WebPage
 public class BasePage extends WebPage

+ 17 - 12
wicket/src/main/java/hellowicket/WicketApplication.java

@@ -1,39 +1,44 @@
 package hellowicket;
 package hellowicket;
 
 
+import hellowicket.fortune.FortunePage;
+import org.apache.wicket.RuntimeConfigurationType;
 import org.apache.wicket.protocol.http.WebApplication;
 import org.apache.wicket.protocol.http.WebApplication;
+import org.apache.wicket.settings.IRequestCycleSettings;
 
 
 /**
 /**
- * Application object for your web application. If you want to run this application without deploying, run the Start class.
- * 
- * @see hellowicket.Start#main(String[])
+ * Application object for your web application..
  */
  */
 public class WicketApplication extends WebApplication
 public class WicketApplication extends WebApplication
 {
 {
-	/**
-	 * @see org.apache.wicket.Application#getHomePage()
-	 */
 	@Override
 	@Override
 	public Class<HomePage> getHomePage()
 	public Class<HomePage> getHomePage()
 	{
 	{
 		return HomePage.class;
 		return HomePage.class;
 	}
 	}
 
 
-	/**
-	 * @see org.apache.wicket.Application#init()
-	 */
 	@Override
 	@Override
 	public void init()
 	public void init()
 	{
 	{
 		super.init();
 		super.init();
 
 
-		// add your configuration here
-
 		// mount the resources under test
 		// mount the resources under test
 		mountResource("/json", new HelloJsonReference());
 		mountResource("/json", new HelloJsonReference());
 		mountResource("/db", new HelloDbReference());
 		mountResource("/db", new HelloDbReference());
 
 
+		mountPage("/fortunes", FortunePage.class);
+
 		// disable response caching to be more close to other
 		// disable response caching to be more close to other
 		// test applications' behavior
 		// test applications' behavior
-		getRequestCycleSettings().setBufferResponse(false);
+		IRequestCycleSettings requestCycleSettings = getRequestCycleSettings();
+		requestCycleSettings.setBufferResponse(false);
+
+		// set UTF-8 for /fortunes test
+		requestCycleSettings.setResponseRequestEncoding("UTF-8");
+	}
+
+	@Override
+	public RuntimeConfigurationType getConfigurationType()
+	{
+		return RuntimeConfigurationType.DEPLOYMENT;
 	}
 	}
 }
 }

+ 18 - 0
wicket/src/main/java/hellowicket/fortune/Fortune.java

@@ -0,0 +1,18 @@
+package hellowicket.fortune;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+
+/**
+ *
+ */
+@Entity
+public class Fortune
+{
+	@Id
+	@GeneratedValue(strategy = GenerationType.IDENTITY)
+	public int id;
+	public String message;
+}

+ 14 - 0
wicket/src/main/java/hellowicket/fortune/FortunePage.html

@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<html xmlns:wicket="http://wicket.apache.org">
+<head>
+    <title>Fortunes</title>
+</head>
+<body>
+    <table>
+        <tr><th>id</th><th>message</th></tr>
+        <wicket:container wicket:id="fortunes">
+            <tr><td wicket:id="id"></td><td wicket:id="message"></td></tr>
+        </wicket:container>
+    </table>
+</body>
+</html>

+ 63 - 0
wicket/src/main/java/hellowicket/fortune/FortunePage.java

@@ -0,0 +1,63 @@
+package hellowicket.fortune;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
+import org.apache.wicket.markup.html.WebPage;
+import org.apache.wicket.markup.html.basic.Label;
+import org.apache.wicket.markup.html.list.ListItem;
+import org.apache.wicket.markup.html.list.ListView;
+import org.hibernate.Query;
+import org.hibernate.Session;
+
+import hellowicket.HibernateUtil;
+
+/**
+ *
+ */
+public class FortunePage extends WebPage
+{
+	public FortunePage()
+	{
+		final Session session = HibernateUtil.getSessionFactory().openSession();
+
+		Query query = session.createQuery("from Fortune");
+		query.setReadOnly(true);
+		List list = query.list();
+		List<Fortune> fortunes = new ArrayList<Fortune>(list);
+
+		session.close();
+
+		Fortune newFortune = new Fortune();
+		newFortune.message = "Additional fortune added at request time.";
+		fortunes.add(newFortune);
+
+		sort(fortunes);
+
+		ListView<Fortune> listView = new ListView<Fortune>("fortunes", fortunes)
+		{
+			@Override
+			protected void populateItem(ListItem<Fortune> item)
+			{
+				Fortune fortune = item.getModelObject();
+				item.add(new Label("id", fortune.id));
+				item.add(new Label("message", fortune.message));
+			}
+		};
+		add(listView);
+	}
+
+	private void sort(List<Fortune> fortunes)
+	{
+		Collections.sort(fortunes, new Comparator<Fortune>()
+		{
+			@Override
+			public int compare(Fortune f1, Fortune f2)
+			{
+				return f1.message.compareTo(f2.message);
+			}
+		});
+	}
+}

+ 6 - 1
wicket/src/main/resources/hibernate.cfg.xml

@@ -3,7 +3,11 @@
         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 <hibernate-configuration>
 <hibernate-configuration>
     <session-factory>
     <session-factory>
-        <property name="hibernate.connection.datasource">java:comp/env/jdbc/hello_world</property>
+        <!--<property name="hibernate.connection.datasource">java:comp/env/jdbc/hello_world</property>-->
+        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
+        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hello_world?jdbcCompliantTruncation=false&amp;elideSetAutoCommits=true&amp;useLocalSessionState=true&amp;cachePrepStmts=true&amp;cacheCallableStmts=true&amp;alwaysSendSetIsolation=false&amp;prepStmtCacheSize=4096&amp;cacheServerConfiguration=true&amp;prepStmtCacheSqlLimit=2048&amp;zeroDateTimeBehavior=convertToNull&amp;traceProtocol=false&amp;useUnbufferedInput=false&amp;useReadAheadInput=false&amp;maintainTimeStats=false&amp;useServerPrepStmts&amp;cacheRSMetadata=true</property>
+        <property name="hibernate.connection.username">benchmarkdbuser</property>
+        <property name="hibernate.connection.password">benchmarkdbpass</property>
  
  
         <!-- SQL dialect -->
         <!-- SQL dialect -->
         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
@@ -14,5 +18,6 @@
         <property name="hibernate.current_session_context_class">thread</property>
         <property name="hibernate.current_session_context_class">thread</property>
 
 
         <mapping class="hellowicket.World" />
         <mapping class="hellowicket.World" />
+        <mapping class="hellowicket.fortune.Fortune"/>
     </session-factory>
     </session-factory>
 </hibernate-configuration>
 </hibernate-configuration>