001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)ChartTreeBean.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package org.openesb.tools.charting;
030:
031: import org.openesb.tools.charting.exception.ChartingException;
032: import org.openesb.tools.charting.persist.ChartApplicationBeansContainer;
033: import org.openesb.tools.charting.persist.ChartBean;
034: import org.openesb.tools.charting.persist.ChartDataPersistence;
035: import org.openesb.tools.charting.persist.DataBean;
036:
037: import com.sun.rave.web.ui.appbase.AbstractPageBean;
038:
039: import com.sun.web.ui.component.TextField;
040: import com.sun.web.ui.component.TreeNode;
041: import com.sun.web.ui.component.Tree;
042:
043: import java.util.Iterator;
044: import java.util.List;
045: import java.util.Map;
046: import java.util.Set;
047: import java.util.logging.Logger;
048: import javax.faces.application.FacesMessage;
049: import javax.faces.component.UIComponent;
050: import javax.faces.context.FacesContext;
051: import javax.faces.event.ValueChangeEvent;
052: import javax.faces.event.ValueChangeListener;
053: import javax.faces.validator.Validator;
054: import javax.faces.validator.ValidatorException;
055:
056: /**
057: *
058: * @author rdwivedi
059: */
060: public class ChartTreeBean extends AbstractPageBean implements
061: ValueChangeListener, Validator {
062:
063: private TreeNode rootNode = null;
064: private static Logger mLogger = Logger
065: .getLogger(ChartTreeBean.class.getName());
066: private static String ROOT_IMGSRC = "/images/root.png"; //$NON-NLS-1$
067: private static String C_NODE_IMAGE = "/images/chart.png";
068: private static String TARGET_FRAME = "workspaceFrame";
069: private static String GROUP_IMGSRC = "/images/Charts_group.png"; //$NON-NLS-1$
070:
071: /** Creates a new instance of TreeBuilder */
072: public ChartTreeBean() {
073:
074: }
075:
076: public void init() {
077:
078: super .init();
079:
080: }
081:
082: /**
083: * <p>Callback method that is called after the component tree has been
084: * restored, but before any event processing takes place. This method
085: * will <strong>only</strong> be called on a postback request that
086: * is processing a form submit. Customize this method to allocate
087: * resources that will be required in your event handlers.</p>
088: */
089: public void preprocess() {
090: }
091:
092: /**
093: * <p>Callback method that is called just before rendering takes place.
094: * This method will <strong>only</strong> be called for the page that
095: * will actually be rendered (and not, for example, on a page that
096: * handled a postback and then navigated to a different page). Customize
097: * this method to allocate resources that will be required for rendering
098: * this page.</p>
099: */
100: public void prerender() {
101: }
102:
103: /**
104: * <p>Callback method that is called after rendering is completed for
105: * this request, if <code>init()</code> was called (regardless of whether
106: * or not this was the page that was actually rendered). Customize this
107: * method to release resources acquired in the <code>init()</code>,
108: * <code>preprocess()</code>, or <code>prerender()</code> methods (or
109: * acquired during execution of an event handler).</p>
110: */
111: public void destroy() {
112: }
113:
114: /**
115: * <p>Return a reference to the scoped data bean.</p>
116: */
117: protected RequestBean1 getRequestBean1() {
118: return (RequestBean1) getBean("RequestBean1");
119: }
120:
121: /**
122: * <p>Return a reference to the scoped data bean.</p>
123: */
124: protected SessionBean1 getSessionBean1() {
125: return (SessionBean1) getBean("SessionBean1");
126: }
127:
128: /**
129: * <p>Return a reference to the scoped data bean.</p>
130: */
131: protected ApplicationBean1 getApplicationBean1() {
132: return (ApplicationBean1) getBean("ApplicationBean1");
133: }
134:
135: private ChartApplicationBeansContainer getChartApplicationBeansContainer() {
136: return (ChartApplicationBeansContainer) getBean("ChartApplicationBeansContainer");
137:
138: }
139:
140: /**
141: * create a tree node
142: * @param String id - unique node id
143: * @param String name - name of node
144: **/
145: private TreeNode createNode(String id, String name, String imgsrc,
146: String url, String targetFrame) {
147: TreeNode node = new TreeNode();
148: node.setId(id);
149: node.setText(name);
150: node.setUrl(url);
151: node.setExpanded(true);
152: node.setTarget(targetFrame);
153: if (imgsrc != null) {
154: node.setImageURL(imgsrc);
155: }
156:
157: return node;
158: }
159:
160: public void addNode(TreeNode parent, TreeNode node) {
161: List children = parent.getChildren();
162: children.add(node);
163: }
164:
165: /**
166: * delete a tree node
167: * @param String nodeId - node ID
168: **/
169: public void deleteNode(TreeNode root, String nodeId) {
170: UIComponent node = root.findComponent(nodeId);
171: if (node != null) {
172: UIComponent parent = node.getParent();
173: List children = parent.getChildren();
174: children.remove((Object) node);
175: }
176:
177: }
178:
179: public TreeNode getRootNode() throws ChartingException {
180: mLogger.info("Get root node is called. The root node is "
181: + rootNode);
182: if (rootNode == null) {
183: rootNode = new TreeNode();
184: rootNode.setUrl("#");
185: rootNode.setText("tree root");
186: rootNode.setId("root"); //$NON-NLS-1$
187: rootNode.setExpanded(true);
188: rootNode.setImageURL(ROOT_IMGSRC);
189: buildChartingTree(rootNode);
190: TreeNode t = buildDAGroup("Some Name");
191: addNode(rootNode, t);
192: }
193: return rootNode;
194: }
195:
196: private void buildChartingTree(TreeNode root)
197: throws ChartingException {
198: mLogger.info("Build tree node for chart called.");
199: ChartApplicationBeansContainer appC = getChartApplicationBeansContainer();
200: Set s = appC.getAllChartGroups();
201: mLogger.info("Build tree node for chart called.2");
202: if (s != null) {
203: Iterator iter = s.iterator();
204: while (iter.hasNext()) {
205: mLogger.info("Build tree node for chart called.3");
206: String name = (String) iter.next();
207: mLogger.info("Build tree node for chart called.4"
208: + name);
209: TreeNode cg = buildChartGroup(name);
210: addNode(root, cg);
211: Map m = appC.getAllChartsForGroup(name);
212: if (m != null) {
213: Iterator iter2 = m.values().iterator();
214: while (iter2.hasNext()) {
215: ChartBean b = (ChartBean) iter2.next();
216: TreeNode n = createNode(b.getChartID(), b
217: .getChartName(), C_NODE_IMAGE,
218: getURL(b), TARGET_FRAME);
219: addNode(cg, n);
220: }
221: }
222: }
223: }
224:
225: }
226:
227: private TreeNode buildChartGroup(String gName) {
228: TreeNode chNode = new TreeNode();
229: chNode.setUrl("#");
230: chNode.setText(gName);
231: chNode.setId("__CG" + gName.replaceAll(" ", "_")); //$NON-NLS-1$
232: chNode.setExpanded(true);
233: chNode.setImageURL(GROUP_IMGSRC);
234: return chNode;
235: }
236:
237: private TreeNode buildDAGroup(String gName) {
238: ChartApplicationBeansContainer appC = getChartApplicationBeansContainer();
239: gName = "Data Access";
240: TreeNode chNode = new TreeNode();
241: chNode.setUrl("#");
242: chNode.setText(gName);
243: chNode.setId("__DAG" + gName.replaceAll(" ", "_")); //$NON-NLS-1$
244: chNode.setExpanded(true);
245: chNode.setImageURL(GROUP_IMGSRC);
246: Map m = appC.getAllDataBeans();
247: if (m != null) {
248: Iterator iter2 = m.values().iterator();
249: while (iter2.hasNext()) {
250: DataBean b = (DataBean) iter2.next();
251: TreeNode n = createNode(b.getID(), b.getDisplayName(),
252: C_NODE_IMAGE, getURL(b), TARGET_FRAME);
253: addNode(chNode, n);
254: }
255: }
256: return chNode;
257: }
258:
259: private String getURL(ChartBean b) {
260:
261: String url = "/faces/configure/processTreeNodeClick.jsp";
262: if (b == null) {
263:
264: //url = "/faces/layout/preChartConfiguration.jsp";
265: } else {
266: // url = "/faces/layout/preChartConfiguration.jsp?_chartUID="+b.getChartID();
267: url += "?_chartUID=" + b.getChartID() + "&rdm="
268: + Math.random();
269: }
270:
271: return url;
272: }
273:
274: public void setRootNode(TreeNode node) {
275: mLogger.info("Set root node is called");
276: rootNode = node;
277: }
278:
279: public TreeNode findTreeNode(String nodeId) {
280: mLogger.warning("The Find TreeNode not implemented");
281: TreeNode node = rootNode;
282: return node;
283: }
284:
285: private String createNewChildChartNode(TreeNode n) {
286: if (n.getChildCount() == 0) {
287: return "New Chart";
288: } else {
289: int ind = 0;
290: Iterator iter = n.getChildren().iterator();
291: while (iter.hasNext()) {
292: Object o = iter.next();
293: if (o instanceof TreeNode) {
294: TreeNode t = (TreeNode) o;
295: if (t.getText().startsWith("New Chart")) {
296: ind++;
297: }
298: }
299: }
300: if (ind == 0) {
301: return "New Chart";
302: } else {
303: return "New Chart_" + ind;
304: }
305: }
306: }
307:
308: private String createNewParentNodeName(TreeNode n) {
309: if (n.getChildCount() == 0) {
310: return "Chart Group";
311: } else {
312: int ind = 0;
313: Iterator iter = n.getChildren().iterator();
314: while (iter.hasNext()) {
315: Object o = iter.next();
316: if (o instanceof TreeNode) {
317: TreeNode t = (TreeNode) o;
318: if (t.getText().startsWith("Chart Group")) {
319: ind++;
320: }
321: }
322: }
323: if (ind == 0) {
324: return "Chart Group";
325: } else {
326: return "Chart Group " + ind;
327: }
328: }
329: }
330:
331: public String addNewChartNode() {
332: String selectedNodeId = null;
333: TreeNode selNode = null;
334: if (rootNode.getParent() instanceof Tree) {
335: Tree tr = (Tree) rootNode.getParent();
336: // mLogger.info("get Selected object is " + tr.getCookieSelectedTreeNode());
337: selectedNodeId = tr.getCookieSelectedTreeNode();
338: //mLogger.info("get Selected is " + selectedNodeId);
339: }
340: if (selectedNodeId == null) {
341: setErrorMessage("Please select a chart group in order to add a new chart.");
342: } else {
343: selNode = findNodeFromSelectedNodeIDString(selectedNodeId);
344: if (selNode == null) {
345: setErrorMessage("Error :: The node is not found.");
346: } else {
347: mLogger.info("Slected node id is " + selNode.getId());
348: if (selNode.getId().startsWith("__CG")) {
349: String name = createNewChildChartNode(selNode);
350: //getChartDataPersistence().resetForNewChart(name,(String)selNode.getText());
351: //ChartBean bean = getChartDataPersistence().getChartInfoBean();
352: ChartBean bean = getChartApplicationBeansContainer()
353: .createNewChart(name, selNode.getText());
354:
355: TreeNode n = createNode(bean.getChartID(), bean
356: .getChartName(), C_NODE_IMAGE,
357: getURL(bean), TARGET_FRAME);
358:
359: addNode(selNode, n);
360: } else {
361: setErrorMessage("Please select a Chart Group to add a chart.");
362: }
363: }
364: }
365: return "success";
366:
367: }
368:
369: public String addNewChartParentNode() {
370:
371: // TO DO ::: persist as a secondary system table in the DB...
372: TreeNode selNode = rootNode;
373: String name = createNewParentNodeName(selNode);
374: TreeNode chNode = new TreeNode();
375: chNode.setUrl("#");
376: chNode.setText(name);
377: chNode
378: .setId("__CG" + selNode.getId() + "_" + selNode.getChildren().size()); //$NON-NLS-1$
379: chNode.setExpanded(true);
380: chNode.setImageURL(GROUP_IMGSRC);
381: addNode(selNode, chNode);
382: return "success";
383: }
384:
385: public String renameSelectedNode() {
386: /// TO DO ::: take care of changing the name of chart group to persist for all charts in that group
387: // as their parent group name...... currenly not done....
388:
389: String selectedNodeId = null;
390: TreeNode selNode = null;
391: if (rootNode.getParent() instanceof Tree) {
392: Tree tr = (Tree) rootNode.getParent();
393: selectedNodeId = tr.getCookieSelectedTreeNode();
394: //mLogger.info("get Selected is " + selectedNodeId);
395: }
396: if (selectedNodeId == null) {
397: setErrorMessage("Please select a node to rename.");
398: } else {
399: selNode = findNodeFromSelectedNodeIDString(selectedNodeId);
400:
401: if (selNode == null) {
402: setErrorMessage("Error :: The node is not found.");
403: }
404: if (selNode == rootNode) {
405:
406: setErrorMessage("This node can not be renamed.");
407:
408: } else {
409: //mLogger.info("Slected node id is " + selNode.getId());
410: //mLogger.info("Slected node image key is " + selNode.getImageKeys().size());
411: //mLogger.info("Content facet " + selNode.getFacet(TreeNode.CONTENT_FACET_KEY));
412: //mLogger.info("Image facet " + selNode.getFacet(TreeNode.IMAGE_FACET_KEY));
413:
414: //UIComponent comp = selNode.getFacet(TreeNode.IMAGE_FACET_KEY);
415:
416: TextField field = new TextField();
417:
418: field.setId("_editTxt");
419: field.setDisabled(false);
420: field.setVisible(true);
421: field.setValue(selNode.getText());
422: field.setText(selNode.getText());
423: field.setOnBlur("this.form.submit()");
424: field.addValueChangeListener(this );
425: field.addValidator(this );
426: selNode.setText(" ");
427: selNode.setUrl("#");
428:
429: //MethodBinding binding = getFacesContext().getCurrentInstance().getApplication().createMethodBinding("#{ChartTreeBean.renameTextBoxAction}",null);
430: //selNode.setAction(binding);
431:
432: // comp.getChildren().add(field);
433: selNode.getFacets().put(TreeNode.CONTENT_FACET_KEY,
434: field);
435: //mLogger.info("The facet is" + comp.getId());
436:
437: }
438: }
439: return "success";
440: }
441:
442: public String removeSelectedNode() {
443:
444: /// TO DO all of it ....
445: mLogger.info("Remove node called ");
446: setErrorMessage("remove method is not implemented.");
447: return "success";
448: }
449:
450: private void setErrorMessage(String error) {
451: ErrorInfoBean infoBean = (ErrorInfoBean) getBean("ErrorInfoBean");
452: infoBean.populate("ChartTree", error);
453: // Need to implement.
454: //mLogger.info("Set Error " + error);
455: }
456:
457: private TreeNode findSelectedNode() {
458: String selectedNodeId = null;
459: TreeNode selNode = null;
460: if (rootNode.getParent() instanceof Tree) {
461: Tree tr = (Tree) rootNode.getParent();
462:
463: selectedNodeId = tr.getCookieSelectedTreeNode();
464: mLogger.info("get Selected is " + selectedNodeId);
465: }
466: if (selectedNodeId != null) {
467: selNode = findNodeFromSelectedNodeIDString(selectedNodeId);
468: }
469: return selNode;
470: }
471:
472: private TreeNode findNodeFromSelectedNodeIDString(String str) {
473: String[] strs = str.split(":");
474: int index = 0;
475: for (int i = strs.length; i > 0; i--) {
476: if (strs[i - 1].equals("rootNode")) {
477: index = i - 1;
478: }
479: }
480: TreeNode t = null;
481: TreeNode temp = null;
482: t = rootNode;
483: for (int k = index + 1; k < strs.length; k++) {
484: temp = findNodeAmongChildren(t, strs[k]);
485: if (temp != null) {
486: t = temp;
487: }
488: }
489: return t;
490: }
491:
492: private TreeNode findNodeAmongChildren(TreeNode node, String id) {
493:
494: if (node.getChildCount() > 0) {
495: Iterator iter = node.getChildren().iterator();
496: while (iter.hasNext()) {
497: UIComponent com = (UIComponent) iter.next();
498: if (com.getId().equals(id)) {
499:
500: return (TreeNode) com;
501: }
502: }
503: }
504: return null;
505: }
506:
507: public void processValueChange(ValueChangeEvent event) {
508: UIComponent comp1 = event.getComponent().getParent();
509: TreeNode node = (TreeNode) comp1;
510: UIComponent comp = node.getFacet(TreeNode.CONTENT_FACET_KEY);
511: TextField field = (TextField) comp;
512: String name = (String) field.getText();
513: node.getFacets().remove(TreeNode.CONTENT_FACET_KEY);
514: node.setText(name);
515: ChartBean b = getChartApplicationBeansContainer()
516: .getChartBeanByID(node.getId());
517: if (b != null) {
518: b.setChartName(name);
519: node.setUrl(this .getURL(b));
520: } else {
521: node.setUrl("#");
522: }
523:
524: }
525:
526: public void validate(FacesContext context, UIComponent component,
527: Object value) {
528: String str = (String) value;
529: if (str.startsWith("@")) {
530: throw new ValidatorException(new FacesMessage(
531: "Not a valid value!"));
532: } else {
533: UIComponent comp1 = component.getParent();
534: TreeNode node = (TreeNode) comp1;
535: UIComponent comp = node
536: .getFacet(TreeNode.CONTENT_FACET_KEY);
537: TextField field = (TextField) comp;
538: if (((String) field.getText()).equals(str)) {
539: // No change so go ahead with component adjustment else let the value change listener do adjustment.
540: String name = str;
541: node.setText(name);
542: node.getFacets().remove(TreeNode.CONTENT_FACET_KEY);
543: ChartBean b = getChartApplicationBeansContainer()
544: .getChartBeanByID(node.getId());
545: if (b != null) {
546: b.setChartName(name);
547: node.setUrl(this .getURL(b));
548: } else {
549: node.setUrl("#");
550: }
551: }
552: }
553:
554: }
555:
556: public String addNewDataAccessNode() {
557: String selectedNodeId = null;
558: TreeNode selNode = null;
559: if (rootNode.getParent() instanceof Tree) {
560: Tree tr = (Tree) rootNode.getParent();
561: mLogger.info("get Selected object is "
562: + tr.getCookieSelectedTreeNode());
563: selectedNodeId = tr.getCookieSelectedTreeNode();
564: mLogger.info("get Selected is " + selectedNodeId);
565: }
566: if (selectedNodeId == null) {
567: setErrorMessage("Please select a Data Access group in order to add a new DA Object.");
568: } else {
569: selNode = findNodeFromSelectedNodeIDString(selectedNodeId);
570: if (selNode == null) {
571: setErrorMessage("Error :: The node is not found.");
572: } else {
573: mLogger.info("Slected node id is " + selNode.getId());
574: if (selNode.getId().startsWith("__DAG")) {
575: String name = createNewChildDANode(selNode);
576: //getChartDataPersistence().resetForNewChart(name,(String)selNode.getText());
577: //ChartBean bean = getChartDataPersistence().getChartInfoBean();
578: DataBean bean = getChartApplicationBeansContainer()
579: .createNewDataBean(name, selNode.getText());
580:
581: TreeNode n = createNode(bean.getID(), bean
582: .getDisplayName(), C_NODE_IMAGE,
583: getURL(bean), TARGET_FRAME);
584:
585: addNode(selNode, n);
586: } else {
587: setErrorMessage("Please select a DA Group to add a DA Object.");
588: }
589: }
590: }
591: return "success";
592:
593: }
594:
595: private String createNewChildDANode(TreeNode n) {
596: if (n.getChildCount() == 0) {
597: return "New Data Access";
598: } else {
599: int ind = 0;
600: Iterator iter = n.getChildren().iterator();
601: while (iter.hasNext()) {
602: Object o = iter.next();
603: if (o instanceof TreeNode) {
604: TreeNode t = (TreeNode) o;
605: if (t.getText().startsWith("New Data Access")) {
606: ind++;
607: }
608: }
609: }
610: if (ind == 0) {
611: return "New Data Access";
612: } else {
613: return "New Data Access_" + ind;
614: }
615: }
616: }
617:
618: private String getURL(DataBean b) {
619:
620: String url = "/faces/configure/processTreeNodeClick.jsp";
621: if (b == null) {
622:
623: //url = "/faces/layout/preChartConfiguration.jsp";
624: } else {
625: // url = "/faces/layout/preChartConfiguration.jsp?_chartUID="+b.getChartID();
626: url += "?_daUID=" + b.getID() + "&rdm=" + Math.random();
627: }
628:
629: return url;
630: }
631: }
|