BrightSide Workbench Full Report + Source Code
HandshakeDocControl.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2015 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 
19 package org.turro.financials.handshake;
20 
21 import org.turro.elephant.util.Images;
22 import org.turro.financials.entity.ContractHandshake;
23 import org.turro.financials.entity.Document;
24 import org.turro.http.ElephantResponse;
25 import org.turro.log.SystemLogType;
26 import org.turro.log.SystemLogger;
27 import org.zkoss.zk.ui.event.Event;
28 import org.zkoss.zk.ui.event.EventListener;
29 import org.zkoss.zk.ui.event.Events;
30 import org.zkoss.zk.ui.ext.AfterCompose;
31 import org.zkoss.zk.ui.util.Clients;
32 import org.zkoss.zul.Toolbarbutton;
33 
38 public class HandshakeDocControl extends Toolbarbutton implements AfterCompose {
39 
40  private Document document;
41  private EventListener onClick;
42 
43  public void setDocument(Document document) {
44  this.document = document;
45  }
46 
47  @Override
48  public void afterCompose() {
49  if(document == null || document.getDocumentDefinition() == null) {
50  setVisible(false);
51  return;
52  }
53  if(document.getDocumentDefinition().getId() == 1) {
54  setImage(Images.getImage("handshake"));
55  if(onClick == null) {
56  onClick = new EventListener() {
57  @Override
58  public void onEvent(Event event) throws Exception {
59  ContractHandshake handshake = document.getContract().getContractHandshake();
60  if(handshake != null) {
62  if(ElephantResponse.isCorrect(er)) {
64  Clients.showNotification(er.message + ": " + er.code);
65  updateImage(status);
66  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, document, "sent", "Handshake");
67  } else {
68  Clients.showNotification("Handshake server not found");
69  }
70  }
71  Events.postEvent(new Event(Events.ON_CHANGE, HandshakeDocControl.this));
72  }
73  };
74  addEventListener(Events.ON_CLICK, onClick);
75  }
76  } else {
77  setVisible(false);
78  }
79  }
80 
81  private void updateImage(HandshakeStatus status) {
82  switch(status) {
83  case HS_ESTABLISHED:
84  setImage(Images.getImage("handshake_ok"));
85  break;
86  case HS_BROKEN:
87  setImage(Images.getImage("handshake_broken"));
88  break;
89  case HS_NO_HANDSHAKE:
90  setImage(Images.getImage("handshake_out"));
91  break;
92  default:
93  setImage(Images.getImage("handshake"));
94  break;
95  }
96  }
97 
98 }
static String getImage(String image)
Definition: Images.java:36
ContractHandshake getContractHandshake()
Definition: Contract.java:328
DocumentDefinition getDocumentDefinition()
Definition: Document.java:167
static ElephantResponse sendDocument(Document document)
static boolean isCorrect(ElephantResponse er)
static ISystemLogger getInstance()
static HandshakeStatus getStatus(ElephantResponse er)
void doLog(SystemLogType type, Object entity, String comment, Serializable data)