Coverage Report - org.kuali.student.common.ui.client.util.ExportUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ExportUtils
0%
0/169
0%
0/100
6.889
 
 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.KSLabel;
 25  
 import org.kuali.student.common.ui.client.widgets.list.KSLabelList;
 26  
 import org.kuali.student.common.ui.client.widgets.list.KSSelectedList;
 27  
 import org.kuali.student.common.ui.client.widgets.menus.KSListPanel;
 28  
 import org.kuali.student.common.ui.client.widgets.search.KSPicker;
 29  
 import org.kuali.student.common.ui.client.widgets.table.summary.SummaryTable;
 30  
 import org.kuali.student.common.ui.client.widgets.table.summary.SummaryTableBlock;
 31  
 import org.kuali.student.common.ui.client.widgets.table.summary.SummaryTableModel;
 32  
 import org.kuali.student.common.ui.client.widgets.table.summary.SummaryTableRow;
 33  
 import org.kuali.student.common.ui.client.widgets.table.summary.SummaryTableSection;
 34  
 
 35  
 import com.google.gwt.user.client.ui.ComplexPanel;
 36  
 import com.google.gwt.user.client.ui.HasText;
 37  
 import com.google.gwt.user.client.ui.ListBox;
 38  
 import com.google.gwt.user.client.ui.Widget;
 39  
 import com.google.gwt.user.client.ui.WidgetCollection;
 40  
 
 41  0
 public class ExportUtils {
 42  
     public static final String PDF = "PDF";
 43  
     public static final String DOC = "DOC";
 44  
     public static final String XLS = "XLS";
 45  
 
 46  
     public static ExportElement getExportItemDetails(ExportElement exportItem, Widget fieldWidget, boolean setFirstFieldValue, String viewName, String sectionName) {
 47  0
         String fieldValue = null;
 48  0
         if (fieldWidget instanceof HasText) {
 49  0
             HasText itemHasTextValue = (HasText) fieldWidget;
 50  0
             fieldValue = itemHasTextValue.getText();
 51  
 
 52  0
         } else if (fieldWidget instanceof KSLabel) {
 53  
             // ignore labels... as we're not interested
 54  0
         } else if (fieldWidget instanceof KSSelectedList) {
 55  0
             KSSelectedList selectedList = (KSSelectedList) fieldWidget;
 56  0
             List<KSItemLabel> selectedItems = selectedList.getSelectedItems();
 57  0
             String values = new String();
 58  0
             for (int j = 0; j < selectedItems.size(); j++) {
 59  0
                 values = selectedItems.get(j).getDisplayText();
 60  
             }
 61  0
             fieldValue = values;
 62  0
         } else if (fieldWidget instanceof KSPicker) {
 63  0
             KSPicker picker = (KSPicker) fieldWidget;
 64  0
             if (picker.getInputWidget() instanceof HasText) {
 65  0
                 HasText item = (HasText) picker.getInputWidget();
 66  0
                 fieldValue = item.getText();
 67  0
             } else if (picker.getInputWidget() instanceof KSLabelList) {
 68  0
                 KSLabelList widget = (KSLabelList) picker.getInputWidget();
 69  0
                 List<String> selected = widget.getSelectedItemsForExport();
 70  0
                 for (int j = 0; j < selected.size(); j++) {
 71  0
                     if (fieldValue == null) {
 72  0
                         fieldValue = new String(selected.get(j));
 73  
                     } else {
 74  0
                         fieldValue = fieldValue + ", " + selected.get(j);
 75  
                     }
 76  
                 }
 77  
             }
 78  0
         } else if (fieldWidget instanceof ListBox) {
 79  0
             ListBox listBox = (ListBox) fieldWidget;
 80  0
             fieldValue = listBox.getItemText(listBox.getSelectedIndex());
 81  0
         } else if (fieldWidget instanceof SectionTitle) {
 82  
             try {
 83  0
                 SectionTitle sectionTitle = (SectionTitle) fieldWidget;
 84  0
                 fieldValue = sectionTitle.getElement().getInnerText();
 85  
                 // fieldValue = sectionTitle.getTitleText();
 86  0
             } catch (Exception e) {
 87  0
                 System.out.println("Section tile interface probleem - only in debugging.");
 88  0
                 e.printStackTrace();
 89  0
             }
 90  
             // vrydag moved down } else if (fieldWidget instanceof ComplexPanel) {
 91  
             // subExportElements = ExportUtils.getDetailsForWidget(fieldWidget,
 92  
             // subExportElements, viewName, sectionName);
 93  0
         } else if (fieldWidget instanceof ReportExportWidget) {
 94  0
             ReportExportWidget widget = (ReportExportWidget) fieldWidget;
 95  0
             if (widget.isExportElement()) {
 96  0
                 fieldValue = widget.getExportFieldValue();
 97  0
                 exportItem.setSubset(widget.getExportElementSubset(exportItem));
 98  
             }
 99  0
         } else if (fieldWidget instanceof ComplexPanel) {
 100  0
             exportItem.setSubset(ExportUtils.getDetailsForWidget(fieldWidget, viewName, sectionName));
 101  
 
 102  
         } else {
 103  
             // logger.warn(exportItem.getFieldLabel() +
 104  
             // " Fieldwidget not catered for : class type = " +
 105  
             // fieldWidget.getClass().getName());
 106  
         }
 107  0
         if (setFirstFieldValue) {
 108  0
             exportItem.setFieldValue(fieldValue);
 109  
         } else {
 110  0
             exportItem.setFieldValue2(fieldValue);
 111  
         }
 112  
 
 113  
         // System.out.println(exportItem.getSectionName() + " : Label = " +
 114  
         // exportItem.getFieldLabel() + " fieldValue : " +
 115  
         // exportItem.getFieldValue() + " fieldValue2 : " +
 116  
         // exportItem.getFieldValue2());
 117  0
         return exportItem;
 118  
     }
 119  
 
 120  
     /**
 121  
      * This method gets the current controller based on the widget that was passed to it.
 122  
      * 
 123  
      * @param theWidget
 124  
      * @return currentController
 125  
      */
 126  
     public static Controller getController(Widget theWidget) {
 127  
         // TODO Nina - This can't be the correct way of getting handle to
 128  
         // Controller, isn't there a better way??
 129  0
         if (theWidget != null) {
 130  
 
 131  0
             if (theWidget instanceof Controller) {
 132  0
                 Controller controller = (Controller) theWidget;
 133  0
                 return controller;
 134  
             } else {
 135  0
                 return getController(theWidget.getParent());
 136  
             }
 137  
         } else {
 138  0
             return null;
 139  
         }
 140  
     }
 141  
 
 142  
     public static void handleExportClickEvent(Controller currentController, String format, String reportTitle) {
 143  0
         List<ExportElement> exportElements = new ArrayList<ExportElement>();
 144  0
         exportElements = currentController.getExportElementsFromView();
 145  0
         if (exportElements != null && exportElements.size() > 0) {
 146  0
             debugExportElementsArray(exportElements);
 147  0
             currentController.doReportExport(exportElements, format, reportTitle);
 148  
         }
 149  0
     }
 150  
 
 151  
     public static void debugExportElementsArray(List<ExportElement> exportElements) {
 152  0
         System.out.println(" ");
 153  0
         System.out.println(" ");
 154  0
         System.out.println(" ");
 155  0
         System.out.println(" ");
 156  0
         System.out.println(" ");
 157  0
         System.out.println(" ");
 158  0
         System.out.println(" ");
 159  0
         System.out.println(" ");
 160  
 
 161  0
         for (int i = 0; i < exportElements.size(); i++) {
 162  0
             System.out.println(exportElements.get(i).toString());
 163  0
             debutExportElementsArraySubList(exportElements.get(i).getSubset());
 164  
         }
 165  0
     }
 166  
 
 167  
     private static void debutExportElementsArraySubList(List<ExportElement> exportElements) {
 168  0
         if (exportElements != null) {
 169  0
             System.out.println("Sub list : ");
 170  0
             for (int j = 0; j < exportElements.size(); j++) {
 171  0
                 ExportElement element = exportElements.get(j);
 172  0
                 System.out.println(element.toString());
 173  0
                 debutExportElementsArraySubList(element.getSubset());
 174  
 
 175  
             }
 176  
         }
 177  0
     }
 178  
 
 179  
     public static ArrayList<ExportElement> getDetailsForWidget(SummaryTableSection tableSection, ArrayList<ExportElement> exportElements) {
 180  0
         SummaryTable sumTable = tableSection.getSummaryTable();
 181  0
         SummaryTableModel model = sumTable.getModel();
 182  0
         List<SummaryTableBlock> tableSectionList = model.getSectionList();
 183  0
         for (int i = 0; i < tableSectionList.size(); i++) {
 184  0
             SummaryTableBlock item = tableSectionList.get(i);
 185  0
             String blockName = item.getTitle();
 186  0
             List<SummaryTableRow> rowItems = item.getSectionRowList();
 187  0
             for (int j = 0; j < rowItems.size(); j++) {
 188  0
                 ExportElement element = new ExportElement();
 189  0
                                 SummaryTableRow row = rowItems.get(j);
 190  0
                                 element.setSectionName(blockName);
 191  0
                                 element.setViewName(blockName);
 192  0
                                 element.setFieldLabel(row.getTitle());
 193  0
                                 element.setMandatory(row.isRequired());
 194  0
                 Widget fdWidget = row.getCell1();
 195  0
                 if (row.isShown()) {
 196  0
                     if (fdWidget != null) {
 197  
                         //
 198  0
                         if (fdWidget instanceof KSListPanel) {
 199  0
                             element.setSubset(ExportUtils.getDetailsForWidget(fdWidget, blockName, blockName));
 200  
                         } else {
 201  
                             //
 202  0
                             element = ExportUtils.getExportItemDetails(element, fdWidget, true, blockName, blockName);
 203  
                         }
 204  
                     } else {
 205  0
                         if (row.getTitle() != null) {
 206  0
                             element.setFieldLabel(row.getTitle());
 207  
                         }
 208  
                     }
 209  
                     //
 210  0
                     Widget fdWidget2 = row.getCell2();
 211  0
                     if (fdWidget2 != null) {
 212  0
                         element = ExportUtils.getExportItemDetails(element, fdWidget2, false, blockName, blockName);
 213  
 
 214  
                     } else {
 215  0
                         if (row.getTitle() != null) {
 216  0
                             element.setFieldLabel(row.getTitle());
 217  
                         }
 218  
                     }
 219  0
                     if (element != null && element.getViewName() != null) {
 220  0
                         exportElements.add(element);
 221  
                     }
 222  
                 }
 223  
                 
 224  
             }
 225  
         }
 226  0
         return exportElements;
 227  
     }
 228  
 
 229  
     public static List<ExportElement> getDetailsForWidget(Widget currentViewWidget, String viewName, String sectionName) {
 230  0
         List<ExportElement> childElements = new ArrayList<ExportElement>();
 231  0
         if (currentViewWidget instanceof Section) {
 232  0
             Section widgetHasFields = (Section) currentViewWidget;
 233  0
             List<FieldDescriptor> widgetFields = widgetHasFields.getFields();
 234  0
             for (FieldDescriptor field : widgetFields) {
 235  
 
 236  0
                 Widget fieldWidget = field.getFieldElement().getFieldWidget();
 237  0
                 ExportElement exportItem = new ExportElement();
 238  0
                                 exportItem.setSectionName(sectionName + viewName);
 239  0
                                 exportItem.setViewName(sectionName + viewName);
 240  0
                                 exportItem.setFieldLabel(field.getFieldLabel());
 241  0
                 exportItem = getExportItemDetails(exportItem, fieldWidget, true, viewName, sectionName);
 242  
                 //
 243  0
                 ExportUtils.addElementToElementArray(childElements, exportItem);
 244  
                 //
 245  
                 // Vrydag exportElements.add(getExportItemDetails(exportItem,
 246  
                 // fieldWidget, true, viewName, sectionName));
 247  0
             }
 248  0
         } else if (currentViewWidget instanceof KSListPanel) {
 249  0
             KSListPanel ksListPanelWidget = (KSListPanel) currentViewWidget;
 250  0
             WidgetCollection children = ksListPanelWidget.getChildren();
 251  0
             for (int i = 0; i < children.size(); i++) {
 252  0
                 Widget child = children.get(i);
 253  
 
 254  0
                                 ExportElement exportItem = new ExportElement();
 255  0
                                 exportItem.setSectionName(sectionName + viewName);
 256  0
                                 exportItem.setViewName(sectionName + viewName);
 257  0
                 exportItem = getExportItemDetails(exportItem, child, true, viewName, sectionName);
 258  
                 // Vrydag exportItem.setFieldLabel("");
 259  
                 //
 260  0
                 ExportUtils.addElementToElementArray(childElements, exportItem);
 261  
                 //
 262  
                 // Vrydag exportElements.add(getExportItemDetails(exportItem, child,
 263  
                 // true, viewName, sectionName));
 264  
             }
 265  
 
 266  0
         } else if (currentViewWidget instanceof ComplexPanel) {
 267  0
             ComplexPanel complexPanel = (ComplexPanel) currentViewWidget;
 268  0
             for (int i = 0; i < complexPanel.getWidgetCount(); i++) {
 269  0
                 Widget child = complexPanel.getWidget(i);
 270  0
                 if (!(child instanceof KSButton)
 271  
                 // Vrydag && !(child instanceof SpanPanel) && !(child instanceof WarnContainer)) {
 272  
                         && !(child instanceof WarnContainer)) {
 273  
                                         
 274  0
                                         ExportElement exportItem = new ExportElement();
 275  0
                                         exportItem.setSectionName(sectionName);
 276  0
                                         exportItem.setViewName(viewName);
 277  
                                         
 278  0
                     exportItem = getExportItemDetails(exportItem, child, true, viewName, sectionName);
 279  0
                     ExportUtils.addElementToElementArray(childElements, exportItem);
 280  
                     // Vrydag if ((exportItemDetails.getFieldValue() != null &&
 281  
                     // !exportItemDetails.getFieldValue().equals(""))
 282  
                     // || (exportItemDetails.getSubset() != null && exportItemDetails
 283  
                     // .getSubset().size() > 0)) {
 284  
                     // exportElements.add(exportItemDetails);
 285  
                     // }
 286  
                 }
 287  
             }
 288  
 
 289  0
         } else {
 290  
 
 291  0
             System.out.println("ExportUtils does not cater for this type..." + currentViewWidget.getClass().getName());
 292  
 
 293  
         }
 294  0
         return childElements;
 295  
     }
 296  
 
 297  
     public static ArrayList<ExportElement> getExportElementsFromView(Widget currentViewWidget, ArrayList<ExportElement> exportElements, String viewName, String sectionName) {
 298  0
         if (exportElements == null) {
 299  0
             exportElements = new ArrayList<ExportElement>();
 300  
         }
 301  0
         if (currentViewWidget instanceof VerticalSectionView) {
 302  0
             Section widgetHasFields = (Section) currentViewWidget;
 303  0
             List<FieldDescriptor> widgetFields = widgetHasFields.getFields();
 304  0
             for (FieldDescriptor field : widgetFields) {
 305  0
                     ExportElement exportItem = new ExportElement();
 306  0
                                 exportItem.setSectionName(sectionName + viewName);
 307  0
                                 exportItem.setViewName(sectionName + viewName);
 308  0
                                 exportItem.setFieldLabel(field.getFieldLabel());
 309  0
                 Widget fieldWidget = field.getFieldElement().getFieldWidget();
 310  
 
 311  0
                 exportItem = getExportItemDetails(exportItem, fieldWidget, true, viewName, sectionName);
 312  
 
 313  0
                 exportElements.add(exportItem);
 314  0
             }
 315  0
             if ((currentViewWidget instanceof BaseSection) && (widgetHasFields.getFields().size() == 0)) {
 316  0
                 BaseSection bSection = (BaseSection) currentViewWidget;
 317  0
                 ExportElement exportItem = new ExportElement();
 318  0
                                 exportItem.setSectionName(sectionName + viewName);
 319  0
                                 exportItem.setViewName(sectionName + viewName);
 320  0
                                 exportItem.setFieldLabel("???00");
 321  0
                 exportItem = getExportItemDetails(exportItem, bSection.getLayout(), true, viewName, sectionName);
 322  0
                 exportElements.add(exportItem);
 323  
 
 324  
             }
 325  
             // } else { // Debugging
 326  
             // System.out.println("ExportUtils.getExportElementsFromView is not implemented for your View, either implement it here or do "
 327  
             // +
 328  
             // "not call the ExportUtils.getExportElementsFromView but implement it directly on your view");
 329  
         }
 330  0
         return exportElements;
 331  
     }
 332  
 
 333  
     // Only add element if it is not null
 334  
     public static List<ExportElement> addElementToElementArray(List<ExportElement> elementArray, ExportElement element) {
 335  0
         if (element.getFieldLabel() != null || element.getFieldValue() != null || element.getFieldValue2() != null || (element.getSubset() != null && element.getSubset().size() > 0)) {
 336  0
             elementArray.add(element);
 337  
         }
 338  0
         return elementArray;
 339  
     }
 340  
 
 341  
 }