BrightSide Workbench Full Report + Source Code
HandshakeContract.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 com.google.gson.Gson;
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.Objects;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import org.turro.elephant.context.ElephantContext;
28 import org.turro.elephant.impl.util.FileUtil;
29 import org.turro.financials.db.FinancialsPU;
30 import org.turro.financials.entity.Company;
31 import org.turro.financials.entity.Contract;
32 import org.turro.financials.entity.ContractHandshake;
33 import org.turro.financials.model.business.CompanyWrapper;
34 import org.turro.jpa.Dao;
35 import org.turro.util.IdGenerator;
36 
41 public class HandshakeContract {
42 
43  public final static String
44  HANDSHAKE_FOLDER = "/WEB-INF/handshake/contract";
45 
46  public String petitioner;
47  public long remoteId;
48  public String remoteName;
49  public String remoteServer;
50 
51  public static String createJsonFromContract(Contract contract) {
53  if(hc != null) {
54  return new Gson().toJson(hc);
55  }
56  return null;
57  }
58 
61  if(company != null) {
63  hc.petitioner = company.getName();
65  hc.remoteId = contract.getId();
66  hc.remoteName = contract.getName();
67  return hc;
68  }
69  return null;
70  }
71 
72  public static HandshakeContract createHandshakeFromJson(String value) {
73  return new Gson().fromJson(value, HandshakeContract.class);
74  }
75 
76  public static HandshakeContract createHandshakeFromFile(File file) {
77  try {
79  } catch (IOException ex) {
80  Logger.getLogger(HandshakeContract.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
81  }
82  return null;
83  }
84 
85  public static void saveHandshakeToFile(File file, HandshakeContract hc) {
86  try {
87  FileUtil.setContent(file, new Gson().toJson(hc));
88  } catch (IOException ex) {
89  Logger.getLogger(HandshakeContract.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
90  }
91  }
92 
93  public static Contract getContractFromRemoteContract(String value) {
95  return getContractFromHandshake(hc);
96  }
97 
100  return ch != null ? ch.getContract() : null;
101  }
102 
104  Dao dao = new FinancialsPU();
106  "select ch from ContractHandshake ch " +
107  "where ch.remoteServer = ? " +
108  "and ch.remoteId = ?",
109  new Object[] {
110  hc.remoteServer,
111  hc.remoteId
112  });
113  }
114 
115  public static void saveHandshakePetition(HandshakeContract hc) {
116  if(!existsHandshake(hc)) {
117  File file = new File(ElephantContext.getRealPath(HANDSHAKE_FOLDER) + "/contract" + IdGenerator.generate() + ".handshake");
118  saveHandshakeToFile(file, hc);
119  }
120  }
121 
122  public static boolean existsHandshake(HandshakeContract hc) {
123  File folder = new File(ElephantContext.getRealPath(HANDSHAKE_FOLDER));
124  if(!folder.exists()) { folder.mkdirs(); }
125  for(File file : folder.listFiles()) {
127  if(hc.equals(fhc)) {
128  return true;
129  }
130  }
131  return false;
132  }
133 
134  public static boolean updateRemoteId(String remoteServer, String remoteId, long contractId) {
135  Dao dao = new FinancialsPU();
136  return dao.executeUpdate(
137  "update ContractHandshake " +
138  "set remoteId = ? " +
139  "where remoteServer = ? " +
140  "and contract.id = ?",
141  new Object[] {
142  contractId,
143  remoteServer,
144  Long.valueOf(remoteId)
145  }) > 0;
146  }
147 
148  @Override
149  public int hashCode() {
150  int hash = 7;
151  hash = 89 * hash + (int) (this.remoteId ^ (this.remoteId >>> 32));
152  hash = 89 * hash + Objects.hashCode(this.remoteServer);
153  return hash;
154  }
155 
156  @Override
157  public boolean equals(Object obj) {
158  if (obj == null) {
159  return false;
160  }
161  if (getClass() != obj.getClass()) {
162  return false;
163  }
164  final HandshakeContract other = (HandshakeContract) obj;
165  if (this.remoteId != other.remoteId) {
166  return false;
167  }
168  if (!Objects.equals(this.remoteServer, other.remoteServer)) {
169  return false;
170  }
171  return true;
172  }
173 
174 }
static String getServerUrl(String scheme)
static void setContent(File file, String value)
Definition: FileUtil.java:274
static String getContent(File file)
Definition: FileUtil.java:245
static Contract getContractFromRemoteContract(String value)
static void saveHandshakeToFile(File file, HandshakeContract hc)
static Contract getContractFromHandshake(HandshakeContract hc)
static void saveHandshakePetition(HandshakeContract hc)
static String createJsonFromContract(Contract contract)
static ContractHandshake getContractHandshakeFromHandshake(HandshakeContract hc)
static boolean updateRemoteId(String remoteServer, String remoteId, long contractId)
static HandshakeContract createHandshakeFromContract(Contract contract)
static HandshakeContract createHandshakeFromJson(String value)
static boolean existsHandshake(HandshakeContract hc)
static HandshakeContract createHandshakeFromFile(File file)
int executeUpdate(String query)
Definition: Dao.java:463
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419