10 Replies Latest reply on Apr 18, 2008 3:42 AM by Jason Christian

    Upload file and insert description in DB

    Jacob SMA Newbie

      Hi everyone,


      I want to upload a file to a server and afterward write a description about the file and save the description and path to the file in the DB.


      The uploading part is already working. Below is my GUI. First the user should browse and upload the file and then fill out the description and press save.




      I have an action bean wich job is to upload the file. This part is working.


      I have a bean to save the information (description and image path) in the database.


      My code in the view look like this:


      <ice:form enctype="multipart/form-data">     
               <ice:panelGrid styleClass="componentLayoutRootTable" columns="1">
                   <ice:panelGroup styleClass="formBorderHighlight" style="text-align:left">
                       <ice:inputFile progressListener="#{inputFileSave.progressMonitor.progress}" 
                       actionListener="#{inputFileSave.progressMonitor.action}"/>
                                   
                      <ice:outputProgress id="pro" value="#{inputFileSave.percent}"/>
                     </ice:panelGroup>
                </ice:panelGrid>     
                
            </ice:form>
            <!-- Print uploaded filename and path to the screen -->
          <h:outputText id="imgNameId" value="#{inputFileSave.fileName}"/>     
           <h:outputText id="imgPathId" value="#{inputFileSave.filePath}"/>
          
          <ice:form id="imageEditForm">
             <ice:panelGroup  id="editimageId" styleClass="formBorderHighlight">
                
                  <s:decorate id="descriptionDecoration" template="layout/edit.xhtml">
                      <ui:define name="label">Description</ui:define>
                      <h:inputText id="description" required="false"
                                   value="#{imageHome.instance.description}"/>
                  </s:decorate>
                  
                  <div style="clear:both"/>
             </ice:panelGroup>
      
              <div class="actionButtons">
                  <h:commandButton id="editimageTypeHomesave" 
                                value="Save" 
                               action="#{imageHome.persist}"
                             rendered="#{!imageHome.managed}"/>                   
                  <h:commandButton id="editimageTypeHomeupdate" 
                                value="Save" 
                               action="#{imageHome.update}"
                             rendered="#{imageHome.managed}"/>                  
                  <h:commandButton id="editimageHomedelete" 
                                value="Delete" 
                               action="#{imageHome.remove}"
                            immediate="true"
                             rendered="#{imageHome.managed}"/>
                  <s:button propagation="end" 
                                     id="editimageTypeHomedone" 
                                  value="Done"
                                   view="/imageList.xhtml"/>
              </div>
          </ice:form>
      



      My problem is how to I pass the path information from InputFileSaveAction to the ImageHome bean in a clever way? I have tried to set the @Out and @In annotation on the beans, but without luck. I have tried to find any examples on the net. Im just started using JSF and Seam, so maybe this is really simple?




      Thanks, regards
      Jacob






      Here is the code from the two beans:


      @Name("imageHome")
      @Scope(ScopeType.SESSION)
      public class ImageHome extends EntityHome<Image>
      {
          @RequestParameter 
          Integer imageId;
          
          @Factory("imageType")
           public Image initImage() { return getInstance(); }
          
          @In(required=true)
          private String filePath = "";
          
          @Override
          public Object getId() 
          { 
              if (imageId==null)
              {
                  return super.getId();
              }
              else
              {
                  return imageId;
              }
          }
          
          @Override
          public void create() {
              super.create();
          }
      
           @Override
           public String persist() {
                instance.setPath(filePath);
                return super.persist();
           }
          
      }
      




      @Stateful
      @Name("inputFileSave")
      @Local(InputFileSave.class)
      @Remote(InputFileSave.class)
      @Scope(ScopeType.SESSION)
      public class InputFileSaveAction implements Renderable, InputFileSave {
           
           private @Logger Log log;
            
          private int percent = -1;
          private File file = null;
      
          private transient PersistentFacesState state = PersistentFacesState.getInstance();
      
          private String fileName = "";
          @Out(required=true)
          private String filePath = "";
          private String contentType = "";
      
          private InnerProgressMonitor pmImpl = new InnerProgressMonitor();
      
          public InputFileSaveAction() {
              pmImpl = new InnerProgressMonitor();
              state = PersistentFacesState.getInstance();
          }
      
      
          public PersistentFacesState getState() {
              return state;
          }
      
          public void renderingException(RenderingException re) {
              log.error("Exception : " + re);
          }
      
      
          public void setPercent(int percent) {
              this.percent = percent;
          }
      
          public int getPercent() {
              return percent;
          }
      
          public void setFile(File file) {
              this.file = file;
          }
      
          public File getFile() {
              return file;
          }
      
          public InnerProgressMonitor getProgressMonitor() {
              return pmImpl;
          }
      
          public InnerProgressMonitor getActionMonitor() {
              return pmImpl;
          }
      
      
          public void setFileName(String fileName) {
              this.fileName = fileName;
          }
      
          public String getFileName() {
      
              return fileName;
          }
          
           public String getFilePath() {
                return filePath;
           }
      
           public void setFilePath(String filePath) {
                this.filePath = filePath;
           }
      
          public void setContentType(String contentType) {
              this.contentType = contentType;
          }
      
          public String getContentType() {
              return contentType;
          }
      
          @Remove
          @Destroy
          public void destroy() {
              log.info("Destroying ");
          }
      
      
          public class InnerProgressMonitor implements Serializable {
      
      
              public void progress(EventObject event) {
      
                  com.icesoft.faces.component.inputfile.InputFile file =
                          (com.icesoft.faces.component.inputfile.InputFile) event.getSource();
                  int percent = file.getFileInfo().getPercent();
                  InputFileSaveAction.this.setPercent(percent);
                  InputFileSaveAction.this.setFile(file.getFile());
      
      
                  log.info("Progress - Percent: " + percent);
                  
                              try {
                                  state.execute();
                                  state.render();
                              } catch (RenderingException re ) {
                                  System.out.println("Rendering exception : " + re);
                                  re.printStackTrace();
                              }
      
              }
      
      
              public String action(ActionEvent event) {
      
                  com.icesoft.faces.component.inputfile.InputFile inputFile = (com.icesoft.faces.component.inputfile.InputFile) event.getSource();
                  if (inputFile.getStatus() == com.icesoft.faces.component.inputfile.InputFile.SAVED) {
                      InputFileSaveAction.this.setFileName(inputFile.getFileInfo().getFileName());
                      InputFileSaveAction.this.setContentType(inputFile.getFileInfo().getContentType());
                      InputFileSaveAction.this.setFile(inputFile.getFile());
                      InputFileSaveAction.this.setFilePath(inputFile.getFileInfo().getPhysicalPath());
      
                      log.info("File uploaded: " + inputFile.getFileInfo().getFileName());
                  }
      
                  if (inputFile.getStatus() == com.icesoft.faces.component.inputfile.InputFile.INVALID) {
                      inputFile.getFileInfo().getException().printStackTrace();
                  }
      
                  if (inputFile.getStatus() == com.icesoft.faces.component.inputfile.InputFile.SIZE_LIMIT_EXCEEDED) {
                      inputFile.getFileInfo().getException().printStackTrace();
                  }
      
                  if (inputFile.getStatus() == com.icesoft.faces.component.inputfile.InputFile.UNKNOWN_SIZE) {
                      inputFile.getFileInfo().getException().printStackTrace();
                  }
                  return null;
              }
          }
      
      }
      
      

        • 1. Re: Upload file and insert description in DB
          Jacob SMA Newbie

          I fixed above problem with following changes:


          - In FileInputSaveAction I removed (required=true) on @Out private String filePath = ""


          - Added @Begin on the method public String action(ActionEvent event)


          - In ImageHome @In is now set to @In(required=false) on private String filePath


          Thats nice, but when I have added a photo to the DB and want to add another one, I get an error. I think its a problem with my scope, it should not be session or can anyone tell me something else do to?


          Thank you, regards
          Jacob


          The error:


          Exception during request processing:
          Caused by javax.faces.FacesException with message: "Problem in renderResponse: javax.el.ELException: /image.xhtml 
          @51,58 rendered="#{!imageHome.managed}": Error reading 'managed' on type com.testproject.ImageHome_$$_javassist_2"
          
          com.icesoft.faces.facelets.D2DFaceletViewHandler.renderResponse(D2DFaceletViewHandler.java:324)
          com.icesoft.faces.application.D2DViewHandler.renderView(D2DViewHandler.java:153)
          com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:106)
          com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
          com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:144)
          com.icesoft.faces.webapp.http.core.PageServer$1.respond(PageServer.java:26)
          com.icesoft.faces.webapp.http.servlet.ServletRequestResponse.respondWith(ServletRequestResponse.java:143)
          com.icesoft.faces.webapp.http.servlet.ThreadBlockingAdaptingServlet$ThreadBlockingRequestResponse.respondWith(ThreadBlockingAdaptingServlet.java:36)
          com.icesoft.faces.webapp.http.core.PageServer.service(PageServer.java:31)
          com.icesoft.faces.webapp.http.core.SingleViewServer.service(SingleViewServer.java:46)
          com.icesoft.faces.webapp.http.common.standard.PathDispatcherServer$Matcher.serviceOnMatch(PathDispatcherServer.java:50)
          com.icesoft.faces.webapp.http.common.standard.PathDispatcherServer.service(PathDispatcherServer.java:19)
          com.icesoft.faces.webapp.http.servlet.ThreadBlockingAdaptingServlet.service(ThreadBlockingAdaptingServlet.java:19)
          com.icesoft.faces.webapp.http.servlet.EnvironmentAdaptingServlet.service(EnvironmentAdaptingServlet.java:29)
          com.icesoft.faces.webapp.http.servlet.MainSessionBoundServlet.service(MainSessionBoundServlet.java:109)
          com.icesoft.faces.webapp.http.servlet.SessionDispatcher.service(SessionDispatcher.java:35)
          com.icesoft.faces.webapp.http.servlet.PathDispatcher$Matcher.serviceOnMatch(PathDispatcher.java:52)
          com.icesoft.faces.webapp.http.servlet.PathDispatcher.service(PathDispatcher.java:29)
          com.icesoft.faces.webapp.http.servlet.MainServlet.service(MainServlet.java:98)
          javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
          org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
          org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
          org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
          org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:58)
          org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
          org.jboss.seam.debug.hot.HotDeployFilter.doFilter(HotDeployFilter.java:68)
          org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
          org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:85)
          org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
          org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
          org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
          org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
          org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
          org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
          org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
          org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
          org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
          org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
          org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
          org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
          org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
          org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
          org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:432)
          org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
          org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
          org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
          org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
          org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
          org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
          org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
          org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
          org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
          java.lang.Thread.run(Thread.java:619)
          

          • 2. Re: Upload file and insert description in DB
            judy guglielmin Novice

            Just wondering what version of ICEfaces you are using here....If it isn't 1.7.0.Beta1, you might want to consider trying it. 

            • 3. Re: Upload file and insert description in DB
              Jacob SMA Newbie

              I dont think its the new beta, just the version bundled with seam. Downloaded it one week ago.


              But I will try using that :)

              • 4. Re: Upload file and insert description in DB
                Jacob SMA Newbie

                Now i'm using 1.7.0.Beta1 but I still have the above problem. But I have refactored a lot of the code so its much simpler now than before thanks to the new version.


                Step 1:
                In my photoList.xhtml I press Create new and I'm linked to photo.xhtml. On this page I upload a photo and afterward press Save to save it in the DB. This works fine! Then I press Done and i'm redirected to photoList.xhtml again, where the new photo is listed! SUPER :)


                Step 2:
                Here is the PROBLEM :( Now I press Create new to upload another photo. I'm prompted with this error (Much like the other one):


                Exception during request processing:
                Caused by javax.faces.FacesException with message: "Problem in renderResponse: javax.el.ELException: 
                /photoHome.xhtml @36,61 rendered="#{!imageNewHome.managed}": Error reading 'managed' on type
                com.testproject.ImageNewHome_$$_javassist_2"
                
                com.icesoft.faces.facelets.D2DFaceletViewHandler.renderResponse(D2DFaceletViewHandler.java:298)
                com.icesoft.faces.application.D2DViewHandler.renderView(D2DViewHandler.java:161)
                com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:106)
                com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
                com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:144)
                com.icesoft.faces.webapp.http.core.JsfLifecycleExecutor.apply(JsfLifecycleExecutor.java:18)
                com.icesoft.faces.webapp.http.core.PageServer$1.respond(PageServer.java:25)
                com.icesoft.faces.webapp.http.servlet.ServletRequestResponse.respondWith(ServletRequestResponse.java:161)
                com.icesoft.faces.webapp.http.servlet.ThreadBlockingAdaptingServlet$ThreadBlockingRequestResponse.respondWith(ThreadBlockingAdaptingServlet.java:36)
                com.icesoft.faces.webapp.http.core.PageServer.service(PageServer.java:30)
                com.icesoft.faces.webapp.http.core.SingleViewServer.service(SingleViewServer.java:48)
                com.icesoft.faces.webapp.http.common.standard.PathDispatcherServer$Matcher.serviceOnMatch(PathDispatcherServer.java:50)
                com.icesoft.faces.webapp.http.common.standard.PathDispatcherServer.service(PathDispatcherServer.java:19)
                com.icesoft.faces.webapp.http.servlet.ThreadBlockingAdaptingServlet.service(ThreadBlockingAdaptingServlet.java:19)
                com.icesoft.faces.webapp.http.servlet.EnvironmentAdaptingServlet.service(EnvironmentAdaptingServlet.java:29)
                com.icesoft.faces.webapp.http.servlet.MainSessionBoundServlet.service(MainSessionBoundServlet.java:105)
                com.icesoft.faces.webapp.http.servlet.SessionDispatcher.service(SessionDispatcher.java:37)
                com.icesoft.faces.webapp.http.servlet.PathDispatcher$Matcher.serviceOnMatch(PathDispatcher.java:52)
                com.icesoft.faces.webapp.http.servlet.PathDispatcher.service(PathDispatcher.java:29)
                com.icesoft.faces.webapp.http.servlet.MainServlet.service(MainServlet.java:76)
                javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
                org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
                org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
                org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:58)
                org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                org.jboss.seam.debug.hot.HotDeployFilter.doFilter(HotDeployFilter.java:68)
                org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:85)
                org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
                org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
                org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
                org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
                org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
                org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
                org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
                org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:432)
                org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
                org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
                org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
                org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
                org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
                org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
                org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
                org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
                org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
                java.lang.Thread.run(Thread.java:619)
                
                
                Caused by java.lang.IllegalStateException with message: "EntityManager is closed"
                ...
                ...
                



                My bean look like this:


                import org.jboss.seam.ScopeType;
                import org.jboss.seam.annotations.Begin;
                import org.jboss.seam.annotations.Factory;
                import org.jboss.seam.annotations.Name;
                import org.jboss.seam.annotations.Scope;
                import org.jboss.seam.annotations.web.RequestParameter;
                import org.jboss.seam.framework.EntityHome;
                
                @Name("imageNewHome")
                @Scope(ScopeType.SESSION)
                public class ImageNewHome extends EntityHome<Image>
                {
                    @RequestParameter 
                    Integer imageId;
                    
                    @Factory("imageNewType")
                     public Image initImage() { return getInstance(); }
                    
                    private InputFileBean inputFile;
                    
                    public ImageNewHome() {
                         inputFile = new InputFileBean();
                    }
                    
                    @Override
                    public Object getId() 
                    { 
                        if (imageId==null)
                        {
                            return super.getId();
                        }
                        else
                        {
                            return imageId;
                        }
                    }
                    
                    @Override @Begin
                    public void create() {
                        super.create();
                    }
                
                     @Override
                     public String update() {
                          instance.setPath(getInputFile().getFileServerPath());
                          return super.update();
                     }
                
                     @Override
                     public String persist() {
                          instance.setPath(getInputFile().getFileServerPath());
                          return super.persist();
                     }
                
                     public InputFileBean getInputFile() {
                          return inputFile;
                     }
                
                     public void setInputFile(InputFileBean inputFile) {
                          this.inputFile = inputFile;
                     }
                    
                }
                
                



                And photo.xhtml:


                <ice:form enctype="multipart/form-data">     
                         <ice:panelGrid styleClass="componentLayoutRootTable" columns="1">
                             <ice:panelGroup styleClass="formBorderHighlight" style="text-align:left">
                                 <ice:inputFile progressListener="#{imageNewHome.inputFile.progress}" 
                                 actionListener="#{imageNewHome.inputFile.action}"
                                 file="#{imageNewHome.inputFile.savedFile}"/>
                                             
                                <ice:outputProgress id="pro" value="#{imageNewHome.inputFile.percent}"/>
                               </ice:panelGroup>
                          </ice:panelGrid>
                          
                          
                      </ice:form>
                    <h:outputText id="imgNameId" value="#{imageNewHome.inputFile.fileName}"/>
                     
                     <h:form>
                          <h:commandButton id="editimageTypeHomesave" 
                                          value="Save" 
                                         action="#{imageNewHome.persist}"
                                       rendered="#{!imageNewHome.managed}"/>                   
                 
                            <h:commandButton id="editimageTypeHomeupdate" 
                                          value="Save" 
                                         action="#{imageNewHome.update}"
                                       rendered="#{imageNewHome.managed}"/>                  
                            <h:commandButton id="editimageHomedelete" 
                                          value="Delete" 
                                         action="#{imageNewHome.remove}"
                                      immediate="true"
                                       rendered="#{imageNewHome.managed}"/>
                            <s:button propagation="end" 
                                               id="editimageTypeHomedone" 
                                            value="Done"
                                             view="/imageList.xhtml"/>
                     </h:form>
                




                I think it's because of the Session scope, but I get a 500 error when uploading a file if the scope isn't set to Session. What can I do to close the conversation when I press Save or Done and redirected to photoList.xhtml??


                Any help is appreciated.


                Thanks
                /Jacob

                • 5. Re: Upload file and insert description in DB
                  judy guglielmin Novice

                  I would suggest a Session-scoped bean which would manage the fileupload as well as your EntityHome object (which could be conversation-scoped). You can reuse your imageHome by creating a new Instance of your image and then using setInstance(), in a new conversation.  The error is that the managed property is not found on your EntityHome object.  Once you create a new Image entity, then setInstance to that entity, you can check to see if it's defined, wired or managed in the bean to check that everything's good before you persist, remove, update, etc.


                  ....are you sure you are using 1.7.0.Beta1 version? 

                  • 6. Re: Upload file and insert description in DB
                    Jacob SMA Newbie

                    Hi judy,


                    I have now tried your suggestion, but I still get HTTP Status 500.  I'm using Icefaces 1.7.0.Beta1, Seam 2.0.1.GA running on jboss-4.2.2.GA.


                    My code right now:



                    My PhotoController (Former called ImageHome)


                    @Name("photoController")
                    public class PhotoController extends EntityController {
                        
                         private FileAdminBean fileAdminBean;
                        
                         private String title;
                         private String description;
                         private Date date;
                        
                        public PhotoController() {
                             fileAdminBean = FileAdminBean.getInstance();
                        }
                        
                        public FileAdminBean getFileAdminBean() {
                             return fileAdminBean;
                        }
                        
                        public void saveFiles(ActionEvent e) {
                             // Save file info in DB
                        }
                    
                    .... Some setters and getters to get input from the view.
                    }
                    



                    And my FileAdminBean:


                    @Name("fileAdminBean")
                    @Scope(ScopeType.SESSION)
                    public class FileAdminBean implements Serializable {
                    
                         ...
                         private InputFileBean fileUpload;
                    
                         // Users list of files just uploaded
                         private List<FileEntry> filesList;
                         
                         private static FileAdminBean instance = null;
                         
                         protected FileAdminBean() {
                              init();
                         }
                            
                         public static FileAdminBean getInstance() {
                              // Create new instance
                              if(instance == null) {
                                   instance = new FileAdminBean();
                              // Else clean up the old one
                              } else {
                                   instance.init();
                                   
                              }
                              return instance;
                         }
                    
                    ...
                    }
                    




                    Hope that was enough code else I can upload all of it to you maybe?
                    Are you sure its possible with out setting the scope of PhotoController to Session?



                    Thanks,
                    Jacob

                    • 7. Re: Upload file and insert description in DB
                      judy guglielmin Novice

                      I was thinking that you would inject a PhotoHome object into your FileAdminBean and then when the file is uploaded, you would then have a method that would set the instance to the current file that was uploaded, make sure it was wired properly and then persist it. (perhaps I am not 100% sure of what you are trying to accomplish?).    You could just reuse the same instance of your Home object, but change the instance it is managing. 


                      • 8. Re: Upload file and insert description in DB
                        Jacob SMA Newbie

                        Hi again again Judy :)


                        What I want to accomplish is a general FileAdminBean that I can use every time I want to upload a photo on a web page. For example in a gallery or a single profile picture. Because its session scoped I need to tell the FileAdminBean somehow what kind of upload type i'm doing right now.


                        If its a gallery-type it has to make x Image entries in the DB and one Gallery with a reference to all the images.
                        If its a profile picture I don't need to create a gallery.


                        The code for doing that is already made and seems to work, but i'm not capable to inject anything to the FileAdminBean. Right now i'm trying to inject a enum called FileUploadType. I get this error:

                        Caused by org.jboss.seam.RequiredException with message: @In attribute requires non-null value: fileAdminBean.uploadType


                        My enum class FileUploadType looks like this:


                        public enum FileUploadType {
                             
                             NONE,
                             PROFILE,
                             GALLERY:
                        }
                        



                        I then have a page with two buttons (One for adding photos to the gallery and one for a profile picture) with following actions:


                        <div class="actionButtons">
                             <s:button value="Upload GALLERY" action="#{imageTypeBean.uploadPhoto(1)}"/>
                                    
                             <s:button value="Upload PROFILE" action="#{imageTypeBean.uploadPhoto(2)}"/>
                                            
                        </div>
                        



                        And my ImageTypeBean class is making the @Out injection:


                        @Name("imageTypeBean")
                        public class ImageTypeBean {
                        
                             @Out
                             private FileUploadType uploadType = FileUploadType.NONE;
                        
                             public String uploadPhoto(int i) {
                                 if (i == 1)
                                      uploadType = FileUploadType.GALLERY;
                                 else
                                      uploadType = FileUploadType.PROFILE;
                                      
                                 return "/image.xhtml";
                            }
                        
                        
                        }
                        



                        When I press one of the buttons I'm directed to the image.seam page Where I can upload the files. But as said before, I'm prompted with an error because @In FileUploadType uploadType is null :(


                        The injection code in FileAdminBean looks like this:


                        @Name("fileAdminBean")
                        @Scope(ScopeType.SESSION)
                        public class FileAdminBean implements Serializable {
                        
                            @In
                            private FileUploadType uploadType;
                        
                            public void saveFiles(ActionEvent e) {
                             if (uploadType == FileUploadType.GALLERY) {
                                    ... Code for saving the Gallery and Images in DB
                                } else if (uploadType == FileUploadType.PROFILE) {
                                    ... Code for saving the profile image
                                }
                        
                        }
                        



                        Is this possible with session scope on FileAdminBean?


                        Thanks again,
                        Jacob

                        • 9. Re: Upload file and insert description in DB
                          judy guglielmin Novice

                          Since your FileAdminBean is probably created before you have a chance to set your FileUploadType, you may need


                          @In(create=true)
                          private FileUploadType uploadType;


                          to get an instance of this.  Then, when you select which file to upload, you can set it and continue.  If you have the ImageTypeBean created previously to your FileUploadBean, then the conversation where it has been created (since ImageTypeBean has conversation scope) has to still be the LR conversation.

                          • 10. Re: Upload file and insert description in DB
                            Jason Christian Newbie

                            Do you have an @END to follow your @BEGIN?