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.config;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import org.apache.commons.lang3.StringUtils;
022import org.springframework.util.Assert;
023
024/**
025 * @deprecated
026 */
027@Deprecated
028public class ConfigUtils {
029
030        public static final String DELIMITER = ":";
031
032        public static List<ProjectConfig> getProjectConfigs(List<String> configIds) {
033                List<ProjectConfig> requests = new ArrayList<ProjectConfig>();
034                for (String configId : configIds) {
035                        ProjectConfig request = ConfigUtils.getProjectConfig(configId);
036                        requests.add(request);
037                }
038                return requests;
039        }
040
041        public static String getConfigId(ProjectConfig config) {
042                return getConfigId(config.getGroupId(), config.getArtifactId(), config.getContextId());
043        }
044
045        public static String getConfigId(String groupId, String artifactId, String contextId) {
046                Assert.hasText(groupId, "groupId is blank");
047                Assert.hasText(artifactId, "artifactId is blank");
048                StringBuilder sb = new StringBuilder();
049                sb.append(StringUtils.trimToEmpty(groupId));
050                sb.append(DELIMITER);
051                sb.append(StringUtils.trimToEmpty(artifactId));
052                if (!StringUtils.isBlank(contextId)) {
053                        sb.append(DELIMITER);
054                        sb.append(StringUtils.trimToEmpty(contextId));
055                }
056                return sb.toString();
057        }
058
059        public static ProjectConfig getProjectConfig(String configId) {
060
061                // Split the id up into tokens
062                String[] tokens = StringUtils.split(configId, DELIMITER);
063
064                // A config id with less than 2 tokens is invalid
065                // In other words, groupId + artifactId are required
066                Assert.isTrue(tokens.length > 1, "2 tokens are required");
067
068                // Extract each portion into an explicit variable
069                String groupId = tokens[0];
070                String artifactId = tokens[1];
071                String contextId = getContextId(tokens);
072
073                // Store the variable inside an object
074                DefaultProjectConfig config = new DefaultProjectConfig();
075                config.setGroupId(groupId);
076                config.setArtifactId(artifactId);
077                config.setContextId(contextId);
078                return config;
079        }
080
081        protected static String getContextId(String[] tokens) {
082                if (tokens.length < 3) {
083                        return null;
084                }
085                StringBuilder sb = new StringBuilder();
086                for (int i = 2; i < tokens.length; i++) {
087                        if (i != 2) {
088                                sb.append(DELIMITER);
089                        }
090                        sb.append(tokens[i]);
091                }
092                return sb.toString();
093        }
094
095}