001/*
002 * The Kuali Financial System, a comprehensive financial management system for higher education.
003 * 
004 * Copyright 2005-2014 The Kuali Foundation
005 * 
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU Affero General Public License as
008 * published by the Free Software Foundation, either version 3 of the
009 * License, or (at your option) any later version.
010 * 
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU Affero General Public License for more details.
015 * 
016 * You should have received a copy of the GNU Affero General Public License
017 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package org.kuali.rice.kim.api.jaxb;
020
021import javax.xml.bind.MarshalException;
022import javax.xml.bind.UnmarshalException;
023import javax.xml.bind.annotation.adapters.NormalizedStringAdapter;
024import javax.xml.bind.annotation.adapters.XmlAdapter;
025
026import org.kuali.rice.core.util.jaxb.NameAndNamespacePair;
027import org.kuali.rice.kim.api.common.template.Template;
028import org.kuali.rice.kim.api.services.KimApiServiceLocator;
029
030/**
031 * An XML adapter that converts between a NameAndNamespacePair and a permission template ID.
032 * 
033 * @author Kuali Rice Team (rice.collab@kuali.org)
034 */
035public class NameAndNamespacePairToPermTemplateIdAdapter extends XmlAdapter<NameAndNamespacePair,String> {
036
037    /**
038     * @see javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal(java.lang.Object)
039     */
040    @Override
041    public String unmarshal(NameAndNamespacePair v) throws Exception {
042        if (v != null) {
043            Template permissionTemplate = KimApiServiceLocator.getPermissionService().findPermTemplateByNamespaceCodeAndName(
044                    v.getNamespaceCode(), new NormalizedStringAdapter().unmarshal(v.getName()));
045            if (permissionTemplate == null) {
046                throw new UnmarshalException("Cannot find permission template with namespace \"" + v.getNamespaceCode() + "\" and name \"" + v.getName() + "\"");
047            }
048            return permissionTemplate.getId();
049        }
050        return null;
051    }
052
053    /**
054     * @see javax.xml.bind.annotation.adapters.XmlAdapter#marshal(java.lang.Object)
055     */
056    @Override
057    public NameAndNamespacePair marshal(String v) throws Exception {
058        if (v != null) {
059            Template permissionTemplate = KimApiServiceLocator.getPermissionService().getPermissionTemplate(v);
060            if (permissionTemplate == null) {
061                throw new MarshalException("Cannot find permission template with ID \"" + v + "\"");
062            }
063            return new NameAndNamespacePair(permissionTemplate.getNamespaceCode(), permissionTemplate.getName());
064        }
065        return null;
066    }
067
068}