1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/java/isac/util/users/UserSettings.java Wed Jun 15 18:31:02 2005 +0200
1.3 @@ -0,0 +1,140 @@
1.4 +/*
1.5 + * Created on Feb 22, 2005
1.6 + *
1.7 + * @author Alan Krempler
1.8 + */
1.9 +
1.10 +package isac.util.users;
1.11 +
1.12 +import java.io.FileInputStream;
1.13 +import java.io.FileNotFoundException;
1.14 +import java.io.FileOutputStream;
1.15 +import java.io.IOException;
1.16 +import java.util.Enumeration;
1.17 +import java.util.HashMap;
1.18 +import java.util.Map;
1.19 +import java.util.Properties;
1.20 +
1.21 +/**
1.22 + * Class for storing/retrieving user settings, i.e. settings which can be set
1.23 + * manually by the user as opposed to the
1.24 + * {@link isac.util.usersettings.UserModel#}, which is an abstraction of the
1.25 + * system's expeience with a user and canot be altered manually. As there is no
1.26 + * fixed set of user settings, you can regard this class as a simple storage
1.27 + * backend for key/value pairs.
1.28 + *
1.29 + * @author Alan Krempler
1.30 + *
1.31 + */
1.32 +public class UserSettings extends Properties implements IUserSettings {
1.33 +
1.34 + private HashMap storage_;
1.35 +
1.36 + String userpath_;
1.37 +
1.38 + String username_;
1.39 +
1.40 + public static Map valstr_valint_;
1.41 +
1.42 + /**
1.43 + * connect the String-representation of a UserSettings-value with the
1.44 + * respective Integer-representation
1.45 + *
1.46 + * @see isac.uti.users.IUserSettings
1.47 + */
1.48 + private HashMap createValstrValint() {
1.49 + HashMap m = new HashMap();
1.50 + m.put("DUMMY", new Integer(IUserSettings.DUMMY));
1.51 + m.put("VAL1_SKIP_SPECIFY_TO_START_SOLVE", new Integer(
1.52 + IUserSettings.VAL1_SKIP_SPECIFY_TO_START_SOLVE));
1.53 + m.put("VAL1_POP_CALCHEAD_WITH_DESCRIPTIONS", new Integer(
1.54 + IUserSettings.VAL1_POP_CALCHEAD_WITH_DESCRIPTIONS));
1.55 + m.put("VAL2_FORMULAE_ONLY", new Integer(
1.56 + IUserSettings.VAL2_FORMULAE_ONLY));
1.57 + m.put("VAL2_FORMULA_TACTIC_FORMULA_", new Integer(
1.58 + IUserSettings.VAL2_FORMULA_TACTIC_FORMULA_));
1.59 + //m.put("",
1.60 + // new Integer(IUserSettings.));
1.61 + return m;
1.62 + }
1.63 +
1.64 + public UserSettings(String userpath, String username) {
1.65 + userpath_ = userpath;
1.66 + username_ = username;
1.67 +
1.68 + //load Properties (keystr, valstr) temporarily
1.69 + Properties keystr_valstr = new Properties();
1.70 + String filename = userpath_ + username + "_settings.txt";
1.71 + try {
1.72 + FileInputStream propInFile = new FileInputStream(filename);
1.73 + keystr_valstr.load(propInFile);
1.74 + keystr_valstr.list(System.out);
1.75 + } catch (FileNotFoundException e) {
1.76 + System.err.println("Can?t find " + filename);
1.77 + } catch (IOException e) {
1.78 + System.err.println("I/O failed.");
1.79 + }
1.80 +
1.81 + //create HashMap (valstr, valint) temporarily
1.82 + HashMap valstr_valint = new HashMap();
1.83 + valstr_valint = this.createValstrValint();
1.84 +
1.85 + //create HashMap (keystr, \\valstr\\ valint) and store it
1.86 + storage_ = new HashMap();
1.87 + Enumeration keystrs = keystr_valstr.propertyNames();//WN050311 how
1.88 + // better ?
1.89 + String keystr = null;
1.90 + String valstr = null;
1.91 + Object valint = null;
1.92 + while (keystrs.hasMoreElements()) {
1.93 + keystr = (String) keystrs.nextElement();
1.94 + valstr = keystr_valstr.getProperty(keystr);
1.95 + valint = valstr_valint.get(valstr);
1.96 + storage_.put(keystr, valint);
1.97 + }
1.98 + }
1.99 +
1.100 + /**
1.101 + * @param key
1.102 + * A string key
1.103 + * @return The string value associated with the key parameter; null, if
1.104 + * nothing is stored under this key
1.105 + */
1.106 + public int getValue(String key) {
1.107 + return ((Integer) storage_.get(key)).intValue();
1.108 + }
1.109 +
1.110 + /**
1.111 + * @param key
1.112 + * The key under which "value" is to be stored. Existing data
1.113 + * unde the same key will be replaced.
1.114 + * @param value
1.115 + * The value to be stored under "key"
1.116 + * @return true in case of success, false in case of error, for example if
1.117 + * trying to replace stored values without proper permissions
1.118 + */
1.119 + public boolean setValue(String key, int value) {
1.120 + //TODO.WN050311 storage_.put(key, value);
1.121 + return true;
1.122 + }
1.123 +
1.124 + //WN050311 just for creation of the first file
1.125 + //beware of the fix path !
1.126 + public static void main(String[] args) {
1.127 + UserSettings s = new UserSettings("isac/xmldata/users/", "x");
1.128 + s.setProperty("start_worksheet", "SKIP_SPECIFY_START_SOLVE");
1.129 + s.setProperty("enter_specify", "SKIP_SPECIFY_START_SOLVE");
1.130 + s.setProperty("next_button", "FORMULAE_ONLY");
1.131 +
1.132 + String filename = "isac/xmldata/users/x_settings.txt";
1.133 + try {
1.134 + FileOutputStream propOutFile = new FileOutputStream(filename);
1.135 + s.store(propOutFile, "static dialog data of user 'x'");
1.136 +
1.137 + } catch (IOException e) {
1.138 + System.err.println("I/O failed.");
1.139 + }
1.140 + }
1.141 +
1.142 +}
1.143 +