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.assembly.data.LookupMetadata;
25  import org.kuali.student.common.assembly.data.LookupParamMetadata;
26  import org.kuali.student.common.ui.client.application.Application;
27  import org.kuali.student.common.ui.client.application.ViewContext;
28  import org.kuali.student.common.ui.client.configurable.mvc.WidgetConfigInfo;
29  import org.kuali.student.common.ui.client.mvc.Controller;
30  import org.kuali.student.common.ui.client.widgets.KSErrorDialog;
31  import org.kuali.student.common.ui.client.widgets.layout.VerticalFlowPanel;
32  import org.kuali.student.common.ui.shared.IdAttributes.IdType;
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 ();
80  	}
81  
82  	private class ExecuteNextSearchCallback implements BrowsePanel.OnSelectedCallback {
83  
84  		private BrowsePanel nextBrowsePanel;
85  		private LookupParamMetadata nextParamMetadata;
86  
87  		public ExecuteNextSearchCallback (BrowsePanel nextBrowsePanel,
88  				LookupParamMetadata nextParamMetadata) {
89  			this.nextBrowsePanel = nextBrowsePanel;
90  			this.nextParamMetadata = nextParamMetadata;
91  		}
92  
93  		@Override
94  		public void selected (List<String> selectedIds)	{
95  			if (selectedIds.size () == 0) {
96  				Window.alert ("Please select a row before clicking");
97  				return;
98  			}
99  			Map<String, Object> parameters = new LinkedHashMap<String, Object> ();
100 			parameters.put (nextParamMetadata.getKey (), selectedIds.get (0));
101 			nextBrowsePanel.setParameters (parameters);
102 			nextBrowsePanel.executeSearch ();
103 		}
104 	}
105 
106 	private class ViewCourseCallback implements BrowsePanel.OnSelectedCallback {
107 
108 		private Controller controller;
109 
110 		public ViewCourseCallback (Controller controller)
111 		{
112 			this.controller = controller;
113 		}
114 
115 		@Override
116 		public void selected (List<String> selectedIds)	{
117 			if (selectedIds.size () == 0) {
118 				Window.alert ("Please select a row before clicking");
119 				return;
120 			}
121 			ViewContext viewContext = new ViewContext ();
122 			viewContext.setId (selectedIds.get (0));
123 			viewContext.setIdType (IdType.OBJECT_ID);
124 			Application.navigate("/HOME/CURRICULUM_HOME/VIEW_COURSE", viewContext);
125 		}
126 	}
127 
128 	private void addChildLookups (LookupMetadata current) {
129 		for (LookupParamMetadata param : current.getParams ())	{
130 			if (param.getChildLookup () != null) {
131 				cascadingChildParameters.add (param);
132 				cascadingLookups.add (param.getChildLookup ());
133 				addChildLookups (param.getChildLookup ());
134 			}
135 		}
136 	}
137 
138 }