001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.uif.service.impl;
017
018import java.util.List;
019import java.util.Map;
020
021import org.apache.log4j.Logger;
022import org.kuali.rice.krad.service.DataDictionaryService;
023import org.kuali.rice.krad.uif.UifConstants;
024import org.kuali.rice.krad.uif.UifConstants.ViewType;
025import org.kuali.rice.krad.uif.service.ViewService;
026import org.kuali.rice.krad.uif.service.ViewTypeService;
027import org.kuali.rice.krad.uif.view.View;
028
029/**
030 * Implementation of <code>ViewService</code>
031 *
032 * <p>
033 * Provides methods for retrieving View instances and carrying out the View
034 * lifecycle methods. Interacts with the configured <code>ViewHelperService</code>
035 * during the view lifecycle
036 * </p>
037 *
038 * @author Kuali Rice Team (rice.collab@kuali.org)
039 */
040public class ViewServiceImpl implements ViewService {
041    private static final Logger LOG = Logger.getLogger(ViewServiceImpl.class);
042
043    private DataDictionaryService dataDictionaryService;
044
045    // TODO: remove once we can get beans by type from spring
046    private List<ViewTypeService> viewTypeServices;
047
048    /**
049     * @see org.kuali.rice.krad.uif.service.ViewService#getViewById(java.lang.String)
050     */
051    public View getViewById(final String viewId) {
052        if (LOG.isDebugEnabled()) {
053            LOG.debug("retrieving view instance for id: " + viewId);
054        }
055        
056        return dataDictionaryService.getViewById(viewId);
057    }
058
059    /**
060     * Retrieves the <code>ViewTypeService</code> for the given view type, then builds up the index based
061     * on the supported view type parameters and queries the dictionary service to retrieve the view
062     * based on its type and index
063     *
064     * @see org.kuali.rice.krad.uif.service.ViewService#getViewByType(org.kuali.rice.krad.uif.UifConstants.ViewType,
065     *      java.util.Map<java.lang.String,java.lang.String>)
066     */
067    public View getViewByType(ViewType viewType, Map<String, String> parameters) {
068        ViewTypeService typeService = getViewTypeService(viewType);
069        if (typeService == null) {
070            throw new RuntimeException("Unable to find view type service for view type name: " + viewType);
071        }
072
073        Map<String, String> typeParameters = typeService.getParametersFromRequest(parameters);
074
075        View view = dataDictionaryService.getViewByTypeIndex(viewType, typeParameters);
076        if (view == null) {
077            LOG.warn("View not found for type: " + viewType);
078        }
079
080        return view;
081    }
082
083    /**
084     * @see org.kuali.rice.krad.uif.service.ViewService#getViewIdForViewType(org.kuali.rice.krad.uif.UifConstants.ViewType,
085     * java.util.Map<java.lang.String,java.lang.String>)
086     */
087    public String getViewIdForViewType(ViewType viewType, Map<String, String> parameters) {
088        ViewTypeService typeService = getViewTypeService(viewType);
089        if (typeService == null) {
090            throw new RuntimeException("Unable to find view type service for view type name: " + viewType);
091        }
092
093        Map<String, String> typeParameters = typeService.getParametersFromRequest(parameters);
094
095        return dataDictionaryService.getViewIdByTypeIndex(viewType, typeParameters);
096    }
097
098    public ViewTypeService getViewTypeService(UifConstants.ViewType viewType) {
099        if (viewTypeServices != null) {
100            for (ViewTypeService typeService : viewTypeServices) {
101                if (viewType.equals(typeService.getViewTypeName())) {
102                    return typeService;
103                }
104            }
105        }
106
107        return null;
108    }
109
110    public List<ViewTypeService> getViewTypeServices() {
111        return this.viewTypeServices;
112    }
113
114    public void setViewTypeServices(List<ViewTypeService> viewTypeServices) {
115        this.viewTypeServices = viewTypeServices;
116    }
117
118    protected DataDictionaryService getDataDictionaryService() {
119        return this.dataDictionaryService;
120    }
121
122    public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
123        this.dataDictionaryService = dataDictionaryService;
124    }
125
126}