Advertisement
Guest User

Untitled

a guest
Jun 13th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.OutputStream;
  3. import java.util.List;
  4. import java.util.UUID;
  5. import javax.faces.bean.ManagedBean;
  6. import javax.faces.bean.SessionScoped;
  7. import javax.faces.context.ExternalContext;
  8. import javax.faces.context.FacesContext;
  9. import javax.xml.parsers.ParserConfigurationException;
  10. import javax.xml.transform.TransformerException;
  11. import org.apache.fop.apps.FOPException;
  12. import org.json.JSONObject;
  13. import org.xml.sax.SAXException;
  14. import com.google.gson.Gson;
  15. import data.PDF.JSONvXML;
  16. import data.PDF.XmlVPDF;
  17. import data.blockchain.Block;
  18. import data.blockchain.BlockStorage;
  19. import data.entity.Evidence;
  20. import data.util.BlockChainUtil;
  21.  
  22. @SuppressWarnings("deprecation")
  23. @ManagedBean
  24. @SessionScoped
  25. public class EvidenceBean {
  26.  
  27. private Evidence newEvidence = new Evidence();
  28. private Evidence selectedEvidence = new Evidence();
  29. public static final int difficulty = 5;
  30.  
  31. //adding evidence
  32. public void addEvidence() {
  33. List<Block> blocks = BlockStorage.getInstance().getBlockchain();
  34. System.out.println(blocks.size());
  35. Block prevBlock = blocks.get(blocks.size() - 1);
  36. blocks.add(new Block(prevBlock.getHash(), newEvidence));
  37. blocks.get(blocks.size() - 1).mineBlock(difficulty);
  38. newEvidence = new Evidence();
  39. }
  40.  
  41. //checking if chain is valid
  42. public static Boolean isChainValid() {
  43. Block current;
  44. Block previous;
  45. List<Block> blockchain = BlockStorage.getInstance().getBlockchain();
  46. for (int i = 1; i < blockchain.size(); i++) {
  47. current = blockchain.get(i);
  48. previous = blockchain.get(i - 1);
  49.  
  50. if (!current.getHash().equals(current.calculateHash())) {
  51. System.out.println("Current hashes not equal!");
  52. System.out.println(
  53. "Current stored: " + current.getHash() + "; Current calculated: " + current.calculateHash());
  54. return false;
  55. }
  56.  
  57. if (!previous.getHash().equals(previous.calculateHash())) {
  58. System.out.println("Previous hashes not equal");
  59. System.out.println("Previous stored: " + previous.getHash() + "; Previous calculated: "
  60. + previous.calculateHash());
  61. return false;
  62. }
  63.  
  64. }
  65.  
  66. return true;
  67. }
  68.  
  69. //updating data in evidence
  70. public void updateEvidence() {
  71. List<Block> blocks = BlockStorage.getInstance().getBlockchain();
  72. Block prevBlock = blocks.get(blocks.size() - 1);
  73. selectedEvidence.increaseVersion(selectedEvidence.getVersion());
  74. blocks.add(new Block(prevBlock.getHash(), selectedEvidence));
  75. blocks.get(blocks.size() - 1).mineBlock(difficulty);
  76. selectedEvidence = new Evidence();
  77.  
  78. }
  79.  
  80.  
  81. //selecting Evidence for printing
  82. public String json() throws IOException, TransformerException, SAXException, ParserConfigurationException {
  83. Gson gson = new Gson();
  84.  
  85. String result = gson.toJson(selectedEvidence);
  86. String xml = JSONvXML.readFile(result);
  87. XmlVPDF v=null;
  88. v.vPDF(xml);
  89.  
  90.  
  91. return xml;
  92. }
  93.  
  94. //this part is about downloading pdf
  95. public void download() throws IOException, FOPException, TransformerException {
  96. String fileName = "Verzija";
  97. FacesContext fc = FacesContext.getCurrentInstance();
  98. ExternalContext ec = fc.getExternalContext();
  99.  
  100. ec.responseReset(); // Some JSF component library or some Filter might have set some headers in the buffer beforehand. We want to get rid of them, else it may collide.
  101. ec.setResponseContentType("application/pdf");
  102. ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); // Shrani z Pop-up window
  103.  
  104. OutputStream output = ec.getResponseOutputStream();
  105. // Now you can write the InputStream of the file to the above OutputStream the usual way.
  106. // ...
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113. fc.responseComplete(); // Important! Otherwise JSF will attempt to render the response which obviously will fail since it's already written with a file and closed.
  114. }
  115.  
  116.  
  117.  
  118.  
  119. public List<Evidence> getAllPublicEvidences() {
  120. // log user who is accessing
  121. return BlockChainUtil.getAllPublicEvidences();
  122. }
  123.  
  124. public List<Evidence> getAllPublicLastVersionEvidences() {
  125. // log user
  126. return BlockChainUtil.getAllPublicEvidencesLastVersion();
  127. }
  128.  
  129. public List<Evidence> getAllUserEvidences(String id) {
  130. // log user
  131. return BlockChainUtil.getAllUserEvidencesList(id);
  132. }
  133.  
  134. public List<Evidence> getAllUserLastVersionEvidences(String id) {
  135. // log user
  136. return BlockChainUtil.getCurrentUserEvidencesList(id);
  137. }
  138.  
  139. public Evidence getEvidence(UUID uuid) {
  140. // log user
  141. return BlockChainUtil.getEvidence(uuid);
  142. }
  143.  
  144. public Evidence getNewEvidence() {
  145. return newEvidence;
  146. }
  147.  
  148. public void setNewEvidence(Evidence newEvidence) {
  149. this.newEvidence = newEvidence;
  150. }
  151.  
  152. public List<Block> getBlocks() {
  153. return BlockStorage.getInstance().getBlockchain();
  154. }
  155.  
  156. public Evidence getSelectedEvidence() {
  157. return selectedEvidence;
  158. }
  159.  
  160. public void setSelectedEvidence(Evidence selectedEvidence) {
  161. this.selectedEvidence = selectedEvidence;
  162. }
  163.  
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement