Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a helper for instance customization options #901

Merged
merged 4 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Combine markers resource
  • Loading branch information
ZakarFin committed Dec 8, 2022
commit eecf2f824dd04da8a23311f21ccb2c2ae028272d
15 changes: 6 additions & 9 deletions control-base/src/main/java/fi/nls/oskari/util/EnvHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.oskari.util.Customization;

import java.io.InputStream;
import java.io.IOException;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -34,7 +35,6 @@ public class EnvHelper {

// markers
private static final String KEY_SVG_MARKERS = "svgMarkers";
private static final String SVG_MARKERS_JSON = "svg-markers.json";

// urls
private static final String KEY_URLS = "urls";
Expand Down Expand Up @@ -108,14 +108,11 @@ public static JSONObject getEnvironmentJSON(ActionParameters params, View view)

// setup markers SVG info
try {
InputStream inp = EnvHelper.class.getResourceAsStream(SVG_MARKERS_JSON);
if (inp != null) {
JSONArray svgMarkers = JSONHelper.createJSONArray(IOHelper.readString(inp));
if(svgMarkers != null || svgMarkers.length() > 0) {
JSONHelper.putValue(env, KEY_SVG_MARKERS, svgMarkers);
}
JSONArray svgMarkers = Customization.getMarkers();
if (svgMarkers != null || svgMarkers.length() > 0) {
JSONHelper.putValue(env, KEY_SVG_MARKERS, svgMarkers);
}
} catch (Exception e) {
} catch (IOException e) {
LOGGER.info("No setup for svg markers found", e);
}
return env;
Expand Down

This file was deleted.

32 changes: 32 additions & 0 deletions service-base/src/main/java/org/oskari/util/Customization.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.oskari.util;

import fi.nls.oskari.cache.Cache;
import fi.nls.oskari.cache.CacheManager;
import fi.nls.oskari.util.IOHelper;
import fi.nls.oskari.util.JSONHelper;
import org.json.JSONArray;

import java.io.IOException;
import java.io.InputStream;

public class Customization {
private static final String SVG_MARKERS_JSON = "svg-markers.json";
private static Cache<String> CUSTOMIZATION = CacheManager.getCache("Customization");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit of an overkill if we can't get the logo byte[] here as well. Maybe even if we could store the logos here.

private static final String CACHE_KEY_MARKERS = "markers";

public static JSONArray getMarkers() throws IOException {
// caches are mutable so store as string in case someone modifies the json
String json = CUSTOMIZATION.get(CACHE_KEY_MARKERS);
if (json != null) {
return JSONHelper.createJSONArray(json);
}
try (InputStream is = Customization.class.getResourceAsStream(SVG_MARKERS_JSON)) {
if (is == null) {
throw new IOException("Resource file " + SVG_MARKERS_JSON + " does not exist");
}
json = IOHelper.readString(is);
CUSTOMIZATION.put(CACHE_KEY_MARKERS, json);
return JSONHelper.createJSONArray(json);
}
}
}
19 changes: 5 additions & 14 deletions service-print/src/main/java/org/oskari/print/util/StyleUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -29,18 +28,16 @@
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.oskari.print.PDF;
import org.oskari.util.Customization;
import org.oskari.print.request.PDPrintStyle;
import org.oskari.print.request.PDPrintStyle.LineCap;
import org.oskari.print.request.PDPrintStyle.LineJoin;
import org.oskari.print.request.PDPrintStyle.LinePattern;

import fi.nls.oskari.util.IOHelper;
import fi.nls.oskari.util.JSONHelper;

public class StyleUtil {

private static final String SVG_MARKERS_JSON = "svg-markers.json";
private static final String ICON_STROKE_COLOR = "#000000";
private static final float ICON_SIZE = 32f;
private static final double ICON_OFFSET = ICON_SIZE/2.0;
Expand Down Expand Up @@ -167,18 +164,12 @@ public static PDFormXObject getIcon (PDDocument doc, int shape, String fillColor
}
}

// TODO: get marker data from EnvHelper
public static JSONObject getMarker (int index) throws IOException {
try (InputStream is = PDF.class.getResourceAsStream(SVG_MARKERS_JSON)) {
if (is == null) {
throw new IOException("Resource file " + SVG_MARKERS_JSON + " does not exist");
}
JSONArray svgMarkers = JSONHelper.createJSONArray(IOHelper.readString(is));
if (index >= svgMarkers.length()) {
throw new IOException("SVG marker:" + index + " does not exist");
}
return JSONHelper.getJSONObject(svgMarkers, index);
JSONArray svgMarkers = Customization.getMarkers();
if (index < 0 || index >= svgMarkers.length()) {
throw new IOException("SVG marker:" + index + " does not exist");
}
return JSONHelper.getJSONObject(svgMarkers, index);
}

private static PDFormXObject createIcon (PDDocument doc, JSONObject marker, String fillColor, int size) throws JSONException, IOException, TranscoderException {
Expand Down