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: package com.sun.portal.search.admin;
007:
008: import java.io.IOException;
009: import java.util.*;
010: import java.util.logging.Logger;
011: import java.util.logging.Level;
012: import java.lang.*;
013: import java.net.*;
014:
015: import javax.servlet.*;
016: import javax.servlet.http.*;
017:
018: import com.iplanet.jato.*;
019: import com.iplanet.jato.view.*;
020: import com.iplanet.jato.model.*;
021: import com.iplanet.jato.view.event.*;
022: import com.iplanet.jato.view.html.*;
023: import com.iplanet.jato.model.*;
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.http.HttpServletResponse;
026:
027: import com.iplanet.am.console.components.view.html.IPlanetButton;
028:
029: import com.sun.portal.search.rdm.RDMTaxonomy;
030: import com.sun.portal.log.common.PortalLogger;
031:
032: /**
033: * This view uses model of type RequestHandlingTiledViewBase to display
034: * a path like view of the selected category
035: */
036: public class CategoryPathView extends RequestHandlingTiledViewBase {
037: public static final String CHILD_ELEM_HREF = "catPathElemHref";
038: public static final String CHILD_ELEM_NAME = "catPathElemName";
039: public static final String CHILD_ELEM_SEPARATOR = "catPathElemSeparator";
040: public static final String CHILD_CATEGORY_ID = "catId";
041:
042: private static String separatorStr;
043: private String selectedCategory;
044: private int nbLevels = 0;
045:
046: // Create a logger for this class
047: private static Logger debugLogger = PortalLogger
048: .getLogger(CategoryPathView.class);
049:
050: public CategoryPathView(View parent, String name) {
051: super (parent, name);
052: // init of the Category Path Element's separator string
053: separatorStr = ((CSViewBeanBase) getParentViewBean())
054: .getLocalizedString("category.edit.path_separator");
055: separatorStr = (separatorStr == null) ? ">" : separatorStr;
056: setPrimaryModel((DatasetModel) getDefaultModel());
057: registerChildren();
058: }
059:
060: protected void registerChildren() {
061: registerChild(CHILD_ELEM_HREF, HREF.class);
062: registerChild(CHILD_ELEM_NAME, StaticTextField.class);
063: registerChild(CHILD_ELEM_SEPARATOR, StaticTextField.class);
064: registerChild(CHILD_CATEGORY_ID, HiddenField.class);
065: }
066:
067: protected View createChild(String name) {
068: if (name.equals(CHILD_ELEM_HREF)) {
069: // return new HREF(this, CHILD_ELEM_HREF, "");
070: return new HREF(this , CHILD_ELEM_HREF, "");
071: }
072: if (name.equals(CHILD_ELEM_NAME)) {
073: return new StaticTextField(this , CHILD_ELEM_NAME, "");
074: }
075: if (name.equals(CHILD_ELEM_SEPARATOR)) {
076: return new StaticTextField(this , CHILD_ELEM_SEPARATOR,
077: separatorStr);
078: }
079: if (name.equals(CHILD_CATEGORY_ID)) {
080: return new HiddenField(this , CHILD_CATEGORY_ID, "");
081: }
082: throw new IllegalArgumentException("Invalid child name ["
083: + name + "]");
084: }
085:
086: public void beginDisplay(DisplayEvent event)
087: throws ModelControlException {
088: super .beginDisplay(event);
089: resetTileIndex();
090: updateModel();
091: }
092:
093: /*
094: * do not display the separator for the last element of the path
095: */
096: public boolean beginCatPathElemSeparatorDisplay(
097: ChildDisplayEvent event) {
098: try {
099: int tileIndex = getTileIndex();
100: if ((tileIndex + 1) >= nbLevels) {
101: return false;
102: } else {
103: return true;
104: }
105: } catch (Exception e) {
106: return false;
107: }
108: }
109:
110: public boolean nextTile() throws ModelControlException {
111: boolean movedToRow = super .nextTile();
112: if (movedToRow) {
113: NTEViewBean pvb = (NTEViewBean) getParentViewBean();
114: // Put row specific logic here
115: int index = getTileIndex();
116: String targetCategoryId = (String) ((DefaultModel) getPrimaryModel())
117: .getValue(CHILD_CATEGORY_ID);
118: setDisplayFieldValue(CHILD_CATEGORY_ID, targetCategoryId);
119: HREF href = (HREF) getDisplayField(CHILD_ELEM_HREF);
120: href.addExtraValue(NTEViewBean.DISPLAY_PAGE, String
121: .valueOf(pvb.findNodePage(targetCategoryId)));
122: setDisplayFieldValue(CHILD_ELEM_HREF, href);
123: setDisplayFieldValue(CHILD_ELEM_NAME,
124: (String) ((DefaultModel) getPrimaryModel())
125: .getValue(CHILD_ELEM_NAME));
126: }
127: return movedToRow;
128: }
129:
130: /*
131: * edit mode is displayed if selected id for action
132: * and/or if an error occured during the preceeding edition
133: */
134: public boolean beginEditModeDisplay(ChildDisplayEvent event) {
135: return !beginBrowseModeDisplay(event);
136: }
137:
138: public boolean beginBrowseModeDisplay(ChildDisplayEvent event) {
139: NTEViewBean pvb = (NTEViewBean) getParentViewBean();
140: return pvb.beginBrowseModeDisplay(event);
141: }
142:
143: /*
144: * returns the number of element in the path to the selected Category.
145: * if selected category is null we always display root so return=1
146: * for any given non root category, the return value is the number of
147: * category separator(interval) caracter ":" +(1 for interval)
148: * +(1 for root level not counted in category ID)
149: */
150: private void updateModel() throws ModelControlException {
151: nbLevels = 0;
152: DefaultModel model = (DefaultModel) getPrimaryModel();
153: try {
154: // clear Model
155: model.clear();
156:
157: NTEViewBean pvb = (NTEViewBean) getParentViewBean();
158: RequestContext rc = getRequestContext();
159: HttpServletRequest req = rc.getRequest();
160: String nodePageStr = (String) req
161: .getParameter(NTEViewBean.DISPLAY_PAGE);
162: String nodeEditStr = (String) req
163: .getParameter(NTEViewBean.TAX_ACTION_EDIT);
164: String catIdStr;
165: if (nodeEditStr != null) {
166: catIdStr = pvb.findNodeId((new Integer(nodeEditStr))
167: .intValue());
168: } else if (nodePageStr != null) {
169: catIdStr = pvb.findNodeId((new Integer(nodePageStr))
170: .intValue());
171: } else {
172: Integer currentPageInt = (Integer) pvb
173: .getPageSessionAttribute(NTEViewBean.DISPLAY_PAGE);
174: int currentPage = (currentPageInt != null) ? currentPageInt
175: .intValue()
176: : 1;
177: catIdStr = pvb.findNodeId(currentPage);
178: }
179:
180: // adding the taxonomy element (ie ROOT)
181: model.appendRow();
182: model.setValue(CHILD_ELEM_NAME, pvb.getTaxonomyName());
183: model.setValue(CHILD_CATEGORY_ID,
184: RDMTaxonomy.RDM_TAXONOMY_ROOT);
185: nbLevels++;
186:
187: // adding the category path elements
188: StringTokenizer st = new StringTokenizer(catIdStr, ":");
189: int prevIndex = 0;
190: int nextIndex = 0;
191: String levelCategoryId;
192:
193: // do following only if selecteed cat is not root
194: if (!catIdStr.equals(pvb.getTaxonomyName())) {
195: while (st.hasMoreTokens()) {
196: String token = st.nextToken();
197: // currentId = currentId + token;
198: model.appendRow();
199: model.setValue(CHILD_ELEM_NAME, token);
200: nbLevels++;
201: nextIndex = catIdStr.indexOf(":", prevIndex + 1);
202: if (nextIndex != -1) {
203: levelCategoryId = catIdStr.substring(0,
204: nextIndex);
205: prevIndex = nextIndex;
206: } else {
207: levelCategoryId = catIdStr;
208: }
209: model.setValue(CHILD_CATEGORY_ID, levelCategoryId);
210: }
211: }
212: // setting the number of tiles
213: } catch (Exception e) {
214: getPrimaryModel().setSize(nbLevels);
215: }
216: }
217:
218: /*
219: * handles the clik on the HREF
220: */
221: public void handleCatPathElemHrefRequest(
222: RequestInvocationEvent event) throws ModelControlException {
223: // dertermining the selected node
224: RequestContext rc = event.getRequestContext();
225: HttpServletRequest req = rc.getRequest();
226: int nodePage;
227: try {
228: nodePage = (new Integer((String) req
229: .getParameter(NTEViewBean.DISPLAY_PAGE)))
230: .intValue();
231: } catch (NumberFormatException nfe) {
232: debugLogger.finer("PSSH_CSPSA0005");
233: nodePage = 1;
234: }
235: req.setAttribute(NTEViewBean.DISPLAY_PAGE, String
236: .valueOf(nodePage));
237: getParentViewBean().forwardTo(rc);
238:
239: }
240: }
|