6 Replies Latest reply: Aug 23, 2013 6:55 AM by Timo Hahn RSS

    image upload issue ..

    Oracle ITself

      Dear All

       

      i made the solution of image upload using blob domain in one table , using input file, upload button and bean in request scope and image item value used Servlet to retrieve the image "/ImageServletFromDB?id=#{bindings.CompanyId.inputValue}&type=company"

       

      public String uploadImage() { // Dynamic

       

      UploadedFile myfile = (UploadedFile) this.getMyFile();
      if (getMyFile() != null) {
      mybean.AccessIteratorBinding(IteratorName).getDataControl().commitTransaction();

       

      if (getMyFile().getFilename().substring(getMyFile().getFilename().lastIndexOf(".") + 1,
      getMyFile().getFilename().length()).equalsIgnoreCase("jpg") ||
      getMyFile().getFilename().substring(getMyFile().getFilename().lastIndexOf(".") + 1,
      getMyFile().getFilename().length()).equalsIgnoreCase("png")) {
      mybean.AccessAttribute(AttName).setInputValue(createBlobDomain(myfile));
      mybean.AccessIteratorBinding(IteratorName).getDataControl().commitTransaction();
      mybean.RefreshItem(getCompanyLogo());

       

      } else {
      mybean.Validate(mybean.getFacesContext(),
      mybean.AccessBundleItemValue("IMS.view.IMS-InterfaceBundle", "ValidatorMessageHeader4"),
      mybean.AccessBundleItemValue("IMS.view.IMS-InterfaceBundle", "ValidatorMessageFooter4"),
      1);
      }
      } else {
      mybean.Validate(mybean.getFacesContext(),
      mybean.AccessBundleItemValue("IMS.view.IMS-InterfaceBundle", "ValidatorMessageHeader5"),
      mybean.AccessBundleItemValue("IMS.view.IMS-InterfaceBundle", "ValidatorMessageFooter5"), 1);
      }
      return null;
      // Wael Abdeen
      }

       

      private BlobDomain createBlobDomain(UploadedFile file) {

       

      InputStream in = null;
      BlobDomain blobDomain = null;
      OutputStream out = null;

       

      try {
      in = file.getInputStream();

       

      blobDomain = new BlobDomain();
      out = blobDomain.getBinaryOutputStream();
      byte[] buffer = new byte[8192];
      int bytesRead = 0;

       

      while ((bytesRead = in.read(buffer, 0, 8192)) != -1) {
      out.write(buffer, 0, bytesRead);
      }

       

      in.close();

       

      } catch (IOException e) {
      e.printStackTrace();
      } catch (SQLException e) {
      e.fillInStackTrace();
      }

       

      return blobDomain;
      }

       

      solution is working fine except one thing .. when press upload button, it workes fine but to see the new result in image item "i have to execute the iterator" , refresh on insert & update and ppr didn't give the result !!

      Now when i upload image in record no 5 , execute return back to record no 1, how to enhance my method above to fix this issue !!

      thanks ..

        • 1. Re: image upload issue ..
          Oracle ITself

          Any update ??

          • 2. Re: image upload issue ..
            kdario

            You will need to restore current row after refresh.

            For example: ADF Code Bits: Bit #23 - Using findAndSetCurrentRowByKey() to set the View Object currency

             

            Dario

            • 3. Re: image upload issue ..
              Timo Hahn

              May I ask why you call commit twice in your code?

              Dario's solution is OK.

              I only wonder why you call commit before setting the value (which is potential dangerous as the row you try to update could have be changes by somebody else) and after setting the value?

               

              Timo

              • 4. Re: image upload issue ..
                Oracle ITself

                Thanks Dario & timo,

                 

                sorry i forgot to remove duplicated commit, now i enhanced my VO with Dario Query and call it in upload process .. i made debug making break point inside the servlet ..

                i find that when choosing image and press upload, "Servlet doesn't resfreh the image", it didn't go inside Servlet completly however i execute query using Dairo requery method.

                Question now, why upload button doesn't activate the servlet to refresh the image item ?

                 

                public void requery() {
                      Row currentRow = getCurrentRow();
                      Key currentRowKey = currentRow.getKey();
                      int rangePosOfCurrentRow = getRangeIndexOf(currentRow);
                      int rangeStartBeforeQuery = getRangeStart();
                      executeQuery();
                      setRangeStart(rangeStartBeforeQuery);
                      findAndSetCurrentRowByKey(currentRowKey,rangePosOfCurrentRow);

                }

                 

                 

                 

                public String uploadImage() { // Dynamic
                    UploadedFile myfile = (UploadedFile) this.getMyFile();
                    if (getMyFile() != null) {

                 

                        if (getMyFile().getFilename().substring(getMyFile().getFilename().lastIndexOf(".") + 1,
                                                                getMyFile().getFilename().length()).equalsIgnoreCase("jpg") ||
                            getMyFile().getFilename().substring(getMyFile().getFilename().lastIndexOf(".") + 1,
                                                                getMyFile().getFilename().length()).equalsIgnoreCase("png")) {
                            mybean.AccessAttribute(AttName).setInputValue(createBlobDomain(myfile));
                            //String myKey = mybean.AccessAttribute("CompanyId").toString();
                            mybean.AccessOperation("Commit").execute();
                            mybean.AccessOperation("requery").execute();
                            mybean.RefreshItem(getCompanyLogo());
                        } else {
                            mybean.Validate(mybean.getFacesContext(),
                                            mybean.AccessBundleItemValue("IMS.view.IMS-InterfaceBundle", "ValidatorMessageHeader4"),
                                            mybean.AccessBundleItemValue("IMS.view.IMS-InterfaceBundle", "ValidatorMessageFooter4"),
                                            1);
                        }
                    } else {
                        mybean.Validate(mybean.getFacesContext(),
                                        mybean.AccessBundleItemValue("IMS.view.IMS-InterfaceBundle", "ValidatorMessageHeader5"),
                                        mybean.AccessBundleItemValue("IMS.view.IMS-InterfaceBundle", "ValidatorMessageFooter5"), 1);
                    }
                    return null;
                    // Wael Abdeen
                }

                 

                i don't know why

                • 5. Re: image upload issue ..
                  kdario

                  I suppose that: mybean.RefreshItem(getCompanyLogo());  internally invokes: AdfFacesContext.getCurrentInstance().addPartialTarget(yourImageComponent);  ?

                  You can try to refresh page(just to see if there is any difference):

                   

                  private void refreshPage() {

                     FacesContext fctx = FacesContext.getCurrentInstance();

                     String refreshpage = fctx.getViewRoot().getViewId();

                     ViewHandler ViewH = fctx.getApplication().getViewHandler();

                     UIViewRoot UIV = ViewH.createView(fctx, refreshpage);

                     UIV.setViewId(refreshpage);

                     fctx.setViewRoot(UIV);

                  }

                   

                  Dario

                  • 6. Re: image upload issue ..
                    Timo Hahn

                    Normally you don't have to refresh the whole page. The container the image is in should be enough.

                    One more thing which might prevent the image to refresh is caching. You can add a dummy parameter to the url for the image

                    Date d = new Date();

                    String url  = "/ImageServletFromDB?id={bindings.companyid.inputvalue}&type=company&x="+ d.getTime();

                    This why the browser can't cache the call to the url and has to call it again.

                     

                    Timo