View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kim.api.jaxb;
17  
18  import javax.xml.bind.MarshalException;
19  import javax.xml.bind.UnmarshalException;
20  import javax.xml.bind.annotation.adapters.NormalizedStringAdapter;
21  import javax.xml.bind.annotation.adapters.XmlAdapter;
22  
23  import org.kuali.rice.core.util.jaxb.NameAndNamespacePair;
24  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
25  import org.kuali.rice.kim.api.common.template.Template;
26  
27  /**
28   * An XML adapter that converts between a NameAndNamespacePair and a permission template ID.
29   * 
30   * @author Kuali Rice Team (rice.collab@kuali.org)
31   */
32  public class NameAndNamespacePairToPermTemplateIdAdapter extends XmlAdapter<NameAndNamespacePair,String> {
33  
34      /**
35       * @see javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal(java.lang.Object)
36       */
37      @Override
38      public String unmarshal(NameAndNamespacePair v) throws Exception {
39          if (v != null) {
40              Template permissionTemplate = KimApiServiceLocator.getPermissionService().findPermTemplateByNamespaceCodeAndName(
41                      v.getNamespaceCode(), new NormalizedStringAdapter().unmarshal(v.getName()));
42              if (permissionTemplate == null) {
43                  throw new UnmarshalException("Cannot find permission template with namespace \"" + v.getNamespaceCode() + "\" and name \"" + v.getName() + "\"");
44              }
45              return permissionTemplate.getId();
46          }
47          return null;
48      }
49  
50      /**
51       * @see javax.xml.bind.annotation.adapters.XmlAdapter#marshal(java.lang.Object)
52       */
53      @Override
54      public NameAndNamespacePair marshal(String v) throws Exception {
55          if (v != null) {
56              Template permissionTemplate = KimApiServiceLocator.getPermissionService().getPermissionTemplate(v);
57              if (permissionTemplate == null) {
58                  throw new MarshalException("Cannot find permission template with ID \"" + v + "\"");
59              }
60              return new NameAndNamespacePair(permissionTemplate.getNamespaceCode(), permissionTemplate.getName());
61          }
62          return null;
63      }
64  
65  }