001/*
002 * (C) Copyright 2006-2012 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 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021package org.nuxeo.ecm.platform.scanimporter.service;
022
023import java.util.ArrayList;
024import java.util.List;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.nuxeo.common.xmap.annotation.XNode;
029import org.nuxeo.common.xmap.annotation.XNodeList;
030import org.nuxeo.common.xmap.annotation.XObject;
031import org.nuxeo.ecm.platform.scanimporter.processor.DocumentTypeMapper;
032
033/**
034 * Top level descriptor for mapping
035 *
036 * @author Thierry Delprat
037 */
038@XObject("mapping")
039public class ScanFileMappingDescriptor {
040
041    public static final String DEFAULT_CONTAINER_TYPE = "Folder";
042
043    public static final String DEFAULT_LEAF_TYPE = "File";
044
045    private static final Log log = LogFactory.getLog(ScanFileMappingDescriptor.class);
046
047    @XNode("targetContainerType")
048    protected String targetContainerType = DEFAULT_CONTAINER_TYPE;
049
050    @XNode("targetLeafType")
051    protected String targetLeafType = DEFAULT_LEAF_TYPE;
052
053    @XNode("targetLeafTypeMapper")
054    protected Class<DocumentTypeMapper> mapperClass;
055
056    protected DocumentTypeMapper leafTypeMapper = null;
057
058    @XNodeList(value = "fieldMappings/fieldMapping", type = ArrayList.class, componentType = ScanFileFieldMapping.class)
059    private List<ScanFileFieldMapping> fieldMappings;
060
061    @XNodeList(value = "blobMappings/blobMapping", type = ArrayList.class, componentType = ScanFileBlobMapping.class)
062    private List<ScanFileBlobMapping> blobMappings;
063
064    public List<ScanFileFieldMapping> getFieldMappings() {
065        return fieldMappings;
066    }
067
068    public List<ScanFileBlobMapping> getBlobMappings() {
069        return blobMappings;
070    }
071
072    public String getTargetContainerType() {
073        return targetContainerType;
074    }
075
076    public String getTargetLeafType() {
077        return targetLeafType;
078    }
079
080    public DocumentTypeMapper getTargetLeafTypeMapper() {
081        if (mapperClass == null) {
082            return null;
083        }
084        if (leafTypeMapper == null) {
085            try {
086                leafTypeMapper = mapperClass.getDeclaredConstructor().newInstance();
087            } catch (ReflectiveOperationException e) {
088                log.error("Unable to instanciate mapper class", e);
089            }
090        }
091        return leafTypeMapper;
092    }
093
094}