BrightSide Workbench Full Report + Source Code
JpaGrid.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.jpa.grid;
19 
20 import org.turro.jpa.Dao;
21 import org.turro.zkoss.grid.EditableGrid;
22 import org.zkoss.zk.ui.Component;
23 import org.zkoss.zk.ui.ext.AfterCompose;
24 import org.zkoss.zul.Row;
25 import org.zkoss.zul.Rows;
26 
31 public abstract class JpaGrid<V> extends EditableGrid<V> implements AfterCompose {
32 
33  private Dao dao;
34  private String query;
35  private boolean autoSave = true;
36 
37  public JpaGrid(Dao dao, String query, boolean autoSave) {
38  super();
39  this.dao = dao;
40  this.query = query;
41  this.autoSave = autoSave;
42  }
43 
44  public boolean isAutoSave() {
45  return autoSave;
46  }
47 
48  public void setAutoSave(boolean autoSave) {
49  this.autoSave = autoSave;
50  }
51 
52  public Dao getDao() {
53  return dao;
54  }
55 
56  public void setDao(Dao dao) {
57  this.dao = dao;
58  }
59 
60  public String getQuery() {
61  return query;
62  }
63 
64  public void setQuery(String query) {
65  this.query = query;
66  }
67 
68  public void save() {
69  for(Component row : getRows().getChildren()) {
70  if(isValid((V) ((Row)row).getValue())) {
71  ((Row)row).setValue(dao.saveObject(((Row)row).getValue()));
72  }
73  }
74  }
75 
76  @Override
77  public void afterCompose() {
78  addTableRows();
79  super.afterCompose();
80  }
81 
82  @Override
83  protected void rowChanged(Row row) {
84  if(autoSave && row.getValue() != null && isValid((V) row.getValue())) {
85  row.setValue(dao.saveObject(row.getValue()));
86  updateRow(row);
87  }
88  }
89 
90  @Override
91  protected boolean deleteRow(Row row) {
92  if(autoSave && row.getValue() != null) {
93  dao.deleteObject(row.getValue());
94  return true;
95  }
96  return false;
97  }
98 
99  private void addTableRows() {
100  if(getRows() == null) {
101  appendChild(new Rows());
102  }
103  for(Object o : dao.getResultList(query)) {
104  Row row = new Row();
105  getRows().appendChild(row);
106  initiateRow(row, (V) o);
107  }
108  if(isAllowInsertions()) {
109  Row row = new Row();
110  getRows().appendChild(row);
111  row.setSclass("invalid");
112  initiateRow(row, null);
113  }
114  }
115 
116 }
void setQuery(String query)
Definition: JpaGrid.java:64
void rowChanged(Row row)
Definition: JpaGrid.java:83
boolean deleteRow(Row row)
Definition: JpaGrid.java:91
JpaGrid(Dao dao, String query, boolean autoSave)
Definition: JpaGrid.java:37
void setDao(Dao dao)
Definition: JpaGrid.java:56
void setAutoSave(boolean autoSave)
Definition: JpaGrid.java:48
abstract void initiateRow(Row row, V value)
abstract boolean isValid(V v)
Rows getRows(boolean create)