1 17 package org.alfresco.repo.importer; 18 19 import java.io.BufferedReader ; 20 import java.io.BufferedWriter ; 21 import java.io.File ; 22 import java.io.FileReader ; 23 import java.io.FileWriter ; 24 import java.io.IOException ; 25 import java.io.Reader ; 26 import java.io.Writer ; 27 import java.util.List ; 28 29 import javax.transaction.UserTransaction ; 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 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 path) 90 { 91 this.path = path; 92 } 93 94 public void setStoreRef(String 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 userTransaction = null; 129 try 130 { 131 userTransaction = transactionService.getUserTransaction(); 132 userTransaction.begin(); 133 authenticationComponent.setSystemUserAsCurrentUser(); 134 if(clearAllChildren) 135 { 136 List <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 tempFile = TempFileProvider.createTempFile("ExportSourceImporter-", ".xml"); 147 Writer writer = new BufferedWriter (new FileWriter (tempFile)); 148 XMLWriter xmlWriter = createXMLExporter(writer); 149 exportSource.generateExport(xmlWriter); 150 xmlWriter.close(); 151 152 Reader reader = new BufferedReader (new FileReader (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 t) 162 { 163 try { if (userTransaction != null) {userTransaction.rollback();} } catch (Exception ex) {} 164 try {authenticationComponent.clearCurrentSecurityContext(); } catch (Exception ex) {} 165 throw new ExportSourceImporterException("Failed to import", t); 166 } 167 finally 168 { 169 authenticationComponent.clearCurrentSecurityContext(); 170 } 171 } 172 173 private XMLWriter createXMLExporter(Writer writer) 174 { 175 OutputFormat format = OutputFormat.createPrettyPrint(); 177 format.setNewLineAfterDeclaration(false); 178 format.setIndentSize(3); 179 format.setEncoding("UTF-8"); 180 181 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 getValue(String key) 196 { 197 return null; 198 } 199 200 public boolean allowReferenceWithinTransaction() 201 { 202 return false; 203 } 204 205 }; 206 207 } 208 | Popular Tags |