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.List;
016: import java.util.ArrayList;
017: import java.util.StringTokenizer;
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 ABModifyTag class executes the address book add command.
026: *
027: * Attributes needed: name, entryid
028: *
029: * @author Mihir Sambhus
030: * @version 1.0
031: */
032: public class ABModifyTag extends ABBaseCommandTag {
033:
034: // Create a logger for this class
035: private static Logger debugLogger = PortalLogger
036: .getLogger(ABModifyTag.class);
037:
038: /**
039: * Execute the Modify Element from the address book command.
040: * Returns the command status.
041: * If the command fails then the tag body is included,
042: * Else if the command succeeds then the tag body is skipped.
043: *
044: * @return true if the command succeeds.
045: * false if the command fails.
046: * @exception JspException
047: */
048: public boolean execute() throws JspException {
049:
050: // entryid attribute must be present for delete .. if not jsp
051: // exception is throw
052: verifyEntryid();
053:
054: // Get the new element bean that the user entered via <entry> tag to
055: // replace the previous
056: Element elementNew = getElement();
057:
058: if (!requiredPropertiesExist(elementNew)) {
059: return false;
060: }
061:
062: // Get the element object corresponsing to the entryid, from context
063: Element elementOld = getEntry(Integer.parseInt(entryid));
064:
065: // Get the contactDB object interacting with the Backend LDAP server
066: ContactDB contactDB = getContactDB();
067:
068: // perform modify command
069: if (!contactDB.modifyElementFromABook(elementOld, elementNew)) {
070: debugLogger.fine("PSMA_CSPWTA0016");
071: getABContext().setErrorCode("");
072: return false;
073: }
074:
075: String addmembers = null;
076: String members = null;
077: // Get the group members if the element type is group
078: if (elementNew.getElementType() == Element.GROUP) {
079: members = (String) elementNew.getProperty("members");
080: addmembers = (String) elementNew.getProperty("addmembers");
081: }
082:
083: // Either addmembers or members is present simultaneously .. If addmembers
084: // is present members list is ignored
085: if ((addmembers != null) && (!addmembers.equals(""))) {
086: // Add all entries individually to the group using addGroupMember
087: StringTokenizer tok = new StringTokenizer(addmembers, ",");
088: while (tok.hasMoreTokens()) {
089: Element elemToAdd = getABContext().getMemberchoice(
090: Integer.parseInt(tok.nextToken()));
091: contactDB.addElementToGroup(elemToAdd,
092: (Group) elementOld);
093: }
094: } else if ((members != null) && (!members.equals(""))) {
095: // Modify members list
096: // Figure out which entries are changed from the groupmembers collection
097: // and delete members in the new list, and add new members found in
098: // the new list
099:
100: // Find out the intersection between the current groupmembers and the
101: // new members.. and separate it into delList and addList
102: ArrayList tmp = (ArrayList) getABContext()
103: .getGroupmembers();
104: List delList = (ArrayList) tmp.clone();
105: List addList = new ArrayList();
106: StringTokenizer tok = new StringTokenizer(members, ",");
107: while (tok.hasMoreTokens()) {
108: boolean found = false;
109: Element elemToChange = getABContext().getMemberchoice(
110: Integer.parseInt(tok.nextToken()));
111: // If element being changed is same as the current group being
112: // changed .. then empty the delete list and dont add the element
113: // being changed to the add list either
114: if (elemToChange.getUn().equals(elementOld.getUn())) {
115: found = true;
116: delList.removeAll(delList);
117: }
118: for (int i = 0; i < delList.size(); i++) {
119: Element e = (Element) delList.get(i);
120: if (elemToChange.getUn().equals(e.getUn())) {
121: // common element .. remove from the delList
122: delList.remove(i);
123: found = true;
124: break;
125: }
126: }
127: if (!found) {
128: // elemToChange is not found in the delList .. add it to the addList
129: addList.add(elemToChange);
130: }
131: }
132:
133: // Add all the elements from add list
134: for (int i = 0; i < addList.size(); i++) {
135: contactDB.addElementToGroup((Element) addList.get(i),
136: (Group) elementOld);
137: }
138: // Delete all elements from delete list
139: for (int i = 0; i < delList.size(); i++) {
140: contactDB.deleteElementFromGroup((Element) delList
141: .get(i), (Group) elementOld);
142: }
143: }
144: return true;
145: }
146:
147: }
|