View Javadoc
1   /**
2    * Copyright 2014 Internet2
3    *
4    * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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  /*
17   * Copyright (C) 2006-2007 blair christensen.
18   * All Rights Reserved.
19   *
20   * You may use and distribute under the same terms as Grouper itself.
21   */
22  
23  package edu.internet2.middleware.grouper.app.gsh;
24  import bsh.CallStack;
25  import bsh.Interpreter;
26  import edu.internet2.middleware.grouper.Group;
27  import edu.internet2.middleware.grouper.GroupFinder;
28  import edu.internet2.middleware.grouper.GrouperSession;
29  import edu.internet2.middleware.grouper.Field;
30  import edu.internet2.middleware.grouper.Member;
31  import edu.internet2.middleware.grouper.SubjectFinder;
32  import edu.internet2.middleware.grouper.exception.GroupNotFoundException;
33  import edu.internet2.middleware.grouper.exception.InsufficientPrivilegeException;
34  import edu.internet2.middleware.grouper.exception.MemberAddException;
35  import edu.internet2.middleware.grouper.exception.SchemaException;
36  import edu.internet2.middleware.subject.Subject;
37  import edu.internet2.middleware.subject.SubjectNotFoundException;
38  import edu.internet2.middleware.subject.SubjectNotUniqueException;
39  
40  /**
41   * Add a member.
42   * <p/>
43   * @author  blair christensen.
44   * @version $Id: addMember.java,v 1.5 2009-11-02 03:50:51 mchyzer Exp $
45   * @since   0.0.1
46   */
47  public class addMember {
48  
49    // PUBLIC CLASS METHODS //
50  
51    /**
52     * Add a member.
53     * <p/>
54     * @param   i           BeanShell interpreter.
55     * @param   stack       BeanShell call stack.
56     * @param   group       Add {@link Member} to {@link Group} with this name.
57     * @param   subjId      Add {@link Subject} with this <i>subject id</i> as a member.
58     * @return  True if succeeds.
59     * @throws  GrouperShellException
60     * @since   0.0.1
61     */
62    public static boolean invoke(
63      Interpreter i, CallStack stack, String group, String subjId
64    ) 
65      throws  GrouperShellException
66    {
67      GrouperShell.setOurCommand(i, true);
68      try {
69        GrouperSession  s     = GrouperShell.getSession(i);
70        return invoke(s, group, subjId, Group.getDefaultList());
71      }
72      catch (GroupNotFoundException eGNF)         {
73        GrouperShell.error(i, eGNF);
74      }
75      catch (InsufficientPrivilegeException eIP)  {
76        GrouperShell.error(i, eIP);
77      }
78      catch (MemberAddException eMA)              {
79        GrouperShell.error(i, eMA);
80      }
81      catch (SubjectNotFoundException eSNF)       {
82        GrouperShell.error(i, eSNF); 
83      }
84      catch (SubjectNotUniqueException eSNU)      {
85        GrouperShell.error(i, eSNU); 
86      }
87      return false;
88    }
89  
90    /**
91     * Add a member.
92     * <p/>
93     * @param   i           BeanShell interpreter.
94     * @param   stack       BeanShell call stack.
95     * @param   group       Add {@link Member} to {@link Group} with this name.
96     * @param   subjId      Add {@link Subject} with this <i>subject id</i> as a member.
97     * @param   field
98     * @return  True if succeeds.
99     * @throws  GrouperShellException
100    * @since   0.0.1
101    */
102   public static boolean invoke(
103     Interpreter i, CallStack stack, String group, String subjId, Field field
104   ) 
105     throws  GrouperShellException
106   {
107     GrouperShell.setOurCommand(i, true);
108     try {
109       GrouperSession  s     = GrouperShell.getSession(i);
110       return invoke(s, group, subjId, field);
111     }
112     catch (GroupNotFoundException eGNF)         {
113       GrouperShell.error(i, eGNF);
114     }
115     catch (InsufficientPrivilegeException eIP)  {
116       GrouperShell.error(i, eIP);
117     }
118     catch (MemberAddException eMA)              {
119       GrouperShell.error(i, eMA);
120     }
121     catch (SubjectNotFoundException eSNF)       {
122       GrouperShell.error(i, eSNF);
123     }
124     catch (SubjectNotUniqueException eSNU)      {
125       GrouperShell.error(i, eSNU);
126     }
127     catch (SchemaException e)                   {
128       GrouperShell.error(i, e);
129     }
130     return false;
131   }
132 
133   /**
134    * Add a member.
135    * <p/>
136    * @param   grouperSession
137    * @param   group       Add {@link Member} to {@link Group} with this name.
138    * @param   subjId      Add {@link Subject} with this <i>subject id</i> as a member.
139    * @param   field
140    * @return  True if succeeds.
141    */
142   public static boolean invoke(GrouperSession grouperSession, String group, String subjId, Field field) {
143     Group           g     = GroupFinder.findByName(grouperSession, group, true);
144     Subject         subj  = SubjectFinder.findByIdOrIdentifier(subjId, true);
145     g.addMember(subj, field, false);
146     return true;
147   }
148 
149 } // public class addMember
150