示例#1
0
  @Override
  public Object apply(final ActionContext ctx, final GraphObject entity, final Object[] sources)
      throws FrameworkException {

    if (arrayHasLengthAndAllElementsNotNull(sources, 1)) {

      return (int) Math.floor(Double.parseDouble(sources[0].toString()));

    } else {

      logParameterError(entity, sources, ctx.isJavaScriptContext());

      return usage(ctx.isJavaScriptContext());
    }
  }
示例#2
0
  public static Object execute(
      final SecurityContext securityContext,
      final GraphObject entity,
      final String source,
      final Map<String, Object> parameters)
      throws FrameworkException {

    final ActionContext context = new ActionContext(securityContext, parameters);
    final Object result = Scripting.evaluate(context, entity, source);

    // check for errors raised by scripting
    if (context.hasError()) {
      throw new FrameworkException(422, "Server-side scripting error", context.getErrorBuffer());
    }

    return result;
  }
示例#3
0
  @Override
  public Object apply(final ActionContext ctx, final GraphObject entity, final Object[] sources)
      throws FrameworkException {

    try {
      if (!arrayHasLengthAndAllElementsNotNull(sources, 1)) {

        return null;
      }

      return sources[0].toString().toLowerCase();

    } catch (final IllegalArgumentException e) {

      logParameterError(entity, sources, ctx.isJavaScriptContext());
      return usage(ctx.isJavaScriptContext());
    }
  }
示例#4
0
  public Object transformOutput(final ActionContext actionContext, final GraphObject source)
      throws FrameworkException {

    if (outputFunction == null) {
      return source.getProperty(sourceProperty);
    }

    // output transformation requested
    actionContext.setConstant("input", source);
    return Scripting.evaluate(actionContext, null, "${" + outputFunction + "}");
  }
示例#5
0
  public void transformInput(final ActionContext actionContext, final Map<String, Object> source)
      throws FrameworkException {

    // move / rename input value
    Object inputValue = source.remove(targetName);
    if (inputValue != null) {

      if (inputFunction != null) {

        // input transformation requested
        actionContext.setConstant("input", inputValue);
        inputValue = Scripting.evaluate(actionContext, null, "${" + inputFunction + "}");
      }

      source.put(sourceName, inputValue);
    }
  }
示例#6
0
  @Override
  public Object apply(final ActionContext ctx, final GraphObject entity, final Object[] sources)
      throws FrameworkException {

    if (arrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 1, 2)) {

      final String toSplit = sources[0].toString();
      String splitExpr = "[,;\\s]+";

      if (sources.length >= 2) {
        splitExpr = sources[1].toString();
      }

      return Arrays.asList(toSplit.split(splitExpr));

    } else {

      logParameterError(entity, sources, ctx.isJavaScriptContext());
    }

    return "";
  }
示例#7
0
  @Override
  public Object apply(final ActionContext ctx, final GraphObject entity, final Object[] sources)
      throws FrameworkException {

    if (arrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 1, 2)) {

      final String cacheKey = cacheKey(sources);
      String value = getCachedValue(cacheKey);

      if (value == null) {

        final SecurityContext superUserSecurityContext = SecurityContext.getSuperUserInstance();
        final String locale = ctx.getLocale().toString();
        final String name = sources[0].toString();

        Query query =
            StructrApp.getInstance(superUserSecurityContext)
                .nodeQuery(Localization.class)
                .and(Localization.locale, locale)
                .and(Localization.name, name);
        List<Localization> localizations;

        final Locale ctxLocale = ctx.getLocale();
        final String fullLocale = ctxLocale.toString();
        final String lang = ctxLocale.getLanguage();

        if (sources.length == 2) {

          final String domain = sources[1].toString();

          // with domain
          query.and(Localization.domain, domain);

          localizations = query.getAsList();

          if (localizations.isEmpty() && fullLocale.contains("_")) {

            // no language-specific localization found, try language code only

            query =
                StructrApp.getInstance(superUserSecurityContext)
                    .nodeQuery(Localization.class)
                    .and(Localization.locale, lang)
                    .and(Localization.name, name)
                    .and(Localization.domain, domain);

            localizations = query.getAsList();
          }

        } else {

          // without domain
          query.blank(Localization.domain);

          localizations = query.getAsList();

          if (localizations.isEmpty() && fullLocale.contains("_")) {

            // no language-specific localization found, try language code only

            query =
                StructrApp.getInstance(superUserSecurityContext)
                    .nodeQuery(Localization.class)
                    .and(Localization.locale, lang)
                    .and(Localization.name, name)
                    .blank(Localization.domain);

            localizations = query.getAsList();
          }
        }

        if (localizations.size() > 1) {

          // Ambiguous localization found
          if (sources.length > 1) {

            logger.warn(
                "Found ambiguous localization for key \"{}\" and domain \"{}\". Please fix. Parameters: {}",
                new Object[] {
                  sources[0].toString(), sources[1].toString(), getParametersAsString(sources)
                });

          } else {

            logger.warn(
                "Found ambiguous localization for key \"{}\". Please fix. Parameters: {}",
                new Object[] {sources[0].toString(), getParametersAsString(sources)});
          }
        }

        // return first localization
        if (localizations.isEmpty()) {

          // no localization found - return the key
          value = name;

        } else {

          value = localizations.get(0).getProperty(Localization.localizedName);
        }

        cacheValue(cacheKey, value);
      }

      return value;

    } else if (sources.length == 1 || sources.length == 2) {

      logParameterError(entity, sources, ctx.isJavaScriptContext());

      // silently ignore null values
      return "";

    } else {

      logParameterError(entity, sources, ctx.isJavaScriptContext());

      // only show the error message for wrong parameter count
      return usage(ctx.isJavaScriptContext());
    }
  }