001/*
002 * (C) Copyright 2006-2018 Nuxeo (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.lang3.StringUtils;
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    @Override
040    public boolean isOverwriteByTitle() {
041        return false; // by filename
042    }
043
044    @Override
045    public String getDocType(DocumentModel container) {
046        String type = super.getDocType(container);
047        if (type == null) {
048            type = getTypeName(container);
049        }
050        return type;
051    }
052
053    public static String getTypeName(DocumentModel currentDoc) {
054        UITypesConfiguration configuration = getConfiguration(currentDoc);
055        if (configuration != null) {
056            String defaultType = configuration.getDefaultType();
057            if (StringUtils.isNotBlank(defaultType)) {
058                return defaultType;
059            }
060        }
061        return TYPE_NAME;
062    }
063
064    protected static UITypesConfiguration getConfiguration(DocumentModel currentDoc) {
065        LocalConfigurationService localConfigurationService = Framework.getService(LocalConfigurationService.class);
066        return localConfigurationService.getConfiguration(UITypesConfiguration.class, UI_TYPES_CONFIGURATION_FACET,
067                currentDoc);
068    }
069
070}