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.List;
026import java.util.Map;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.nuxeo.ecm.core.api.Blobs;
031import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
032import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolderWithProperties;
033import org.nuxeo.ecm.platform.importer.properties.IndividualMetadataCollector;
034
035/**
036 * {@link SourceNode} implementation that manages Meta-data from properties files per document
037 * <p>
038 * The properties are mapped by the collector using as key the path of the file/folder to import.
039 */
040public class FileWithIndividualMetadasSourceNode extends FileSourceNode {
041
042    private static final Log log = LogFactory.getLog(FileWithIndividualMetadasSourceNode.class);
043
044    public static final String PROPERTY_FILE_SUFIX = ".properties";
045
046    protected static IndividualMetadataCollector collector = new IndividualMetadataCollector();
047
048    public FileWithIndividualMetadasSourceNode(File file) {
049        super(file);
050    }
051
052    public FileWithIndividualMetadasSourceNode(String path) {
053        super(path);
054    }
055
056    @Override
057    public BlobHolder getBlobHolder() throws IOException {
058        BlobHolder bh = new SimpleBlobHolderWithProperties(Blobs.createBlob(file), collector.getProperties(file));
059        return bh;
060    }
061
062    public Map<String, Serializable> getMetadataForFolderishNode() {
063        return collector.getProperties(file);
064    }
065
066    @Override
067    public List<SourceNode> getChildren() {
068        List<SourceNode> children = new ArrayList<SourceNode>();
069        File[] listFiles = file.listFiles();
070        log.trace("Element " + this.getSourcePath() + " has children" + listFiles.toString());
071        for (File child : listFiles) {
072            if (isPropertyFile(child)) {
073                try {
074                    String propKey = getPropertyTargetKey(child, listFiles);
075                    if (propKey != null) {
076                        collector.addPropertyFile(child, propKey);
077                    }
078                } catch (IOException e) {
079                    log.error("Error during properties parsing", e);
080                }
081            } else {
082                children.add(new FileWithIndividualMetadasSourceNode(child));
083            }
084        }
085        return children;
086    }
087
088    protected boolean isPropertyFile(File file) {
089        return (file.getName().contains(PROPERTY_FILE_SUFIX));
090    }
091
092    protected String getPropertyTargetKey(File propFile, File[] listFiles) {
093        String fileName = propFile.getName();
094        String absFileName = fileName.substring(0, fileName.lastIndexOf(PROPERTY_FILE_SUFIX));
095        for (File file2 : listFiles) {
096            if (file2.isDirectory() && file2.getName().equals(absFileName)) {
097                return file2.getAbsolutePath();
098            }
099        }
100        for (File file2 : listFiles) {
101            if (file2.isFile() && !isPropertyFile(file2) && getFileNameNoExt(file2).equals(absFileName)) {
102                return file2.getAbsolutePath();
103            }
104        }
105        return null;
106    }
107
108    public static String getFileNameNoExt(File file) {
109        String name = file.getName();
110        int p = name.lastIndexOf('.');
111        if (p == -1) {
112            return name;
113        }
114        return name.substring(0, p);
115    }
116}