001: /*
002: * %W% %G% Sun Microsystems
003: *
004: * Copyright 2000 Sun Microsystems, Inc. All rights reserved.
005: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
006: */
007:
008: package com.sun.portal.wireless.taglibs.ab;
009:
010: import javax.servlet.jsp.*;
011: import javax.servlet.jsp.tagext.*;
012: import com.sun.portal.wireless.taglibs.base.*;
013: import com.sun.portal.log.common.PortalLogger;
014:
015: import java.util.StringTokenizer;
016: import java.util.List;
017: import java.util.logging.Level;
018: import java.util.logging.Logger;
019:
020: import com.sun.addressbook.Entry;
021: import com.sun.addressbook.Group;
022: import com.sun.addressbook.Element;
023:
024: /**
025: * The ABAddTag class executes the address book add command.
026: *
027: * Attributes needed: name
028: *
029: * @author Mihir Sambhus
030: * @version 1.0
031: */
032: public class ABAddTag extends ABBaseCommandTag {
033:
034: /**
035: * Execute the Add Element command on the Address Book.
036: * Returns the command status.
037: * If the command fails then the tag body is included,
038: * Else if the command succeeds then the tag body is skipped.
039: *
040: * @return true if the command succeeds.
041: * false if the command fails.
042: * @exception JspException
043: */
044:
045: // Create a logger for this class
046: private static Logger debugLogger = PortalLogger
047: .getLogger(ABAddTag.class);
048:
049: public boolean execute() throws JspException {
050:
051: // Get the new element bean that the user entered via <entry> tag to add
052: Element element = getElement();
053:
054: // The Entry bean has to have Last name property filled
055: if (!requiredPropertiesExist(element)) {
056: return false;
057: }
058:
059: // Get the contactDB object interacting with the Backend LDAP server
060: ContactDB contactDB = getContactDB();
061:
062: // perform the add command
063: // If group, do the uniqueness check based on the name of the group
064: if (element.getElementType() == Element.GROUP) {
065: Entry searchEntry = new Entry();
066: searchEntry.setCn(element.getCn());
067: searchEntry.setElementType(Element.GROUP);
068: // Workaround for quirk in Lotus Notes .. doesnt refresh without folder
069: // refresh in same session
070: contactDB.refreshAB();
071: List elements = contactDB.searchABook(searchEntry, null,
072: null, null, null, true);
073:
074: if ((elements == null) || (elements.size() > 0)) {
075: debugLogger.fine("PSMA_CSPWTA0004");
076: getABContext().setErrorCode("AB_003"); // AB_003 = Group name not uniq
077: return false;
078: }
079: }
080:
081: if (!contactDB.addElementToABook(element)) {
082: debugLogger.fine("PSMA_CSPWTA0005");
083: getABContext().setErrorCode("");
084: return false;
085: }
086:
087: String members = null;
088: // Get the group members if the element type is group
089: if (element.getElementType() == Element.GROUP) {
090: members = (String) element.getProperty("members");
091: if (members == null) {
092: members = (String) element.getProperty("addmembers");
093: }
094: }
095: if (members != null) {
096: // Search the address book to get the un for the group that just added.
097: Entry searchEntry = new Entry();
098: searchEntry.setCn(element.getCn());
099: searchEntry.setElementType(Element.GROUP);
100: // Workaround for quirk in Lotus Notes .. doesnt refresh without folder
101: // refresh
102: contactDB.refreshAB();
103: List elements = contactDB.searchABook(searchEntry, null,
104: null, null, null, true);
105:
106: if ((elements == null) || (elements.size() != 1)) {
107: debugLogger.fine("PSMA_CSPWTA0005");
108: getABContext().setErrorCode("");
109: return false;
110: }
111: Group group = (Group) elements.get(0);
112: if (group.getElementType() != Element.GROUP) {
113: debugLogger.fine("PSMA_CSPWTA0005");
114: getABContext().setErrorCode("");
115: return false;
116: }
117:
118: // Add all entries individually to the group using addGroupMember
119: StringTokenizer tok = new StringTokenizer(members, ",");
120: while (tok.hasMoreTokens()) {
121: Element elemToAdd = getABContext().getMemberchoice(
122: Integer.parseInt(tok.nextToken()));
123: contactDB.addElementToGroup(elemToAdd, group);
124: }
125: }
126: return true;
127: }
128:
129: }
|