// ***********************
// INITIALIZAION
// ***********************
Long twistInfoId = null;
TwistInfo twistInfo = null;
try {
twistInfoId = new Long(req.getParameter(PARAMETER_KEY_TWIST_ID));
twistInfo = store.getTwistInfoById(twistInfoId);
} catch (Exception e) {
// Do nothing. If the value doesn't exist,
// then we'll create a new TwistInfo
}
if (twistInfo == null) {
twistInfo = new TwistInfo();
}
// ***********************
// DATA HANDLING
// ***********************
String name = req.getParameter(PARAMETER_KEY_TWIST_NAME);
if (name == null || name.trim().length() == 0) {
name = "TwistInfo (auto-generated)";
}
String[] originationArguments = req.getParameterValues(PARAMETER_KEY_TWIST_ORIGINATION_LIST);
String[] destinationArguments = req.getParameterValues(PARAMETER_KEY_TWIST_DESTINATION_LIST);
// Remove any existing TwistInfo patterns.
twistInfo.setPatternPairList(new ArrayList<PatternPair>());
if ((originationArguments != null && destinationArguments != null)
&& (originationArguments.length == destinationArguments.length)) {
for (int i = 0; i < originationArguments.length; i++) {
twistInfo.addPatternPair(new PatternPair(originationArguments[i], destinationArguments[i]));
}
}
twistInfo.setName(name);
twistInfo = store.saveOrUpdateTwistInfo(twistInfo);
// ***********************
// RESPONSE - in JSON or JSP
// ***********************
String responseType = req.getParameter(PARAMETER_KEY_RESPONSE_TYPE);
if (PARAMETER_KEY_RESPONSE_TYPE_VALUE_JSON.equalsIgnoreCase(responseType)) {
// BUILD JSON response
resp.setContentType("application/json");
PrintWriter out = resp.getWriter();
try {
JSONObject jsonResponseObject = new JSONObject();
JSONObject jsonObject = new JSONObject();
jsonObject.put("success", "Twist updated");
jsonObject.put("id", ""+twistInfo.getId());
jsonObject.put("name", twistInfo.getName());
jsonResponseObject.put("result", jsonObject);
out.println(jsonResponseObject.toString());
} catch (JSONException jsonException) {