2 Replies Latest reply on Jun 2, 2009 10:56 AM by sparkettin

    A few beginner questons.

    sparkettin

      Hello,
      In my case there is a course, and several documents related to course which I want to attach. The document have name, date and keywords. After I write them, I use rich:upload to temporaliy keep tthe document to be attached. When I finish uploading it, I want a blank interface to enter data for the next document. I use <a4j:support event="onuploadcomplete" reRender="panel, panel1" /><a4j:support event="onuploadcomplete" reRender="panel, panel1" /> to get a blank text and date box. this helps me to clear the data table which I keep the key words. but h:inputText and rich:calendar areas are always there with previous values. Whatever I do they are always there. I even tried ajax commandButton to rerender those fields. But they are never blank.
      The other problem is I want date in dd.MM.yyyy format. When I save them there is hour and minutes. I do not want to see them, why they are there? The worse, in the data table I see temporaliy kept document, date is in a very different format (sometimes long date sometimes it is like 09 jun 2009). Why I can not see in my datatable in the way I want it? My code is below. I'd appreciate a lot If you help me. Thanks... (I use RF 3.3.1 with spring and hibernate. Sorry for the code junk:( )

      course object:

      package course.model.temel;
      
      import java.io.Serializable;
      import java.util.ArrayList;
      import java.util.Date;
      import java.util.List;
      
      import javax.persistence.CascadeType;
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.OneToMany;
      import javax.persistence.Table;
      
      import course.model.ortak.Dokuman;
      
      
      @Entity
      @Table(name = "COURSE")
      public class Course implements Serializable {
       /**
       *
       */
       private static final long serialVersionUID = 1562265533130786503L;
      
       @Id
       @GeneratedValue(strategy = GenerationType.AUTO)
       @Column(name = "COURSE_ID")
       private Integer id;
      
       @Column(name = "name")
       private String name;
       private Date startDate;
       private Date endDate;
      
       @OneToMany(mappedBy = "course", cascade = CascadeType.ALL)
       private List<Dokuman> documents = new ArrayList<Dokuman>();
      
       @Override
       public int hashCode() {
      
       final int prime = 31;
       int result = 1;
       result = prime * result + ((id == null) ? 0 : id.hashCode());
       return result;
       }
      
       @Override
       public boolean equals(Object obj) {
      
       if (this == obj)
       return true;
       if (obj == null)
       return false;
       if (getClass() != obj.getClass())
       return false;
       Course other = (Course) obj;
       if (id == null) {
       if (other.id != null)
       return false;
       } else if (!id.equals(other.id))
       return false;
       return true;
       }
      
       public Course() {
       }
      
       gettes and setters
      }


      document object
      package course.model.ortak;
      
      import java.io.Serializable;
      import java.util.Date;
      
      import javax.persistence.CascadeType;
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.JoinColumn;
      import javax.persistence.JoinTable;
      import javax.persistence.ManyToOne;
      import javax.persistence.Table;
      
      
      import org.hibernate.annotations.Parameter;
      
      import course.model.temel.Course;
      
      
      @Entity
      @Table(name="DOCUMENT")
      public class Dokuman implements Serializable{
      
       /**
       *
       */
       private static final long serialVersionUID = 8497390464772048029L;
      
       @Id
       @GeneratedValue(strategy = GenerationType.AUTO)
       @Column(name="DOCUMENT_ID")
       private Integer id;
      
       private Date date;
       private String name;
       private String keyWords;
      
       @ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
       @JoinTable(name = "DOCUMENT",
       inverseJoinColumns = @JoinColumn(name = "COURSE_ID", referencedColumnName = "COURSE_ID"),
       joinColumns = @JoinColumn(name = "DOCUMENT_ID", referencedColumnName = "DOCUMENT_ID"))
       private Course course;
      
      
       @Override
       public int hashCode() {
       final int prime = 31;
       int result = 1;
       result = prime * result + ((id == null) ? 0 : id.hashCode());
       return result;
       }
      
      
       @Override
       public boolean equals(Object obj) {
       if (this == obj)
       return true;
       if (obj == null)
       return false;
       if (getClass() != obj.getClass())
       return false;
       Dokuman other = (Dokuman) obj;
       if (id == null) {
       if (other.id != null)
       return false;
       } else if (!id.equals(other.id))
       return false;
       return true;
       }
      
      
       public Dokuman(){}
      
      getters and setters
      
      }


      my controller
      package course.controller.temel;
      
      import java.util.ArrayList;
      import java.util.Date;
      import java.util.Iterator;
      import java.util.List;
      
      import javax.faces.model.ListDataModel;
      
      
      import org.richfaces.event.UploadEvent;
      import org.richfaces.model.UploadItem;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.context.annotation.Scope;
      import org.springframework.stereotype.Controller;
      
      import course.model.ortak.Dokuman;
      import course.model.temel.Course;
      import course.service.temel.CourseService;
      
      
      @Controller("courseController")
      @Scope("session")
      public class CourseController {
      
       private CourseService courseService;
       private ListDataModel list;
       private boolean kontrol = false;
       private String keyWord;
       private String documentKeyWord;
       private List<String> keyWords;
       private Course course;
       private boolean disabled;
       private boolean autoclear;
       private boolean rendered;
       private boolean required;
       private String requiredMessage;
       private String listHeight;
       private boolean immediateUpdate;
       private String listWidth;
       private String addControlLabel;
       private String clearAllControlLabel;
       private String clearControlLabel;
       private String stopEntryControlLabel;
       private String uploadControlLabel;
       private Dokuman document;
       private Integer maxFilesQuantity;
      
       private String documentName;
       private Date documentDate;
      
       private StringBuilder sb;
      
      
       public CourseController() {
      
       disabled = false;
       autoclear = true;
       rendered = true;
       required = false;
       immediateUpdate = true;
       requiredMessage = "requiredMessage";
       listHeight = "50px";
       listWidth = "300px";
       addControlLabel = "Tara";
       clearAllControlLabel = "Hepsini Temizle";
       clearControlLabel = "Temizle";
       stopEntryControlLabel = "Durdur";
       uploadControlLabel = "Y�kle";
       maxFilesQuantity = 1;
      
       this.keyWord = new String();
       this.keyWords = new ArrayList<String>();
       this.documentName = new String();
       this.course = new Course();
       this.list = new ListDataModel();
       this.document = new Dokuman();
       this.keyWord = new String();
       this.documentDate = new Date();
      
       }
      
       /*
       * Here I save my object. The areas are cleared only when
       * I run clear method
       */
       public String save() {
      
       courseService.courseSave(course);
       clear();
       return "tamam";
       }
      
       /*Here I store key words to be added
       *
       */
       public void keyWordAdd() {
      
       this.keyWords.add(keyWord);
       keyWord = "";
       }
      
       /*
       * In case to erase key words before saving
       */
       public void keyWordErase() {
      
       ListDataModel list = new ListDataModel();
       list.setWrappedData(this.keyWords);
       String kelime = (String) list.getRowData();
       this.keyWords.remove(kelime);
       }
      
       /*
       * In case to remove an attached document before saving
       */
       public void removeAttachedDocument() {
      
       ListDataModel list = new ListDataModel();
       list.setWrappedData(this.course.getDocuments());
       Dokuman document = (Dokuman) list.getRowData();
      
       this.course.getDocuments().remove(document);
      
       }
      
       /*
       * This is the only way I clear my interface. But everythin is null.
       */
       public void clear() {
      
       this.keyWord = new String();
       this.keyWords = new ArrayList<String>();
       this.documentName = new String();
       this.course = new Course();
       this.list = new ListDataModel();
       this.document = new Dokuman();
       this.keyWord = new String();
       this.documentDate = new Date();
      
       }
      
       /*
       * This place kills me. I wat to see a all boxes clear to enter a new document
       * information. whatever I do, document date and document name is still there.
       */
       public void fileUploadListener(UploadEvent event) throws Exception {
      
       UploadItem item = event.getUploadItem();
      
       if (item != null && item.getData() != null) {
      
       documentKeyWord();
       this.document.setDate(documentDate);
       this.document.setName(documentName);
       this.course.getDocuments().add(this.document);
       //this.documentDate = new Date();
       //this.documentName = new String();
       this.documentDate = null;
       this.documentName="";
       this.keyWord = new String();
       this.keyWords = new ArrayList<String>();
       this.document = new Dokuman();
       }
      
       }
      
       /*just trying to transform string array to one string
       * I want to keep keywords as a string line in db
       */
       private void documentKeyWord() {
      
       Iterator<String> i = this.keyWords.iterator();
       String s;
       String key;
      
       if (i.hasNext()) {
       s = i.next();
       key = new String(s);
       while (i.hasNext()) {
       s = i.next();
       key = key + "; " + s;
       }
       this.document.setKeyWords(key);
       }
      
       }
      
      getters and setters
      
      }


      my jsp page
      <html>
      <%@ page contentType="text/html; charset=ISO-8859-9"%>
      <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
      <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
      <%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
      <%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>
      
      <head>
      </head>
      
      <f:view>
       <h:form id="myForm">
       <center><rich:tabPanel switchType="client"
       style="width:400px;">
       <rich:tab label="Course Info">
       <h:panelGrid columns="1">
       <rich:panel style="height:245px;width:375px;">
       <h:panelGrid columns="2">
      
       <h:outputLabel value="Course Name :" />
       <h:inputText value="#{courseController.course.name}"
       style="width:231px;" />
      
       <h:outputLabel value="Start Date :" />
       <rich:calendar value="#{courseController.course.startDate}"
       locale="tr/TR" datePattern="dd.MM.yyyy" cellWidth="width:231px"
       cellHeight="22px" style="width:200px" />
      
       <h:outputLabel value="End Date :" />
       <rich:calendar value="#{courseController.course.endDate}"
       locale="tr/TR" datePattern="dd.MM.yyyy" cellWidth="24px"
       cellHeight="22px" style="width:200px" />
       </h:panelGrid>
       </rich:panel>
       </h:panelGrid>
       </rich:tab>
      
       <rich:tab label="Documents">
       <h:panelGrid columns="2" id="panel">
       <rich:panel style="height:304px;width:375px;">
       <h:panelGrid columns="2">
      
       <h:outputLabel value="Document Name :" />
       <h:inputText id="dokumanAditxt"
       value="#{courseController.documentName}" style="width:150px;" />
       <h:outputLabel value="Doument Date :" />
       <rich:calendar locale="tr/TR" id="dokumanTarihitxt"
       value="#{courseController.documentDate}"
       datePattern="dd.MM.yyyy" cellWidth="170px" cellHeight="22px"
       style="width:145px;" />
      
       <h:outputLabel value="Key Word :" />
       <a4j:outputPanel>
       <h:inputText id="anahtarKelimeGirisitxt"
       value="#{courseController.keyWord}" style="width:150px" />
       <a4j:commandButton id="eklemeBtn"
       action="#{courseController.keyWordAdd}"
       reRender="dt2, anahtarKelimeGirisitxt"
       image="../../images/img/Plus1png.png" />
       <rich:toolTip for="eklemeBtn" value="Add" />
       </a4j:outputPanel>
       </h:panelGrid>
       </rich:panel>
      
       <rich:panel style="width:375px; height : 304px;">
       <center><h:panelGrid columns="1">
       <rich:dataTable id="dt2" value="#{courseController.keyWords}"
       var="kelime" border="1"
       onRowMouseOver="this.style.backgroundColor='efdc3b'"
       onRowMouseOut="this.style.backgroundColor='F1F1F1'"
       align="center" rules="all" rows="10" width="100%">
      
       <f:facet name="header">
       <h:outputText value="Key Words" />
       </f:facet>
      
       <rich:column sortBy="#{kelime}" filterBy="#{kelime}"
       filterEvent="onkeyup">
       <f:facet name="header">
       <h:outputText value="Kelime" />
       </f:facet>
       <h:outputText value="#{kelime}"></h:outputText>
       </rich:column>
      
       <rich:column>
       <f:facet name="header">
       </f:facet>
       <table width="100%">
       <tr align="center">
       <td><a4j:commandLink
       action="#{courseController.keyWordErase}"
       immediate="true" id="deleteLink"
       onclick="if (!confirm('Sure?')) return false;"
       reRender="dt2">
       <h:graphicImage url="../../images/img/delete.gif"
       style="text-align:center;border:0; height : 16px;"></h:graphicImage>
       </a4j:commandLink> <rich:toolTip for="deleteLink" value="Delete" /></td>
       </tr>
       </table>
       </rich:column>
       </rich:dataTable>
      
       <h:outputLabel value="Upload Document :" />
       <rich:fileUpload id="upload"
       disabled="#{courseController.disabled}"
       autoclear="#{courseController.autoclear}"
       required="#{courseController.required}"
       requiredMessage="#{courseController.requiredMessage}"
       rendered="#{courseController.rendered}"
       listHeight="#{courseController.listHeight}"
       listWidth="#{courseController.listWidth}"
       fileUploadListener="#{courseController.fileUploadListener}"
       maxFilesQuantity="#{courseController.maxFilesQuantity}"
       immediateUpload="#{courseController.immediateUpdate}"
       addControlLabel="#{courseController.addControlLabel}"
       clearAllControlLabel="#{courseController.clearAllControlLabel}"
       clearControlLabel="#{courseController.clearControlLabel}"
       stopEntryControlLabel="#{courseController.stopEntryControlLabel}"
       uploadControlLabel="#{courseController.uploadControlLabel}"
       stopControlLabel="Stop">
       <a4j:support event="onuploadcomplete" reRender="panel, panel1" />
       <f:facet name="progress">
       <rich:progressBar style="height: 10px; width: 250px;">
       </rich:progressBar>
       </f:facet>
       </rich:fileUpload>
       </h:panelGrid></center>
       </rich:panel>
       </h:panelGrid>
      
       <center><h:panelGrid columns="1">
       <rich:panel style="width:375px;" id="panel1">
       <rich:dataTable id="dt1"
       value="#{courseController.course.documents}" var="dokuman"
       border="1" onRowMouseOver="this.style.backgroundColor='efdc3b'"
       onRowMouseOut="this.style.backgroundColor='F1F1F1'"
       align="center" rules="all" rows="10" width="100%">
      
       <f:facet name="header">
       <h:outputText value="Attached Documents" />
       </f:facet>
      
       <rich:column sortBy="#{dokuman.name}" filterBy="#{dokuman.name}"
       filterEvent="onkeyup">
       <f:facet name="header">
       <h:outputText value="Doküman Adı" />
       </f:facet>
       <h:outputText value="#{dokuman.name}"></h:outputText>
       </rich:column>
      
       <rich:column sortBy="#{dokuman.date}" filterBy="#{dokuman.date}"
       filterEvent="onkeyup">
       <f:facet name="header">
       <h:outputText value="Doküman Tarihi" />
       </f:facet>
       <h:outputText value="#{dokuman.date}"></h:outputText>
       </rich:column>
      
       <rich:column>
       <f:facet name="header">
       <h:outputText value="Key Words" />
       </f:facet>
       <h:outputText value="#{dokuman.keyWords}"></h:outputText>
       </rich:column>
      
       <h:column>
       <f:facet name="header">
       </f:facet>
       <table width="100%">
       <tr align="center">
       <td><a4j:commandLink
       action="#{courseController.removeAttachedDocument}"
       immediate="true" id="deleteLink"
       onclick="if (!confirm('Sure?')) return false;" reRender="dt1">
       <h:graphicImage url="../../images/img/delete.gif"
       style="text-align:center;border:0;"></h:graphicImage>
       </a4j:commandLink> <rich:toolTip for="deleteLink" value="Delete" /></td>
       </tr>
       </table>
       </h:column>
       </rich:dataTable>
       <rich:datascroller for="dt1" align="center"></rich:datascroller>
       </rich:panel>
       </h:panelGrid></center>
       </rich:tab>
       </rich:tabPanel>
      
       <table align="center" width="450px">
       <tr align="right">
       <h:commandButton action="#{courseController.save}" id="kaydet"
       value="Save"></h:commandButton>
       </tr>
       </table>
       </center>
       </h:form>
      </f:view>
      </html>