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.datadictionary.parse;
017
018import org.kuali.rice.krad.util.KRADUtils;
019import org.springframework.core.convert.TypeDescriptor;
020import org.springframework.core.convert.converter.GenericConverter;
021
022import java.util.HashSet;
023import java.util.Map;
024import java.util.Set;
025
026/**
027 * Converts a string configured in the dictionary for a map property type to a Map
028 * object using the convention of commas to separate map entries and a colon to separate
029 * the key value pair
030 *
031 * @author Kuali Rice Team (rice.collab@kuali.org)
032 */
033public class StringMapConverter implements GenericConverter {
034
035    @Override
036    public Set<ConvertiblePair> getConvertibleTypes() {
037        Set<ConvertiblePair> pairs = new HashSet<ConvertiblePair>();
038
039        ConvertiblePair stringMapPair = new ConvertiblePair(String.class, Map.class);
040        pairs.add(stringMapPair);
041
042        return pairs;
043    }
044
045    @Override
046    public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
047        return KRADUtils.convertStringParameterToMap((String) source);
048    }
049}