public abstract class Account
{
public static final int PRIVACY_PUBLIC = 1;
public static final int PRIVACY_PRIVATE = 2;
public static final int PRIVACY_FRIENDS = 4;
public static final int PRIVACY_FAMILY = 8;
protected String name;
protected List defaultTags = new LinkedList();
protected List userTags = new LinkedList();
protected int defaultPrivacySetting = 1;
protected boolean authenticated;
protected Preferences prefs = Preferences.userNodeForPackage(getClass());
protected URL photoURL;
private long currentUploadUsed;
private long monthlyUploadLimit;
protected String username;
protected static URLCodec codec = new URLCodec();
public Account(String name) {
this.name = name;
}
public boolean isAuthenticated()
{
return this.authenticated;
}
public void setAuthenticated(boolean authenticated)
{
this.authenticated = authenticated;
}
public String getName()
{
return this.name;
}
public void setName(String name)
{
this.name = name;
}
public abstract ImageUploadApi getApi();
public synchronized void save() {
save(this.prefs);
}
package org.scohen.juploadr.uploadapi;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.net.URLCodec;
import org.scohen.juploadr.app.tags.TagParser;
public abstract class Account
{
public static final int PRIVACY_PUBLIC = 1;
public static final int PRIVACY_PRIVATE = 2;
public static final int PRIVACY_FRIENDS = 4;
public static final int PRIVACY_FAMILY = 8;
protected String name;
protected List defaultTags = new LinkedList();
protected List userTags = new LinkedList();
protected int defaultPrivacySetting = 1;
protected boolean authenticated;
protected Preferences prefs = Preferences.userNodeForPackage(getClass());
protected URL photoURL;
private long currentUploadUsed;
private long monthlyUploadLimit;
protected String username;
protected static URLCodec codec = new URLCodec();
public Account(String name) {
this.name = name;
}
public boolean isAuthenticated()
{
return this.authenticated;
}
public void setAuthenticated(boolean authenticated)
{
this.authenticated = authenticated;
}
public String getName()
{
return this.name;
}
public void setName(String name)
{
this.name = name;
}
public abstract ImageUploadApi getApi();
public synchronized void save() {
save(this.prefs);
}
protected void save(Preferences prefs) {
try {
Map properties = BeanUtils.describe(this);
Iterator iter = properties.keySet().iterator();
String encodedUsername = encodeUsername(this.name);
}
public void delete()
{
try
{
String[] keys = this.prefs.keys();
String encodedName = encodeUsername(this.name);
for (int i = 0; i < keys.length; i++) {
if (keys[i].indexOf(encodedName) >= 0)
this.prefs.remove(keys[i]);
}
}
catch (BackingStoreException e)
{
e.printStackTrace();
}
}
protected static Map allAccountsIn(Class accountClass, Preferences prefs)
{
HashMap usernames = new HashMap();
try {
String[] keys = prefs.keys();
for (int i = 0; i < keys.length; i++) {
String[] keyParts = keys[i].split("\.");
if (getShortClassName(accountClass).equals(keyParts[0])) {
String decodedUsername = decodeUsername(keyParts[1]);
if (usernames.get(decodedUsername) == null) {
try {
usernames.put(decodedUsername, accountClass.newInstance());
}
catch (InstantiationException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
}
Object account = usernames.get(decodedUsername);
String property = prefs.get(keys[i], "");
if (!property.startsWith("["))
{
BeanUtils.setProperty(account, keyParts[2], prefs.get(keys[i], ""));
}
}
}
}
catch (BackingStoreException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
return usernames;
}
private static String getShortClassName(Class clazz)
{
String className = clazz.getName();
return className.substring(className.lastIndexOf(".") + 1);
}
public long getMonthlyUploadLimit() {
return this.monthlyUploadLimit;
}
public long getCurrentUploadUsed() {
return this.currentUploadUsed;
}
public void setCurrentUploadUsed(long currentUploadUsed)
{
this.currentUploadUsed = currentUploadUsed;
}
public void setMonthlyUploadLimit(long monthlyUploadLimit)
{
this.monthlyUploadLimit = monthlyUploadLimit;
}
public boolean equals(Object other)
{
if (other.getClass().equals(getClass())) {
Account otherAccount = (Account)other;
return getName().equals(otherAccount.getName());
}
return false;
}
public int getDefaultPrivacySetting()
{
return this.defaultPrivacySetting;
}
public void setDefaultPrivacySetting(int defaultPrivacySetting)
{
this.defaultPrivacySetting = defaultPrivacySetting;
}
public List getDefaultTags()
{
return Collections.unmodifiableList(this.defaultTags);
}
public void setSavedDefaultTags(String defaultTags) {
this.defaultTags = TagParser.parse(defaultTags);
}
public String getSavedDefaultTags() {
return TagParser.toInputString(this.defaultTags);
}
public void setDefaultTags(List defaultTags)
{
this.defaultTags = defaultTags;
}
public List getUserTags() {
return this.userTags;
}
public void setUserTags(List userTags) {
this.userTags = userTags;
}
public abstract URL getPhotoURL();
public String getUsername() {
return this.username;
}
public void setUsername(String username)
{
this.username = username;
}
private String encodeUsername(String username) {
String encoded = username;
try {
codec.encode(username);
}
catch (EncoderException ee) {
}
encoded = encoded.replaceAll("\.", "%2e");
return encoded;
}
private static String decodeUsername(String encoded) {
String decoded = "";
try {
decoded = codec.decode(encoded);
}
catch (DecoderException e) {
}
return decoded;
}
}