2 * Sone - EditAlbumAjaxPage.java - Copyright © 2011–2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.web.ajax;
20 import java.util.Collections;
22 import net.pterodactylus.sone.data.Album;
23 import net.pterodactylus.sone.data.Image;
24 import net.pterodactylus.sone.template.ParserFilter;
25 import net.pterodactylus.sone.template.RenderFilter;
26 import net.pterodactylus.sone.text.Part;
27 import net.pterodactylus.sone.text.TextFilter;
28 import net.pterodactylus.sone.web.WebInterface;
29 import net.pterodactylus.sone.web.page.FreenetRequest;
30 import net.pterodactylus.util.template.TemplateContext;
32 import com.google.common.base.Optional;
33 import com.google.common.collect.ImmutableMap;
36 * Page that stores a user’s album modifications.
38 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40 public class EditAlbumAjaxPage extends JsonPage {
42 private final ParserFilter parserFilter;
43 private final RenderFilter renderFilter;
45 public EditAlbumAjaxPage(WebInterface webInterface, ParserFilter parserFilter, RenderFilter renderFilter) {
46 super("editAlbum.ajax", webInterface);
47 this.parserFilter = parserFilter;
48 this.renderFilter = renderFilter;
59 protected JsonReturnObject createJsonObject(FreenetRequest request) {
60 String albumId = request.getHttpRequest().getParam("album");
61 Optional<Album> album = webInterface.getCore().getAlbum(albumId);
62 if (!album.isPresent()) {
63 return createErrorJsonObject("invalid-album-id");
65 if (!album.get().getSone().isLocal()) {
66 return createErrorJsonObject("not-authorized");
68 if ("true".equals(request.getHttpRequest().getParam("moveLeft"))) {
69 Album swappedAlbum = album.get().getParent().moveAlbumUp(album.get());
70 webInterface.getCore().touchConfiguration();
71 return createSuccessJsonObject().put("sourceAlbumId", album.get().getId()).put("destinationAlbumId", swappedAlbum.getId());
73 if ("true".equals(request.getHttpRequest().getParam("moveRight"))) {
74 Album swappedAlbum = album.get().getParent().moveAlbumDown(album.get());
75 webInterface.getCore().touchConfiguration();
76 return createSuccessJsonObject().put("sourceAlbumId", album.get().getId()).put("destinationAlbumId", swappedAlbum.getId());
78 String title = request.getHttpRequest().getParam("title").trim();
79 String description = request.getHttpRequest().getParam("description").trim();
81 album.get().modify().setTitle(title).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).update();
82 webInterface.getCore().touchConfiguration();
83 return createSuccessJsonObject().put("albumId", album.get().getId()).put("title", album.get().getTitle()).put("description", parseDescription(album.get()));
84 } catch (IllegalStateException e) {
85 return createErrorJsonObject("invalid-album-title");
89 private String parseDescription(Album album) {
90 Iterable<Part> parts = (Iterable<Part>) parserFilter.format(new TemplateContext(), album.getDescription(),
91 ImmutableMap.<String, Object>builder().put("sone", album.getSone()).build());
92 return (String) renderFilter.format(new TemplateContext(), parts, Collections.<String, Object>emptyMap());