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.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  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          String fieldValue = null;
48          if (!fieldWidget.getParent().getElement().getStyle().getDisplay().equals("none")){
49          if (fieldWidget instanceof HasText) {
50              HasText itemHasTextValue = (HasText) fieldWidget;
51              fieldValue = itemHasTextValue.getText();
52  
53          } else if (fieldWidget instanceof KSLabel) {
54              // ignore labels... as we're not interested
55          } else if (fieldWidget instanceof KSSelectedList) {
56              KSSelectedList selectedList = (KSSelectedList) fieldWidget;
57              List<KSItemLabel> selectedItems = selectedList.getSelectedItems();
58              String values = new String();
59              for (int j = 0; j < selectedItems.size(); j++) {
60                  values = selectedItems.get(j).getDisplayText();
61              }
62              fieldValue = values;
63          } else if (fieldWidget instanceof KSPicker) {
64              KSPicker picker = (KSPicker) fieldWidget;
65              if (picker.getInputWidget() instanceof HasText) {
66                  HasText item = (HasText) picker.getInputWidget();
67                  fieldValue = item.getText();
68              } else if (picker.getInputWidget() instanceof KSLabelList) {
69                  KSLabelList widget = (KSLabelList) picker.getInputWidget();
70                  List<String> selected = widget.getSelectedItemsForExport();
71                  for (int j = 0; j < selected.size(); j++) {
72                      if (fieldValue == null) {
73                          fieldValue = new String(selected.get(j));
74                      } else {
75                          fieldValue = fieldValue + ", " + selected.get(j);
76                      }
77                  }
78              }
79          } else if (fieldWidget instanceof ListBox) {
80              ListBox listBox = (ListBox) fieldWidget;
81              fieldValue = listBox.getItemText(listBox.getSelectedIndex());
82          } else if (fieldWidget instanceof SectionTitle) {
83              try {
84                  SectionTitle sectionTitle = (SectionTitle) fieldWidget;
85                  fieldValue = sectionTitle.getElement().getInnerText();
86                  // fieldValue = sectionTitle.getTitleText();
87              } catch (Exception e) {
88                  System.out.println("Section tile interface probleem - only in debugging.");
89                  e.printStackTrace();
90              }
91              // vrydag moved down } else if (fieldWidget instanceof ComplexPanel) {
92              // subExportElements = ExportUtils.getDetailsForWidget(fieldWidget,
93              // subExportElements, viewName, sectionName);
94          } else if (fieldWidget instanceof ReportExportWidget) {
95          	
96          	if (fieldWidget.isVisible()){
97              ReportExportWidget widget = (ReportExportWidget) fieldWidget;
98              if (widget.isExportElement() ) {
99                  fieldValue = widget.getExportFieldValue();
100                 exportItem.setSubset(widget.getExportElementSubset(exportItem));
101             }
102             }
103         } else if (fieldWidget instanceof ComplexPanel) {
104         	if(fieldWidget.isVisible())
105             {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         if (setFirstFieldValue) {
113             exportItem.setFieldValue(fieldValue);
114         } else {
115             exportItem.setFieldValue2(fieldValue);
116         }
117         }
118         // System.out.println(exportItem.getSectionName() + " : Label = " +
119         // exportItem.getFieldLabel() + " fieldValue : " +
120         // exportItem.getFieldValue() + " fieldValue2 : " +
121         // exportItem.getFieldValue2());
122         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         if (theWidget != null) {
135 
136             if (theWidget instanceof Controller) {
137                 Controller controller = (Controller) theWidget;
138                 return controller;
139             } else {
140                 return getController(theWidget.getParent());
141             }
142         } else {
143             return null;
144         }
145     }
146 
147     public static void handleExportClickEvent(Controller currentController, String format, String reportTitle) {
148         List<ExportElement> exportElements = new ArrayList<ExportElement>();
149         exportElements = currentController.getExportElementsFromView();
150         if (exportElements != null && exportElements.size() > 0) {
151             debugExportElementsArray(exportElements);
152             currentController.doReportExport(exportElements, format, reportTitle);
153         }
154     }
155 
156     public static void debugExportElementsArray(List<ExportElement> exportElements) {
157         System.out.println(" ");
158         System.out.println(" ");
159         System.out.println(" ");
160         System.out.println(" ");
161         System.out.println(" ");
162         System.out.println(" ");
163         System.out.println(" ");
164         System.out.println(" ");
165 
166         for (int i = 0; i < exportElements.size(); i++) {
167             System.out.println(exportElements.get(i).toString());
168             debutExportElementsArraySubList(exportElements.get(i).getSubset());
169         }
170     }
171 
172     private static void debutExportElementsArraySubList(List<ExportElement> exportElements) {
173         if (exportElements != null) {
174             System.out.println("Sub list : ");
175             for (int j = 0; j < exportElements.size(); j++) {
176                 ExportElement element = exportElements.get(j);
177                 System.out.println(element.toString());
178                 debutExportElementsArraySubList(element.getSubset());
179 
180             }
181         }
182     }
183 
184     public static ArrayList<ExportElement> getDetailsForWidget(SummaryTableSection tableSection, ArrayList<ExportElement> exportElements) {
185         SummaryTable sumTable = tableSection.getSummaryTable();
186         SummaryTableModel model = sumTable.getModel();
187         List<SummaryTableBlock> tableSectionList = model.getSectionList();
188         for (int i = 0; i < tableSectionList.size(); i++) {
189             SummaryTableBlock item = tableSectionList.get(i);
190             String blockName = item.getTitle();
191             List<SummaryTableRow> rowItems = item.getSectionRowList();
192             for (int j = 0; j < rowItems.size(); j++) {
193                 ExportElement element = new ExportElement();
194 				SummaryTableRow row = rowItems.get(j);
195 				element.setSectionName(blockName);
196 				element.setViewName(blockName);
197 				element.setFieldLabel(row.getTitle());
198 				element.setMandatory(row.isRequired());
199                 Widget fdWidget = row.getCell1();
200                 if (row.isShown()) {
201                     if (fdWidget != null) {
202                         //
203                         if (fdWidget instanceof KSListPanel) {
204                             element.setSubset(ExportUtils.getDetailsForWidget(fdWidget, blockName, blockName));
205                         } else {
206                             //
207                             element = ExportUtils.getExportItemDetails(element, fdWidget, true, blockName, blockName);
208                         }
209                     } else {
210                         if (row.getTitle() != null) {
211                             element.setFieldLabel(row.getTitle());
212                         }
213                     }
214                     //
215                     Widget fdWidget2 = row.getCell2();
216                     if (fdWidget2 != null) {
217                         element = ExportUtils.getExportItemDetails(element, fdWidget2, false, blockName, blockName);
218 
219                     } else {
220                         if (row.getTitle() != null) {
221                             element.setFieldLabel(row.getTitle());
222                         }
223                     }
224                     if (element != null && element.getViewName() != null) {
225                         exportElements.add(element);
226                     }
227                 }
228                 
229             }
230         }
231         return exportElements;
232     }
233 
234     public static List<ExportElement> getDetailsForWidget(Widget currentViewWidget, String viewName, String sectionName) {
235         List<ExportElement> childElements = new ArrayList<ExportElement>();
236         if (!currentViewWidget.getParent().getElement().getStyle().getDisplay().equals("none")){
237         if (currentViewWidget instanceof Section) {
238             Section widgetHasFields = (Section) currentViewWidget;
239             List<FieldDescriptor> widgetFields = widgetHasFields.getFields();
240             for (FieldDescriptor field : widgetFields) {
241 
242                 Widget fieldWidget = field.getFieldElement().getFieldWidget();
243                 ExportElement exportItem = new ExportElement();
244 				exportItem.setSectionName(sectionName + viewName);
245 				exportItem.setViewName(sectionName + viewName);
246 				exportItem.setFieldLabel(field.getFieldLabel());
247                 exportItem = getExportItemDetails(exportItem, fieldWidget, true, viewName, sectionName);
248                 //
249                 ExportUtils.addElementToElementArray(childElements, exportItem);
250                 //
251                 // Vrydag exportElements.add(getExportItemDetails(exportItem,
252                 // fieldWidget, true, viewName, sectionName));
253             }
254         } else if (currentViewWidget instanceof KSListPanel) {
255             KSListPanel ksListPanelWidget = (KSListPanel) currentViewWidget;
256             WidgetCollection children = ksListPanelWidget.getChildren();
257             for (int i = 0; i < children.size(); i++) {
258                 Widget child = children.get(i);
259 
260 				ExportElement exportItem = new ExportElement();
261 				exportItem.setSectionName(sectionName + viewName);
262 				exportItem.setViewName(sectionName + viewName);
263                 exportItem = getExportItemDetails(exportItem, child, true, viewName, sectionName);
264                 // Vrydag exportItem.setFieldLabel("");
265                 //
266                 ExportUtils.addElementToElementArray(childElements, exportItem);
267                 //
268                 // Vrydag exportElements.add(getExportItemDetails(exportItem, child,
269                 // true, viewName, sectionName));
270             }
271 
272         } else if (currentViewWidget instanceof ComplexPanel) {
273             ComplexPanel complexPanel = (ComplexPanel) currentViewWidget;
274             if (complexPanel.isVisible()){
275             for (int i = 0; i < complexPanel.getWidgetCount(); i++) {
276                 Widget child = complexPanel.getWidget(i);
277                 if (!(child instanceof KSButton)
278                 // Vrydag && !(child instanceof SpanPanel) && !(child instanceof WarnContainer)) {
279                         && !(child instanceof WarnContainer)) {
280 					
281 					ExportElement exportItem = new ExportElement();
282 					exportItem.setSectionName(sectionName);
283 					exportItem.setViewName(viewName);
284 					
285                     exportItem = getExportItemDetails(exportItem, child, true, viewName, sectionName);
286                     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         } else {
297 
298             System.out.println("ExportUtils does not cater for this type..." + currentViewWidget.getClass().getName());
299 
300         }
301         }
302         return childElements;
303     }
304 
305     public static ArrayList<ExportElement> getExportElementsFromView(Widget currentViewWidget, ArrayList<ExportElement> exportElements, String viewName, String sectionName) {
306         if (exportElements == null) {
307             exportElements = new ArrayList<ExportElement>();
308         }
309         if (currentViewWidget.getParent() == null || !currentViewWidget.getParent().getElement().getStyle().getDisplay().equals("none")) {
310             if (currentViewWidget instanceof VerticalSectionView) {
311                 Section widgetHasFields = (Section) currentViewWidget;
312                 List<FieldDescriptor> widgetFields = widgetHasFields.getFields();
313                 for (FieldDescriptor field : widgetFields) {
314                     ExportElement exportItem = new ExportElement();
315                     exportItem.setSectionName(sectionName + viewName);
316                     exportItem.setViewName(sectionName + viewName);
317                     exportItem.setFieldLabel(field.getFieldLabel());
318                     Widget fieldWidget = field.getFieldElement().getFieldWidget();
319 
320                     exportItem = getExportItemDetails(exportItem, fieldWidget, true, viewName, sectionName);
321 
322                     exportElements.add(exportItem);
323                 }
324                 if ((currentViewWidget instanceof BaseSection) && (widgetHasFields.getFields().size() == 0)) {
325                     BaseSection bSection = (BaseSection) currentViewWidget;
326                     ExportElement exportItem = new ExportElement();
327                     exportItem.setSectionName(sectionName + viewName);
328                     exportItem.setViewName(sectionName + viewName);
329                     exportItem.setFieldLabel("???00");
330                     exportItem = getExportItemDetails(exportItem, bSection.getLayout(), true, viewName, sectionName);
331                     exportElements.add(exportItem);
332 
333                 }
334             }
335         }
336         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         if (element.getFieldLabel() != null || element.getFieldValue() != null || element.getFieldValue2() != null || (element.getSubset() != null && element.getSubset().size() > 0)) {
342             elementArray.add(element);
343         }
344         return elementArray;
345     }
346 
347 }