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.properties;
017
018import java.util.ArrayList;
019import java.util.Arrays;
020import java.util.List;
021
022import org.kuali.common.util.project.ProjectService;
023import org.kuali.common.util.project.ProjectUtils;
024import org.kuali.common.util.project.model.Project;
025import org.kuali.common.util.project.model.ProjectIdentifier;
026import org.kuali.common.util.project.model.ProjectResource;
027
028import com.google.common.base.Preconditions;
029import com.google.common.collect.ImmutableList;
030
031public class DefaultPropertiesLocationService implements PropertiesLocationService {
032
033        public DefaultPropertiesLocationService(ProjectService projectService) {
034                this(projectService, DEFAULT_CACHE_PROPERTIES_VALUE);
035        }
036
037        public DefaultPropertiesLocationService(ProjectService projectService, boolean cache) {
038                Preconditions.checkNotNull(projectService, "'projectService' cannot be null");
039                this.projectService = projectService;
040                this.cache = cache;
041        }
042
043        private static final boolean DEFAULT_CACHE_PROPERTIES_VALUE = true;
044
045        private final ProjectService projectService;
046        private final boolean cache;
047
048        @Override
049        public List<Location> getLocations(ProjectIdentifier identifier, List<String> filenames) {
050                List<Location> locations = new ArrayList<Location>();
051                for (String filename : filenames) {
052                        locations.add(getLocation(identifier, filename));
053                }
054                return locations;
055        }
056
057        @Override
058        public List<Location> getLocations(ProjectIdentifier identifier, String... filenames) {
059                return getLocations(identifier, Arrays.asList(filenames));
060        }
061
062        @Override
063        public Location getLocation(ProjectIdentifier identifier, String filename) {
064                Project project = projectService.getProject(identifier);
065                String value = ProjectUtils.getClasspathPrefix(identifier) + "/" + filename;
066                String encoding = ProjectUtils.getEncoding(project);
067                return new Location(value, encoding, cache);
068        }
069
070        /**
071         * @deprecated
072         */
073        @Deprecated
074        @Override
075        public List<Location> getLocations(org.kuali.common.util.project.model.FeatureIdentifier identifier, String... filenames) {
076                return getLocations(identifier, ImmutableList.copyOf(filenames));
077        }
078
079        /**
080         * @deprecated
081         */
082        @Deprecated
083        @Override
084        public List<Location> getLocations(org.kuali.common.util.project.model.FeatureIdentifier identifier, List<String> filenames) {
085                List<Location> locations = new ArrayList<Location>();
086                for (String filename : filenames) {
087                        locations.add(getLocation(identifier, filename));
088                }
089                return locations;
090        }
091
092        /**
093         * @deprecated
094         */
095        @Deprecated
096        @Override
097        public Location getLocation(org.kuali.common.util.project.model.FeatureIdentifier identifier, String filename) {
098                Project project = projectService.getProject(identifier.getProject());
099                String value = ProjectUtils.getClasspathPrefix(identifier) + "/" + filename;
100                String encoding = ProjectUtils.getEncoding(project);
101                return new Location(value, encoding, cache);
102        }
103
104        @Override
105        public Location getLocation(ProjectResource resource) {
106                return getLocation(resource.getProject(), resource.getPath());
107        }
108}