1 17 package org.alfresco.repo.importer; 18 19 import java.io.BufferedInputStream ; 20 import java.io.File ; 21 import java.io.FileFilter ; 22 import java.io.FileInputStream ; 23 import java.io.FileNotFoundException ; 24 import java.io.Serializable ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 28 import org.alfresco.model.ContentModel; 29 import org.alfresco.service.cmr.dictionary.DictionaryService; 30 import org.alfresco.service.cmr.repository.ChildAssociationRef; 31 import org.alfresco.service.cmr.repository.ContentData; 32 import org.alfresco.service.cmr.repository.ContentIOException; 33 import org.alfresco.service.cmr.repository.ContentService; 34 import org.alfresco.service.cmr.repository.ContentWriter; 35 import org.alfresco.service.cmr.repository.MimetypeService; 36 import org.alfresco.service.cmr.repository.NodeRef; 37 import org.alfresco.service.cmr.repository.NodeService; 38 import org.alfresco.service.cmr.security.AuthenticationService; 39 import org.alfresco.service.namespace.NamespaceService; 40 import org.alfresco.service.namespace.QName; 41 import org.apache.commons.logging.Log; 42 import org.apache.commons.logging.LogFactory; 43 44 49 public class FileImporterImpl implements FileImporter 50 { 51 private static Log logger = LogFactory.getLog(FileImporterImpl.class); 52 53 private AuthenticationService authenticationService; 54 private NodeService nodeService; 55 private DictionaryService dictionaryService; 56 private ContentService contentService; 57 private MimetypeService mimetypeService; 58 59 public FileImporterImpl() 60 { 61 super(); 62 } 63 64 public int loadFile(NodeRef container, File file, boolean recurse) throws FileImporterException 65 { 66 Counter counter = new Counter(); 67 create(counter, container, file, null, recurse, null); 68 return counter.getCount(); 69 } 70 71 public int loadNamedFile(NodeRef container, File file, boolean recurse, String name) throws FileImporterException 72 { 73 Counter counter = new Counter(); 74 create(counter, container, file, null, recurse, name); 75 return counter.getCount(); 76 } 77 78 public int loadFile(NodeRef container, File file, FileFilter filter, boolean recurse) throws FileImporterException 79 { 80 Counter counter = new Counter(); 81 create(counter, container, file, filter, recurse, null); 82 return counter.getCount(); 83 } 84 85 public int loadFile(NodeRef container, File file) throws FileImporterException 86 { 87 Counter counter = new Counter(); 88 create(counter, container, file, null, false, null); 89 return counter.getCount(); 90 } 91 92 93 private static class Counter 94 { 95 private int count = 0; 96 public void increment() 97 { 98 count++; 99 } 100 public int getCount() 101 { 102 return count; 103 } 104 } 105 106 private NodeRef create(Counter counter, NodeRef container, File file, FileFilter filter, boolean recurse, String containerName) 107 { 108 if(containerName != null) 109 { 110 NodeRef newContainer = createDirectory(container, containerName, containerName); 111 return create(counter, newContainer, file, filter, recurse, null); 112 113 } 114 if (file.isDirectory()) 115 { 116 NodeRef directoryNodeRef = createDirectory(container, file); 117 counter.increment(); 118 119 if(recurse) 120 { 121 File [] files = ((filter == null) ? file.listFiles() : file.listFiles(filter)); 122 for(int i = 0; i < files.length; i++) 123 { 124 create(counter, directoryNodeRef, files[i], filter, recurse, null); 125 } 126 } 127 128 return directoryNodeRef; 129 } 130 else 131 { 132 counter.increment(); 133 return createFile(container, file); 134 } 135 } 136 137 144 private QName getAssocTypeQName(NodeRef parentNodeRef) 145 { 146 QName parentNodeTypeQName = nodeService.getType(parentNodeRef); 148 QName assocTypeQName = null; 149 if (dictionaryService.isSubClass(parentNodeTypeQName, ContentModel.TYPE_CONTAINER)) 150 { 151 assocTypeQName = ContentModel.ASSOC_CHILDREN; 153 } 154 else if (dictionaryService.isSubClass(parentNodeTypeQName, ContentModel.TYPE_FOLDER)) 155 { 156 assocTypeQName = ContentModel.ASSOC_CONTAINS; 158 } 159 return assocTypeQName; 160 } 161 162 private NodeRef createFile(NodeRef parentNodeRef, File file) 163 { 164 QName assocTypeQName = getAssocTypeQName(parentNodeRef); 166 if (assocTypeQName == null) 167 { 168 throw new IllegalArgumentException ( 169 "Unable to create file. " + 170 "Parent type is inappropriate: " + nodeService.getType(parentNodeRef)); 171 } 172 173 Map <QName, Serializable > contentProps = new HashMap <QName, Serializable >(3, 1.0f); 175 contentProps.put(ContentModel.PROP_NAME, file.getName()); 176 contentProps.put( 177 ContentModel.PROP_CONTENT, 178 new ContentData(null, mimetypeService.guessMimetype(file.getName()), 0L, "UTF-8")); 179 String currentUser = authenticationService.getCurrentUserName(); 180 contentProps.put(ContentModel.PROP_CREATOR, currentUser == null ? "unknown" : currentUser); 181 182 String assocName = QName.createValidLocalName(file.getName()); 184 ChildAssociationRef assocRef = this.nodeService.createNode( 185 parentNodeRef, 186 assocTypeQName, 187 QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, assocName), 188 ContentModel.TYPE_CONTENT, contentProps); 189 190 NodeRef fileNodeRef = assocRef.getChildRef(); 191 192 193 if (logger.isDebugEnabled()) 194 logger.debug("Created file node for file: " + file.getName()); 195 196 Map <QName, Serializable > titledProps = new HashMap <QName, Serializable >(5); 198 titledProps.put(ContentModel.PROP_TITLE, file.getName()); 199 200 titledProps.put(ContentModel.PROP_DESCRIPTION, file.getPath()); 201 202 this.nodeService.addAspect(fileNodeRef, ContentModel.ASPECT_TITLED, titledProps); 203 204 if (logger.isDebugEnabled()) 205 logger.debug("Added titled aspect with properties: " + titledProps); 206 207 ContentWriter writer = contentService.getWriter(fileNodeRef, ContentModel.PROP_CONTENT, true); 209 try 210 { 211 writer.putContent(new BufferedInputStream (new FileInputStream (file))); 212 } 213 catch (ContentIOException e) 214 { 215 throw new FileImporterException("Failed to load content from "+file.getPath(), e); 216 } 217 catch (FileNotFoundException e) 218 { 219 throw new FileImporterException("Failed to load content (file not found) "+file.getPath(), e); 220 } 221 222 return fileNodeRef; 223 } 224 225 private NodeRef createDirectory(NodeRef parentNodeRef, File file) 226 { 227 return createDirectory(parentNodeRef, file.getName(), file.getPath()); 228 229 } 230 231 private NodeRef createDirectory(NodeRef parentNodeRef, String name, String path) 232 { 233 QName assocTypeQName = getAssocTypeQName(parentNodeRef); 235 if (assocTypeQName == null) 236 { 237 throw new IllegalArgumentException ( 238 "Unable to create directory. " + 239 "Parent type is inappropriate: " + nodeService.getType(parentNodeRef)); 240 } 241 242 String qname = QName.createValidLocalName(name); 243 ChildAssociationRef assocRef = this.nodeService.createNode( 244 parentNodeRef, 245 assocTypeQName, 246 QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, qname), 247 ContentModel.TYPE_FOLDER); 248 249 NodeRef nodeRef = assocRef.getChildRef(); 250 251 this.nodeService.setProperty(nodeRef, ContentModel.PROP_NAME, name); 253 254 if (logger.isDebugEnabled()) 255 logger.debug("Created folder node with name: " + name); 256 257 Map <QName, Serializable > uiFacetsProps = new HashMap <QName, Serializable >(5); 259 uiFacetsProps.put(ContentModel.PROP_ICON, "space-icon-default"); 260 uiFacetsProps.put(ContentModel.PROP_TITLE, name); 261 uiFacetsProps.put(ContentModel.PROP_DESCRIPTION, path); 262 this.nodeService.addAspect(nodeRef, ContentModel.ASPECT_UIFACETS, uiFacetsProps); 263 264 if (logger.isDebugEnabled()) 265 logger.debug("Added uifacets aspect with properties: " + uiFacetsProps); 266 267 return nodeRef; 268 } 269 270 protected void setAuthenticationService(AuthenticationService authenticationService) 271 { 272 this.authenticationService = authenticationService; 273 } 274 275 protected void setContentService(ContentService contentService) 276 { 277 this.contentService = contentService; 278 } 279 280 protected void setMimetypeService(MimetypeService mimetypeService) 281 { 282 this.mimetypeService = mimetypeService; 283 } 284 285 protected void setNodeService(NodeService nodeService) 286 { 287 this.nodeService = nodeService; 288 } 289 290 public void setDictionaryService(DictionaryService dictionaryService) 291 { 292 this.dictionaryService = dictionaryService; 293 } 294 } 295 | Popular Tags |