import (not working)

This commit is contained in:
2025-10-12 15:51:38 +01:00
commit 0dfc15c7ab
10 changed files with 562 additions and 0 deletions

145
pom.xml Normal file
View File

@@ -0,0 +1,145 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>uk.org.mafoo.cpomssummariser</groupId>
<artifactId>cpomssummariser</artifactId>
<version>1.0-SNAPSHOT</version>
<name>cpomssummariser</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>17</maven.compiler.release>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.11.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<!-- Optionally: parameterized tests support -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.3.0</version>
</dependency>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-jexcel</artifactId>
<version>1.0.9</version>
</dependency>
<dependency>
<groupId>org.dhatim</groupId>
<artifactId>fastexcel-reader</artifactId>
<version>0.18.1</version>
</dependency>
<dependency>
<groupId>org.dhatim</groupId>
<artifactId>fastexcel</artifactId>
<version>0.18.1</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.4.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.3.0</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.4.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>3.1.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.1.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.12.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.6.1</version>
</plugin>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>uk.org.mafoo.cpomssummariser.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<!-- Add the assemble plugin with standard configuration -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>uk.org.mafoo.cpomssummariser.App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

View File

@@ -0,0 +1,66 @@
package uk.org.mafoo.cpomssummariser;
import java.io.*;
import java.util.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
/**
* Hello world!
*/
public class App {
public static void xlsxtest(String filename) throws IOException {
FileInputStream file = new FileInputStream(new File(filename));
//Create Workbook instance holding reference to .xlsx file
XSSFWorkbook workbook = new XSSFWorkbook(file);
//Get first/desired sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
//Iterate through each rows one by one
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
//For each row, iterate through all the columns
Iterator<Cell> cellIterator = row.cellIterator();
System.out.print(" | ");
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
//Check the cell type and format accordingly
switch (cell.getCellType()) {
case NUMERIC:
System.out.print(cell.getNumericCellValue());
break;
case STRING:
System.out.print(cell.getStringCellValue());
break;
default:
System.out.print(cell.getNumericCellValue());
}
System.out.print(" | ");
}
System.out.println("");
}
file.close();
}
public static void main(String[] args) throws IOException {
String filename = "testdata.xlsx";
if(args.length > 0) filename = args[0];
FileInputStream file = new FileInputStream(filename);
List<List<String>> data = Loader.loadXlsx(file);
// System.out.println(data);
List<Record> records = Parser.parse(data);
Summariser summariser = new Summariser(records);
System.out.println("Ending...");
}
}

View File

@@ -0,0 +1,8 @@
package uk.org.mafoo.cpomssummariser.DataModel;
import java.util.List;
import java.util.Map;
public record Category(String superior, String name, Map<String,List<CategoryGroupData>> data) {
}

View File

@@ -0,0 +1,67 @@
package uk.org.mafoo.cpomssummariser.DataModel;
import java.util.HashMap;
import java.util.Map;
public class CategoryGroupData {
String group;
Map<String, Integer> sex;
Integer SEND;
Integer PP;
public CategoryGroupData(String group, Map<String, Integer> sex, Integer SEND, Integer PP) {
this.group = group;
this.sex = sex;
this.SEND = SEND;
this.PP = PP;
}
public CategoryGroupData(String group) {
this(
group,
new HashMap<String, Integer>(),
0,
0
);
this.sex.put("Male", 0);
this.sex.put("Female", 0);
// System.out.println("[CategoryGroupData] Created new group: " + group);
}
public void incrementPP() {
this.PP += 1;
}
public void incrementSEND() {
this.SEND += 1;
}
public void incrementSexValue(String sex) {
assert(sex.equals("Male") || sex.equals("Female"));
this.sex.put(sex, this.sex.get(sex) + 1);
}
public int getPP() {
return this.PP.intValue();
}
public int getSEND() {
return this.SEND.intValue();
}
public int getSexValue(String sex) {
assert(sex.equals("Male") || sex.equals("Female"));
return this.sex.get(sex).intValue();
}
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Group="); stringBuilder.append(this.group); stringBuilder.append(" ");
stringBuilder.append("Male="); stringBuilder.append(this.getSexValue("Male")); stringBuilder.append(" ");
stringBuilder.append("Female="); stringBuilder.append(this.getSexValue("Female")); stringBuilder.append(" ");
stringBuilder.append("SEND="); stringBuilder.append(this.getSEND()); stringBuilder.append(" ");
stringBuilder.append("PP="); stringBuilder.append(this.getPP()); stringBuilder.append(" ");
return stringBuilder.toString();
}
}

View File

@@ -0,0 +1,11 @@
package uk.org.mafoo.cpomssummariser.DataModel;
public enum Groups {
YEAR_7,
YEAR_8,
YEAR_9,
YEAR_10,
YEAR_11,
YEAR_12,
YEAR_13
}

View File

@@ -0,0 +1,65 @@
package uk.org.mafoo.cpomssummariser;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Loader {
public static List<List<String>> loadXlsx(FileInputStream fd) throws IOException {
return loadXlsx(fd, true);
}
public static List<List<String>> loadXlsx(FileInputStream fd, boolean forceGroupToInteger) throws IOException {
List<List<String>> data = new ArrayList<>();
XSSFWorkbook workbook = new XSSFWorkbook(fd);
// Get first/desired sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
// Iterate through each rows one by one
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
List<String> rowdata = new ArrayList<String>();
// For each row, iterate through all the columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
// Check the cell type and format accordingly
switch (cell.getCellType()) {
case NUMERIC:
if(forceGroupToInteger) {
rowdata.add( "" + ((int)cell.getNumericCellValue()) );
} else {
rowdata.add("" + cell.getNumericCellValue());
}
break;
case STRING:
rowdata.add(cell.getStringCellValue());
break;
default:
rowdata.add(cell.getStringCellValue());
}
// rowdata.add(cell.getStringCellValue());
}
data.add(rowdata);
}
workbook.close();
return data;
}
}

View File

@@ -0,0 +1,23 @@
package uk.org.mafoo.cpomssummariser;
import java.util.ArrayList;
import java.util.List;
public class Parser {
private static final int expected = 5;
public static List<Record> parse(List<List<String>> in) {
List<Record> out = new ArrayList<Record>();
for (List<String> row : in) {
if (row.get(0).equals("Sex")) continue;
// System.out.println("Parsing: " + String.join(" :: ", row));
int missing = expected - row.size();
while(missing-- > 0) row.add(""); // Fill in the gaps
Record r = new Record(row.get(0), row.get(1), row.get(2), row.get(3), row.get(4));
out.add(r);
}
return out;
}
}

View File

@@ -0,0 +1,101 @@
package uk.org.mafoo.cpomssummariser;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Record {
private final String p_sex;
private final String p_group;
private final Map<String, List<String>> p_categories;
private final boolean p_send;
private final boolean p_pp;
private static final String topLevelCategories[] = {
"Cause for Concern - Green form",
"Bullying/ Friendship Related Issues"
};
private static List<String> p_groups = new ArrayList<String>();
private Map<String, List<String>> splitCategories(String str) {
Map<String, List<String>> categories = new HashMap<String, List<String>>();
for (String cat : topLevelCategories) {
categories.put(cat, new ArrayList<String>());
}
String currentTopLevelCategory = null;
for (String cat : str.split(", ")) {
if (cat.equals("Categories")) continue; // Skip the header line
if (categories.containsKey(cat)) {
currentTopLevelCategory = cat;
} else {
assert(currentTopLevelCategory != null);
categories.get(currentTopLevelCategory).add(cat);
}
}
return categories;
}
private boolean boolstrToBool(String str) {
if(str == "Yes")
return true;
else
return false;
}
public Record (String sex, String group, String categories, String send, String pp) {
this.p_sex = sex;
this.p_group = "Year " + group;
this.p_categories = splitCategories(categories);
this.p_send = boolstrToBool(send);
this.p_pp = boolstrToBool(pp);
if(! p_groups.contains(group)) p_groups.add("Year " + group);
}
protected String getSex() {
return p_sex;
}
protected String getGroup() {
return p_group;
}
protected Map<String, List<String>> getCategories() {
return p_categories;
}
protected boolean getSEND() {
return p_send;
}
protected boolean getPP() {
return p_pp;
}
public String toString() {
return Map.of(
"Sex", getSex(),
"Group", getGroup(),
"Categories", getCategories(),
"SEND", getSEND(),
"PP", getPP()
).toString();
}
public static List<String> getGroups() {
return Collections.unmodifiableList(p_groups);
}
public static List<String> getTopLevelCategories() {
return new ArrayList<String>(Arrays.asList(topLevelCategories));
}
}

View File

@@ -0,0 +1,57 @@
package uk.org.mafoo.cpomssummariser;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import uk.org.mafoo.cpomssummariser.DataModel.CategoryGroupData;
public class Summariser {
List<Record> records;
Map<String, Map<String, CategoryGroupData>> data = new HashMap<String, Map<String, CategoryGroupData>>();
public Summariser(List<Record> records) {
this.records = records;
// Prepare the top-level data structure
for (String c : Record.getTopLevelCategories()) {
Map<String, CategoryGroupData> cat = new HashMap<String, CategoryGroupData>();
for (String g : Record.getGroups()) {
cat.put(g, new CategoryGroupData(g));
}
data.put(c, cat);
System.out.println("[Summariser] Created: " + c);
}
process();
}
Map<String, CategoryGroupData> data_getOrCreate(String cat) {
if(! data.containsKey(cat)) {
Map<String, CategoryGroupData> tmp = new HashMap<String, CategoryGroupData>();
for (String g : Record.getGroups()) {
tmp.put(g, new CategoryGroupData(g));
}
data.put(cat, tmp);
}
return data.get(cat);
}
public void process() {
for (Record record : records) {
System.out.println("--> " + record);
for (Entry<String, List<String>> category : record.getCategories().entrySet()) {
for (String subcat : category.getValue()) {
String group = record.getGroup();
Map<String, CategoryGroupData> x = data_getOrCreate(subcat);
CategoryGroupData blob = x.get(group);
if(record.getSEND()) blob.incrementSEND();
if(record.getPP()) blob.incrementPP();
blob.incrementSexValue(record.getSex());
}
}
}
}
}

View File

@@ -0,0 +1,19 @@
package uk.org.mafoo.cpomssummariser;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
/**
* Unit test for simple App.
*/
public class AppTest {
/**
* Rigorous Test :-)
*/
@Test
public void shouldAnswerWithTrue() {
assertTrue(true);
}
}