Example #1
0
  /**
   * Converts the value into an entity. The type of the entity depends on the type that the daoCrud
   * returns.
   *
   * @param facesContext current faces context
   * @param component current component
   * @param value current value submitted from user.
   * @return An Entity
   */
  public Object getAsObject(
      final FacesContext facesContext, final UIComponent component, final String value) {
    logger.debug(
        String.format(
            "getAsObject() called value=%s, component=%s, value class=%s",
            value, component.getClientId(facesContext), value.getClass().getName()));
    UIInput input = (UIInput) component;
    if (value.equals("-1")
        && (input.isRequired() || component.getAttributes().get("required_bean") != null)) {
      logger.debug("Required field and the value was -1");
      throw new ConverterException(new FacesMessage("Required", "Required"));
    }

    try {
      Serializable entityId = CrudUtils.getIdObject(value, this.idType);
      logger.debug(String.format("entityId %s", entityId));
      if (dao == null) {
        ObjectRegistry objectRegistry = CrankContext.getObjectRegistry();
        Map<String, GenericDao> repos = (Map<String, GenericDao>) objectRegistry.getObject("repos");

        if (managedObject != null) {
          logger.debug("Looking up DAO by managedObject");
          dao = repos.get(managedObject.getName());
        } else {
          Object key = component.getAttributes().get("beanType");
          logger.debug("Looking up DAO by beanType");
          dao = repos.get((String) key);
        }
      }
      Object object = dao.read(entityId);
      logger.debug(String.format("Read object %s", object));
      if (object == null) {
        if ("-1".equals(value)) {
          logger.debug("No object found and the value was -1");
          throw new ConverterException(new FacesMessage("Required", "Required"));
        } else {
          throw new ConverterException(
              new FacesMessage(
                  "Can't find object with id " + value, "Can't find object with id " + value));
        }
      }
      LogUtils.debug(logger, "Returning converted object %s", object);
      return object;
    } catch (ConverterException ex) {
      throw ex;
    } catch (Exception ex) {
      logger.error("Unable to convert object", ex);
      String message = String.format("Unable to convert, fatal issue, %s ", ex.getMessage());
      throw new ConverterException(new FacesMessage(message, message));
    }
  }
 public void testTransientState() {
   transientState = !widgetTransient.isRequired();
 }
  // === continue code from InputRendererUtil ===
  // ClientSide validation
  // com.ibm.xsp.renderkit.html_basic.InputRendererUtil.generateClientSideValidation(FacesContext,
  // UIInput, ResponseWriter)
  private void generateClientSideValidation(
      FacesContext context, UIInput uiInput, ResponseWriter writer) throws IOException {
    // Check if the input field is required
    boolean required = uiInput.isRequired();
    Converter c = InputRendererUtil.findConverter(context, uiInput);
    Validator[] v = uiInput.getValidators();

    // Check if it might make sense to generate a function
    boolean validate = required;
    if (!validate) {
      validate = c instanceof ClientSideConverter;
    }
    if (!validate) {
      for (int i = 0; i < v.length; i++) {
        validate = v[i] instanceof ClientSideValidator;
        if (validate) {
          break;
        }
      }
    }

    if (validate) {
      // This flag is maintained if we actually need to generate the function
      // Some converter/validator may not actually generate any client code, depending on their
      // parameters
      validate = false;

      StringBuilder b = new StringBuilder(128);
      b.append("XSP.attachValidator("); // $NON-NLS-1$
      JavaScriptUtil.addString(b, uiInput.getClientId(context));

      // Add the required flag
      if (required) {
        b.append(",new XSP.RequiredValidator("); // $NON-NLS-1$
        JavaScriptUtil.addMessage(b, InputRendererUtil.getRequiredMessage(context, uiInput));
        b.append(")");
        validate = true;
      } else {
        b.append(",null"); // $NON-NLS-1$
      }

      // Add the converter
      if (c instanceof ClientSideConverter) {
        ClientSideConverter clientSideConverter = (ClientSideConverter) c;
        // TODO this is handling of converterRenderer
        // differs from the original implementation in InputRendererUtil.
        String s;
        Renderer renderer =
            FacesUtil.getRenderer(context, uiInput.getFamily(), uiInput.getRendererType());
        ClientSideConverter converterRenderer =
            (ClientSideConverter) FacesUtil.getRendererAs(renderer, ClientSideConverter.class);
        if (null != converterRenderer) {
          // if the control renderer implements ClientSideConverter
          // delegate to the renderer instead of to the converter.
          s = converterRenderer.generateClientSideConverter(context, uiInput);
        } else {
          s = clientSideConverter.generateClientSideConverter(context, uiInput);
        }
        if (StringUtil.isNotEmpty(s)) {
          b.append(",");
          b.append(s); // not JSUtil because contains client script.
          validate = true;
        } else {
          b.append(",null"); // $NON-NLS-1$
        }
      } else {
        b.append(",null"); // $NON-NLS-1$
      }

      // And add the validator
      for (int i = 0; i < v.length; i++) {
        if (v[i] instanceof ClientSideValidator) {
          String s = ((ClientSideValidator) v[i]).generateClientSideValidation(context, uiInput);
          if (StringUtil.isNotEmpty(s)) {
            b.append(",");
            b.append(s); // not JSUtil because contains client script.
            validate = true;
          }
        }
      }

      // Finally, check for multiple values
      String multiSep = null;
      if (uiInput instanceof UIInputEx) {
        multiSep = ((UIInputEx) uiInput).getMultipleSeparator();
      }
      if (c instanceof ListConverter) {
        multiSep = ((ListConverter) c).getDelimiter();
        if (StringUtil.isEmpty(multiSep)) {
          multiSep = ","; // $NON-NLS-1$
        }
      }
      if (StringUtil.isNotEmpty(multiSep)) {
        b.append(",");
        JSUtil.addString(b, multiSep);
      }

      b.append(");");

      // get the scriptcollector component (needed to add script blocks the the rendered output).
      if (validate) {
        JavaScriptUtil.addScriptOnLoad(b.toString());
      }
    }
  }
 public void testFullState() {
   fullState = widgetFull.isRequired();
 }