Efficiency!

This commit is contained in:
Steven Tracey 2023-05-10 14:18:24 -04:00
parent 9fdff1ecba
commit 8a9dd0afd4
2 changed files with 83 additions and 40 deletions

View File

@ -3,18 +3,18 @@
"id": "caiu", "id": "caiu",
"name": "Capital Area Intermediate Unit", "name": "Capital Area Intermediate Unit",
"address": "55 Miller St, Enola, PA, 17025", "address": "55 Miller St, Enola, PA, 17025",
"phone": "(717)732-8400" "number": "(717) 732-8400"
}, },
{ {
"id": "caelc", "id": "caelc",
"name": "Capital Area Early Learning Center", "name": "Capital Area Early Learning Center",
"address": "4100 Gettysburg Rd, Camp Hill, PA, 17011", "address": "4100 Gettysburg Rd, Camp Hill, PA, 17011",
"phone": "(717)732-" "number": "(717) 732-8470"
}, },
{ {
"id": "hta", "id": "hta",
"name": "Hill Top Academy", "name": "Hill Top Academy",
"address": "405 E Winding Hill Rd, Mechanicsburg, PA, 17055", "address": "405 E Winding Hill Rd, Mechanicsburg, PA, 17055",
"phone": "(717)732-8484" "number": "(717) 732-8484"
} }
] ]

View File

@ -1,14 +1,16 @@
package tech.nevets.vcardgen; package tech.nevets.vcardgen;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import java.io.*;
import java.util.List;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream; import java.util.ArrayList;
import java.io.FileInputStream;
import java.io.IOException;
import static spark.Spark.*; import static spark.Spark.*;
@ -25,7 +27,14 @@ public class Main {
private static final Font ARIAL40I = new Font("Arial", Font.ITALIC, 40); private static final Font ARIAL40I = new Font("Arial", Font.ITALIC, 40);
private static final Font ARIAL38I = new Font("Arial", Font.ITALIC, 38); private static final Font ARIAL38I = new Font("Arial", Font.ITALIC, 38);
private static final List<Location> LOCATIONS = new ArrayList<>();
public static void main(String[] args) { public static void main(String[] args) {
loadLocations();
for (Location loc : LOCATIONS) {
System.out.println(loc);
}
port(8080); port(8080);
get("/heartbeat", (req, res) -> { get("/heartbeat", (req, res) -> {
@ -45,24 +54,33 @@ public class Main {
post("/", (req, res) -> { post("/", (req, res) -> {
res.type("image/png"); res.type("image/png");
res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Origin", "*");
System.out.println(req.body());
JsonObject data = new Gson().fromJson(req.body(), JsonObject.class); JsonObject data = new Gson().fromJson(req.body(), JsonObject.class);
return generate( return generate(
data.get("name").getAsString(), data.get("name").getAsString(),
data.get("title").getAsString(), data.get("title").getAsString(),
data.get("email").getAsString(), data.get("email").getAsString(),
data.get("location").getAsString(), data.get("locationId").getAsString(),
data.get("address").getAsString(),
data.get("schoolNumber").getAsString(),
data.get("extension").getAsString(), data.get("extension").getAsString(),
data.get("hasExtension").getAsBoolean(), data.get("directNumber").getAsString(),
data.get("cellNumber").getAsString(), data.get("cellNumber").getAsString(),
data.get("hasCell").getAsBoolean() data.get("size").getAsInt()
); );
}); });
} }
private static byte[] generate(String name, String title, String email, String location, String address, String schoolNumber, String extension, boolean hasExtension, String cellNumber, boolean hasCell) { private static byte[] generate(String name, String title, String email, String locationId, String extension, String directNumber, String cellNumber, int size) {
Location loc = getLocation(locationId);
assert loc != null;
String location = loc.name();
String address = loc.address();
String schoolNumber = loc.number();
boolean hasExtension = extension.length() > 0;
boolean hasDirectNumber = directNumber.length() > 0;
boolean hasCell = cellNumber.length() > 0;
BufferedImage rawImage = new BufferedImage(1080, 602, BufferedImage.TYPE_INT_ARGB); BufferedImage rawImage = new BufferedImage(1080, 602, BufferedImage.TYPE_INT_ARGB);
BufferedImage background; BufferedImage background;
@ -96,16 +114,12 @@ public class Main {
} }
g.drawString(title, 89, 176); g.drawString(title, 89, 176);
boolean directLine = false;
if (hasExtension && extension.length() == 0) {
directLine = true;
}
int doubleNumOffset = 0; int doubleNumOffset = 0;
if (hasExtension && hasCell) { if (!hasCell) {
doubleNumOffset = 40; doubleNumOffset = 40;
} }
//TODO: Fix when 2 numbers present no mergy
int longAddrOffset = 0; int longAddrOffset = 0;
fm = g.getFontMetrics(ARIAL38I); fm = g.getFontMetrics(ARIAL38I);
if (fm.stringWidth(address) > 694) { if (fm.stringWidth(address) > 694) {
@ -152,36 +166,36 @@ public class Main {
} }
String number = ""; String number = "";
if (hasExtension && !hasCell) { int numY = hasCell ? 540 : 496;
if (directLine) {
number = "W: " + schoolNumber; if (hasExtension) {
} else { number = "W: " + schoolNumber + " x" + extension;
number = "W: " + schoolNumber + " x" + extension; }
} if (hasDirectNumber) {
} else if (!hasExtension && hasCell) { number = "W: " + directNumber;
number = "C: " + cellNumber;
} }
if (!(hasExtension && hasCell)) { g.drawString(number, 59, numY);
g.drawString(number, 59, 536);
} else { if (hasCell) {
if (directLine) {
g.drawString("W: " + schoolNumber, 59, 496);
} else {
g.drawString("W: " + schoolNumber + " x" + extension, 59, 496);
}
g.drawString("C: " + cellNumber, 59, 540); g.drawString("C: " + cellNumber, 59, 540);
} }
g.dispose(); g.dispose();
BufferedImage finalImage = new BufferedImage(300, 167, BufferedImage.TYPE_INT_ARGB); BufferedImage finalImage;
switch (size) {
g = finalImage.createGraphics(); case 0 -> {
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); finalImage = new BufferedImage(300, 167, BufferedImage.TYPE_INT_ARGB);
g.drawImage(rawImage, 0, 0, 300, 167, null); g = finalImage.createGraphics();
g.dispose(); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(rawImage, 0, 0, 300, 167, null);
g.dispose();
}
case 1 -> finalImage = rawImage;
default -> finalImage = background;
}
ByteArrayOutputStream baos; ByteArrayOutputStream baos;
try { try {
@ -194,4 +208,33 @@ public class Main {
return baos.toByteArray(); return baos.toByteArray();
} }
private static void loadLocations() {
try {
Gson gson = new Gson();
JsonArray jsonFile = gson.fromJson(new FileReader("./locations.json"), JsonArray.class);
for (JsonElement element : jsonFile.asList()) {
LOCATIONS.add(gson.fromJson(element, Location.class));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private static Location getLocation(String locationId) {
for (Location loc : LOCATIONS) {
if (loc.id().equals(locationId)) {
return loc;
}
}
return null;
}
}
record Location(String id, String name, String address, String number) {
@Override
public String toString() {
return "{ \"id\": " + id + ", \"name\": " + name + ", \"address\": " + address + ", \"number\": " + number + " }";
}
} }