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.property.processor;
017
018import static org.kuali.common.util.log.Loggers.newLogger;
019
020import java.io.File;
021import java.util.Properties;
022
023import org.apache.commons.lang3.StringUtils;
024import org.kuali.common.util.OrgUtils;
025import org.kuali.common.util.Str;
026import org.kuali.common.util.maven.MavenConstants;
027import org.kuali.common.util.project.KualiProjectConstants;
028import org.kuali.common.util.project.ProjectUtils;
029import org.kuali.common.util.project.model.Project;
030import org.slf4j.Logger;
031import org.springframework.util.Assert;
032
033/**
034 * This processor is called *very* early in the Maven build lifecyle in order to augment the default set of Maven properties.
035 * 
036 */
037public class ProjectProcessor implements PropertyProcessor {
038
039        private static final Logger logger = newLogger();
040
041        /**
042         * @deprecated
043         */
044        @Deprecated
045        private static final String KS_GROUP_ID = KualiProjectConstants.STUDENT_GROUP_ID;
046        private static final String FS = File.separator;
047        private static final String DOT = ".";
048        private static final String PROJECT_GROUP_ID_PATH = "project.groupId.path";
049
050        @Override
051        public void process(Properties properties) {
052
053                // Make sure we are configured correctly
054                Assert.notNull(properties, "properties are null");
055
056                // Make sure groupId, artifactId, orgId, and orgCode are present
057                validate(properties);
058
059                // Fix the funk in KS groupId's (if its a KS project)
060                fixKSGroupIds(properties);
061
062                // Now that the groupId is fixed, it is safe to use the properties to get a project object
063                Project p = ProjectUtils.getProject(properties);
064
065                // Extract org info
066                String orgId = properties.getProperty(MavenConstants.ORG_ID_KEY);
067                String orgCode = properties.getProperty(MavenConstants.ORG_ID_CODE_KEY);
068
069                // Figure out the group code (eg "rice", "student", "ole", etc)
070                String groupCode = OrgUtils.getGroupCode(orgId, p.getGroupId());
071
072                // Setup some org and group paths based on user.home
073                String userHome = System.getProperty("user.home");
074                String orgHome = userHome + FS + DOT + orgCode;
075                String groupHome = orgHome + FS + groupCode;
076
077                // Store the org and group paths
078                properties.setProperty(PROJECT_GROUP_ID_PATH, Str.getPath(p.getGroupId()));
079                properties.setProperty("project.orgId.home", orgHome);
080                properties.setProperty("project.groupId.home", groupHome);
081                properties.setProperty("project.home", groupHome);
082
083                // Store the groupCode
084                properties.setProperty("project.groupId.code", groupCode);
085
086                // Add the current milliseconds value as a project property
087                properties.setProperty("project.build.timestamp.millis", Long.toString(System.currentTimeMillis()));
088
089        }
090
091        // Make sure the properties hold basic project identifier info
092        protected void validate(Properties properties) {
093                Assert.notNull(properties.getProperty(MavenConstants.GROUP_ID_KEY), MavenConstants.GROUP_ID_KEY + " is null");
094                Assert.notNull(properties.getProperty(MavenConstants.ARTIFACT_ID_KEY), MavenConstants.ARTIFACT_ID_KEY + " is null");
095                Assert.notNull(properties.getProperty(MavenConstants.ORG_ID_KEY), MavenConstants.ORG_ID_KEY + " is null");
096                Assert.notNull(properties.getProperty(MavenConstants.ORG_ID_CODE_KEY), MavenConstants.ORG_ID_CODE_KEY + " is null");
097        }
098
099        /**
100         * @deprecated
101         */
102        @Deprecated
103        protected void fixKSGroupIds(Properties properties) {
104                // Extract the groupId
105                String groupId = properties.getProperty(MavenConstants.GROUP_ID_KEY);
106
107                // Only muck with KS projects
108                if (!StringUtils.startsWith(groupId, KS_GROUP_ID)) {
109                        return;
110                }
111
112                // All KS projects should have a groupId of "org.kuali.student" no matter what
113                properties.setProperty(MavenConstants.GROUP_ID_KEY, KualiProjectConstants.STUDENT_GROUP_ID);
114
115                // If this KS project is using some other group id for some reason
116                // Store it in project.properties just for posterity
117                if (!StringUtils.equals(groupId, KualiProjectConstants.STUDENT_GROUP_ID)) {
118                        logger.debug("original={}", groupId);
119                        properties.setProperty(MavenConstants.GROUP_ID_ORIGINAL_KEY, groupId);
120                }
121        }
122
123}