BrightSide Workbench Full Report + Source Code
HandshakeControl.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.string.Strings;
22 import org.turro.command.Command;
23 import org.turro.command.Context;
24 import org.turro.elephant.util.Images;
25 import org.turro.financials.entity.Contract;
26 import org.turro.financials.entity.ContractHandshake;
27 import org.turro.http.ElephantResponse;
28 import org.turro.http.URLUtil;
29 import org.turro.i18n.I_;
30 import org.turro.zkoss.dialog.InputDialog;
31 import org.zkoss.zk.ui.event.Event;
32 import org.zkoss.zk.ui.event.EventListener;
33 import org.zkoss.zk.ui.event.Events;
34 import org.zkoss.zk.ui.ext.AfterCompose;
35 import org.zkoss.zk.ui.util.Clients;
36 import org.zkoss.zul.Toolbarbutton;
37 
42 public class HandshakeControl extends Toolbarbutton implements AfterCompose {
43 
44  private Contract contract;
45  private EventListener onClick;
46  private boolean mustSave = false;
47 
48  public void setContract(Contract contract) {
49  this.contract = contract;
50  }
51 
52  public boolean isMustSave() {
53  return mustSave;
54  }
55 
56  @Override
57  public void afterCompose() {
58  if(contract == null || contract.getContractDefinition() == null) {
59  setVisible(false);
60  return;
61  }
62  if((contract.getContractDefinition().getId() == 47L) || (contract.getContractDefinition().getId() == 56L)) {
63  setImage(Images.getImage("handshake"));
64  if(onClick == null) {
65  onClick = new EventListener() {
66  @Override
67  public void onEvent(Event event) throws Exception {
68  ContractHandshake handshake = contract.getContractHandshake();
69  if(handshake != null) {
71  if(ElephantResponse.isCorrect(er)) {
73  Clients.showNotification(er.message + ": " + er.code);
74  updateImage(status);
75  } else {
76  Clients.showNotification("Handshake server not found");
77  }
78  Events.postEvent(new Event(Events.ON_CHANGE, HandshakeControl.this));
79  } else {
80  InputDialog.getInput(getPage(), "Handshake", "Remote server", "", null, 0,
81  new Command() {
82  @Override
83  public Object execute(Context context) {
84  String remoteUrl = (String) context.get("value");
85  remoteUrl = URLUtil.checkAppServer(remoteUrl, true);
86  if(!Strings.isBlank(remoteUrl)) {
87  ElephantResponse er = HandshakeUtil.sendPetition(contract, remoteUrl);
88  if(ElephantResponse.isCorrect(er)) {
89  ContractHandshake handshake = new ContractHandshake();
90  handshake.setContract(contract);
91  handshake.setRemoteServer(remoteUrl);
92  contract.setContractHandshake(handshake);
94  Clients.showNotification(I_.get("Save before continue") + "<br/>" + er.message + ": " + er.code);
95  mustSave = true;
96  updateImage(status);
97  } else {
98  Clients.showNotification("Handshake server not found");
99  contract.setContractHandshake(null);
100  }
101  }
102  Events.postEvent(new Event(Events.ON_CHANGE, HandshakeControl.this));
103  return null;
104  }
105  });
106  }
107  }
108  };
109  addEventListener(Events.ON_CLICK, onClick);
110  }
111  } else if((contract.getContractDefinition().getId() == 48L) || (contract.getContractDefinition().getId() == 55L)) {
112  setImage(Images.getImage("handshake"));
113  if(onClick == null) {
114  onClick = new EventListener() {
115  @Override
116  public void onEvent(Event event) throws Exception {
117  ContractHandshake handshake = contract.getContractHandshake();
118  if(handshake != null) {
120  if(ElephantResponse.isCorrect(er)) {
122  Clients.showNotification(er.message + ": " + er.code);
123  updateImage(status);
124  } else {
125  Clients.showNotification("Handshake server not found");
126  }
127  } else {
128  updateImage(HandshakeStatus.HS_NO_HANDSHAKE);
129  }
130  Events.postEvent(new Event(Events.ON_CHANGE, HandshakeControl.this));
131  }
132  };
133  addEventListener(Events.ON_CLICK, onClick);
134  }
135  } else {
136  setVisible(false);
137  }
138  }
139 
140  private void updateImage(HandshakeStatus status) {
141  switch(status) {
142  case HS_ESTABLISHED:
143  setImage(Images.getImage("handshake_ok"));
144  break;
145  case HS_BROKEN:
146  setImage(Images.getImage("handshake_broken"));
147  break;
148  case HS_NO_HANDSHAKE:
149  setImage(Images.getImage("handshake_out"));
150  break;
151  default:
152  setImage(Images.getImage("handshake"));
153  break;
154  }
155  }
156 
157 }
static String getImage(String image)
Definition: Images.java:36
ContractHandshake getContractHandshake()
Definition: Contract.java:328
ContractDefinition getContractDefinition()
Definition: Contract.java:125
void setContractHandshake(ContractHandshake contractHandshake)
Definition: Contract.java:332
static ElephantResponse sendPetition(Contract contract, String remoteUrl)
static ElephantResponse checkStatus(Contract contract)
static boolean isCorrect(ElephantResponse er)
static String checkAppServer(String server, boolean secured)
Definition: URLUtil.java:37
static String get(String msg)
Definition: I_.java:41
static void getInput(Page page, String title, String label, Object value, String format, int scale, final Command onOk)
static HandshakeStatus getStatus(ElephantResponse er)