001 /** 002 * Copyright 2010 The Kuali Foundation Licensed under the Educational Community License, Version 2.0 (the "License"); you may 003 * not use this file except in compliance with the License. You may obtain a copy of the License at 004 * http://www.osedu.org/licenses/ECL-2.0 Unless required by applicable law or agreed to in writing, software distributed 005 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 006 * implied. See the License for the specific language governing permissions and limitations under the License. 007 */ 008 009 package org.kuali.student.common.ui.client.util; 010 011 import java.util.ArrayList; 012 import java.util.List; 013 014 import org.kuali.student.common.ui.client.configurable.mvc.FieldDescriptor; 015 import org.kuali.student.common.ui.client.configurable.mvc.LayoutController; 016 import org.kuali.student.common.ui.client.configurable.mvc.SectionTitle; 017 import org.kuali.student.common.ui.client.configurable.mvc.sections.BaseSection; 018 import org.kuali.student.common.ui.client.configurable.mvc.sections.Section; 019 import org.kuali.student.common.ui.client.configurable.mvc.views.VerticalSectionView; 020 import org.kuali.student.common.ui.client.mvc.Controller; 021 import org.kuali.student.common.ui.client.reporting.ReportExportWidget; 022 import org.kuali.student.common.ui.client.widgets.KSItemLabel; 023 import org.kuali.student.common.ui.client.widgets.KSLabel; 024 import org.kuali.student.common.ui.client.widgets.list.KSLabelList; 025 import org.kuali.student.common.ui.client.widgets.list.KSSelectedList; 026 import org.kuali.student.common.ui.client.widgets.list.impl.KSLabelListImpl; 027 import org.kuali.student.common.ui.client.widgets.menus.KSListPanel; 028 import org.kuali.student.common.ui.client.widgets.search.KSPicker; 029 import org.kuali.student.common.ui.client.widgets.table.summary.SummaryTable; 030 import org.kuali.student.common.ui.client.widgets.table.summary.SummaryTableBlock; 031 import org.kuali.student.common.ui.client.widgets.table.summary.SummaryTableModel; 032 import org.kuali.student.common.ui.client.widgets.table.summary.SummaryTableRow; 033 import org.kuali.student.common.ui.client.widgets.table.summary.SummaryTableSection; 034 035 import com.google.gwt.user.client.ui.ComplexPanel; 036 import com.google.gwt.user.client.ui.HasText; 037 import com.google.gwt.user.client.ui.ListBox; 038 import com.google.gwt.user.client.ui.Widget; 039 import com.google.gwt.user.client.ui.WidgetCollection; 040 041 public class ExportUtils { 042 public static final String PDF = "PDF"; 043 public static final String DOC = "DOC"; 044 public static final String XLS = "XLS"; 045 046 public static ExportElement getExportItemDetails(ExportElement exportItem, Widget fieldWidget, boolean setFirstFieldValue, String viewName, String sectionName) { 047 ArrayList<ExportElement> subExportElements = new ArrayList<ExportElement>(); 048 String fieldValue = null; 049 if (fieldWidget instanceof HasText) { 050 HasText itemHasTextValue = (HasText) fieldWidget; 051 fieldValue = itemHasTextValue.getText(); 052 053 } else if (fieldWidget instanceof KSLabel) { 054 // ignore labels... as we're not interested 055 } else if (fieldWidget instanceof KSSelectedList) { 056 KSSelectedList selectedList = (KSSelectedList) fieldWidget; 057 List<KSItemLabel> selectedItems = selectedList.getSelectedItems(); 058 String values = new String(); 059 for (int j = 0; j < selectedItems.size(); j++) { 060 values = selectedItems.get(j).getDisplayText(); 061 } 062 fieldValue = values; 063 } else if (fieldWidget instanceof KSPicker) { 064 KSPicker picker = (KSPicker) fieldWidget; 065 if (picker.getInputWidget() instanceof HasText) { 066 HasText item = (HasText) picker.getInputWidget(); 067 fieldValue = item.getText(); 068 } else if (picker.getInputWidget() instanceof KSLabelList) { 069 KSLabelList widget = (KSLabelList) picker.getInputWidget(); 070 List<String> selected = widget.getSelectedItemsForExport(); 071 for (int j = 0; j < selected.size(); j++) { 072 if (fieldValue == null) { 073 fieldValue = new String(selected.get(j)); 074 } else { 075 fieldValue = fieldValue + ", " + selected.get(j); 076 } 077 } 078 } 079 } else if (fieldWidget instanceof ListBox) { 080 ListBox listBox = (ListBox) fieldWidget; 081 fieldValue = listBox.getItemText(listBox.getSelectedIndex()); 082 } else if (fieldWidget instanceof SectionTitle) { 083 SectionTitle sectionTitle = (SectionTitle) fieldWidget; 084 fieldValue = sectionTitle.getElement().getInnerText(); 085 } else if (fieldWidget instanceof ComplexPanel) { 086 subExportElements = ExportUtils.getDetailsForWidget(fieldWidget, subExportElements, viewName, sectionName); 087 } else if (fieldWidget instanceof ReportExportWidget) { 088 ReportExportWidget widget = (ReportExportWidget) fieldWidget; 089 subExportElements = widget.getExportElementsWidget(viewName, sectionName); 090 } else 091 { 092 // logger.warn(exportItem.getFieldLabel() + " Fieldwidget not catered for : class type = " + 093 // fieldWidget.getClass().getName()); 094 } 095 if (setFirstFieldValue) { 096 exportItem.setFieldValue(fieldValue); 097 } else { 098 exportItem.setFieldValue2(fieldValue); 099 } 100 if (subExportElements != null && subExportElements.size() > 0) { 101 exportItem.setSubset(subExportElements); 102 } 103 104 // System.out.println(exportItem.getSectionName() + " : Label = " + exportItem.getFieldLabel() + " fieldValue : " + exportItem.getFieldValue() + " fieldValue2 : " + exportItem.getFieldValue2()); 105 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 if (theWidget != null) { 117 118 if (theWidget instanceof Controller) { 119 Controller controller = (Controller) theWidget; 120 return controller; 121 } else { 122 return getController(theWidget.getParent()); 123 } 124 } else { 125 return null; 126 } 127 } 128 129 public static void handleExportClickEvent(Controller currentController, String format, String reportTitle) { 130 ArrayList<ExportElement> exportElements = new ArrayList<ExportElement>(); 131 exportElements = currentController.getExportElementsFromView(); 132 if (exportElements != null && exportElements.size() > 0) { 133 // for debugging debutExportElementsArray(exportElements); 134 currentController.doReportExport(exportElements, format, reportTitle); 135 } 136 } 137 138 private static void debutExportElementsArray(ArrayList<ExportElement> exportElements) { 139 System.out.println(" "); 140 System.out.println(" "); 141 System.out.println(" "); 142 System.out.println(" "); 143 System.out.println(" "); 144 System.out.println(" "); 145 System.out.println(" "); 146 System.out.println(" "); 147 148 for (int i = 0; i < exportElements.size(); i++) { 149 System.out.println(exportElements.get(i).printLine()); 150 debutExportElementsArraySubList(exportElements.get(i).getSubset()); 151 } 152 } 153 154 private static void debutExportElementsArraySubList(List<ExportElement> exportElements) { 155 if (exportElements != null) { 156 System.out.println("Sub list : "); 157 for (int j = 0; j < exportElements.size(); j++) { 158 ExportElement element = exportElements.get(j); 159 System.out.println(element.printLine()); 160 debutExportElementsArraySubList(element.getSubset()); 161 162 } 163 } 164 } 165 166 public static ArrayList<ExportElement> getDetailsForWidget(SummaryTableSection tableSection, ArrayList<ExportElement> exportElements) { 167 SummaryTable sumTable = tableSection.getSummaryTable(); 168 SummaryTableModel model = sumTable.getModel(); 169 List<SummaryTableBlock> tableSectionList = model.getSectionList(); 170 for (int i = 0; i < tableSectionList.size(); i++) { 171 SummaryTableBlock item = tableSectionList.get(i); 172 String blockName = item.getTitle(); 173 List<SummaryTableRow> rowItems = item.getSectionRowList(); 174 for (int j = 0; j < rowItems.size(); j++) { 175 ExportElement element = new ExportElement(); 176 SummaryTableRow row = rowItems.get(j); 177 element.setSectionName(blockName); 178 element.setViewName(blockName); 179 element.setFieldLabel(row.getTitle()); 180 element.setMandatory(row.isRequired()); 181 Widget fdWidget = row.getCell1(); 182 if (row.isShown()) { 183 if (fdWidget != null) { 184 // 185 if (fdWidget instanceof KSListPanel) { 186 ArrayList<ExportElement> subExportElements = new ArrayList<ExportElement>(); 187 subExportElements = ExportUtils.getDetailsForWidget(fdWidget, subExportElements, blockName, blockName); 188 element.setSubset(subExportElements); 189 } else { 190 // 191 element = ExportUtils.getExportItemDetails(element,fdWidget,true, blockName, blockName); 192 } 193 } else { 194 if (row.getTitle() != null) { 195 element.setFieldLabel(row.getTitle()); 196 } 197 } 198 // 199 Widget fdWidget2 = row.getCell2(); 200 if (fdWidget2 != null) { 201 element = ExportUtils.getExportItemDetails(element,fdWidget2,false, blockName, blockName); 202 203 } else { 204 if (row.getTitle() != null) { 205 element.setFieldLabel(row.getTitle()); 206 } 207 } 208 if (element != null && element.getViewName() != null) { 209 exportElements.add(element); 210 } 211 } 212 } 213 } 214 return exportElements; 215 } 216 217 public static ArrayList<ExportElement> getDetailsForWidget(Widget currentViewWidget, ArrayList<ExportElement> exportElements, String viewName, String sectionName) { 218 if (currentViewWidget instanceof Section) { 219 Section widgetHasFields = (Section) currentViewWidget; 220 List<FieldDescriptor> widgetFields = widgetHasFields.getFields(); 221 for (FieldDescriptor field : widgetFields) { 222 ExportElement exportItem = new ExportElement(); 223 exportItem.setSectionName(sectionName + viewName); 224 exportItem.setViewName(sectionName + viewName); 225 exportItem.setFieldLabel(field.getFieldLabel()); 226 Widget fieldWidget = field.getFieldElement().getFieldWidget(); 227 228 exportElements.add(getExportItemDetails(exportItem, fieldWidget, true, viewName, sectionName)); 229 } 230 } else if (currentViewWidget instanceof KSListPanel) { 231 KSListPanel ksListPanelWidget = (KSListPanel) currentViewWidget; 232 WidgetCollection children = ksListPanelWidget.getChildren(); 233 for (int i = 0; i < children.size(); i++) { 234 Widget child = children.get(i); 235 236 ExportElement exportItem = new ExportElement(); 237 exportItem.setSectionName(sectionName + viewName); 238 exportItem.setViewName(sectionName + viewName); 239 exportItem.setFieldLabel(""); 240 exportElements.add(getExportItemDetails(exportItem, child, true, viewName, sectionName)); 241 } 242 243 } else if (currentViewWidget instanceof ComplexPanel){ 244 ComplexPanel complexPanel = (ComplexPanel) currentViewWidget; 245 for (int i = 0; i < complexPanel.getWidgetCount(); i++) { 246 Widget child = complexPanel.getWidget(i); 247 ExportElement exportItem = new ExportElement(); 248 exportItem.setSectionName(sectionName); 249 exportItem.setViewName(viewName); 250 ExportElement exportItemDetails = getExportItemDetails(exportItem, child, true, viewName, sectionName); 251 if (exportItemDetails.getFieldValue() != null || (exportItemDetails.getSubset() != null && exportItemDetails.getSubset().size() > 0)) { 252 exportElements.add(exportItemDetails); 253 } 254 } 255 256 257 258 } else { 259 260 System.out.println("ExportUtils does not cater for this type..." + currentViewWidget.getClass().getName()); 261 262 } 263 return exportElements; 264 } 265 266 public static ArrayList<ExportElement> getExportElementsFromView(Widget currentViewWidget, ArrayList<ExportElement> exportElements, String viewName, String sectionName) { 267 if (exportElements == null) { 268 exportElements = new ArrayList<ExportElement>(); 269 } 270 if (currentViewWidget instanceof VerticalSectionView) { 271 Section widgetHasFields = (Section) currentViewWidget; 272 List<FieldDescriptor> widgetFields = widgetHasFields.getFields(); 273 for (FieldDescriptor field : widgetFields) { 274 ExportElement exportItem = new ExportElement(); 275 exportItem.setSectionName(sectionName + viewName); 276 exportItem.setViewName(sectionName + viewName); 277 exportItem.setFieldLabel(field.getFieldLabel()); 278 Widget fieldWidget = field.getFieldElement().getFieldWidget(); 279 280 exportElements.add(getExportItemDetails(exportItem, fieldWidget,true,viewName, sectionName)); 281 } 282 if ((currentViewWidget instanceof BaseSection) && (widgetHasFields.getFields().size() == 0)) { 283 BaseSection bSection = (BaseSection) currentViewWidget; 284 ExportElement exportItem = new ExportElement(); 285 exportItem.setSectionName(sectionName + viewName); 286 exportItem.setViewName(sectionName + viewName); 287 exportItem.setFieldLabel("???00"); 288 exportItem = getExportItemDetails(exportItem, bSection.getLayout(), true, viewName, sectionName); 289 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 return exportElements; 296 } 297 298 }