001: /**
002: * Copyright 2002 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: *
013: * @ Author Bhavanishankar
014: */package com.sun.portal.netlet.admin.model;
015:
016: // JDK classes
017:
018: import com.iplanet.am.console.base.model.AMAttrSchemaComparator;
019: import com.sun.portal.log.common.PortalLogger;
020: import com.iplanet.am.console.base.model.AMConsoleException;
021: import com.iplanet.am.console.base.model.AMDisplayTypeConverter;
022: import com.iplanet.am.console.base.model.AMResBundleCacher;
023: import com.iplanet.am.console.user.model.UMUserProfileModelImpl;
024: import com.iplanet.am.sdk.AMException;
025: import com.iplanet.am.sdk.AMStoreConnection;
026: import com.iplanet.sso.SSOException;
027: import com.iplanet.sso.SSOToken;
028: import com.iplanet.sso.SSOTokenManager;
029: import com.sun.identity.sm.AttributeSchema;
030: import com.sun.identity.sm.SchemaType;
031: import com.sun.identity.sm.ServiceSchema;
032: import com.sun.identity.sm.ServiceSchemaManager;
033: import com.sun.portal.netlet.admin.NetletAdminModelManager;
034:
035: import javax.servlet.http.HttpServletRequest;
036: import java.text.Collator;
037: import java.util.*;
038:
039: public class NetletAdminUserProfileModelImpl extends
040: UMUserProfileModelImpl {
041:
042: private HttpServletRequest req = null;
043: private SSOToken ssoToken = null;
044: private ServiceSchemaManager schemaMgr = null;
045: private ServiceSchema schema = null;
046: private Set dynAttrNames = null;
047: private List attrNames = null;
048: private Map netletUserAttrs = null;
049: private int currentRow = -1;
050: public static final String ANY_OPTION_SEPARATOR = "|";
051:
052: public NetletAdminUserProfileModelImpl(HttpServletRequest req,
053: Map map) {
054: super (req, map);
055: //super.initModel();
056: resBundle = AMResBundleCacher.getBundle("srapnetletadminmsgs",
057: getUserLocale());
058: this .req = req;
059: }
060:
061: public void initModel(String userDN) {
062: //super.initModel();
063: this .userDN = userDN;
064: if (userDN == null || userDN.trim().length() == 0) {
065: return;
066: }
067: try {
068: SSOTokenManager ssoTokenManager = SSOTokenManager
069: .getInstance();
070: ssoToken = ssoTokenManager.createSSOToken(req);
071:
072: AMStoreConnection amConn = new AMStoreConnection(ssoToken);
073: curUser = amConn.getUser(userDN);
074:
075: schemaMgr = new ServiceSchemaManager(ssoToken,
076: "srapNetletService", "1.0");
077: schema = schemaMgr.getSchema(SchemaType.USER);
078: attrNames = getUserAttrNames();
079: // Get the dynamic attribute names
080: ServiceSchema dynSchema = schemaMgr
081: .getSchema(SchemaType.DYNAMIC);
082: dynAttrNames = dynSchema.getAttributeSchemaNames();
083: int size = (attrNames == null) ? 0 : attrNames.size();
084: if (size > 0) {
085: netletUserAttrs = new HashMap();
086: Iterator iter = attrNames.iterator();
087: while (iter.hasNext()) {
088: String attrName = (String) iter.next();
089: Set attrValue = curUser.getAttribute(attrName);
090: netletUserAttrs.put(attrName, attrValue);
091: }
092: setAttributeValues(curUser.getAttributes());
093: }
094: } catch (Exception ex) {
095: NetletAdminModelManager
096: .debugMessage(
097: "NetletAdminUserProfileModelImpl - Unable to initialize model : ",
098: ex);
099: }
100: }
101:
102: public int getSize() {
103: return attrNames == null ? 0 : attrNames.size();
104: }
105:
106: public boolean setCurrentRow(int currRow) {
107: if (netletUserAttrs == null) {
108: return false;
109: }
110: currentRow = currRow;
111: return currRow >= netletUserAttrs.size() ? false : true;
112: }
113:
114: public String getAttrName() {
115: return attrNames == null ? null : (String) attrNames
116: .get(currentRow);
117: }
118:
119: public String getAttrLabel(String attrName) {
120: if (schema == null || attrName == null
121: || attrName.trim().length() == 0)
122: return attrName;
123: AttributeSchema attrSchema = schema
124: .getAttributeSchema(attrName);
125: String i18nKey = attrSchema.getI18NKey();
126: /*ServiceSchemaManager schemaMgr = null;
127: try {
128: schemaMgr = new ServiceSchemaManager("srapNetletService", ssoToken);
129: } catch(Exception ex) {
130: NetletAdminModelManager.debugError("Unable to create service manager.", ex);
131: }*/
132: String l10name = i18nKey;
133: if (schemaMgr != null)
134: l10name = getL10NAttributeName(schemaMgr, i18nKey);
135:
136: return l10name;
137: }
138:
139: public String getAttrLabel() {
140: return getAttrLabel(getAttrName());
141: }
142:
143: public int getAttrType() {
144: if (schema == null)
145: return AMDisplayTypeConverter.DEFAULT_TYPE;
146: String attrLabel = getAttrName();
147: AttributeSchema attrSchema = schema
148: .getAttributeSchema(attrLabel);
149: int type = AMDisplayTypeConverter.getDisplayType(attrSchema);
150: return type;
151: }
152:
153: public int getAttrSyntax() {
154: int syntax = AMDisplayTypeConverter.DEFAULT_SYNTAX;
155: if (schema == null)
156: return syntax;
157: String attrLabel = getAttrName();
158: AttributeSchema attrSchema = schema
159: .getAttributeSchema(attrLabel);
160: syntax = AMDisplayTypeConverter.getDisplaySyntax(attrSchema);
161: //syntax = attrSchema.getSyntax();
162: return syntax;
163: }
164:
165: /*
166: * Return true if the current attribute is read-only by the current user
167: */
168: public boolean isReadOnly(String attrName) {
169: AttributeSchema attrSchema = schema
170: .getAttributeSchema(attrName);
171: String any = attrSchema.getAny();
172: Set displayOptions = getDisplayOptions(any);
173: if (displayOptions.isEmpty()
174: || (displayOptions.contains(DISPLAY_ATTRIBUTE))) {
175: return false;
176: } else if (isAdministrator()
177: && displayOptions
178: .contains(ADMINISTRATOR_DISPLAY_ATTRIBUTE)) {
179: return false;
180: }
181: return true;
182: }
183:
184: public boolean isReadOnly() {
185: return isReadOnly(getAttrName());
186: }
187:
188: /*
189: * Returns the attribute value by fetching from Schema
190: */
191: public Set getAttributeValue(String attrName) {
192: if (curUser == null)
193: return null;
194: Set attrValue = null;
195: try {
196: attrValue = curUser.getAttribute(attrName);
197: } catch (Exception ex) {
198: NetletAdminModelManager
199: .debugError("NetletAdminUserProfileModelImpl: Unable to get value of "
200: + attrName + ex);
201: }
202: return attrValue;
203: }
204:
205: /*
206: * Returns the value of the Attribute if it is displayable, null otherwise
207: */
208: public Set getAttrValues(String attrName) {
209: return netletUserAttrs == null ? null : (Set) netletUserAttrs
210: .get(attrName);
211: }
212:
213: /*
214: * Returns the value of the current attribute in the row.
215: * @ Returns null if the attribute is not displayable.
216: */
217: public Set getAttrValues() {
218: return getAttrValues(getAttrName());
219: }
220:
221: /*
222: * Returns the string value of the attribute by reading from the schema,
223: */
224: public String getAttrStringValue(String attrName,
225: String defaultValue) {
226: Set set = getAttributeValue(attrName);
227: if (null == set) {
228: return defaultValue;
229: }
230: Iterator it = set.iterator();
231: return (it.hasNext()) ? (String) it.next() : defaultValue;
232: }
233:
234: public String getAttrTrueValue() {
235: String trueValue = "true";
236: if (schema == null)
237: return trueValue;
238: String attrLabel = getAttrName();
239: AttributeSchema attrSchema = schema
240: .getAttributeSchema(attrLabel);
241: trueValue = attrSchema.getTrueValue();
242: return trueValue;
243: // return getTrueValue("srapNetletService", 0, getAttrName());
244: }
245:
246: public String getAttrFalseValue() {
247: String falseValue = "false";
248: if (schema == null)
249: return falseValue;
250: String attrLabel = getAttrName();
251: AttributeSchema attrSchema = schema
252: .getAttributeSchema(attrLabel);
253: falseValue = attrSchema.getFalseValue();
254: return falseValue;
255: }
256:
257: public String[] getAttrChoices() {
258: if (schema == null)
259: return null;
260: String attrLabel = getAttrName();
261: AttributeSchema attrSchema = schema
262: .getAttributeSchema(attrLabel);
263: String[] values = attrSchema.getChoiceValues();
264: return values;
265: }
266:
267: public Set getNetletRules() {
268: return netletUserAttrs == null ? null : (Set) netletUserAttrs
269: .get("sunPortalNetletRules");
270: }
271:
272: public int getNetletRulesIndex() {
273: return attrNames == null ? -1 : attrNames
274: .indexOf("sunPortalNetletRules");
275: }
276:
277: public Set getNetletCipherList() {
278: return netletUserAttrs == null ? null : (Set) netletUserAttrs
279: .get("sunPortalNetletCipherList");
280: }
281:
282: public int getNetletCipherListIndex() {
283: return attrNames == null ? -1 : attrNames
284: .indexOf("sunPortalNetletCipherList");
285: }
286:
287: public int getNetletRulesCount() {
288: Set s = getNetletRules();
289: return s == null ? 0 : s.size();
290: }
291:
292: public String getServiceDescription() {
293: return getLocalizedSvcName("srapNetletService");
294: }
295:
296: public boolean store(Map newMap) {
297: if (curUser == null)
298: return false;
299: HashMap attrMap = new HashMap(1);
300: Set attrNames = newMap.keySet();
301: Iterator iter = attrNames.iterator();
302: String attrName = null;
303: while (iter.hasNext()) {
304: try {
305: attrName = (String) iter.next();
306: Set newValue = (Set) newMap.get(attrName);
307: attrMap.clear();
308: attrMap.put(attrName, newValue);
309: curUser.setAttributes(attrMap);
310: curUser.store();
311: } catch (Exception ex) {
312: NetletAdminModelManager.debugError(ex + " - "
313: + attrName);
314: continue;
315: }
316: }
317: return true;
318: }
319:
320: /**
321: * Stroes the attribute map for the current user as user attrs.
322: *
323: * @param attrMap The map of name-(Set)values for attrs.
324: */
325: public void storeAttributes(Map userOnlyAttrs, Map customizedAttrs,
326: Set inheritedAttrs) throws AMConsoleException {
327: List errorList = null;
328: /**
329: * Write the attributes without a choice menu. These values will
330: * overwrite any existing value, if and only if the value is different.
331: */
332: Map attrs = new HashMap(userOnlyAttrs.size());
333: Set namesSet = userOnlyAttrs.keySet();
334: Iterator iter = namesSet.iterator();
335: while (iter.hasNext()) {
336: String attrName = (String) iter.next();
337: ;
338: //if (!isAttrReadOnly("srapNetletService", SCHEMA_TYPE_USER, attrName))
339: //if (!isAttrReadOnly(SCHEMA_TYPE_USER, attrName)) {
340: Set attrValues = (Set) userOnlyAttrs.get(attrName);
341: if (attrValues != null) {
342: attrs.put(attrName, attrValues);
343: }
344: //}
345: }
346: try {
347: if (!attrs.isEmpty()) {
348: writeProfile(curUser, attrs, false);
349: }
350: } catch (AMConsoleException ace) {
351: errorList = ace.getErrors();
352: } catch (SSOException ssoe) {
353: NetletAdminModelManager
354: .debugError("NetletAdminUserProfileModelImpl: Invalid SSOToken -> "
355: + ssoe);
356: return;
357: }
358:
359: /**
360: * Write the attributes with a choice menu that are customized.
361: * These values will overwrite any existing value even of it is same.
362: */
363: attrs = new HashMap(customizedAttrs.size());
364: namesSet = customizedAttrs.keySet();
365: iter = namesSet.iterator();
366: while (iter.hasNext()) {
367: String attrName = (String) iter.next();
368: ;
369: //if (!isAttrReadOnly("srapNetletService", SCHEMA_TYPE_USER, attrName))
370: //if (!isAttrReadOnly(SCHEMA_TYPE_USER, attrName)) {
371: Set attrValues = (Set) customizedAttrs.get(attrName);
372: if (attrValues != null) {
373: attrs.put(attrName, attrValues);
374: }
375: //}
376: }
377: try {
378: if (!attrs.isEmpty()) {
379: writeProfile(curUser, attrs, true);
380: }
381: } catch (AMConsoleException ace) {
382: errorList = ace.getErrors();
383: } catch (SSOException ssoe) {
384: NetletAdminModelManager
385: .debugError("NetletAdminUserProfileModelImpl: Invalid SSOToken -> "
386: + ssoe);
387: return;
388: }
389:
390: /**
391: * Remove the inherited attributes from the user entry. An exception
392: * will be thrown if the attribute doesn't exist in the user entry.
393: */
394: if (!inheritedAttrs.isEmpty()) {
395: if (errorList == null) {
396: errorList = new ArrayList(inheritedAttrs.size());
397: }
398:
399: Set attr = new HashSet(1);
400: iter = inheritedAttrs.iterator();
401: while (iter.hasNext()) {
402: String name = (String) iter.next();
403: attr.add(name);
404: try {
405: curUser.removeAttributes(attr);
406: logger.doLog("attribute.remove", name);
407: } catch (AMException ame) {
408: if (debug.warningEnabled()) {
409: debug.warning("error removing attribute "
410: + name, ame);
411: }
412: errorList.add(name + "-" + ame.getMessage());
413: } catch (SSOException ssoe) {
414: NetletAdminModelManager
415: .debugError("NetletAdminUserProfileModelImpl: Invalid SSOToken -> "
416: + ssoe);
417: return;
418: } finally {
419: attr.clear();
420: }
421: }
422: }
423:
424: if (errorList != null && !errorList.isEmpty()) {
425: throw new AMConsoleException(errorList);
426: }
427: }
428:
429: /*
430: * Returns the List of attribute names (String) which should be displayed in the Admin console
431: *
432: */
433:
434: private List getUserAttrNames() {
435: List attrNames = getAttrSchema(SchemaType.USER);
436: List displayableAttrNames = getDisplayableAttrNames(attrNames);
437: return displayableAttrNames;
438: }
439:
440: /*
441: * Return the list of attribute schemas (AMAttributeSchema)
442: */
443:
444: private List getAttrSchema(SchemaType schemaType) {
445: List sortedAttrNames = null;
446: Set attrNames = schema.getAttributeSchemas();
447: sortedAttrNames = sortAttrSchema(attrNames);
448: return sortedAttrNames;
449: }
450:
451: /*
452: * Return the list of attribute names which should be displayed in the admin console
453: */
454:
455: public List getDisplayableAttrNames(List attrNames) {
456: List newAttrNames = new LinkedList();
457: Iterator iter = attrNames.iterator();
458: while (iter.hasNext()) {
459: AttributeSchema attrSchema = (AttributeSchema) iter.next();
460: String attrName = attrSchema.getName();
461: String i18nKey = attrSchema.getI18NKey();
462: if (i18nKey == null || i18nKey.trim().length() == 0) {
463: continue;
464: }
465: String any = attrSchema.getAny();
466: Set displayOptions = getDisplayOptions(any);
467: if (displayOptions.isEmpty()
468: || (displayOptions.contains(DISPLAY_ATTRIBUTE)
469: || displayOptions
470: .contains(READONLY_ATTRIBUTE) || displayOptions
471: .contains(USER_READ_ONLY_ATTRIBUTE))
472: || (isAdministrator() && displayOptions
473: .contains(ADMINISTRATOR_DISPLAY_ATTRIBUTE))) {
474: newAttrNames.add(attrName);
475: }
476: }
477: return newAttrNames;
478: }
479:
480: /*
481: * Parse the "any" string, Get the display options
482: */
483:
484: private Set getDisplayOptions(String anyOptions) {
485: if (anyOptions == null || anyOptions.length() == 0) {
486: return Collections.EMPTY_SET;
487: }
488: StringTokenizer optionTokenizer = new StringTokenizer(
489: anyOptions, ANY_OPTION_SEPARATOR);
490: Set options = new HashSet(optionTokenizer.countTokens());
491: while (optionTokenizer.hasMoreTokens()) {
492: options.add((String) optionTokenizer.nextToken());
493: }
494: return options;
495: }
496:
497: /*
498: * Sort the attribute names in the order it has to be displayed in the admin console
499: */
500:
501: private List sortAttrSchema(Set unordered) {
502: Collator collator = Collator.getInstance(getUserLocale());
503: AMAttrSchemaComparator c = new AMAttrSchemaComparator(collator);
504: List ordered = new ArrayList(unordered);
505: Collections.sort(ordered, c);
506: return ordered;
507: }
508:
509: public String getUserID() {
510: if (userDN != null && userDN.trim().length() != 0) {
511: try {
512: return userDN.substring(userDN.indexOf('=') + 1, userDN
513: .indexOf(','));
514: } catch (Exception ex) {
515: NetletAdminModelManager
516: .debugError("NetletAdminUserProfileModelImpl: Unable to get userID -> "
517: + ex);
518: }
519: }
520: return null;
521: }
522:
523: public String getAttributeStatus(String attrName) {
524: if (dynAttrNames.contains(attrName)) {
525: return getSkipValue();
526: }
527: return null;
528: }
529: }
|