View Javadoc

1   /**
2    * Copyright 2010-2013 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.common.util;
17  
18  import org.apache.commons.lang3.StringUtils;
19  
20  public class OrgUtils {
21  
22  	/**
23  	 * Given {@code org.kuali} return {@code kuali}
24  	 */
25  	public static final String getOrgCode(String organizationGroupId) {
26  		int pos = StringUtils.lastIndexOf(organizationGroupId, ".");
27  		if (pos == -1) {
28  			return organizationGroupId;
29  		} else {
30  			return StringUtils.substring(organizationGroupId, pos + 1);
31  		}
32  	}
33  
34  	/**
35  	 * Given {@code org.kuali} and {@code org.kuali.rice} return {@code rice}<br>
36  	 * Given {@code org.kuali} and {@code org.kuali.student.web} return {@code student}<br>
37  	 */
38  	public static final String getGroupCode(String organizationGroupId, String groupId) {
39  		if (!StringUtils.startsWith(groupId, organizationGroupId)) {
40  			throw new IllegalArgumentException(groupId + " does not start with " + organizationGroupId);
41  		}
42  		String code = StringUtils.remove(groupId, organizationGroupId);
43  		if (StringUtils.startsWith(code, ".")) {
44  			code = StringUtils.substring(code, 1);
45  		}
46  		int pos = StringUtils.indexOf(code, ".");
47  		if (pos != -1) {
48  			code = StringUtils.substring(code, 0, pos);
49  		}
50  		return code;
51  	}
52  
53  	/**
54  	 * Given {@code org.kuali} and {@code org.kuali.student.web} return {@code org.kuali.student}<br>
55  	 */
56  	public static final String getGroupBase(String organizationGroupId, String groupId) {
57  		return organizationGroupId + "." + getGroupCode(organizationGroupId, groupId);
58  	}
59  
60  }