0 Replies Latest reply on Oct 15, 2009 10:37 AM by Adam Mscisz

    weird datatable rerender behaviour

    Adam Mscisz Newbie

      Hi there.
      I'm experiencing weird behavior and maybe someone could help me.

      I've got one page on which I use file upload and above it I list uploaded pictures with some more data.

      <rich:dataTable id="fileList" value="#{placeController.userFiles}" var="file"
      onRowMouseOver="this.style.backgroundColor='#F1F1F1'"
      onRowMouseOut="this.style.backgroundColor='# {a4jSkin.tableBackgroundColor}'" cellpadding="0" cellspacing="0" width="300" border="0">
      <f:facet name="header">
      <h:outputText value="Twoje pliki" />
      </f:facet>
      <rich:column width="50px">
      <a4j:mediaOutput element="img" mimeType="image/jpeg"
      createContent="#{placeController.paint}"
      value="#{file.tbFileNameAndPath}">
      </a4j:mediaOutput>
      </rich:column>
      <rich:column width="200px">
      <f:facet name="header">
      <h:outputText value="Description" />
      </f:facet>
      <h:inputText value="#{file.description}"/>
      <h:commandButton value="Save comment" action="#{placeController.saveComment}"/>
      </rich:column>
      <rich:column width="200px">
      <h:commandButton value="delete photo" action="#{placeController.deletePhoto}"/>
      </rich:column>
      </rich:dataTable>

      <rich:fileUpload fileUploadListener="#{placeController.fileUploadListener}"
      maxFilesQuantity="100"
      id="upload"
      cancelEntryControlLabel="Anuluj"
      clearAllControlLabel="Wyczysz liste"
      clearControlLabel="Usun z listy"
      stopEntryControlLabel="Zatrzymaj"
      addControlLabel="Dodaj plik/i"
      uploadControlLabel="Zapisz plik/i"
      immediateUpload="false"
      listWidth="500px"
      listHeight="250px"
      acceptedTypes="jpg, gif, png" allowFlash="true">
      <a4j:support event="onuploadcomplete" reRender="info,mess2" />
      </rich:fileUpload>

      In my PlaceController I have a collection of UploadedFiles, which contain saved file path and photo description.

      private DataModel userFiles; with getter and setter

      This is my listener:
      public void fileUploadListener(UploadEvent event) {
      UploadItem item = event.getUploadItem();
      if (item == null) {
      System.out.println("----item null");
      } else {
      try {
      FileSaver fileSaver = new FileSaver();
      UploadedFileInfo fileInfo= new UploadedFileInfo();
      String filePath ="/Users/adam/devproject/workspace/photos/"+place.getId();
      fileInfo.setPath(filePath);
      fileSaver.storeFile(item.getFile(), item.getFileSize(), fileInfo);
      UploadedFile uf = new UploadedFile();
      uf.setFilePath(fileInfo.getPath());
      uf.setFileName(fileInfo.getName());
      uf.setTbFilePath(fileInfo.getTbPath());
      uf.setTbFileName(fileInfo.getTbName());
      uf.setUser(userService.findUserById(1l));
      uf.setPlace(place);
      uf.setCreateDate(new Date());
      fileUploadService.saveFile(uf);
      } catch (Exception e) {
      e.printStackTrace();
      } finally {
      //userFiles = new ListDataModel(fileUploadService.getUploadedFilesByPlace(place.getId());
      }
      }
      }

      And now the problem.

      As you can see few rows above there's commented line:
      //userFiles = new ListDataModel(fileUploadService.getUploadedFilesByPlace(place.getId());
      as I wanted to get the newly uploadedFiles list after I add new file

      As you can see in dataTable I iterate over Uploaded files and use inputTextArea to display file description with option of saving it.

      Now - the problem.
      Assuming scenario:
      1) I upload 1 file
      2) add description to it and save the comment, which is done by
      public String saveComment() {
      if (userFiles != null && userFiles.isRowAvailable()) {
      UploadedFile uploadedFile= (UploadedFile) userFiles.getRowData();
      fileUploadService.saveFile(uploadedFile);
      }
      return "";
      }
      3) upload new file/files

      When I retrieve uploaded files list in my finally block, some of newly added pictures get the description field set to the same value as description from first uploaded photo - not in database just on xhtml page. It's weirs as if I iterate in my controller ove the retrieved uploaded files list only one field has this description, other files have null value there.

      Now, when I move the retrieval code to getters it works fine:
      public DataModel getUserFiles() {
      //FIXME - shouldn't be here but in listener
      if(place != null && place.getId()!=null)
      userFiles = new ListDataModel(fileUploadService.getUploadedFilesByPlace(place.getId()));
      return userFiles;
      }

      But the problem is that getters is invoked multiple times and don't want to hit db so many times.

      Does anyone have the idea of what can cause such weird behavior?
      Will be very grateful for any ideas.

      Thanks.
      Adam