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