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