Efficiency!
This commit is contained in:
parent
9fdff1ecba
commit
8a9dd0afd4
@ -3,18 +3,18 @@
|
||||
"id": "caiu",
|
||||
"name": "Capital Area Intermediate Unit",
|
||||
"address": "55 Miller St, Enola, PA, 17025",
|
||||
"phone": "(717)732-8400"
|
||||
"number": "(717) 732-8400"
|
||||
},
|
||||
{
|
||||
"id": "caelc",
|
||||
"name": "Capital Area Early Learning Center",
|
||||
"address": "4100 Gettysburg Rd, Camp Hill, PA, 17011",
|
||||
"phone": "(717)732-"
|
||||
"number": "(717) 732-8470"
|
||||
},
|
||||
{
|
||||
"id": "hta",
|
||||
"name": "Hill Top Academy",
|
||||
"address": "405 E Winding Hill Rd, Mechanicsburg, PA, 17055",
|
||||
"phone": "(717)732-8484"
|
||||
"number": "(717) 732-8484"
|
||||
}
|
||||
]
|
@ -1,14 +1,16 @@
|
||||
package tech.nevets.vcardgen;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.io.*;
|
||||
import java.util.List;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
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 ARIAL38I = new Font("Arial", Font.ITALIC, 38);
|
||||
|
||||
private static final List<Location> LOCATIONS = new ArrayList<>();
|
||||
|
||||
public static void main(String[] args) {
|
||||
loadLocations();
|
||||
|
||||
for (Location loc : LOCATIONS) {
|
||||
System.out.println(loc);
|
||||
}
|
||||
|
||||
port(8080);
|
||||
get("/heartbeat", (req, res) -> {
|
||||
@ -45,24 +54,33 @@ public class Main {
|
||||
post("/", (req, res) -> {
|
||||
res.type("image/png");
|
||||
res.header("Access-Control-Allow-Origin", "*");
|
||||
System.out.println(req.body());
|
||||
JsonObject data = new Gson().fromJson(req.body(), JsonObject.class);
|
||||
return generate(
|
||||
data.get("name").getAsString(),
|
||||
data.get("title").getAsString(),
|
||||
data.get("email").getAsString(),
|
||||
data.get("location").getAsString(),
|
||||
data.get("address").getAsString(),
|
||||
data.get("schoolNumber").getAsString(),
|
||||
data.get("locationId").getAsString(),
|
||||
data.get("extension").getAsString(),
|
||||
data.get("hasExtension").getAsBoolean(),
|
||||
data.get("directNumber").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 background;
|
||||
|
||||
@ -96,16 +114,12 @@ public class Main {
|
||||
}
|
||||
g.drawString(title, 89, 176);
|
||||
|
||||
boolean directLine = false;
|
||||
if (hasExtension && extension.length() == 0) {
|
||||
directLine = true;
|
||||
}
|
||||
|
||||
int doubleNumOffset = 0;
|
||||
if (hasExtension && hasCell) {
|
||||
if (!hasCell) {
|
||||
doubleNumOffset = 40;
|
||||
}
|
||||
|
||||
//TODO: Fix when 2 numbers present no mergy
|
||||
int longAddrOffset = 0;
|
||||
fm = g.getFontMetrics(ARIAL38I);
|
||||
if (fm.stringWidth(address) > 694) {
|
||||
@ -152,36 +166,36 @@ public class Main {
|
||||
}
|
||||
|
||||
String number = "";
|
||||
if (hasExtension && !hasCell) {
|
||||
if (directLine) {
|
||||
number = "W: " + schoolNumber;
|
||||
} else {
|
||||
number = "W: " + schoolNumber + " x" + extension;
|
||||
}
|
||||
} else if (!hasExtension && hasCell) {
|
||||
number = "C: " + cellNumber;
|
||||
int numY = hasCell ? 540 : 496;
|
||||
|
||||
if (hasExtension) {
|
||||
number = "W: " + schoolNumber + " x" + extension;
|
||||
}
|
||||
if (hasDirectNumber) {
|
||||
number = "W: " + directNumber;
|
||||
}
|
||||
|
||||
if (!(hasExtension && hasCell)) {
|
||||
g.drawString(number, 59, 536);
|
||||
} else {
|
||||
if (directLine) {
|
||||
g.drawString("W: " + schoolNumber, 59, 496);
|
||||
} else {
|
||||
g.drawString("W: " + schoolNumber + " x" + extension, 59, 496);
|
||||
}
|
||||
g.drawString(number, 59, numY);
|
||||
|
||||
if (hasCell) {
|
||||
g.drawString("C: " + cellNumber, 59, 540);
|
||||
}
|
||||
|
||||
g.dispose();
|
||||
|
||||
BufferedImage finalImage = new BufferedImage(300, 167, BufferedImage.TYPE_INT_ARGB);
|
||||
BufferedImage finalImage;
|
||||
|
||||
|
||||
g = finalImage.createGraphics();
|
||||
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
||||
g.drawImage(rawImage, 0, 0, 300, 167, null);
|
||||
g.dispose();
|
||||
switch (size) {
|
||||
case 0 -> {
|
||||
finalImage = new BufferedImage(300, 167, BufferedImage.TYPE_INT_ARGB);
|
||||
g = finalImage.createGraphics();
|
||||
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;
|
||||
try {
|
||||
@ -194,4 +208,33 @@ public class Main {
|
||||
|
||||
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 + " }";
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user