001/**
002 * Copyright 2010-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.common.util;
017
018import org.apache.commons.lang3.StringUtils;
019
020public class OrgUtils {
021
022        /**
023         * Given {@code org.kuali} return {@code kuali}
024         */
025        public static final String getOrgCode(String organizationGroupId) {
026                int pos = StringUtils.lastIndexOf(organizationGroupId, ".");
027                if (pos == -1) {
028                        return organizationGroupId;
029                } else {
030                        return StringUtils.substring(organizationGroupId, pos + 1);
031                }
032        }
033
034        /**
035         * Given {@code org.kuali} and {@code org.kuali.rice} return {@code rice}<br>
036         * Given {@code org.kuali} and {@code org.kuali.student.web} return {@code student}<br>
037         */
038        public static final String getGroupCode(String organizationGroupId, String groupId) {
039                if (!StringUtils.startsWith(groupId, organizationGroupId)) {
040                        throw new IllegalArgumentException(groupId + " does not start with " + organizationGroupId);
041                }
042                String code = StringUtils.remove(groupId, organizationGroupId);
043                if (StringUtils.startsWith(code, ".")) {
044                        code = StringUtils.substring(code, 1);
045                }
046                int pos = StringUtils.indexOf(code, ".");
047                if (pos != -1) {
048                        code = StringUtils.substring(code, 0, pos);
049                }
050                return code;
051        }
052
053        /**
054         * Given {@code org.kuali} and {@code org.kuali.student.web} return {@code org.kuali.student}<br>
055         */
056        public static final String getGroupBase(String organizationGroupId, String groupId) {
057                return organizationGroupId + "." + getGroupCode(organizationGroupId, groupId);
058        }
059
060}