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