diff --git a/.gitignore b/.gitignore
index e067009..65e59ee 100644
--- a/.gitignore
+++ b/.gitignore
@@ -99,3 +99,4 @@
/Semester 3/Assignments/TermProject1_CalebFontenot/target/
/Semester 3/Assignments/params/target/
/Semester 3/Assignments/templatesMatricesSample/target/
+/Semester 3/Assignments/StockBrokerAjaxThreads/target/
diff --git a/Semester 3/Assignments/StockBrokerAjaxThreads.zip b/Semester 3/Assignments/StockBrokerAjaxThreads.zip
new file mode 100644
index 0000000..a990ca5
Binary files /dev/null and b/Semester 3/Assignments/StockBrokerAjaxThreads.zip differ
diff --git a/Semester 3/Assignments/StockBrokerAjaxThreads/faces-config.NavData b/Semester 3/Assignments/StockBrokerAjaxThreads/faces-config.NavData
new file mode 100644
index 0000000..298bfc5
--- /dev/null
+++ b/Semester 3/Assignments/StockBrokerAjaxThreads/faces-config.NavData
@@ -0,0 +1,6 @@
+
+
+
+
+
+
diff --git a/Semester 3/Assignments/StockBrokerAjaxThreads/nb-configuration.xml b/Semester 3/Assignments/StockBrokerAjaxThreads/nb-configuration.xml
new file mode 100644
index 0000000..5e1a2de
--- /dev/null
+++ b/Semester 3/Assignments/StockBrokerAjaxThreads/nb-configuration.xml
@@ -0,0 +1,20 @@
+
+
+
+
+
+ 10-web
+ gfv700ee10
+ Facelets
+
+
diff --git a/Semester 3/Assignments/StockBrokerAjaxThreads/pom.xml b/Semester 3/Assignments/StockBrokerAjaxThreads/pom.xml
new file mode 100644
index 0000000..9943544
--- /dev/null
+++ b/Semester 3/Assignments/StockBrokerAjaxThreads/pom.xml
@@ -0,0 +1,53 @@
+
+ 4.0.0
+ asdv
+ StockBrokerAjaxThreads
+ 1
+ war
+ StockBrokerAjaxThreads-1
+
+
+ UTF-8
+ 10.0.0
+
+
+
+
+ jakarta.platform
+ jakarta.jakartaee-api
+ ${jakartaee}
+ provided
+
+
+ org.primefaces
+ primefaces
+ 13.0.2
+ jakarta
+
+
+ com.mysql
+ mysql-connector-j
+ 8.1.0
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ 3.8.1
+
+
+ 11
+
+
+
+ org.apache.maven.plugins
+ maven-war-plugin
+ 3.3.2
+
+
+
+
\ No newline at end of file
diff --git a/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/asdv/mp1_ajax/pojos/Stock.java b/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/asdv/mp1_ajax/pojos/Stock.java
new file mode 100644
index 0000000..bf20264
--- /dev/null
+++ b/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/asdv/mp1_ajax/pojos/Stock.java
@@ -0,0 +1,138 @@
+/*
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
+ */
+package asdv.mp1_ajax.pojos;
+
+import java.io.Serializable;
+
+
+public class Stock implements Serializable
+{
+ private static final long serialVersionUID = 1L;
+
+ private String stockId;
+
+ private String companyName;
+
+ private double priceCurrent;
+
+ private double priceClosing;
+
+ private long numberOfSharesAvailable;
+
+ private long numberOfSharesSold;
+
+ public Stock()
+ {
+ }
+
+ public Stock(String stockId)
+ {
+ this.stockId = stockId;
+ }
+
+ public Stock(String stockId, String companyName, double priceCurrent, double priceClosing, long numberOfSharesAvailable, long numberOfSharesSold)
+ {
+ this.stockId = stockId;
+ this.companyName = companyName;
+ this.priceCurrent = priceCurrent;
+ this.priceClosing = priceClosing;
+ this.numberOfSharesAvailable = numberOfSharesAvailable;
+ this.numberOfSharesSold = numberOfSharesSold;
+ }
+
+ public String getStockId()
+ {
+ return stockId;
+ }
+
+ public void setStockId(String stockId)
+ {
+ this.stockId = stockId;
+ }
+
+ public String getCompanyName()
+ {
+ return companyName;
+ }
+
+ public void setCompanyName(String companyName)
+ {
+ this.companyName = companyName;
+ }
+
+ public double getPriceCurrent()
+ {
+ return priceCurrent;
+ }
+
+ public void setPriceCurrent(double priceCurrent)
+ {
+ this.priceCurrent = priceCurrent;
+ }
+
+ public double getPriceClosing()
+ {
+ return priceClosing;
+ }
+
+ public void setPriceClosing(double priceClosing)
+ {
+ this.priceClosing = priceClosing;
+ }
+
+ public long getNumberOfSharesAvailable()
+ {
+ return numberOfSharesAvailable;
+ }
+
+ public void setNumberOfSharesAvailable(long numberOfSharesAvailable)
+ {
+ this.numberOfSharesAvailable = numberOfSharesAvailable;
+ }
+
+ public long getNumberOfSharesSold()
+ {
+ return numberOfSharesSold;
+ }
+
+ public void setNumberOfSharesSold(long numberOfSharesSold)
+ {
+ this.numberOfSharesSold = numberOfSharesSold;
+ }
+
+
+ @Override
+ public int hashCode()
+ {
+ int hash = 0;
+ hash += (stockId != null ? stockId.hashCode() : 0);
+ return hash;
+ }
+
+ @Override
+ public boolean equals(Object object)
+ {
+ // TODO: Warning - this method won't work in the case the id fields are not set
+ if (!(object instanceof Stock))
+ {
+ return false;
+ }
+ Stock other = (Stock) object;
+ if ((this.stockId == null && other.stockId != null) || (this.stockId != null && !this.stockId.equals(other.stockId)))
+ {
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public String toString()
+ {
+ return "asdv.mp1_ajax.entities.Stock[ stockId=" + stockId + " ]";
+ }
+
+
+
+}
diff --git a/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/asdv/stockbrokerajaxthreads/JakartaRestConfiguration.java b/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/asdv/stockbrokerajaxthreads/JakartaRestConfiguration.java
new file mode 100644
index 0000000..b1bd3ef
--- /dev/null
+++ b/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/asdv/stockbrokerajaxthreads/JakartaRestConfiguration.java
@@ -0,0 +1,13 @@
+package asdv.stockbrokerajaxthreads;
+
+import jakarta.ws.rs.ApplicationPath;
+import jakarta.ws.rs.core.Application;
+
+/**
+ * Configures Jakarta RESTful Web Services for the application.
+ * @author Juneau
+ */
+@ApplicationPath("resources")
+public class JakartaRestConfiguration extends Application {
+
+}
diff --git a/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/asdv/stockbrokerajaxthreads/resources/JakartaEE10Resource.java b/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/asdv/stockbrokerajaxthreads/resources/JakartaEE10Resource.java
new file mode 100644
index 0000000..cbe26f3
--- /dev/null
+++ b/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/asdv/stockbrokerajaxthreads/resources/JakartaEE10Resource.java
@@ -0,0 +1,20 @@
+package asdv.stockbrokerajaxthreads.resources;
+
+import jakarta.ws.rs.GET;
+import jakarta.ws.rs.Path;
+import jakarta.ws.rs.core.Response;
+
+/**
+ *
+ * @author
+ */
+@Path("jakartaee10")
+public class JakartaEE10Resource {
+
+ @GET
+ public Response ping(){
+ return Response
+ .ok("ping Jakarta EE")
+ .build();
+ }
+}
diff --git a/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/beans/ClientBean.java b/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/beans/ClientBean.java
new file mode 100644
index 0000000..f12d19c
--- /dev/null
+++ b/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/beans/ClientBean.java
@@ -0,0 +1,188 @@
+
+package beans;
+
+import java.io.Serializable;
+import java.util.HashMap;
+import java.util.Map;
+import jakarta.faces.context.FacesContext;
+import jakarta.faces.event.AbortProcessingException;
+import jakarta.faces.event.AjaxBehaviorEvent;
+import jakarta.faces.view.ViewScoped;
+import jakarta.inject.Named;
+import java.util.Date;
+
+@Named(value = "clientBean")
+@ViewScoped
+public class ClientBean implements Serializable
+{
+
+
+ private boolean viewStcokPrices = false;
+
+
+ private String fromCallerUrlOfParent;
+ private String fromCallerEmail;
+ private String name = "";
+ private String totalValue = "120.00";
+ private String email = "";
+ private String emailAgain = "";
+ private Date date;
+ private String tickets = "1";
+ private String price = "120";
+ private Map ticketAttrs;
+
+ public ClientBean()
+ {
+ getParametersFromCaller();
+ this.ticketAttrs = new HashMap<>();
+ this.ticketAttrs.put("type", "number");
+ this.ticketAttrs.put("min", "1");
+ this.ticketAttrs.put("max", "4");
+ this.ticketAttrs.put("required", "required");
+ this.ticketAttrs.put("title",
+ "Enter a number between 1 and 4 inclusive.");
+ }
+ private void getParametersFromCaller()
+ {
+ //> Get parameters from caller
+ Map m = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
+ this.fromCallerUrlOfParent = m.get("parent");
+ this.fromCallerEmail = m.get("email");
+ }
+
+ public boolean isViewStcokPrices()
+ {
+ return viewStcokPrices;
+ }
+
+ public void setViewStcokPrices(boolean viewStcokPrices)
+ {
+ this.viewStcokPrices = viewStcokPrices;
+ }
+ public String getFromCallerStockId()
+ {
+ return fromCallerEmail;
+ }
+
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public void setName(String name)
+ {
+ this.name = name;
+ }
+
+ public String getTotalValue()
+ {
+ return totalValue;
+ }
+
+ public void setTotalValue(String totalValue)
+ {
+ this.totalValue = totalValue;
+ }
+
+ public String getEmail()
+ {
+ return email;
+ }
+
+ public void setEmail(String email)
+ {
+ this.email = email;
+ }
+
+ public String getEmailAgain()
+ {
+ return emailAgain;
+ }
+
+ public void setEmailAgain(String emailAgain)
+ {
+ this.emailAgain = emailAgain;
+ }
+
+ public Date getDate()
+ {
+ return date;
+ }
+
+ public void setDate(Date date)
+ {
+ this.date = date;
+ }
+
+ public String getTickets()
+ {
+ return tickets;
+ }
+
+ public void setTickets(String tickets)
+ {
+ this.tickets = tickets;
+ }
+
+ public String getPrice()
+ {
+ return price;
+ }
+
+ public void setPrice(String price)
+ {
+ this.price = price;
+ }
+
+ public Map getTicketAttrs()
+ {
+ return ticketAttrs;
+ }
+
+ public void setTicketAttrs(Map ticketAttrs)
+ {
+ this.ticketAttrs = ticketAttrs;
+ }
+
+ public void calculateTotal(AjaxBehaviorEvent event)
+ throws AbortProcessingException
+ {
+ int ticketsNum = 1;
+ int ticketPrice = 0;
+ int total;
+ if (tickets.trim().length() > 0)
+ {
+ ticketsNum = Integer.parseInt(tickets);
+ }
+ if (price.trim().length() > 0)
+ {
+ ticketPrice = Integer.parseInt(price);
+ }
+ total = (ticketsNum * ticketPrice);
+ totalValue = String.valueOf(total) + ".00";
+ }
+
+ public void clear(AjaxBehaviorEvent event)
+ throws AbortProcessingException
+ {
+ name = "";
+ email = "";
+ emailAgain = "";
+ date = null;
+ price = "120.00";
+ totalValue = "120.00";
+ tickets = "1";
+ }
+
+
+
+
+
+
+
+
+
+
+
+}
diff --git a/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/beans/Login.java b/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/beans/Login.java
new file mode 100644
index 0000000..a251c5e
--- /dev/null
+++ b/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/beans/Login.java
@@ -0,0 +1,57 @@
+/*
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
+ * Click nbfs://nbhost/SystemFileSystem/Templates/JSF/JSFManagedBean.java to edit this template
+ */
+package beans;
+
+import asdv.mp1_ajax.pojos.Stock;
+import jakarta.inject.Named;
+import jakarta.enterprise.context.RequestScoped;
+import jakarta.faces.application.ConfigurableNavigationHandler;
+import jakarta.faces.context.ExternalContext;
+import jakarta.faces.context.FacesContext;
+
+/**
+ *
+ * @author asdv5
+ */
+@Named(value = "login")
+@RequestScoped
+public class Login
+{
+ private String email;
+ private String password;
+
+ public String getPassword()
+ {
+ return password;
+ }
+
+ public void setPassword(String password)
+ {
+ this.password = password;
+ }
+
+
+ public String getEmail()
+ {
+ return email;
+ }
+
+ public void setEmail(String email)
+ {
+ this.email = email;
+ }
+
+
+ public void go()
+ {
+ FacesContext fc = FacesContext.getCurrentInstance();
+ ExternalContext ec = fc.getExternalContext();
+ String page = "client-page";
+ String params = "&parent=" + "login";
+ params += "&email=" + this.email;
+ ConfigurableNavigationHandler handler = (ConfigurableNavigationHandler) fc.getApplication().getNavigationHandler();
+ handler.performNavigation(page + "?faces-redirect=true" + params);
+ }
+}
diff --git a/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/beans/OneToManyBean.java b/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/beans/OneToManyBean.java
new file mode 100644
index 0000000..8933c7a
--- /dev/null
+++ b/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/beans/OneToManyBean.java
@@ -0,0 +1,82 @@
+
+package beans;
+
+import factory_1_m.OneToMany;
+import factory_1_m.OneToManyFactory;
+import jakarta.faces.view.ViewScoped;
+import jakarta.inject.Named;
+import java.io.Serializable;
+import java.util.Arrays;
+import java.util.Collection;
+
+@Named(value = "oneToManyBean")
+@ViewScoped
+public class OneToManyBean implements Serializable
+{
+ private String country;
+ private String city;
+ private String[] countries =
+ {
+ "Greece", "USA"
+ };
+ private String[] citiesGreece =
+ {
+ "Athens", "Sparta"
+ };
+ private String[] citiesUsa =
+ {
+ "Lafayette", "New Orleans", "Houston"
+ };
+ private OneToMany oneToMany = OneToManyFactory.createOneToMany();
+
+ public OneToManyBean()
+ {
+ country = this.countries[1];
+ oneToMany.initializeOne(this.countries);
+ oneToMany.initializeMany(this.countries[0], citiesGreece);
+ oneToMany.initializeMany(this.countries[1], citiesUsa);
+ }
+
+ public OneToMany getOneToMany()
+ {
+ return oneToMany;
+ }
+
+ public String getCountry()
+ {
+ return country;
+ }
+
+ public void setCountry(String country)
+ {
+ this.country = country;
+ }
+
+ public String getCity()
+ {
+ return city;
+ }
+
+ public void setCity(String city)
+ {
+ this.city = city;
+ }
+
+
+ public void handleCountryChange()
+ {
+ if (country != null && !country.equals(""))
+ {
+ getMany();
+ }
+ }
+
+ public Collection getMany()
+ {
+ if (country != null && !country.equals(""))
+ {
+ return this.oneToMany.getMany(country);
+ }
+ return Arrays.asList(citiesGreece);
+ }
+}
diff --git a/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/beans/StockPricesBean.java b/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/beans/StockPricesBean.java
new file mode 100644
index 0000000..76d80b4
--- /dev/null
+++ b/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/beans/StockPricesBean.java
@@ -0,0 +1,179 @@
+/*
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
+ * Click nbfs://nbhost/SystemFileSystem/Templates/JSF/JSFManagedBean.java to edit this template
+ */
+package beans;
+
+import asdv.mp1_ajax.pojos.Stock;
+import edu.slcc.ajax.bl.CheckStockPricesThread;
+import edu.slcc.ajax.utilities.UtilitiesDatabase;
+import jakarta.annotation.PostConstruct;
+import jakarta.annotation.PreDestroy;
+import jakarta.faces.application.FacesMessage;
+import jakarta.faces.event.AbortProcessingException;
+import jakarta.faces.event.AjaxBehaviorEvent;
+import jakarta.faces.view.ViewScoped;
+import jakarta.inject.Named;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+import org.primefaces.PrimeFaces;
+
+/**
+ *
+ * @author a.v. markou
+ */
+@Named(value = "stockPricesBean")
+@ViewScoped
+public class StockPricesBean implements Serializable
+{
+ private boolean isThreadRunning = false;
+ private List stocks;
+ private CheckStockPricesThread stocksThread;
+ private Thread thread;
+ private Object parentIDfromJSF;//the id of the element that contains the
+ // client-page.xhtml
+ // the ID is: "id_form_container:id_pgStocks"
+
+ /**
+ * Creates a new instance of StockBean
+ *
+ * @throws java.lang.Exception
+ */
+ public StockPricesBean()
+ throws Exception
+ {
+ stocksThread = new CheckStockPricesThread();
+ }
+
+ public void parametersFromJSF(Object o)
+ {
+ parentIDfromJSF = o;
+ System.out.println(o + " ------------------parametersFromJSF");
+ }
+
+ @PostConstruct
+ void init()
+ {
+ try
+ {
+ thread = new Thread(stocksThread);
+ thread.start();
+ isThreadRunning = true;
+ stocks = this.stocksThread.getStocks();
+ }
+ catch (Exception e)
+ {
+ System.out.println(e);
+ UtilitiesDatabase.addMessage(FacesMessage.SEVERITY_FATAL, "Problem occurred with the database.", e.getMessage());
+ }
+ }
+
+ /**
+ * The PreDestroy for this bean will not be called until the application
+ * terminates There is an issue with JSF. You can verify this by looking ta
+ * the log of Glassfish for the sout "destroy called" WE HAVE TO KILL THE
+ * THREADS manually with stop method since pre-destroy is not called when
+ * the this Bean is not viewed anymore.
+ *
+ * @ViewScoped: a bean in this scope lives as long as you're interacting
+ * with the same JSF view in the browser window/tab. It get created upon a
+ * HTTP request and get destroyed once you postback to a different view. It
+ * doesn't immediately get destroyed when you leave/close the view by a GET
+ * request, but it is not accessible the usual way anymore. JSF stores the
+ * bean in the UIViewRoot#getViewMap() with the managed bean name as key,
+ * which is in turn stored in the session. You need to return null or void
+ * from action (listener) methods to keep the bean alive. Use this scope for
+ * more complex forms which use ajax, data tables and/or several
+ * rendered/disabled attributes whose state needs to be retained in the
+ * subsequent requests within the same browser window/tab (view). When you
+ * want to destroy the bean manually) call the @PreDesroy, use commandLink
+ * to leave the page.
+ */
+ @PreDestroy
+ void destroy()
+ {
+ System.out.println("destroy called");
+ if (thread != null)
+ {
+ if (thread.isAlive())
+ {
+ thread.stop();
+ isThreadRunning = false;
+ }
+ }
+ }
+
+ public List getStocks()
+ {
+ return stocks;
+ }
+
+ public void setStocks(List stocks)
+ {
+ this.stocks = stocks;
+ }
+
+ public void mouseClickListener(AjaxBehaviorEvent event)
+ throws AbortProcessingException
+ {
+ System.out.println("+++on click called" + " isThreadRunning=" + isThreadRunning);
+ if (thread != null)
+ {
+ if (!thread.isAlive())
+ {
+ if (isThreadRunning == false)
+ {
+ isThreadRunning = true;
+ System.out.println("\tthread.isAlive()=" + thread.isAlive());
+ System.out.println("\tcreating new thread");
+ thread = new Thread(stocksThread);
+ thread.start();
+ System.out.println("\tthread.start() called");
+ refresh();
+ UtilitiesDatabase.addMessage(FacesMessage.SEVERITY_INFO, "on click", "started a thread");
+ }
+ }
+ else//thread is alive, kill it
+ {
+ System.out.println("\tthread=" + thread);
+ System.out.println("\tthread.isAlive()=" + thread.isAlive());
+ thread.stop();
+ System.out.println("\tthread.stop() called");
+ isThreadRunning = false;
+ UtilitiesDatabase.addMessage(FacesMessage.SEVERITY_INFO, "on click", "destoyed a thread");
+ }
+ }
+ else
+ {
+ if (isThreadRunning == false)
+ {
+ isThreadRunning = true;
+ System.out.println("\tthread.isAlive()=" + thread.isAlive());
+ System.out.println("\tcreating new thread");
+ thread = new Thread(stocksThread);
+ thread.start();
+ System.out.println("\tthread.start() called");
+ refresh();
+ UtilitiesDatabase.addMessage(FacesMessage.SEVERITY_INFO, "on click", "started a thread");
+ }
+ }
+ }
+
+ public void refresh()
+ {
+ System.out.println("refresh!!!()");
+ if (stocksThread != null)
+ {
+ List id = new ArrayList();
+ id.add(this.parentIDfromJSF.toString());
+ stocks = stocksThread.getStocks();
+ PrimeFaces.current().ajax().update(this.parentIDfromJSF.toString());
+ }
+ else
+ {
+ UtilitiesDatabase.addMessage(FacesMessage.SEVERITY_INFO, "stocksThread is null", "");
+ }
+//FacesContext.getCurrentInstance().getPartialViewContext().getRenderIds().add("f1:id_dataTable1");
+ }
+}
diff --git a/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/edu/slcc/ajax/bl/CheckStockPricesThread.java b/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/edu/slcc/ajax/bl/CheckStockPricesThread.java
new file mode 100644
index 0000000..6a811c7
--- /dev/null
+++ b/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/edu/slcc/ajax/bl/CheckStockPricesThread.java
@@ -0,0 +1,87 @@
+package edu.slcc.ajax.bl;
+
+import asdv.mp1_ajax.pojos.Stock;
+import edu.slcc.ajax.utilities.UtilitiesDatabase;
+import java.io.Serializable;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.util.List;
+
+public class CheckStockPricesThread
+ implements Runnable, Serializable
+{
+ static int numberOfThreads = 0;//
+ long numberOfCallsToDatabase = 0;
+ StockDB stockDB;
+ List stocks;
+ String databaseName = "nyse";
+ String userName = "java";
+ String password = "java";
+ String URL2 = "com.mysql.jdbc.Driver";
+ PreparedStatement ps = null;
+ Connection con = null;
+
+ public CheckStockPricesThread() throws Exception
+ {
+ con = new UtilitiesDatabase().connection(databaseName, userName, password, URL2);
+ // close the resources
+ stockDB = new StockDB();
+ System.out.println("CheckStockPrices Runnable created.");
+ stocks = stockDB.findAll(
+ con,
+ databaseName,
+ userName,
+ password,
+ URL2
+ );
+ }
+
+ public List getStocks()
+ {
+ return stocks;
+ }
+
+ @Override
+ public void run()
+ {
+ numberOfThreads++;
+
+ try
+ {
+ while (true)//isInterrupted method Does not clear flag
+ {
+ System.out.println("threads = " + numberOfThreads + "\t" + this +": database-calls = " + ++numberOfCallsToDatabase);
+ stocks = stockDB.findAll(
+ con,
+ databaseName,
+ userName,
+ password,
+ URL2
+ );
+ Thread.sleep(2000);
+ }
+ }
+ catch (InterruptedException e)
+ {
+ System.out.println("---------------------------------Thread interrupt:" + e);
+ }
+ catch (Exception e)
+ {
+ try
+ {
+ new UtilitiesDatabase().closeDatabaseConnection(con);
+ }
+ catch (SQLException se)
+ {
+ System.out.println(se);
+ }
+ }
+ finally
+ {
+ numberOfThreads--;
+ System.out.println(this + " finally called threads=" + numberOfThreads + ", \ttotal database-calls=" + ++numberOfCallsToDatabase);
+ numberOfCallsToDatabase = 0;
+ }
+ }
+}
diff --git a/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/edu/slcc/ajax/bl/Dao.java b/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/edu/slcc/ajax/bl/Dao.java
new file mode 100644
index 0000000..ff1300d
--- /dev/null
+++ b/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/edu/slcc/ajax/bl/Dao.java
@@ -0,0 +1,28 @@
+/*
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template
+ */
+package edu.slcc.ajax.bl;
+
+import java.sql.SQLException;
+import java.util.List;
+
+public interface Dao
+{
+ void create(T t)
+ throws Exception;
+
+ void edit(T t)
+ throws Exception;
+
+ void remove(T t)
+ throws Exception;
+
+ T find(Object id)
+ throws Exception;
+
+ List findAll()
+ throws Exception;
+
+ int count() throws Exception;
+}
diff --git a/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/edu/slcc/ajax/bl/StockDB.java b/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/edu/slcc/ajax/bl/StockDB.java
new file mode 100644
index 0000000..09137b7
--- /dev/null
+++ b/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/edu/slcc/ajax/bl/StockDB.java
@@ -0,0 +1,413 @@
+/*
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
+ */
+package edu.slcc.ajax.bl;
+
+import edu.slcc.ajax.utilities.UtilitiesDatabase;
+import asdv.mp1_ajax.pojos.Stock;
+import java.io.Serializable;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ *
+ * @author asdv5
+ */
+public class StockDB
+ implements Dao, Serializable
+{
+ @Override
+ public List findAll()
+ throws Exception
+ {
+ List tableStocks = new ArrayList();
+ String databaseName = "nyse";
+ String userName = "java";
+ String password = "java";
+ String URL2 = "com.mysql.jdbc.Driver";
+ PreparedStatement ps = null;
+ Connection con = null;
+ try
+ {
+ con = new UtilitiesDatabase().connection(
+ databaseName, userName, password, URL2);
+ if (con == null)
+ {
+ throw new RuntimeException("cannot connect to database");
+ }
+ String table = "";
+ ResultSet rs = null;
+ String sqlStr = "SELECT * FROM stock";
+ //prepare statement
+ ps = con.prepareStatement(sqlStr);
+ //execute
+ rs = ps.executeQuery();
+ while (rs.next())
+ {
+ String stockId = rs.getString(1);
+ String companyName = rs.getString(2);
+ double priceCurrent = rs.getDouble(3);
+ double priceClosing = rs.getDouble(4);
+ long numberOfSharesAvailable = rs.getLong(5);
+ long numberOfSharesSold = rs.getLong(6);
+ Stock stock = new Stock(stockId, companyName, priceCurrent, priceClosing, numberOfSharesAvailable, numberOfSharesSold);
+ tableStocks.add(stock);
+ }
+ }
+ catch (Exception ex)
+ {
+ ex.printStackTrace();
+ throw ex;
+ }
+ finally
+ {
+ try
+ {
+ new UtilitiesDatabase().closeDatabaseConnection(con);
+ // close the resources
+ if (ps != null)
+ {
+ ps.close();
+ }
+ }
+ catch (SQLException sqle)
+ {
+ System.out.println(sqle);
+ sqle.printStackTrace();
+ throw sqle;
+ }
+ }
+ return tableStocks;
+ }
+
+ public List findAll(Connection con,
+ String databaseName,
+ String userName,
+ String password,
+ String URL2
+ )
+ throws Exception
+ {
+ List tableStocks = new ArrayList();
+ PreparedStatement ps = null;
+ try
+ {
+ if (con == null)
+ {
+ throw new RuntimeException("cannot connect to database");
+ }
+ String table = "";
+ ResultSet rs = null;
+ String sqlStr = "SELECT * FROM stock";
+ //prepare statement
+ ps = con.prepareStatement(sqlStr);
+ //execute
+ rs = ps.executeQuery();
+ while (rs.next())
+ {
+ String stockId = rs.getString(1);
+ String companyName = rs.getString(2);
+ double priceCurrent = rs.getDouble(3);
+ double priceClosing = rs.getDouble(4);
+ long numberOfSharesAvailable = rs.getLong(5);
+ long numberOfSharesSold = rs.getLong(6);
+ Stock stock = new Stock(stockId, companyName, priceCurrent, priceClosing, numberOfSharesAvailable, numberOfSharesSold);
+ tableStocks.add(stock);
+ }
+ }
+ catch (Exception ex)
+ {
+ ex.printStackTrace();
+ throw ex;
+ }
+ finally
+ {
+ if (ps != null)
+ {
+ ps.close();
+ }
+ }
+ return tableStocks;
+ }
+
+ @Override
+ public void create(Stock t)
+ throws Exception
+ {
+ int result = 0;
+ Connection con = null;
+ try
+ {
+ con = new UtilitiesDatabase().connection(
+ "nyse", "java", "java", "com.mysql.jdbc.Driver");
+ }
+ catch (Exception e)
+ {
+ System.err.println(e);
+ throw e;
+ }
+ PreparedStatement updateStock = null;
+ try
+ {
+ updateStock = con.prepareStatement(
+ "INSERT INTO stock (stock_id, company_name, price_current, price_closing, number_of_shares_available, number_of_shares_sold ) "
+ + "VALUES ( ?, ?, ? , ? ,? , ?)");
+ updateStock.setString(1, t.getStockId());
+ updateStock.setString(2, t.getCompanyName());
+ updateStock.setDouble(3, t.getPriceCurrent());
+ updateStock.setDouble(4, t.getPriceClosing());
+ updateStock.setLong(5, t.getNumberOfSharesAvailable());
+ updateStock.setLong(6, t.getNumberOfSharesSold());
+ int updateCount = updateStock.executeUpdate();
+ result = updateCount;
+ }
+ catch (SQLException ex)
+ {
+ System.err.println(ex.toString());
+ throw ex;
+ }
+ finally
+ {
+ try
+ {
+ new UtilitiesDatabase().closeDatabaseConnection(con);
+ // close the resources
+ if (updateStock != null)
+ {
+ updateStock.close();
+ }
+ }
+ catch (SQLException e)
+ {
+ System.err.println(e.toString());
+ throw e;
+ }
+ }
+ }
+
+ @Override
+ public void edit(Stock t)
+ throws Exception
+ {
+ String databaseName = "nyse";
+ String userName = "java";
+ String password = "java";
+ String URL2 = "com.mysql.jdbc.Driver";
+ Connection con = null;
+ PreparedStatement updateSupplier = null;
+ try
+ {
+ con = new UtilitiesDatabase().connection(
+ databaseName, userName, password, URL2);
+ if (con == null)
+ {
+ throw new RuntimeException("cannot connect to database");
+ }
+ updateSupplier = null;
+ updateSupplier = con.prepareStatement(
+ "UPDATE stock SET stock_id=?, company_name=?, price_current=?, price_closing=?, number_of_shares_available=?, "
+ + "number_of_shares_sold=? "
+ + "WHERE stock_id=?");
+ updateSupplier.setString(1, t.getStockId());
+ updateSupplier.setString(2, t.getCompanyName());
+ updateSupplier.setDouble(3, t.getPriceCurrent());
+ updateSupplier.setDouble(4, t.getPriceClosing());
+ updateSupplier.setLong(5, t.getNumberOfSharesAvailable());
+ updateSupplier.setLong(6, t.getNumberOfSharesSold());
+ updateSupplier.setString(7, t.getStockId());
+ int result = updateSupplier.executeUpdate();
+ }
+ catch (SQLException ex)
+ {
+ System.err.println(ex.toString());
+ throw ex;
+ }
+ finally
+ {
+ try
+ {
+ new UtilitiesDatabase().closeDatabaseConnection(con);
+ // close the resources
+ if (updateSupplier != null)
+ {
+ updateSupplier.close();
+ }
+ }
+ catch (SQLException sqlee)
+ {
+ sqlee.printStackTrace();
+ throw sqlee;
+ }
+ }
+ }
+
+ @Override
+ public void remove(Stock t)
+ throws Exception
+ {
+ String databaseName = "nyse";
+ String userName = "java";
+ String password = "java";
+ String URL2 = "com.mysql.jdbc.Driver";
+ Connection con = null;
+ PreparedStatement ps = null;
+ try
+ {
+ con = new UtilitiesDatabase().connection(
+ databaseName, userName, password, URL2);
+ if (con == null)
+ {
+ throw new RuntimeException("cannot connect to database");
+ }
+ int rowsAffected = -1;
+ String query = "DELETE FROM stock WHERE stock_id=? ";
+ ps = con.prepareStatement(query);
+ ps.setString(1, t.getStockId());
+ rowsAffected = ps.executeUpdate();
+ }
+ catch (Exception ex)
+ {
+ System.err.println(ex.toString());
+ }
+ finally
+ {
+ try
+ {
+ new UtilitiesDatabase().closeDatabaseConnection(con);
+ // close the resources
+ if (ps != null)
+ {
+ ps.close();
+ }
+ }
+ catch (SQLException sqlee)
+ {
+ sqlee.printStackTrace();
+ }
+ }
+ }
+
+ @Override
+ public Stock find(Object id)
+ throws Exception
+ {
+ String databaseName = "nyse";
+ String userName = "java";
+ String password = "java";
+ String URL2 = "com.mysql.jdbc.Driver";
+ PreparedStatement ps = null;
+ Connection con = null;
+ try
+ {
+ con = new UtilitiesDatabase().connection(
+ databaseName, userName, password, URL2);
+ if (con == null)
+ {
+ throw new RuntimeException("cannot connect to database");
+ }
+ String table = "";
+ ResultSet rs = null;
+ String sqlStr = "SELECT * FROM stock WHERE stock_id=? ";
+ //prepare statement
+ ps = con.prepareStatement(sqlStr);
+ ps.setString(1, (String) id);
+ //execute
+ rs = ps.executeQuery();
+ if (rs.next())
+ {
+ String stockId = rs.getString(1);
+ String companyName = rs.getString(2);
+ double priceCurrent = rs.getDouble(3);
+ double priceClosing = rs.getDouble(4);
+ long numberOfSharesAvailable = rs.getLong(5);
+ long numberOfSharesSold = rs.getLong(6);
+ Stock stock = new Stock(stockId, companyName, priceCurrent, priceClosing, numberOfSharesAvailable, numberOfSharesSold);
+ return stock;
+ }
+ }
+ catch (Exception ex)
+ {
+ ex.printStackTrace();
+ throw ex;
+ }
+ finally
+ {
+ try
+ {
+ new UtilitiesDatabase().closeDatabaseConnection(con);
+ // close the resources
+ if (ps != null)
+ {
+ ps.close();
+ }
+ }
+ catch (SQLException sqle)
+ {
+ System.out.println(sqle);
+ sqle.printStackTrace();
+ throw sqle;
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public int count()
+ throws Exception
+ {
+ String databaseName = "nyse";
+ String userName = "java";
+ String password = "java";
+ String URL2 = "com.mysql.jdbc.Driver";
+ PreparedStatement ps = null;
+ Connection con = null;
+ int rowCount = -1;
+ try
+ {
+ con = new UtilitiesDatabase().connection(
+ databaseName, userName, password, URL2);
+ if (con == null)
+ {
+ throw new RuntimeException("cannot connect to database");
+ }
+ ResultSet rs = null;
+ String sqlStr = "SELECT count(*) " + " FROM " + "stock";
+ ps = con.prepareStatement(sqlStr);
+ rs = ps.executeQuery(sqlStr);
+ if (rs.next() == true)
+ {
+ rowCount = rs.getInt(1);
+ }
+ }
+ catch (SQLException ex)
+ {
+ Logger.getLogger(StockDB.class.getName()).log(Level.SEVERE, null, ex);
+ ex.printStackTrace();
+ throw ex;
+ }
+ finally
+ {
+ try
+ {
+ if (ps != null)
+ {
+ ps.close();
+ }
+ }
+ catch (SQLException sqle)
+ {
+ Logger.getLogger(StockDB.class.getName()).log(Level.WARNING, null, sqle);
+ sqle.printStackTrace();
+ throw sqle;
+ }
+ }
+ return rowCount;
+ }
+}
diff --git a/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/edu/slcc/ajax/utilities/UtilitiesDatabase.java b/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/edu/slcc/ajax/utilities/UtilitiesDatabase.java
new file mode 100644
index 0000000..1220526
--- /dev/null
+++ b/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/edu/slcc/ajax/utilities/UtilitiesDatabase.java
@@ -0,0 +1,111 @@
+/*
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
+ */
+package edu.slcc.ajax.utilities;
+
+import jakarta.faces.application.FacesMessage;
+import jakarta.faces.context.FacesContext;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Iterator;
+
+public class UtilitiesDatabase
+{
+ public Connection connection(
+ String databaseName,
+ String userName,
+ String password,
+ String URL2
+ ) throws SQLException, InstantiationException,
+ IllegalAccessException, ClassNotFoundException
+ {
+ Connection con;
+ try
+ {// Load Sun's jdbc driver
+ Class.forName(URL2).newInstance();
+ System.out.println("JDBC Driver loaded!");
+ }
+ catch (Exception e) // driver not found
+ {
+ System.err.println("Unable to load database driver");
+ System.err.println("Details : " + e);
+ throw e;
+ }
+ String ip = "localhost"; //internet connection
+ String url = "jdbc:mysql://" + ip + ":3306/" + databaseName + "?allowPublicKeyRetrieval=true&useSSL=false";
+ try
+ {
+ con = DriverManager.getConnection(url, userName, password);
+ con.setReadOnly(false);
+ }
+ catch (Exception e)
+ {
+ System.err.println(e.toString());
+ throw e;
+ }
+ System.out.println("connection successfull");
+ return con;
+ }
+
+ public void closeDatabaseConnection(Connection con)
+ throws SQLException
+ {
+ try
+ {
+ if (con != null)
+ {
+ con.close();
+ }
+ }
+ catch (SQLException e)
+ {
+ System.out.println(e);
+ e.printStackTrace();
+ throw e;
+ }
+ }
+
+ public static java.sql.Date stringDateToSqlDate(String sDate)
+ {
+ java.util.Date date = null;
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+ try
+ {
+ date = sdf.parse(sDate);
+ }
+ catch (ParseException e)
+ {
+ e.printStackTrace();
+ }
+ return new java.sql.Date(date.getTime());
+ }
+
+ public static java.util.Date stringDateToJavaUtilitiesDate(String sDate)
+ {
+ java.sql.Date sqldate = stringDateToSqlDate(sDate);
+ return new java.util.Date(sqldate.getTime());
+ }
+
+
+ public static void addMessage(FacesMessage.Severity severity, String summary, String detail)
+ {
+ FacesMessage msg = new FacesMessage(severity, summary, detail);
+ FacesContext.getCurrentInstance().addMessage(null, msg);
+ }
+
+ public static void clearAllMessages()
+ {
+ FacesContext context = FacesContext.getCurrentInstance();
+ Iterator it = context.getMessages();
+ while (it.hasNext())
+ {
+ it.next();
+ it.remove();
+ }
+ }
+
+}
diff --git a/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/factory_1_m/OneToMany.java b/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/factory_1_m/OneToMany.java
new file mode 100644
index 0000000..f451fc7
--- /dev/null
+++ b/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/factory_1_m/OneToMany.java
@@ -0,0 +1,68 @@
+/*
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
+ */
+package factory_1_m;
+
+import java.util.Collection;
+import java.util.Set;
+
+/**
+ *
+ * @author asdv5
+ * @param
+ * @param
+ */
+public interface OneToMany
+{
+ /**
+ * Initialization of Ones. The method should be used first before any other
+ * methods
+ *
+ * @param one - the ones ( i.e, countries, or SelectItem or any Object) to
+ * use for initialization
+ * @return true if the initialization succeeded by using the method once,
+ * false when the method is used more than once.
+ */
+ boolean initializeOne(One... one);
+
+ /**
+ * Initialization of the many for a given one. The method can be used
+ * multiple times after the method initializeOne has succeeded.
+ *
+ * @param one - the one that has the many
+ * @param many - the many which belong to th eone
+ * @throws IllegalArgumentException when the one does not exist (i.e. user's
+ * typing error for the name of one) or when the initialization of the one
+ * has not occurred.
+ *
+ */
+ void initializeMany(One one, Many... many)
+ throws IllegalArgumentException;
+
+
+ /**
+ * Gets the many of a specific one.
+ *
+ * @param one the one to get its many
+ * @return the many of the parameter one or null if the one does not exist.
+ */
+ Collection getMany(One one);
+
+
+
+ /**
+ * Given a value of the many it gets the one that the many belongs to.
+ *
+ * @param many one of the values of the many
+ * @return the one
+ */
+ One getOne(Many many);
+
+ /**
+ * Gets a set with all the ones
+ *
+ * @return the set of ones.
+ */
+ Set getAllOnes();
+}
diff --git a/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/factory_1_m/OneToManyFactory.java b/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/factory_1_m/OneToManyFactory.java
new file mode 100644
index 0000000..61a5e1c
--- /dev/null
+++ b/Semester 3/Assignments/StockBrokerAjaxThreads/src/main/java/factory_1_m/OneToManyFactory.java
@@ -0,0 +1,204 @@
+package factory_1_m;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+
+public class OneToManyFactory
+{
+ /**
+ * Creates an object of type OneToMany
+ *
+ * @param a generic parameter can be any object that denotes a One.
+ * @param a generic parameter can be any object that denotes a city
+ * that belongs to the one generic type.
+ * @return a OneCity object.
+ */
+ public static //generic types to be used in the method
+ OneToMany //rturn type
+ createOneToMany()
+ {
+ return new OneToMany()
+ {
+ private Map