私は JSF (Primefaces) を使用してプロジェクトを行っています。Google データストアからデータをロードし、p:dataTable に表示します。削除と編集の関数を使用します。ただし、マージを使用して編集すると新しいレコードが挿入されますが、削除は機能せず、エラー メッセージも表示されません。以下の私のコード。
JSFページ
<h:form id="applicationsForm">
<p:growl id="ngrowl" sticky="true" showDetail="true"/>
<p:dataTable id="allEventsTable" var="events" value="#`{EventsMngtBean.events}">`
<p:column style="text-align: left; white-space: normal; width: 90%" headerText="Event Title">
<h:outputText value="#{events.eventTitle}" />
</p:column>
<p:column style="text-align: left; white-space: normal; width: 10%" headerText="Posted">
<h:outputText value="#{events.eventPostDate}">
<f:convertDateTime type="date" dateStyle="short" pattern="dd/MM/yyyy" />
</h:outputText>
</p:column>
<p:column style="width:4%">
<p:commandButton id="editButton" update=":memDetailForm" oncomplete="appDialog.show()" value="Edit" title="Edit">
<f:setPropertyActionListener value="#{events}" target="#{EventsMngtBean.selectedEvent}" />
</p:commandButton>
<p:commandButton id="showDialogButton" value="Delete" oncomplete="confirmation.show()" ajax="true" update=":delForm:confirmDialog">
<f:setPropertyActionListener target="#{EventsMngtBean.selectedEvent}" value="#{events}" />
</p:commandButton>
</p:column>
</p:dataTable>
</h:form>
<h:form id="delForm">
<p:growl id="growl" sticky="true" showDetail="true"/>
<p:confirmDialog id="confirmDialog" message="Are you sure to delete this event (#{EventsMngtBean.selectedEvent.eventTitle})?"
header="Delete Event" severity="alert" widgetVar="confirmation">
<p:commandButton id="confirm" value="Yes" update="growl" oncomplete="confirmation.hide()"
actionListener="#{EventsMngtBean.deleteEvent}" />
<p:commandButton id="decline" value="No" onclick="confirmation.hide()" type="button" />
</p:confirmDialog>
</h:form>
<h:form id="memDetailForm">
<p:dialog header="Event Details!" widgetVar="appDialog" resizable="false" id="stdappDlg"
showEffect="fade" hideEffect="slide" modal="true">
<p:scrollPanel mode="native" style="width: 900px; height: 550px;">
<p:panelGrid columns="2" id="display" style="margin:0 auto; padding: 5px;">
<p:column>
<p:outputLabel value="Event Title!" />
</p:column>
<p:column>
<p:inplace editor="true">
<p:inputText size="50" value="#{EventsMngtBean.selectedEvent.eventTitle}" />
</p:inplace>
</p:column>
<p:column>
<p:outputLabel value="Event Date!" />
</p:column>
<p:column>
<p:inplace editor="true">
<p:calendar value="#{EventsMngtBean.selectedEvent.eventDate}" />
</p:inplace>
</p:column>
<p:column>
<p:outputLabel value="Event Details!" />
</p:column>
<p:column>
<p:editor value="#{EventsMngtBean.selectedEvent.eventDetails.value}" />
</p:column>
<p:column>
<p:outputLabel value="Event Location!" />
</p:column>
<p:column>
<p:inplace editor="true">
<p:inputText size="50" value="#{EventsMngtBean.selectedEvent.eventLocation}" />
</p:inplace>
</p:column>
<p:column>
<p:outputLabel value="Posting Time!" />
</p:column>
<p:column>
<p:outputLabel value="#{EventsMngtBean.selectedEvent.eventPostDate}">
<f:convertDateTime type="date" dateStyle="short" pattern="dd/MM/yyyy hh:mm" />
</p:outputLabel>
</p:column>
<p:column style="width:4%">
<p:commandButton id="saveButton" update=":memDetailForm" oncomplete="appDialog.show()" value="Save Changes" title="Save">
<f:setPropertyActionListener value="#{events}" target="#{EventsMngtBean.selectedEvent}" />
</p:commandButton>
</p:column>
</p:panelGrid>
</p:scrollPanel>
</p:dialog>
</h:form>
JSFビーン
/*
* このテンプレートを変更するには、[ツール] | [ツール] を選択します。テンプレート * エディターでテンプレートを開きます。*/
package com.wayah.nsa.admin.jsfbeans;
import com.google.appengine.api.datastore.Text;
import com.wayah.nsa.dbo.Event;
import com.wayah.nsa.dbo.providerimpl.EMFProvider;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.persistence.EntityManager;
import javax.persistence.Query;
/**
*
* @author MUHAMMAD
*/
public class EventsMngtBean {
private Event selectedEvent = new Event();
private List<Event> events = new ArrayList();
private String value;
/** Creates a new instance of EventsMngtBean */
public EventsMngtBean() {
}
public List<Event> getEvents() {
return events;
}
public String getValue() {
value = selectedEvent.getEventDetails().getValue();
return value;
}
public void setValue(String value) {
selectedEvent.setEventDetails(new Text(value));
this.value = value;
}
public void setEvents(List<Event> events) {
this.events = events;
}
public Event getSelectedEvent() {
return selectedEvent;
}
public void setSelectedEvent(Event selectedEvent) {
this.selectedEvent = selectedEvent;
}
@PostConstruct
public void getAllEvents(){
EntityManager em = EMFProvider.get().createEntityManager();
Query query = em.createQuery("Select e From Events e");
events = query.getResultList();
}
public void deleteEvent(ActionEvent ae){
try{
EntityManager em = EMFProvider.get().createEntityManager();
System.out.println("EM: "+ em.isOpen());
em.getTransaction().begin();
Event ev = new Event();
ev.setEventDate(selectedEvent.getEventDate());
ev.setEventDetails(selectedEvent.getEventDetails());
ev.setEventLocation(selectedEvent.getEventLocation());
ev.setEventPostDate(selectedEvent.getEventPostDate());
ev.setEventTitle(selectedEvent.getEventTitle());
ev.setKey(selectedEvent.getKey());
em.remove(ev);
em.getTransaction().commit();
//getAllEvents();
FacesMessage msg = new FacesMessage("Deleted", "Event Deleted");
FacesContext.getCurrentInstance().addMessage(null, msg);
}catch(Exception e){
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error when deleting event!", null);
FacesContext.getCurrentInstance().addMessage(null, msg);
e.printStackTrace();
}
}
public void saveChanges(ActionEvent ae){
try{
EntityManager em = EMFProvider.get().createEntityManager();
em.getTransaction().begin();
Event ev = new Event();
ev.setEventDate(selectedEvent.getEventDate());
ev.setEventDetails(selectedEvent.getEventDetails());
ev.setEventLocation(selectedEvent.getEventLocation());
ev.setEventPostDate(new Date());
ev.setEventTitle(selectedEvent.getEventTitle());
ev.setKey(selectedEvent.getKey());
em.persist(ev);
em.getTransaction().commit();
FacesMessage msg = new FacesMessage("Saved", "Changes Saved!");
FacesContext.getCurrentInstance().addMessage(null, msg);
}catch(Exception e){
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error when updating event!", null);
FacesContext.getCurrentInstance().addMessage(null, msg);
e.printStackTrace();
}
}
}
エンティティ
@Entity(name="Events")
public class Event implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Key keys;
private String eventTitle;
private Text eventDetails;
private String eventLocation;
@Temporal(javax.persistence.TemporalType.DATE)
private Date eventDate;
@Temporal(javax.persistence.TemporalType.DATE)
private Date eventPostDate;
public Key getKey() {
return keys;
}
public void setKey(Key keys) {
this.keys = keys;
}
public Date getEventDate() {
return eventDate;
}
public void setEventDate(Date eventDate) {
this.eventDate = eventDate;
}
public Text getEventDetails() {
return eventDetails;
}
public void setEventDetails(Text eventDetails) {
this.eventDetails = eventDetails;
}
public String getEventLocation() {
return eventLocation;
}
public void setEventLocation(String eventLocation) {
this.eventLocation = eventLocation;
}
public Date getEventPostDate() {
return eventPostDate;
}
public void setEventPostDate(Date eventPostDate) {
this.eventPostDate = eventPostDate;
}
public String getEventTitle() {
return eventTitle;
}
public void setEventTitle(String eventTitle) {
this.eventTitle = eventTitle;
}
@Override
public int hashCode() {
int hash = 0;
hash += (keys != null ? keys.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Event)) {
return false;
}
Event other = (Event) object;
if ((this.keys == null && other.keys != null) || (this.keys != null && !this.keys.equals(other.keys))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.wayah.nsa.dbo.Event[key=" + keys + "]";
}