|
| 1 | +package de.ulbms.scdh.seed.xc.jena; |
| 2 | + |
| 3 | +import de.ulbms.scdh.seed.xc.api.TransformationPreparationException; |
| 4 | +import jakarta.enterprise.context.ApplicationScoped; |
| 5 | +import java.text.ParseException; |
| 6 | +import java.text.SimpleDateFormat; |
| 7 | +import java.util.Calendar; |
| 8 | +import org.apache.jena.query.ParameterizedSparqlString; |
| 9 | +import org.slf4j.Logger; |
| 10 | +import org.slf4j.LoggerFactory; |
| 11 | + |
| 12 | +/** |
| 13 | + * The {@link ParameterConverter} class casts strings to Java objects based on a xs-type. |
| 14 | + */ |
| 15 | +@ApplicationScoped |
| 16 | +public class ParameterConverter { |
| 17 | + |
| 18 | + private static final Logger LOG = LoggerFactory.getLogger(ParameterConverter.class); |
| 19 | + |
| 20 | + /** |
| 21 | + * Sets the variable with <code>name</code> in the supplied query. |
| 22 | + * |
| 23 | + * @param name - Name of the SPARQL variable |
| 24 | + * @param value - Value to be set |
| 25 | + * @param type - type information |
| 26 | + * @param query - the parametrized query |
| 27 | + * @throws TransformationPreparationException - on a cast failure |
| 28 | + */ |
| 29 | + public void setQueryParameter(String name, String value, String type, ParameterizedSparqlString query) |
| 30 | + throws TransformationPreparationException { |
| 31 | + switch (type) { |
| 32 | + case "xs:anyURI" -> query.setIri(name, value); |
| 33 | + case "xs:string" -> query.setLiteral(name, value); |
| 34 | + case "xs:integer" -> { |
| 35 | + try { |
| 36 | + query.setLiteral(name, Integer.parseInt(value)); |
| 37 | + } catch (NumberFormatException e) { |
| 38 | + LOG.error("failed to cast '{}' value of parameter {} to integer", value, name); |
| 39 | + throw new TransformationPreparationException("failed to set parameter " + name, e); |
| 40 | + } |
| 41 | + } |
| 42 | + case "xs:long" -> { |
| 43 | + try { |
| 44 | + query.setLiteral(name, Long.parseLong(value)); |
| 45 | + } catch (NumberFormatException e) { |
| 46 | + LOG.error("failed to cast '{}' value of parameter {} to long", value, name); |
| 47 | + throw new TransformationPreparationException("failed to set parameter " + name, e); |
| 48 | + } |
| 49 | + } |
| 50 | + case "xs:float" -> { |
| 51 | + try { |
| 52 | + query.setLiteral(name, Float.parseFloat(value)); |
| 53 | + } catch (NumberFormatException e) { |
| 54 | + LOG.error("failed to cast '{}' value of parameter {} to float", value, name); |
| 55 | + throw new TransformationPreparationException("failed to set parameter " + name, e); |
| 56 | + } |
| 57 | + } |
| 58 | + case "xs:double" -> { |
| 59 | + try { |
| 60 | + query.setLiteral(name, Double.parseDouble(value)); |
| 61 | + } catch (NumberFormatException e) { |
| 62 | + LOG.error("failed to cast '{}' value of parameter {} to double", value, name); |
| 63 | + throw new TransformationPreparationException("failed to set parameter " + name, e); |
| 64 | + } |
| 65 | + } |
| 66 | + case "xs:date" -> { |
| 67 | + try { |
| 68 | + Calendar calendar = Calendar.getInstance(); |
| 69 | + SimpleDateFormat sdf = new SimpleDateFormat(); |
| 70 | + calendar.setTime(sdf.parse(value)); |
| 71 | + query.setLiteral(name, calendar); |
| 72 | + } catch (ParseException e) { |
| 73 | + LOG.error("failed to cast '{}' value of parameter {} to calendar", value, name); |
| 74 | + throw new TransformationPreparationException("failed to set parameter " + name, e); |
| 75 | + } |
| 76 | + } |
| 77 | + default -> { |
| 78 | + // defaults to string again |
| 79 | + LOG.error("no valid type information for parameter {}: {}. Using string", name, type); |
| 80 | + query.setLiteral(name, value); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments