KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > importer > FileImporterImpl


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.importer;
18
19 import java.io.BufferedInputStream JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.FileFilter JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24 import java.io.Serializable JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
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 /**
45  * Simple import of content into the repository
46  *
47  * @author andyh
48  */

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 JavaDoc 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 JavaDoc file, boolean recurse, String JavaDoc 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 JavaDoc file, FileFilter JavaDoc 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 JavaDoc file) throws FileImporterException
86     {
87         Counter counter = new Counter();
88         create(counter, container, file, null, false, null);
89         return counter.getCount();
90     }
91     
92     /** Helper class for mutable int */
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 JavaDoc file, FileFilter JavaDoc filter, boolean recurse, String JavaDoc 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 JavaDoc[] 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     /**
138      * Get the type of child association that should be created.
139      *
140      * @param parentNodeRef the parent
141      * @return Returns the appropriate child association type qualified name for the type of the
142      * parent. Null will be returned if it can't be determined.
143      */

144     private QName getAssocTypeQName(NodeRef parentNodeRef)
145     {
146         // check the parent node's type to determine which association to use
147
QName parentNodeTypeQName = nodeService.getType(parentNodeRef);
148         QName assocTypeQName = null;
149         if (dictionaryService.isSubClass(parentNodeTypeQName, ContentModel.TYPE_CONTAINER))
150         {
151             // it may be a root node or something similar
152
assocTypeQName = ContentModel.ASSOC_CHILDREN;
153         }
154         else if (dictionaryService.isSubClass(parentNodeTypeQName, ContentModel.TYPE_FOLDER))
155         {
156             // more like a directory
157
assocTypeQName = ContentModel.ASSOC_CONTAINS;
158         }
159         return assocTypeQName;
160     }
161
162     private NodeRef createFile(NodeRef parentNodeRef, File JavaDoc file)
163     {
164         // check the parent node's type to determine which association to use
165
QName assocTypeQName = getAssocTypeQName(parentNodeRef);
166         if (assocTypeQName == null)
167         {
168             throw new IllegalArgumentException JavaDoc(
169                     "Unable to create file. " +
170                     "Parent type is inappropriate: " + nodeService.getType(parentNodeRef));
171         }
172
173         // create properties for content type
174
Map JavaDoc<QName, Serializable JavaDoc> contentProps = new HashMap JavaDoc<QName, Serializable JavaDoc>(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 JavaDoc currentUser = authenticationService.getCurrentUserName();
180         contentProps.put(ContentModel.PROP_CREATOR, currentUser == null ? "unknown" : currentUser);
181
182         // create the node to represent the node
183
String JavaDoc 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         // apply the titled aspect - title and description
197
Map JavaDoc<QName, Serializable JavaDoc> titledProps = new HashMap JavaDoc<QName, Serializable JavaDoc>(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         // get a writer for the content and put the file
208
ContentWriter writer = contentService.getWriter(fileNodeRef, ContentModel.PROP_CONTENT, true);
209         try
210         {
211             writer.putContent(new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file)));
212         }
213         catch (ContentIOException e)
214         {
215             throw new FileImporterException("Failed to load content from "+file.getPath(), e);
216         }
217         catch (FileNotFoundException JavaDoc 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 JavaDoc file)
226     {
227         return createDirectory(parentNodeRef, file.getName(), file.getPath());
228         
229     }
230     
231     private NodeRef createDirectory(NodeRef parentNodeRef, String JavaDoc name, String JavaDoc path)
232     {
233         // check the parent node's type to determine which association to use
234
QName assocTypeQName = getAssocTypeQName(parentNodeRef);
235         if (assocTypeQName == null)
236         {
237             throw new IllegalArgumentException JavaDoc(
238                     "Unable to create directory. " +
239                     "Parent type is inappropriate: " + nodeService.getType(parentNodeRef));
240         }
241         
242         String JavaDoc 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         // set the name property on the node
252
this.nodeService.setProperty(nodeRef, ContentModel.PROP_NAME, name);
253         
254         if (logger.isDebugEnabled())
255            logger.debug("Created folder node with name: " + name);
256
257         // apply the uifacets aspect - icon, title and description props
258
Map JavaDoc<QName, Serializable JavaDoc> uiFacetsProps = new HashMap JavaDoc<QName, Serializable JavaDoc>(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