mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-05 10:49:24 +02:00
NetworkDatabase implementation
git-svn-id: https://only.mawhrin.net/repos/FBReaderJ/trunk@1520 6a642e6f-84f6-412e-ac94-c4a38d5a04b0
This commit is contained in:
parent
ba7b135ebf
commit
c7e52706ac
9 changed files with 332 additions and 68 deletions
|
@ -58,6 +58,8 @@ class NetworkView {
|
|||
}
|
||||
|
||||
public void initialize() {
|
||||
new SQLiteNetworkDatabase();
|
||||
|
||||
NetworkLibrary.Instance().synchronize();
|
||||
|
||||
myActions.add(new NetworkBookActions());
|
||||
|
|
|
@ -19,18 +19,43 @@
|
|||
|
||||
package org.geometerplus.android.fbreader.network;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteStatement;
|
||||
|
||||
import org.geometerplus.zlibrary.ui.android.library.ZLAndroidApplication;
|
||||
|
||||
import org.geometerplus.fbreader.network.ICustomNetworkLink;
|
||||
import org.geometerplus.fbreader.network.NetworkDatabase;
|
||||
|
||||
public class SQLiteNetworkDatabase extends NetworkDatabase {
|
||||
class SQLiteNetworkDatabase extends NetworkDatabase {
|
||||
private final SQLiteDatabase myDatabase;
|
||||
|
||||
public SQLiteNetworkDatabase() {
|
||||
SQLiteNetworkDatabase() {
|
||||
myDatabase = ZLAndroidApplication.Instance().openOrCreateDatabase("network.db", Context.MODE_PRIVATE, null);
|
||||
migrate();
|
||||
}
|
||||
|
||||
private void migrate() {
|
||||
final int version = myDatabase.getVersion();
|
||||
final int currentCodeVersion = 1;
|
||||
if (version >= currentCodeVersion) {
|
||||
return;
|
||||
}
|
||||
myDatabase.beginTransaction();
|
||||
switch (version) {
|
||||
case 0:
|
||||
createTables();
|
||||
}
|
||||
myDatabase.setTransactionSuccessful();
|
||||
myDatabase.endTransaction();
|
||||
|
||||
myDatabase.execSQL("VACUUM");
|
||||
myDatabase.setVersion(currentCodeVersion);
|
||||
}
|
||||
|
||||
protected void executeAsATransaction(Runnable actions) {
|
||||
|
@ -42,4 +67,141 @@ public class SQLiteNetworkDatabase extends NetworkDatabase {
|
|||
myDatabase.endTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
private static void bindString(SQLiteStatement statement, int index, String value) {
|
||||
if (value != null) {
|
||||
statement.bindString(index, value);
|
||||
} else {
|
||||
statement.bindNull(index);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void loadCustomLinks(List<ICustomNetworkLink> links, ICustomLinksFactory factory) {
|
||||
final Cursor cursor = myDatabase.rawQuery("SELECT link_id,title,site_name,summary,icon FROM CustomLinks", null);
|
||||
final HashMap<String,String> linksMap = new HashMap<String,String>();
|
||||
while (cursor.moveToNext()) {
|
||||
final int id = cursor.getInt(0);
|
||||
final String title = cursor.getString(1);
|
||||
final String siteName = cursor.getString(2);
|
||||
final String summary = cursor.getString(3);
|
||||
final String icon = cursor.getString(4);
|
||||
|
||||
linksMap.clear();
|
||||
final Cursor linksCursor = myDatabase.rawQuery("SELECT key,url FROM CustomLinkUrls WHERE link_id = " + id, null);
|
||||
while (linksCursor.moveToNext()) {
|
||||
linksMap.put(linksCursor.getString(0), linksCursor.getString(1));
|
||||
}
|
||||
linksCursor.close();
|
||||
|
||||
final ICustomNetworkLink newLink = factory.createCustomLink(id, siteName, title, summary, icon, linksMap);
|
||||
if (newLink != null) {
|
||||
links.add(newLink);
|
||||
}
|
||||
}
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
private SQLiteStatement myInsertCustomLinkStatement;
|
||||
private SQLiteStatement myUpdateCustomLinkStatement;
|
||||
private SQLiteStatement myInsertCustomLinkUrlStatement;
|
||||
private SQLiteStatement myUpdateCustomLinkUrlStatement;
|
||||
private SQLiteStatement myDeleteCustomLinkUrlStatement;
|
||||
@Override
|
||||
protected void saveCustomLink(final ICustomNetworkLink link) {
|
||||
executeAsATransaction(new Runnable() {
|
||||
public void run() {
|
||||
final SQLiteStatement statement;
|
||||
if (link.getId() == ICustomNetworkLink.INVALID_ID) {
|
||||
if (myInsertCustomLinkStatement == null) {
|
||||
myInsertCustomLinkStatement = myDatabase.compileStatement(
|
||||
"INSERT INTO CustomLinks (title,site_name,summary,icon) VALUES (?,?,?,?)"
|
||||
);
|
||||
}
|
||||
statement = myInsertCustomLinkStatement;
|
||||
} else {
|
||||
if (myUpdateCustomLinkStatement == null) {
|
||||
myUpdateCustomLinkStatement = myDatabase.compileStatement(
|
||||
"UPDATE CustomLinks SET title = ?, site_name = ?, summary =?, icon = ? "
|
||||
+ "WHERE link_id = ?"
|
||||
);
|
||||
}
|
||||
statement = myUpdateCustomLinkStatement;
|
||||
}
|
||||
|
||||
statement.bindString(1, link.getTitle());
|
||||
statement.bindString(2, link.getSiteName());
|
||||
bindString(statement, 3, link.getSummary());
|
||||
bindString(statement, 4, link.getIcon());
|
||||
|
||||
final long id;
|
||||
final HashMap<String,String> linksMap = new HashMap<String,String>();
|
||||
|
||||
if (statement == myInsertCustomLinkStatement) {
|
||||
id = statement.executeInsert();
|
||||
link.setId((int) id);
|
||||
} else {
|
||||
id = link.getId();
|
||||
statement.bindLong(5, id);
|
||||
statement.execute();
|
||||
|
||||
final Cursor linksCursor = myDatabase.rawQuery("SELECT key,url FROM CustomLinkUrls WHERE link_id = " + link.getId(), null);
|
||||
while (linksCursor.moveToNext()) {
|
||||
linksMap.put(linksCursor.getString(0), linksCursor.getString(1));
|
||||
}
|
||||
linksCursor.close();
|
||||
}
|
||||
|
||||
for (String key: link.getLinkKeys()) {
|
||||
final String value = link.getLink(key);
|
||||
final String dbValue = linksMap.remove(key);
|
||||
final SQLiteStatement urlStatement;
|
||||
if (dbValue == null) {
|
||||
if (myInsertCustomLinkUrlStatement == null) {
|
||||
myInsertCustomLinkUrlStatement = myDatabase.compileStatement(
|
||||
"INSERT INTO CustomLinkUrls(url,link_id,key) VALUES (?,?,?)");
|
||||
}
|
||||
urlStatement = myInsertCustomLinkUrlStatement;
|
||||
} else if (!value.equals(dbValue)) {
|
||||
if (myUpdateCustomLinkUrlStatement == null) {
|
||||
myUpdateCustomLinkUrlStatement = myDatabase.compileStatement(
|
||||
"UPDATE CustomLinkUrls SET url = ? WHERE link_id = ? AND key = ?");
|
||||
}
|
||||
urlStatement = myUpdateCustomLinkUrlStatement;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
urlStatement.bindString(1, value);
|
||||
urlStatement.bindLong(2, id);
|
||||
urlStatement.bindString(3, key);
|
||||
urlStatement.execute();
|
||||
}
|
||||
for (String key: linksMap.keySet()) {
|
||||
if (myDeleteCustomLinkUrlStatement == null) {
|
||||
myDeleteCustomLinkUrlStatement = myDatabase.compileStatement(
|
||||
"DELETE FROM CustomLinkUrls WHERE link_id = ? AND key = ?");
|
||||
}
|
||||
myDeleteCustomLinkUrlStatement.bindLong(1, id);
|
||||
myDeleteCustomLinkUrlStatement.bindString(2, key);
|
||||
myDeleteCustomLinkUrlStatement.execute();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void createTables() {
|
||||
myDatabase.execSQL(
|
||||
"CREATE TABLE CustomLinks(" +
|
||||
"link_id INTEGER PRIMARY KEY," +
|
||||
"title TEXT UNIQUE NOT NULL," +
|
||||
"site_name TEXT NOT NULL," +
|
||||
"summary TEXT," +
|
||||
"icon TEXT)");
|
||||
myDatabase.execSQL(
|
||||
"CREATE TABLE CustomLinkUrls(" +
|
||||
"key TEXT NOT NULL," +
|
||||
"link_id INTEGER NOT NULL REFERENCES CustomLinks(link_id)," +
|
||||
"url TEXT NOT NULL," +
|
||||
"CONSTRAINT CustomLinkUrls_PK PRIMARY KEY (key, link_id))");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
package org.geometerplus.fbreader.network;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeMap;
|
||||
|
||||
|
||||
|
@ -68,4 +69,8 @@ public abstract class AbstractNetworkLink implements INetworkLink {
|
|||
public String getLink(String urlKey) {
|
||||
return myLinks.get(urlKey);
|
||||
}
|
||||
|
||||
public Set<String> getLinkKeys() {
|
||||
return myLinks.keySet();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,6 +21,18 @@ package org.geometerplus.fbreader.network;
|
|||
|
||||
public interface ICustomNetworkLink extends INetworkLink {
|
||||
|
||||
public static final int INVALID_ID = -1;
|
||||
|
||||
int getId();
|
||||
void setId(int id);
|
||||
|
||||
interface SaveLinkListener {
|
||||
void onSaveLink(ICustomNetworkLink link);
|
||||
}
|
||||
|
||||
void setSaveLinkListener(SaveLinkListener listener);
|
||||
void saveLink();
|
||||
|
||||
void setSiteName(String name);
|
||||
void setTitle(String title);
|
||||
void setSummary(String summary);
|
||||
|
|
|
@ -19,6 +19,8 @@
|
|||
|
||||
package org.geometerplus.fbreader.network;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.geometerplus.zlibrary.core.network.ZLNetworkRequest;
|
||||
|
||||
import org.geometerplus.fbreader.network.authentication.NetworkAuthenticationManager;
|
||||
|
@ -40,6 +42,7 @@ public interface INetworkLink {
|
|||
String getIcon();
|
||||
String getLink(String urlKey);
|
||||
|
||||
Set<String> getLinkKeys();
|
||||
|
||||
ZLNetworkRequest simpleSearchRequest(String pattern, NetworkOperationData data);
|
||||
ZLNetworkRequest resume(NetworkOperationData data);
|
||||
|
|
|
@ -19,6 +19,9 @@
|
|||
|
||||
package org.geometerplus.fbreader.network;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public abstract class NetworkDatabase {
|
||||
private static NetworkDatabase ourInstance;
|
||||
|
||||
|
@ -32,4 +35,10 @@ public abstract class NetworkDatabase {
|
|||
|
||||
protected abstract void executeAsATransaction(Runnable actions);
|
||||
|
||||
public interface ICustomLinksFactory {
|
||||
ICustomNetworkLink createCustomLink(int id, String siteName, String title, String summary, String icon, Map<String, String> links);
|
||||
}
|
||||
|
||||
protected abstract void loadCustomLinks(List<ICustomNetworkLink> links, ICustomLinksFactory factory);
|
||||
protected abstract void saveCustomLink(ICustomNetworkLink link);
|
||||
}
|
||||
|
|
|
@ -213,7 +213,6 @@ public class NetworkLibrary {
|
|||
|
||||
private final ArrayList<INetworkLink> myLoadedLinks = new ArrayList<INetworkLink>();
|
||||
private final ArrayList<ICustomNetworkLink> myCustomLinks = new ArrayList<ICustomNetworkLink>();
|
||||
|
||||
private final CompositeList myLinks;
|
||||
|
||||
private final RootTree myRootTree = new RootTree();
|
||||
|
@ -223,7 +222,7 @@ public class NetworkLibrary {
|
|||
|
||||
private NetworkLibrary() {
|
||||
LinkedList<String> catalogs = readCatalogFileNames();
|
||||
OPDSLinkReader reader = new OPDSLinkReader();
|
||||
final OPDSLinkReader reader = new OPDSLinkReader();
|
||||
for (String fileName: catalogs) {
|
||||
INetworkLink link = reader.readDocument(ZLResourceFile.createResourceFile("data/network/" + fileName));
|
||||
if (link != null) {
|
||||
|
@ -231,17 +230,31 @@ public class NetworkLibrary {
|
|||
}
|
||||
}
|
||||
|
||||
Map<String, String> links;
|
||||
|
||||
links = new TreeMap<String, String>();
|
||||
/*final HashMap<String, String> links = new HashMap<String, String>();
|
||||
links.put(INetworkLink.URL_MAIN, "http://bookserver.archive.org/catalog/");
|
||||
myCustomLinks.add( reader.createCustomLink("archive.org",
|
||||
"Internet Archive Catalog", null, null, links));
|
||||
|
||||
links = new TreeMap<String, String>();
|
||||
NetworkDatabase.Instance().saveCustomLink(
|
||||
reader.createCustomLink(
|
||||
ICustomNetworkLink.INVALID_ID,
|
||||
"archive.org", "Internet Archive Catalog", null, null, links
|
||||
)
|
||||
);
|
||||
links.clear();
|
||||
links.put(INetworkLink.URL_MAIN, "http://pragprog.com/magazines.opds");
|
||||
myCustomLinks.add( reader.createCustomLink("pragprog.com",
|
||||
"PragPub Magazine", "The Pragmatic Bookshelf", null, links));
|
||||
NetworkDatabase.Instance().saveCustomLink(
|
||||
reader.createCustomLink(
|
||||
ICustomNetworkLink.INVALID_ID,
|
||||
"pragprog.com", "PragPub Magazine", "The Pragmatic Bookshelf", null, links
|
||||
)
|
||||
);*/
|
||||
|
||||
NetworkDatabase.Instance().loadCustomLinks(myCustomLinks,
|
||||
new NetworkDatabase.ICustomLinksFactory() {
|
||||
public ICustomNetworkLink createCustomLink(int id, String siteName,
|
||||
String title, String summary, String icon, Map<String, String> links) {
|
||||
return reader.createCustomLink(id, siteName, title, summary, icon, links);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
LinksComparator comparator = new LinksComparator();
|
||||
Collections.sort(myLoadedLinks, comparator);
|
||||
|
@ -275,11 +288,13 @@ public class NetworkLibrary {
|
|||
|
||||
public String rewriteUrl(String url, boolean externalUrl) {
|
||||
final String host = ZLNetworkUtil.hostFromUrl(url).toLowerCase();
|
||||
synchronized (myLinks) {
|
||||
for (INetworkLink link: myLinks) {
|
||||
if (host.contains(link.getSiteName())) {
|
||||
url = link.rewriteUrl(url, externalUrl);
|
||||
}
|
||||
}
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
|
@ -298,6 +313,7 @@ public class NetworkLibrary {
|
|||
FBTree currentNode = null;
|
||||
int nodeCount = 0;
|
||||
|
||||
synchronized (myLinks) {
|
||||
ListIterator<INetworkLink> it = myLinks.listIterator();
|
||||
while (it.hasNext()) {
|
||||
INetworkLink link = it.next();
|
||||
|
@ -344,6 +360,7 @@ public class NetworkLibrary {
|
|||
nodeIterator = myRootTree.subTrees().listIterator(nextIndex + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while (currentNode != null || nodeIterator.hasNext()) {
|
||||
if (currentNode == null) {
|
||||
|
@ -351,7 +368,6 @@ public class NetworkLibrary {
|
|||
}
|
||||
toRemove.add(currentNode);
|
||||
currentNode = null;
|
||||
//++nodeCount; // TODO: where to increment???
|
||||
}
|
||||
|
||||
for (FBTree tree: toRemove) {
|
||||
|
@ -396,7 +412,8 @@ public class NetworkLibrary {
|
|||
}
|
||||
};
|
||||
|
||||
for (INetworkLink link: myLoadedLinks) {
|
||||
synchronized (myLinks) {
|
||||
for (INetworkLink link: myLinks) {
|
||||
//if (link.OnOption.getValue()) {
|
||||
// execute next code only if link is enabled
|
||||
//}
|
||||
|
@ -407,6 +424,7 @@ public class NetworkLibrary {
|
|||
requestList.add(request);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while (requestList.size() != 0) {
|
||||
final String errorMessage = ZLNetworkManager.Instance().perform(requestList);
|
||||
|
@ -426,4 +444,34 @@ public class NetworkLibrary {
|
|||
|
||||
return null;
|
||||
}
|
||||
|
||||
private ICustomNetworkLink.SaveLinkListener myChangesListener = new ICustomNetworkLink.SaveLinkListener() {
|
||||
public void onSaveLink(ICustomNetworkLink link) {
|
||||
NetworkDatabase.Instance().saveCustomLink(link);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @return <code>false</code> if this library already contains link with the specified title
|
||||
* <code>true</code> if links has been inserted into this library
|
||||
*/
|
||||
public boolean addCustomLink(ICustomNetworkLink link) {
|
||||
final int index = Collections.binarySearch(myCustomLinks, link, new LinksComparator());
|
||||
if (index >= 0) {
|
||||
return false;
|
||||
}
|
||||
final int insertAt = -index - 1;
|
||||
myCustomLinks.add(insertAt, link);
|
||||
link.setSaveLinkListener(myChangesListener);
|
||||
link.saveLink();
|
||||
return true;
|
||||
}
|
||||
|
||||
public int getCustomLinksNumber() {
|
||||
return myCustomLinks.size();
|
||||
}
|
||||
|
||||
public ICustomNetworkLink getCustomLink(int index) {
|
||||
return myCustomLinks.get(index);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,8 +26,30 @@ import org.geometerplus.fbreader.network.ICustomNetworkLink;
|
|||
|
||||
class OPDSCustomLink extends OPDSLink implements ICustomNetworkLink {
|
||||
|
||||
OPDSCustomLink(String siteName, String title, String summary, String icon, Map<String, String> links) {
|
||||
private int myId;
|
||||
private SaveLinkListener myListener;
|
||||
|
||||
OPDSCustomLink(int id, String siteName, String title, String summary, String icon, Map<String, String> links) {
|
||||
super(siteName, title, summary, icon, links);
|
||||
myId = id;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return myId;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
myId = id;
|
||||
}
|
||||
|
||||
public void setSaveLinkListener(SaveLinkListener listener) {
|
||||
myListener = listener;
|
||||
}
|
||||
|
||||
public void saveLink() {
|
||||
if (myListener != null) {
|
||||
myListener.onSaveLink(this);
|
||||
}
|
||||
}
|
||||
|
||||
public final void setIcon(String icon) {
|
||||
|
|
|
@ -87,14 +87,15 @@ public class OPDSLinkReader extends ZLXMLReaderAdapter {
|
|||
return opdsLink;
|
||||
}
|
||||
|
||||
public ICustomNetworkLink createCustomLink(String siteName, String title, String summary, String icon, Map<String, String> links) {
|
||||
public ICustomNetworkLink createCustomLink(int id, String siteName, String title, String summary, String icon, Map<String, String> links) {
|
||||
if (siteName == null || title == null || links.get(INetworkLink.URL_MAIN) == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
OPDSCustomLink link = new OPDSCustomLink(siteName, title, summary, icon, links);
|
||||
OPDSCustomLink link = new OPDSCustomLink(id, siteName, title, summary, icon, links);
|
||||
|
||||
// TODO: read common OPDSLink attributes from special custom.xml file
|
||||
// Does this additional info have to override duplicated settings, received from user???
|
||||
|
||||
return link;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue