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/02/20 09:55:29 UTC

svn commit: r509469 - in /myfaces/tobago/trunk/example: addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/ addressbook/src/main/resources/META-INF/ addressbook/src/main/webapp/ addressbook/src/main/webapp/WEB-INF/ demo/src/main/ja...

Author: bommel
Date: Tue Feb 20 00:55:28 2007
New Revision: 509469

URL: http://svn.apache.org/viewvc?view=rev&rev=509469
Log:
fileupload in addressbook demo
checkstyle

Added:
    myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/Picture.java
    myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/PictureServlet.java
Modified:
    myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/Address.java
    myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/AddressDAO.java
    myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/Controller.java
    myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/InMemoryAddressDAO.java
    myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/JpaAddressDAO.java
    myfaces/tobago/trunk/example/addressbook/src/main/resources/META-INF/persistence.xml
    myfaces/tobago/trunk/example/addressbook/src/main/webapp/WEB-INF/web.xml
    myfaces/tobago/trunk/example/addressbook/src/main/webapp/editor.jsp
    myfaces/tobago/trunk/example/demo/src/main/java/org/apache/myfaces/tobago/example/demo/ServerInfo.java

Modified: myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/Address.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/Address.java?view=diff&rev=509469&r1=509468&r2=509469
==============================================================================
--- myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/Address.java (original)
+++ myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/Address.java Tue Feb 20 00:55:28 2007
@@ -40,7 +40,8 @@
 import javax.persistence.PrePersist;
 import javax.persistence.PreUpdate;
 import javax.persistence.PostLoad;
-import java.io.File;
+import javax.persistence.OneToOne;
+import javax.persistence.CascadeType;
 import java.util.Date;
 import java.util.Locale;
 
@@ -77,7 +78,10 @@
   @AttributeOverrides(@AttributeOverride(name="email", column = @Column(name = "jobEmail")))
   private EmailAddress jobEmail;
   private String jobHomePage;
-  private static final String EMPTY_PORTRAIT = "image/empty_portrait.png";
+  @OneToOne(cascade = {CascadeType.ALL})
+  private Picture picture;
+
+ 
 
   public Address() {
     LOG.debug("Creating new Address");
@@ -288,13 +292,16 @@
     this.jobHomePage = jobHomePage;
   }
 
-  public String getImageFileName() {
-    String fileName = id + ".png";
-    if (new File(fileName).exists()) {
-      return fileName;
-    } else {
-      return EMPTY_PORTRAIT;
-    }
+  public boolean hasPicture() {
+    return picture != null;
+  }
+
+  public Picture getPicture() {
+    return picture;
+  }
+
+  public void setPicture(Picture picture) {
+    this.picture = picture;
   }
 
   public String toString() {

Modified: myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/AddressDAO.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/AddressDAO.java?view=diff&rev=509469&r1=509468&r2=509469
==============================================================================
--- myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/AddressDAO.java (original)
+++ myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/AddressDAO.java Tue Feb 20 00:55:28 2007
@@ -31,4 +31,6 @@
   List<Address> findAddresses() throws AddressDAOException;
 
   void removeAddress(Address address) throws AddressDAOException;
+
+  Address getAddress(Integer id);
 }

Modified: myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/Controller.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/Controller.java?view=diff&rev=509469&r1=509468&r2=509469
==============================================================================
--- myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/Controller.java (original)
+++ myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/Controller.java Tue Feb 20 00:55:28 2007
@@ -57,6 +57,7 @@
 
   public Controller() {
     LOG.debug("Creating new Controller");
+    System.err.println("Controller " + this);
   }
 
   public void setAddressDAO(AddressDAO addressDAO) throws AddressDAOException {
@@ -67,6 +68,7 @@
     LOG.debug("applicationContext: "+ctx);
     currentAddressList = addressDAO.findAddresses();
     currentAddress = new Address();
+
   }
 
   public String createAddress() {
@@ -75,6 +77,11 @@
     return OUTCOME_EDITOR;
   }
 
+  public String cancelAddress() throws AddressDAOException {
+    currentAddressList = addressDAO.findAddresses();    
+    return OUTCOME_LIST;
+  }
+
   public String editAddress() {
     LOG.debug("action: editAddress");
     List<Integer> selection = selectedAddresses.getSelectedRows();
@@ -83,8 +90,7 @@
       FacesContext.getCurrentInstance().addMessage(null, error);
       return null;
     }
-    Address selectedAddress = currentAddressList.get(selection.get(0));
-    currentAddress.fill(selectedAddress);
+    currentAddress = currentAddressList.get(selection.get(0));
     return OUTCOME_EDITOR;
   }
 
@@ -148,7 +154,7 @@
     this.renderPopup = renderPopup;
   }
 
- public boolean isRenderPopup() {
+  public boolean isRenderPopup() {
     return renderPopup;
   }
 
@@ -159,6 +165,8 @@
 
   public String okFileUpload() {
     setRenderFileUploadPopup(false);
+    Picture picture = new Picture(uploadedFile.getContentType(), uploadedFile.get());
+    currentAddress.setPicture(picture);
     return null;
   }
 
@@ -206,9 +214,8 @@
   }
 
   public String popupFileUpload() {
-    LOG.error("AHHHHHHH");
     setRenderFileUploadPopup(true);
-    return OUTCOME_EDITOR;
+    return null;
   }
 
 }

Modified: myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/InMemoryAddressDAO.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/InMemoryAddressDAO.java?view=diff&rev=509469&r1=509468&r2=509469
==============================================================================
--- myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/InMemoryAddressDAO.java (original)
+++ myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/InMemoryAddressDAO.java Tue Feb 20 00:55:28 2007
@@ -69,7 +69,7 @@
     }
   }
 
-  private Address getAddress(int id) {
+  public Address getAddress(Integer id) {
     for (Address address : addresses) {
       if (address.getId() == id) {
         return address;

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?view=diff&rev=509469&r1=509468&r2=509469
==============================================================================
--- 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 Tue Feb 20 00:55:28 2007
@@ -38,9 +38,13 @@
   private static final Log LOG = LogFactory.getLog(JpaAddressDAO.class);
 
   public Address updateAddress(Address address) throws AddressDAOException {
-    if (address.getId() == null) { 
+    if (address.getId() == null) {
       getJpaTemplate().persist(address);
     } else {
+      Picture picture = address.getPicture();
+      if (picture != null && picture.getId() == null) {
+        getJpaTemplate().persist(picture);
+      }
       getJpaTemplate().merge(address);
     }
     return address;
@@ -51,8 +55,12 @@
   }
 
   public  void removeAddress(Address address) throws AddressDAOException {
-    address = getJpaTemplate().find(Address.class, address.getId());
+    address = getAddress(address.getId());
     getJpaTemplate().remove(address);
   }
 
+
+  public Address getAddress(Integer id) {
+    return getJpaTemplate().find(Address.class, id);
+  }
 }

Added: myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/Picture.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/Picture.java?view=auto&rev=509469
==============================================================================
--- myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/Picture.java (added)
+++ myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/Picture.java Tue Feb 20 00:55:28 2007
@@ -0,0 +1,64 @@
+package org.apache.myfaces.tobago.example.addressbook;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import javax.persistence.Entity;
+import javax.persistence.Lob;
+import javax.persistence.Basic;
+import javax.persistence.FetchType;
+import javax.persistence.Id;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+
+/*
+ * Created by IntelliJ IDEA.
+ * User: bommel
+ * Date: Feb 19, 2007
+ * Time: 7:45:33 PM
+ */
+@Entity
+public class Picture {
+  @Id
+  @GeneratedValue(strategy = GenerationType.IDENTITY)
+  private Integer id;
+  private String contentType;
+  @Lob
+  @Basic(fetch = FetchType.EAGER)
+  private byte [] content;
+
+  public Picture() {
+  }
+
+  public Picture(String contentType, byte[] content) {
+    this.contentType = contentType;
+    this.content = content;
+  }
+
+  public Integer getId() {
+    return id;
+  }
+
+  public String getContentType() {
+    return contentType;
+  }
+
+  public byte[] getContent() {
+    return content;
+  }
+
+}

Added: myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/PictureServlet.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/PictureServlet.java?view=auto&rev=509469
==============================================================================
--- myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/PictureServlet.java (added)
+++ myfaces/tobago/trunk/example/addressbook/src/main/java/org/apache/myfaces/tobago/example/addressbook/PictureServlet.java Tue Feb 20 00:55:28 2007
@@ -0,0 +1,66 @@
+package org.apache.myfaces.tobago.example.addressbook;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.apache.myfaces.tobago.util.VariableResolverUtil;
+import org.apache.commons.io.IOUtils;
+
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.ServletException;
+import javax.faces.context.FacesContextFactory;
+import javax.faces.context.FacesContext;
+import javax.faces.FactoryFinder;
+import javax.faces.lifecycle.LifecycleFactory;
+import javax.faces.lifecycle.Lifecycle;
+import java.io.IOException;
+import java.io.ByteArrayInputStream;
+
+
+/*
+ * Created by IntelliJ IDEA.
+ * User: bommel
+ * Date: Feb 19, 2007
+ * Time: 8:09:49 PM
+ */
+public class PictureServlet extends HttpServlet {
+
+  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+    LifecycleFactory lFactory = (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
+    Lifecycle lifecycle = lFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
+    FacesContextFactory fcFactory =
+        (FacesContextFactory) FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
+    FacesContext facesContext = fcFactory.getFacesContext(getServletContext(), request, response, lifecycle);
+    Controller controller = (Controller) VariableResolverUtil.resolveVariable(facesContext, "controller");
+    Address address = controller.getCurrentAddress();
+    if (address.hasPicture()) {
+      Picture picture = address.getPicture();
+      byte[] content = picture.getContent();
+      if (content != null && content.length > 0) {
+        response.setContentType(picture.getContentType());
+        ByteArrayInputStream inputStream = new ByteArrayInputStream(content);
+        try {
+          IOUtils.copy(inputStream, response.getOutputStream());
+        } finally{
+          IOUtils.closeQuietly(inputStream);
+        }
+      }
+    }
+  }
+}

Modified: myfaces/tobago/trunk/example/addressbook/src/main/resources/META-INF/persistence.xml
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/example/addressbook/src/main/resources/META-INF/persistence.xml?view=diff&rev=509469&r1=509468&r2=509469
==============================================================================
--- myfaces/tobago/trunk/example/addressbook/src/main/resources/META-INF/persistence.xml (original)
+++ myfaces/tobago/trunk/example/addressbook/src/main/resources/META-INF/persistence.xml Tue Feb 20 00:55:28 2007
@@ -22,6 +22,7 @@
     <persistence-unit name="addressBook" transaction-type="RESOURCE_LOCAL">
       <class>org.apache.myfaces.tobago.example.addressbook.Address</class>
       <class>org.apache.myfaces.tobago.example.addressbook.EmailAddress</class>
+      <class>org.apache.myfaces.tobago.example.addressbook.Picture</class>
     </persistence-unit>
 </persistence>
 

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?view=diff&rev=509469&r1=509468&r2=509469
==============================================================================
--- 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 Tue Feb 20 00:55:28 2007
@@ -56,6 +56,11 @@
     <load-on-startup>1</load-on-startup>
   </servlet>
 
+  <servlet>
+    <servlet-name>PictureServlet</servlet-name>
+    <servlet-class>org.apache.myfaces.tobago.example.addressbook.PictureServlet</servlet-class>
+  </servlet>
+
   <!-- workaround for a bug in Bea Weblogic 8.1 SP 2 and older -->
   <servlet>
     <servlet-name>WeblogicWorkaroundServlet</servlet-name>
@@ -72,6 +77,11 @@
   <servlet-mapping>
     <servlet-name>FacesServlet</servlet-name>
     <url-pattern>/faces/*</url-pattern>
+  </servlet-mapping>
+
+  <servlet-mapping>
+    <servlet-name>PictureServlet</servlet-name>
+    <url-pattern>/faces/picture</url-pattern>
   </servlet-mapping>
 
   <servlet-mapping>

Modified: myfaces/tobago/trunk/example/addressbook/src/main/webapp/editor.jsp
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/example/addressbook/src/main/webapp/editor.jsp?view=diff&rev=509469&r1=509468&r2=509469
==============================================================================
--- myfaces/tobago/trunk/example/addressbook/src/main/webapp/editor.jsp (original)
+++ myfaces/tobago/trunk/example/addressbook/src/main/webapp/editor.jsp Tue Feb 20 00:55:28 2007
@@ -87,7 +87,8 @@
                   <tc:gridLayout rows="160px" columns="120px"/>
                 </f:facet>
                 <tc:form>
-                  <tc:button image="#{controller.currentAddress.imageFileName}"
+                  <tc:button
+                      image="#{controller.currentAddress.picture != null?'/tobago-example-addressbook/faces/picture?id=XXXX':'image/empty_portrait.png'}"
                       action="#{controller.popupFileUpload}">
                     <f:facet name="popup">
                       <tc:popup width="300px" height="170px" left="200px"
@@ -194,7 +195,7 @@
           <tc:cell />
           <tc:button action="#{controller.storeAddress}"
               label="#{bundle.editorStore}" defaultCommand="true" />
-          <tc:button action="list" immediate="true"
+          <tc:button action="#{controller.cancelAddress}" immediate="true"
               label="#{bundle.editorCancel}" />
         </tc:panel>
 

Modified: myfaces/tobago/trunk/example/demo/src/main/java/org/apache/myfaces/tobago/example/demo/ServerInfo.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/example/demo/src/main/java/org/apache/myfaces/tobago/example/demo/ServerInfo.java?view=diff&rev=509469&r1=509468&r2=509469
==============================================================================
--- myfaces/tobago/trunk/example/demo/src/main/java/org/apache/myfaces/tobago/example/demo/ServerInfo.java (original)
+++ myfaces/tobago/trunk/example/demo/src/main/java/org/apache/myfaces/tobago/example/demo/ServerInfo.java Tue Feb 20 00:55:28 2007
@@ -1,15 +1,5 @@
 package org.apache.myfaces.tobago.example.demo;
 
-import org.apache.commons.io.IOUtils;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
-import javax.faces.context.FacesContext;
-import javax.servlet.ServletContext;
-import java.util.Properties;
-import java.io.InputStream;
-import java.io.IOException;
-
 /*
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
@@ -27,6 +17,16 @@
  * limitations under the License.
  */
 
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import javax.faces.context.FacesContext;
+import javax.servlet.ServletContext;
+import java.util.Properties;
+import java.io.InputStream;
+import java.io.IOException;
+
 public class ServerInfo {
 
   private static final Log LOG = LogFactory.getLog(ServerInfo.class);
@@ -52,7 +52,7 @@
 
   public String getServerInfo() {
     if (enabled) {
-      return ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getServerInfo();
+      return ((ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext()).getServerInfo();
     } else {
       return null;
     }