001    /**
002     * Copyright 2005-2012 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     */
016    package org.kuali.rice.kim.api.jaxb;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.rice.core.api.util.jaxb.MapStringStringAdapter;
020    
021    import javax.xml.bind.UnmarshalException;
022    import javax.xml.bind.annotation.adapters.NormalizedStringAdapter;
023    import javax.xml.bind.annotation.adapters.XmlAdapter;
024    import java.util.HashMap;
025    import java.util.Map;
026    
027    /**
028     * An XML adapter that converts between QualificationList objects and Map<String, String> objects.
029     * Unmarshalled keys and values will automatically be trimmed if non-null.
030     * 
031     * <p>This adapter will throw an exception during unmarshalling if blank or duplicate keys are encountered.
032     * 
033     * @author Kuali Rice Team (rice.collab@kuali.org)
034     */
035    public class QualificationListAdapter extends XmlAdapter<QualificationList,Map<String, String>> {
036    
037        /**
038         * @see javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal(java.lang.Object)
039         */
040        @Override
041        public Map<String, String> unmarshal(QualificationList v) throws Exception {
042            if (v != null) {
043                NormalizedStringAdapter normalizedStringAdapter = new NormalizedStringAdapter();
044                Map<String, String> map = new HashMap<String, String>();
045                for (MapStringStringAdapter.StringMapEntry stringMapEntry : v.getQualifications()) {
046                    String tempKey = normalizedStringAdapter.unmarshal(stringMapEntry.getKey());
047                    if (StringUtils.isBlank(tempKey)) {
048                        throw new UnmarshalException("Cannot create a qualification entry with a blank key");
049                    } else if (map.containsKey(tempKey)) {
050                        throw new UnmarshalException("Cannot create more than one qualification entry with a key of \"" + tempKey + "\"");
051                    }
052                    map.put(tempKey, normalizedStringAdapter.unmarshal(stringMapEntry.getValue()));
053                }
054            }
055            return null;
056        }
057    
058        /**
059         * @see javax.xml.bind.annotation.adapters.XmlAdapter#marshal(java.lang.Object)
060         */
061        @Override
062        public QualificationList marshal(Map<String, String> v) throws Exception {
063            return (v != null) ? new QualificationList(v) : null;
064        }
065    
066    }