Skip to content

Commit

Permalink
Added creating albums.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Luc Paour committed Jun 23, 2010
1 parent 09abfca commit a2db731
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 20 deletions.
6 changes: 1 addition & 5 deletions com/gallery/GalleryRemote/GalleryComm.java
Expand Up @@ -149,12 +149,8 @@ public void albumProperties(StatusUpdate su, Album a, boolean async) {
* the specified album (or at the root if album is null)
*
* @param su an instance that implements the StatusUpdate interface.
* @param parentAlbum if null, create the album in the root of the gallery; otherwise
* create as a child of the given album
*/
public String newAlbum(StatusUpdate su, Album parentAlbum,
String newAlbumName, String newAlbumTitle,
String newAlbumDesc, boolean async) {
public void newAlbum(StatusUpdate su, Album album, boolean async) {
throw new RuntimeException("This method is not available on this protocol");
}

Expand Down
47 changes: 45 additions & 2 deletions com/gallery/GalleryRemote/GalleryComm3.java
Expand Up @@ -54,6 +54,11 @@ public void uploadFiles(StatusUpdate su, boolean async) {
doTask(uploadTask, async);
}

public void newAlbum(StatusUpdate su, Album album, boolean async) {
NewAlbumTask newAlbumTask = new NewAlbumTask(su, album);
doTask(newAlbumTask, async);
}

public boolean checkAuth() {
try {
sendRequest(g.getUrlString() + api + "item/1", "get", (HttpEntity) null);
Expand Down Expand Up @@ -508,9 +513,9 @@ boolean uploadPicture(Picture p) {
ContentBody body = new FileBody(p.getUploadSource());
entity.addPart("file", body);

BufferedReader r = sendRequest(p.getParentAlbum().getUrl(), "post", entity);
BufferedReader entityReader = sendRequest(p.getParentAlbum().getUrl(), "post", entity);

String url = ((JSONObject) JSONValue.parse(r)).get("url").toString();
String url = ((JSONObject) JSONValue.parse(entityReader)).get("url").toString();
status(su, StatusUpdate.LEVEL_UPLOAD_ONE, GRI18n.getString(MODULE, "upSucc"));
p.setUrl(url);

Expand Down Expand Up @@ -566,6 +571,44 @@ boolean uploadPicture(Picture p) {
}
}

class NewAlbumTask extends GalleryTask {
Album album;

NewAlbumTask(StatusUpdate su, Album album) {
super(su);
this.album = album;
}

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));
}
}
}

/**
* This class serves as the base class for each GalleryComm task.
*/
Expand Down
15 changes: 15 additions & 0 deletions com/gallery/GalleryRemote/GalleryCommTest.java
Expand Up @@ -84,6 +84,21 @@ public void testUploadFilesToRoot() {
assertNotNull(p.getUrl());
}

@Test
public void testCreateAlbumToRoot() {
Album a = new Album(g);
a.setUrl(g.getUrlString() + "index.php/rest/item/1");
Album na = new Album(g);
a.add(na);
g.setRoot(a);
na.setName("New Album");
na.setTitle("New Album Title");
na.setDescription("Created by testCreateNewAlbum");
assertNull(na.getUrl());
comm.newAlbum(su, na, false);
assertNotNull(na.getUrl());
}

@BeforeClass
public static void setupAll() {
GalleryRemote.createInstance("com.gallery.GalleryRemote.GalleryRemoteMainFrame", null);
Expand Down
8 changes: 4 additions & 4 deletions com/gallery/GalleryRemote/MainFrame.java
Expand Up @@ -819,10 +819,10 @@ public void newAlbum() {
return;
}

String newAlbumName = getCurrentGallery().doNewAlbum(newAlbum, GalleryRemote._().getCore().getMainStatusUpdate());
if (!newAlbumName.equals(newAlbum.getName())) {
newAlbum.setName(newAlbumName);
}
getCurrentGallery().doNewAlbum(newAlbum, GalleryRemote._().getCore().getMainStatusUpdate());
// if (!newAlbumName.equals(newAlbum.getName())) {
// newAlbum.setName(newAlbumName);
// }

newAlbum.fetchAlbumProperties(GalleryRemote._().getCore().getMainStatusUpdate());

Expand Down
17 changes: 8 additions & 9 deletions com/gallery/GalleryRemote/model/Gallery.java
Expand Up @@ -133,25 +133,24 @@ public void doFetchAlbums(StatusUpdate su, boolean async) {
}
}

public String doNewAlbum(Album a, StatusUpdate su) {
public void doNewAlbum(Album a, StatusUpdate su) {
Log.log(Log.LEVEL_INFO, MODULE, "Creating new album " + a.toString());

// create album synchronously
String newAlbumName = getComm(su).newAlbum(su, a.getParentAlbum(), a.getName(),
a.getTitle(), a.getDescription(), false);
getComm(su).newAlbum(su, a, false);

// refresh album list asynchronously
//fetchAlbums(su);

if (!newAlbumName.equals(a.getName())) {
//Log.log(Log.LEVEL_INFO, MODULE, "Album name probably conflicted on the server, need to reload album list");
//getComm(su).fetchAlbums(su, false);
a.setName(newAlbumName);
}
// if (!newAlbumName.equals(a.getName())) {
// //Log.log(Log.LEVEL_INFO, MODULE, "Album name probably conflicted on the server, need to reload album list");
// //getComm(su).fetchAlbums(su, false);
// a.setName(newAlbumName);
// }

//addAlbum(a);

return newAlbumName;
//return newAlbumName;
}

public void incrementViewCount(Picture p, StatusUpdate su) {
Expand Down

0 comments on commit a2db731

Please sign in to comment.