001/*
002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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 * Contributors:
017 *     Antoine Taillefer
018 */
019package org.nuxeo.drive.service.impl;
020
021import java.util.HashSet;
022import java.util.LinkedHashMap;
023import java.util.Map;
024import java.util.Set;
025
026import org.apache.commons.lang.StringUtils;
027import org.nuxeo.ecm.core.api.IdRef;
028
029/**
030 * Helper to handle synchronization root definitions.
031 *
032 * @author Antoine Taillefer
033 */
034public final class RootDefinitionsHelper {
035
036    private RootDefinitionsHelper() {
037        // Utility class
038    }
039
040    /**
041     * Parses the given synchronization root definitions string.
042     */
043    public static Map<String, Set<IdRef>> parseRootDefinitions(String rootDefinitions) {
044        Map<String, Set<IdRef>> lastActiveRootRefs = new LinkedHashMap<String, Set<IdRef>>();
045        if (rootDefinitions != null) {
046            String[] rootDefinitionComponents = StringUtils.split(rootDefinitions, ",");
047            for (String rootDefinition : rootDefinitionComponents) {
048                String[] rootComponents = StringUtils.split(rootDefinition, ":");
049                String repoName = rootComponents[0].trim();
050                Set<IdRef> refs = lastActiveRootRefs.get(repoName);
051                if (refs == null) {
052                    refs = new HashSet<IdRef>();
053                    lastActiveRootRefs.put(repoName, refs);
054                }
055                refs.add(new IdRef(rootComponents[1].trim()));
056            }
057        }
058        return lastActiveRootRefs;
059    }
060
061}