Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@

import jakarta.persistence.*;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

import java.io.File;
import java.io.IOException;
Expand All @@ -26,8 +22,6 @@ public class Categorical extends StatsFunctions {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private int size;

private List<String> variableNames; //if size == 2, first val = explanatory, second = response

Expand All @@ -43,26 +37,4 @@ public Map<String, Double> calculateRelativeFrequency() {
e -> e.getValue() / total
));
}

// Method to generate a bar chart
public void generateBarChart(String chartTitle, String categoryAxisLabel, String valueAxisLabel, String filePath) throws IOException {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();

items.forEach((key, value) -> {
dataset.addValue(value, "Frequency", key);
});

JFreeChart barChart = ChartFactory.createBarChart(
chartTitle,
categoryAxisLabel,
valueAxisLabel,
dataset,
PlotOrientation.VERTICAL,
true, true, false);

int width = 640; /* Width of the image */
int height = 480; /* Height of the image */
File barChartFile = new File(filePath);
ChartUtils.saveChartAsJPEG(barChartFile, barChart, width, height);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
package com.nighthawk.spring_portfolio.mvc.statsData;

import java.util.List;
import java.util.Map;
import java.util.ArrayList;

import lombok.Data;

@Data
public class CategoricalRequest {
private int size;
private Map<String, Double> data;
private ArrayList<String> data;

// Getters and Setters
public int getSize() {
return size;
}

public void setSize(int size) {
this.size = size;
}

public ArrayList<String> getData() {
return data;
}

public void setData(ArrayList<String> data) {
this.data = data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
import lombok.NoArgsConstructor;

import jakarta.persistence.*;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -47,30 +42,6 @@ public double[][] calculateRelativeFrequencies() {
return relativeFrequencies;
}

// Method to generate a bar chart for the two-way table
public void generateBarChart(String chartTitle, String categoryAxisLabel, String valueAxisLabel, String filePath) throws IOException {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();

for (int i = 0; i < frequencies.length; i++) {
for (int j = 0; j < frequencies[i].length; j++) {
dataset.addValue(frequencies[i][j], explanatoryName + " " + i, responseName + " " + j);
}
}

JFreeChart barChart = ChartFactory.createBarChart(
chartTitle,
categoryAxisLabel,
valueAxisLabel,
dataset,
PlotOrientation.VERTICAL,
true, true, false);

int width = 640; /* Width of the image */
int height = 480; /* Height of the image */
File barChartFile = new File(filePath);
ChartUtils.saveChartAsJPEG(barChartFile, barChart, width, height);
}

// Override toString method to handle the array printing properly
@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,15 @@
import com.nighthawk.spring_portfolio.mvc.jwt.JwtTokenUtil;
import com.nighthawk.spring_portfolio.mvc.qrCode.QrCodeDetailsService;

import jakarta.transaction.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController // annotation to simplify the creation of RESTful web services
@RequestMapping("/api/stats") // all requests in file begin with this URI
@RestController
@RequestMapping("/api/stats")
public class StatsApiController {

// Autowired enables Control to connect URI request and POJO Object to easily for Database CRUD operations
@Autowired
private QuantitativeJpaRepository qRepository;
@Autowired
Expand All @@ -32,43 +29,29 @@ public class StatsApiController {
@Autowired
private TwoCategoricalJpaRepository twoCategoricalRepository;

@Autowired
private JwtTokenUtil tokenUtil;

@Autowired
private QuantitativeDetailsService quantitativeDetailsService;

@Autowired
private TwoQuantitativeDetailsService twoQuantitativeDetailsService;


/* GET List of all of any type of data
* @GetMapping annotation is used for mapping HTTP GET requests onto specific handler methods.
*/

@GetMapping("/twoQuantitative")
@Transactional
public ResponseEntity<List<TwoQuantitative>> getTwoQuantitatives() {
List<TwoQuantitative> twoQuantitatives = twoQRepository.findAll();
twoQuantitatives.forEach(quantitative -> Hibernate.initialize(quantitative.getLsrl()));
return new ResponseEntity<>(twoQuantitatives, HttpStatus.OK);
}
}

@GetMapping("/quantitative")
public ResponseEntity<List<Quantitative>> getQuantitatives() {
List<Quantitative> quantitative = qRepository.findAll();
return new ResponseEntity<>(quantitative, HttpStatus.OK);
}
}

@PostMapping("/newQuantitative")
public ResponseEntity<Quantitative> newQuantitative(@CookieValue("jwt") String jwtToken, @RequestBody QuantitativeRequest quantitativeRequest) {
List<Double> data = quantitativeRequest.getData();
String name = quantitativeRequest.getName();
String name = quantitativeRequest.getName();

String userEmail = tokenUtil.getUsernameFromToken(jwtToken);

Quantitative quantitative = new Quantitative(data, name);

qRepository.save(quantitative);

quantitativeDetailsService.addQuantitativeToUser(userEmail, quantitative.getId());
Expand All @@ -77,15 +60,15 @@ public ResponseEntity<Quantitative> newQuantitative(@CookieValue("jwt") String j
}

@PostMapping("/newTwoQuantitative")
public ResponseEntity<TwoQuantitative> newCode(@CookieValue("jwt") String jwtToken, @RequestBody TwoQuantitativeRequest twoQuantitativeRequest) {
public ResponseEntity<TwoQuantitative> newTwoQuantitative(@RequestBody TwoQuantitativeRequest twoQuantitativeRequest) {
List<Double> explanatory = twoQuantitativeRequest.getExplanatory();
List<Double> response = twoQuantitativeRequest.getResponse();
String explanatoryName = twoQuantitativeRequest.getExplanatoryName();
String responseName = twoQuantitativeRequest.getResponseName();
String explanatoryName = twoQuantitativeRequest.getExplanatoryName();
String responseName = twoQuantitativeRequest.getResponseName();

Quantitative quantitative1 = new Quantitative(explanatory, explanatoryName);
Quantitative quantitative2 = new Quantitative(response, responseName);

qRepository.save(quantitative1);
qRepository.save(quantitative2);

Expand All @@ -103,33 +86,28 @@ public ResponseEntity<TwoQuantitative> newCode(@CookieValue("jwt") String jwtTok
twoQuantitativeDetailsService.addTwoQuantitativeToUser(userEmail, twoQuantitative.getId());

return new ResponseEntity<>(twoQuantitative, HttpStatus.OK);
}
}

/* GET Specific of any type of data
* @GetMapping annotation is used for mapping HTTP GET requests onto specific handler methods.
*/
@GetMapping("/getQuantitative{id}")
@GetMapping("/getQuantitative/{id}")
public ResponseEntity<Quantitative> getQuantitative(@PathVariable long id) {
Optional<Quantitative> optional = qRepository.findById(id);
if (optional.isPresent()) { // Good ID
Quantitative quantitative = optional.get(); // value from findByID
return new ResponseEntity<>(quantitative, HttpStatus.OK); // OK HTTP response: status code, headers, and body
if (optional.isPresent()) {
Quantitative quantitative = optional.get();
return new ResponseEntity<>(quantitative, HttpStatus.OK);
}
// Bad ID
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}

@GetMapping("/getTwoQuantitative{id}")
@GetMapping("/getTwoQuantitative/{id}")
@Transactional
public ResponseEntity<TwoQuantitative> getTwoQuantitative(@PathVariable long id) {
Optional<TwoQuantitative> optional = twoQRepository.findById(id);
if (optional.isPresent()) { // Good ID
TwoQuantitative twoQuantitative = optional.get(); // value from findByID
if (optional.isPresent()) {
TwoQuantitative twoQuantitative = optional.get();
Hibernate.initialize(twoQuantitative.getLsrl());
return new ResponseEntity<>(twoQuantitative, HttpStatus.OK); // OK HTTP response: status code, headers, and body
return new ResponseEntity<>(twoQuantitative, HttpStatus.OK);
}
// Bad ID
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}

@GetMapping("/categorical")
Expand All @@ -141,6 +119,7 @@ public ResponseEntity<List<Categorical>> getCategoricals() {
@PostMapping("/newCategorical")
public ResponseEntity<Categorical> newCategorical(@RequestBody CategoricalRequest categoricalRequest) {
int size = categoricalRequest.getSize();
ArrayList<String> data = categoricalRequest.getData();
Map<String, Double> data = categoricalRequest.getData();

Categorical categorical = new Categorical();
Expand All @@ -152,15 +131,14 @@ public ResponseEntity<Categorical> newCategorical(@RequestBody CategoricalReques
return new ResponseEntity<>(categorical, HttpStatus.OK);
}

@GetMapping("/getCategorical{id}")
@GetMapping("/getCategorical/{id}")
public ResponseEntity<Categorical> getCategorical(@PathVariable long id) {
Optional<Categorical> optional = cRepository.findById(id);
if (optional.isPresent()) { // Good ID
Categorical categorical = optional.get(); // value from findByID
return new ResponseEntity<>(categorical, HttpStatus.OK); // OK HTTP response: status code, headers, and body
if (optional.isPresent()) {
Categorical categorical = optional.get();
return new ResponseEntity<>(categorical, HttpStatus.OK);
}
// Bad ID
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}

@GetMapping("/categoricalVars")
Expand All @@ -185,7 +163,7 @@ public ResponseEntity<CategoricalVars> newCategoricalVars(@RequestBody Categoric
return new ResponseEntity<>(categoricalVars, HttpStatus.OK);
}

@GetMapping("/getCategoricalVars{id}")
@GetMapping("/getCategoricalVars/{id}")
public ResponseEntity<CategoricalVars> getCategoricalVars(@PathVariable long id) {
Optional<CategoricalVars> optional = cVarsRepository.findById(id);
if (optional.isPresent()) {
Expand All @@ -203,21 +181,23 @@ public ResponseEntity<List<TwoCategorical>> getTwoCategoricals() {

@PostMapping("/newTwoCategorical")
public ResponseEntity<TwoCategorical> newTwoCategorical(@RequestBody TwoCategoricalRequest twoCategoricalRequest) {
String explanatoryName = twoCategoricalRequest.getExplanatoryName();
String responseName = twoCategoricalRequest.getResponseName();
double[][] frequencies = twoCategoricalRequest.getFrequencies();
String explanatory = twoCategoricalRequest.getExplanatory();
String response = twoCategoricalRequest.getResponse();
int freq = twoCategoricalRequest.getFreq();
double relFreq = twoCategoricalRequest.getRelFreq();

TwoCategorical twoCategorical = new TwoCategorical();
twoCategorical.setExplanatoryName(explanatoryName);
twoCategorical.setResponseName(responseName);
twoCategorical.setFrequencies(frequencies);
twoCategorical.setExplanatory(explanatory);
twoCategorical.setResponse(response);
twoCategorical.setFreq(freq);
twoCategorical.setRelFreq(relFreq);

twoCategoricalRepository.save(twoCategorical);

return new ResponseEntity<>(twoCategorical, HttpStatus.OK);
}

@GetMapping("/getTwoCategorical{id}")
@GetMapping("/getTwoCategorical/{id}")
public ResponseEntity<TwoCategorical> getTwoCategorical(@PathVariable long id) {
Optional<TwoCategorical> optional = twoCategoricalRepository.findById(id);
if (optional.isPresent()) {
Expand Down
Loading