001/*
002 * (C) Copyright 2006-2013 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 *     Anahide Tchertchian
018 *     Florent Guillaume
019 */
020package org.nuxeo.ecm.platform.filemanager.service.extension;
021
022import static org.nuxeo.ecm.platform.types.localconfiguration.UITypesConfigurationConstants.UI_TYPES_CONFIGURATION_FACET;
023
024import org.apache.commons.lang.StringUtils;
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.localconfiguration.LocalConfigurationService;
029import org.nuxeo.ecm.platform.types.localconfiguration.UITypesConfiguration;
030import org.nuxeo.runtime.api.Framework;
031
032/**
033 * Default file importer, creating a regular file.
034 */
035public class DefaultFileImporter extends AbstractFileImporter {
036
037    public static final String TYPE_NAME = "File";
038
039    private static final long serialVersionUID = 1L;
040
041    private static final Log log = LogFactory.getLog(DefaultFileImporter.class);
042
043    @Override
044    public boolean isOverwriteByTitle() {
045        return false; // by filename
046    }
047
048    @Override
049    public String getDocType(DocumentModel container) {
050        String type = super.getDocType(container);
051        if (type == null) {
052            type = getTypeName(container);
053        }
054        return type;
055    }
056
057    public static String getTypeName(DocumentModel currentDoc) {
058        UITypesConfiguration configuration = getConfiguration(currentDoc);
059        if (configuration != null) {
060            String defaultType = configuration.getDefaultType();
061            if (StringUtils.isNotBlank(defaultType)) {
062                return defaultType;
063            }
064        }
065        return TYPE_NAME;
066    }
067
068    protected static UITypesConfiguration getConfiguration(DocumentModel currentDoc) {
069        LocalConfigurationService localConfigurationService = Framework.getService(LocalConfigurationService.class);
070        return localConfigurationService.getConfiguration(UITypesConfiguration.class, UI_TYPES_CONFIGURATION_FACET,
071                currentDoc);
072    }
073
074}