001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.fabric;
016    
017    import com.liferay.portal.fabric.repository.RepositoryHelperUtil;
018    import com.liferay.portal.kernel.process.ProcessCallable;
019    import com.liferay.portal.kernel.util.ObjectGraphUtil.AnnotatedFieldMappingVisitor;
020    
021    import java.io.File;
022    
023    import java.lang.annotation.Annotation;
024    import java.lang.reflect.Field;
025    
026    import java.nio.file.Path;
027    
028    import java.util.Collections;
029    import java.util.HashMap;
030    import java.util.Map;
031    
032    /**
033     * @author Shuyang Zhou
034     */
035    public class FabricPathMappingVisitor extends AnnotatedFieldMappingVisitor {
036    
037            public FabricPathMappingVisitor(
038                    Class<? extends Annotation> annotationClass,
039                    Path remoteRepositoryPath) {
040    
041                    this(annotationClass, remoteRepositoryPath, false);
042            }
043    
044            public FabricPathMappingVisitor(
045                    Class<? extends Annotation> annotationClass, Path remoteRepositoryPath,
046                    boolean reverseMapping) {
047    
048                    super(
049                            Collections.<Class<?>>singleton(ProcessCallable.class),
050                            Collections.<Class<? extends Annotation>>singleton(annotationClass),
051                            Collections.<Class<?>>singleton(File.class));
052    
053                    _remoteRepositoryPath = remoteRepositoryPath;
054                    _reverseMapping = reverseMapping;
055            }
056    
057            public Map<Path, Path> getPathMap() {
058                    return _pathMap;
059            }
060    
061            @Override
062            protected Object doMap(Field field, Object value) {
063                    File file = (File)value;
064    
065                    Path path = file.toPath();
066    
067                    Path mappedPath = RepositoryHelperUtil.getRepositoryFilePath(
068                            _remoteRepositoryPath, path);
069    
070                    if (_reverseMapping) {
071                            _pathMap.put(mappedPath, path);
072                    }
073                    else {
074                            _pathMap.put(path, mappedPath);
075                    }
076    
077                    return mappedPath.toFile();
078            }
079    
080            private final Map<Path, Path> _pathMap = new HashMap<>();
081            private final Path _remoteRepositoryPath;
082            private final boolean _reverseMapping;
083    
084    }