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