001/*
002 * (C) Copyright 2006-2013 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Anahide Tchertchian
016 *     Florent Guillaume
017 */
018package org.nuxeo.ecm.platform.filemanager.service.extension;
019
020import static org.nuxeo.ecm.platform.types.localconfiguration.UITypesConfigurationConstants.UI_TYPES_CONFIGURATION_FACET;
021
022import org.apache.commons.lang.StringUtils;
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.core.api.localconfiguration.LocalConfigurationService;
027import org.nuxeo.ecm.platform.types.localconfiguration.UITypesConfiguration;
028import org.nuxeo.runtime.api.Framework;
029
030/**
031 * Default file importer, creating a regular file.
032 */
033public class DefaultFileImporter extends AbstractFileImporter {
034
035    public static final String TYPE_NAME = "File";
036
037    private static final long serialVersionUID = 1L;
038
039    private static final Log log = LogFactory.getLog(DefaultFileImporter.class);
040
041    @Override
042    public boolean isOverwriteByTitle() {
043        return false; // by filename
044    }
045
046    @Override
047    public String getDocType(DocumentModel container) {
048        String type = super.getDocType(container);
049        if (type == null) {
050            type = getTypeName(container);
051        }
052        return type;
053    }
054
055    public static String getTypeName(DocumentModel currentDoc) {
056        UITypesConfiguration configuration = getConfiguration(currentDoc);
057        if (configuration != null) {
058            String defaultType = configuration.getDefaultType();
059            if (StringUtils.isNotBlank(defaultType)) {
060                return defaultType;
061            }
062        }
063        return TYPE_NAME;
064    }
065
066    protected static UITypesConfiguration getConfiguration(DocumentModel currentDoc) {
067        LocalConfigurationService localConfigurationService = Framework.getService(LocalConfigurationService.class);
068        return localConfigurationService.getConfiguration(UITypesConfiguration.class, UI_TYPES_CONFIGURATION_FACET,
069                currentDoc);
070    }
071
072}