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:
013: import com.sun.portal.wireless.taglibs.base.*;
014: import com.sun.portal.log.common.PortalLogger;
015: import com.sun.addressbook.Element;
016: import com.sun.addressbook.Entry;
017: import com.sun.addressbook.Group;
018:
019: import java.util.logging.Logger;
020:
021: /**
022: * The ABBaseCommandTag class forms the base class of all the command
023: * tags classes in Address book. It provides functionality common to all
024: * address book tags like retrieving the context object and the contactDB
025: * object from the context which performs backend LDAP operations.
026: *
027: * @author Mihir Sambhus
028: * @version 1.0
029: */
030: public abstract class ABBaseCommandTag extends CommandTag {
031:
032: // Create a logger for this class
033: private static Logger debugLogger = PortalLogger
034: .getLogger(ABBaseCommandTag.class);
035:
036: /*
037: * Address Book context object to be accessed
038: */
039: protected ABContext abContext = null;
040:
041: /*
042: * name of the entry bean to be modified
043: */
044: protected String name = null;
045:
046: /**
047: * Get the name attribute
048: *
049: * @return the name attribute
050: */
051: public String getName() {
052: return name;
053: }
054:
055: /**
056: * Set the name attribute
057: *
058: * @param set the name attribute
059: */
060: public void setName(String name) {
061: this .name = name;
062: }
063:
064: /*
065: * entryid of the address book entry to be deleted
066: */
067: protected String entryid = null;
068:
069: /**
070: * Get the entryid attribute
071: *
072: * @return the entryid attribute
073: */
074: public String getEntryid() {
075: return entryid;
076: }
077:
078: /**
079: * Set the entryid attribute
080: *
081: * @param set the entryid attribute
082: */
083: public void setEntryid(String entryid) {
084: this .entryid = evalAttribute(entryid);
085: }
086:
087: /**
088: * Get the current session's ABContext object
089: *
090: * @return ABContext object
091: * @exception JspException
092: */
093: public ABContext getABContext() throws JspException {
094:
095: // try to retrieve address book context from pagecontext ..
096: try {
097: this .abContext = ABContext.getContext(pageContext);
098: } catch (Exception e) {
099: throw new JspException(e.getMessage());
100: }
101: if (this .abContext == null) {
102: throw new JspException(
103: "ABBaseCommandTag.execute(): Address Book context not found");
104: }
105:
106: return this .abContext;
107: }
108:
109: /**
110: * Retrieve the contactDB object from the AB context.
111: *
112: * @return ContactDB object
113: * @exception JspException
114: */
115: public ContactDB getContactDB() throws JspException {
116:
117: ContactDB contactDB = getABContext().getContactDB();
118: if (contactDB == null) {
119: throw new JspException(
120: "ABBaseCommandTag.getContactDB(): ContactDB object not found");
121: }
122: return contactDB;
123: }
124:
125: /**
126: * Verifying if the entryid attribute exists.. only called from
127: * delete, modify tags
128: *
129: * @exception JspException
130: */
131: public void verifyEntryid() throws JspException {
132: // entryid is mandatory attribute for modify. delete
133: if (entryid == null) {
134: throw new JspException(
135: "ABCommangTag.verifyEntryid(): 'entryid' attribute not specified");
136: }
137: }
138:
139: /**
140: * Verifying if the last name property filled in .. only called from
141: * add, modify tags
142: *
143: * @exception JspException
144: */
145: public boolean requiredPropertiesExist(Element e)
146: throws JspException {
147:
148: if (e.isEntry()) {
149: //
150: // Entry has to have Last name property filled
151: //
152: Entry entry = (Entry) e;
153:
154: if ((entry.getLn() != null) && (!entry.getLn().equals(""))) {
155: return true;
156: }
157:
158: getABContext().setErrorCode("AB_001"); // AB_001 = Last name required
159: debugLogger.finer("PSMA_CSPWTA0006");
160:
161: } else if (e.isGroup()) {
162: // No required property for now
163: Group group = (Group) e;
164: if ((group.getCn() != null) && (!group.getCn().equals(""))) {
165: return true;
166: }
167:
168: getABContext().setErrorCode("AB_002"); // AB_002 = Group name required
169: debugLogger.finer("PSMA_CSPWTA0007");
170:
171: }
172:
173: return false;
174: }
175:
176: /**
177: * Retrieve the element bean from the pagecontext... only called from
178: * add, modify tags
179: *
180: * @return Element object
181: * @exception JspException
182: */
183: public Element getElement() throws JspException {
184: // name is a mandatory attribute for add, modify
185: if (name == null) {
186: throw new JspException(
187: "ABCommangTag.getEntry(): 'name' attribute not specified");
188: }
189: // if name attribute present, return the bean corresponding to the name
190: Element element = (Element) pageContext.getAttribute(name);
191: if (element == null) {
192: throw new JspException(
193: "ABBaseCommandTag.getEntry(): Bean corresponding to "
194: + name
195: + "not found in address book context");
196: }
197: return element;
198: }
199:
200: /**
201: * Retrieve the element bean from the resultset in context... only called
202: * from delete, modify tags
203: *
204: * @param int entryid - corresponding to which the element object from
205: * the collection is to be retrieved
206: * @return Element object
207: * @exception JspException
208: */
209: public Element getEntry(int entryid) throws JspException {
210:
211: // get the context first if null.
212: if (this .abContext == null)
213: this .abContext = getABContext();
214:
215: // Get the element object corresponsing to the entryid from context
216: Element element = this .abContext.getEntry(entryid);
217:
218: if (element == null) {
219: throw new JspException(
220: "ABBaseCommandTag.getEntry(): Element bean corresponding to "
221: + entryid + " not found in the collection");
222: }
223: return element;
224: }
225:
226: /**
227: * Cleanup
228: */
229: public void release() {
230: super.release();
231: abContext = null;
232: name = null;
233: entryid = null;
234: }
235:
236: }
|