View Javadoc
1   /**
2    * Copyright 2005-2014 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.uif.service.impl;
17  
18  import java.util.List;
19  import java.util.Map;
20  
21  import org.apache.log4j.Logger;
22  import org.kuali.rice.krad.service.DataDictionaryService;
23  import org.kuali.rice.krad.uif.UifConstants;
24  import org.kuali.rice.krad.uif.UifConstants.ViewType;
25  import org.kuali.rice.krad.uif.service.ViewService;
26  import org.kuali.rice.krad.uif.service.ViewTypeService;
27  import org.kuali.rice.krad.uif.view.View;
28  
29  /**
30   * Implementation of <code>ViewService</code>
31   *
32   * <p>
33   * Provides methods for retrieving View instances and carrying out the View
34   * lifecycle methods. Interacts with the configured <code>ViewHelperService</code>
35   * during the view lifecycle
36   * </p>
37   *
38   * @author Kuali Rice Team (rice.collab@kuali.org)
39   */
40  public class ViewServiceImpl implements ViewService {
41      private static final Logger LOG = Logger.getLogger(ViewServiceImpl.class);
42  
43      private DataDictionaryService dataDictionaryService;
44  
45      // TODO: remove once we can get beans by type from spring
46      private List<ViewTypeService> viewTypeServices;
47  
48      /**
49       * @see org.kuali.rice.krad.uif.service.ViewService#getViewById(java.lang.String)
50       */
51      public View getViewById(final String viewId) {
52          if (LOG.isDebugEnabled()) {
53              LOG.debug("retrieving view instance for id: " + viewId);
54          }
55          
56          return dataDictionaryService.getViewById(viewId);
57      }
58  
59      /**
60       * Retrieves the <code>ViewTypeService</code> for the given view type, then builds up the index based
61       * on the supported view type parameters and queries the dictionary service to retrieve the view
62       * based on its type and index
63       *
64       * @see org.kuali.rice.krad.uif.service.ViewService#getViewByType(org.kuali.rice.krad.uif.UifConstants.ViewType,
65       *      java.util.Map<java.lang.String,java.lang.String>)
66       */
67      public View getViewByType(ViewType viewType, Map<String, String> parameters) {
68          ViewTypeService typeService = getViewTypeService(viewType);
69          if (typeService == null) {
70              throw new RuntimeException("Unable to find view type service for view type name: " + viewType);
71          }
72  
73          Map<String, String> typeParameters = typeService.getParametersFromRequest(parameters);
74  
75          View view = dataDictionaryService.getViewByTypeIndex(viewType, typeParameters);
76          if (view == null) {
77              LOG.warn("View not found for type: " + viewType);
78          }
79  
80          return view;
81      }
82  
83      /**
84       * @see org.kuali.rice.krad.uif.service.ViewService#getViewIdForViewType(org.kuali.rice.krad.uif.UifConstants.ViewType,
85       * java.util.Map<java.lang.String,java.lang.String>)
86       */
87      public String getViewIdForViewType(ViewType viewType, Map<String, String> parameters) {
88          ViewTypeService typeService = getViewTypeService(viewType);
89          if (typeService == null) {
90              throw new RuntimeException("Unable to find view type service for view type name: " + viewType);
91          }
92  
93          Map<String, String> typeParameters = typeService.getParametersFromRequest(parameters);
94  
95          return dataDictionaryService.getViewIdByTypeIndex(viewType, typeParameters);
96      }
97  
98      public ViewTypeService getViewTypeService(UifConstants.ViewType viewType) {
99          if (viewTypeServices != null) {
100             for (ViewTypeService typeService : viewTypeServices) {
101                 if (viewType.equals(typeService.getViewTypeName())) {
102                     return typeService;
103                 }
104             }
105         }
106 
107         return null;
108     }
109 
110     public List<ViewTypeService> getViewTypeServices() {
111         return this.viewTypeServices;
112     }
113 
114     public void setViewTypeServices(List<ViewTypeService> viewTypeServices) {
115         this.viewTypeServices = viewTypeServices;
116     }
117 
118     protected DataDictionaryService getDataDictionaryService() {
119         return this.dataDictionaryService;
120     }
121 
122     public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
123         this.dataDictionaryService = dataDictionaryService;
124     }
125 
126 }