001: package com.sun.portal.wireless.admin;
002:
003: import java.util.Map;
004: import java.util.List;
005: import java.util.ArrayList;
006: import java.math.BigInteger;
007:
008: import com.sun.portal.admin.common.AttrOptionConstants;
009: import com.sun.portal.admin.common.AttributeInfo;
010: import com.sun.portal.admin.common.PSMBeanException;
011:
012: /**
013: * Default attribute handler.
014: *
015: * @author ashwin.mathew@sun.com
016: */
017: public class DefaultMobileAppAttributeHandler implements
018: MobileAppAttributeHandler {
019:
020: private String service;
021:
022: private String attributeName;
023:
024: private String userFriendlyName;
025:
026: private int scope;
027:
028: private int type;
029:
030: // Type is the type declared in the schema.
031: // Actual type is type we have to validate for.
032: private int actualType;
033:
034: private int privilege;
035:
036: private static final String TRUE_VALUE = "true";
037: private static final String FALSE_VALUE = "false";
038:
039: private static final BigInteger MAX_INTEGER_VAL = new BigInteger(
040: String.valueOf(Integer.MAX_VALUE));
041:
042: protected DefaultMobileAppAttributeHandler(String service,
043: String attributeName, String userFriendlyName, int scope,
044: int type, int actualType, int privilege) {
045: this .service = service;
046: this .attributeName = attributeName;
047: this .userFriendlyName = userFriendlyName;
048: this .scope = scope;
049: this .type = type;
050: this .actualType = actualType;
051: this .privilege = privilege;
052: }
053:
054: public String getService() {
055: return service;
056: }
057:
058: public String getAttributeName() {
059: return attributeName;
060: }
061:
062: public String getUserFriendlyName() {
063: return userFriendlyName;
064: }
065:
066: public int getScope() {
067: return scope;
068: }
069:
070: public int getPrivilege() {
071: return privilege;
072: }
073:
074: public int getType() {
075: return type;
076: }
077:
078: // We need this to perform validations properly,
079: // since many MA types are not indicative of the
080: // types actually stored.
081: public int getActualType() {
082: return actualType;
083: }
084:
085: public boolean requiresOldValues() {
086: return false;
087: }
088:
089: // Default implementation, subclasses may override.
090: public void validate(List values, Map optionsMap)
091: throws PSMBeanException {
092: boolean isGlobal = Boolean
093: .valueOf(
094: (String) optionsMap
095: .get(AttrOptionConstants.OPT_GLOBAL))
096: .booleanValue();
097:
098: boolean isOrg = Boolean.valueOf(
099: (String) optionsMap.get(AttrOptionConstants.OPT_ORG))
100: .booleanValue();
101:
102: String dn = (String) optionsMap.get(AttrOptionConstants.OPT_DN);
103:
104: switch (scope) {
105: case MobileAppAttributeHandler.SCOPE_GLOBAL: {
106: if (dn != null) {
107: throw new PSMBeanException(
108: "error.psadmin.attribute.not.dn.specific");
109: }
110:
111: if (isOrg) {
112: throw new PSMBeanException(
113: "error.psadmin.attribute.not.organization");
114: }
115:
116: break;
117: }
118: case MobileAppAttributeHandler.SCOPE_ORGANIZATION: {
119: if (isGlobal) {
120: throw new PSMBeanException(
121: "error.psadmin.attribute.not.global");
122: }
123:
124: if (dn == null) {
125: throw new PSMBeanException(
126: "error.psadmin.dn.is.required");
127: }
128: break;
129: }
130: case MobileAppAttributeHandler.SCOPE_USER: {
131: if (isGlobal) {
132: throw new PSMBeanException(
133: "error.psadmin.attribute.not.global");
134: }
135:
136: if (isOrg) {
137: throw new PSMBeanException(
138: "error.psadmin.attribute.not.organization");
139: }
140:
141: if (dn == null) {
142: throw new PSMBeanException(
143: "error.psadmin.dn.is.required");
144: }
145: break;
146: }
147: }
148:
149: if (values != null
150: && getActualType() != AttributeInfo.LIST_NUMERIC
151: && getActualType() != AttributeInfo.LIST_STRING
152: && getActualType() != AttributeInfo.MULTIPLE_CHOICE_NUMERIC
153: && getActualType() != AttributeInfo.MULTIPLE_CHOICE_STRING
154: && getActualType() != AttributeInfo.UNKOWN_TYPE) {
155: validateSingleValueAttribute(values, optionsMap,
156: getActualType());
157: }
158: }
159:
160: /**
161: * This method does following validations
162: * 1) add/remove options are not allowed for single valued attribute
163: * 2) boolean attribute can have only true/fase as their values
164: * 3) numeric attribute can have only numeric value
165: * @param values
166: * @param optionsMap
167: * @param attributeType
168: * @throws PSMBeanException
169: */
170: private void validateSingleValueAttribute(List values,
171: Map optionsMap, int attributeType) throws PSMBeanException {
172: List addValues = (List) optionsMap
173: .get(AttrOptionConstants.OPT_ADD);
174: List removeValues = (List) optionsMap
175: .get(AttrOptionConstants.OPT_REMOVE);
176:
177: if (addValues != null || removeValues != null
178: || values.size() != 1) {
179: throw new PSMBeanException(
180: "error.psadmin.attribute.is.single.valued");
181: }
182:
183: String valueStr = ((String) values.get(0)).trim();
184:
185: switch (attributeType) {
186: case AttributeInfo.SINGLE_BOOLEAN: {
187: if (!(valueStr.equalsIgnoreCase(TRUE_VALUE) || valueStr
188: .equalsIgnoreCase(FALSE_VALUE))) {
189: throw new PSMBeanException(
190: "error.psadmin.attribute.is.boolean");
191: }
192: break;
193: }
194: case AttributeInfo.SINGLE_NUMERIC: {
195: BigInteger valueBigInt = new BigInteger(valueStr);
196: if (valueBigInt.compareTo(MAX_INTEGER_VAL) > 0) {
197: throw new PSMBeanException(
198: "error.psadmin.attribute.number.too.large");
199: } else if (valueBigInt.compareTo(BigInteger.ZERO) <= 0) {
200: throw new PSMBeanException(
201: "error.psadmin.attribute.number.too.small");
202: }
203:
204: try {
205: int valueInt = Integer.parseInt(valueStr);
206:
207: // This will take care of any leading 0's
208: values.set(0, String.valueOf(valueInt));
209: } catch (NumberFormatException e) {
210: throw new PSMBeanException(
211: "error.psadmin.attribute.is.number");
212: }
213: break;
214: }
215: }
216: }
217:
218: // Default implementation, subclasses may override.
219: public List processGetValue(List values) throws PSMBeanException {
220: return values;
221: }
222:
223: // Default implementation, subclasses may override.
224: public List processSetValue(List values) throws PSMBeanException {
225: return values;
226: }
227:
228: public final List getAttributeInfoList() {
229: ArrayList infoList = new ArrayList();
230:
231: // Not setting these for now, may support later
232: String desc = null;
233: String exampleKey = null;
234:
235: // Note the use of actualType rather than type.
236: // If type were to be used, users would get confused
237: // by the validations, which function based on actualType.
238: infoList.add(new Integer(getActualType()));
239: infoList.add(desc);
240: infoList.add(new Integer(getPrivilege()));
241: infoList.add(exampleKey);
242:
243: return infoList;
244: }
245:
246: }
|