001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions
006: * are met:
007: *
008: * - Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: *
011: * - Redistribution in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in
013: * the documentation and/or other materials provided with the
014: * distribution.
015: *
016: * Neither the name of Sun Microsystems, Inc. or the names of
017: * contributors may be used to endorse or promote products derived
018: * from this software without specific prior written permission.
019: *
020: * This software is provided "AS IS," without a warranty of any
021: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
022: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
024: * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
025: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
026: * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
027: * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
028: * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
029: * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
030: * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
031: * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
032: *
033: * You acknowledge that Software is not designed, licensed or intended
034: * any nuclear facility.
035: */
036:
037: package com.sun.portal.search.providers.faces;
038:
039: import com.sun.web.ui.component.Hyperlink;
040: import com.sun.web.ui.component.PanelGroup;
041: import com.sun.web.ui.component.StaticText;
042: import com.sun.web.ui.model.Option;
043: import java.util.ArrayList;
044: import java.util.List;
045: import java.util.ResourceBundle;
046: import java.util.logging.Level;
047: import java.util.logging.Logger;
048: import javax.faces.component.UIParameter;
049: import javax.faces.component.html.HtmlPanelGrid;
050: import javax.faces.context.FacesContext;
051: import javax.faces.model.ArrayDataModel;
052:
053: public class Category {
054:
055: private String id;
056: private PanelGroup panelGroupUI;
057: private PanelGroup childrenPanelGroupUI;
058: private String categoryRootID;
059: private HtmlPanelGrid childPanelGridUI;
060: private Hyperlink hyperlinkUI;
061: HtmlPanelGrid twoLevelPanelGrid;
062:
063: private int numChildPerPage = 50;
064: private int subCatPage = 1;
065: private List children;
066: static int idndx = 0;
067: private static Logger logger = SearchLogger
068: .getLogger(Category.class);
069: static int lindNdx = 0;
070: protected static int numberOfChild4TwoLevel = 4;
071:
072: private SearchHandler searchHandler;
073:
074: private String getLocalizedMessage(String key) {
075: return searchHandler.getLocalizedMessage(key);
076: }
077:
078: /** Creates a new instance of Category */
079: public Category(SearchHandler searchHandler, String root, String id) {
080: categoryRootID = root;
081: this .id = id;
082: this .searchHandler = searchHandler;
083: }
084:
085: public String getId() {
086: return id;
087: }
088:
089: public String getRelativeID() {
090: if (categoryRootID.equals("ROOT")) {
091: return id;
092: } else {
093: return id.substring(categoryRootID.length() + 1);
094: }
095: }
096:
097: public String getLabel() {
098: return getRelativeID().replaceAll(":", " > ");
099: }
100:
101: public String getName() {
102: int n = id.lastIndexOf(':');
103: if (n > 0) {
104: return id.substring(n + 1);
105: }
106: return id;
107: }
108:
109: public List getChildrenList() {
110: if (children == null) {
111: children = findChildren();
112: }
113: return children;
114: }
115:
116: public void setChildrenList(List value) {
117: children = value;
118: }
119:
120: public void setChildrenPanelGroupUI(PanelGroup o) {
121: }
122:
123: public PanelGroup getChildrenPanelGroupUI() {
124: if (childrenPanelGroupUI == null) {
125: childrenPanelGroupUI = new PanelGroup();
126: childrenPanelGroupUI.getChildren().add(new StaticText());
127: }
128: if (children == null) {
129: children = findChildren();
130: }
131: int s = (this .subCatPage - 1) * this .numChildPerPage; //index of the array
132: int e = subCatPage * numChildPerPage;
133: for (int i = s; i < e && i < children.size(); i++) {
134: Category c = (Category) children.get(i);
135: this .childrenPanelGroupUI.getChildren().add(
136: c.getHyperlinkUI());
137: }
138: return childrenPanelGroupUI;
139: }
140:
141: public ArrayDataModel getChildren() {
142: if (children == null) {
143: children = findChildren();
144: }
145: int s = (this .subCatPage - 1) * this .numChildPerPage; //index of the array
146: int e = subCatPage * numChildPerPage;
147: ArrayList pageChild = new ArrayList();
148: for (int i = s; i < e && i < children.size(); i++) {
149: pageChild.add(children.get(i));
150: }
151: return new ArrayDataModel(pageChild.toArray());
152: }
153:
154: public int getTotalChildren() {
155: getChildren();
156: return children.size();
157: }
158:
159: public int getStartCount() {
160: getChildren();
161: return (this .subCatPage - 1) * this .numChildPerPage + 1;
162: }
163:
164: public int getEndCount() {
165: getChildren();
166: int count = subCatPage * this .numChildPerPage;
167: if (count > children.size()) {
168: count = children.size();
169: }
170: return count;
171: }
172:
173: public boolean isHasNextSubcatPage() {
174: return (subCatPage * this .numChildPerPage < this .children
175: .size());
176: }
177:
178: public boolean isHasPreSubcatPage() {
179: return subCatPage > 1;
180: }
181:
182: public String nextSubcatPage() {
183: this .subCatPage++;
184: replaceChildComponents(childPanelGridUI);
185: return "";
186: }
187:
188: public String preSubcatPage() {
189: if (subCatPage > 1) {
190: this .subCatPage--;
191: replaceChildComponents(childPanelGridUI);
192: }
193: return "";
194: }
195:
196: public void setPanelGroupUI(PanelGroup o) {
197: panelGroupUI = o;
198: }
199:
200: public PanelGroup getPanelGroupUI() {
201: if (panelGroupUI == null) {
202: panelGroupUI = new PanelGroup();
203: //panelGroupUI.setTransient(true);
204: this .panelGroupUI.getChildren().addAll(getLinks());
205: }
206: return panelGroupUI;
207: }
208:
209: protected List getLinks() {
210: ArrayList links = new ArrayList();
211: String markupID;
212: String restoreID = "";
213: if (categoryRootID.equals("ROOT")) {
214: if (!this .id.equals(this .categoryRootID)) {
215: markupID = "ROOT:" + id;
216: } else {
217: markupID = id;
218: }
219: } else {
220: restoreID = categoryRootID + ":";
221: markupID = id.replaceFirst(categoryRootID, "ROOT");
222: }
223: String[] ps = markupID.split(":");
224: String id = null;
225: String uid = null;
226: for (int i = 1; i < ps.length; i++) {
227: if (i + 1 < ps.length) {
228: Hyperlink link = new Hyperlink();
229: link.setId("bclinks" + lindNdx++);
230: id = (id == null) ? (restoreID + ps[i])
231: : (id + ":" + ps[i]);
232: UIParameter p = new UIParameter();
233: p.setName("browseidParam");
234: p.setValue(id);
235: link.getChildren().add(p);
236: link.setText(ps[i]);
237: link.setAction(FacesContext.getCurrentInstance()
238: .getApplication().createMethodBinding(
239: "#{" + searchHandler.getBrowseMethod()
240: + "}", null));
241: links.add(link);
242: } else {
243: StaticText text = new StaticText();
244: text.setId("bclinks" + lindNdx++);
245: if (ps[i].equals(categoryRootID)) {
246: text
247: .setText(getLocalizedMessage("label_category_root"));
248: } else {
249: text.setText(ps[i]);
250: }
251: links.add(text);
252: }
253:
254: }
255: return links;
256: }
257:
258: public Hyperlink getHyperlinkUI() {
259: if (this .hyperlinkUI == null) {
260: hyperlinkUI = new Hyperlink();
261: hyperlinkUI.setId("category_" + idndx++);
262: UIParameter p = new UIParameter();
263: p.setName("browseidParam");
264: p.setValue(getId());
265: hyperlinkUI.getChildren().add(p);
266: hyperlinkUI.setText(getName());
267: hyperlinkUI.setAction(FacesContext.getCurrentInstance()
268: .getApplication().createMethodBinding(
269: "#{" + searchHandler.getBrowseMethod()
270: + "}", null));
271: }
272: return hyperlinkUI;
273: }
274:
275: public Option[] getAvailableParents() {
276: ArrayList options = new ArrayList();
277: String markupID;
278: String restoreID = "";
279: if (categoryRootID.equals("ROOT")) {
280: if (!this .id.equals(this .categoryRootID)) {
281: markupID = "ROOT:" + id;
282: } else {
283: markupID = id;
284: }
285: } else {
286: restoreID = categoryRootID + ":";
287: markupID = id.replaceFirst(categoryRootID, "ROOT");
288: }
289: String[] ps = markupID.split(":");
290: String id = null;
291: String label = getLocalizedMessage("page_top");
292: Option op;
293: for (int i = 0; i < ps.length; i++) {
294: if (i == 0) {
295: op = new Option(categoryRootID, label);
296: } else {
297: id = (id == null) ? (restoreID + ps[i])
298: : (id + ":" + ps[i]);
299: label = label + ">" + ps[i];
300: op = new Option(id, label);
301:
302: }
303: options.add(op);
304:
305: }
306: return (Option[]) options.toArray(new Option[0]);
307: }
308:
309: public Option[] getAvailableCategories() {
310: Option[] parents = getAvailableParents();
311: Option[] children = getAvailableSubCategories();
312: Option[] options = new Option[parents.length + children.length
313: - 1];
314: int ndx = 0;
315: String myLabel = "";
316: for (int i = 0; i < parents.length; i++) {
317: options[ndx++] = parents[i];
318: myLabel = parents[i].getLabel();
319: }
320:
321: for (int i = 1; i < children.length; i++) {
322: Option op = children[i];
323: op.setLabel(myLabel + ">" + op.getLabel());
324: options[ndx++] = op;
325: }
326: return options;
327: }
328:
329: public Option[] getAvailableSubCategories() {
330: if (children == null) {
331: children = findChildren();
332: }
333: Option[] options = new Option[children.size() + 1];
334: options[0] = new Option("", "-----------");
335: for (int i = 0; i < children.size(); i++) {
336: Category c = (Category) children.get(i);
337: options[i + 1] = new Option(c.getId(), c.getName());
338: }
339: return options;
340: }
341:
342: public void setChildPanelGridUI(HtmlPanelGrid o) {
343: childPanelGridUI = o;
344: }
345:
346: private void replaceChildComponents(HtmlPanelGrid o) {
347: this .childPanelGridUI.getChildren().clear();
348: if (children == null) {
349: children = findChildren();
350: }
351: int s = (this .subCatPage - 1) * this .numChildPerPage; //index of the array
352: int e = subCatPage * numChildPerPage;
353: for (int i = s; i < e && i < children.size(); i++) {
354: Category c = (Category) children.get(i);
355: this .childPanelGridUI.getChildren().add(c.getHyperlinkUI());
356: }
357: }
358:
359: public HtmlPanelGrid getChildPanelGridUI() {
360: if (childPanelGridUI != null) {
361: return childPanelGridUI;
362: }
363: childPanelGridUI = new HtmlPanelGrid();
364: //childPanelGridUI.setTransient(true);
365: if (children == null) {
366: children = findChildren();
367: }
368: int s = (this .subCatPage - 1) * this .numChildPerPage; //index of the array
369: int e = subCatPage * numChildPerPage;
370: for (int i = s; i < e && i < children.size(); i++) {
371: Category c = (Category) children.get(i);
372: this .childPanelGridUI.getChildren().add(c.getHyperlinkUI());
373: }
374: return childPanelGridUI;
375: }
376:
377: private ArrayList findChildren() {
378: TaxonomyDatabase taxonomyDb = searchHandler
379: .getTaxonomyDatabase();
380: if (taxonomyDb != null) {
381: return taxonomyDb.findChildren(this .getId());
382: }
383: return new ArrayList();
384: }
385:
386: public void setNumberPerPage(int value) {
387: this .numChildPerPage = value;
388: }
389:
390: public void setTwoLevelPanelGrid(HtmlPanelGrid o) {
391: //twoLevelPanelGrid = o;
392: }
393:
394: public HtmlPanelGrid getTwoLevelPanelGrid() {
395: if (twoLevelPanelGrid == null) {
396: twoLevelPanelGrid = new HtmlPanelGrid();
397: List categories = getChildrenList();
398: List topChildGrids = new ArrayList();
399: for (int i = 0; i < categories.size(); i++) {
400: Category c = (Category) categories.get(i);
401: HtmlPanelGrid topChild = new HtmlPanelGrid();
402: topChild.setColumns(1);
403: topChild.setColumnClasses("level1,level2");
404: Hyperlink topChildlink = new Hyperlink();
405: topChildlink.setStyleClass("level1link");
406: topChildlink.setId("bclinks" + idndx++);
407: topChildlink.setText(c.getName());
408: topChildlink.setValue(c.getName());
409: topChildlink.setAction(FacesContext
410: .getCurrentInstance().getApplication()
411: .createMethodBinding(
412: "#{" + searchHandler.getBrowseMethod()
413: + "}", null));
414: //link.getAttributes().put("browseidParam", c.getId());
415: UIParameter p = new UIParameter();
416: p.setName("browseidParam");
417: p.setValue(c.getId());
418: topChildlink.getChildren().add(p);
419: topChild.getChildren().add(topChildlink);
420: PanelGroup childLinks = new PanelGroup();
421: childLinks.setId("childlevel1" + i);
422: childLinks.setSeparator(", ");
423: List childs = c.getChildrenList();
424: List links = new ArrayList();
425:
426: for (int j = 0; j < childs.size()
427: && j < numberOfChild4TwoLevel; j++) {
428: Category cc = (Category) childs.get(j);
429: Hyperlink link = new Hyperlink();
430: link.setId("bclinks" + idndx++);
431: link.setAction(FacesContext.getCurrentInstance()
432: .getApplication().createMethodBinding(
433: "#{"
434: + searchHandler
435: .getBrowseMethod()
436: + "}", null));
437: link.setStyleClass("level2links");
438: p = new UIParameter();
439: p.setName("browseidParam");
440: if (j == numberOfChild4TwoLevel - 1) {
441: link
442: .setText(getLocalizedMessage("text_child_more"));
443: p
444: .setValue(c.getId().replaceAll("\'",
445: "\\\\\'"));
446: } else {
447: link.setText(cc.getName());
448: p.setValue(cc.getId()
449: .replaceAll("\'", "\\\\\'"));
450: }
451: link.getChildren().add(p);
452: links.add(link);
453: }
454: childLinks.getChildren().addAll(links);
455: topChild.getChildren().add(childLinks);
456: topChildGrids.add(topChild);
457: }
458: twoLevelPanelGrid.getChildren().addAll(topChildGrids);
459: }
460: return twoLevelPanelGrid;
461: }
462:
463: public boolean isRoot() {
464: return categoryRootID.equals(id);
465: }
466:
467: public String browse() {
468: this.searchHandler.setCurrentCategory(this);
469: return searchHandler.doBrowse();
470: }
471:
472: }
|