[weld-commits] Weld SVN: r6468 - in examples/trunk/jsf/pastecode: src/main/java/org/jboss/weld/examples/pastecode/jsf and 4 other directories.

weld-commits at lists.jboss.org weld-commits at lists.jboss.org
Wed Jun 16 21:01:05 EDT 2010


Author: pete.muir at jboss.org
Date: 2010-06-16 21:01:04 -0400 (Wed, 16 Jun 2010)
New Revision: 6468

Added:
   examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/jsf/TruncatingConverter.java
Modified:
   examples/trunk/jsf/pastecode/pom.xml
   examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/CodeFragmentManagerImpl.java
   examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/History.java
   examples/trunk/jsf/pastecode/src/main/resources/META-INF/persistence.xml
   examples/trunk/jsf/pastecode/src/main/webapp/WEB-INF/faces-config.xml
   examples/trunk/jsf/pastecode/src/main/webapp/history.xhtml
Log:
change search to use criteria, fix truncation

Modified: examples/trunk/jsf/pastecode/pom.xml
===================================================================
--- examples/trunk/jsf/pastecode/pom.xml	2010-06-17 00:09:39 UTC (rev 6467)
+++ examples/trunk/jsf/pastecode/pom.xml	2010-06-17 01:01:04 UTC (rev 6468)
@@ -51,12 +51,21 @@
       </dependency>
 
       <dependency>
-         <groupId>javax.persistence</groupId>
-         <artifactId>persistence-api</artifactId>
+         <groupId>org.hibernate.javax.persistence</groupId>
+         <artifactId>hibernate-jpa-2.0-api</artifactId>
+         <version>1.0.0.Final</version>
          <scope>provided</scope>
       </dependency>
 
       <dependency>
+         <groupId>org.hibernate</groupId>
+         <artifactId>hibernate-jpamodelgen</artifactId>
+         <!-- TODO Get this into Weld Parent! -->
+         <version>1.0.0.Final</version>
+         <scope>provided</scope>
+      </dependency>
+
+      <dependency>
          <groupId>javax.validation</groupId>
          <artifactId>validation-api</artifactId>
          <scope>provided</scope>
@@ -106,27 +115,47 @@
             </configuration>
          </plugin>
          <plugin>
-            <groupId>com.pyx4j</groupId>
-            <artifactId>maven-junction-plugin</artifactId>
+            <artifactId>maven-compiler-plugin</artifactId>
+            <configuration>
+               <compilerArgument>-proc:none</compilerArgument>
+            </configuration>
+         </plugin>
+         <plugin>
+            <groupId>org.bsc.maven</groupId>
+            <artifactId>maven-processor-plugin</artifactId>
+            <version>1.3.5</version>
             <executions>
                <execution>
-                  <id>unlink</id>
-                  <phase>clean</phase>
+                  <id>process</id>
                   <goals>
-                     <goal>unlink</goal>
+                     <goal>process</goal>
                   </goals>
+                  <phase>generate-sources</phase>
+                  <configuration>
+                     <!-- source output directory -->
+                     <outputDirectory>target/metamodel</outputDirectory>
+                  </configuration>
                </execution>
             </executions>
-            <configuration>
-               <links>
-                  <!-- link src/main/webapp -> war/ for IDEs -->
-                  <link>
-                     <dst>${basedir}/war</dst>
-                     <src>${basedir}/src/main/webapp</src>
-                  </link>
-               </links>
-            </configuration>
          </plugin>
+         <plugin>
+            <groupId>org.codehaus.mojo</groupId>
+            <artifactId>build-helper-maven-plugin</artifactId>
+            <executions>
+               <execution>
+                  <id>add-source</id>
+                  <phase>generate-sources</phase>
+                  <goals>
+                     <goal>add-source</goal>
+                  </goals>
+                  <configuration>
+                     <sources>
+                        <source>target/metamodel</source>
+                     </sources>
+                  </configuration>
+               </execution>
+            </executions>
+         </plugin>
       </plugins>
    </build>
 

Added: examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/jsf/TruncatingConverter.java
===================================================================
--- examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/jsf/TruncatingConverter.java	                        (rev 0)
+++ examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/jsf/TruncatingConverter.java	2010-06-17 01:01:04 UTC (rev 6468)
@@ -0,0 +1,24 @@
+package org.jboss.weld.examples.pastecode.jsf;
+
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.convert.Converter;
+
+public class TruncatingConverter implements Converter
+{
+
+   // The max length of the snippet we show
+   private static int TRIMMED_TEXT_LEN = 100;
+   
+   public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2)
+   {
+      return arg2;
+   }
+
+   public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2)
+   {
+      String text = arg2.toString();
+      return text.length() < TRIMMED_TEXT_LEN ? text : text.substring(0, TRIMMED_TEXT_LEN) + "\n ...";
+   }
+
+}


Property changes on: examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/jsf/TruncatingConverter.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:eol-style
   + native

Modified: examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/CodeFragmentManagerImpl.java
===================================================================
--- examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/CodeFragmentManagerImpl.java	2010-06-17 00:09:39 UTC (rev 6467)
+++ examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/CodeFragmentManagerImpl.java	2010-06-17 01:01:04 UTC (rev 6468)
@@ -22,7 +22,7 @@
 package org.jboss.weld.examples.pastecode.session;
 
 import java.security.NoSuchAlgorithmException;
-import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.List;
@@ -35,8 +35,13 @@
 import javax.persistence.EntityManager;
 import javax.persistence.PersistenceContext;
 import javax.persistence.Query;
+import javax.persistence.criteria.CriteriaBuilder;
+import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.criteria.Predicate;
+import javax.persistence.criteria.Root;
 
 import org.jboss.weld.examples.pastecode.model.CodeFragment;
+import org.jboss.weld.examples.pastecode.model.CodeFragment_;
 
 @Stateless
 public class CodeFragmentManagerImpl implements CodeFragmentManager
@@ -160,44 +165,48 @@
 
       return codes;
    }
+   
+   private static boolean isEmpty(String string)
+   {
+      return string == null || string.equals("");
+   }
 
-   public List<CodeFragment> searchCodeFragments(CodeFragment code, int page, Paginator paginator)
+   public List<CodeFragment> searchCodeFragments(CodeFragment codeFragment, int page, Paginator paginator)
    {
-      StringBuilder sb = new StringBuilder().append("SELECT c FROM CodeFragment c WHERE c.hash=null");
       
-      if (!code.getUser().trim().equals(""))
+      CriteriaBuilder builder = entityManager.getCriteriaBuilder();
+      CriteriaQuery<CodeFragment> criteria = builder.createQuery(CodeFragment.class);
+      
+      Root<CodeFragment> root = criteria.from(CodeFragment.class);
+      
+      List<Predicate> predicates = new ArrayList<Predicate>();
+      
+      predicates.add(builder.isNull(root.get(CodeFragment_.hash)));
+
+      if (!isEmpty(codeFragment.getUser()))
       {
-         sb.append(" AND c.user = \'").append(code.getUser().trim().toLowerCase()).append("\'");
+         predicates.add( builder.equal(root.get(CodeFragment_.user), codeFragment.getUser().toLowerCase().trim()) );
       }
-      if (code.getLanguage() != null)
+      if (codeFragment.getLanguage() != null)
       {
-         sb.append(" AND c.language = \'").append(code.getLanguage().name()).append(("\'"));
+         predicates.add( builder.equal(root.get(CodeFragment_.language), codeFragment.getLanguage()) );
       }
-      if (!code.getNote().trim().equals(""))
+      if (!isEmpty(codeFragment.getNote()))
       {
-         sb.append(" AND c.note LIKE \'%").append(code.getNote().trim().toLowerCase()).append("%\'");
+         predicates.add( builder.like(root.get(CodeFragment_.note), codeFragment.getNote().toLowerCase()) );
       }
-      if (!code.getText().trim().equals(""))
+      if (!isEmpty(codeFragment.getText()))
       {
-         sb.append(" AND c.text LIKE \'%").append(code.getText().toLowerCase()).append("%\'");
+         predicates.add( builder.like(root.get(CodeFragment_.text), codeFragment.getText().toLowerCase()) );
       }
-      if (code.getDatetime() != null)
+      if (codeFragment.getDatetime() != null)
       {
-         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
-         Date date2 = new Date();
-         date2.setTime(code.getDatetime().getTime() + 24 * 60 * 60 * 1000); // +1
-         // day
-
-         String formattedDate1 = formatter.format(code.getDatetime());
-         String formattedDate2 = formatter.format(date2);
-
-         sb.append(" AND c.datetime between \'").append(formattedDate1).append("\' and \'").append(formattedDate2).append("\'");
+         predicates.add( builder.between(root.get(CodeFragment_.datetime), codeFragment.getDatetime(), new Date()) );
       }
-      sb.append(" ORDER BY datetime DESC");
-      String queryString = sb.toString();
       
-      Query q = entityManager.createQuery(queryString);
+      criteria.where(predicates.toArray(new Predicate[0])).orderBy(builder.desc(root.get(CodeFragment_.datetime)));
       
+      Query q = entityManager.createQuery(criteria);
       q.setFirstResult(page * PAGE_SIZE);
       q.setMaxResults(PAGE_SIZE);
       

Modified: examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/History.java
===================================================================
--- examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/History.java	2010-06-17 00:09:39 UTC (rev 6467)
+++ examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/History.java	2010-06-17 01:01:04 UTC (rev 6468)
@@ -26,7 +26,6 @@
 
 import javax.ejb.Stateful;
 import javax.enterprise.context.SessionScoped;
-import javax.enterprise.inject.Produces;
 import javax.inject.Inject;
 import javax.inject.Named;
 
@@ -43,9 +42,6 @@
 {
 
    private static final long serialVersionUID = 20L;
-   
-   // The max length of the snippet we show
-   private static int TRIMMED_TEXT_LEN = 120;
 
    @Inject
    private CodeFragmentManager codeFragmentManager;
@@ -69,7 +65,6 @@
       return this.codes;
    }
 
-   @Produces @Named
    public CodeFragment getCodeFragmentPrototype()
    {
       return codeFragmentPrototype;
@@ -88,15 +83,9 @@
       this.paginator = new Paginator();
       this.codes = null;
       
-      // Perform a seach
+      // Perform a search
       
       this.codes = codeFragmentManager.searchCodeFragments(this.codeFragmentPrototype, this.page, this.paginator);
-
-      for (int i = 0; i != this.codes.size(); i++)
-      {
-         String s = this.codes.get(i).getText();
-         this.codes.get(i).setText(s.substring(0, s.length() > TRIMMED_TEXT_LEN ? TRIMMED_TEXT_LEN : s.length()) + "  .....");
-      }
       return "history";
    }
 

Modified: examples/trunk/jsf/pastecode/src/main/resources/META-INF/persistence.xml
===================================================================
--- examples/trunk/jsf/pastecode/src/main/resources/META-INF/persistence.xml	2010-06-17 00:09:39 UTC (rev 6467)
+++ examples/trunk/jsf/pastecode/src/main/resources/META-INF/persistence.xml	2010-06-17 01:01:04 UTC (rev 6468)
@@ -1,8 +1,8 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <persistence xmlns="http://java.sun.com/xml/ns/persistence" 
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" 
-             version="1.0">
+             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" 
+             version="2.0">
    <persistence-unit name="pastecodeDatabase">
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
       <jta-data-source>java:/pastecodeDatasource</jta-data-source>

Modified: examples/trunk/jsf/pastecode/src/main/webapp/WEB-INF/faces-config.xml
===================================================================
--- examples/trunk/jsf/pastecode/src/main/webapp/WEB-INF/faces-config.xml	2010-06-17 00:09:39 UTC (rev 6467)
+++ examples/trunk/jsf/pastecode/src/main/webapp/WEB-INF/faces-config.xml	2010-06-17 01:01:04 UTC (rev 6468)
@@ -26,5 +26,10 @@
       <converter-for-class>org.jboss.weld.examples.pastecode.model.Language</converter-for-class>
       <converter-class>org.jboss.weld.examples.pastecode.jsf.LanguageConverter</converter-class>
    </converter>
+   
+   <converter>
+      <converter-id>truncatingConverter</converter-id>
+      <converter-class>org.jboss.weld.examples.pastecode.jsf.TruncatingConverter</converter-class>
+   </converter>
 
 </faces-config>

Modified: examples/trunk/jsf/pastecode/src/main/webapp/history.xhtml
===================================================================
--- examples/trunk/jsf/pastecode/src/main/webapp/history.xhtml	2010-06-17 00:09:39 UTC (rev 6467)
+++ examples/trunk/jsf/pastecode/src/main/webapp/history.xhtml	2010-06-17 01:01:04 UTC (rev 6468)
@@ -45,7 +45,11 @@
 				<h:outputText value="Code (truncated): " />
 				<div
 					style="height: 100px; width: 99%; border: 1px solid #7F7F7F; overflow: auto; padding: 1px;">
-				<pre>#{codeFragment.text}</pre></div>
+				<pre>
+				   <h:outputText value="#{codeFragment.text}">
+				     <f:converter converterId="truncatingConverter"/>   
+				   </h:outputText>
+				</pre></div>
 				<br />
 		                &nbsp;<br />
 			</h:column>
@@ -67,25 +71,27 @@
 			cellpadding="0" cellspacing="0" columns="2" width="100%">
 			<h:outputLabel for="user" value="User:"></h:outputLabel>
 			<h:inputText id="user" maxlength="30" style="width:97%;"
-				value="#{codeFragmentPrototype.user}" />
+				value="#{history.codeFragmentPrototype.user}" />
 			<h:outputLabel for="language" value="Language:" />
-			<h:selectOneMenu id="language" value="#{codeFragmentPrototype.language}">
+			<h:selectOneMenu id="language" value="#{history.codeFragmentPrototype.language}">
 			   <f:selectItem itemLabel="Any" itemValue="" />
 				<f:selectItems value="#{languages}" var="language"
 					itemLabel="#{language.name}" itemValue="#{language}" />
 			</h:selectOneMenu>
 			<h:outputLabel for="note" value="Note:"></h:outputLabel>
 			<h:inputText id="note" maxlength="30" style="width:97%;"
-				value="#{codeFragmentPrototype.note}" />
+				value="#{history.codeFragmentPrototype.note}" />
 			<h:outputLabel for="pasteDate" value="Date: " />
-			<h:inputText value="#{codeFragmentPrototype.datetime}" id="pasteDate"
+			<h:inputText value="#{history.codeFragmentPrototype.datetime}" id="pasteDate"
 				style="width:97%;" alt="yyyy-mm-dd" title="yyyy-mm-dd"
 				converterMessage="Enter date formatted as yyyy-mm-dd !">
 				<f:convertDateTime type="date" pattern="yyyy-MM-dd" />
 			</h:inputText>
 			<h:outputLabel for="code" value="Code:" style="width:100%;"></h:outputLabel>
 			<h:inputTextarea id="code" maxlength="30" rows="2" style="width:97%;"
-				value="#{codeFragmentPrototype.text}" />
+				value="#{history.codeFragmentPrototype.text}">
+				<f:validateBean disabled="true" />
+		   </h:inputTextarea>
 		</h:panelGrid>
 		<div style="width: 100%; text-align: right;"><h:commandButton
 			value="Search" action="#{history.newSearch}" /></div>



More information about the weld-commits mailing list