View Javadoc

1   /**
2    * Copyright 2005-2012 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.HashMap;
19  import java.util.List;
20  import java.util.Map;
21  
22  import org.apache.log4j.Logger;
23  import org.apache.log4j.Priority;
24  import org.kuali.rice.krad.service.DataDictionaryService;
25  import org.kuali.rice.krad.uif.UifConstants;
26  import org.kuali.rice.krad.uif.UifConstants.ViewStatus;
27  import org.kuali.rice.krad.uif.view.View;
28  import org.kuali.rice.krad.uif.service.ViewHelperService;
29  import org.kuali.rice.krad.uif.service.ViewService;
30  import org.kuali.rice.krad.uif.service.ViewTypeService;
31  import org.kuali.rice.krad.uif.UifConstants.ViewType;
32  import org.kuali.rice.krad.web.form.UifFormBase;
33  
34  /**
35   * Implementation of <code>ViewService</code>
36   *
37   * <p>
38   * Provides methods for retrieving View instances and carrying out the View
39   * lifecycle methods. Interacts with the configured <code>ViewHelperService</code>
40   * during the view lifecycle
41   * </p>
42   *
43   * @author Kuali Rice Team (rice.collab@kuali.org)
44   */
45  public class ViewServiceImpl implements ViewService {
46      private static final Logger LOG = Logger.getLogger(ViewServiceImpl.class);
47  
48      private DataDictionaryService dataDictionaryService;
49  
50      // TODO: remove once we can get beans by type from spring
51      private List<ViewTypeService> viewTypeServices;
52  
53      /**
54       * @see org.kuali.rice.krad.uif.service.ViewService#getViewById(java.lang.String)
55       */
56      public View getViewById(String viewId) {
57          if (LOG.isDebugEnabled()) {
58              LOG.debug("retrieving view instance for id: " + viewId);
59          }
60  
61          View view = dataDictionaryService.getViewById(viewId);
62          if (view == null) {
63              LOG.warn("View not found for id: " + viewId);
64          } else {
65              if (LOG.isDebugEnabled()) {
66                  LOG.debug("Updating view status to CREATED for view: " + view.getId());
67              }
68              view.setViewStatus(ViewStatus.CREATED);
69          }
70  
71          return view;
72      }
73  
74      /**
75       * Retrieves the <code>ViewTypeService</code> for the given view type, then builds up the index based
76       * on the supported view type parameters and queries the dictionary service to retrieve the view
77       * based on its type and index
78       *
79       * @see org.kuali.rice.krad.uif.service.ViewService#getViewByType(org.kuali.rice.krad.uif.UifConstants.ViewType,
80       *      java.util.Map<java.lang.String,java.lang.String>)
81       */
82      public View getViewByType(ViewType viewType, Map<String, String> parameters) {
83          ViewTypeService typeService = getViewTypeService(viewType);
84          if (typeService == null) {
85              throw new RuntimeException("Unable to find view type service for view type name: " + viewType);
86          }
87  
88          Map<String, String> typeParameters = typeService.getParametersFromRequest(parameters);
89  
90          Map<String, String> indexKey = new HashMap<String, String>();
91          for (Map.Entry<String, String> parameter : typeParameters.entrySet()) {
92              indexKey.put(parameter.getKey(), parameter.getValue());
93          }
94  
95          View view = dataDictionaryService.getViewByTypeIndex(viewType, indexKey);
96          if (view == null) {
97              LOG.warn("View not found for type: " + viewType);
98          } else {
99              LOG.debug("Updating view status to CREATED for view: " + view.getId());
100             view.setViewStatus(ViewStatus.CREATED);
101         }
102 
103         return view;
104     }
105 
106     /**
107      * @see org.kuali.rice.krad.uif.service.ViewService#buildView(org.kuali.rice.krad.uif.view.View, java.lang.Object,
108      * java.util.Map<java.lang.String,java.lang.String>)
109      */
110     public void buildView(View view, Object model, Map<String, String> parameters) {
111         // get the configured helper service for the view
112         ViewHelperService helperService = view.getViewHelperService();
113 
114         // populate view from request parameters
115         helperService.populateViewFromRequestParameters(view, parameters);
116 
117         // backup view request parameters on form for recreating lost views (session timeout)
118         ((UifFormBase) model).setViewRequestParameters(view.getViewRequestParameters());
119 
120         // run view lifecycle
121         performViewLifecycle(view, model, parameters);
122     }
123 
124     /**
125      * Initializes a newly created <code>View</code> instance. Each component of the tree is invoked
126      * to perform setup based on its configuration. In addition helper service methods are invoked to
127      * perform custom initialization
128      *
129      * @param view - view instance to initialize
130      * @param model - object instance containing the view data
131      * @param parameters - Map of key values pairs that provide configuration for the <code>View</code>, this
132      * is generally comes from the request and can be the request parameter Map itself. Any parameters
133      * not valid for the View will be filtered out
134      */
135     protected void performViewLifecycle(View view, Object model, Map<String, String> parameters) {
136         // get the configured helper service for the view
137         ViewHelperService helperService = view.getViewHelperService();
138 
139         // invoke initialize phase on the views helper service
140         // Heavily called method showed up on profile as a hotspot.  Putting log statements in checks cuts execution time by ~75%
141         if (LOG.isEnabledFor(Priority.INFO)) {
142             LOG.info("performing initialize phase for view: " + view.getId());
143         }
144         helperService.performInitialization(view, model);
145 
146         // do indexing
147         if (LOG.isDebugEnabled()) {
148             LOG.debug("processing indexing for view: " + view.getId());
149         }
150         view.index();
151 
152         // update status on view
153         if (LOG.isDebugEnabled()) {
154             LOG.debug("Updating view status to INITIALIZED for view: " + view.getId());
155         }
156         view.setViewStatus(ViewStatus.INITIALIZED);
157 
158         // Apply Model Phase
159         if (LOG.isEnabledFor(Priority.INFO)) {
160             LOG.info("performing apply model phase for view: " + view.getId());
161         }
162         helperService.performApplyModel(view, model);
163 
164         // do indexing
165         if (LOG.isEnabledFor(Priority.INFO)) {
166             LOG.info("reindexing after apply model for view: " + view.getId());
167         }
168         view.index();
169 
170         // Finalize Phase
171         if (LOG.isEnabledFor(Priority.INFO)) {
172             LOG.info("performing finalize phase for view: " + view.getId());
173         }
174         helperService.performFinalize(view, model);
175 
176         // do indexing
177         if (LOG.isEnabledFor(Priority.INFO)) {
178             LOG.info("processing final indexing for view: " + view.getId());
179         }
180         view.index();
181 
182         // update status on view
183         if (LOG.isDebugEnabled()) {
184             LOG.debug("Updating view status to FINAL for view: " + view.getId());
185         }
186         view.setViewStatus(ViewStatus.FINAL);
187     }
188 
189     public ViewTypeService getViewTypeService(UifConstants.ViewType viewType) {
190         if (viewTypeServices != null) {
191             for (ViewTypeService typeService : viewTypeServices) {
192                 if (viewType.equals(typeService.getViewTypeName())) {
193                     return typeService;
194                 }
195             }
196         }
197 
198         return null;
199     }
200 
201     public List<ViewTypeService> getViewTypeServices() {
202         return this.viewTypeServices;
203     }
204 
205     public void setViewTypeServices(List<ViewTypeService> viewTypeServices) {
206         this.viewTypeServices = viewTypeServices;
207     }
208 
209     protected DataDictionaryService getDataDictionaryService() {
210         return this.dataDictionaryService;
211     }
212 
213     public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
214         this.dataDictionaryService = dataDictionaryService;
215     }
216 
217 }