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.server.gwt;
10  
11  import java.util.List;
12  import java.util.UUID;
13  
14  import org.apache.log4j.Logger;
15  import org.kuali.student.common.assembly.data.Data;
16  import org.kuali.student.common.ui.client.service.GwtExportRpcService;
17  import org.kuali.student.common.ui.client.util.ExportElement;
18  import org.kuali.student.common.ui.client.util.ExportUtils;
19  import org.kuali.student.common.ui.server.screenreport.ScreenReportProcessor;
20  
21  import com.google.gwt.user.server.rpc.RemoteServiceServlet;
22  
23  @SuppressWarnings("serial")
24  public class ExportGwtRpcServlet extends RemoteServiceServlet implements GwtExportRpcService {
25  
26      final Logger logger = Logger.getLogger(ExportGwtRpcServlet.class);
27  
28      private ScreenReportProcessor reportProcessor;
29  
30      public ScreenReportProcessor getReportProcessor() {
31          return reportProcessor;
32      }
33  
34      public void setReportProcessor(ScreenReportProcessor reportProcessor) {
35          this.reportProcessor = reportProcessor;
36      }
37  
38      @Override
39      public String reportExport(List<ExportElement> exportElements, Data root, String templateName, String exportFormat, String reportTitle) {
40          String exportId = null;
41          boolean exportBasedOnView = true; // TODO Nina do we want this as a system Property??
42          try {
43              // If templateName is null, then default to default Template ie base.template
44              if (templateName == null) {
45                  templateName = "base.template";
46              }
47              logger.info("Template Name = " + templateName + " For format = " + exportFormat);
48              byte[] exportOutput = null;
49              if (exportBasedOnView) {
50                  exportOutput = exportBasedOnView(exportElements, templateName, exportFormat, reportTitle);
51  
52              } else {
53                  exportOutput = exportBasedOnDataModel(root, templateName, exportFormat, reportTitle);
54              }
55              exportId = this.getExportId();
56              logger.info("Export succesful - Export ID = " + exportId);
57              getThreadLocalRequest().getSession(true).setAttribute(exportId, exportOutput);
58          } catch (RuntimeException e) {
59              e.printStackTrace();
60              throw e;
61          }
62          return exportId;
63      }
64  
65      private byte[] exportBasedOnDataModel(Data root, String templateName, String exportFormat, String reportTitle) {
66          byte[] exportOutput = null;
67          if (exportFormat.equals(ExportUtils.PDF)) {
68              exportOutput = reportProcessor.createPdf(root, templateName, reportTitle);
69          } else if (exportFormat.equals(ExportUtils.DOC)) {
70              exportOutput = reportProcessor.createDoc(root, templateName, reportTitle);
71  
72          } else if (exportFormat.equals(ExportUtils.XLS)) {
73              exportOutput = reportProcessor.createXls(root, templateName, reportTitle);
74          }
75          return exportOutput;
76      }
77  
78      private byte[] exportBasedOnView(List<ExportElement> exportElements, String templateName, String exportFormat, String reportTitle) {
79          byte[] exportOutput = null;
80          if (exportFormat.equals(ExportUtils.PDF)) {
81              exportOutput = reportProcessor.createPdf(exportElements, templateName, reportTitle);
82          } else if (exportFormat.equals(ExportUtils.DOC)) {
83              exportOutput = reportProcessor.createDoc(exportElements, templateName, reportTitle);
84  
85          } else if (exportFormat.equals(ExportUtils.XLS)) {
86              exportOutput = reportProcessor.createXls(exportElements, templateName, reportTitle);
87          }
88          return exportOutput;
89      }
90  
91      private String getExportId() {
92          return UUID.randomUUID().toString();
93      }
94  
95  }