View Javadoc

1   package org.kuali.student.lum.lu.ui.browseprogram.client.views;
2   
3   import java.util.ArrayList;
4   import java.util.Arrays;
5   import java.util.HashSet;
6   import java.util.List;
7   import java.util.Map;
8   import java.util.Map.Entry;
9   
10  import org.kuali.student.common.assembly.data.LookupMetadata;
11  import org.kuali.student.common.assembly.data.Metadata;
12  import org.kuali.student.common.ui.client.application.Application;
13  import org.kuali.student.common.ui.client.application.KSAsyncCallback;
14  import org.kuali.student.common.ui.client.application.ViewContext;
15  import org.kuali.student.common.ui.client.mvc.Callback;
16  import org.kuali.student.common.ui.client.mvc.Controller;
17  import org.kuali.student.common.ui.client.mvc.DataModelDefinition;
18  import org.kuali.student.common.ui.client.mvc.ViewComposite;
19  import org.kuali.student.common.ui.client.service.MetadataRpcService;
20  import org.kuali.student.common.ui.client.service.MetadataRpcServiceAsync;
21  import org.kuali.student.common.ui.client.widgets.field.layout.layouts.VerticalFieldLayout;
22  import org.kuali.student.common.ui.client.widgets.filter.FilterEvent;
23  import org.kuali.student.common.ui.client.widgets.filter.FilterEventHandler;
24  import org.kuali.student.common.ui.client.widgets.filter.FilterResetEventHandler;
25  import org.kuali.student.common.ui.client.widgets.filter.KSFilterOptions;
26  import org.kuali.student.common.ui.client.widgets.headers.KSDocumentHeader;
27  import org.kuali.student.common.ui.client.widgets.layout.HorizontalTableCellPanel;
28  import org.kuali.student.common.ui.client.widgets.progress.BlockingTask;
29  import org.kuali.student.common.ui.client.widgets.progress.KSBlockingProgressIndicator;
30  import org.kuali.student.common.ui.client.widgets.searchtable.ResultRow;
31  import org.kuali.student.common.ui.shared.IdAttributes.IdType;
32  import org.kuali.student.lum.lu.ui.tools.client.widgets.BrowsePanel;
33  
34  import com.google.gwt.core.client.GWT;
35  import com.google.gwt.user.client.Window;
36  
37  
38  public class BrowseProgramView extends ViewComposite {
39      
40  	private final BlockingTask initializingTask = new BlockingTask("Loading");
41      protected MetadataRpcServiceAsync metadataServiceAsync = GWT.create(MetadataRpcService.class);
42  	
43      private VerticalFieldLayout container = new VerticalFieldLayout();
44  	private HorizontalTableCellPanel layout = new HorizontalTableCellPanel();
45  	protected KSFilterOptions dependencyFilter;
46  	protected DataModelDefinition searchDefinition;
47  	protected boolean initialized = false;
48  	BrowsePanel browsePanel;
49  	
50  	public BrowseProgramView(Controller controller, String name,
51  			Enum<?> viewType) {
52  		super(controller, name, viewType);
53  	}
54  
55  	private void init() {
56  		initWidget(container);
57  		Metadata metaData;
58  		
59  		KSDocumentHeader header = new KSDocumentHeader();
60          header.setTitle("Browse Majors and Specializations");
61          
62          container.setTitleWidget(header);
63          
64          List<LookupMetadata> lookups = new ArrayList<LookupMetadata>();
65          metaData = searchDefinition.getMetadata("filter");
66          lookups.add(metaData.getInitialLookup());
67          
68          dependencyFilter = new KSFilterOptions(metaData.getAdditionalLookups());
69          dependencyFilter.addFilterEventHandler(new FilterEventHandler(){
70  			@Override
71  			public void onDeselect(FilterEvent e) {
72  				handleSelections(e.getSelections());
73  			}
74  			@Override
75  			public void onSelect(FilterEvent e) {
76  				handleSelections(e.getSelections());
77  			}
78         	
79          });
80          
81          dependencyFilter.addFilterResetEventHandler(new FilterResetEventHandler(){
82  			@Override
83  			public void onReset() {
84  				handleSelections(null);				
85  			}        	
86          });
87  		
88          layout.add(dependencyFilter);
89          
90  		metaData = searchDefinition.getMetadata("search");
91  		
92  		browsePanel = new BrowsePanel(metaData.getInitialLookup(),400);
93  
94  		browsePanel.setOnSelectectedCallback(new ViewCourseCallback());
95  		
96  		layout.add(browsePanel);
97  
98  		container.add(layout);
99  		
100 		browsePanel.executeSearch(new Callback<Boolean>(){
101 			@Override
102 			public void exec(Boolean result) {
103 			}
104 		});
105 
106 	}   
107 
108 	private void handleSelections(Map<String, List<String>> selections) {
109 		if(selections == null || selections.isEmpty()){
110 			browsePanel.showAllRows();
111 		}else{
112 			HashSet<String> rowKeys = new HashSet<String>();
113 			for(ResultRow resultRow:browsePanel.getAllResultRows()){
114 				boolean matches = true;
115 				for(Entry<String, List<String>> entry:selections.entrySet()){
116 					String entryKey = entry.getKey();
117 					String rowValue = resultRow.getValue(entryKey);
118 					List<String> entryValue = entry.getValue();
119 					if(entryValue!=null){
120 						if("lu.resultColumn.resultComponentId".equals(entryKey)||
121 	                       "lu.resultColumn.luOptionalCampusLocation".equals(entryKey)){
122 							String[] rowValues = rowValue.split("<br/>");
123 							ArrayList<String> intersection = new ArrayList<String>(entryValue);
124 							intersection.retainAll(Arrays.asList(rowValues));
125 							if(intersection.isEmpty()){
126 								matches = false;
127 								break;
128 							}
129 						}else if(!entryValue.contains(rowValue)){
130 							matches = false;
131 							break;
132 						}
133 					}
134 				}
135 				if(matches){
136 					rowKeys.add(resultRow.getId());
137 				}
138 			}
139 			
140 			browsePanel.showOnlyRows(rowKeys);
141 		}
142 		
143 
144 		
145 	}
146 	
147 	@Override
148 	public void beforeShow(final Callback<Boolean> onReadyCallback) {
149         if (!initialized) {
150     		KSBlockingProgressIndicator.addTask(initializingTask);
151 
152     		//This loads search definitions for the dependency analysis search 
153             metadataServiceAsync.getMetadata("browseProgram", null, null, new KSAsyncCallback<Metadata>(){
154 
155                 @Override
156                 public void handleFailure(Throwable caught) {
157                     KSBlockingProgressIndicator.removeTask(initializingTask);
158                     throw new RuntimeException("Failed to load search definitions.", caught);                        
159                 }
160 
161                 @Override
162                 public void onSuccess(Metadata result) {
163                 	searchDefinition = new DataModelDefinition(result);
164                 	init();
165                 	onReadyCallback.exec(true);                	
166                     initialized = true;
167                     KSBlockingProgressIndicator.removeTask(initializingTask);
168                 }
169             
170             });	        
171         } else {
172         	onReadyCallback.exec(true);
173         }
174 	}
175 	
176 	private class ViewCourseCallback implements BrowsePanel.OnSelectedCallback {
177 
178 		@Override
179 		public void selected (List<String> selectedIds)	{
180 			if (selectedIds.size () == 0) {
181 				Window.alert ("Please select a row before clicking");
182 				return;
183 			}
184 			ViewContext viewContext = new ViewContext ();
185 			viewContext.setId (selectedIds.get (0));
186 			viewContext.setIdType (IdType.OBJECT_ID);
187 			Application.navigate("/HOME/CURRICULUM_HOME/PROGRAM_VIEW", viewContext);
188 		}
189 	}
190 
191 
192 
193 
194 }