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.kim.api.jaxb;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.util.jaxb.MapStringStringAdapter;
020
021import javax.xml.bind.UnmarshalException;
022import javax.xml.bind.annotation.adapters.NormalizedStringAdapter;
023import javax.xml.bind.annotation.adapters.XmlAdapter;
024import java.util.HashMap;
025import java.util.Map;
026
027/**
028 * An XML adapter that converts between PermissionDetailList 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 */
035public class PermissionDetailListAdapter extends XmlAdapter<PermissionDetailList,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(PermissionDetailList 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.getPermissionDetails()) {
046                String tempKey = normalizedStringAdapter.unmarshal(stringMapEntry.getKey());
047                if (StringUtils.isBlank(tempKey)) {
048                    throw new UnmarshalException("Cannot create a permission detail entry with a blank key");
049                } else if (map.containsKey(tempKey)) {
050                    throw new UnmarshalException("Cannot create more than one permission detail 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 PermissionDetailList marshal(Map<String, String> v) throws Exception {
063        return (v != null) ? new PermissionDetailList(v) : null;
064    }
065
066}