View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the Educational Community License, Version 2.0 (the "License"); you may
3    * not use this file except in compliance with the License. You may obtain a copy of the License at
4    * http://www.osedu.org/licenses/ECL-2.0 Unless required by applicable law or agreed to in writing, software distributed
5    * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
6    * implied. See the License for the specific language governing permissions and limitations under the License.
7    */
8   
9   package org.kuali.student.common.ui.client.util;
10  
11  import java.util.ArrayList;
12  import java.util.List;
13  
14  import org.kuali.student.common.ui.client.configurable.mvc.FieldDescriptor;
15  import org.kuali.student.common.ui.client.configurable.mvc.SectionTitle;
16  import org.kuali.student.common.ui.client.configurable.mvc.sections.BaseSection;
17  import org.kuali.student.common.ui.client.configurable.mvc.sections.Section;
18  import org.kuali.student.common.ui.client.configurable.mvc.sections.WarnContainer;
19  import org.kuali.student.common.ui.client.configurable.mvc.views.VerticalSectionView;
20  import org.kuali.student.common.ui.client.mvc.Controller;
21  import org.kuali.student.common.ui.client.reporting.ReportExportWidget;
22  import org.kuali.student.common.ui.client.widgets.KSButton;
23  import org.kuali.student.common.ui.client.widgets.KSItemLabel;
24  import org.kuali.student.common.ui.client.widgets.ULPanel;
25  import org.kuali.student.common.ui.client.widgets.field.layout.element.FieldElement;
26  import org.kuali.student.common.ui.client.widgets.list.KSLabelList;
27  import org.kuali.student.common.ui.client.widgets.list.KSSelectedList;
28  import org.kuali.student.common.ui.client.widgets.menus.KSListPanel;
29  import org.kuali.student.common.ui.client.widgets.search.KSPicker;
30  import org.kuali.student.common.ui.client.widgets.table.summary.SummaryTable;
31  import org.kuali.student.common.ui.client.widgets.table.summary.SummaryTableBlock;
32  import org.kuali.student.common.ui.client.widgets.table.summary.SummaryTableModel;
33  import org.kuali.student.common.ui.client.widgets.table.summary.SummaryTableRow;
34  
35  import com.google.gwt.core.client.GWT;
36  import com.google.gwt.user.client.ui.ComplexPanel;
37  import com.google.gwt.user.client.ui.FlowPanel;
38  import com.google.gwt.user.client.ui.HasHTML;
39  import com.google.gwt.user.client.ui.HasText;
40  import com.google.gwt.user.client.ui.ListBox;
41  import com.google.gwt.user.client.ui.Widget;
42  import com.google.gwt.user.client.ui.WidgetCollection;
43  
44  /**
45   * 
46   * This is a description of what this class does - pctsw don't forget to fill this in. 
47   * 
48   * @author Kuali Rice Team (kuali-rice@googlegroups.com)
49   *
50   */
51  public class ExportUtils {
52      public static final String PDF = "PDF";
53      public static final String DOC = "DOC";
54      public static final String XLS = "XLS";
55      public static final String XML = "XML";
56      public static final String RTF = "RTF";
57      public static final String TEXT = "TEXT";
58  
59      /**
60       * 
61       * Inspect the given widget for the value and add it to the export element object.
62       * 
63       * @param exportItem
64       * @param fieldWidget
65       * @param setFirstFieldValue
66       * @param viewName
67       * @param sectionName
68       * @return
69       */
70      public static ExportElement getExportItemDetails(ExportElement exportItem, Widget fieldWidget, boolean setFirstFieldValue, String viewName, String sectionName) {
71          
72          // Do not display the widget data if it is not visible on the screen.
73          if (!fieldWidget.getParent().getElement().getStyle().getDisplay().equals("none")){
74              
75              if (fieldWidget instanceof HasHTML) {
76                  // HasHTML...
77                  HasHTML itemHasHTML = (HasHTML) fieldWidget;
78                  setFieldValue(exportItem, setFirstFieldValue, itemHasHTML.getHTML());
79                  
80              } else if (fieldWidget instanceof HasText) {
81                  // Hastext...
82                  HasText itemHasTextValue = (HasText) fieldWidget;
83                  setFieldValue(exportItem, setFirstFieldValue, itemHasTextValue.getText());
84  
85              } else if (fieldWidget instanceof KSSelectedList) {
86                  KSSelectedList selectedList = (KSSelectedList) fieldWidget;
87                  List<KSItemLabel> selectedItems = selectedList.getSelectedItems();
88                  String values = new String();
89                  for (int j = 0; j < selectedItems.size(); j++) {
90                      values = selectedItems.get(j).getDisplayText();
91                  }
92                  setFieldValue(exportItem, setFirstFieldValue, values);
93                  
94              } else if (fieldWidget instanceof KSPicker) {
95                  KSPicker picker = (KSPicker) fieldWidget;
96                  if (picker.getInputWidget() instanceof HasText) {
97                      HasText item = (HasText) picker.getInputWidget();
98                      setFieldValue(exportItem, setFirstFieldValue, item.getText());
99                  } else if (picker.getInputWidget() instanceof KSLabelList) {
100                     String fieldValue = null;
101                     KSLabelList widget = (KSLabelList) picker.getInputWidget();
102                     List<String> selected = widget.getSelectedItemsForExport();
103                     for (int j = 0; j < selected.size(); j++) {
104                         if (fieldValue == null) {
105                             fieldValue = new String(selected.get(j));
106                         } else {
107                             fieldValue = fieldValue + ", " + selected.get(j);
108                         }
109                     }
110                     setFieldValue(exportItem, setFirstFieldValue, fieldValue);
111                 }
112                 
113             } else if (fieldWidget instanceof ListBox) {
114                 // Get the selected element from a list box.
115                 ListBox listBox = (ListBox) fieldWidget;
116                 setFieldValue(exportItem, setFirstFieldValue, listBox.getItemText(listBox.getSelectedIndex()));
117             
118             } else if (fieldWidget instanceof SectionTitle) {
119                 try {
120                     // Retrieve the title from sections.
121                     SectionTitle sectionTitle = (SectionTitle) fieldWidget;
122                     setFieldValue(exportItem, setFirstFieldValue, sectionTitle.getExportFieldValue());
123                     // If the value does not already contain the bold html tags, set the print type to bold.
124                     if (!exportItem.getValue().contains("<b>")){
125                         exportItem.setPrintType(ExportElement.BOLD);
126                     }
127                 } catch (Exception e) {
128                     // ignore, section tile interface problem - only in debugging.");
129                 }
130                 
131             } else if (fieldWidget instanceof SummaryTable) {
132                 // Call custom details for widget method for summary tables.
133                 if (fieldWidget.isVisible()){
134                     exportItem.setSubset(ExportUtils.getDetailsForWidget((SummaryTable)fieldWidget));
135                 }
136         
137             } else if (fieldWidget instanceof ReportExportWidget) {
138                 // Retrieve custom implementation data of report export widget.
139         	    if (fieldWidget.isVisible()){
140                     ReportExportWidget widget = (ReportExportWidget) fieldWidget;
141                     if (widget.isExportElement() ) {
142                         exportItem.setSubset(widget.getExportElementSubset(exportItem));
143                         setFieldValue(exportItem, setFirstFieldValue, widget.getExportFieldValue());
144                     }
145                 }
146         
147             } else if (fieldWidget instanceof ComplexPanel) {
148                 // Retrieve child elements from complex panel.
149                 if(fieldWidget.isVisible()) {
150                     exportItem.setSubset(ExportUtils.getDetailsForWidget(fieldWidget, viewName, sectionName));
151                 }
152 
153             } else if (fieldWidget instanceof FieldElement) {
154                 // Retrieve subset from the widget on the field element.
155                 if(fieldWidget.isVisible()) {
156                     Widget widget = ((FieldElement)fieldWidget).getFieldWidget();
157                     exportItem = getExportItemDetails(exportItem, widget, true, viewName, sectionName);
158                     exportItem.setPrintType(ExportElement.PARAGRAPH);
159                 }
160 
161             } else {
162                 // don't set anything
163             }
164         
165             
166         }
167         return exportItem;
168     }
169 
170 
171     /**
172      * 
173      * This method sets the extracted string value.
174      * 
175      * @param exportItem
176      * @param setFirstFieldValue
177      * @param fieldValue
178      */
179     private static void setFieldValue(ExportElement exportItem, boolean setFirstFieldValue, String fieldValue) {
180         if (setFirstFieldValue) {
181             exportItem.setFieldValue(fieldValue);
182         } else {
183             exportItem.setFieldValue2(fieldValue);
184         }
185     }
186     
187     
188 
189     /**
190      * This method gets the current controller based on the widget that was passed to it.
191      * 
192      * @param theWidget
193      * @return currentController
194      */
195     public static Controller getController(Widget theWidget) {
196         // TODO Nina - This can't be the correct way of getting handle to
197         // Controller, isn't there a better way??
198         if (theWidget != null) {
199 
200             if (theWidget instanceof Controller) {
201                 Controller controller = (Controller) theWidget;
202                 return controller;
203             } else {
204                 return getController(theWidget.getParent());
205             }
206         } else {
207             return null;
208         }
209     }
210 
211     /**
212      * Handles the export click event.
213      * 
214      * @param currentController
215      * @param format
216      * @param reportTitle
217      */
218     public static void handleExportClickEvent(Controller currentController, String format, String reportTitle) {
219         List<ExportElement> exportElements = new ArrayList<ExportElement>();
220         exportElements = currentController.getExportElementsFromView();
221         if (exportElements != null && exportElements.size() > 0) {
222             currentController.doReportExport(exportElements, format, reportTitle);
223         }
224     }
225 
226     /**
227      * 
228      * Retrieve the sub elements from the summary table on a table section.
229      * 
230      * @param tableSection
231      * @param exportElements
232      * @return
233      */
234     public static List<ExportElement> getDetailsForWidget(SummaryTable sumTable) {
235         List<ExportElement> exportElements = new ArrayList<ExportElement>();
236         SummaryTableModel model = sumTable.getModel();
237         
238         // Loop through the different sections. 
239         for (SummaryTableBlock block : model.getSectionList()) {
240             String blockName = block.getTitle();
241             
242             // Loop through all the rows.
243             for (SummaryTableRow row : block.getSectionRowList()) {
244                 ExportElement element = GWT.create(ExportElement.class);
245 				element.setSectionName(blockName);
246 				element.setViewName(blockName);
247 				element.setFieldLabel(row.getTitle());
248 				element.setMandatory(row.isRequired());
249                 
250 				// Only add elements if row is visible.
251                 if (row.isShown()) {
252                     // Add the first element of the row.
253                     element = setSummaryElement(blockName, row, element, row.getCell1(), true);
254                     // Add the second element of the row.
255                     element = setSummaryElement(blockName, row, element, row.getCell2(), false);
256                     // All element to list.
257                     if (element != null && element.getViewName() != null) {
258                         exportElements.add(element);
259                     }
260                 }
261                 
262             }
263         }
264         return exportElements;
265     }
266 
267     /**
268      * Set the details for the summary element.
269      * 
270      * @param blockName
271      * @param row
272      * @param element
273      * @param fdWidget
274      * @param setFirstFieldValue
275      * @return
276      */
277     private static ExportElement setSummaryElement(String blockName, SummaryTableRow row, ExportElement element, Widget fdWidget, boolean setFirstFieldValue) {
278         if (fdWidget != null) {
279             if (fdWidget instanceof KSListPanel) {
280                 element.setSubset(ExportUtils.getDetailsForWidget(fdWidget, blockName, blockName));
281             } else {
282                 element = ExportUtils.getExportItemDetails(element, fdWidget, setFirstFieldValue, blockName, blockName);
283             }
284         } else {
285             if (row.getTitle() != null) {
286                 element.setFieldLabel(row.getTitle());
287             }
288         }
289         return element;
290     }
291 
292     /**
293      * 
294      * Retrieves the sub elements from a container widget.
295      * 
296      * @param currentViewWidget
297      * @param viewName
298      * @param sectionName
299      * @return
300      */
301     public static List<ExportElement> getDetailsForWidget(Widget currentViewWidget, String viewName, String sectionName) {
302         List<ExportElement> childElements = new ArrayList<ExportElement>();
303         if (!currentViewWidget.getParent().getElement().getStyle().getDisplay().equals("none")){
304             if (currentViewWidget instanceof Section) {
305                 Section widgetHasFields = (Section) currentViewWidget;
306                 List<FieldDescriptor> widgetFields = widgetHasFields.getFields();
307                 for (FieldDescriptor field : widgetFields) {
308                     ExportElement exportItem = createExportElement(viewName, sectionName, childElements, field.getFieldElement().getFieldWidget());
309                     exportItem.setFieldLabel(field.getFieldLabel());
310                 }
311             } else if (currentViewWidget instanceof KSListPanel) {
312                 KSListPanel ksListPanelWidget = (KSListPanel) currentViewWidget;
313                 WidgetCollection children = ksListPanelWidget.getChildren();
314                 for (int i = 0; i < children.size(); i++) {
315                     createExportElement(viewName, sectionName, childElements, children.get(i));
316                 }
317 
318             } else if (currentViewWidget instanceof ULPanel) {
319                 ComplexPanel complexPanel = (ComplexPanel) currentViewWidget;
320                 if (complexPanel.isVisible()){
321                     for (int i = 0; i < complexPanel.getWidgetCount(); i++) {
322                         Widget child = complexPanel.getWidget(i);
323                         if (child instanceof FlowPanel){
324                             List<ExportElement> subset = ExportUtils.getDetailsForWidget(child, viewName, sectionName);
325                             if (subset != null && subset.size() > 0){
326                                 subset.get(0).setPrintType(ExportElement.LIST);
327                                 childElements.addAll(subset);
328                             }
329                             
330                         } else if (!(child instanceof KSButton)
331                                 && !(child instanceof WarnContainer)) {
332                             ExportElement exportItem = createExportElement(viewName, sectionName, childElements, child);
333                             exportItem.setPrintType(ExportElement.LIST);
334                         }
335                     }
336                 }
337                 
338             } else if (currentViewWidget instanceof ComplexPanel) {
339                 ComplexPanel complexPanel = (ComplexPanel) currentViewWidget;
340                 if (complexPanel.isVisible()){
341                     for (int i = 0; i < complexPanel.getWidgetCount(); i++) {
342                         Widget child = complexPanel.getWidget(i);
343                         if (child instanceof FlowPanel){
344                             List<ExportElement> subset = ExportUtils.getDetailsForWidget(child, viewName, sectionName);
345                             if (subset != null && subset.size() > 0){
346                                 childElements.addAll(subset);
347                             }
348                             
349                         } else if (!(child instanceof KSButton)
350                                 && !(child instanceof WarnContainer)) {
351                             createExportElement(viewName, sectionName, childElements, child);
352                         }
353                     }
354                 }
355             } else {
356 
357                 System.out.println("ExportUtils does not cater for this type..." + currentViewWidget.getClass().getName());
358 
359             }
360         }
361         return childElements;
362     }
363 
364     // Added for 2nd widget (side-by-side comparison)
365     public static List<ExportElement> getDetailsForWidget(ExportElement element, Widget currentViewWidget, boolean setFirstFieldValue, String viewName, String sectionName) {
366         List<ExportElement> childElements = new ArrayList<ExportElement>();
367         if (!currentViewWidget.getParent().getElement().getStyle().getDisplay().equals("none")){
368             if (currentViewWidget instanceof Section) {
369                 Section widgetHasFields = (Section) currentViewWidget;
370                 List<FieldDescriptor> widgetFields = widgetHasFields.getFields();
371                  for (FieldDescriptor field : widgetFields) {
372                      ExportElement exportItem = createExportElement(viewName, sectionName, childElements, field.getFieldElement().getFieldWidget());
373                      exportItem.setFieldLabel(field.getFieldLabel());
374                 }
375             } else if (currentViewWidget instanceof KSListPanel) {
376                 KSListPanel ksListPanelWidget = (KSListPanel) currentViewWidget;
377                 WidgetCollection children = ksListPanelWidget.getChildren();
378                 for (int i = 0; i < children.size(); i++) {
379                 	if (!element.getSubset().isEmpty() && i < element.getSubset().size()) 
380                 		createExportElementOld(element.getSubset().get(i), viewName, sectionName, childElements, children.get(i)); 
381                 	else	
382                 		createExportElement2(viewName, sectionName, childElements, children.get(i));
383                 }
384             } else if (currentViewWidget instanceof ULPanel) {
385                 ComplexPanel complexPanel = (ComplexPanel) currentViewWidget;
386                 if (complexPanel.isVisible()){
387                     for (int i = 0; i < complexPanel.getWidgetCount(); i++) {
388                         Widget child = complexPanel.getWidget(i);
389                         if (child instanceof FlowPanel){
390                             List<ExportElement> subset = ExportUtils.getDetailsForWidget(child, viewName, sectionName);
391                             if (subset != null && subset.size() > 0){
392                                 subset.get(0).setPrintType(ExportElement.LIST);
393                                 childElements.addAll(subset);
394                             }
395                             
396                         } else if (!(child instanceof KSButton)
397                                 && !(child instanceof WarnContainer)) {
398                             ExportElement exportItem = createExportElement(viewName, sectionName, childElements, child);
399                             exportItem.setPrintType(ExportElement.LIST);
400                         }
401                     }
402                 }
403                 
404             } else if (currentViewWidget instanceof ComplexPanel) {
405                 ComplexPanel complexPanel = (ComplexPanel) currentViewWidget;
406                 if (complexPanel.isVisible()){
407                     for (int i = 0; i < complexPanel.getWidgetCount(); i++) {
408                         Widget child = complexPanel.getWidget(i);
409                         if (child instanceof FlowPanel){
410                             List<ExportElement> subset = ExportUtils.getDetailsForWidget(child, viewName, sectionName);
411                             if (subset != null && subset.size() > 0){
412                                 childElements.addAll(subset);
413                             }
414                             
415                         } else if (!(child instanceof KSButton)
416                                 && !(child instanceof WarnContainer)) {
417                             createExportElement(viewName, sectionName, childElements, child);
418                         }
419                     }
420                 }
421             } else {
422 
423                 System.out.println("ExportUtils does not cater for this type..." + currentViewWidget.getClass().getName());
424 
425             }
426         }
427         return childElements;
428     }
429 
430     public static ArrayList<ExportElement> getExportElementsFromView(Widget currentViewWidget, ArrayList<ExportElement> exportElements, String viewName, String sectionName) {
431         if (exportElements == null) {
432             exportElements = new ArrayList<ExportElement>();
433         }
434         if (currentViewWidget.getParent() == null || !currentViewWidget.getParent().getElement().getStyle().getDisplay().equals("none")) {
435             if (currentViewWidget instanceof VerticalSectionView) {
436                 Section widgetHasFields = (Section) currentViewWidget;
437                 List<FieldDescriptor> widgetFields = widgetHasFields.getFields();
438                 for (FieldDescriptor field : widgetFields) {
439                     Widget child = field.getFieldElement().getFieldWidget();
440                     ExportElement exportItem = createExportElement(viewName, sectionName, exportElements, child);
441                     exportItem.setFieldLabel(field.getFieldLabel());
442                 }
443                 if ((currentViewWidget instanceof BaseSection) && (widgetHasFields.getFields().size() == 0)) {
444                     BaseSection bSection = (BaseSection) currentViewWidget;
445                     createExportElement(viewName, sectionName, exportElements, bSection.getLayout());
446                 }
447             }
448         }
449         return exportElements;
450     }
451     
452     /**
453      * 
454      * Creates a new export element with its sub elements.
455      * 
456      * @param viewName
457      * @param sectionName
458      * @param childElements
459      * @param child
460      * @return
461      */
462     private static ExportElement createExportElement(String viewName, String sectionName, List<ExportElement> childElements, Widget child) {
463         ExportElement exportItem = GWT.create(ExportElement.class);
464         exportItem.setSectionName(sectionName);
465         exportItem.setViewName(viewName);
466                             
467         exportItem = getExportItemDetails(exportItem, child, true, viewName, sectionName);
468         ExportUtils.addElementToElementArray(childElements, exportItem);
469         return exportItem;
470     }
471 
472     private static ExportElement createExportElement2(String viewName, String sectionName, List<ExportElement> childElements, Widget child) {
473         ExportElement exportItem = GWT.create(ExportElement.class);
474         exportItem.setSectionName(sectionName);
475         exportItem.setViewName(viewName);
476                             
477         exportItem = getExportItemDetails(exportItem, child, false, viewName, sectionName);
478         ExportUtils.addElementToElementArray(childElements, exportItem);
479         return exportItem;
480     }
481 
482     private static ExportElement createExportElementOld(ExportElement exportItem, String viewName, String sectionName, List<ExportElement> childElements, Widget child) {
483         exportItem = getExportItemDetails(exportItem, child, false, viewName, sectionName);
484         ExportUtils.addElementToElementArray(childElements, exportItem);
485         return exportItem;
486     }
487 
488     // Only add element if it is not null
489     public static List<ExportElement> addElementToElementArray(List<ExportElement> elementArray, ExportElement element) {
490         if (element.getFieldLabel() != null || element.getFieldValue() != null || element.getFieldValue2() != null || (element.getSubset() != null && element.getSubset().size() > 0)) {
491             elementArray.add(element);
492         }
493         return elementArray;
494     }
495 
496 }