View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.student.lum.lu.ui.tools.client.widgets;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.LinkedHashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.kuali.student.common.ui.client.application.Application;
25  import org.kuali.student.common.ui.client.application.ViewContext;
26  import org.kuali.student.common.ui.client.configurable.mvc.WidgetConfigInfo;
27  import org.kuali.student.common.ui.client.mvc.Callback;
28  import org.kuali.student.common.ui.client.mvc.Controller;
29  import org.kuali.student.common.ui.client.widgets.layout.VerticalFlowPanel;
30  import org.kuali.student.common.ui.shared.IdAttributes.IdType;
31  import org.kuali.student.r1.common.assembly.data.LookupMetadata;
32  import org.kuali.student.r1.common.assembly.data.LookupParamMetadata;
33  
34  import com.google.gwt.user.client.Window;
35  import com.google.gwt.user.client.ui.Composite;
36  
37  public class KSBrowser extends Composite {
38  
39  	private List<BrowsePanel> browsePanels;
40  	private LookupMetadata fieldLookup;
41  	private List<LookupMetadata> cascadingLookups;
42  	private List<LookupParamMetadata> cascadingChildParameters;
43  	private WidgetConfigInfo config;
44  	private VerticalFlowPanel layout = new VerticalFlowPanel ();
45  
46  	public KSBrowser (LookupMetadata fieldLookup, Controller controller)
47  	{
48  		this.fieldLookup = fieldLookup;
49  
50  		if (config == null)
51  		{
52  			config = new WidgetConfigInfo ();
53  		}
54  
55  		browsePanels = new ArrayList<BrowsePanel> ();
56  		cascadingLookups = new ArrayList<LookupMetadata> ();
57  		cascadingChildParameters = new ArrayList<LookupParamMetadata> ();
58  		cascadingLookups.add (this.fieldLookup);
59  		this.addChildLookups (this.fieldLookup);
60  		Collections.reverse (cascadingChildParameters);
61  		Collections.reverse (cascadingLookups);
62  
63  		for (LookupMetadata lookupMetadata : cascadingLookups)
64  		{
65  			BrowsePanel browsePanel = new BrowsePanel (lookupMetadata);
66  			browsePanels.add (browsePanel);
67  			layout.add (browsePanel);
68  		}
69  		for (int i = 0; i < browsePanels.size () - 1; i ++)
70  		{
71  			BrowsePanel currentBP = browsePanels.get (i);
72  			LookupParamMetadata nextParamMetadata = this.cascadingChildParameters.get (i);
73  			BrowsePanel nextBP = browsePanels.get (i + 1);
74  			currentBP.setOnSelectectedCallback (new ExecuteNextSearchCallback (nextBP, nextParamMetadata));
75  		}
76  		browsePanels.get (browsePanels.size () - 1).setOnSelectectedCallback (new ViewCourseCallback (controller));
77  
78  		this.initWidget (layout);
79  		browsePanels.get (0).executeSearch (new Callback<Boolean>(){
80  			public void exec(Boolean result) {
81  			}
82  		});
83  	}
84  
85  	private class ExecuteNextSearchCallback implements BrowsePanel.OnSelectedCallback {
86  
87  		private BrowsePanel nextBrowsePanel;
88  		private LookupParamMetadata nextParamMetadata;
89  
90  		public ExecuteNextSearchCallback (BrowsePanel nextBrowsePanel,
91  				LookupParamMetadata nextParamMetadata) {
92  			this.nextBrowsePanel = nextBrowsePanel;
93  			this.nextParamMetadata = nextParamMetadata;
94  		}
95  
96  		@Override
97  		public void selected (List<String> selectedIds)	{
98  			if (selectedIds.size () == 0) {
99  				Window.alert ("Please select a row before clicking");
100 				return;
101 			}
102 			Map<String, Object> parameters = new LinkedHashMap<String, Object> ();
103 			parameters.put (nextParamMetadata.getKey (), selectedIds.get (0));
104 			nextBrowsePanel.setParameters (parameters);
105 			nextBrowsePanel.executeSearch (new Callback<Boolean>(){
106 				public void exec(Boolean result) {
107 				}
108 			});
109 		}
110 	}
111 
112 	private class ViewCourseCallback implements BrowsePanel.OnSelectedCallback {
113 
114 		private Controller controller;
115 
116 		public ViewCourseCallback (Controller controller)
117 		{
118 			this.controller = controller;
119 		}
120 
121 		@Override
122 		public void selected (List<String> selectedIds)	{
123 			if (selectedIds.size () == 0) {
124 				Window.alert ("Please select a row before clicking");
125 				return;
126 			}
127 			ViewContext viewContext = new ViewContext ();
128 			viewContext.setId (selectedIds.get (0));
129 			viewContext.setIdType (IdType.OBJECT_ID);
130 			Application.navigate("/HOME/CURRICULUM_HOME/VIEW_COURSE", viewContext);
131 		}
132 	}
133 
134 	private void addChildLookups (LookupMetadata current) {
135 		for (LookupParamMetadata param : current.getParams ())	{
136 			if (param.getChildLookup () != null) {
137 				cascadingChildParameters.add (param);
138 				cascadingLookups.add (param.getChildLookup ());
139 				addChildLookups (param.getChildLookup ());
140 			}
141 		}
142 	}
143 
144 }