001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005:
006: /*
007: * TaxonomyBrowserTreeView.java
008: *
009: * Created on January 24, 2002, 12:42 AM
010: */
011:
012: package com.sun.portal.search.admin;
013:
014: import java.io.*;
015: import java.lang.reflect.*;
016: import java.util.*;
017: import java.util.logging.Logger;
018: import java.util.logging.Level;
019: import java.net.*;
020:
021: import javax.servlet.*;
022: import javax.servlet.http.*;
023: import javax.servlet.jsp.tagext.Tag;
024:
025: import com.iplanet.jato.*;
026: import com.iplanet.jato.model.*;
027: import com.iplanet.jato.util.*;
028: import com.iplanet.jato.view.*;
029: import com.iplanet.jato.view.event.*;
030: import com.iplanet.jato.view.html.*;
031: import com.iplanet.jato.taglib.tree.html.TreeNodeHandleTag;
032:
033: import com.sun.portal.search.util.SearchConfig;
034: import com.sun.portal.search.rdm.RDMClassification;
035: import com.sun.portal.search.rdm.RDMTaxonomy;
036: import com.sun.portal.search.admin.model.TaxonomyTreeModel;
037: import com.sun.portal.log.common.PortalLogger;
038:
039: import com.iplanet.am.console.components.view.html.IPlanetButton;
040:
041: /**
042: *
043: */
044: public class TaxonomyBrowserTreeView extends
045: RequestHandlingTreeViewBase {
046: //////////////////////////////////////
047: // Class variables
048: //////////////////////////////////////
049: // children handles
050: public static final String CHILD_NAME = "name";
051: public static final String CHILD_HREF = "Href1";
052:
053: //miscelaneous
054: public static final String NODE_ID = "nodeId";
055:
056: //////////////////////////////////////
057: // Instance variables
058: //////////////////////////////////////
059: public int currentPage = 1;
060: public int nbPages = 1;
061: public int nbDisplayable = -1;
062: public int maxNbNodesDisplayed;
063: public int displayableNodeCounter = 0;
064: // index of the page to display
065: private int displayedPage = -1;
066: private int editedCategory = -1;
067: private String editTimeStamp = null;
068:
069: // Create a Logger for this class
070: private static Logger debugLogger = PortalLogger
071: .getLogger(TaxonomyBrowserTreeView.class);
072:
073: /** Creates new NTTView */
074: public TaxonomyBrowserTreeView(View parent, String name)
075: throws ModelControlException {
076: super (parent, name);
077: try {
078: setPrimaryModel(getTaxonomyTreeModel());
079: registerChildren();
080: } catch (Exception e) {
081: debugLogger.log(Level.INFO, "PSSH_CSPSA0010", e
082: .getMessage());
083: throw new ModelControlException(e.getMessage());
084: }
085: }
086:
087: /**
088: *
089: *
090: */
091: protected void registerChildren() {
092: try {
093: registerChild(CHILD_NAME, StaticTextField.class);
094: registerChild(CHILD_HREF, HREF.class);
095: // in case we use images link for the editing commandes
096: } catch (Exception e) {
097: debugLogger.log(Level.INFO, "PSSH_CSPSA0010", e
098: .getMessage());
099: }
100: }
101:
102: /**
103: *
104: *
105: */
106: protected View createChild(String name) {
107: try {
108: View child = null;
109: // CHILD_NAME
110: if (name.equals(CHILD_NAME)) {
111: child = new StaticTextField(this ,
112: getTaxonomyTreeModel(), CHILD_NAME,
113: TaxonomyTreeModel.FIELD_NAME, "", null);
114: return child;
115: }
116: // CHILD_HREF
117: if (name.equals(CHILD_HREF)) {
118: // build the URL
119: child = new HREF(this , CHILD_HREF, "");
120: return child;
121: }
122: debugLogger.log(Level.INFO, "PSSH_CSPSA0002", name);
123: if (name != null) {
124: throw new IllegalArgumentException(
125: "Invalid child name [" + name + "]");
126: } else {
127: throw new IllegalArgumentException(
128: "Invalid child name [<NULL>]");
129: }
130: } catch (Exception e) {
131: debugLogger.log(Level.INFO, "PSSH_CSPSA0010", e
132: .getMessage());
133: if (name != null) {
134: throw new IllegalArgumentException(
135: "Invalid child name [" + name + "]");
136: } else {
137: throw new IllegalArgumentException(
138: "Invalid child name [<NULL>]");
139: }
140: }
141: }
142:
143: /*
144: *
145: */
146: public boolean beginDisplayedNodeDisplay(ChildDisplayEvent event) {
147: // increase the number of node Displayed.
148: displayableNodeCounter++;
149:
150: if (maxNbNodesDisplayed != -1) {
151: //if node in the page range display it
152: if ((displayableNodeCounter >= currentPage)
153: && (displayableNodeCounter < currentPage
154: + maxNbNodesDisplayed)) {
155: return true;
156: } else {
157: return false;
158: }
159: } else {
160: return true;
161: }
162: }
163:
164: /**
165: *
166: *
167: */
168: public void beginDisplay(DisplayEvent event) {
169: try {
170: super .beginDisplay(event);
171: ViewBean pvb = getParentViewBean();
172: String editTimeStamp = (String) pvb
173: .getPageSessionAttribute("editTimeStamp");
174: if (editTimeStamp == null) {
175: editTimeStamp = getRequestContext().getRequest()
176: .getParameter("editTimeStamp");
177: if (editTimeStamp != null) {
178: pvb.setPageSessionAttribute("editTimeStamp",
179: editTimeStamp);
180: }
181: }
182: } catch (ModelControlException e) {
183: }
184: TaxonomyBrowserView pv = (TaxonomyBrowserView) getParent();
185: ViewBean pvb = getParentViewBean();
186: nbPages = pv.nbPages;
187: maxNbNodesDisplayed = pv.maxNbNodesDisplayed;
188: currentPage = pv.currentPage;
189: }
190:
191: /*
192: * returns true if current node is root
193: */
194: public boolean beginIsRootNodeDisplay(ChildDisplayEvent event) {
195: boolean b = false;
196: try {
197: RDMClassification currentNode = (RDMClassification) getTaxonomyTreeModel()
198: .getCurrentNode();
199: if (currentNode.getParentId() == null) {
200: b = true;
201: } else {
202: b = false;
203: }
204: return b;
205: } catch (Exception e) {
206: debugLogger.log(Level.INFO, "PSSH_CSPSA0010", e
207: .getMessage());
208: return b;
209: }
210: }
211:
212: /*
213: * returns false if current node is NOT root
214: */
215: public boolean beginIsNotRootNodeDisplay(ChildDisplayEvent event) {
216: boolean b = !beginIsRootNodeDisplay(event);
217: return b;
218: }
219:
220: /**
221: *
222: */
223: public boolean beginHrefDisplay(ChildDisplayEvent event, HREF href,
224: String action) {
225: try {
226: RDMClassification currentNode = (RDMClassification) getTaxonomyTreeModel()
227: .getCurrentNode();
228: href.setValue(String.valueOf(displayableNodeCounter));
229: return true;
230: } catch (Exception e) {
231: debugLogger.log(Level.INFO, "PSSH_CSPSA0066", new String[] {
232: event.toString(), href.toString(), action,
233: e.getMessage() });
234: return false;
235: }
236: }
237:
238: /**
239: *
240: */
241: public boolean beginHref1Display(ChildDisplayEvent event) {
242: try {
243: return beginHrefDisplay(event,
244: (HREF) getDisplayField(CHILD_HREF),
245: NTEViewBean.TAX_ACTION_EDIT);
246: } catch (Exception e) {
247: debugLogger.log(Level.INFO, "PSSH_CSPSA0061", new String[] {
248: event.toString(), e.getMessage() });
249: return false;
250: }
251: }
252:
253: /*
254: *
255: */
256: public TaxonomyTreeModel getTaxonomyTreeModel() {
257: try {
258: CSViewBeanBase pvb = (CSViewBeanBase) getParentViewBean();
259: ServletContext sc = getRequestContext().getServletContext();
260: Locale locale = pvb.getUserLocale();
261: // adding locale as part of attribute key, so user with same locale shares the same model */
262: TaxonomyTreeModel model = (TaxonomyTreeModel) sc
263: .getAttribute(TaxonomyTreeModel.MODEL_NAME + "."
264: + locale.toString());
265: if (model == null) {
266: // instanciate the model and put it in the Servlet Context
267: model = new TaxonomyTreeModel();
268: model.userLocale = locale;
269: sc.setAttribute(TaxonomyTreeModel.MODEL_NAME + "."
270: + locale.toString(), model);
271: }
272: return model;
273: } catch (Exception e) {
274: debugLogger.log(Level.INFO, "PSSH_CSPSA0010", e
275: .getMessage());
276: return null;
277: }
278: }
279:
280: // Event handling methods
281: //////////////////////////////////////
282:
283: /**
284: *
285: */
286: public void handleHref1Request(RequestInvocationEvent event)
287: throws ModelControlException {
288: debugLogger.finer("PSSH_CSPSA0117");
289: int i;
290: int nodeIndex = getDisplayFieldIntValue(CHILD_HREF);
291: ViewBean pvb = getParentViewBean();
292: String taxName = getTaxByIndex(nodeIndex);
293: debugLogger.log(Level.FINER, "PSSH_CSPSA0118", taxName);
294: String editTimeStamp = (String) pvb
295: .getPageSessionAttribute("editTimeStamp");
296: debugLogger.log(Level.FINER, "PSSH_CSPSA0089", editTimeStamp);
297:
298: if (taxName != null
299: && !taxName.equals(RDMTaxonomy.RDM_TAXONOMY_ROOT)) {
300: Object selTaxObj = pvb
301: .getRequestContext()
302: .getRequest()
303: .getSession()
304: .getAttribute(
305: TaxonomyBrowserView.SESION_ATTR_SELECTED_TAX
306: + editTimeStamp);
307: if (selTaxObj != null) {
308: debugLogger.finer("PSSH_CSPSA0119");
309: //check multiple selection enabled
310: if (selTaxObj.getClass().isArray()) {
311: debugLogger.finer("PSSH_CSPSA0120");
312: String[] selTax = (String[]) selTaxObj;
313: String[] newSelTax = new String[selTax.length + 1];
314: for (i = 0; i < selTax.length; i++) {
315: newSelTax[i] = selTax[i];
316: }
317: newSelTax[i] = taxName;
318: pvb
319: .getRequestContext()
320: .getRequest()
321: .getSession()
322: .setAttribute(
323: TaxonomyBrowserView.SESION_ATTR_SELECTED_TAX
324: + editTimeStamp, newSelTax);
325: } else {
326: debugLogger.log(Level.FINER, "PSSH_CSPSA0121",
327: taxName);
328: pvb
329: .getRequestContext()
330: .getRequest()
331: .getSession()
332: .setAttribute(
333: TaxonomyBrowserView.SESION_ATTR_SELECTED_TAX
334: + editTimeStamp, taxName);
335: }
336: } else {
337: debugLogger.finer("PSSH_CSPSA0122");
338: }
339: }
340: pvb.forwardTo(getRequestContext());
341: }
342:
343: /*
344: * The index start with 1 no 0
345: */
346: public String getTaxByIndex(int index) {
347: try {
348: int i;
349: resetNodeLocation();
350: for (i = 1; nextNode() && i < index; i++)
351: ;
352: if (i == index) {
353: RDMClassification node = (RDMClassification) ((TaxonomyTreeModel) getPrimaryModel())
354: .getCurrentNode();
355: return node.getId();
356: }
357: } catch (Exception e) {
358: }
359: return null;
360: }
361:
362: public int getIndexByTax(String taxName) {
363: try {
364: int i;
365: debugLogger.log(Level.FINER, "PSSH_CSPSA0123", taxName);
366: resetNodeLocation();
367: for (i = 1; nextNode(); i++) {
368: RDMClassification node = (RDMClassification) ((TaxonomyTreeModel) getPrimaryModel())
369: .getCurrentNode();
370: if (taxName.equals(node.getId())) {
371: debugLogger.log(Level.FINER, "PSSH_CSPSA0124",
372: Integer.toString(i));
373: return i;
374: }
375: }
376: } catch (Exception e) {
377: }
378: return -1;
379: }
380:
381: /*
382: * count number of node in current three shape.
383: * also add first node name for every page into given ArrayList
384: * by giving number of nodes per page.
385: */
386: public int getNbDisplay(int numPerPage, ArrayList pageNodeList) {
387: int numNode = 0;
388: int numPage = 1;
389: try {
390: resetNodeLocation();
391: int currPageNodeCount = 0;
392: pageNodeList.clear();
393: while (nextNode()) {
394: numNode++;
395: if (currPageNodeCount == 0) {
396: RDMClassification node = (RDMClassification) ((TaxonomyTreeModel) getPrimaryModel())
397: .getCurrentNode();
398: if (node.getParent() != null) {
399: pageNodeList.add(node.getParentId() + ":"
400: + node.getId());
401: } else {
402: pageNodeList.add(getTaxonomyTreeModel()
403: .getNodeName(
404: RDMTaxonomy.RDM_TAXONOMY_ROOT));
405: }
406: }
407: currPageNodeCount = ++currPageNodeCount % numPerPage;
408: }
409: debugLogger.log(Level.FINER, "PSSH_CSPSA0125", Integer
410: .toString(numNode));
411: } catch (Exception e) {
412: }
413: return numNode;
414: }
415:
416: public void expandAll() {
417: try {
418: resetNodeLocation();
419: while (nextNode()) {
420: if (isParentNode() && !isNodeExpanded()) {
421: getStateData().setNodeExpanded(
422: getPrimaryModel().getNodeID(), true);
423: }
424: }
425: } catch (Exception e) {
426: }
427: }
428:
429: public void collapseAll() {
430: try {
431: resetNodeLocation();
432: while (nextNode()) {
433: if (isParentNode() && isNodeExpanded()) {
434: getStateData().setNodeExpanded(
435: getPrimaryModel().getNodeID(), false);
436: }
437: }
438: } catch (Exception e) {
439: }
440: }
441:
442: }
|