/** * 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; }
public Map processParamNames(Map reqParamValuesMap) { Map modifiedMap = new HashMap(); Iterator iter = reqParamValuesMap.keySet().iterator(); RequestParamValue reqParamObj = null; while (iter.hasNext()) { reqParamObj = new RequestParamValue(); String keyName = (String) iter.next(); if (keyName.endsWith("Set") && keyName.contains(",")) { // nameOfParentClassVariable if (keyName.endsWith("Set")) { reqParamObj.setTypeOfParentClassVarible("Set"); } else { reqParamObj.setTypeOfParentClassVarible("NULL"); } StringTokenizer strTokens = new StringTokenizer(keyName, ":"); while (strTokens.hasMoreTokens()) { String token = strTokens.nextToken(); if (token.contains(",")) { StringTokenizer nameTokens = new StringTokenizer(token, ","); if (nameTokens.countTokens() == 2) { reqParamObj.setNameofChildClassVarible(nameTokens.nextToken()); // name of the parent property reqParamObj.setNameOfParentClassVariable(getPropertyName(nameTokens.nextToken())); } } else { reqParamObj.setClassOfParentClassVariable(token); } } } else { if (keyName.endsWith("Set")) { reqParamObj.setNameOfParentClassVariable(getPropertyName(keyName)); reqParamObj.setTypeOfParentClassVarible("Set"); reqParamObj.setClassOfParentClassVariable("java.lang.String"); } else { // It is of type String ex: name reqParamObj.setNameOfParentClassVariable(keyName); reqParamObj.setTypeOfParentClassVarible("NULL"); } } modifiedMap.put(reqParamObj, (String[]) reqParamValuesMap.get(keyName)); } return modifiedMap; }