Example #1
0
  /**
   * Creates an object of specified type from the request values.
   *
   * @param objClassName name of the class of the main object to be created.
   * @param reqParamMap values of the object in hash map
   * @return
   * @throws Exception
   */
  public Object createObject(String objClassName, HashMap reqParamMap) throws Exception {
    Object convertedObject = null;

    CommonLogger.logDebug(log, "In ObjectHandler.createObject()");

    try {
      Class classToLoad = Class.forName(objClassName);
      convertedObject = classToLoad.newInstance();

      // The name of the html field tha contains the set values should end
      // with "Set"
      // Map newMap = (HashMap) reqParamMap.clone();
      Map newMap = processParamNames(reqParamMap);
      Set mapKeysSet = newMap.keySet();
      Iterator keys = mapKeysSet.iterator();
      // To contain the parameter name values for the Set of specified
      // object type
      Map childPropertiesMap = new HashMap();
      while (keys.hasNext()) {
        // String keyName = (String) keys.next();
        RequestParamValue reqParam = (RequestParamValue) keys.next();
        // if (keyName.endsWith("Set")) {
        if (reqParam.getTypeOfParentClassVarible().equals("Set")) {
          // String setPropertyName =
          // keyName.substring(keyName.lastIndexOf(',') + 1);
          String setPropertyName = reqParam.getNameOfParentClassVariable() + "Set";
          // Check if the values needs to be set in different object
          // than the parent.
          if (setPropertyName != null && !childPropertiesMap.containsKey(setPropertyName)) {
            Map valueMap = new HashMap();
            valueMap.put(reqParam, newMap.get(reqParam));
            childPropertiesMap.put(setPropertyName, valueMap);
          } else {
            Map valueMap = (Map) childPropertiesMap.get(setPropertyName);
            valueMap.put(reqParam, newMap.get(reqParam));
            childPropertiesMap.put(setPropertyName, valueMap);
          }
        }
      }

      // Now further process the childPropertiesMap to generate the Set
      // object with specified object types.
      Iterator childIterator = childPropertiesMap.keySet().iterator();
      while (childIterator.hasNext()) {
        String keyName = (String) childIterator.next();
        // RequestParamValue reqParam = (RequestParamValue)
        // childIterator.next();

        if (keyName.endsWith("Set")) {
          // if (reqParam.getTypeOfParentClassVarible().equals("Set"))
          // {
          // String parentPropertyName = getPropertyName(keyName);

          Map valueMap = (Map) childPropertiesMap.get(keyName);
          ArrayToSetConverter converter = new ArrayToSetConverter();
          Set convertedSets = converter.convert(Set.class, valueMap);
          String parentClassPropertyName = null;
          Iterator iter = valueMap.keySet().iterator();
          while (iter.hasNext()) {
            parentClassPropertyName =
                ((RequestParamValue) iter.next()).getNameOfParentClassVariable();
            break;
          }
          // reqParamMap.put(reqParam.getNameOfParentClassVariable(),
          // convertedSets);
          reqParamMap.put(parentClassPropertyName, convertedSets);
        }
      }

      BeanUtils.populate(convertedObject, reqParamMap);
    } catch (BSIException ex1) {
      CommonLogger.logDebug(
          log, "In CreateObject \n exception occured. Exception message is " + ex1.getMessage());
    } catch (Exception ex2) {
      ex2.printStackTrace();
      throw ex2;
    }
    return convertedObject;
  }