Пример #1
0
  public Handler(Query query, Form form) throws ResourceException {
    this.query = query;
    this.queryString = form.getQueryString();
    for (QueryParameter p : query.getParameters()) {

      String name = p.getName();
      String svalue = form.getFirstValue(name, true);

      switch (p.getType()) {
        case DATE:
          parameters.put(p, readParameterValue(Date.class, p, svalue));
          break;

        case NUMBER:
          parameters.put(p, readParameterValue(BigDecimal.class, p, svalue));
          break;

        case STRING:
          parameters.put(p, readParameterValue(String.class, p, svalue));
          break;

        case CLOB:
        case BLOB:
          throw new ClientErrorException(
              Status.CLIENT_ERROR_BAD_REQUEST,
              String.format("LOBs are not supported as parameters: %s", name));
      }
    }

    if (log.isDebugEnabled()) {
      for (QueryParameter qp : parameters.keySet()) {
        log.debug(qp.toString(parameters.get(qp)));
      }
    }
  }
Пример #2
0
  public void applyParameters(SQLQuery query) throws SQLException {
    for (Map.Entry<QueryParameter, Object> e : parameters.entrySet()) {

      QueryParameter p = e.getKey();
      Object value = e.getValue();
      String name = p.getSqlName();

      if (value != null && p.getArray()) {

        switch (p.getType()) {
          case DATE:
            query.setParameterList(name, (Object[]) value, new DateType());
            break;
          case NUMBER:
            query.setParameterList(name, (Object[]) value, new BigDecimalType());
            break;
          case STRING:
            query.setParameterList(name, (Object[]) value, new StringType());
            break;
          case CLOB:
          case BLOB:
            throw new ClientErrorException(
                Status.CLIENT_ERROR_BAD_REQUEST,
                String.format("LOBs are not supported as parameters: %s", name));
        }

      } else {

        switch (p.getType()) {
          case DATE:
            query.setDate(name, (Date) value);
            break;
          case NUMBER:
            query.setBigDecimal(name, (BigDecimal) value);
            break;
          case STRING:
            query.setString(name, (String) value);
            break;
          case CLOB:
          case BLOB:
            throw new ClientErrorException(
                Status.CLIENT_ERROR_BAD_REQUEST,
                String.format("LOBs are not supported as parameters: %s", name));
        }
      }
    }
  }
Пример #3
0
 private static <T> Object readParameterValue(Class<T> clazz, QueryParameter param, String svalue)
     throws ResourceException {
   if (svalue == null) {
     return null;
   } else {
     try {
       if (param.getArray()) {
         JSONArray ja = new JSONArray(svalue);
         List<T> a = new ArrayList<>();
         for (int i = 0; i < ja.length(); i++) {
           a.add(BaseResource.convertValue(clazz, ja.getString(i)));
         }
         return a.toArray();
       } else {
         return BaseResource.convertValue(clazz, svalue);
       }
     } catch (JSONException ex) {
       throw new ClientErrorException(Status.CLIENT_ERROR_BAD_REQUEST, ex);
     }
   }
 }