void runTask() { status(su, StatusUpdate.LEVEL_GENERIC, GRI18n.getString(MODULE, "newAlbm", new Object[] { album.getName(), g.toString() })); try { List<NameValuePair> formparams = new ArrayList<NameValuePair>(); JSONObject jsonEntity = new JSONObject(); jsonEntity.put("type", "album"); if (album.getName() != null) jsonEntity.put("name", album.getName()); if (album.getTitle() != null) jsonEntity.put("title", album.getTitle()); if (album.getDescription() != null) jsonEntity.put("description", album.getDescription()); formparams.add(new BasicNameValuePair("entity", jsonEntity.toJSONString())); BufferedReader entityReader = sendRequest(album.getParentAlbum().getUrl(), "post", formparams); String url = ((JSONObject) JSONValue.parse(entityReader)).get("url").toString(); status(su, StatusUpdate.LEVEL_GENERIC, GRI18n.getString(MODULE, "crateAlbmOk")); album.setUrl(url); Log.log(Log.LEVEL_INFO, "Created album " + album.toString()); } catch (IOException ioe) { Log.logException(Log.LEVEL_ERROR, MODULE, ioe); Object[] params2 = {ioe.toString()}; error(su, GRI18n.getString(MODULE, "error", params2)); } }
private void list() throws IOException { List<NameValuePair> formparams = new ArrayList<NameValuePair>(); formparams.add(new BasicNameValuePair("type", "album")); formparams.add(new BasicNameValuePair("scope", "all")); BufferedReader entityReader = sendRequest(g.getUrlString() + api + "item/1", "get", formparams); JSONParser parser = new JSONParser(); ListContentHandler lch = new ListContentHandler(); HashMap<String,String> url2parentUrl = new HashMap<String,String>(); HashMap<String,Album> url2album = new HashMap<String,Album>(); ArrayList<Album> albums = new ArrayList<Album>(); try { Album rootAlbum = g.createRootAlbum(); rootAlbum.setUrl(g.getUrlString() + api + "item/1"); rootAlbum.setSuppressEvents(true); lch.setAlbum(rootAlbum); parser.parse(entityReader, lch, true); rootAlbum.setSuppressEvents(false); // map album names to albums url2album.put(rootAlbum.getUrl(), rootAlbum); url2parentUrl.put(rootAlbum.getUrl(), lch.getParentUrl()); while (!lch.isEnd()) { Album a = g.newAlbum(); a.setSuppressEvents(true); lch.setAlbum(a); parser.parse(entityReader, lch, true); a.setSuppressEvents(false); albums.add(a); // map album names to albums url2album.put(a.getUrl(), a); url2parentUrl.put(a.getUrl(), lch.getParentUrl()); } } catch (ParseException e) { Log.logException(Log.LEVEL_CRITICAL, MODULE, e); } Log.log(Log.LEVEL_TRACE, MODULE, "Created " + albums.size() + " albums"); // link albums to parents for (Object o : url2parentUrl.keySet()) { String name = (String) o; String parentName = url2parentUrl.get(name); Album child = url2album.get(name); Album parent = url2album.get(parentName); if (child != null && parent != null) { parent.add(child); } } Log.log(Log.LEVEL_TRACE, MODULE, "Linked " + url2parentUrl.size() + " albums to their parents"); // reorder Collections.sort(albums, new NaturalOrderComparator<Album>()); Collections.reverse(albums); ArrayList<Album> orderedAlbums = new ArrayList<Album>(); int depth = 0; while (!albums.isEmpty()) { Iterator<Album> it = albums.iterator(); while (it.hasNext()) { Album a = it.next(); try { if (a.getAlbumDepth() == depth) { it.remove(); a.sortSubAlbums(); Album parentAlbum = a.getParentAlbum(); if (parentAlbum == null) { orderedAlbums.add(0, a); } else { int i = orderedAlbums.indexOf(parentAlbum); orderedAlbums.add(i + 1, a); } } } catch (IllegalArgumentException e) { it.remove(); Log.log(Log.LEVEL_TRACE, MODULE, "Gallery server album list is corrupted: " + "album " + a.getName() + " has a bad containment hierarchy."); } } depth++; } Log.log(Log.LEVEL_TRACE, MODULE, "Ordered " + orderedAlbums.size() + " albums"); status(su, StatusUpdate.LEVEL_BACKGROUND, GRI18n.getString(MODULE, "ftchdAlbms")); }