Coverage Report - org.kuali.student.common.ui.client.util.ExportUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ExportUtils
0%
0/176
0%
0/114
7.667
 
 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.getParent().getElement().getStyle().getDisplay().equals("none")){
 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  
             try {
 84  0
                 SectionTitle sectionTitle = (SectionTitle) fieldWidget;
 85  0
                 fieldValue = sectionTitle.getElement().getInnerText();
 86  
                 // fieldValue = sectionTitle.getTitleText();
 87  0
             } catch (Exception e) {
 88  0
                 System.out.println("Section tile interface probleem - only in debugging.");
 89  0
                 e.printStackTrace();
 90  0
             }
 91  
             // vrydag moved down } else if (fieldWidget instanceof ComplexPanel) {
 92  
             // subExportElements = ExportUtils.getDetailsForWidget(fieldWidget,
 93  
             // subExportElements, viewName, sectionName);
 94  0
         } else if (fieldWidget instanceof ReportExportWidget) {
 95  
                 
 96  0
                 if (fieldWidget.isVisible()){
 97  0
             ReportExportWidget widget = (ReportExportWidget) fieldWidget;
 98  0
             if (widget.isExportElement() ) {
 99  0
                 fieldValue = widget.getExportFieldValue();
 100  0
                 exportItem.setSubset(widget.getExportElementSubset(exportItem));
 101  
             }
 102  0
             }
 103  0
         } else if (fieldWidget instanceof ComplexPanel) {
 104  0
                 if(fieldWidget.isVisible())
 105  0
             {exportItem.setSubset(ExportUtils.getDetailsForWidget(fieldWidget, viewName, sectionName));}
 106  
 
 107  
         } else {
 108  
             // logger.warn(exportItem.getFieldLabel() +
 109  
             // " Fieldwidget not catered for : class type = " +
 110  
             // fieldWidget.getClass().getName());
 111  
         }
 112  0
         if (setFirstFieldValue) {
 113  0
             exportItem.setFieldValue(fieldValue);
 114  
         } else {
 115  0
             exportItem.setFieldValue2(fieldValue);
 116  
         }
 117  
         }
 118  
         // System.out.println(exportItem.getSectionName() + " : Label = " +
 119  
         // exportItem.getFieldLabel() + " fieldValue : " +
 120  
         // exportItem.getFieldValue() + " fieldValue2 : " +
 121  
         // exportItem.getFieldValue2());
 122  0
         return exportItem;
 123  
     }
 124  
 
 125  
     /**
 126  
      * This method gets the current controller based on the widget that was passed to it.
 127  
      * 
 128  
      * @param theWidget
 129  
      * @return currentController
 130  
      */
 131  
     public static Controller getController(Widget theWidget) {
 132  
         // TODO Nina - This can't be the correct way of getting handle to
 133  
         // Controller, isn't there a better way??
 134  0
         if (theWidget != null) {
 135  
 
 136  0
             if (theWidget instanceof Controller) {
 137  0
                 Controller controller = (Controller) theWidget;
 138  0
                 return controller;
 139  
             } else {
 140  0
                 return getController(theWidget.getParent());
 141  
             }
 142  
         } else {
 143  0
             return null;
 144  
         }
 145  
     }
 146  
 
 147  
     public static void handleExportClickEvent(Controller currentController, String format, String reportTitle) {
 148  0
         List<ExportElement> exportElements = new ArrayList<ExportElement>();
 149  0
         exportElements = currentController.getExportElementsFromView();
 150  0
         if (exportElements != null && exportElements.size() > 0) {
 151  0
             debugExportElementsArray(exportElements);
 152  0
             currentController.doReportExport(exportElements, format, reportTitle);
 153  
         }
 154  0
     }
 155  
 
 156  
     public static void debugExportElementsArray(List<ExportElement> exportElements) {
 157  0
         System.out.println(" ");
 158  0
         System.out.println(" ");
 159  0
         System.out.println(" ");
 160  0
         System.out.println(" ");
 161  0
         System.out.println(" ");
 162  0
         System.out.println(" ");
 163  0
         System.out.println(" ");
 164  0
         System.out.println(" ");
 165  
 
 166  0
         for (int i = 0; i < exportElements.size(); i++) {
 167  0
             System.out.println(exportElements.get(i).toString());
 168  0
             debutExportElementsArraySubList(exportElements.get(i).getSubset());
 169  
         }
 170  0
     }
 171  
 
 172  
     private static void debutExportElementsArraySubList(List<ExportElement> exportElements) {
 173  0
         if (exportElements != null) {
 174  0
             System.out.println("Sub list : ");
 175  0
             for (int j = 0; j < exportElements.size(); j++) {
 176  0
                 ExportElement element = exportElements.get(j);
 177  0
                 System.out.println(element.toString());
 178  0
                 debutExportElementsArraySubList(element.getSubset());
 179  
 
 180  
             }
 181  
         }
 182  0
     }
 183  
 
 184  
     public static ArrayList<ExportElement> getDetailsForWidget(SummaryTableSection tableSection, ArrayList<ExportElement> exportElements) {
 185  0
         SummaryTable sumTable = tableSection.getSummaryTable();
 186  0
         SummaryTableModel model = sumTable.getModel();
 187  0
         List<SummaryTableBlock> tableSectionList = model.getSectionList();
 188  0
         for (int i = 0; i < tableSectionList.size(); i++) {
 189  0
             SummaryTableBlock item = tableSectionList.get(i);
 190  0
             String blockName = item.getTitle();
 191  0
             List<SummaryTableRow> rowItems = item.getSectionRowList();
 192  0
             for (int j = 0; j < rowItems.size(); j++) {
 193  0
                 ExportElement element = new ExportElement();
 194  0
                                 SummaryTableRow row = rowItems.get(j);
 195  0
                                 element.setSectionName(blockName);
 196  0
                                 element.setViewName(blockName);
 197  0
                                 element.setFieldLabel(row.getTitle());
 198  0
                                 element.setMandatory(row.isRequired());
 199  0
                 Widget fdWidget = row.getCell1();
 200  0
                 if (row.isShown()) {
 201  0
                     if (fdWidget != null) {
 202  
                         //
 203  0
                         if (fdWidget instanceof KSListPanel) {
 204  0
                             element.setSubset(ExportUtils.getDetailsForWidget(fdWidget, blockName, blockName));
 205  
                         } else {
 206  
                             //
 207  0
                             element = ExportUtils.getExportItemDetails(element, fdWidget, true, blockName, blockName);
 208  
                         }
 209  
                     } else {
 210  0
                         if (row.getTitle() != null) {
 211  0
                             element.setFieldLabel(row.getTitle());
 212  
                         }
 213  
                     }
 214  
                     //
 215  0
                     Widget fdWidget2 = row.getCell2();
 216  0
                     if (fdWidget2 != null) {
 217  0
                         element = ExportUtils.getExportItemDetails(element, fdWidget2, false, blockName, blockName);
 218  
 
 219  
                     } else {
 220  0
                         if (row.getTitle() != null) {
 221  0
                             element.setFieldLabel(row.getTitle());
 222  
                         }
 223  
                     }
 224  0
                     if (element != null && element.getViewName() != null) {
 225  0
                         exportElements.add(element);
 226  
                     }
 227  
                 }
 228  
                 
 229  
             }
 230  
         }
 231  0
         return exportElements;
 232  
     }
 233  
 
 234  
     public static List<ExportElement> getDetailsForWidget(Widget currentViewWidget, String viewName, String sectionName) {
 235  0
         List<ExportElement> childElements = new ArrayList<ExportElement>();
 236  0
         if (!currentViewWidget.getParent().getElement().getStyle().getDisplay().equals("none")){
 237  0
         if (currentViewWidget instanceof Section) {
 238  0
             Section widgetHasFields = (Section) currentViewWidget;
 239  0
             List<FieldDescriptor> widgetFields = widgetHasFields.getFields();
 240  0
             for (FieldDescriptor field : widgetFields) {
 241  
 
 242  0
                 Widget fieldWidget = field.getFieldElement().getFieldWidget();
 243  0
                 ExportElement exportItem = new ExportElement();
 244  0
                                 exportItem.setSectionName(sectionName + viewName);
 245  0
                                 exportItem.setViewName(sectionName + viewName);
 246  0
                                 exportItem.setFieldLabel(field.getFieldLabel());
 247  0
                 exportItem = getExportItemDetails(exportItem, fieldWidget, true, viewName, sectionName);
 248  
                 //
 249  0
                 ExportUtils.addElementToElementArray(childElements, exportItem);
 250  
                 //
 251  
                 // Vrydag exportElements.add(getExportItemDetails(exportItem,
 252  
                 // fieldWidget, true, viewName, sectionName));
 253  0
             }
 254  0
         } else if (currentViewWidget instanceof KSListPanel) {
 255  0
             KSListPanel ksListPanelWidget = (KSListPanel) currentViewWidget;
 256  0
             WidgetCollection children = ksListPanelWidget.getChildren();
 257  0
             for (int i = 0; i < children.size(); i++) {
 258  0
                 Widget child = children.get(i);
 259  
 
 260  0
                                 ExportElement exportItem = new ExportElement();
 261  0
                                 exportItem.setSectionName(sectionName + viewName);
 262  0
                                 exportItem.setViewName(sectionName + viewName);
 263  0
                 exportItem = getExportItemDetails(exportItem, child, true, viewName, sectionName);
 264  
                 // Vrydag exportItem.setFieldLabel("");
 265  
                 //
 266  0
                 ExportUtils.addElementToElementArray(childElements, exportItem);
 267  
                 //
 268  
                 // Vrydag exportElements.add(getExportItemDetails(exportItem, child,
 269  
                 // true, viewName, sectionName));
 270  
             }
 271  
 
 272  0
         } else if (currentViewWidget instanceof ComplexPanel) {
 273  0
             ComplexPanel complexPanel = (ComplexPanel) currentViewWidget;
 274  0
             if (complexPanel.isVisible()){
 275  0
             for (int i = 0; i < complexPanel.getWidgetCount(); i++) {
 276  0
                 Widget child = complexPanel.getWidget(i);
 277  0
                 if (!(child instanceof KSButton)
 278  
                 // Vrydag && !(child instanceof SpanPanel) && !(child instanceof WarnContainer)) {
 279  
                         && !(child instanceof WarnContainer)) {
 280  
                                         
 281  0
                                         ExportElement exportItem = new ExportElement();
 282  0
                                         exportItem.setSectionName(sectionName);
 283  0
                                         exportItem.setViewName(viewName);
 284  
                                         
 285  0
                     exportItem = getExportItemDetails(exportItem, child, true, viewName, sectionName);
 286  0
                     ExportUtils.addElementToElementArray(childElements, exportItem);
 287  
                     // Vrydag if ((exportItemDetails.getFieldValue() != null &&
 288  
                     // !exportItemDetails.getFieldValue().equals(""))
 289  
                     // || (exportItemDetails.getSubset() != null && exportItemDetails
 290  
                     // .getSubset().size() > 0)) {
 291  
                     // exportElements.add(exportItemDetails);
 292  
                     // }
 293  
                 }
 294  
             }
 295  
             }
 296  0
         } else {
 297  
 
 298  0
             System.out.println("ExportUtils does not cater for this type..." + currentViewWidget.getClass().getName());
 299  
 
 300  
         }
 301  
         }
 302  0
         return childElements;
 303  
     }
 304  
 
 305  
     public static ArrayList<ExportElement> getExportElementsFromView(Widget currentViewWidget, ArrayList<ExportElement> exportElements, String viewName, String sectionName) {
 306  0
         if (exportElements == null) {
 307  0
             exportElements = new ArrayList<ExportElement>();
 308  
         }
 309  0
         if (currentViewWidget.getParent() == null || !currentViewWidget.getParent().getElement().getStyle().getDisplay().equals("none")) {
 310  0
             if (currentViewWidget instanceof VerticalSectionView) {
 311  0
                 Section widgetHasFields = (Section) currentViewWidget;
 312  0
                 List<FieldDescriptor> widgetFields = widgetHasFields.getFields();
 313  0
                 for (FieldDescriptor field : widgetFields) {
 314  0
                     ExportElement exportItem = new ExportElement();
 315  0
                     exportItem.setSectionName(sectionName + viewName);
 316  0
                     exportItem.setViewName(sectionName + viewName);
 317  0
                     exportItem.setFieldLabel(field.getFieldLabel());
 318  0
                     Widget fieldWidget = field.getFieldElement().getFieldWidget();
 319  
 
 320  0
                     exportItem = getExportItemDetails(exportItem, fieldWidget, true, viewName, sectionName);
 321  
 
 322  0
                     exportElements.add(exportItem);
 323  0
                 }
 324  0
                 if ((currentViewWidget instanceof BaseSection) && (widgetHasFields.getFields().size() == 0)) {
 325  0
                     BaseSection bSection = (BaseSection) currentViewWidget;
 326  0
                     ExportElement exportItem = new ExportElement();
 327  0
                     exportItem.setSectionName(sectionName + viewName);
 328  0
                     exportItem.setViewName(sectionName + viewName);
 329  0
                     exportItem.setFieldLabel("???00");
 330  0
                     exportItem = getExportItemDetails(exportItem, bSection.getLayout(), true, viewName, sectionName);
 331  0
                     exportElements.add(exportItem);
 332  
 
 333  
                 }
 334  
             }
 335  
         }
 336  0
         return exportElements;
 337  
     }
 338  
 
 339  
     // Only add element if it is not null
 340  
     public static List<ExportElement> addElementToElementArray(List<ExportElement> elementArray, ExportElement element) {
 341  0
         if (element.getFieldLabel() != null || element.getFieldValue() != null || element.getFieldValue2() != null || (element.getSubset() != null && element.getSubset().size() > 0)) {
 342  0
             elementArray.add(element);
 343  
         }
 344  0
         return elementArray;
 345  
     }
 346  
 
 347  
 }