0001: /*
0002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
0003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
0004: */
0005:
0006: /*
0007: * TaxonomyBrowserView.java
0008: */
0009:
0010: package com.sun.portal.search.admin;
0011:
0012: import java.io.*;
0013: import java.lang.reflect.*;
0014: import java.lang.*;
0015: import java.net.*;
0016: import java.util.*;
0017: import java.util.logging.Logger;
0018: import java.util.logging.Level;
0019: import javax.servlet.*;
0020: import javax.servlet.http.*;
0021: import com.iplanet.jato.*;
0022: import com.iplanet.jato.model.*;
0023: import com.iplanet.jato.util.*;
0024: import com.iplanet.jato.view.*;
0025: import com.iplanet.jato.view.event.*;
0026: import com.iplanet.jato.view.html.*;
0027:
0028: import com.sun.portal.search.util.SearchConfig;
0029: import com.sun.portal.search.rdm.RDMTaxonomy;
0030: import com.sun.portal.search.rdm.RDMClassification;
0031: import com.sun.portal.search.admin.model.TaxonomyTreeModel;
0032: import com.sun.portal.log.common.PortalLogger;
0033:
0034: import com.iplanet.am.console.components.view.html.MessageBox;
0035: import com.iplanet.am.console.components.view.html.IPlanetButton;
0036:
0037: /**
0038: *
0039: */
0040: public class TaxonomyBrowserView extends RequestHandlingViewBase {
0041: public static final String DISPLAY_PAGE = "page";
0042: public static final String DISPLAY_NB_NODES = "nbNodes";
0043: // banner elements
0044: public static final String CHILD_EXPAND_ALL = "ExpandAll";
0045: public static final String CHILD_COLLAPSE_ALL = "CollapseAll";
0046: public static final String CHILD_PREVIOUS = "PreviousLink";
0047: public static final String CHILD_NEXT = "NextLink";
0048: public static final String CHILD_PAGE_SELECT = "PageSelect";
0049: public static final String CHILD_NODES_PER_PAGE = "NodesPerPage";
0050: public static final String CHILD_CATEGORY_PATH = "catPath";
0051: public static final String CHILD_JUMP_PAGE_URL = "jumpPageUrl";
0052:
0053: public static final String CHILD_OK_BTN = "OkButton";
0054: public static final String CHILD_CANCEL_BTN = "CancelButton";
0055: public static final String CHILD_RELOADPAGE = "ReloadPage";
0056:
0057: //for multiple selection enabled
0058: public static final String CHILD_SELECTED_LABEL = "SelectedTaxLabel";
0059: public static final String CHILD_SELECTED_LIST = "SelectedTax";
0060: public static final String CHILD_SINGLESELECTED_TAX = "SingleSelectedTax";
0061: public static final String CHILD_REMOVE_BTN = "RemoveButton";
0062: public static final String CHILD_PAGE_HELP = "PageHelp";
0063:
0064: public static final String TAX_TREEVIEW = "TaxonomyTreeView";
0065:
0066: public static final String TAX_ACTION_ADD_CHILD = "add_child";
0067: public static final String TAX_ACTION_ADD_SIBLING = "add_sibling";
0068: public static final String TAX_ACTION_UPDATE = "update";
0069: public static final String TAX_ACTION_DELETE = "delete";
0070: public static final String TAX_ACTION_EDIT = "edit";
0071: public static final String TAX_ACTION_SELECT = "select";
0072:
0073: private static final String DEF_MAX_DISPLAYED_NODE = "25";
0074:
0075: public static final String SESION_ATTR_SELECTED_TAX = "SelectedTax";
0076: public static final String SESION_ATTR_SELECTED_TAX_ORG = "SelectedTaxOrigin";
0077: public static final String SESION_ATTR_BROWSE_FOR = "BrowseFor";
0078: public static final String SESION_ATTR_BROWSING_ON = "BrowsingMode";
0079:
0080: public static int maxNbNodesDisplayed;
0081: public static int maxNbComboElement;
0082: public int currentPage = 1;
0083: public int nbPages = 1;
0084: public int nbDisplayable = -1;
0085: public int nbParents = -1;
0086: protected String nodePage;
0087: private int intervalSize;
0088: private static String[] nodesPerPageLabels;
0089: private static String[] nodesPerPageValues;
0090: public String currentTaxName = "";
0091: private boolean isSingle = true;
0092: private String editTimeStamp = null;
0093:
0094: // Create a Logger for this class
0095: private static Logger debugLogger = PortalLogger
0096: .getLogger(TaxonomyBrowserView.class);
0097:
0098: static {
0099: try {
0100: // init of the pagination related attributes
0101: String maxComboStr = SearchConfig
0102: .getValue(SearchConfig.CATEGORY_MAX_COMBO_ELEMENT);
0103: if (maxComboStr != null) {
0104: maxNbComboElement = Integer.parseInt(maxComboStr);
0105: } else {
0106: maxNbComboElement = 10;
0107: }
0108: nodesPerPageLabels = new String[] { DEF_MAX_DISPLAYED_NODE };
0109: nodesPerPageValues = new String[] { DEF_MAX_DISPLAYED_NODE };
0110: String maxNodeDisplayedStr = SearchConfig
0111: .getValue(SearchConfig.CATEGORY_ELEMENTS_PER_PAGE);
0112: if (maxNodeDisplayedStr != null) {
0113: StringTokenizer st = new StringTokenizer(
0114: maxNodeDisplayedStr, ",");
0115: if (st.countTokens() >= 1) {
0116: nodesPerPageLabels = new String[st.countTokens()];
0117: nodesPerPageValues = new String[st.countTokens()];
0118: boolean hasAll = false;
0119: int ndx = 0;
0120: String labelStr;
0121: String valueStr;
0122: while (st.hasMoreTokens()) {
0123: String token = (String) st.nextToken();
0124: int value = Integer.parseInt(token);
0125: if ((value <= 0)) {
0126: // add "all" only if not present already
0127: labelStr = "all";
0128: valueStr = "-1";
0129: if (!hasAll) {
0130: // add the couple Label/Value
0131: nodesPerPageLabels[ndx] = labelStr;
0132: nodesPerPageValues[ndx] = valueStr;
0133: if (value <= 0) {
0134: hasAll = true;
0135: }
0136: }
0137: } else {
0138: nodesPerPageLabels[ndx] = token;
0139: nodesPerPageValues[ndx] = token;
0140: }
0141: ndx++;
0142: }
0143: }
0144: }
0145: maxNbNodesDisplayed = Integer
0146: .parseInt(nodesPerPageValues[0]);
0147: } catch (NumberFormatException nfe) {
0148: debugLogger.log(Level.INFO, "PSSH_CSPSA0060",
0149: SearchConfig.CATEGORY_ELEMENTS_PER_PAGE);
0150: maxNbNodesDisplayed = -1;
0151: maxNbComboElement = 10;
0152: }
0153: }
0154:
0155: private TaxonomyBrowserView() {
0156: // Empty constructor declared private
0157: // This is used for logging in a static method
0158: }
0159:
0160: /**
0161: * Creates new TaxonomyBrowserView
0162: */
0163: public TaxonomyBrowserView(View parent, String name) {
0164: super (parent, name);
0165: try {
0166: registerChildren();
0167: } catch (Exception e) {
0168: debugLogger.log(Level.INFO, "PSSH_CSPSA0010", e
0169: .getMessage());
0170: }
0171: }
0172:
0173: /**
0174: *
0175: */
0176: protected void registerChildren() {
0177: try {
0178: registerChild(TAX_TREEVIEW, TaxonomyBrowserTreeView.class);
0179: registerChild(CHILD_PREVIOUS, HREF.class);
0180: registerChild(CHILD_EXPAND_ALL, IPlanetButton.class);
0181: registerChild(CHILD_COLLAPSE_ALL, IPlanetButton.class);
0182: registerChild(CHILD_NEXT, HREF.class);
0183: registerChild(CHILD_PAGE_SELECT, ComboBox.class);
0184: registerChild(CHILD_NODES_PER_PAGE, ComboBox.class);
0185: registerChild(CHILD_CATEGORY_PATH,
0186: NewCategoryPathView.class);
0187:
0188: registerChild(CHILD_OK_BTN, IPlanetButton.class);
0189: registerChild(CHILD_CANCEL_BTN, IPlanetButton.class);
0190: registerChild(CHILD_REMOVE_BTN, IPlanetButton.class);
0191: registerChild(CHILD_SELECTED_LABEL, StaticTextField.class);
0192: registerChild(CHILD_SELECTED_LIST, ListBox.class);
0193: registerChild(CHILD_SINGLESELECTED_TAX, TextField.class);
0194: registerChild(CHILD_RELOADPAGE, IPlanetButton.class);
0195: registerChild(CHILD_PAGE_HELP, StaticTextField.class);
0196:
0197: } catch (Exception e) {
0198: debugLogger.log(Level.INFO, "PSSH_CSPSA0010", e
0199: .getMessage());
0200: }
0201: }
0202:
0203: /**
0204: *
0205: */
0206: protected View createChild(String name) {
0207: try {
0208: View child = null;
0209: if (name.equals(TAX_TREEVIEW)) {
0210: child = new TaxonomyBrowserTreeView(this , TAX_TREEVIEW);
0211: return child;
0212: }
0213: // CHILD_TAX_EXPAND_ALL
0214: if (name.equals(CHILD_EXPAND_ALL)) {
0215: child = new IPlanetButton(this , CHILD_EXPAND_ALL, "");
0216: if (getTaxonomyTreeModel().nChildren(
0217: RDMTaxonomy.RDM_TAXONOMY_ROOT) > 0) {
0218: ((IPlanetButton) child).setEnable(true);
0219: } else {
0220: ((IPlanetButton) child).setEnable(false);
0221: }
0222: return child;
0223: }
0224: // CHILD_TAX_COLLAPSE_ALL
0225: if (name.equals(CHILD_COLLAPSE_ALL)) {
0226: child = new IPlanetButton(this , CHILD_COLLAPSE_ALL, "");
0227: if (getTaxonomyTreeModel().nChildren(
0228: RDMTaxonomy.RDM_TAXONOMY_ROOT) > 0) {
0229: ((IPlanetButton) child).setEnable(true);
0230: } else {
0231: ((IPlanetButton) child).setEnable(false);
0232: }
0233: return child;
0234: }
0235: if (name.equals(CHILD_PREVIOUS)) {
0236: child = new HREF(this , CHILD_PREVIOUS, "");
0237: return child;
0238: }
0239: if (name.equals(CHILD_NEXT)) {
0240: child = new HREF(this , CHILD_NEXT, "");
0241: return child;
0242: }
0243: if (name.equals(CHILD_CATEGORY_PATH)) {
0244: child = new NewCategoryPathView(this ,
0245: CHILD_CATEGORY_PATH);
0246: return child;
0247: }
0248: if (name.equals(CHILD_PAGE_HELP)) {
0249: child = new StaticTextField(this , CHILD_PAGE_HELP, "");
0250: return child;
0251: }
0252: if (name.equals(CHILD_NODES_PER_PAGE)) {
0253: child = new ComboBox(this , CHILD_NODES_PER_PAGE, String
0254: .valueOf(maxNbNodesDisplayed));
0255: ((ComboBox) child).setMultiSelect(false);
0256: OptionList nodesPerPageOptions = new OptionList(
0257: nodesPerPageLabels, nodesPerPageValues);
0258: ((ComboBox) child).setOptions(nodesPerPageOptions);
0259: return child;
0260: }
0261: if (name.equals(CHILD_PAGE_SELECT)) {
0262: child = new ComboBox(this , CHILD_PAGE_SELECT, "-1");
0263: ((ComboBox) child).setMultiSelect(false);
0264: return child;
0265: }
0266: if (name.equals(CHILD_OK_BTN)) {
0267: return new IPlanetButton(this , CHILD_OK_BTN,
0268: getLocalizedString("ok.text"));
0269: }
0270: if (name.equals(CHILD_CANCEL_BTN)) {
0271: return new IPlanetButton(this , CHILD_CANCEL_BTN,
0272: getLocalizedString("cancel.text"));
0273: }
0274: if (name.equals(CHILD_REMOVE_BTN)) {
0275: return new IPlanetButton(this , CHILD_REMOVE_BTN,
0276: getLocalizedString("remove.text"));
0277: }
0278: if (name.equals(CHILD_SELECTED_LABEL)) {
0279: return new StaticTextField(this , CHILD_SELECTED_LABEL,
0280: "");
0281: }
0282: if (name.equals(CHILD_SELECTED_LIST)) {
0283: return new ListBox(this , CHILD_SELECTED_LIST, "");
0284: }
0285: if (name.equals(CHILD_SINGLESELECTED_TAX)) {
0286: return new TextField(this , CHILD_SINGLESELECTED_TAX, "");
0287: }
0288: if (name.equals(CHILD_RELOADPAGE)) {
0289: return new IPlanetButton(this , CHILD_RELOADPAGE, "");
0290: }
0291:
0292: debugLogger.log(Level.FINER, "PSSH_CSPSA0048", name);
0293: if (name != null) {
0294: throw new IllegalArgumentException(
0295: "Invalid child name [" + name + "]");
0296: } else {
0297: throw new IllegalArgumentException(
0298: "Invalid child name [<NULL>]");
0299: }
0300: } catch (Exception e) {
0301: debugLogger.log(Level.INFO, "PSSH_CSPSA0061", new String[] {
0302: name, e.getMessage() });
0303: if (name != null) {
0304: throw new IllegalArgumentException(
0305: "Invalid child name [" + name + "]");
0306: } else {
0307: throw new IllegalArgumentException(
0308: "Invalid child name [<NULL>]");
0309: }
0310: }
0311: }
0312:
0313: public int getIndexByTax(String taxName) {
0314: TaxonomyBrowserTreeView tv = (TaxonomyBrowserTreeView) getChild(TAX_TREEVIEW);
0315: return tv.getIndexByTax(taxName);
0316: }
0317:
0318: public boolean beginSingleSectionDisplay(ChildDisplayEvent event) {
0319: return isSingle;
0320: }
0321:
0322: public boolean beginMultipleSectionDisplay(ChildDisplayEvent event) {
0323: return !isSingle;
0324: }
0325:
0326: public void beginDisplay(DisplayEvent event) {
0327:
0328: // setting page encoding
0329: //setPageEncoding();
0330: setDisplayFieldValue(CHILD_EXPAND_ALL,
0331: getLocalizedString("category.edit.btn_expand_all"));
0332: setDisplayFieldValue(CHILD_COLLAPSE_ALL,
0333: getLocalizedString("category.edit.btn_collapse_all"));
0334:
0335: maxNbNodesDisplayed = getDisplayFieldIntValue(CHILD_NODES_PER_PAGE);
0336:
0337: // counting the number of expanded (displayable nodes
0338: TaxonomyBrowserTreeView tv = (TaxonomyBrowserTreeView) getChild(TAX_TREEVIEW);
0339: ArrayList firstNodeList = new ArrayList();
0340: nbDisplayable = tv.getNbDisplay(maxNbNodesDisplayed,
0341: firstNodeList);
0342:
0343: // get the number of pages
0344: getNbPages();
0345:
0346: // get currentPage
0347: getCurrentPage();
0348: debugLogger.log(Level.FINER, "PSSH_CSPSA0126", Integer
0349: .toString(currentPage));
0350:
0351: // set the pageSelect dropdown
0352: setPageSelect(firstNodeList);
0353:
0354: ViewBean pvb = getParentViewBean();
0355:
0356: /* even if the attribute is named "editTimeStamp" this is not related to
0357: the edition of a classification, this timestamp is RDEditor RDs' session
0358: timestamp. */
0359: editTimeStamp = (String) pvb
0360: .getPageSessionAttribute("editTimeStamp");
0361: Object selTaxObj = pvb.getRequestContext().getRequest()
0362: .getSession().getAttribute(
0363: SESION_ATTR_SELECTED_TAX + editTimeStamp);
0364: Object orgSelTaxObj = pvb.getRequestContext().getRequest()
0365: .getSession().getAttribute(
0366: SESION_ATTR_SELECTED_TAX_ORG + editTimeStamp);
0367: if (selTaxObj != null) {
0368: //check multiple selection enabled
0369: if (selTaxObj.getClass().isArray()) {
0370: setDisplayFieldValue(
0371: CHILD_SELECTED_LABEL,
0372: getLocalizedString("category.browser.selectedtaxs"));
0373: isSingle = false;
0374: String[] selTax = (String[]) selTaxObj;
0375: //save origin selected taxonomy for CANCEL button
0376: if (orgSelTaxObj == null) {
0377: pvb.getRequestContext().getRequest().getSession()
0378: .setAttribute(
0379: SESION_ATTR_SELECTED_TAX_ORG
0380: + editTimeStamp, selTax);
0381: }
0382: String[] selTaxLabel = new String[selTax.length];
0383: for (int i = 0; i < selTax.length; i++) {
0384: selTaxLabel[i] = prettyPrintCategory(selTax[i]);
0385: }
0386: ListBox child = (ListBox) getChild(CHILD_SELECTED_LIST);
0387: child.setOptions(new OptionList(selTaxLabel, selTax));
0388: child.setMultiSelect(true);
0389: } else {
0390: setDisplayFieldValue(
0391: CHILD_SELECTED_LABEL,
0392: getLocalizedString("category.browser.selectedtax"));
0393: //save origin selected taxonomy for CANCEL button
0394: if (orgSelTaxObj == null) {
0395: pvb.getRequestContext().getRequest().getSession()
0396: .setAttribute(
0397: SESION_ATTR_SELECTED_TAX_ORG
0398: + editTimeStamp,
0399: (String) selTaxObj);
0400: }
0401: setDisplayFieldValue(this .CHILD_SINGLESELECTED_TAX,
0402: selTaxObj);
0403: }
0404: }
0405: String pageHelp = (String) pvb
0406: .getPageSessionAttribute(CHILD_PAGE_HELP);
0407: if (pageHelp != null) {
0408: setDisplayFieldValue(CHILD_PAGE_HELP, pageHelp);
0409: } else {
0410: setDisplayFieldValue(CHILD_PAGE_HELP, this
0411: .getLocalizedString("category.browser.desc"));
0412: }
0413:
0414: currentTaxName = tv.getTaxByIndex(currentPage);
0415: }
0416:
0417: /*
0418: *
0419: */
0420: public String getErrorMsg() {
0421: try {
0422: StringBuffer errMsg = new StringBuffer();
0423: // get model errors
0424: String modelErr = (String) getTaxonomyTreeModel()
0425: .getErrorMsg();
0426: if (modelErr != null) {
0427: errMsg.append(modelErr);
0428: }
0429: if (errMsg.length() != 0) {
0430: return (errMsg.toString());
0431: } else {
0432: return null;
0433: }
0434: } catch (Exception e) {
0435: debugLogger.log(Level.INFO, "PSSH_CSPSA0010", e
0436: .getMessage());
0437: return "A system error occured but cannot identify the source";
0438: }
0439: }
0440:
0441: /*
0442: * finds among the displayable nodes the NodeId of the one index is passed on
0443: */
0444: String findDisplayableNodeId(int displayableNodeIndex) {
0445: return String.valueOf(displayableNodeIndex); //TODO really search for it
0446: }
0447:
0448: /*
0449: * finds the index of the displayable node which nodeId is passed on
0450: */
0451: int findDisplayableNodeIndex(String NodeId) {
0452: //TODO not used for now, but might be needed if we want to search for the
0453: // taxonomy Id (String) given it's index in the displayable list
0454: return 0;
0455: }
0456:
0457: /*
0458: * return true if there is a need of navigation or if
0459: * we currently display All, to allow reduction of the nbNodes per page
0460: */
0461: public boolean beginNavigationBarDisplay(ChildDisplayEvent event) {
0462: boolean ret = false;
0463: // determining the interval between displayable node to form the
0464: // combobox menu that enables jumping directly to specific page
0465: // labeled by a node
0466: if (beginPageSelectDisplay(event)
0467: || beginNodesPerPageDisplay(event)) {
0468: // dont display the comboBox
0469: ret = true;
0470: } else {
0471: ret = false;
0472: }
0473: return ret;
0474: }
0475:
0476: /*
0477: * set the comboBox option list if pages > 1
0478: * set the current Page accordingly if possible
0479: */
0480: public boolean beginPageSelectDisplay(ChildDisplayEvent event) {
0481: boolean ret = false;
0482: // determining the interval between displayable node to form the
0483: // combobox menu that enables jumping directly to specific page
0484: // labeled by a node
0485: if (beginPreviousLinkDisplay(event)
0486: || beginNextLinkDisplay(event)) {
0487: // dont display the comboBox
0488: ret = true;
0489: } else {
0490: ret = false;
0491: }
0492: return ret;
0493: }
0494:
0495: /*
0496: * set the comboBox option list if more than one choice in search.conf
0497: * for attribute admin-category_editor_nodes_per_page
0498: */
0499: public boolean beginNodesPerPageDisplay(ChildDisplayEvent event) {
0500: boolean ret = false;
0501: // determining the interval between displayable node to form the
0502: // combobox menu that enables jumping directly to specific page
0503: // labeled by a node
0504: if (nodesPerPageLabels.length > 1) {
0505: ret = true;
0506: } else {
0507: // dont display the comboBox
0508: ret = false;
0509: }
0510: return ret;
0511: }
0512:
0513: /*
0514: * do not diplays the page select label and drop down
0515: */
0516: public boolean beginNotPageSelectDisplay(ChildDisplayEvent event) {
0517: return !beginPageSelectDisplay(event);
0518: }
0519:
0520: /*
0521: * displaying the Previous link
0522: */
0523: public boolean beginPreviousLinkDisplay(ChildDisplayEvent event) {
0524: boolean ret = false;
0525: if (nbPages > 1) {
0526: if (isFirstDisplayed()) {
0527: ret = false;
0528: } else {
0529: ret = true;
0530: }
0531: } else {
0532: ret = false;
0533: }
0534: return ret;
0535: }
0536:
0537: /*
0538: * displaying the Previous link
0539: */
0540: public boolean beginNextLinkDisplay(ChildDisplayEvent event) {
0541: boolean ret = false;
0542: if (nbPages > 1) {
0543: if (isLastDisplayed()) {
0544: ret = false;
0545: } else {
0546: ret = true;
0547: }
0548: } else {
0549: ret = false;
0550: }
0551: return ret;
0552: }
0553:
0554: /*
0555: * not displaying the previous link
0556: */
0557: public boolean beginNotPreviousLinkDisplay(ChildDisplayEvent event) {
0558: return !beginPreviousLinkDisplay(event);
0559: }
0560:
0561: /*
0562: * not displaying the previous link
0563: */
0564: public boolean beginNotNextLinkDisplay(ChildDisplayEvent event) {
0565: return !beginNextLinkDisplay(event);
0566: }
0567:
0568: /*
0569: * expandAll
0570: */
0571: public void handleExpandAllRequest(RequestInvocationEvent event) {
0572: TaxonomyBrowserTreeView tv = (TaxonomyBrowserTreeView) getChild(TAX_TREEVIEW);
0573: tv.expandAll();
0574: ViewBean viewBean = getParentViewBean();
0575: viewBean.forwardTo(getRequestContext());
0576:
0577: }
0578:
0579: /*
0580: * collapseAll
0581: */
0582: public void handleCollapseAllRequest(RequestInvocationEvent event) {
0583: TaxonomyBrowserTreeView tv = (TaxonomyBrowserTreeView) getChild(TAX_TREEVIEW);
0584: tv.collapseAll();
0585: ViewBean viewBean = getParentViewBean();
0586: viewBean.forwardTo(getRequestContext());
0587:
0588: }
0589:
0590: public void handleReloadPageRequest(RequestInvocationEvent event) {
0591: ViewBean pvb = getParentViewBean();
0592: pvb.forwardTo(getRequestContext());
0593: }
0594:
0595: /*
0596: * returns the taxonomyName
0597: */
0598: public String getTaxonomyName() {
0599: try {
0600: return getTaxonomyTreeModel().getNodeName(
0601: RDMTaxonomy.RDM_TAXONOMY_ROOT);
0602: } catch (ModelControlException e) {
0603: // return the default taxonomy name out of the seach.config
0604: String defaultTaxonomyName = SearchConfig
0605: .getValue(SearchConfig.TAX);
0606: if (defaultTaxonomyName.trim().length() == 0) {
0607: return RDMTaxonomy.RDM_TAXONOMY_ROOT;
0608: } else {
0609: return defaultTaxonomyName;
0610: }
0611: }
0612: }
0613:
0614: /*
0615: * getting the tree Model
0616: */
0617: public TaxonomyTreeModel getTaxonomyTreeModel() {
0618: try {
0619: CSViewBeanBase pvb = (CSViewBeanBase) getParentViewBean();
0620: ServletContext sc = getRequestContext().getServletContext();
0621: Locale locale = pvb.getUserLocale();
0622: // adding locale as part of attribute key, so user with same locale shares the same model */
0623: TaxonomyTreeModel model = (TaxonomyTreeModel) sc
0624: .getAttribute(TaxonomyTreeModel.MODEL_NAME + "."
0625: + locale.toString());
0626: if (model == null) {
0627: // instanciate the model and put it in the Servlet Context
0628: model = new TaxonomyTreeModel();
0629: model.userLocale = locale;
0630: sc.setAttribute(TaxonomyTreeModel.MODEL_NAME + "."
0631: + locale.toString(), model);
0632: }
0633: return model;
0634: } catch (Exception e) {
0635: debugLogger.log(Level.INFO, "PSSH_CSPSA0010", e
0636: .getMessage());
0637: return null;
0638: }
0639: }
0640:
0641: /*
0642: * check if first node displayed in current page
0643: */
0644: public boolean isFirstDisplayed() {
0645: boolean ret = true;
0646: if (currentPage == 1) {
0647: ret = true;
0648: } else {
0649: ret = false;
0650: }
0651: return ret;
0652: }
0653:
0654: /*
0655: * check if last node displayed in current page
0656: */
0657: public boolean isLastDisplayed() {
0658: boolean ret = true;
0659: if (currentPage >= (nbDisplayable - maxNbNodesDisplayed + 1)) {
0660: // adding 1 because the Root nodes need to be accounted
0661: ret = true;
0662: } else {
0663: ret = false;
0664: }
0665: return ret;
0666: }
0667:
0668: /*
0669: * returns the nodeId for a given linearized index.
0670: * catches the CategoryName thrown by findNodeId(int, int, String)
0671: * else returns the TaxonomyName.
0672: */
0673: public String findNodeId(int index) {
0674: try {
0675: findNodeId(index, 0, RDMTaxonomy.RDM_TAXONOMY_ROOT);
0676: return getTaxonomyName();
0677: } catch (ModelControlException mce) {
0678: // found the nodeId which linearized index is nodeIndex
0679: return mce.getMessage();
0680: }
0681: }
0682:
0683: /*
0684: * go recursily through the treeStateData till counted the "nodeIndex"
0685: * displayable node, at which point through an exception which message is
0686: * the String representation of the Node which linearise index is nodeIndex
0687: * if no exception, that mean count down not finished, and method return
0688: * current index count, for using it as start point for next call
0689: */
0690: public int findNodeId(int nodeIndex, int currentIndex,
0691: String startNodeId) throws ModelControlException {
0692: int ret = currentIndex + 1;
0693: if (ret == nodeIndex) {
0694: // found the node in question
0695: if (nodeIndex == 1) {
0696: // this is the taxonomy node
0697: throw new ModelControlException(getTaxonomyName());
0698: } else {
0699: // regular category Node
0700: throw new ModelControlException(startNodeId);
0701: }
0702: } else {
0703: // not yet reach the node, continue drillng down
0704: try {
0705: TreeView tv = (TreeView) getChild(TAX_TREEVIEW);
0706: TreeViewStateData sd = tv.getStateData();
0707: if (getTaxonomyTreeModel().isParentNode(startNodeId)) {
0708: // if parentNode expanded
0709: if (sd.isNodeExpanded(TaxonomyTreeModel
0710: .hashNodeId(startNodeId))) {
0711: // search nodeId within children
0712: ArrayList children = (ArrayList) getTaxonomyTreeModel()
0713: .getChildren(startNodeId);
0714: Iterator c = children.iterator();
0715: RDMClassification childNode;
0716: String childNodeId;
0717: while (c.hasNext()) {
0718: childNode = (RDMClassification) c.next();
0719: childNodeId = childNode.getId();
0720: ret = findNodeId(nodeIndex, ret,
0721: childNodeId);
0722: }
0723: } else {
0724: // not expanded parent node, also not node Indexed search for
0725: }
0726: } else {
0727: // leaf node, if it was the one, would already have handle it
0728: }
0729: return ret;
0730: } catch (NullPointerException e) {
0731: throw new ModelControlException(getTaxonomyName());
0732: }
0733: }
0734: }
0735:
0736: /*
0737: * returns the linearized index of the nodeId
0738: * every parent and leaf node that is displayable prior to the argument
0739: * nodeId is accounted for including itself.
0740: * nodes that might be displayable but that would appear down the "nodeId"
0741: * are skipped all together
0742: */
0743: public int findNodePage(String nodeId) {
0744: int page = 0;
0745: if (nodeId != null) {
0746: if (nodeId.equals(RDMTaxonomy.RDM_TAXONOMY_ROOT)) {
0747: page = 1;
0748: } else {
0749: // recusively accumulate displayable children of ROOT
0750: page += findNodePage(nodeId,
0751: RDMTaxonomy.RDM_TAXONOMY_ROOT);
0752: }
0753: } else {
0754: page = 1;
0755: }
0756: return page;
0757: }
0758:
0759: /*
0760: *
0761: */
0762: public int findNodePage(String nodeId, String startNodeId) {
0763: int nbNodes = 0; // root always displayable;
0764: try {
0765: // count the startNodeId if before or the searched nodeId
0766: if ((startNodeId.compareTo(nodeId) <= 0)
0767: || (startNodeId
0768: .equals(RDMTaxonomy.RDM_TAXONOMY_ROOT))) {
0769: TreeView tv = (TreeView) getChild(TAX_TREEVIEW);
0770: TreeViewStateData sd = tv.getStateData();
0771: // if node is a parent
0772: if (getTaxonomyTreeModel().isParentNode(startNodeId)) {
0773: // if parentNode expanded
0774: if (sd.isNodeExpanded(TaxonomyTreeModel
0775: .hashNodeId(startNodeId))) {
0776: // accumulate the number of displayable for all children
0777: ArrayList children = (ArrayList) getTaxonomyTreeModel()
0778: .getChildren(startNodeId);
0779: Iterator c = children.iterator();
0780: RDMClassification childNode;
0781: String childNodeId;
0782: while (c.hasNext()) {
0783: childNode = (RDMClassification) c.next();
0784: childNodeId = childNode.getId();
0785: int nbNodesInChild = findNodePage(nodeId,
0786: childNodeId);
0787: nbNodes += nbNodesInChild;
0788: }
0789: nbNodes += 1; // adding the parent
0790: } else {
0791: nbNodes = 1;
0792: }
0793: } else {
0794: nbNodes = 1;
0795: }
0796: } else {
0797: nbNodes = 0;
0798: }
0799: return nbNodes;
0800: } catch (Exception e) {
0801: debugLogger.log(Level.INFO, "PSSH_CSPSA0055", new String[] {
0802: nodeId, startNodeId, e.getMessage() });
0803: return 0;
0804: }
0805: }
0806:
0807: /*
0808: * get the total number of pages
0809: */
0810: public void getNbPages() {
0811: // calculate nbPages do add in the pageSelection option drop down
0812: if ((maxNbNodesDisplayed != 0)
0813: && ((nbDisplayable != -1) && (maxNbNodesDisplayed != -1))) {
0814: // caculate the number of pages (add 1 if division not full)
0815: nbPages = (new Double(StrictMath
0816: .ceil((double) nbDisplayable
0817: / (double) maxNbNodesDisplayed)))
0818: .intValue();
0819: } else {
0820: // displays all in one page
0821: nbPages = 1;
0822: }
0823: }
0824:
0825: /*
0826: * create dropDown list of page navigation
0827: */
0828: public void setPageSelect(ArrayList firstNodeList) {
0829:
0830: // Populating the Page drop down combobox
0831: OptionList pageOptions = new OptionList();
0832: for (int i = 0; i < maxNbComboElement
0833: && i < firstNodeList.size(); i++) {
0834: pageOptions.add(new Option(
0835: prettyPrintCategory((String) firstNodeList.get(i)),
0836: Integer.toString(i)));
0837: }
0838: // updating the dropdown JATO combobox field
0839: try {
0840: ((ComboBox) getChild(CHILD_PAGE_SELECT))
0841: .setOptions(pageOptions);
0842: int selectedPage = currentPage / maxNbNodesDisplayed;
0843: setDisplayFieldValue(CHILD_PAGE_SELECT, String
0844: .valueOf(selectedPage));
0845: debugLogger.log(Level.FINER, "PSSH_CSPSA0127", Integer
0846: .toString(selectedPage));
0847: } catch (Exception e) {
0848: debugLogger.log(Level.INFO, "PSSH_CSPSA0068", e
0849: .getMessage());
0850: }
0851: }
0852:
0853: /*
0854: * determines the page to be displayed
0855: * this depends on
0856: * - DISPLAY_PAGE : specifically giving the page number to display
0857: * this is passed on upon page selection or click on next/previous btns
0858: */
0859: public void getCurrentPage() {
0860: RequestContext rc = getRequestContext();
0861: HttpServletRequest req = rc.getRequest();
0862:
0863: // get the 1st page to display from
0864: try {
0865: // put the selected Category if it's in the request
0866: nodePage = (String) req.getParameter(DISPLAY_PAGE);
0867: // browsing mode
0868: // get the currentPage from DISPLAY_PAGE
0869: if (nodePage != null) {
0870: // this is in the case we come from a click on a node link
0871: currentPage = Integer.parseInt(nodePage);
0872: if (currentPage < 1)
0873: currentPage = 1;
0874: if (currentPage > (nbDisplayable - maxNbNodesDisplayed))
0875: currentPage = nbDisplayable - maxNbNodesDisplayed
0876: + 1;
0877: } else {
0878: int selectedPage = getDisplayFieldIntValue(CHILD_PAGE_SELECT);
0879: if (selectedPage >= 0) {
0880: currentPage = selectedPage * maxNbNodesDisplayed
0881: + 1;
0882: } else {
0883:
0884: Integer currentPageInt = (Integer) getParentViewBean()
0885: .getPageSessionAttribute(DISPLAY_PAGE);
0886: if (currentPageInt != null) {
0887: currentPage = currentPageInt.intValue();
0888: }
0889: }
0890: }
0891: currentPage = (currentPage < 1) ? 1 : currentPage;
0892: currentPage = (currentPage > nbPages * maxNbNodesDisplayed) ? nbPages
0893: : currentPage;
0894: } catch (NullPointerException npe) {
0895: debugLogger.log(Level.INFO, "PSSH_CSPSA0010", npe
0896: .getMessage());
0897: currentPage = 1;
0898: } catch (Exception e) {
0899: debugLogger.log(Level.INFO, "PSSH_CSPSA0010", e
0900: .getMessage());
0901: }
0902:
0903: getParentViewBean().setPageSessionAttribute(DISPLAY_PAGE,
0904: new Integer(currentPage));
0905:
0906: }
0907:
0908: /*
0909: *
0910: */
0911: public int getPrevious() {
0912: int ret;
0913: try {
0914: currentPage = ((Integer) getParentViewBean()
0915: .getPageSessionAttribute(DISPLAY_PAGE)).intValue();
0916: } catch (NullPointerException npe) {
0917: currentPage = 1;
0918: }
0919: if (currentPage > maxNbNodesDisplayed) {
0920: ret = currentPage - maxNbNodesDisplayed;
0921: } else {
0922: ret = 1;
0923: }
0924: getParentViewBean().removePageSessionAttribute(DISPLAY_PAGE);
0925: return ret;
0926: }
0927:
0928: /*
0929: *
0930: */
0931: public int getNext() {
0932: int ret;
0933: try {
0934: currentPage = ((Integer) getParentViewBean()
0935: .getPageSessionAttribute(DISPLAY_PAGE)).intValue();
0936: } catch (NullPointerException npe) {
0937: currentPage = 1;
0938: }
0939: if ((currentPage != -1)
0940: || (currentPage < (nbDisplayable - maxNbNodesDisplayed))) {
0941: ret = currentPage + maxNbNodesDisplayed;
0942: } else {
0943: ret = nbPages * intervalSize;
0944: }
0945: //clearPageSessionAttributes();
0946: getParentViewBean().removePageSessionAttribute(DISPLAY_PAGE);
0947: return ret;
0948: }
0949:
0950: //////////////////////////////////////
0951: // Event handling methods
0952: //////////////////////////////////////
0953:
0954: /**
0955: *
0956: */
0957: public void handleNextLinkRequest(RequestInvocationEvent event)
0958: throws ModelControlException {
0959: try {
0960: ViewBean pvb = getParentViewBean();
0961: pvb.setPageSessionAttribute(DISPLAY_PAGE, new Integer(
0962: getNext()));
0963: pvb.forwardTo(getRequestContext());
0964: // refresh the category editor this time should
0965: // display the CategoryEditor "jato:content"
0966: } catch (Exception e) {
0967: debugLogger.log(Level.INFO, "PSSH_CSPSA0010", e
0968: .getMessage());
0969: throw new ModelControlException(
0970: "failed to handle click on next page link "
0971: + e.getMessage());
0972: }
0973: }
0974:
0975: /**
0976: *
0977: */
0978: public void handlePreviousLinkRequest(RequestInvocationEvent event)
0979: throws ModelControlException {
0980: try {
0981: ViewBean pvb = getParentViewBean();
0982: pvb.setPageSessionAttribute(DISPLAY_PAGE, new Integer(
0983: getPrevious()));
0984: pvb.forwardTo(getRequestContext());
0985: } catch (Exception e) {
0986: debugLogger.log(Level.INFO, "PSSH_CSPSA0010", e
0987: .getMessage());
0988: throw new ModelControlException(
0989: "failed to handle click on previous page link "
0990: + e.getMessage());
0991: }
0992: }
0993:
0994: public boolean beginExpandCollapseDisplay(ChildDisplayEvent event) {
0995: // TODO elaborate the code here
0996: return true;
0997: }
0998:
0999: /**
1000: *
1001: */
1002: public void handleOkButtonRequest(RequestInvocationEvent event)
1003: throws ModelControlException {
1004:
1005: ViewBean pvb = getParentViewBean();
1006: pvb.removePageSessionAttribute(SESION_ATTR_BROWSING_ON);
1007: String timeStamp = (String) pvb
1008: .getPageSessionAttribute("editTimeStamp");
1009: getRequestContext().getRequest().getSession().removeAttribute(
1010: SESION_ATTR_SELECTED_TAX_ORG + timeStamp);
1011: pvb.removePageSessionAttribute(CHILD_PAGE_HELP);
1012: pvb.forwardTo(getRequestContext());
1013: }
1014:
1015: /**
1016: *
1017: */
1018: public void handleCancelButtonRequest(RequestInvocationEvent event)
1019: throws ModelControlException {
1020: ViewBean pvb = getParentViewBean();
1021: String timeStamp = (String) pvb
1022: .getPageSessionAttribute("editTimeStamp");
1023: Object orgSelTaxObj = pvb.getRequestContext().getRequest()
1024: .getSession().getAttribute(
1025: SESION_ATTR_SELECTED_TAX_ORG + editTimeStamp);
1026: if (orgSelTaxObj != null) {
1027: if (orgSelTaxObj.getClass().isArray()) {
1028: pvb.getRequestContext().getRequest().getSession()
1029: .setAttribute(
1030: SESION_ATTR_SELECTED_TAX
1031: + editTimeStamp,
1032: (String[]) orgSelTaxObj);
1033: } else {
1034: pvb.getRequestContext().getRequest().getSession()
1035: .setAttribute(
1036: SESION_ATTR_SELECTED_TAX
1037: + editTimeStamp,
1038: (String) orgSelTaxObj);
1039: }
1040: }
1041: getRequestContext().getRequest().getSession().removeAttribute(
1042: SESION_ATTR_SELECTED_TAX_ORG + editTimeStamp);
1043: pvb.removePageSessionAttribute(SESION_ATTR_BROWSING_ON);
1044: pvb.removePageSessionAttribute(CHILD_PAGE_HELP);
1045: pvb.forwardTo(getRequestContext());
1046: }
1047:
1048: /*
1049: * returns true if modifications where made to the tree
1050: */
1051: public boolean beginPersistanceMsgDisplay(ChildDisplayEvent event) {
1052: try {
1053: if ((getErrorMsg() != null)
1054: || (getTaxonomyTreeModel().getModifiedState())
1055: || (getTaxonomyTreeModel().isConfigNewer())) {
1056: return true;
1057: } else {
1058: return false;
1059: }
1060: } catch (Exception e) {
1061: debugLogger.log(Level.INFO, "PSSH_CSPSA0010", e
1062: .getMessage());
1063: return false;
1064: }
1065: }
1066:
1067: ////////////////////////////////////////
1068: // Utility
1069: ////////////////////////////////////////
1070: public String prettyPrintCategory(String catStr) {
1071: String retStr;
1072: if (catStr != null) {
1073: StringBuffer sb = new StringBuffer();
1074: // adding the category path elements
1075: StringTokenizer st = new StringTokenizer(catStr, ":");
1076: int nbTokens = st.countTokens();
1077: int tokenIndex = 0;
1078: while (st.hasMoreTokens()) {
1079: sb.append(st.nextToken());
1080: tokenIndex++;
1081: if (tokenIndex < nbTokens) {
1082: sb.append(" > ");
1083: }
1084: }
1085: retStr = sb.toString();
1086: } else {
1087: debugLogger.finer("PSSH_CSPSA0070");
1088: retStr = "";
1089: }
1090: return retStr;
1091: }
1092:
1093: Locale getUserLocale() {
1094: CSViewBeanBase p = (CSViewBeanBase) this .getParentViewBean();
1095: return p.getUserLocale();
1096: }
1097:
1098: String getLocalizedString(String key) {
1099: CSViewBeanBase p = (CSViewBeanBase) this .getParentViewBean();
1100: return p.getLocalizedString(key);
1101: }
1102:
1103: String getModuleURL() {
1104: return this.getParentViewBean().getModuleURL();
1105: }
1106: }
|