/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package semesterprojectv5;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import static java.
util.
Map.
entry;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.swing.JOptionPane;
import okhttp3.OkHttpClient;
import okhttp3.Request;
/**
*
* @author alexa
*/
public class Datamanager
extends javax.
swing.
JFrame {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("Semesterprojectv5PU");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
CountryJpaController cjc = new CountryJpaController(emf);
CoviddataJpaController covjc = new CoviddataJpaController(emf);
public Datamanager() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1
= new javax.
swing.
JButton();
jButton2
= new javax.
swing.
JButton();
jLabel1
= new javax.
swing.
JLabel();
jLabel2
= new javax.
swing.
JLabel();
jLabel3
= new javax.
swing.
JLabel();
jComboBox1 = new javax.swing.JComboBox<>();
jButton5
= new javax.
swing.
JButton();
jButton6
= new javax.
swing.
JButton();
jButton1.
setFont(new java.
awt.
Font("Tahoma",
0,
16)); // NOI18N
jButton1.setText("Insert");
public void actionPerformed
(java.
awt.
event.
ActionEvent evt
) {
jButton1ActionPerformed(evt);
}
});
jButton2.
setFont(new java.
awt.
Font("Tahoma",
0,
16)); // NOI18N
jButton2.setText("Delete");
public void actionPerformed
(java.
awt.
event.
ActionEvent evt
) {
jButton2ActionPerformed(evt);
}
});
jLabel1.
setFont(new java.
awt.
Font("Tahoma",
1,
20)); // NOI18N
jLabel1.setText("Insert-Delete Countries");
jLabel2.
setFont(new java.
awt.
Font("Tahoma",
1,
20)); // NOI18N
jLabel2.setText("Insert-Delete Data");
jLabel3.
setFont(new java.
awt.
Font("Tahoma",
0,
16)); // NOI18N
jLabel3.setText("Chose Data");
jComboBox1.
setFont(new java.
awt.
Font("Tahoma",
0,
16)); // NOI18N
jComboBox1.
setModel(new javax.
swing.
DefaultComboBoxModel<>(new String[] { "Deaths",
"Recovered",
"Confirmed Cases" }));
public void actionPerformed
(java.
awt.
event.
ActionEvent evt
) {
jComboBox1ActionPerformed(evt);
}
});
jButton5.
setFont(new java.
awt.
Font("Tahoma",
0,
16)); // NOI18N
jButton5.setText("Insert");
public void actionPerformed
(java.
awt.
event.
ActionEvent evt
) {
jButton5ActionPerformed(evt);
}
});
jButton6.
setFont(new java.
awt.
Font("Tahoma",
0,
16)); // NOI18N
jButton6.setText("Delete");
public void actionPerformed
(java.
awt.
event.
ActionEvent evt
) {
jButton6ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(49, 49, 49)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1)
.addGap(78, 78, 78)
.addComponent(jButton2))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)))
.addComponent(jLabel2)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.
addComponent(jComboBox1, javax.
swing.
GroupLayout.
Alignment.
LEADING,
0, javax.
swing.
GroupLayout.
DEFAULT_SIZE,
Short.
MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton6))))
.
addContainerGap(62,
Short.
MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(32, 32, 32)
.addComponent(jLabel1)
.addGap(35, 35, 35)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addGap(33, 33, 33)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton5)
.addComponent(jButton6))
.
addContainerGap(51,
Short.
MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton2ActionPerformed
(java.
awt.
event.
ActionEvent evt
) {
// TODO add your handling code here:
// Complete any data deletion after it has been completed
// verification query with user confirmation.
int a
= JOptionPane.
showConfirmDialog(null,
"The system will delete from the database\n" + "all countries. Are you sure?",
"Delete Data",
JOptionPane.
YES_NO_OPTION);
try {
// Delete table COUNTRY
tx.begin();
// We check if we have data entries in Coviddata table
Query q1 = em.createQuery("SELECT c FROM Coviddata c");
if (q1.getResultList().isEmpty()) {
em.createQuery("DELETE FROM Country").executeUpdate();
em.createNativeQuery("ALTER TABLE COUNTRY ALTER COLUMN COUNTRY RESTART WITH 1").executeUpdate();
tx.commit();
JOptionPane.
showMessageDialog(null,
"The Countries deleted from Database",
"Delete Countries",
JOptionPane.
INFORMATION_MESSAGE);
} else {
JOptionPane.
showMessageDialog(null,
"Countries not deleted from Database\n" + "Please delete the Coviddata first.",
"Delete Countries",
JOptionPane.
INFORMATION_MESSAGE);
}
}
}
}
private void jButton1ActionPerformed
(java.
awt.
event.
ActionEvent evt
) {
// TODO add your handling code here:
String urlToCall
= "https://covid2019-api.herokuapp.com/timeseries/recovered";
//Create a object OkHttpClient
OkHttpClient client = new OkHttpClient();
//Create a object Request with reference the url we call
//We check if the url we call give us the data
try (okhttp3.Response response = client.newCall(request).execute()) {
if (response.isSuccessful() && response.body() != null) {
//We save the response String
String responseString
= response.
body().
string();
//Create a object GsonBuilder
GsonBuilder builder = new GsonBuilder();
builder.setPrettyPrinting();
Gson gson = builder.create();
//We get the results in JsonObject and we create a JsonArray
JsonObject json = gson.fromJson(responseString, JsonObject.class);
JsonArray items = json.get("recovered").getAsJsonArray();
//foreach in JsonElements
for (JsonElement element : items) {
JsonObject object = element.getAsJsonObject();
try {
//We get Country/Region, Lat και Long
String Name = object.
getAsJsonObject().
get("Country/Region").
getAsString();
Double Lat
= object.
getAsJsonObject().
get("Lat").
getAsDouble();
Double Long = object.
getAsJsonObject().
get("Long").
getAsDouble();
//Create a object Country
Country c = new Country();
c.setLat(Lat);
//We search the country if it's in Database
String name
= object.
getAsJsonObject().
get("Country/Region").
getAsString();
Query q1 = em.createQuery("SELECT c FROM Country c WHERE c.name = :name");
q1.setParameter("name", name);
//We check if we have data in Database
if (q1.getResultList().isEmpty()) {
//With CountryJpaController we send the object to the Database
cjc.create(c);
}
}
}
// Information message
JOptionPane.
showMessageDialog(null,
"The countries were registered in the database ",
"Information",
JOptionPane.
INFORMATION_MESSAGE);
}
}
}
private void jButton5ActionPerformed
(java.
awt.
event.
ActionEvent evt
) {
Integer selectint
= jComboBox1.
getSelectedIndex();
if (selectint == 0) {
selecturl = "deaths";
}else if (selectint == 1) {
selecturl = "recovered";
}else if (selectint == 2) {
selecturl = "confirmed";
}
String urlToCall
= "https://covid2019-api.herokuapp.com/timeseries/" + selecturl
;
//Create a object OkHttpClient
OkHttpClient client = new OkHttpClient();
//Create a object Request with reference the url we call
//We check if the url we call give us the data
try (okhttp3.Response response = client.newCall(request).execute()) {
if (response.isSuccessful() && response.body() != null) {
//We save the response String
String responseString
= response.
body().
string();
//Create a object GsonBuilder
GsonBuilder builder = new GsonBuilder();
builder.setPrettyPrinting();
Gson gson = builder.create();
//We get the results in JsonObject and we create a JsonArray
JsonObject json = gson.fromJson(responseString, JsonObject.class);
JsonArray items = json.get(selecturl).getAsJsonArray();
for (JsonElement element : items) {
JsonObject object = element.getAsJsonObject();
Set
<Map.
Entry<String, JsonElement
>> entries
= object.
entrySet();
//System.out.println(entries);
try{
String Name = object.
getAsJsonObject().
get("Country/Region").
getAsString();
Query q1 = em.createQuery("SELECT c FROM Country c WHERE c.name = :name");
q1.
setParameter("name",
Name);
Country country = (Country) q1.getSingleResult();
Coviddata coviddata = new Coviddata();
coviddata.setCountry(country);
short Datakind = selectint.shortValue();
coviddata.setDatakind(++Datakind);
int qty = 0;
for (Map.
Entry<String, JsonElement
> entry
: entries
) {
if (entry.getKey().charAt(1) == '/' || entry.getKey().charAt(2) == '/'){
coviddata.setTrndate(date);
int proodqty = entry.getValue().getAsInt();
coviddata.setProodqty(proodqty);
if (proodqty > qty){
qty = proodqty - qty;
coviddata.setQty(qty);
qty = proodqty;
}else if (proodqty == qty){
coviddata.setQty(0);
}
System.
out.
println(coviddata
);
covjc.create(coviddata);
}
}
}
}
JOptionPane.
showMessageDialog(null,
"The data " +selecturl
+ " were registered in the database ",
"Information",
JOptionPane.
INFORMATION_MESSAGE);
}
}
private void jButton6ActionPerformed
(java.
awt.
event.
ActionEvent evt
) {
int a
= JOptionPane.
showConfirmDialog(null,
"The system will delete from the database\n" + "all countries. Are you sure?",
"Delete Data",
JOptionPane.
YES_NO_OPTION);
Integer selectint
= jComboBox1.
getSelectedIndex();
if (selectint == 0) {
selecturl = "deaths";
}else if (selectint == 1) {
selecturl = "recovered";
}else if (selectint == 2) {
selecturl = "confirmed";
}
try {
// Delete table COUNTRY
tx.begin();
// We check if we have data entries in Coviddata table
em.createQuery("DELETE FROM"+selecturl).executeUpdate();
em.createNativeQuery("ALTER TABLE COVIDDATA ALTER COLUMN COVIDDATA RESTART WITH 1").executeUpdate();
tx.commit();
JOptionPane.
showMessageDialog(null,
"The "+ selecturl
+" deleted from Database",
"Delete coviddata",
JOptionPane.
INFORMATION_MESSAGE);
}
}
}
private void jComboBox1ActionPerformed
(java.
awt.
event.
ActionEvent evt
) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main
(String args
[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
if ("Nimbus".equals(info.getName())) {
javax.
swing.
UIManager.
setLookAndFeel(info.
getClassName());
break;
}
}
java.util.logging.Logger.getLogger(Datamanager.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
java.util.logging.Logger.getLogger(Datamanager.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
java.util.logging.Logger.getLogger(Datamanager.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
java.util.logging.Logger.getLogger(Datamanager.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
public void run() {
new Datamanager().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.
swing.
JButton jButton1
;
private javax.
swing.
JButton jButton2
;
private javax.
swing.
JButton jButton5
;
private javax.
swing.
JButton jButton6
;
private javax.swing.JComboBox<String> jComboBox1;
private javax.
swing.
JLabel jLabel1
;
private javax.
swing.
JLabel jLabel2
;
private javax.
swing.
JLabel jLabel3
;
// End of variables declaration
}