Ejemplo n.º 1
0
  @Override
  public StrategyResult evaluate(StrategyContext context) {
    Home home = null;

    // take the name from the argument, if given
    String name = context.getArg();
    // otherwise use the name given at instantiation
    if (name == null) name = homeName;

    log.debug("HomeNamedHome: name={}", name);

    if (name != null) {
      home = homeDAO.findHomeByNameAndPlayer(name, context.getPlayer().getName());

      log.debug(
          "HomeNamedHome: home pre-modes={}, current modes={}", home, context.getCurrentModes());

      if (context.isModeEnabled(StrategyMode.MODE_HOME_DEFAULT_ONLY) && !home.isDefaultHome())
        home = null;
      if (context.isModeEnabled(StrategyMode.MODE_HOME_BED_ONLY) && !home.isBedHome()) home = null;
      if (context.isModeEnabled(StrategyMode.MODE_HOME_NO_BED) && home.isBedHome()) home = null;
      if (context.isModeEnabled(StrategyMode.MODE_HOME_REQUIRES_BED) && !isBedNearby(home)) {
        logVerbose(
            "Home ",
            home,
            " skipped because MODE_HOME_REQUIRES_BED is true and no bed is nearby the home location");
        home = null;
      }
    } else
      log.warn(
          "Strategy "
              + getStrategyConfigName()
              + " was not given a homeName argument, nothing to do");

    return new StrategyResultImpl(home);
  }
  private String jsonToHtml(SkysailResponse<List<?>> skysailResponse, Resource resource) {

    PresentationStyle styleOldp = ConverterUtils.evalPresentationStyle(resource);
    PresentationStyle style = skysailResponse.getPresentationStyleHint();

    String page = rootTemplate;
    long executionTimeInNanos = skysailResponse.getExecutionTime();
    float performance = new Long(1000000000) / executionTimeInNanos;
    page = page.replace("${performance}", String.format("%s", performance));
    page = page.replace("${result}", calcResult(skysailResponse));
    page =
        page.replace(
            "${message}",
            skysailResponse.getMessage() == null
                ? "no message available"
                : skysailResponse.getMessage());
    page = page.replace("${linkedPages}", linkedPages(resource));
    page = page.replace("${commands}", commands(resource));
    page = page.replace("${presentations}", presentations(skysailResponse));
    page = page.replace("${filterExpression}", getFilter());
    page = page.replace("${history}", "");
    page =
        page.replace(
            "${mainNav}",
            getMainNav(((SkysailApplication) resource.getApplication()).getBundleContext()));

    String username = "******";
    // if (resource.getRequest().getChallengeResponse() != null) {
    // username = resource.getRequest().getChallengeResponse().getIdentifier();
    // }
    Subject subject = SecurityUtils.getSubject();
    if (subject.getPrincipal() != null) {
      username = subject.getPrincipal().toString();
    }
    page =
        page.replace(
            "${username}",
            "<li><a href='#'><i class=\"icon-user icon-white\"></i>&nbsp;"
                + username
                + "</a></li>\n");

    if (subject.getPrincipal() != null) {
      page = page.replace("${loginLogout}", "<a href='/logout?targetUri=/'>[Logout]</a>");
    } else {
      page = page.replace("${loginLogout}", "<a href='/login?media=htmlform'>[Login]</a>");
    }

    page =
        page.replace(
            "${productName}",
            ((SkysailApplication) resource.getApplication()).getConfigForKey("productName"));

    Object skysailResponseAsObject = skysailResponse.getData();
    if (skysailResponseAsObject != null) {
      if (style.equals(PresentationStyle.LIST)) {
        StrategyContext context = new StrategyContext(new ListForContentStrategy());
        page = context.createHtml(page, skysailResponseAsObject, skysailResponse);
      } else if (style.equals(PresentationStyle.LIST2)) {
        StrategyContext context =
            new StrategyContext(
                new ListForContentStrategy2(
                    ((SkysailApplication) resource.getApplication()).getBundleContext(), resource));
        page = context.createHtml(page, skysailResponseAsObject, skysailResponse);
      } else if (style.equals(PresentationStyle.TABLE)) {
        StrategyContext context = new StrategyContext(new TableForContentStrategy());
        page = context.createHtml(page, skysailResponseAsObject, skysailResponse);
      } else if (style.equals(PresentationStyle.EDIT)) {
        StrategyContext context = new StrategyContext(new FormForContentStrategy());
        page = context.createHtml(page, skysailResponseAsObject, skysailResponse);
      } else if (style.equals(PresentationStyle.D3_SIMPLE_GRAPH)) {
        page = createD3SimpleGraphForContent(skysailResponseAsObject, skysailResponse);
      } else if (style.equals(PresentationStyle.IFRAME)) {
        StrategyContext context = new StrategyContext(new IFrameForContentStrategy());
        page = context.createHtml(page, skysailResponseAsObject, skysailResponse);
      } else if (style.equals(PresentationStyle.ACE_EDITOR)) {
        StrategyContext context = new StrategyContext(new AceEditorForContentStrategy());
        page = context.createHtml(page, skysailResponseAsObject, skysailResponse);
      }
    } else {
      if (skysailResponse instanceof ConstraintViolationsResponse) {
        StrategyContext context = new StrategyContext(new FormForContentStrategy());
        page = context.createHtml(page, skysailResponseAsObject, skysailResponse);
      } else {
        page = page.replace("${content}", "");
      }
    }

    StringBuilder breadcrumb = getBreadcrumbHtml(resource);
    page = page.replace("${breadcrumb}", breadcrumb.toString());

    String stacktrace = "";
    if (skysailResponse instanceof FailureResponse) {
      Exception exception = ((FailureResponse<?>) skysailResponse).getException();
      if (exception != null) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        exception.printStackTrace(pw);
        stacktrace = "<pre>" + sw.toString() + "</pre>";
      }
    }
    page = page.replace("${stacktrace}", stacktrace);
    return page;
  }