001/*
002 * (C) Copyright 2011 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 *    Mariana Cedica
018 */
019package org.nuxeo.ecm.platform.importer.source;
020
021import java.io.File;
022import java.io.IOException;
023import java.io.Serializable;
024import java.util.ArrayList;
025import java.util.HashMap;
026import java.util.List;
027import java.util.Map;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.nuxeo.ecm.core.api.Blobs;
032import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
033import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolderWithProperties;
034import org.nuxeo.ecm.platform.importer.properties.IndividualMetadataCollector;
035
036/**
037 * {@link SourceNode} implementation that manages Meta-data from properties files per document
038 * <p>
039 * The properties are mapped by the collector using as key the path of the file/folder to import.
040 */
041public class FileWithIndividualMetadasSourceNode extends FileSourceNode {
042
043    private static final Log log = LogFactory.getLog(FileWithIndividualMetadasSourceNode.class);
044
045    public static final String PROPERTY_FILE_SUFFIX = ".properties";
046
047    /** @deprecated since 8.3 misspelled */
048    @Deprecated
049    public static final String PROPERTY_FILE_SUFIX = PROPERTY_FILE_SUFFIX;
050
051    protected static IndividualMetadataCollector collector = new IndividualMetadataCollector();
052
053    public FileWithIndividualMetadasSourceNode(File file) {
054        super(file);
055    }
056
057    public FileWithIndividualMetadasSourceNode(String path) {
058        super(path);
059    }
060
061    /* convenience factory to easy subclassing */
062    protected FileWithIndividualMetadasSourceNode newInstance(File file) {
063        return new FileWithIndividualMetadasSourceNode(file);
064    }
065
066    @Override
067    public BlobHolder getBlobHolder() throws IOException {
068        BlobHolder bh = new SimpleBlobHolderWithProperties(Blobs.createBlob(file), collector.getProperties(file));
069        return bh;
070    }
071
072    public Map<String, Serializable> getMetadataForFolderishNode() {
073        return collector.getProperties(file);
074    }
075
076    @Override
077    public List<SourceNode> getChildren() {
078        List<SourceNode> children = new ArrayList<SourceNode>();
079        File[] listFiles = file.listFiles();
080        log.trace("Element " + this.getSourcePath() + " has " + listFiles.length + " children");
081        // compute map from base name without extension to absolute path
082        Map<String, String> paths = new HashMap<>();
083        for (File child : listFiles) {
084            String name = child.getName();
085            if (child.isDirectory()) {
086                paths.put(name, child.getAbsolutePath());
087            } else if (child.isFile() && !name.endsWith(PROPERTY_FILE_SUFFIX)) {
088                String base = getFileNameNoExt(child);
089                paths.put(base, child.getAbsolutePath());
090            }
091        }
092        for (File child : listFiles) {
093            String name = child.getName();
094            if (name.endsWith(PROPERTY_FILE_SUFFIX)) {
095                String base = name.substring(0, name.length() - PROPERTY_FILE_SUFFIX.length());
096                String path = paths.get(base);
097                if (path != null) {
098                    try {
099                        collector.addPropertyFile(child, path);
100                    } catch (IOException e) {
101                        log.error("Error during properties parsing for: " + child, e);
102                    }
103                }
104            } else {
105                children.add(newInstance(child));
106            }
107        }
108        return children;
109    }
110
111    /** @deprecated since 8.3 unused. */
112    @Deprecated
113    protected boolean isPropertyFile(File file) {
114        return file.getName().endsWith(PROPERTY_FILE_SUFFIX);
115    }
116
117    public static String getFileNameNoExt(File file) {
118        String name = file.getName();
119        int p = name.lastIndexOf('.');
120        if (p == -1) {
121            return name;
122        }
123        return name.substring(0, p);
124    }
125}