Skip to content

Commit

Permalink
Implemented a new way to communicate with the server for getting the …
Browse files Browse the repository at this point in the history
…list of albums
  • Loading branch information
Pierre-Luc Paour committed Jun 25, 2003
1 parent c937893 commit 2c0df14
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 78 deletions.
269 changes: 192 additions & 77 deletions com/gallery/GalleryRemote/GalleryComm2.java
Expand Up @@ -26,11 +26,7 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import java.util.*;

import HTTPClient.Codecs;
import HTTPClient.HTTPConnection;
Expand Down Expand Up @@ -98,7 +94,8 @@ public class GalleryComm2 extends GalleryComm implements GalleryComm2Consts,
* The capabilities for 2.1
*/
private static int[] capabilities1;

private static int[] capabilities2;


/* -------------------------------------------------------------------------
* CONSTRUCTION
Expand All @@ -122,6 +119,8 @@ protected GalleryComm2( Gallery g ) {
CAPA_FETCH_HIERARCHICAL, CAPA_ALBUM_INFO };
capabilities1 = new int[] { CAPA_UPLOAD_FILES, CAPA_FETCH_ALBUMS, CAPA_UPLOAD_CAPTION,
CAPA_FETCH_HIERARCHICAL, CAPA_ALBUM_INFO, CAPA_NEW_ALBUM };
capabilities2 = new int[] { CAPA_UPLOAD_FILES, CAPA_FETCH_ALBUMS, CAPA_UPLOAD_CAPTION,
CAPA_FETCH_HIERARCHICAL, CAPA_ALBUM_INFO, CAPA_NEW_ALBUM, CAPA_FETCH_ALBUMS_PRUNE };
Arrays.sort(capabilities);
}

Expand Down Expand Up @@ -372,9 +371,12 @@ private boolean login() {

Log.log(Log.TRACE, MODULE, "Server minor version: " + serverMinorVersion);

if (serverMinorVersion >= 1) {
if (serverMinorVersion == 1) {
// we have more than the 2.0 capabilities, we have 2.1 capabilities.
capabilities = capabilities1;
} else if (serverMinorVersion >= 2) {
// we have more than the 2.1 capabilities, we have 2.2 capabilities.
capabilities = capabilities2;
}
} catch (Exception e) {
Log.log( Log.ERROR, MODULE, "Malformed server_version: " + p.getProperty( "server_version" ) );
Expand Down Expand Up @@ -434,7 +436,9 @@ void runTask() {

su.updateProgressValue(pId, uploadedCount++);

p.getAlbum().removePicture(p);
if (allGood) {
p.getAlbum().removePicture(p);
}
}

if (allGood) {
Expand Down Expand Up @@ -513,77 +517,15 @@ void runTask() {
pId = su.startProgress(0, 10, "Fetching albums from " + g.toString(), true);

try {
// setup the protocol parameters
NVPair form_data[] = {
new NVPair("cmd", "fetch-albums"),
new NVPair("protocol_version", PROTOCOL_VERSION )
};
Log.log(Log.TRACE, MODULE, "fetchAlbums parameters: " + Arrays.asList(form_data));

// load and validate the response
Properties p = requestResponse( form_data );
if ( p.getProperty( "status" ).equals(GR_STAT_SUCCESS) ) {
ArrayList mAlbumList = new ArrayList();

// parse and store the data
int albumCount = Integer.parseInt( p.getProperty( "album_count" ) );
// System.err.println( "### albumCount = " + albumCount );
HashMap ref2parKey = new HashMap();
HashMap ref2album = new HashMap();
for ( int i = 1; i < albumCount + 1; i++ ) {
Album a = new Album();

String nameKey = "album.name." + i;
String titleKey = "album.title." + i;
String parentKey = "album.parent." + i;
String permsAddKey = "album.perms.add." + i;
String permsWriteKey = "album.perms.write." + i;
String permsDelItemKey = "album.perms.del_item." + i;
String permsDelAlbKey = "album.perms.del_alb." + i;
String permsCreateSubKey = "album.perms.create_sub." + i;

a.setCanAdd( isTrue( p.getProperty( permsAddKey ) ) );
a.setCanWrite( isTrue( p.getProperty( permsWriteKey ) ) );
a.setCanDeleteFrom( isTrue( p.getProperty( permsDelItemKey ) ) );
a.setCanDeleteThisAlbum( isTrue( p.getProperty( permsDelAlbKey ) ) );
a.setCanCreateSubAlbum( isTrue( p.getProperty( permsCreateSubKey ) ) );

a.setName( p.getProperty( nameKey ) );
a.setTitle( p.getProperty( titleKey ) );

a.setGallery( g );
mAlbumList.add( a );

// map album ref nums to albums
ref2album.put( "" + i, a );

// map album refs to parent refs
String parentRefS = p.getProperty( parentKey );
int parentRef = Integer.parseInt( parentRefS );
if ( parentRef != 0 ) {
ref2parKey.put( "" + i, parentRefS );
}
}

// link albums to parents
for ( int i = 1; i < albumCount + 1; i++ ) {
String parentKey = (String)ref2parKey.get( "" + i );
if ( parentKey != null ) {
Album a = (Album)ref2album.get( "" + i );
if ( a == null ) {
}
Album pa = (Album)ref2album.get( parentKey );
a.setParentAlbum( pa );
}
}

status(su, "Fetched albums");

g.setAlbumList(mAlbumList);
long startTime = System.currentTimeMillis();

if (serverMinorVersion < 2) {
list20();
} else {
error(su, "Error: " + p.getProperty( "status_text" ));
list22();
}


Log.log(Log.INFO, MODULE, "execution time for AlbumList: " + (System.currentTimeMillis() - startTime));
} catch ( GR2Exception gr2e ) {
Log.logException(Log.ERROR, MODULE, gr2e );
error(su, "Error: " + gr2e.getMessage());
Expand All @@ -597,6 +539,179 @@ void runTask() {

su.stopProgress(pId, "Fetch complete");
}

private void list20() throws IOException, ModuleException {
// setup the protocol parameters
NVPair form_data[] = {
new NVPair("cmd", "fetch-albums"),
new NVPair("protocol_version", PROTOCOL_VERSION )
};
Log.log(Log.TRACE, MODULE, "fetchAlbums parameters: " + Arrays.asList(form_data));

// load and validate the response
Properties p = requestResponse( form_data );
if ( p.getProperty( "status" ).equals(GR_STAT_SUCCESS) ) {
ArrayList mAlbumList = new ArrayList();

// parse and store the data
int albumCount = Integer.parseInt( p.getProperty( "album_count" ) );
// System.err.println( "### albumCount = " + albumCount );
HashMap ref2parKey = new HashMap();
HashMap ref2album = new HashMap();
for ( int i = 1; i < albumCount + 1; i++ ) {
Album a = new Album();

String nameKey = "album.name." + i;
String titleKey = "album.title." + i;
String parentKey = "album.parent." + i;
String permsAddKey = "album.perms.add." + i;
String permsWriteKey = "album.perms.write." + i;
String permsDelItemKey = "album.perms.del_item." + i;
String permsDelAlbKey = "album.perms.del_alb." + i;
String permsCreateSubKey = "album.perms.create_sub." + i;

a.setCanAdd( isTrue( p.getProperty( permsAddKey ) ) );
a.setCanWrite( isTrue( p.getProperty( permsWriteKey ) ) );
a.setCanDeleteFrom( isTrue( p.getProperty( permsDelItemKey ) ) );
a.setCanDeleteThisAlbum( isTrue( p.getProperty( permsDelAlbKey ) ) );
a.setCanCreateSubAlbum( isTrue( p.getProperty( permsCreateSubKey ) ) );

a.setName( p.getProperty( nameKey ) );
a.setTitle( p.getProperty( titleKey ) );

a.setGallery( g );
mAlbumList.add( a );

// map album ref nums to albums
ref2album.put( "" + i, a );

// map album refs to parent refs
String parentRefS = p.getProperty( parentKey );
int parentRef = Integer.parseInt( parentRefS );
if ( parentRef != 0 ) {
ref2parKey.put( "" + i, parentRefS );
}
}

// link albums to parents
for ( int i = 1; i < albumCount + 1; i++ ) {
String parentKey = (String)ref2parKey.get( "" + i );
if ( parentKey != null ) {
Album a = (Album)ref2album.get( "" + i );
if ( a == null ) {
}
Album pa = (Album)ref2album.get( parentKey );
a.setParentAlbum( pa );
}
}

status(su, "Fetched albums");

g.setAlbumList(mAlbumList);
} else {
error(su, "Error: " + p.getProperty( "status_text" ));
}
}

private void list22() throws IOException, ModuleException {
// setup the protocol parameters
NVPair form_data[] = {
new NVPair("cmd", "fetch-albums-prune"),
new NVPair("protocol_version", PROTOCOL_VERSION )
};
Log.log(Log.TRACE, MODULE, "fetchAlbums parameters: " + Arrays.asList(form_data));

// load and validate the response
Properties p = requestResponse( form_data );
if ( p.getProperty( "status" ).equals(GR_STAT_SUCCESS) ) {
ArrayList mAlbumList = new ArrayList();

// parse and store the data
int albumCount = Integer.parseInt( p.getProperty( "album_count" ) );
// System.err.println( "### albumCount = " + albumCount );
HashMap name2parentName = new HashMap();
HashMap name2album = new HashMap();
for ( int i = 1; i < albumCount + 1; i++ ) {
Album a = new Album();

String nameKey = "album.name." + i;
String titleKey = "album.title." + i;
String parentKey = "album.parent." + i;
String permsAddKey = "album.perms.add." + i;
String permsWriteKey = "album.perms.write." + i;
String permsDelItemKey = "album.perms.del_item." + i;
String permsDelAlbKey = "album.perms.del_alb." + i;
String permsCreateSubKey = "album.perms.create_sub." + i;

a.setCanAdd( isTrue( p.getProperty( permsAddKey ) ) );
a.setCanWrite( isTrue( p.getProperty( permsWriteKey ) ) );
a.setCanDeleteFrom( isTrue( p.getProperty( permsDelItemKey ) ) );
a.setCanDeleteThisAlbum( isTrue( p.getProperty( permsDelAlbKey ) ) );
a.setCanCreateSubAlbum( isTrue( p.getProperty( permsCreateSubKey ) ) );

String name = p.getProperty( nameKey );
String title = p.getProperty( titleKey );
a.setName( name );
a.setTitle( title );

a.setGallery( g );
mAlbumList.add( a );

// map album names to parent albums
name2album.put( name, a );

// map album refs to parent refs
String parentName = p.getProperty( parentKey );
if ( parentName != null && parentName.length() > 0 && !parentName.equals("0")) {
name2parentName.put( name, parentName );
}
}

// link albums to parents
Iterator it = name2parentName.keySet().iterator();
while (it.hasNext()) {
String name = (String) it.next();
String parentName = (String) name2parentName.get(name);
Album child = (Album) name2album.get(name);
Album parent = (Album) name2album.get(parentName);

if (child != null && parent != null) {
child.setParentAlbum(parent);
}
}

// reorder
//Collections.reverse(mAlbumList);
ArrayList orderedAlbums = new ArrayList();
int depth = 0;
while (!mAlbumList.isEmpty()) {
it = mAlbumList.iterator();
while (it.hasNext()) {
Album a = (Album) it.next();

if (a.getAlbumDepth() == depth) {
it.remove();

Album parentAlbum = a.getParentAlbum();
if (parentAlbum == null) {
orderedAlbums.add(a);
} else {
int i = orderedAlbums.indexOf(parentAlbum);
orderedAlbums.add(i + 1, a);
}
}
}

depth++;
}

status(su, "Fetched albums");

g.setAlbumList(orderedAlbums);
} else {
error(su, "Error: " + p.getProperty( "status_text" ));
}
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion com/gallery/GalleryRemote/GalleryComm2Consts.java
Expand Up @@ -38,7 +38,7 @@ public interface GalleryComm2Consts {
/**
* Protocol version string.
*/
public static final String PROTOCOL_VERSION = "2.1";
public static final String PROTOCOL_VERSION = "2.2";

/**
* Remote scriptname that provides version 2 of the protocol on the server.
Expand Down
1 change: 1 addition & 0 deletions com/gallery/GalleryRemote/GalleryCommCapabilities.java
Expand Up @@ -35,4 +35,5 @@ public interface GalleryCommCapabilities {
public static final int CAPA_FETCH_HIERARCHICAL = 4;
public static final int CAPA_ALBUM_INFO = 5;
public static final int CAPA_NEW_ALBUM = 6;
public static final int CAPA_FETCH_ALBUMS_PRUNE = 7;
}

0 comments on commit 2c0df14

Please sign in to comment.