KickJava   Java API By Example, From Geeks To Geeks.

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


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.BufferedReader JavaDoc;
20 import java.io.BufferedWriter JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.FileReader JavaDoc;
23 import java.io.FileWriter JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.Reader JavaDoc;
26 import java.io.Writer JavaDoc;
27 import java.util.List JavaDoc;
28
29 import javax.transaction.UserTransaction JavaDoc;
30
31 import org.alfresco.error.AlfrescoRuntimeException;
32 import org.alfresco.repo.security.authentication.AuthenticationComponent;
33 import org.alfresco.service.cmr.repository.ChildAssociationRef;
34 import org.alfresco.service.cmr.repository.NodeRef;
35 import org.alfresco.service.cmr.repository.NodeService;
36 import org.alfresco.service.cmr.repository.StoreRef;
37 import org.alfresco.service.cmr.search.SearchService;
38 import org.alfresco.service.cmr.view.ImporterBinding;
39 import org.alfresco.service.cmr.view.ImporterService;
40 import org.alfresco.service.cmr.view.Location;
41 import org.alfresco.service.namespace.NamespacePrefixResolver;
42 import org.alfresco.service.transaction.TransactionService;
43 import org.alfresco.util.TempFileProvider;
44 import org.dom4j.io.OutputFormat;
45 import org.dom4j.io.XMLWriter;
46
47 public class ExportSourceImporter implements ImporterJobSPI
48 {
49     private ImporterService importerService;
50
51     private ExportSource exportSource;
52
53     private AuthenticationComponent authenticationComponent;
54     
55     private StoreRef storeRef;
56
57     private String JavaDoc path;
58     
59     private boolean clearAllChildren;
60     
61     private NodeService nodeService;
62     
63     private SearchService searchService;
64     
65     private NamespacePrefixResolver namespacePrefixResolver;
66     
67     private TransactionService transactionService;
68
69     public ExportSourceImporter()
70     {
71         super();
72     }
73
74     public void setImporterService(ImporterService importerService)
75     {
76         this.importerService = importerService;
77     }
78
79     public void setExportSource(ExportSource exportSource)
80     {
81         this.exportSource = exportSource;
82     }
83
84     public void setClearAllChildren(boolean clearAllChildren)
85     {
86         this.clearAllChildren = clearAllChildren;
87     }
88
89     public void setPath(String JavaDoc path)
90     {
91         this.path = path;
92     }
93
94     public void setStoreRef(String JavaDoc storeRef)
95     {
96         this.storeRef = new StoreRef(storeRef);
97     }
98     
99     public void setTransactionService(TransactionService transactionService)
100     {
101         this.transactionService = transactionService;
102     }
103
104     public void setNamespacePrefixResolver(NamespacePrefixResolver namespacePrefixResolver)
105     {
106         this.namespacePrefixResolver = namespacePrefixResolver;
107     }
108
109     public void setNodeService(NodeService nodeService)
110     {
111         this.nodeService = nodeService;
112     }
113     
114     
115
116     public void setAuthenticationComponent(AuthenticationComponent authenticationComponent)
117     {
118         this.authenticationComponent = authenticationComponent;
119     }
120
121     public void setSearchService(SearchService searchService)
122     {
123         this.searchService = searchService;
124     }
125
126     public void doImport()
127     {
128         UserTransaction JavaDoc userTransaction = null;
129         try
130         {
131             userTransaction = transactionService.getUserTransaction();
132             userTransaction.begin();
133             authenticationComponent.setSystemUserAsCurrentUser();
134             if(clearAllChildren)
135             {
136                 List JavaDoc<NodeRef> refs = searchService.selectNodes(nodeService.getRootNode(storeRef), path, null, namespacePrefixResolver, false);
137                 for(NodeRef ref: refs)
138                 {
139                     for(ChildAssociationRef car: nodeService.getChildAssocs(ref))
140                     {
141                         nodeService.deleteNode(car.getChildRef());
142                     }
143                 }
144             }
145             
146             File JavaDoc tempFile = TempFileProvider.createTempFile("ExportSourceImporter-", ".xml");
147             Writer JavaDoc writer = new BufferedWriter JavaDoc(new FileWriter JavaDoc(tempFile));
148             XMLWriter xmlWriter = createXMLExporter(writer);
149             exportSource.generateExport(xmlWriter);
150             xmlWriter.close();
151
152             Reader JavaDoc reader = new BufferedReader JavaDoc(new FileReader JavaDoc(tempFile));
153
154             Location location = new Location(storeRef);
155             location.setPath(path);
156
157             importerService.importView(reader, location, REPLACE_BINDING, null);
158             reader.close();
159             userTransaction.commit();
160         }
161         catch(Throwable JavaDoc t)
162         {
163             try { if (userTransaction != null) {userTransaction.rollback();} } catch (Exception JavaDoc ex) {}
164             try {authenticationComponent.clearCurrentSecurityContext(); } catch (Exception JavaDoc ex) {}
165             throw new ExportSourceImporterException("Failed to import", t);
166         }
167         finally
168         {
169             authenticationComponent.clearCurrentSecurityContext();
170         }
171     }
172
173     private XMLWriter createXMLExporter(Writer JavaDoc writer)
174     {
175         // Define output format
176
OutputFormat format = OutputFormat.createPrettyPrint();
177         format.setNewLineAfterDeclaration(false);
178         format.setIndentSize(3);
179         format.setEncoding("UTF-8");
180
181         // Construct an XML Exporter
182

183         XMLWriter xmlWriter = new XMLWriter(writer, format);
184         return xmlWriter;
185     }
186
187     private static ImporterBinding REPLACE_BINDING = new ImporterBinding()
188     {
189
190         public UUID_BINDING getUUIDBinding()
191         {
192             return UUID_BINDING.UPDATE_EXISTING;
193         }
194
195         public String JavaDoc getValue(String JavaDoc key)
196         {
197             return null;
198         }
199
200         public boolean allowReferenceWithinTransaction()
201         {
202             return false;
203         }
204
205     };
206
207 }
208
Popular Tags