001: /*
002: * Created on Mar 17, 2005
003: */
004: package com.sun.portal.wireless.admin;
005:
006: import java.util.List;
007: import java.util.Map;
008: import java.util.HashMap;
009: import java.util.ArrayList;
010: import java.util.Iterator;
011:
012: import com.sun.portal.admin.common.PSMBeanException;
013: import com.sun.portal.admin.common.AttrOptionConstants;
014:
015: /**
016: * Attribute handler for predefinedReplies
017: *
018: * @author ashwin.mathew@sun.com
019: */
020: public class PredefinedRepliesMobileAppAttributeHandler extends
021: DefaultMobileAppAttributeHandler {
022:
023: // The values should be of the form "presetN=<value>".
024: private static final String PRESET = "preset";
025: private static final char EQUALS = '=';
026: private static final int INDEX_EQUALS = 7;
027: private static final int INDEX_NUMBER = 6;
028:
029: /**
030: * @param service
031: * @param attributeName
032: * @param userFriendlyName
033: * @param scope
034: * @param type
035: * @param privilege
036: */
037: protected PredefinedRepliesMobileAppAttributeHandler(
038: String service, String attributeName,
039: String userFriendlyName, int scope, int type,
040: int actualType, int privilege) {
041: super (service, attributeName, userFriendlyName, scope, type,
042: actualType, privilege);
043: }
044:
045: public boolean requiresOldValues() {
046: return true;
047: }
048:
049: /* (non-Javadoc)
050: * @see com.sun.portal.wireless.admin.MobileAppAttributeHandler#validate(java.util.List, java.util.Map)
051: */
052: public void validate(List values, Map optionsMap)
053: throws PSMBeanException {
054: if (values == null || optionsMap == null) {
055: // It's a get no validation required
056: return;
057: }
058:
059: Map valuesMap = validatePredefinedReplies(values);
060:
061: Map addValuesMap = null;
062: Map removeValuesMap = null;
063:
064: List addValues = (List) optionsMap
065: .get(AttrOptionConstants.OPT_ADD);
066: if (addValues != null) {
067: addValuesMap = validatePredefinedReplies(addValues);
068: }
069:
070: List removeValues = (List) optionsMap
071: .get(AttrOptionConstants.OPT_REMOVE);
072: if (removeValues != null) {
073: removeValuesMap = validatePredefinedReplies(removeValues);
074: }
075:
076: List outputList = null;
077:
078: List oldValues = (List) optionsMap
079: .get(AbstractMobileGetSetAttributeHandler.KEY_OLD_VALUES);
080: // No need to do this if the complete value list is being replaced by values
081: if (oldValues != null
082: && (addValues != null || removeValues != null)) {
083: outputList = new ArrayList();
084:
085: // Copy oldValues to a HashMap to make processing easier
086: Map oldValuesMap = new HashMap();
087: for (int i = 0; i < oldValues.size(); i++) {
088: String value = (String) oldValues.get(i);
089: String key = value.substring(0, INDEX_EQUALS);
090: String predefinedReply = value
091: .substring(INDEX_EQUALS + 1);
092: oldValuesMap.put(key, predefinedReply);
093: }
094:
095: // Replace keys in oldValues to empty for keys in removeValues
096: if (removeValuesMap != null) {
097: Iterator removeKeys = removeValuesMap.keySet()
098: .iterator();
099: while (removeKeys.hasNext()) {
100: String key = (String) removeKeys.next();
101: oldValuesMap.put(key, null);
102: }
103: }
104:
105: // Replace keys in oldValues with those from addValues
106: if (addValuesMap != null) {
107: oldValuesMap.putAll(addValuesMap);
108: }
109:
110: // Remove addValues and removeValues from optionsMap
111: optionsMap.remove(AttrOptionConstants.OPT_ADD);
112: optionsMap.remove(AttrOptionConstants.OPT_REMOVE);
113:
114: outputList = getOutputList(oldValuesMap);
115: } else {
116: outputList = getOutputList(valuesMap);
117: }
118:
119: optionsMap
120: .put(
121: AbstractMobileGetSetAttributeHandler.KEY_OVERRIDE_VALUES,
122: outputList);
123: }
124:
125: private List getOutputList(Map valuesMap) {
126: List outputList = new ArrayList();
127:
128: // Array to make sure that all presetN entries are present
129: boolean[] isPresent = new boolean[10];
130:
131: Iterator keys = valuesMap.keySet().iterator();
132: while (keys.hasNext()) {
133: String key = (String) keys.next();
134: String predefinedReply = (String) valuesMap.get(key);
135: if (predefinedReply == null) {
136: predefinedReply = "";
137: }
138:
139: String element = key + EQUALS + predefinedReply;
140:
141: outputList.add(element);
142:
143: int presetNum = Integer.parseInt(key
144: .substring(INDEX_NUMBER));
145: isPresent[presetNum] = true;
146: }
147:
148: // Values are from 1 to 9
149: for (int i = 1; i < isPresent.length; i++) {
150: if (!isPresent[i]) {
151: outputList.add(PRESET + i + EQUALS);
152: }
153: }
154:
155: return outputList;
156: }
157:
158: private Map validatePredefinedReplies(List values)
159: throws PSMBeanException {
160: HashMap valuesMap = new HashMap();
161:
162: for (int i = 0; i < values.size(); i++) {
163: String value = (String) values.get(i);
164:
165: if (!value.startsWith(PRESET)) {
166: throw new PSMBeanException(
167: "error.psadmin.predefinedReplies.format.incorrect");
168: }
169:
170: try {
171: Integer.parseInt(value.substring(INDEX_NUMBER,
172: INDEX_NUMBER + 1));
173: } catch (NumberFormatException e) {
174: throw new PSMBeanException(
175: "error.psadmin.predefinedReplies.format.incorrect");
176: }
177:
178: if (value.length() >= INDEX_EQUALS + 1
179: && !(value.charAt(INDEX_EQUALS) == EQUALS)) {
180: throw new PSMBeanException(
181: "error.psadmin.predefinedReplies.format.incorrect");
182: }
183:
184: String key = value.substring(0, INDEX_EQUALS);
185: String predefinedReply = null;
186: if (value.length() >= INDEX_EQUALS + 1) {
187: predefinedReply = value.substring(INDEX_EQUALS + 1);
188: }
189: valuesMap.put(key, predefinedReply);
190: }
191:
192: return valuesMap;
193: }
194:
195: }
|