/*
- * Sone - DeleteProfileFieldAjaxPage.java - Copyright © 2011 David Roden
+ * Sone - DeleteProfileFieldAjaxPage.java - Copyright © 2011–2013 David Roden
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
package net.pterodactylus.sone.web.ajax;
+import static com.fasterxml.jackson.databind.node.JsonNodeFactory.instance;
+
import net.pterodactylus.sone.data.Profile;
import net.pterodactylus.sone.data.Profile.Field;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.web.WebInterface;
-import net.pterodactylus.util.json.JsonObject;
+import net.pterodactylus.sone.web.page.FreenetRequest;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.fasterxml.jackson.databind.node.TextNode;
+import com.google.common.base.Optional;
/**
* AJAX page that lets the user delete a profile field.
super("deleteProfileField.ajax", webInterface);
}
- /**
- * {@inheritDoc}
- */
@Override
- protected JsonObject createJsonObject(Request request) {
+ protected JsonReturnObject createJsonObject(FreenetRequest request) {
String fieldId = request.getHttpRequest().getParam("field");
Sone currentSone = getCurrentSone(request.getToadletContext());
Profile profile = currentSone.getProfile();
- Field field = profile.getFieldById(fieldId);
- if (field == null) {
+ Optional<Field> field = profile.getFieldById(fieldId);
+ if (!field.isPresent()) {
return createErrorJsonObject("invalid-field-id");
}
- profile.removeField(field);
+ profile.removeField(field.get());
currentSone.setProfile(profile);
- webInterface.getCore().saveSone(currentSone);
- return createSuccessJsonObject().put("field", new JsonObject().put("id", field.getId()));
+ webInterface.getCore().touchConfiguration();
+ return createSuccessJsonObject().put("field", new ObjectNode(instance).put("id", new TextNode(field.get().getId())));
}
}