src/java/isac/util/users/UserSettings.java
author wneuper
Fri, 15 Apr 2005 18:46:34 +0200
changeset 2245 e37f09c92eb0
parent 2222 13e12bd75283
child 2273 9eb2653559af
permissions -rw-r--r--
java with part. login-triggered startCalculation
     1 /*
     2  * Created on Feb 22, 2005
     3  *
     4  * @author Alan Krempler
     5  */
     6 
     7 package isac.util.users;
     8 
     9 import java.io.FileInputStream;
    10 import java.io.FileNotFoundException;
    11 import java.io.FileOutputStream;
    12 import java.io.IOException;
    13 import java.util.Enumeration;
    14 import java.util.HashMap;
    15 import java.util.Map;
    16 import java.util.Properties;
    17 
    18 /**
    19  * Class for storing/retrieving user settings, i.e. settings which can be set
    20  * manually by the user as opposed to the
    21  * {@link isac.util.usersettings.UserModel#}, which is an abstraction of the
    22  * system's expeience with a user and canot be altered manually. As there is no
    23  * fixed set of user settings, you can regard this class as a simple storage
    24  * backend for key/value pairs.
    25  * 
    26  * @author Alan Krempler
    27  *  
    28  */
    29 public class UserSettings extends Properties implements IUserSettings {
    30 
    31 	private HashMap storage_;
    32 
    33 	String userpath_;
    34 
    35 	String username_;
    36 
    37 	public static Map valstr_valint_;
    38 
    39 	/**
    40 	 * connect the String-representation of a UserSettings-value
    41 	 * with the respective Integer-representation
    42 	 * 
    43 	 * @see isac.uti.users.IUserSettings
    44 	 */
    45 	private HashMap createValstrValint() {
    46 		HashMap m = new HashMap();
    47 		m.put("DUMMY", new Integer(IUserSettings.DUMMY));
    48 		m.put("RESULT_WITH_LONG_CALCULATION", new Integer(
    49 				IUserSettings.RESULT_WITH_LONG_CALCULATION));
    50 		m.put("FORMULAE_ONLY", new Integer(IUserSettings.FORMULAE_ONLY));
    51 		m.put("SKIP_SPECIFY_START_SOLVE", new Integer(
    52 				IUserSettings.SKIP_SPECIFY_START_SOLVE));
    53 		m.put("FORMULA_TACTIC_FORMULA_", new Integer(
    54 				IUserSettings.FORMULA_TACTIC_FORMULA_));
    55 		m.put("POP_CALCHEAD_WITH_DESCRIPTIONS", new Integer(
    56 				IUserSettings.POP_CALCHEAD_WITH_DESCRIPTIONS));
    57 		//valstr_valint_.put("",
    58 		//		new Integer(IUserSettings.));
    59 		return m;
    60 	}
    61 
    62 	public UserSettings(String userpath, String username) {
    63 		userpath_ = userpath;
    64 		username_ = username;
    65 
    66 		//load Properties (keystr, valstr) temporarily
    67 		Properties keystr_valstr = new Properties();
    68 		String filename = userpath_ + username + "_settings.txt";
    69 		try {
    70 			FileInputStream propInFile = new FileInputStream(filename);
    71 			keystr_valstr.load(propInFile);
    72 			keystr_valstr.list(System.out);
    73 		} catch (FileNotFoundException e) {
    74 			System.err.println("Can?t find " + filename);
    75 		} catch (IOException e) {
    76 			System.err.println("I/O failed.");
    77 		}
    78 
    79 		//create HashMap (valstr, valint) temporarily
    80 		HashMap valstr_valint = new HashMap();
    81 		valstr_valint = this.createValstrValint();
    82 
    83 		//create HashMap (keystr, \\valstr\\ valint) and store it
    84 		storage_ = new HashMap();
    85 		Enumeration keystrs = keystr_valstr.propertyNames();//WN050311 how
    86 															// better ?
    87 		String keystr = null;
    88 		String valstr = null;
    89 		Object valint = null;
    90 		while (keystrs.hasMoreElements()) {
    91 			keystr = (String) keystrs.nextElement();
    92 			valstr = keystr_valstr.getProperty(keystr);
    93 			valint = valstr_valint.get(valstr);
    94 			storage_.put(keystr, valint);
    95 		}
    96 	}
    97 
    98 	/**
    99 	 * @param key
   100 	 *            A string key
   101 	 * @return The string value associated with the key parameter; null, if
   102 	 *         nothing is stored under this key
   103 	 */
   104 	public int getValue(String key) {
   105 		return ((Integer) storage_.get(key)).intValue();
   106 	}
   107 
   108 	/**
   109 	 * @param key
   110 	 *            The key under which "value" is to be stored. Existing data
   111 	 *            unde the same key will be replaced.
   112 	 * @param value
   113 	 *            The value to be stored under "key"
   114 	 * @return true in case of success, false in case of error, for example if
   115 	 *         trying to replace stored values without proper permissions
   116 	 */
   117 	public boolean setValue(String key, int value) {
   118 		//TODO.WN050311 storage_.put(key, value);
   119 		return true;
   120 	}
   121 
   122 	//WN050311 just for creation of the first file
   123 	//beware of the fix path !
   124 	public static void main(String[] args) {
   125 		UserSettings s = new UserSettings("isac/xmldata/users/", "x");
   126 		s.setProperty("start_worksheet", "SKIP_SPECIFY_START_SOLVE");
   127 		s.setProperty("enter_specify", "SKIP_SPECIFY_START_SOLVE");
   128 		s.setProperty("next_button", "FORMULAE_ONLY");
   129 
   130 		String filename = "isac/xmldata/users/x_settings.txt";
   131 		try {
   132 			FileOutputStream propOutFile = new FileOutputStream(filename);
   133 			s.store(propOutFile, "static dialog data of user 'x'");
   134 
   135 		} catch (IOException e) {
   136 			System.err.println("I/O failed.");
   137 		}
   138 	}
   139 
   140 }
   141