You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by bo...@apache.org on 2007/11/25 20:59:01 UTC

svn commit: r598043 - in /myfaces/tobago/trunk/example/addressbook/src/main: java/org/apache/myfaces/tobago/example/addressbook/ java/org/apache/myfaces/tobago/example/addressbook/web/ webapp/WEB-INF/

Author: bommel
Date: Sun Nov 25 11:59:00 2007
New Revision: 598043

URL: http://svn.apache.org/viewvc?rev=598043&view=rev
Log:
apply some improvements from spring 2.5

Modified:
    myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/JpaAddressDao.java
    myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/web/AdminController.java
    myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/web/Controller.java
    myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/web/Countries.java
    myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/web/LoggingController.java
    myfaces/tobago/trunk/example/addressbook/src/main/webapp/WEB-INF/applicationContext.xml
    myfaces/tobago/trunk/example/addressbook/src/main/webapp/WEB-INF/faces-config.xml
    myfaces/tobago/trunk/example/addressbook/src/main/webapp/WEB-INF/web.xml

Modified: myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/JpaAddressDao.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/JpaAddressDao.java?rev=598043&r1=598042&r2=598043&view=diff
==============================================================================
--- myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/JpaAddressDao.java (original)
+++ myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/JpaAddressDao.java Sun Nov 25 11:59:00 2007
@@ -17,38 +17,47 @@
  * limitations under the License.
  */
 
-import org.springframework.orm.jpa.support.JpaDaoSupport;
-import org.springframework.transaction.annotation.Transactional;
-import org.springframework.stereotype.Repository;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.springframework.stereotype.Repository;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import javax.persistence.Query;
 import java.util.List;
 
 
 @Repository
 @Transactional()
-public class JpaAddressDao extends JpaDaoSupport implements AddressDao {
+@Service("addressDao")
+public class JpaAddressDao implements AddressDao {
 
   private static final Log LOG = LogFactory.getLog(JpaAddressDao.class);
 
+  @PersistenceContext(unitName = "addressBook")
+  private EntityManager entityManager;
+
   public Address updateAddress(Address address) throws AddressDaoException {
     if (address.getId() == null) {
-      getJpaTemplate().persist(address);
+      entityManager.persist(address);
     } else {
       Picture picture = address.getPicture();
       if (picture != null && picture.getId() == null) {
-        getJpaTemplate().persist(picture);
+        entityManager.persist(picture);
       }
-      getJpaTemplate().merge(address);
+      entityManager.merge(address);
     }
     return address;
   }
-
+  @Transactional(readOnly = true)
   public List<Address> findAddresses(String filter) throws AddressDaoException {
     return findAddresses(filter, null, true);
   }
 
+  @Transactional(readOnly = true)
+  @SuppressWarnings("unchecked")
   public List<Address> findAddresses(String filter, String column, boolean order) throws AddressDaoException {
     StringBuilder builder = new StringBuilder();
     builder.append("select a from Address a");
@@ -67,15 +76,16 @@
       builder.append(column);
       builder.append(order ? " desc" : " asc");
     }
-    return getJpaTemplate().find(builder.toString());
+    Query query = entityManager.createQuery(builder.toString());
+    return query.getResultList();
   }
 
   public void removeAddress(Address address) throws AddressDaoException {
     address = getAddress(address.getId());
-    getJpaTemplate().remove(address);
+    entityManager.remove(address);
   }
-
+  @Transactional(readOnly = true)
   public Address getAddress(Integer id) {
-    return getJpaTemplate().find(Address.class, id);
+    return entityManager.find(Address.class, id);
   }
 }

Modified: myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/web/AdminController.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/web/AdminController.java?rev=598043&r1=598042&r2=598043&view=diff
==============================================================================
--- myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/web/AdminController.java (original)
+++ myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/web/AdminController.java Sun Nov 25 11:59:00 2007
@@ -17,6 +17,9 @@
  * limitations under the License.
  */
 
+import org.springframework.stereotype.Component;
+import org.springframework.context.annotation.Scope;
+
 import javax.swing.DefaultBoundedRangeModel;
 import javax.swing.BoundedRangeModel;
 import javax.annotation.security.RolesAllowed;
@@ -28,6 +31,8 @@
  * Date: Mar 28, 2007
  * Time: 11:20:53 PM
  */
+@Component("admin")
+@Scope(value = "session")
 public class AdminController {
 
   private static final String OUTCOME_ADMIN = "admin";

Modified: myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/web/Controller.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/web/Controller.java?rev=598043&r1=598042&r2=598043&view=diff
==============================================================================
--- myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/web/Controller.java (original)
+++ myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/web/Controller.java Sun Nov 25 11:59:00 2007
@@ -35,7 +35,11 @@
 import org.apache.myfaces.tobago.example.addressbook.AddressDaoException;
 import org.apache.myfaces.tobago.example.addressbook.Picture;
 import org.apache.myfaces.tobago.model.SheetState;
+import org.springframework.context.annotation.Scope;
+import org.springframework.stereotype.Component;
 
+import javax.annotation.PostConstruct;
+import javax.annotation.Resource;
 import javax.faces.application.Application;
 import javax.faces.application.FacesMessage;
 import javax.faces.component.UIComponent;
@@ -51,6 +55,8 @@
 import java.util.List;
 import java.util.Locale;
 
+@Component("controller")
+@Scope(value = "session")
 public class Controller {
 
   private static final Log LOG = LogFactory.getLog(Controller.class);
@@ -81,15 +87,22 @@
   private boolean renderLastName = true;
   private boolean renderDayOfBirth;
 
+  @Resource( name = "addressDao")
   private AddressDao addressDao;
 
   private FileItem uploadedFile;
   private boolean renderFileUploadPopup;
 
   public Controller() {
+
+  }
+
+  @PostConstruct
+  public void init() throws AddressDaoException {
     FacesContext facesContext = FacesContext.getCurrentInstance();
     Application application = facesContext.getApplication();
     language = application.getDefaultLocale();
+    countries.init(language);
     facesContext.getExternalContext().getSession(true);
     initLanguages();
 
@@ -103,11 +116,11 @@
 
     ClientProperties client = ClientProperties.getInstance(facesContext);
     theme = client.getTheme();
+    currentAddressList = addressDao.findAddresses(searchCriterion);
   }
 
   public void setAddressDao(AddressDao addressDao) throws AddressDaoException {
     this.addressDao = addressDao;
-    currentAddressList = addressDao.findAddresses(searchCriterion);
   }
 
   public void sheetSorter(ActionEvent event) throws AddressDaoException {
@@ -305,9 +318,9 @@
     return countries;
   }
 
+  @Resource( name = "countries")
   public void setCountries(Countries countries) {
     this.countries = countries;
-    countries.init(language);
   }
 
   public List<SelectItem> getThemeItems() {

Modified: myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/web/Countries.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/web/Countries.java?rev=598043&r1=598042&r2=598043&view=diff
==============================================================================
--- myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/web/Countries.java (original)
+++ myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/web/Countries.java Sun Nov 25 11:59:00 2007
@@ -17,11 +17,16 @@
  * limitations under the License.
  */
 
+import org.springframework.stereotype.Component;
+import org.springframework.context.annotation.Scope;
+
 import javax.faces.model.SelectItem;
 import java.util.ArrayList;
 import java.util.Locale;
 import java.util.Collections;
 
+@Component("countries")
+@Scope(value = "session")
 public class Countries extends ArrayList<SelectItem> {
 
   public void init(Locale language) {

Modified: myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/web/LoggingController.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/web/LoggingController.java?rev=598043&r1=598042&r2=598043&view=diff
==============================================================================
--- myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/web/LoggingController.java (original)
+++ myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/web/LoggingController.java Sun Nov 25 11:59:00 2007
@@ -27,6 +27,8 @@
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.springframework.stereotype.Component;
+import org.springframework.context.annotation.Scope;
 
 import javax.faces.component.UIParameter;
 import javax.faces.context.FacesContext;
@@ -45,6 +47,9 @@
  * Date: 12.04.2007
  * Time: 22:39:05
  */
+
+@Component("logging")
+@Scope(value = "session")
 public class LoggingController {
 
   private static final Log LOG = LogFactory.getLog(LoggingController.class);

Modified: myfaces/tobago/trunk/example/addressbook/src/main/webapp/WEB-INF/applicationContext.xml
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/example/addressbook/src/main/webapp/WEB-INF/applicationContext.xml?rev=598043&r1=598042&r2=598043&view=diff
==============================================================================
--- myfaces/tobago/trunk/example/addressbook/src/main/webapp/WEB-INF/applicationContext.xml (original)
+++ myfaces/tobago/trunk/example/addressbook/src/main/webapp/WEB-INF/applicationContext.xml Sun Nov 25 11:59:00 2007
@@ -19,11 +19,18 @@
 
 <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
-                           http://www.springframework.org/schema/beans/spring-beans.xsd
-                           http://www.springframework.org/schema/tx
-                           http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
+           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
+           http://www.springframework.org/schema/context
+           http://www.springframework.org/schema/context/spring-context-2.5.xsd
+           http://www.springframework.org/schema/tx
+           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
+
+  <context:annotation-config/>
+
+  <context:component-scan base-package="org.apache.myfaces.tobago.example.addressbook" />
 
   <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
   
@@ -53,10 +60,6 @@
     <property name="dataSource" ref="dataSource"/>
   </bean>
 
-  <bean id="addressDao" class="org.apache.myfaces.tobago.example.addressbook.JpaAddressDao">
-    <property name="entityManagerFactory" ref="entityManagerFactory"/>
-  </bean>
-
-  <tx:annotation-driven />  
-
+  <tx:annotation-driven />
+  
 </beans>

Modified: myfaces/tobago/trunk/example/addressbook/src/main/webapp/WEB-INF/faces-config.xml
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/example/addressbook/src/main/webapp/WEB-INF/faces-config.xml?rev=598043&r1=598042&r2=598043&view=diff
==============================================================================
--- myfaces/tobago/trunk/example/addressbook/src/main/webapp/WEB-INF/faces-config.xml (original)
+++ myfaces/tobago/trunk/example/addressbook/src/main/webapp/WEB-INF/faces-config.xml Sun Nov 25 11:59:00 2007
@@ -62,38 +62,6 @@
   </lifecycle>
 
   <managed-bean>
-    <managed-bean-name>controller</managed-bean-name>
-    <managed-bean-class>org.apache.myfaces.tobago.example.addressbook.web.Controller</managed-bean-class>
-    <managed-bean-scope>session</managed-bean-scope>
-    <managed-property>
-      <property-name>addressDao</property-name>
-      <value>#{addressDao}</value>
-    </managed-property>
-    <managed-property>
-      <property-name>countries</property-name>
-      <value>#{countries}</value>
-    </managed-property>
-  </managed-bean>
-
-  <managed-bean>
-    <managed-bean-name>admin</managed-bean-name>
-    <managed-bean-class>org.apache.myfaces.tobago.example.addressbook.web.AdminController</managed-bean-class>
-    <managed-bean-scope>session</managed-bean-scope>
-  </managed-bean>
-
-  <managed-bean>
-      <managed-bean-name>logging</managed-bean-name>
-      <managed-bean-class>org.apache.myfaces.tobago.example.addressbook.web.LoggingController</managed-bean-class>
-      <managed-bean-scope>session</managed-bean-scope>
-  </managed-bean>
-
-  <managed-bean>
-    <managed-bean-name>countries</managed-bean-name>
-    <managed-bean-class>org.apache.myfaces.tobago.example.addressbook.web.Countries</managed-bean-class>
-    <managed-bean-scope>session</managed-bean-scope>
-  </managed-bean>
-
-  <managed-bean>
     <managed-bean-name>layout</managed-bean-name>
     <managed-bean-class>org.apache.myfaces.tobago.example.addressbook.web.Layout</managed-bean-class>
     <managed-bean-scope>session</managed-bean-scope>

Modified: myfaces/tobago/trunk/example/addressbook/src/main/webapp/WEB-INF/web.xml
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/example/addressbook/src/main/webapp/WEB-INF/web.xml?rev=598043&r1=598042&r2=598043&view=diff
==============================================================================
--- myfaces/tobago/trunk/example/addressbook/src/main/webapp/WEB-INF/web.xml (original)
+++ myfaces/tobago/trunk/example/addressbook/src/main/webapp/WEB-INF/web.xml Sun Nov 25 11:59:00 2007
@@ -31,6 +31,10 @@
   </listener>
 
   <listener>
+    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
+  </listener>
+
+  <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>