001    /**
002     * Copyright 2010-2012 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     */
016    package org.kuali.common.util.property.processor;
017    
018    import java.util.Collections;
019    import java.util.List;
020    import java.util.Properties;
021    
022    import org.kuali.common.util.Mode;
023    import org.kuali.common.util.PropertyUtils;
024    import org.kuali.common.util.Version;
025    import org.kuali.common.util.VersionUtils;
026    import org.kuali.common.util.property.Constants;
027    
028    public class VersionProcessor implements PropertyProcessor {
029    
030            String majorSuffix = Constants.DEFAULT_MAJOR_VERSION_SUFFIX;
031            String minorSuffix = Constants.DEFAULT_MINOR_VERSION_SUFFIX;
032            String incrementalSuffix = Constants.DEFAULT_INCREMENTAL_VERSION_SUFFIX;
033            String qualifierSuffix = Constants.DEFAULT_QUALIFIER_VERSION_SUFFIX;
034            String trimmedSuffix = Constants.DEFAULT_TRIMMED_VERSION_SUFFIX;
035            String snapshotSuffix = Constants.DEFAULT_SNAPSHOT_VERSION_SUFFIX;
036            boolean alwaysAddOrOverride;
037    
038            List<String> includes;
039            List<String> excludes;
040            Mode propertyOverwriteMode = Constants.DEFAULT_PROPERTY_OVERWRITE_MODE;
041    
042            public VersionProcessor() {
043                    this(Collections.<String> emptyList());
044            }
045    
046            public VersionProcessor(String include) {
047                    this(Collections.singletonList(include));
048            }
049    
050            public VersionProcessor(List<String> includes) {
051                    this(includes, false);
052            }
053    
054            public VersionProcessor(List<String> includes, boolean alwaysAddOrOverride) {
055                    super();
056                    this.includes = includes;
057                    this.alwaysAddOrOverride = alwaysAddOrOverride;
058            }
059    
060            @Override
061            public void process(Properties properties) {
062                    List<String> keys = PropertyUtils.getSortedKeys(properties, includes, excludes);
063                    Properties versionProperties = new Properties();
064                    for (String key : keys) {
065                            String version = properties.getProperty(key);
066                            Version v = VersionUtils.getVersion(version);
067                            versionProperties.putAll(getVersionProperties(key, v));
068                    }
069                    List<String> versionKeys = PropertyUtils.getSortedKeys(versionProperties);
070                    for (String versionKey : versionKeys) {
071                            String versionValue = versionProperties.getProperty(versionKey);
072                            if (alwaysAddOrOverride) {
073                                    properties.setProperty(versionKey, versionValue);
074                            } else {
075                                    PropertyUtils.addOrOverrideProperty(properties, versionKey, versionValue, propertyOverwriteMode);
076                            }
077                    }
078            }
079    
080            public Properties getVersionProperties(String key, Version v) {
081                    Properties properties = new Properties();
082                    if (v.getMajor() != null) {
083                            String newKey = key + "." + majorSuffix;
084                            properties.setProperty(newKey, v.getMajor());
085                    }
086                    if (v.getMinor() != null) {
087                            String newKey = key + "." + minorSuffix;
088                            properties.setProperty(newKey, v.getMinor());
089                    }
090                    if (v.getIncremental() != null) {
091                            String newKey = key + "." + incrementalSuffix;
092                            properties.setProperty(newKey, v.getIncremental());
093                    }
094                    if (v.getQualifier() != null) {
095                            String newKey = key + "." + qualifierSuffix;
096                            properties.setProperty(newKey, v.getQualifier());
097                    }
098                    if (v.getTrimmed() != null) {
099                            String newKey = key + "." + trimmedSuffix;
100                            properties.setProperty(newKey, v.getTrimmed());
101                    }
102                    String newKey = key + "." + snapshotSuffix;
103                    properties.setProperty(newKey, Boolean.toString(v.isSnapshot()));
104                    return properties;
105            }
106    
107            public String getMajorSuffix() {
108                    return majorSuffix;
109            }
110    
111            public void setMajorSuffix(String majorSuffix) {
112                    this.majorSuffix = majorSuffix;
113            }
114    
115            public String getMinorSuffix() {
116                    return minorSuffix;
117            }
118    
119            public void setMinorSuffix(String minorSuffix) {
120                    this.minorSuffix = minorSuffix;
121            }
122    
123            public String getIncrementalSuffix() {
124                    return incrementalSuffix;
125            }
126    
127            public void setIncrementalSuffix(String incrementalSuffix) {
128                    this.incrementalSuffix = incrementalSuffix;
129            }
130    
131            public String getQualifierSuffix() {
132                    return qualifierSuffix;
133            }
134    
135            public void setQualifierSuffix(String qualifierSuffix) {
136                    this.qualifierSuffix = qualifierSuffix;
137            }
138    
139            public String getTrimmedSuffix() {
140                    return trimmedSuffix;
141            }
142    
143            public void setTrimmedSuffix(String trimmedSuffix) {
144                    this.trimmedSuffix = trimmedSuffix;
145            }
146    
147            public String getSnapshotSuffix() {
148                    return snapshotSuffix;
149            }
150    
151            public void setSnapshotSuffix(String snapshotSuffix) {
152                    this.snapshotSuffix = snapshotSuffix;
153            }
154    
155            public List<String> getIncludes() {
156                    return includes;
157            }
158    
159            public void setIncludes(List<String> includes) {
160                    this.includes = includes;
161            }
162    
163            public List<String> getExcludes() {
164                    return excludes;
165            }
166    
167            public void setExcludes(List<String> excludes) {
168                    this.excludes = excludes;
169            }
170    
171            public Mode getPropertyOverwriteMode() {
172                    return propertyOverwriteMode;
173            }
174    
175            public void setPropertyOverwriteMode(Mode propertyOverwriteMode) {
176                    this.propertyOverwriteMode = propertyOverwriteMode;
177            }
178    
179            public boolean isAlwaysAddOrOverride() {
180                    return alwaysAddOrOverride;
181            }
182    
183            public void setAlwaysAddOrOverride(boolean alwaysAddOrOverride) {
184                    this.alwaysAddOrOverride = alwaysAddOrOverride;
185            }
186    
187    }