View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.rice.kim.api.jaxb;
20  
21  import javax.xml.bind.MarshalException;
22  import javax.xml.bind.UnmarshalException;
23  import javax.xml.bind.annotation.adapters.NormalizedStringAdapter;
24  import javax.xml.bind.annotation.adapters.XmlAdapter;
25  
26  import org.kuali.rice.core.util.jaxb.NameAndNamespacePair;
27  import org.kuali.rice.kim.api.common.template.Template;
28  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
29  
30  /**
31   * An XML adapter that converts between a NameAndNamespacePair and a permission template ID.
32   * 
33   * @author Kuali Rice Team (rice.collab@kuali.org)
34   */
35  public class NameAndNamespacePairToPermTemplateIdAdapter extends XmlAdapter<NameAndNamespacePair,String> {
36  
37      /**
38       * @see javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal(java.lang.Object)
39       */
40      @Override
41      public String unmarshal(NameAndNamespacePair v) throws Exception {
42          if (v != null) {
43              Template permissionTemplate = KimApiServiceLocator.getPermissionService().findPermTemplateByNamespaceCodeAndName(
44                      v.getNamespaceCode(), new NormalizedStringAdapter().unmarshal(v.getName()));
45              if (permissionTemplate == null) {
46                  throw new UnmarshalException("Cannot find permission template with namespace \"" + v.getNamespaceCode() + "\" and name \"" + v.getName() + "\"");
47              }
48              return permissionTemplate.getId();
49          }
50          return null;
51      }
52  
53      /**
54       * @see javax.xml.bind.annotation.adapters.XmlAdapter#marshal(java.lang.Object)
55       */
56      @Override
57      public NameAndNamespacePair marshal(String v) throws Exception {
58          if (v != null) {
59              Template permissionTemplate = KimApiServiceLocator.getPermissionService().getPermissionTemplate(v);
60              if (permissionTemplate == null) {
61                  throw new MarshalException("Cannot find permission template with ID \"" + v + "\"");
62              }
63              return new NameAndNamespacePair(permissionTemplate.getNamespaceCode(), permissionTemplate.getName());
64          }
65          return null;
66      }
67  
68  }