View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.inquiry;
17  
18  import java.util.Set;
19  
20  import org.kuali.rice.krad.bo.Exporter;
21  import org.kuali.rice.krad.datadictionary.DataObjectEntry;
22  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
23  import org.kuali.rice.krad.uif.view.InquiryView;
24  import org.kuali.rice.krad.uif.view.View;
25  import org.kuali.rice.krad.uif.view.ViewPresentationControllerBase;
26  import org.kuali.rice.krad.util.KRADConstants;
27  import org.kuali.rice.krad.web.form.InquiryForm;
28  import org.kuali.rice.krad.web.form.UifFormBase;
29  
30  /**
31   * Implementation of {@link org.kuali.rice.krad.uif.view.ViewPresentationController} for
32   * {@link InquiryView} instances
33   *
34   * <p>
35   * Adds flag for export of inquiry record
36   * </p>
37   *
38   * @author Kuali Rice Team (rice.collab@kuali.org)
39   */
40  public class InquiryViewPresentationControllerBase extends ViewPresentationControllerBase {
41      private static final long serialVersionUID = 7504225899471226403L;
42  
43      /**
44       * @see org.kuali.rice.krad.uif.view.ViewPresentationController#getActionFlags(org.kuali.rice.krad.uif.view.View,
45       * org.kuali.rice.krad.web.form.UifFormBase)
46       */
47      @Override
48      public Set<String> getActionFlags(View view, UifFormBase model) {
49          Set<String> actionFlags = super.getActionFlags(view, model);
50  
51          InquiryView inquiryView = (InquiryView) view;
52          InquiryForm inquiryForm = (InquiryForm) model;
53  
54          if (isExportSupported(inquiryView, inquiryForm)) {
55              actionFlags.add(KRADConstants.KUALI_ACTION_CAN_EXPORT);
56          }
57  
58          return actionFlags;
59      }
60  
61      /**
62       * Examines the data objects data dictionary entry to determine if it supports XML export or not
63       * @param view inquiry view
64       *
65       * @return boolean true if it supports export, false if not
66       */
67      protected boolean isExportSupported(InquiryView view, InquiryForm form) {
68          Object dataObject = form.getDataObject();
69  
70          if (dataObject == null) {
71              return false;
72          }
73  
74          DataObjectEntry dataObjectEntry =
75                  KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().getDataObjectEntry(
76                          view.getDataObjectClassName().getName());
77  
78          Class<? extends Exporter> exporterClass = dataObjectEntry.getExporterClass();
79          if (exporterClass != null) {
80              try {
81                  Exporter exporter = exporterClass.newInstance();
82                  if (exporter.getSupportedFormats(dataObjectEntry.getDataObjectClass()).contains(
83                          KRADConstants.XML_FORMAT)) {
84                      return true;
85                  }
86              } catch (Exception e) {
87                  throw new RuntimeException("Failed to locate or create exporter class: " + exporterClass);
88              }
89          }
90  
91          return false;
92      }
93  }