001/* 002 * (C) Copyright 2006-2007 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 */ 021 022package org.nuxeo.ecm.platform.content.template.factories; 023 024import java.io.File; 025import java.io.IOException; 026import java.util.List; 027import java.util.Map; 028 029import org.apache.commons.logging.Log; 030import org.apache.commons.logging.LogFactory; 031import org.nuxeo.common.Environment; 032import org.nuxeo.common.utils.FileUtils; 033import org.nuxeo.ecm.core.api.Blob; 034import org.nuxeo.ecm.core.api.Blobs; 035import org.nuxeo.ecm.core.api.DocumentModel; 036import org.nuxeo.ecm.platform.content.template.service.ACEDescriptor; 037import org.nuxeo.ecm.platform.content.template.service.TemplateItemDescriptor; 038import org.nuxeo.ecm.platform.filemanager.api.FileManager; 039import org.nuxeo.runtime.api.Framework; 040 041/** 042 * This factory will import a file using a path defined in the option of the factoryBinding extension point. This path 043 * can be defined using three different prefix. absolute:myAbsolute path will reference a file on the server's 044 * filesystem, nxData:myPath will reference a file inside nuxeo data folder and resource:myPath will reference a file in 045 * the bundle's resources. If the file exist, it's imported by the {@link FileManager} service. 046 * 047 * @author ldoguin 048 */ 049public class ImportBasedFactory extends BaseContentFactory { 050 051 private static final Log log = LogFactory.getLog(ImportBasedFactory.class); 052 053 public static final String IMPORT_FILE_PATH_OPTION = "path"; 054 055 public static final String IMPORT_OVERWRITE_OPTION = "overwrite"; 056 057 protected FileManager fileManager; 058 059 protected Map<String, String> options; 060 061 protected File importedFile; 062 063 protected Boolean overwrite = false; 064 065 public enum PathOptions { 066 resource { 067 @Override 068 public File getFile(String path) { 069 return FileUtils.getResourceFileFromContext(path); 070 } 071 }, 072 nxData { 073 @Override 074 public File getFile(String path) { 075 File nxDdataFolder = Environment.getDefault().getData(); 076 return new File(nxDdataFolder, path); 077 } 078 }, 079 absolute { 080 @Override 081 public File getFile(String path) { 082 return new File(path); 083 } 084 }; 085 086 protected abstract File getFile(String path); 087 088 public static File getResource(String path) { 089 String[] s = path.split(":", 2); 090 String resourceType = s[0]; 091 String resourcePath = s[1]; 092 PathOptions option = valueOf(resourceType); 093 if (option == null) { 094 log.error("Unsupported resource type: " + resourceType); 095 return null; 096 } else { 097 return option.getFile(resourcePath); 098 } 099 } 100 } 101 102 @Override 103 public boolean initFactory(Map<String, String> options, List<ACEDescriptor> rootAcl, 104 List<TemplateItemDescriptor> template) { 105 this.options = options; 106 overwrite = Boolean.valueOf(options.get(IMPORT_OVERWRITE_OPTION)); 107 String path = options.get(IMPORT_FILE_PATH_OPTION); 108 File file = PathOptions.getResource(path); 109 if (file != null) { 110 if (file.exists()) { 111 importedFile = file; 112 return true; 113 } else { 114 log.warn("Following file does not exist: " + file.getAbsolutePath()); 115 } 116 } 117 return false; 118 } 119 120 @Override 121 public void createContentStructure(DocumentModel eventDoc) { 122 initSession(eventDoc); 123 124 if (eventDoc.isVersion()) { 125 return; 126 } 127 try { 128 String parentPath = eventDoc.getPathAsString(); 129 importBlob(importedFile, parentPath); 130 } catch (IOException e) { 131 throw new RuntimeException("Cannot import file: " + importedFile, e); 132 } 133 134 } 135 136 /** 137 * Use fileManager to import the given file. 138 * 139 * @param file to import 140 * @param parentPath of the targetDocument 141 */ 142 protected void importBlob(File file, String parentPath) throws IOException { 143 if (file.isDirectory()) { 144 DocumentModel createdFolder = getFileManagerService().createFolder(session, file.getAbsolutePath(), 145 parentPath, true); 146 File[] files = file.listFiles(); 147 for (File childFile : files) { 148 importBlob(childFile, createdFolder.getPathAsString()); 149 } 150 } else { 151 Blob fb = Blobs.createBlob(file); 152 fb.setFilename(file.getName()); 153 getFileManagerService().createDocumentFromBlob(session, fb, parentPath, overwrite, fb.getFilename()); 154 } 155 } 156 157 protected FileManager getFileManagerService() { 158 if (fileManager == null) { 159 fileManager = Framework.getService(FileManager.class); 160 } 161 return fileManager; 162 } 163}