19 package org.turro.jpa.composer;
21 import org.turro.elephant.util.Components;
22 import org.turro.elephant.util.Images;
23 import org.turro.elephant.util.Messages;
24 import org.turro.i18n.I_;
25 import org.turro.jpa.entity.DaoEntity;
26 import org.turro.jpa.entity.IDaoEntity;
27 import org.turro.json.Jsons;
28 import org.turro.reflection.XMLSerializer;
29 import org.turro.reflection.stub.Stubs;
30 import org.turro.zul.frame.Framework;
31 import org.zkoss.zk.ui.Component;
32 import org.zkoss.zk.ui.Executions;
33 import org.zkoss.zk.ui.event.Event;
34 import org.zkoss.zk.ui.event.EventListener;
35 import org.zkoss.zk.ui.event.Events;
36 import org.zkoss.zk.ui.select.SelectorComposer;
37 import org.zkoss.zk.ui.select.annotation.Listen;
38 import org.zkoss.zk.ui.select.annotation.Wire;
39 import org.zkoss.zul.Tab;
40 import org.zkoss.zul.Tabpanel;
41 import org.zkoss.zul.Toolbarbutton;
49 public abstract class EntityComposer<E, ID>
extends SelectorComposer<Component> {
51 private String entityXML, entityJSON;
56 private Toolbarbutton save;
59 private Toolbarbutton delete;
61 @Listen("onChange = *; onSelect = *; onCheck = *")
62 public final void onChange(Event event) {
63 if(!(event.getTarget() instanceof Tab)) {
64 save.setDisabled(!(wrapper.canSave() && (hasChanged() || shouldBeSaved())) || !inSaveRole());
69 @Listen("onClick = #save")
70 public final void onSave() {
74 @Listen("onClick = #delete")
75 public final void onDelete() {
79 public Object getEntity() {
83 public Object getWrapper() {
88 public void doBeforeComposeChildren(Component comp) throws Exception {
91 comp.setAttribute(getAttributeName(), entity);
92 wrapper = getWrapperInstance(comp);
93 comp.setAttribute("wrapper", wrapper);
94 super.doBeforeComposeChildren(comp);
98 public void doFinally() throws Exception {
100 save.setDisabled(!shouldBeSaved());
101 save.setLabel(I_.get("Save"));
102 save.setImage(Images.getImage("save"));
103 delete.setDisabled(!wrapper.canDelete() || !inDeleteRole());
104 delete.setLabel(I_.get("Delete"));
105 delete.setImage(Images.getImage("edit-delete"));
109 public void doAfterCompose(Component comp) throws Exception {
110 super.doAfterCompose(comp);
114 protected abstract String getAttributeName();
115 protected abstract E getEntityInstance(ID id);
116 protected abstract DaoEntity getWrapperInstance(Component comp);
117 protected abstract boolean inSaveRole();
118 protected abstract boolean inDeleteRole();
120 protected void doOnSave() {
122 entity = (E) wrapper.save();
123 Framework.getCurrent().closeSelected();
128 protected boolean beforeSave() {
129 return !((IDaoEntity) entity).isEmpty();
132 protected void afterSave() {
136 protected void doOnDelete() {
138 if(wrapper.delete()) {
139 Framework.getCurrent().closeSelected();
145 protected boolean beforeDelete() {
149 protected void afterDelete() {
153 protected void doOnChange() {
157 protected boolean shouldBeSaved() {
161 protected boolean hasChanged() {
162 return !Jsons.isEmpty(entityJSON) ? (!Jsons.equals(entityJSON, Stubs.json(entity))) :
163 (entityXML == null || (!entityXML.equals(new XMLSerializer(entity).data())));
166 protected void retrieveEntity() {
167 Framework frame = Framework.getCurrent();
168 entity = (E) frame.getSelectedAttribute("tp_entity");
170 if(Executions.getCurrent().getAttribute(getAttributeName() + "Generated") != null) {
171 entity = (E) Executions.getCurrent().getAttribute(getAttributeName() + "Generated");
172 Executions.getCurrent().removeAttribute(getAttributeName() + "Generated");
174 entity = (E) getEntityInstance((ID) frame.getDesktop().getAttribute(getAttributeName() + "Id"));
175 frame.getDesktop().removeAttribute(getAttributeName() + "Id");
177 frame.setSelectedAttribute("tp_entity", entity);
179 entity = (E) getEntityInstance((ID) DaoEntity.getEntityId(entity));
181 entityXML = new XMLSerializer(entity).data();
182 entityJSON = Stubs.json(entity);
185 protected boolean canClose() {
189 private void interceptOnClose() {
190 Tabpanel container = Components.from(getSelf().getPage()).parent(Tabpanel.class);
191 if(container != null) {
192 final Tab tab = container.getLinkedTab();
193 tab.addEventListener(9000, Events.ON_CLOSE, new EventListener<Event>() {
195 public void onEvent(final Event event) throws Exception {
196 if(!save.isDisabled()) {
198 event.stopPropagation();
199 Messages.question().add("Close without saving?").show(() -> {
200 Framework.getCurrent().closeSelected();