public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
// API BUSINESS LOGIC
// log.debug("Service Plan setup/delete");
ServicePlan servicePlan = null;
Long servicePlanId = null;
String servicePlanIdAsString = req.getParameter(API_SETPLAN_PARAMETER_PLAN_ID);
List<Service> allServices = store.getServices();
// *********************
// BEST EFFORT HERE.
// We try to find the service by ID.
// If not found, we try by NAME.
// Otherwise, let the rest of the logic do its thing.
// *********************
try {
servicePlanId = new Long(servicePlanIdAsString);
servicePlan = store.getServicePlanById(servicePlanId);
} catch (Exception e) {
if (req.getParameter(API_SETPLAN_PARAMETER_PLAN_ID) != null) {
log.debug("No service plan with ID '" + req.getParameter(API_SETPLAN_PARAMETER_PLAN_ID)
+ "' found.", e);
}
}
if (servicePlan == null) {
try {
String servicePlanName = req.getParameter(API_SET_SAVE_OR_UPDATE_PARAMETER_PLAN_NAME);
servicePlan = store.getServicePlanByName(servicePlanName.trim());
} catch (Exception e) {
if (req.getParameter(API_SET_SAVE_OR_UPDATE_PARAMETER_PLAN_NAME) != null) {
log.debug(
"No service plan with NAME '"
+ req.getParameter(API_SET_SAVE_OR_UPDATE_PARAMETER_PLAN_NAME) + "' found.", e);
}
}
}
JSONObject jsonResultObject = new JSONObject();
String action = req.getParameter(API_SETPLAN_PARAMETER_ACTION);
String transientState = req.getParameter(API_TRANSIENT_STATE);
try {
if (transientState != null) {
servicePlan.setTransientState(new Boolean(transientState));
}
} catch (Exception e) {
log.debug("ServicePlan not set to transient state but a value was given as: " + transientState);
}
if (API_SETPLAN_PARAMETER_ACTION_VALUE_DELETE_PLAN.equals(action)) {
JSONObject jsonObject = new JSONObject();
try {
store.deleteServicePlan(servicePlan);
jsonObject.put("success", "Service plan '" + servicePlan.getName() + "' deleted");
jsonObject.put("planId", "" + servicePlan.getId());
jsonObject.put("planName", "" + servicePlan.getName());
} catch (Exception e) {
jsonObject.put("fail", "Service plan not deleted. Please check your logs for insight.");
}
resp.setContentType("application/json");
PrintWriter out = resp.getWriter();
jsonResultObject.put("result", jsonObject);
out.println(jsonResultObject.toString());
out.flush();
out.close();
return;
} else if (API_SETPLAN_PARAMETER_ACTION_VALUE_SET_PLAN.equals(action) && servicePlan != null) {
JSONObject jsonObject = new JSONObject();
try {
store.setServicePlan(servicePlan);
String msg = "Service plan " + servicePlan.getName() + " set";
jsonObject.put("success", msg);
jsonObject.put("planid", "" + servicePlan.getId());
jsonObject.put("planName", "" + servicePlan.getName());
Util.saveSuccessMessage(msg, req); // For redirect
} catch (Exception e) {
jsonObject.put("fail", "Service plan not set. Please check your logs for insight.");
}
resp.setContentType("application/json");
PrintWriter out = resp.getWriter();
jsonResultObject.put("result", jsonObject);
out.println(jsonResultObject.toString());
out.flush();
out.close();
return;
}
else if(API_SETPLAN_PARAMETER_ACTION_VALUE_SET_AS_DEFAULT_PLAN.equals(action)){
PrintWriter out = resp.getWriter();
JSONObject jsonObject = new JSONObject();
if("none".equalsIgnoreCase(servicePlanIdAsString)){
store.setDefaultServicePlanId(null);
// JSON response
jsonObject.put("success", "Removed default plan.");
}
else if(servicePlan!=null && servicePlan.getId()!=null){
store.setDefaultServicePlanId(servicePlan.getId().toString());
// JSON response
jsonObject.put("success", "Set as Default Service Plan");
jsonObject.put("planid", "" + servicePlan.getId());
jsonObject.put("planName", "" + servicePlan.getName());
}else {
jsonObject.put("fail", "Unable to set Default Service Plan. Unknown Service Plan ID.");
}
jsonResultObject.put("result", jsonObject);
out.println(jsonResultObject.toString());
out.flush();
out.close();
return;
}
else if (API_SETPLAN_PARAMETER_ACTION_VALUE_SAVE_PLAN.equals(action)) {
if (servicePlan == null) {
servicePlan = new ServicePlan();
}
String[] serviceIds = req.getParameterValues("service_ids[]");
// ***************************
// LET'S PREVENT EMPTY PLAN NAMES
// ***************************
String servicePlanName = req.getParameter(API_SET_SAVE_OR_UPDATE_PARAMETER_PLAN_NAME);
String servicePlanTag = req.getParameter(API_SET_SAVE_OR_UPDATE_PARAMETER_PLAN_TAG);
if (servicePlanName == null) {
// If possible, carry over the name from an existing Plan.
servicePlanName = servicePlan.getName();
}
// If all fails, inject a name.
if (servicePlanName == null || servicePlanName.trim().length() == 0) {
servicePlanName = "Plan (auto-generated-name)";
}
servicePlan.setName(servicePlanName.trim());
if (servicePlanTag != null) {
servicePlan.setTag(servicePlanTag);
}
// ***************************
// SAVE/UPDATE THE PLAN
// ***************************
ServicePlan savedServicePlan = createOrUpdatePlan(servicePlan, serviceIds);
// ***************************
// SAVE/UPDATE THE PLAN
// ***************************
resp.setContentType("application/json");
PrintWriter out = resp.getWriter();
String msg = "Service plan " + servicePlan.getName() + " saved";
// HACK: For redirect IF JavaScript decides to (if type is not
// JSON)
if (!"json".equalsIgnoreCase(req.getParameter(API_SETPLAN_PARAMETER_TYPE))) {
Util.saveSuccessMessage(msg, req);
}
// JSON response
JSONObject jsonObject = new JSONObject();
jsonObject.put("success", msg);
jsonObject.put("planid", "" + savedServicePlan.getId());
jsonObject.put("planName", "" + savedServicePlan.getName());
jsonResultObject.put("result", jsonObject);
out.println(jsonResultObject.toString());
out.flush();
out.close();
return;