View Javadoc

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