Skip to content

Commit

Permalink
store properties in globals
Browse files Browse the repository at this point in the history
  • Loading branch information
zesaro committed Oct 15, 2016
1 parent 1bd1d2c commit e415619
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public class JabRefPreferences {
*/

// Push to application preferences
public static final String BIBSONOMY_PROPERTIES = "bibsonomyProperties";
public static final String EMACS_PATH = "emacsPath";
public static final String EMACS_ADDITIONAL_PARAMETERS = "emacsParameters";
public static final String EMACS_23 = "emacsUseV23InsertString";
Expand Down
155 changes: 83 additions & 72 deletions src/main/java/org/bibsonomy/plugin/jabref/BibsonomyProperties.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package org.bibsonomy.plugin.jabref;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.sf.jabref.preferences.JabRefPreferences;

import org.bibsonomy.common.enums.GroupingEntity;
import org.bibsonomy.model.enums.Order;

Expand All @@ -18,40 +14,38 @@
*/
public class BibsonomyProperties extends Properties {

private static final Log LOGGER = LogFactory.getLog(BibsonomyProperties.class);

//API properties
private static final String API_URL = "api.url";
private static final String API_USERNAME = "api.username";
private static final String API_KEY = "api.key";


//Plugin properties
private static final String PLUGIN_SAVE_API_KEY = "plugin.saveapikey";
private static final String PLUGIN_DOCUMENTS_IMPORT = "plugin.documents.import";
private static final String PLUGIN_DOCUMENTS_EXPORT = "plugin.documents.export";
private static final String PLUGIN_TAGS_REFRESH_ON_STARTUP = "plugin.tags.refreshonstartup";
private static final String PLUGIN_TAGS_IGNORE_NO_TAGS = "plugin.tags.ignorenotags";
private static final String PLUGIN_NUMBER_OF_POSTS_PER_REQUEST = "plugin.request.size";
private static final String PLUGIN_IGNORE_WARNING_MORE_POSTS = "plugin.request.size.ignorewarning";
private static final String PLUGIN_EXTRA_TAB_FIELDS = "plugin.tabs.extra";
private static final String PLUGIN_VISIBILITY = "plugin.visibilty";
private static final String PLUGIN_TAG_CLOUD_SIZE = "plugin.tagcloud.size";
private static final String PLUGIN_SIDE_PANE_VISIBILITY_TYPE = "plugin.sidepane.visibility.type";
private static final String PLUGIN_SIDE_PANE_VISIBILITY_NAME = "plugin.sidepane.visibility.name";
private static final String PLUGIN_TAG_CLOUD_ORDER = "plugin.tagcloud.order";
//BibSonomy properties
private static final String BIBSONOMY_SAVE_API_KEY = "bibsonomy.saveapikey";
private static final String BIBSONOMY_DOCUMENTS_IMPORT = "bibsonomy.documents.import";
private static final String BIBSONOMY_DOCUMENTS_EXPORT = "bibsonomy.documents.export";
private static final String BIBSONOMY_TAGS_REFRESH_ON_STARTUP = "bibsonomy.tags.refreshonstartup";
private static final String BIBSONOMY_TAGS_IGNORE_NO_TAGS = "bibsonomy.tags.ignorenotags";
private static final String BIBSONOMY_NUMBER_OF_POSTS_PER_REQUEST = "bibsonomy.request.size";
private static final String BIBSONOMY_IGNORE_WARNING_MORE_POSTS = "bibsonomy.request.size.ignorewarning";
private static final String BIBSONOMY_EXTRA_TAB_FIELDS = "bibsonomy.tabs.extra";
private static final String BIBSONOMY_VISIBILITY = "bibsonomy.visibilty";
private static final String BIBSONOMY_TAG_CLOUD_SIZE = "bibsonomy.tagcloud.size";
private static final String BIBSONOMY_SIDE_PANE_VISIBILITY_TYPE = "bibsonomy.sidepane.visibility.type";
private static final String BIBSONOMY_SIDE_PANE_VISIBILITY_NAME = "bibsonomy.sidepane.visibility.name";
private static final String BIBSONOMY_TAG_CLOUD_ORDER = "bibsonomy.tagcloud.order";

//Array containing all property constants
private static final String[] propsArray = {API_URL, API_USERNAME, API_KEY, BIBSONOMY_SAVE_API_KEY, BIBSONOMY_DOCUMENTS_IMPORT, BIBSONOMY_DOCUMENTS_EXPORT, BIBSONOMY_TAGS_REFRESH_ON_STARTUP, BIBSONOMY_TAGS_IGNORE_NO_TAGS, BIBSONOMY_NUMBER_OF_POSTS_PER_REQUEST, BIBSONOMY_IGNORE_WARNING_MORE_POSTS, BIBSONOMY_EXTRA_TAB_FIELDS, BIBSONOMY_VISIBILITY, BIBSONOMY_TAG_CLOUD_SIZE, BIBSONOMY_SIDE_PANE_VISIBILITY_TYPE, BIBSONOMY_SIDE_PANE_VISIBILITY_NAME, BIBSONOMY_TAG_CLOUD_ORDER};


/**
* Singleton of {@link BibsonomyProperties}
*/
private static BibsonomyProperties INSTANCE = null;
private static BibsonomyProperties INSTANCE;

private static JabRefPreferences preferences = JabRefPreferences.getInstance();

/**
* location of properties file; JabRef stores the plugin itself also in this
* directory.
*/
private static final String PATH_TO_PROPERTIES_FILE = System.getProperty("user.home") + "/.jabref/plugins/";
private static final String PROPERTIES_FILE = PATH_TO_PROPERTIES_FILE + "bibsonomy-plugin.properties";

/**
* Get the singleton of {@link BibsonomyProperties}.
Expand All @@ -60,45 +54,62 @@ public class BibsonomyProperties extends Properties {
* @return singleton of {@link BibsonomyProperties}
*/
public static BibsonomyProperties getInstance() {
if (INSTANCE == null)
if (INSTANCE == null) {
INSTANCE = new BibsonomyProperties();
loadProperties(INSTANCE);
}
return INSTANCE;
}

private static void loadProperties(BibsonomyProperties instance) {

String prefs = preferences.get(JabRefPreferences.BIBSONOMY_PROPERTIES);

if (prefs.isEmpty()) {
return;
}

for (String property : propsArray) {
if (prefs.contains(property)) {
int lastIndexOf = prefs.lastIndexOf(property + "=") + property.length() + 1;
String propertyValue;

if (prefs.indexOf(",", lastIndexOf) >= 0) {
propertyValue = prefs.substring(lastIndexOf, prefs.indexOf(",", lastIndexOf));
} else {
propertyValue = prefs.substring(lastIndexOf, prefs.indexOf("}", lastIndexOf));
}
instance.setProperty(property, propertyValue);
}
}

}

/**
* The constructor reads the properties from the file system.
*/
private BibsonomyProperties() {
try {
File propertiesFile = new File(PROPERTIES_FILE);
if (propertiesFile.exists() && propertiesFile.isFile())
load(new FileInputStream(propertiesFile));
} catch (Exception e) {
LOGGER.error("Error loading properties file", e);
}
}


/**
* Saves the properties to the file system.
* Checks if the option to store the API is checked.
*/
public static void save() {
String apiKey = getApiKey();
if (!getStoreApiKey())
setApiKey(""); //set the api key to empty string
//if user does not want to save api key
try {
getInstance().store(new FileOutputStream(PROPERTIES_FILE), "");
} catch (IOException e) {
//TODO: C:\Users\Sascha\.jabref\plugins\bibsonomy-plugin.properties (Das System kann den angegebenen Pfad nicht finden) - zellerdev
LOGGER.error("Failed saving properties file");
if (!getStoreApiKey()) {
setApiKey("");
}
//set api key back to its actual value

preferences.clear(JabRefPreferences.BIBSONOMY_PROPERTIES);
preferences.put(JabRefPreferences.BIBSONOMY_PROPERTIES, INSTANCE.toString());

setApiKey(apiKey);
}

public static boolean ignoreNoTagsAssigned() {
return Boolean.valueOf(getInstance().getProperty(PLUGIN_TAGS_IGNORE_NO_TAGS, "false"));
return Boolean.valueOf(getInstance().getProperty(BIBSONOMY_TAGS_IGNORE_NO_TAGS, "false"));
}

public static String getUsername() {
Expand All @@ -114,39 +125,39 @@ public static String getApiUrl() {
}

public static boolean getDownloadDocumentsOnImport() {
return Boolean.parseBoolean(getInstance().getProperty(PLUGIN_DOCUMENTS_IMPORT, "true"));
return Boolean.parseBoolean(getInstance().getProperty(BIBSONOMY_DOCUMENTS_IMPORT, "true"));
}

public static int getNumberOfPostsPerRequest() {
return Integer.parseInt(getInstance().getProperty(PLUGIN_NUMBER_OF_POSTS_PER_REQUEST, BibsonomyGlobals.BIBSONOMY_NUMBER_OF_POSTS_PER_REQUEST));
return Integer.parseInt(getInstance().getProperty(BIBSONOMY_NUMBER_OF_POSTS_PER_REQUEST, BibsonomyGlobals.BIBSONOMY_NUMBER_OF_POSTS_PER_REQUEST));
}

public static boolean getIgnoreMorePostsWarning() {
return Boolean.parseBoolean(getInstance().getProperty(PLUGIN_IGNORE_WARNING_MORE_POSTS, "false"));
return Boolean.parseBoolean(getInstance().getProperty(BIBSONOMY_IGNORE_WARNING_MORE_POSTS, "false"));
}

public static String getExtraTabFields() {
return getInstance().getProperty(PLUGIN_EXTRA_TAB_FIELDS, "issn;isbn");
return getInstance().getProperty(BIBSONOMY_EXTRA_TAB_FIELDS, "issn;isbn");
}

public static String getDefaultVisibilty() {
return getInstance().getProperty(PLUGIN_VISIBILITY, "public");
return getInstance().getProperty(BIBSONOMY_VISIBILITY, "public");
}

public static boolean getStoreApiKey() {
return Boolean.parseBoolean(getInstance().getProperty(PLUGIN_SAVE_API_KEY, "true"));
return Boolean.parseBoolean(getInstance().getProperty(BIBSONOMY_SAVE_API_KEY, "true"));
}

public static boolean getUpdateTagsOnStartUp() {
return Boolean.parseBoolean(getInstance().getProperty(PLUGIN_TAGS_REFRESH_ON_STARTUP, "false"));
return Boolean.parseBoolean(getInstance().getProperty(BIBSONOMY_TAGS_REFRESH_ON_STARTUP, "false"));
}

public static boolean getUploadDocumentsOnExport() {
return Boolean.parseBoolean(getInstance().getProperty(PLUGIN_DOCUMENTS_EXPORT, "true"));
return Boolean.parseBoolean(getInstance().getProperty(BIBSONOMY_DOCUMENTS_EXPORT, "true"));
}

public static int getTagCloudSize() {
return Integer.parseInt(getInstance().getProperty(PLUGIN_TAG_CLOUD_SIZE, "100"));
return Integer.parseInt(getInstance().getProperty(BIBSONOMY_TAG_CLOUD_SIZE, "100"));
}

public static void setUsername(String text) {
Expand All @@ -158,69 +169,69 @@ public static void setApiKey(String text) {
}

public static void setStoreApiKey(boolean selected) {
getInstance().setProperty(PLUGIN_SAVE_API_KEY, String.valueOf(selected));
getInstance().setProperty(BIBSONOMY_SAVE_API_KEY, String.valueOf(selected));
}

public static void setNumberOfPostsPerRequest(int value) {
getInstance().setProperty(PLUGIN_NUMBER_OF_POSTS_PER_REQUEST, String.valueOf(value));
getInstance().setProperty(BIBSONOMY_NUMBER_OF_POSTS_PER_REQUEST, String.valueOf(value));
}

public static void setTagCloudSize(int value) {
getInstance().setProperty(PLUGIN_TAG_CLOUD_SIZE, String.valueOf(value));
getInstance().setProperty(BIBSONOMY_TAG_CLOUD_SIZE, String.valueOf(value));
}

public static void setIgnoreNoTagsAssigned(boolean selected) {
getInstance().setProperty(PLUGIN_TAGS_IGNORE_NO_TAGS, String.valueOf(selected));
getInstance().setProperty(BIBSONOMY_TAGS_IGNORE_NO_TAGS, String.valueOf(selected));
}

public static void setUpdateTagsOnStartup(boolean selected) {
getInstance().setProperty(PLUGIN_TAGS_REFRESH_ON_STARTUP, String.valueOf(selected));
getInstance().setProperty(BIBSONOMY_TAGS_REFRESH_ON_STARTUP, String.valueOf(selected));
}

public static void setUploadDocumentsOnExport(boolean selected) {
getInstance().setProperty(PLUGIN_DOCUMENTS_EXPORT, String.valueOf(selected));
getInstance().setProperty(BIBSONOMY_DOCUMENTS_EXPORT, String.valueOf(selected));

}

public static void setDownloadDocumentsOnImport(boolean selected) {
getInstance().setProperty(PLUGIN_DOCUMENTS_IMPORT, String.valueOf(selected));
getInstance().setProperty(BIBSONOMY_DOCUMENTS_IMPORT, String.valueOf(selected));
}

public static void setDefaultVisisbility(String key) {
getInstance().setProperty(PLUGIN_VISIBILITY, key);
getInstance().setProperty(BIBSONOMY_VISIBILITY, key);
}

public static void setIgnoreMorePostsWarning(boolean selected) {
getInstance().setProperty(PLUGIN_IGNORE_WARNING_MORE_POSTS, String.valueOf(selected));
getInstance().setProperty(BIBSONOMY_IGNORE_WARNING_MORE_POSTS, String.valueOf(selected));
}

public static void setExtraFields(String text) {
getInstance().setProperty(PLUGIN_EXTRA_TAB_FIELDS, text);
getInstance().setProperty(BIBSONOMY_EXTRA_TAB_FIELDS, text);
}

public static GroupingEntity getSidePaneVisibilityType() {
return GroupingEntity.getGroupingEntity(getInstance().getProperty(PLUGIN_SIDE_PANE_VISIBILITY_TYPE, "ALL"));
return GroupingEntity.getGroupingEntity(getInstance().getProperty(BIBSONOMY_SIDE_PANE_VISIBILITY_TYPE, "ALL"));
}

public static String getSidePaneVisibilityName() {
return getInstance().getProperty(PLUGIN_SIDE_PANE_VISIBILITY_NAME, "all users");
return getInstance().getProperty(BIBSONOMY_SIDE_PANE_VISIBILITY_NAME, "all users");
}

public static void setSidePaneVisibilityType(GroupingEntity entity) {
getInstance().setProperty(PLUGIN_SIDE_PANE_VISIBILITY_TYPE, entity.toString());
getInstance().setProperty(BIBSONOMY_SIDE_PANE_VISIBILITY_TYPE, entity.toString());
}

public static void setSidePaneVisibilityName(String value) {
getInstance().setProperty(PLUGIN_SIDE_PANE_VISIBILITY_NAME, value);
getInstance().setProperty(BIBSONOMY_SIDE_PANE_VISIBILITY_NAME, value);
}

public static Order getTagCloudOrder() {
String order = getInstance().getProperty(PLUGIN_TAG_CLOUD_ORDER, "FREQUENCY");
String order = getInstance().getProperty(BIBSONOMY_TAG_CLOUD_ORDER, "FREQUENCY");
return Order.getOrderByName(order);
}

public static void setTagCloudOrder(Order order) {
getInstance().setProperty(PLUGIN_TAG_CLOUD_ORDER, order.toString());
getInstance().setProperty(BIBSONOMY_TAG_CLOUD_ORDER, order.toString());
}

public static void setApiUrl(String text) {
Expand Down

0 comments on commit e415619

Please sign in to comment.